Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 474

Přidáno uživatelem Michal Kliment před asi 15 roky(ů)

Mensi zmeny struktury db - pridan sloupec segment_id do tabulky subnetu => konecne je zde tak dlouho hledany vztah subnetu x segmentu. Pridan sloupec user_id do tabulky clenu pro potrebu evidence uzivatele, ktery clena zapise do freenetisu. Pocatky praci na synchronizaci freenetisu s OS routeru, zatim pouze RouterOS od Mikrotik.

Zobrazit rozdíly:

freenetis/trunk/kohana/application/controllers/devices.php
$grid->add_new_button(url_lang::base().'devices/add_whole', url_lang::lang('texts.Add new whole device'));
if ($this->acl_check_new(get_class($this),'devices'))
$grid->add_new_button(url_lang::base().'devices/add', url_lang::lang('texts.Add new device'));
if ($this->acl_check_new(get_class($this),'devices'))
$grid->add_new_button(url_lang::base().'devices/import_mikrotik_export', url_lang::lang('texts.Import from Mikrotik export'));
$grid->order_field('id')->label('ID')->class('center');
$grid->order_field('name')->label(url_lang::lang('texts.name'));
$grid->order_field('type')->label(url_lang::lang('texts.type'));
......
// adding interface
$form->group('')->label(url_lang::lang('texts.Interface'));
$form->input('mac')->label(url_lang::lang('texts.MAC').':')->rules('required|length[3,250]');
$form->input('mac')->label(url_lang::lang('texts.MAC').':')->rules('required|length[3,250]');
$form->dropdown('segment_id')->label(url_lang::lang('texts.Segment name').':')->options($arr_segments)->rules('required');
// wireless settings of interface
$form->checkbox('add_wireless')->label(url_lang::lang('texts.Save wireless setting'))->value(1);
......
}
url::redirect($linkback);
}
function import_mikrotik_export()
{
// if form is posted
if ($_FILES)
{
$form = new Validation($_FILES);
$form->add_rules('file', 'upload::valid', 'upload::size[1M]');
if ($form->validate())
{
$filename = upload::save('file');
$parser = new Parser_Mikrotik();
$parser->header_end = '/ip dhcp-server lease';
$parser->set_callback(array(get_class($this),'synchronize_device'));
$parser->parse($filename);
die();
}
}
$view = new View('main');
$view->title = url_lang::lang('texts.Import from Mikrotik export');
$view->content = new View('devices_import');
$view->render(TRUE);
}
function synchronize_device($device)
{
$member_id = Devices_Controller::get_ID_from_comment($device->comment);
$device_model = new Device_Model();
$iface_model = new Iface_Model();
$ip_address_model = new Ip_address_Model();
$user_model = new User_Model();
$subnet_model = new Subnet_Model();
if ($member_id)
{
$member = new Member_Model($member_id);
$user = $user_model->where('type',User_model::$member)->find_by_member_id($member->id);
$ip_address = $ip_address_model->where('ip_address',$device->address)->find();
if ($ip_address->id)
{
if ($device->mac_address != $ip_address->iface->mac)
{
$ip_address->iface->mac = $device->mac_address;
$ip_address->save();
}
if ($user->id != $ip_address->iface->device->user_id)
{
$ip_address->iface->device->user_id = $user->id;
$ip_address->save();
}
}
else
{
$iface = $iface_model->where('mac',$device->mac_address)->find();
if ($iface->id)
{
$ip_addresses = $iface->find_related_ip_addresses();
$ip_address = $ip_addresses->current();
if (!$ip_address->id)
$ip_address->clear();
$ip = ip2long($device->address);
$subnets = $subnet_model
->select("id","name","network_address as net_str",
"inet_aton(network_address) as net",
"32-log2((~inet_aton(netmask) & 0xffffffff) + 1) as mask")
->find_all();
$arr_subnets = array();
foreach($subnets as $subnet)
{
$mask = 0xffffffff << (32-$subnet->mask) & 0xffffffff;
if (($ip & $mask) == $subnet->net)
$arr_subnets[] = $subnet;
}
// remote ip address belongs to some subnet
$subnet_id = count($arr_subnets) ? $arr_subnets[0]->id : NULL;
$ip_address->subnet_id = $subnet_id;
$ip_address->ip_address = $device->address;
$ip_address->iface_id = $iface->id;
if ($user->id != $ip_address->iface->device->user_id)
{
$ip_address->iface->device->user_id = $user->id;
$ip_address->save();
}
}
}
}
else
{
echo "Clen nenalezen - (".$device->comment.")<br />";
}
}
public static function get_ID_from_comment($comment = '')
{
$comment = str_replace(array('_','-','ID'),' ',trim($comment));
$comment = trim(preg_replace('/[ ]{2,}/',' ',$comment));
$words = explode(' ',$comment);
return (int) $words[0];
}
function get_device_names()
{
freenetis/trunk/kohana/application/upgrade_sql/upgrade_sql_470.php
<?php
// array of sql queries that upgrade database
$upgrade_sql[470] = array(
"ALTER TABLE `transfers` ADD `type` TINYINT NULL DEFAULT NULL AFTER `user_id`",
"ALTER TABLE `transfers` CHANGE `user_id` `user_id` INT( 11 ) NULL DEFAULT NULL COMMENT 'id of user, who added transfer'",
"ALTER TABLE `ip_addresses` ADD `gateway` TINYINT NULL DEFAULT NULL AFTER `dhcp` ",
"INSERT INTO `axo_map` (
`acl_id` ,
`section_value` ,
`value`
)
VALUES (
'51', 'Members_Controller', 'organization_id'
), (
'51', 'Members_Controller', 'registration'
), (
'59', 'Users_Controller', 'phone'
), (
'59', 'Users_Controller', 'email'
), (
'56', 'Accounts_Controller', 'unidentified_transfers'
)
;
",
"UPDATE `aro_groups` SET `name` = 'All' WHERE `aro_groups`.`id` = 21 AND `aro_groups`.`value` = 'all' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Regular members', `value` = 'regular_members' WHERE `aro_groups`.`id` = 22 AND `aro_groups`.`value` = 'radni_clenove' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Registered applicants', `value` = 'registered_applicants' WHERE `aro_groups`.`id` = 23 AND `aro_groups`.`value` = 'registrovani_zajemci' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Auditing comittee', `value` = 'auditing_comittee' WHERE `aro_groups`.`id` = 24 AND `aro_groups`.`value` = 'revizni_komise' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Executive counsil', `value` = 'executive_counsil' WHERE `aro_groups`.`id` = 25 AND `aro_groups`.`value` = 'spravni_rada' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Engineers', `value` = 'engineers' WHERE `aro_groups`.`id` = 26 AND `aro_groups`.`value` = 'technici' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Chairman and agent', `value` = 'chairman_and_agent' WHERE `aro_groups`.`id` = 28 AND `aro_groups`.`value` = 'predseda_jednatel' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Certified engineers', `value` = 'certified_engineers' WHERE `aro_groups`.`id` = 29 AND `aro_groups`.`value` = 'certifikovani_technici' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'System administrators', `value` = 'admins' WHERE `aro_groups`.`id` = 32 AND `aro_groups`.`value` = 'admins' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Users of regular members', `value` = 'users' WHERE `aro_groups`.`id` = 33 AND `aro_groups`.`value` = 'uzivatele' LIMIT 1;",
"ALTER TABLE `devices` DROP `GPS` ,
DROP `location_address` ,
DROP `location_details` ;",
"INSERT INTO `aco_map` (
`acl_id` ,
`section_value` ,
`value`
)
VALUES (
'55', 'menu', 'unconfirmed_works'
), (
'55', 'menu', 'unidentified_transfers'
);"
);
?>
freenetis/trunk/kohana/application/upgrade_sql/upgrade_sql.php
// array of sql queries that upgrade database
$upgrade_sql[get_SVN_rev()] = array(
"ALTER TABLE `transfers` ADD `type` TINYINT NULL DEFAULT NULL AFTER `user_id`",
"ALTER TABLE `transfers` CHANGE `user_id` `user_id` INT( 11 ) NULL DEFAULT NULL COMMENT 'id of user, who added transfer'",
"ALTER TABLE `ip_addresses` ADD `gateway` TINYINT NULL DEFAULT NULL AFTER `dhcp` ",
"ALTER TABLE `subnets` ADD `segment_id` INT( 11 ) NOT NULL AFTER `id`",
"INSERT INTO `axo_map` (
`acl_id` ,
`section_value` ,
`value`
)
VALUES (
'51', 'Members_Controller', 'organization_id'
), (
'51', 'Members_Controller', 'registration'
), (
'59', 'Users_Controller', 'phone'
), (
'59', 'Users_Controller', 'email'
), (
'56', 'Accounts_Controller', 'unidentified_transfers'
)
;
",
"UPDATE subnets, segments SET subnets.segment_id = segments.id WHERE segments.name = subnets.name",
"UPDATE `aro_groups` SET `name` = 'All' WHERE `aro_groups`.`id` = 21 AND `aro_groups`.`value` = 'all' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Regular members', `value` = 'regular_members' WHERE `aro_groups`.`id` = 22 AND `aro_groups`.`value` = 'radni_clenove' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Registered applicants', `value` = 'registered_applicants' WHERE `aro_groups`.`id` = 23 AND `aro_groups`.`value` = 'registrovani_zajemci' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Auditing comittee', `value` = 'auditing_comittee' WHERE `aro_groups`.`id` = 24 AND `aro_groups`.`value` = 'revizni_komise' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Executive counsil', `value` = 'executive_counsil' WHERE `aro_groups`.`id` = 25 AND `aro_groups`.`value` = 'spravni_rada' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Engineers', `value` = 'engineers' WHERE `aro_groups`.`id` = 26 AND `aro_groups`.`value` = 'technici' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Chairman and agent', `value` = 'chairman_and_agent' WHERE `aro_groups`.`id` = 28 AND `aro_groups`.`value` = 'predseda_jednatel' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Certified engineers', `value` = 'certified_engineers' WHERE `aro_groups`.`id` = 29 AND `aro_groups`.`value` = 'certifikovani_technici' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'System administrators', `value` = 'admins' WHERE `aro_groups`.`id` = 32 AND `aro_groups`.`value` = 'admins' LIMIT 1;",
"UPDATE `aro_groups` SET `name` = 'Users of regular members', `value` = 'users' WHERE `aro_groups`.`id` = 33 AND `aro_groups`.`value` = 'uzivatele' LIMIT 1;",
"ALTER TABLE `devices` DROP `GPS` ,
DROP `location_address` ,
DROP `location_details` ;",
"INSERT INTO `aco_map` (
`acl_id` ,
`section_value` ,
`value`
)
VALUES (
'55', 'menu', 'unconfirmed_works'
), (
'55', 'menu', 'unidentified_transfers'
);"
"ALTER TABLE `members` ADD `user_id` INT( 11 ) NULL COMMENT 'id of user who added member' AFTER `id`"
);
?>
freenetis/trunk/kohana/application/libraries/Parser_Mikrotik.php
<?php
class Parser_Mikrotik
{
public $header_end;
private $result;
private $callback;
private function open($filename = '')
{
if ($filename == '' || !file_exists($filename)) return false;
$this->lines = file($filename);
if (!$this->lines) return false;
return true;
}
private function repaire_lines()
{
$lines = array();
$offset = 0;
foreach ($this->lines as $line_nr => $line)
{
if (substr(trim($line),strlen(trim($line))-1,1)!="\\")
{
if (isset($lines[$line_nr - $offset]))
$lines[$line_nr - $offset] .= trim($line);
else
$lines[$line_nr - $offset] = trim($line);
}
else
{
if (isset($lines[$line_nr - $offset]))
$lines[$line_nr - $offset] .= substr(trim($line),0,strlen(trim($line))-1);
else
$lines[$line_nr - $offset] = substr(trim($line),0,strlen(trim($line))-1);
$offset++;
}
}
$this->lines = $lines;
}
private function remove_header()
{
$clause = true;
$line_nr = 0;
while($clause && $line_nr<count($this->lines))
{
if ($this->lines[$line_nr] == $this->header_end) $clause = false;
$line_nr++;
}
if ($clause) return true;
else
{
for ($i=0;$i<$line_nr;$i++)
{
array_shift($this->lines);
}
return true;
}
}
public function set_callback($function) {
$this->callback = $function;
$this->result = new stdClass();
}
private function remove_quotes($value = '')
{
return str_replace('"','',$value);
}
private function remove_dashes($value = '')
{
return str_replace('-','_',$value);
}
private function get_data_from_lines()
{
foreach ($this->lines as $line_nr => $line)
{
$data = explode(" ",$line);
$str = '';
foreach ($data as $segment)
{
if (!strpos($segment,"="))
{
if ($str == '') continue;
else
{
$str .= ' '. $segment;
if (strpos($segment,'"'))
{
$segment = $str;
$str = '';
}
}
}
else
{
if (strpos($segment,'"'))
{
$str = $segment;
}
else $str = '';
}
if ($str == '')
{
list($key,$value) = explode("=",$segment,2);
$key = $this->remove_dashes($key);
$result->$key = $this->remove_quotes($value);
}
}
$result->id = $line_nr;
if (isset($this->callback)) call_user_func($this->callback, $result);
}
}
public function parse($filename)
{
if (!$this->open($filename)) die('Nepodarilo se otevrit soubor!');
$this->repaire_lines();
if (isset($this->header_end)) $this->remove_header();
$this->get_data_from_lines();
}
}
?>
freenetis/trunk/kohana/application/views/devices_import.php
<h2><?php echo url_lang::lang('texts.Import from mikrotik export') ?></h2><br />
<?php echo html::anchor(url_lang::base().'devices/show_all',url_lang::lang('texts.Back to list of all devices')) ?>
<br /><br />
<?php echo form::open_multipart(url_lang::base().'devices/import_mikrotik_export') ?>
<table cellspacing="3" class="form">
<tr>
<th><?php echo form::label('file',url_lang::lang('texts.File').':') ?></th>
<td><?php echo form::upload('file', ''); ?></td>
</tr>
<tr>
<th></th>
<td><?php echo form::submit('submit', url_lang::lang('texts.Import'), ' class=submit') ?></td>
</tr>
</table>
<?php echo form::close() ?>

Také k dispozici: Unified diff