Revize 1759
Přidáno uživatelem Ondřej Fibich před téměř 12 roky(ů)
freenetis/branches/testing/application/i18n/cs_CZ/texts.php | ||
---|---|---|
'error - cannot set filter query as default' => 'Chyba - nelze nastavit dotaz filtru jako výchozí.',
|
||
'error - cannot unset filter query as default' => 'Chyba - nelze zrušit dotaz filtru jako výchozí.',
|
||
'error - cannot update access control group' => 'Chyba - nelze aktualizovat přístupovou skupinu uživatelů',
|
||
'error - cannot update allowed subnets of member' => 'Chyba - nelze aktualizovat povolené podsítě člena.',
|
||
'error - cannot update count of allowed subnets' => 'Chyba - nelze aktualizovat počet povolených podsítí.',
|
||
'error - cannot update device admin' => 'Chyba - nelze aktualizovat správce zařízení.',
|
||
'error - cant add contacts' => 'Chyba - nelze přidat kontakt.',
|
freenetis/branches/testing/application/controllers/ip_addresses.php | ||
---|---|---|
) + ORM::factory('iface')->select_list_grouped_by_device($device->id);
|
||
|
||
$title = __('Add new IP address to device').' '.$device->name;
|
||
$link_back_url = 'devices/show/'.$device->id;
|
||
$linkback = 'devices/show/'.$device->id;
|
||
}
|
||
else
|
||
{
|
||
... | ... | |
);
|
||
|
||
$title = __('Add new IP address to interface').' '.strval($iface);
|
||
$link_back_url = 'ifaces/show/'.$device->id;
|
||
$linkback = 'ifaces/show/'.$iface->id;
|
||
}
|
||
|
||
}
|
||
... | ... | |
) + ORM::factory('iface')->select_list_grouped_by_device();
|
||
|
||
$title = __('Add new IP address');
|
||
$link_back_url = 'ip_addresses/show_all';
|
||
$linkback = 'ip_addresses/show_all';
|
||
}
|
||
|
||
$this->form = new Forge();
|
||
... | ... | |
{
|
||
$form_data = $this->form->as_array();
|
||
|
||
$ip = new Ip_address_Model();
|
||
$ip_address = new Ip_address_Model();
|
||
|
||
$ip->delete_ip_address_with_member($form_data['ip_address']);
|
||
// gets number of maximum of acceptable repeating of operation
|
||
// after reaching of deadlock and time of waiting between
|
||
// other attempt to make transaction (#254)
|
||
$transaction_attempt_counter = 0;
|
||
$max_attempts = max(1, abs(Settings::get('db_trans_deadlock_repeats_count')));
|
||
$timeout = abs(Settings::get('db_trans_deadlock_repeats_timeout'));
|
||
|
||
$ip->iface_id = $form_data['iface_id'];
|
||
$ip->ip_address = $form_data['ip_address'];
|
||
$ip->subnet_id = $form_data['subnet_id'];
|
||
$ip->gateway = $form_data['gateway'];
|
||
$ip->service = $form_data['service'];
|
||
$ip->whitelisted = Ip_address_Model::NO_WHITELIST;
|
||
$ip->member_id = NULL;
|
||
// try to delete
|
||
while (TRUE)
|
||
{
|
||
try // try to make DB transction
|
||
{
|
||
$ip_address->transaction_start();
|
||
$ip_address->delete_ip_address_with_member($form_data['ip_address']);
|
||
$ip_address->iface_id = $form_data['iface_id'];
|
||
$ip_address->ip_address = $form_data['ip_address'];
|
||
$ip_address->subnet_id = $form_data['subnet_id'];
|
||
$ip_address->gateway = $form_data['gateway'];
|
||
$ip_address->service = $form_data['service'];
|
||
$ip_address->whitelisted = Ip_address_Model::NO_WHITELIST;
|
||
$ip_address->member_id = NULL;
|
||
$ip_address->save_throwable();
|
||
$ip_address->transaction_commit();
|
||
|
||
unset($form_data);
|
||
try
|
||
{
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$ip_address->iface->device->user->member->id, array($ip_address->subnet_id)
|
||
);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
status::warning('Error - cannot update allowed subnets of member.');
|
||
}
|
||
|
||
if ($ip->save())
|
||
{
|
||
status::success('IP address is successfully saved.');
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$ip->iface->device->user->member->id, array($ip->subnet_id)
|
||
);
|
||
// redirect
|
||
status::success('IP address is successfully saved.');
|
||
$this->redirect($linkback);
|
||
}
|
||
catch (Exception $e) // failed => rollback and wait 100ms before next attempt
|
||
{
|
||
$ip_address->transaction_rollback();
|
||
|
||
if (++$transaction_attempt_counter >= $max_attempts) // this was last attempt?
|
||
{
|
||
Log::add_exception($e);
|
||
status::error('Error - cant add ip address.');
|
||
$this->redirect($linkback);
|
||
}
|
||
|
||
usleep($timeout);
|
||
}
|
||
}
|
||
|
||
$this->redirect($link_back_url);
|
||
}
|
||
|
||
$view = new View('main');
|
||
... | ... | |
{
|
||
$form_data = $this->form->as_array();
|
||
|
||
try
|
||
// gets number of maximum of acceptable repeating of operation
|
||
// after reaching of deadlock and time of waiting between
|
||
// other attempt to make transaction (#254)
|
||
$transaction_attempt_counter = 0;
|
||
$max_attempts = max(1, abs(Settings::get('db_trans_deadlock_repeats_count')));
|
||
$timeout = abs(Settings::get('db_trans_deadlock_repeats_timeout'));
|
||
|
||
// try to delete
|
||
while (TRUE)
|
||
{
|
||
$ip_address->transaction_start();
|
||
try // try to make DB transction
|
||
{
|
||
$ip_address->transaction_start();
|
||
|
||
$ip_address->delete_ip_address_with_member($form_data['ip_address']);
|
||
$ip_address->delete_ip_address_with_member($form_data['ip_address']);
|
||
|
||
$old_subnet_id = $ip_address->subnet_id;
|
||
$old_subnet_id = $ip_address->subnet_id;
|
||
|
||
$ip_address->iface_id = $form_data['iface_id'];
|
||
$ip_address->ip_address = $form_data['ip_address'];
|
||
$ip_address->subnet_id = $form_data['subnet_id'];
|
||
$ip_address->gateway = $form_data['gateway'];
|
||
$ip_address->service = $form_data['service'];
|
||
$ip_address->whitelisted = $form_data['whitelisted'];
|
||
$ip_address->member_id = NULL;
|
||
|
||
unset($form_data);
|
||
$ip_address->iface_id = $form_data['iface_id'];
|
||
$ip_address->ip_address = $form_data['ip_address'];
|
||
$ip_address->subnet_id = $form_data['subnet_id'];
|
||
$ip_address->gateway = $form_data['gateway'];
|
||
$ip_address->service = $form_data['service'];
|
||
$ip_address->whitelisted = $form_data['whitelisted'];
|
||
$ip_address->member_id = NULL;
|
||
|
||
$ip_address->save_throwable();
|
||
$ip_address->save_throwable();
|
||
|
||
$ip_address->transaction_commit();
|
||
|
||
$member_id = $device->user->member_id;
|
||
|
||
// this block of code cannot be in database transation otherwise
|
||
// you will get error
|
||
// subnet has been changed and ip address was the only one of this member
|
||
// from this subnet -> deletes subnet from allowed subnets of member
|
||
if ($old_subnet_id != $ip_address->subnet_id &&
|
||
!$ip_address->count_all_ip_addresses_by_member_and_subnet(
|
||
$member_id, $old_subnet_id
|
||
))
|
||
{
|
||
try
|
||
{
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$member_id, NULL, NULL,
|
||
array($old_subnet_id), TRUE
|
||
);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
status::warning('Error - cannot update allowed subnets of member.');
|
||
}
|
||
}
|
||
|
||
try
|
||
{
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$member_id, array($ip_address->subnet_id), array(),
|
||
array(), TRUE
|
||
);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
status::warning('Error - cannot update allowed subnets of member.');
|
||
}
|
||
|
||
$ip_address->transaction_commit();
|
||
|
||
$member_id = $device->user->member_id;
|
||
|
||
// this block of code cannot be in database transation otherwise
|
||
// you will get error
|
||
// subnet has been changed and ip address was the only one of this member
|
||
// from this subnet -> deletes subnet from allowed subnets of member
|
||
if ($old_subnet_id != $ip_address->subnet_id &&
|
||
!$ip_address->count_all_ip_addresses_by_member_and_subnet(
|
||
$member_id, $old_subnet_id
|
||
))
|
||
status::success('IP address has been successfully updated.');
|
||
$this->redirect('ip_addresses/show/'.$ip_address->id);
|
||
}
|
||
catch (Exception $e) // failed => rollback and wait 100ms before next attempt
|
||
{
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$member_id, NULL, NULL, array($old_subnet_id)
|
||
);
|
||
$ip_address->transaction_rollback();
|
||
|
||
if (++$transaction_attempt_counter >= $max_attempts) // this was last attempt?
|
||
{
|
||
Log::add_exception($e);
|
||
status::error('Error - Cannot update ip address.');
|
||
$this->redirect('ip_addresses/show/'.$ip_address->id);
|
||
}
|
||
|
||
usleep($timeout);
|
||
}
|
||
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$member_id, array($ip_address->subnet_id)
|
||
);
|
||
|
||
status::success('IP address has been successfully updated.');
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$ip_address->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Error - Cannot update ip address.');
|
||
}
|
||
|
||
$this->redirect('ip_addresses/show/'.$ip_address->id);
|
||
}
|
||
else
|
||
{
|
||
... | ... | |
Controller::error(ACCESS);
|
||
}
|
||
|
||
// success
|
||
if ($ip_address->delete())
|
||
// link back
|
||
$linkback = Path::instance()->previous();
|
||
|
||
if (url::slice(url_lang::uri($linkback), 0, 2) == 'ip_addresses/show')
|
||
{
|
||
$linkback = 'ip_addresses/show_all';
|
||
}
|
||
|
||
// gets number of maximum of acceptable repeating of operation
|
||
// after reaching of deadlock and time of waiting between
|
||
// other attempt to make transaction (#254)
|
||
$transaction_attempt_counter = 0;
|
||
$max_attempts = max(1, abs(Settings::get('db_trans_deadlock_repeats_count')));
|
||
$timeout = abs(Settings::get('db_trans_deadlock_repeats_timeout'));
|
||
|
||
// try to delete
|
||
while (TRUE)
|
||
{
|
||
try // try to make DB transction
|
||
{
|
||
$ip_address->transaction_start();
|
||
$ip_address->delete_throwable();
|
||
$ip_address->transaction_commit();
|
||
|
||
// ip address was the only one of this member
|
||
// from this subnet -> deletes subnet from allowed subnets of member
|
||
if (!$ip_address->count_all_ip_addresses_by_member_and_subnet(
|
||
$member_id, $subnet_id
|
||
))
|
||
{
|
||
try
|
||
{
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$member_id, NULL, NULL, array($subnet_id)
|
||
$member_id, NULL, NULL, array($subnet_id), TRUE
|
||
);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
status::warning('Error - cannot update allowed subnets of member.');
|
||
}
|
||
}
|
||
|
||
// redirect
|
||
status::success('IP address has been successfully deleted.');
|
||
$this->redirect($linkback);
|
||
}
|
||
else
|
||
catch (Exception $e) // failed => rollback and wait 100ms before next attempt
|
||
{
|
||
status::error('Error - cant delete ip address.');
|
||
}
|
||
$ip_address->transaction_rollback();
|
||
|
||
$linkback = Path::instance()->previous();
|
||
|
||
if (url::slice(url_lang::uri($linkback),0,2) == 'ip_addresses/show')
|
||
if (++$transaction_attempt_counter >= $max_attempts) // this was last attempt?
|
||
{
|
||
$linkback = 'ip_addresses/show_all';
|
||
Log::add_exception($e);
|
||
status::error('Error - cant delete ip address.');
|
||
$this->redirect($linkback);
|
||
}
|
||
|
||
$this->redirect($linkback);
|
||
usleep($timeout);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Checks validity of ip address.
|
freenetis/branches/testing/application/controllers/allowed_subnets.php | ||
---|---|---|
* @author Michal Kliment
|
||
* @param integer $member_id
|
||
* @param string | array $to_enable
|
||
* @return void
|
||
* @param string | array $to_disable
|
||
* @param string | array $to_remove
|
||
* @param boolean $thow_ex_on_error Trow an exception if an error occure?
|
||
* @return boolean State of operation.
|
||
*/
|
||
public static function update_enabled(
|
||
$member_id, $to_enable = array(), $to_disable = array(),
|
||
$to_remove = array())
|
||
$to_remove = array(), $thow_ex_on_error = FALSE)
|
||
{
|
||
// bad parameter
|
||
if (!$member_id)
|
||
... | ... | |
// finds all allowed subnet of member
|
||
$allowed_subnet_model = new Allowed_subnet_Model();
|
||
|
||
try
|
||
|
||
// gets number of maximum of acceptable repeating of operation
|
||
// after reaching of deadlock and time of waiting between
|
||
// other attempt to make transaction (#254)
|
||
$transaction_attempt_counter = 0;
|
||
$max_attempts = max(1, abs(Settings::get('db_trans_deadlock_repeats_count')));
|
||
$timeout = abs(Settings::get('db_trans_deadlock_repeats_timeout'));
|
||
|
||
// try to delete
|
||
while (TRUE)
|
||
{
|
||
$allowed_subnet_model->transaction_start();
|
||
|
||
$allowed_subnets = $allowed_subnet_model->where('member_id', $member->id)
|
||
->where('member_id', $member->id)
|
||
->orderby(array('enabled' => 'desc', 'last_update' => 'desc'))
|
||
->find_all();
|
||
try
|
||
{
|
||
$allowed_subnet_model->transaction_start();
|
||
|
||
$arr_subnets = array();
|
||
$allowed_subnets = $allowed_subnet_model->where('member_id', $member->id)
|
||
->where('member_id', $member->id)
|
||
->orderby(array('enabled' => 'desc', 'last_update' => 'desc'))
|
||
->find_all();
|
||
|
||
// to_enabled have the hightest priority
|
||
foreach ($allowed_subnets as $allowed_subnet)
|
||
{
|
||
if (in_array($allowed_subnet->subnet_id, $to_remove))
|
||
$arr_subnets = array();
|
||
|
||
// to_enabled have the hightest priority
|
||
foreach ($allowed_subnets as $allowed_subnet)
|
||
{
|
||
$allowed_subnet->delete();
|
||
continue;
|
||
if (in_array($allowed_subnet->subnet_id, $to_remove))
|
||
{
|
||
$allowed_subnet->delete_throwable();
|
||
continue;
|
||
}
|
||
|
||
if (!in_array($allowed_subnet->subnet_id, $to_enable) &&
|
||
!in_array($allowed_subnet->subnet_id, $to_disable))
|
||
{
|
||
$arr_subnets[] = $allowed_subnet->subnet_id;
|
||
}
|
||
}
|
||
|
||
if (!in_array($allowed_subnet->subnet_id, $to_enable) &&
|
||
!in_array($allowed_subnet->subnet_id, $to_disable))
|
||
$arr_subnets = arr::merge($to_enable, $arr_subnets, $to_disable);
|
||
|
||
// maximum count of allowed subnets (0 = unlimited)
|
||
$max_enabled = ($member->allowed_subnets_count->count) ?
|
||
$member->allowed_subnets_count->count : count($arr_subnets);
|
||
|
||
$enabled = 0;
|
||
foreach ($arr_subnets as $subnet)
|
||
{
|
||
$arr_subnets[] = $allowed_subnet->subnet_id;
|
||
if (!$subnet)
|
||
continue;
|
||
|
||
if ($aid = $allowed_subnet_model->exists($member->id, $subnet))
|
||
{
|
||
$allowed_subnet_model->where('id', $aid)->find();
|
||
}
|
||
else
|
||
{
|
||
$allowed_subnet_model->clear();
|
||
$allowed_subnet_model->member_id = $member->id;
|
||
$allowed_subnet_model->subnet_id = $subnet;
|
||
}
|
||
|
||
$allowed_subnet_model->enabled = ($enabled < $max_enabled);
|
||
|
||
$enabled++;
|
||
|
||
$allowed_subnet_model->save_throwable();
|
||
}
|
||
}
|
||
|
||
$arr_subnets = arr::merge($to_enable, $arr_subnets, $to_disable);
|
||
$allowed_subnet_model->transaction_commit();
|
||
|
||
// maximum count of allowed subnets (0 = unlimited)
|
||
$max_enabled = ($member->allowed_subnets_count->count) ?
|
||
$member->allowed_subnets_count->count : count($arr_subnets);
|
||
|
||
$enabled = 0;
|
||
foreach ($arr_subnets as $subnet)
|
||
return TRUE; // all OK
|
||
}
|
||
catch (Exception $e) // failed => rollback and wait 100ms before next attempt
|
||
{
|
||
if (!$subnet)
|
||
continue;
|
||
$allowed_subnet_model->transaction_rollback();
|
||
|
||
if ($aid = $allowed_subnet_model->exists($member->id, $subnet))
|
||
if (++$transaction_attempt_counter >= $max_attempts) // this was last attempt?
|
||
{
|
||
$allowed_subnet_model->where('id', $aid)->find();
|
||
Log::add_exception($e);
|
||
|
||
if ($thow_ex_on_error)
|
||
{
|
||
throw $e;
|
||
}
|
||
else
|
||
{
|
||
return FALSE;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
$allowed_subnet_model->clear();
|
||
$allowed_subnet_model->member_id = $member->id;
|
||
$allowed_subnet_model->subnet_id = $subnet;
|
||
}
|
||
|
||
$allowed_subnet_model->enabled = ($enabled < $max_enabled);
|
||
|
||
$enabled++;
|
||
|
||
$allowed_subnet_model->save();
|
||
usleep($timeout);
|
||
}
|
||
|
||
$allowed_subnet_model->transaction_commit();
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$allowed_subnet_model->transaction_rollback();
|
||
Log::add_exception($e);
|
||
}
|
||
}
|
||
|
||
}
|
freenetis/branches/testing/application/controllers/devices.php | ||
---|---|---|
$dm = new Device_Model();
|
||
|
||
$update_allowed_params = array();
|
||
|
||
try
|
||
|
||
// gets number of maximum of acceptable repeating of operation
|
||
// after reaching of deadlock and time of waiting between
|
||
// other attempt to make transaction (#254)
|
||
$transaction_attempt_counter = 0;
|
||
$max_attempts = max(1, abs(Settings::get('db_trans_deadlock_repeats_count')));
|
||
$timeout = abs(Settings::get('db_trans_deadlock_repeats_timeout'));
|
||
|
||
// try to delete
|
||
while (TRUE)
|
||
{
|
||
$dm->transaction_start();
|
||
|
||
// device //////////////////////////////////////////////////////
|
||
$dm->user_id = $form_data['user_id'];
|
||
|
||
if (!isset($user_id))
|
||
try
|
||
{
|
||
$um = new User_Model($dm->user_id);
|
||
}
|
||
$dm->transaction_start();
|
||
|
||
if (empty($form_data['device_name']))
|
||
{
|
||
$dm->name = $um->login.'_'.$types[$form_data['device_type']];
|
||
}
|
||
else
|
||
{
|
||
$dm->name = $form_data['device_name'];
|
||
}
|
||
|
||
$device_template = new Device_template_Model($form_data['device_template_id']);
|
||
// device //////////////////////////////////////////////////////
|
||
$dm->user_id = $form_data['user_id'];
|
||
|
||
if ($device_template && $device_template->id)
|
||
{
|
||
$dm->trade_name = $device_template->name;
|
||
}
|
||
|
||
$dm->type = $form_data['device_type'];
|
||
$dm->PPPoE_logging_in = $form_data['PPPoE_logging_in'];
|
||
if (!isset($user_id))
|
||
{
|
||
$um = new User_Model($dm->user_id);
|
||
}
|
||
|
||
if ($this->acl_check_new(get_class($this), 'login'))
|
||
{
|
||
$dm->login = $form_data['login'];
|
||
}
|
||
if (empty($form_data['device_name']))
|
||
{
|
||
$dm->name = $um->login.'_'.$types[$form_data['device_type']];
|
||
}
|
||
else
|
||
{
|
||
$dm->name = $form_data['device_name'];
|
||
}
|
||
|
||
if ($this->acl_check_new(get_class($this), 'password'))
|
||
{
|
||
$dm->password = $form_data['login_password'];
|
||
}
|
||
$device_template = new Device_template_Model($form_data['device_template_id']);
|
||
|
||
$dm->price = $form_data['price'];
|
||
$dm->payment_rate = $form_data['payment_rate'];
|
||
$dm->buy_date = date('Y-m-d', $form_data['buy_date']);
|
||
$dm->comment = $form_data['device_comment'];
|
||
if ($device_template && $device_template->id)
|
||
{
|
||
$dm->trade_name = $device_template->name;
|
||
}
|
||
|
||
// address point ///////////////////////////////////////////////////
|
||
$dm->type = $form_data['device_type'];
|
||
$dm->PPPoE_logging_in = $form_data['PPPoE_logging_in'];
|
||
|
||
// gps
|
||
$gpsx = NULL;
|
||
$gpsy = NULL;
|
||
|
||
if (!empty($form_data['gpsx']) && !empty($form_data['gpsy']))
|
||
{
|
||
$gpsx = doubleval($form_data['gpsx']);
|
||
$gpsy = doubleval($form_data['gpsy']);
|
||
|
||
if (gps::is_valid_degrees_coordinate($form_data['gpsx']))
|
||
if ($this->acl_check_new(get_class($this), 'login'))
|
||
{
|
||
$gpsx = gps::degrees2real($form_data['gpsx']);
|
||
$dm->login = $form_data['login'];
|
||
}
|
||
|
||
if (gps::is_valid_degrees_coordinate($form_data['gpsy']))
|
||
if ($this->acl_check_new(get_class($this), 'password'))
|
||
{
|
||
$gpsy = gps::degrees2real($form_data['gpsy']);
|
||
$dm->password = $form_data['login_password'];
|
||
}
|
||
}
|
||
|
||
$address_point_model = new Address_point_Model();
|
||
$dm->price = $form_data['price'];
|
||
$dm->payment_rate = $form_data['payment_rate'];
|
||
$dm->buy_date = date('Y-m-d', $form_data['buy_date']);
|
||
$dm->comment = $form_data['device_comment'];
|
||
|
||
$ap = $address_point_model->get_address_point(
|
||
$form_data['country_id'], $form_data['town_id'],
|
||
$form_data['street_id'], $form_data['street_number'],
|
||
$gpsx, $gpsy
|
||
);
|
||
// address point ///////////////////////////////////////////////////
|
||
|
||
// add address point if there is no such
|
||
if (!$ap->id)
|
||
{
|
||
$ap->save_throwable();
|
||
}
|
||
// gps
|
||
$gpsx = NULL;
|
||
$gpsy = NULL;
|
||
|
||
// add GPS
|
||
if (!empty($gpsx) && !empty($gpsy))
|
||
{ // save
|
||
$ap->update_gps_coordinates($ap->id, $gpsx, $gpsy);
|
||
}
|
||
else
|
||
{ // delete gps
|
||
$ap->gps = '';
|
||
$ap->save_throwable();
|
||
}
|
||
if (!empty($form_data['gpsx']) && !empty($form_data['gpsy']))
|
||
{
|
||
$gpsx = doubleval($form_data['gpsx']);
|
||
$gpsy = doubleval($form_data['gpsy']);
|
||
|
||
$dm->address_point_id = $ap->id;
|
||
$dm->save_throwable();
|
||
if (gps::is_valid_degrees_coordinate($form_data['gpsx']))
|
||
{
|
||
$gpsx = gps::degrees2real($form_data['gpsx']);
|
||
}
|
||
|
||
// device engineer ////////////////////////////////////////////
|
||
|
||
$device_engineer = new Device_engineer_Model();
|
||
$device_engineer->device_id = $dm->id;
|
||
$device_engineer->user_id = $form_data['first_engineer_id'];
|
||
$device_engineer->save();
|
||
|
||
// ifaces //////////////////////////////////////////////////////
|
||
if (gps::is_valid_degrees_coordinate($form_data['gpsy']))
|
||
{
|
||
$gpsy = gps::degrees2real($form_data['gpsy']);
|
||
}
|
||
}
|
||
|
||
$post_use = isset($_POST['use']) ? $_POST['use'] : array();
|
||
|
||
foreach ($post_use as $i => $v)
|
||
{
|
||
// skip not used
|
||
if ($v != 1)
|
||
$address_point_model = new Address_point_Model();
|
||
|
||
$ap = $address_point_model->get_address_point(
|
||
$form_data['country_id'], $form_data['town_id'],
|
||
$form_data['street_id'], $form_data['street_number'],
|
||
$gpsx, $gpsy
|
||
);
|
||
|
||
// add address point if there is no such
|
||
if (!$ap->id)
|
||
{
|
||
continue;
|
||
$ap->save_throwable();
|
||
}
|
||
// save iface
|
||
$im = new Iface_Model();
|
||
$im->device_id = $dm->id;
|
||
$im->name = htmlspecialchars($_POST['name'][$i]);
|
||
$im->comment = htmlspecialchars($_POST['comment'][$i]);
|
||
$im->type = intval($_POST['type'][$i]);
|
||
|
||
if ($im->type == Iface_Model::TYPE_PORT)
|
||
{
|
||
$im->number = intval($_POST['number'][$i]);
|
||
$im->port_mode = intval($_POST['port_mode'][$i]);
|
||
|
||
// add GPS
|
||
if (!empty($gpsx) && !empty($gpsy))
|
||
{ // save
|
||
$ap->update_gps_coordinates($ap->id, $gpsx, $gpsy);
|
||
}
|
||
else
|
||
{
|
||
$im->mac = htmlspecialchars($_POST['mac'][$i]);
|
||
{ // delete gps
|
||
$ap->gps = '';
|
||
$ap->save_throwable();
|
||
}
|
||
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
|
||
$dm->address_point_id = $ap->id;
|
||
$dm->save_throwable();
|
||
|
||
// device engineer ////////////////////////////////////////////
|
||
|
||
$device_engineer = new Device_engineer_Model();
|
||
$device_engineer->device_id = $dm->id;
|
||
$device_engineer->user_id = $form_data['first_engineer_id'];
|
||
$device_engineer->save();
|
||
|
||
// ifaces //////////////////////////////////////////////////////
|
||
|
||
$update_allowed_params = array();// reset
|
||
$post_use = isset($_POST['use']) ? $_POST['use'] : array();
|
||
|
||
foreach ($post_use as $i => $v)
|
||
{
|
||
$im->wireless_antenna = intval($_POST['wireless_antenna'][$i]);
|
||
$im->wireless_mode = intval($_POST['wireless_mode'][$i]);
|
||
}
|
||
|
||
// can autosave?
|
||
$autosave_may = TRUE;
|
||
|
||
if (isset($_POST['connected_iface'][$i]))
|
||
{
|
||
// restrict blank fields
|
||
if (!(Iface_Model::type_has_ip_address($im->type) &&
|
||
Iface_Model::type_has_mac_address($im->type) &&
|
||
empty($im->mac) && (
|
||
!isset($_POST['ip'][$i]) ||
|
||
empty($_POST['ip'][$i]) ||
|
||
!valid::ip_address($_POST['ip'][$i])
|
||
)))
|
||
// skip not used
|
||
if ($v != 1)
|
||
{
|
||
// connected iface
|
||
$im_connect_to = new Iface_Model($_POST['connected_iface'][$i]);
|
||
continue;
|
||
}
|
||
// save iface
|
||
$im = new Iface_Model();
|
||
$im->device_id = $dm->id;
|
||
$im->name = htmlspecialchars($_POST['name'][$i]);
|
||
$im->comment = htmlspecialchars($_POST['comment'][$i]);
|
||
$im->type = intval($_POST['type'][$i]);
|
||
|
||
// save link
|
||
if (Iface_Model::type_has_link($im->type) &&
|
||
$im_connect_to && $im_connect_to->id)
|
||
if ($im->type == Iface_Model::TYPE_PORT)
|
||
{
|
||
$im->number = intval($_POST['number'][$i]);
|
||
$im->port_mode = intval($_POST['port_mode'][$i]);
|
||
}
|
||
else
|
||
{
|
||
$im->mac = htmlspecialchars($_POST['mac'][$i]);
|
||
}
|
||
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
$im->wireless_antenna = intval($_POST['wireless_antenna'][$i]);
|
||
$im->wireless_mode = intval($_POST['wireless_mode'][$i]);
|
||
}
|
||
|
||
// can autosave?
|
||
$autosave_may = TRUE;
|
||
|
||
if (isset($_POST['connected_iface'][$i]))
|
||
{
|
||
// restrict blank fields
|
||
if (!(Iface_Model::type_has_ip_address($im->type) &&
|
||
Iface_Model::type_has_mac_address($im->type) &&
|
||
empty($im->mac) && (
|
||
!isset($_POST['ip'][$i]) ||
|
||
empty($_POST['ip'][$i]) ||
|
||
!valid::ip_address($_POST['ip'][$i])
|
||
)))
|
||
{
|
||
// disable autosave
|
||
$autosave_may = FALSE;
|
||
|
||
$roaming = new Link_Model();
|
||
$link_id = $_POST['link_id'][$i];
|
||
$roaming_id = $roaming->get_roaming();
|
||
$roaming = $roaming->find($roaming_id);
|
||
$name = $_POST['link_name'][$i];
|
||
$medium = $_POST['medium'][$i];
|
||
|
||
// don not connect to roaming
|
||
if ($link_id == $roaming_id)
|
||
// connected iface
|
||
$im_connect_to = new Iface_Model($_POST['connected_iface'][$i]);
|
||
|
||
// save link
|
||
if (Iface_Model::type_has_link($im->type) &&
|
||
$im_connect_to && $im_connect_to->id)
|
||
{
|
||
$link_id = NULL;
|
||
// fix name
|
||
if (trim($name) == trim($roaming->name))
|
||
// disable autosave
|
||
$autosave_may = FALSE;
|
||
|
||
$roaming = new Link_Model();
|
||
$link_id = $_POST['link_id'][$i];
|
||
$roaming_id = $roaming->get_roaming();
|
||
$roaming = $roaming->find($roaming_id);
|
||
$name = $_POST['link_name'][$i];
|
||
$medium = $_POST['medium'][$i];
|
||
|
||
// don not connect to roaming
|
||
if ($link_id == $roaming_id)
|
||
{
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
$link_id = NULL;
|
||
// fix name
|
||
if (trim($name) == trim($roaming->name))
|
||
{
|
||
$name = __('air') . ' ';
|
||
}
|
||
else
|
||
{
|
||
$name = __('cable') . ' ';
|
||
}
|
||
|
||
if ($im_connect_to->type == Iface_Model::TYPE_WIRELESS &&
|
||
$im_connect_to->wireless_mode == Iface_Model::WIRELESS_MODE_AP)
|
||
{
|
||
$name .= $im_connect_to->device->name;
|
||
$name .= ' - ' . $dm->name;
|
||
}
|
||
else
|
||
{
|
||
$name .= $dm->name . ' - ';
|
||
$name .= $im_connect_to->device->name;
|
||
}
|
||
|
||
// fix medium
|
||
if ($medium == Link_Model::MEDIUM_ROAMING)
|
||
{
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
$medium = Link_Model::MEDIUM_AIR;
|
||
$name = __('air') . ' ';
|
||
}
|
||
else
|
||
{
|
||
$medium = Link_Model::MEDIUM_CABLE;
|
||
$name = __('cable') . ' ';
|
||
}
|
||
|
||
if ($im_connect_to->type == Iface_Model::TYPE_WIRELESS &&
|
||
$im_connect_to->wireless_mode == Iface_Model::WIRELESS_MODE_AP)
|
||
{
|
||
$name .= $im_connect_to->device->name;
|
||
$name .= ' - ' . $dm->name;
|
||
}
|
||
else
|
||
{
|
||
$name .= $dm->name . ' - ';
|
||
$name .= $im_connect_to->device->name;
|
||
}
|
||
|
||
// fix medium
|
||
if ($medium == Link_Model::MEDIUM_ROAMING)
|
||
{
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
$medium = Link_Model::MEDIUM_AIR;
|
||
}
|
||
else
|
||
{
|
||
$medium = Link_Model::MEDIUM_CABLE;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
$lm = new Link_Model($link_id);
|
||
$lm->name = htmlspecialchars($name);
|
||
$lm->medium = intval($medium);
|
||
$lm->comment = htmlspecialchars($_POST['link_comment'][$i]);
|
||
$lm->bitrate = network::str2bytes($_POST['bitrate'][$i]);
|
||
$lm->duplex = ($_POST['duplex'][$i] == 1);
|
||
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
$lm->wireless_ssid = htmlspecialchars($_POST['wireless_ssid'][$i]);
|
||
$lm->wireless_norm = intval($_POST['wireless_norm'][$i]);
|
||
$lm->wireless_frequency = intval($_POST['wireless_frequency'][$i]);
|
||
$lm->wireless_channel = intval($_POST['wireless_channel'][$i]);
|
||
$lm->wireless_channel_width = intval($_POST['wireless_channel_width'][$i]);
|
||
$lm->wireless_polarization = intval($_POST['wireless_polarization'][$i]);
|
||
}
|
||
$lm = new Link_Model($link_id);
|
||
$lm->name = htmlspecialchars($name);
|
||
$lm->medium = intval($medium);
|
||
$lm->comment = htmlspecialchars($_POST['link_comment'][$i]);
|
||
$lm->bitrate = network::str2bytes($_POST['bitrate'][$i]);
|
||
$lm->duplex = ($_POST['duplex'][$i] == 1);
|
||
|
||
$lm->save_throwable();
|
||
|
||
// restrict count of connected devices to link
|
||
$max = Link_Model::get_max_ifaces_count($im->type);
|
||
|
||
if ($lm->id != $roaming_id &&
|
||
$max <= 2) // delete connected (port, eth)
|
||
{
|
||
foreach ($lm->ifaces as $i_del)
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
$i_del->link_id = null;
|
||
$i_del->save_throwable();
|
||
$lm->wireless_ssid = htmlspecialchars($_POST['wireless_ssid'][$i]);
|
||
$lm->wireless_norm = intval($_POST['wireless_norm'][$i]);
|
||
$lm->wireless_frequency = intval($_POST['wireless_frequency'][$i]);
|
||
$lm->wireless_channel = intval($_POST['wireless_channel'][$i]);
|
||
$lm->wireless_channel_width = intval($_POST['wireless_channel_width'][$i]);
|
||
$lm->wireless_polarization = intval($_POST['wireless_polarization'][$i]);
|
||
}
|
||
|
||
$lm->save_throwable();
|
||
|
||
// restrict count of connected devices to link
|
||
$max = Link_Model::get_max_ifaces_count($im->type);
|
||
|
||
if ($lm->id != $roaming_id &&
|
||
$max <= 2) // delete connected (port, eth)
|
||
{
|
||
foreach ($lm->ifaces as $i_del)
|
||
{
|
||
$i_del->link_id = null;
|
||
$i_del->save_throwable();
|
||
}
|
||
}
|
||
|
||
$im->link_id = $lm->id;
|
||
$im_connect_to->link_id = $lm->id;
|
||
$im_connect_to->save_throwable();
|
||
}
|
||
|
||
$im->link_id = $lm->id;
|
||
$im_connect_to->link_id = $lm->id;
|
||
$im_connect_to->save_throwable();
|
||
}
|
||
}
|
||
|
||
// autosave (add) link
|
||
if (isset($_POST['link_autosave'][$i]) &&
|
||
$_POST['link_autosave'][$i] && $autosave_may)
|
||
{
|
||
$lm = new Link_Model();
|
||
$lm->name = htmlspecialchars($_POST['link_name'][$i]);
|
||
$lm->medium = intval($_POST['medium'][$i]);
|
||
$lm->comment = htmlspecialchars($_POST['link_comment'][$i]);
|
||
$lm->bitrate = network::str2bytes($_POST['bitrate'][$i]);
|
||
$lm->duplex = ($_POST['duplex'][$i] == 1);
|
||
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
$lm->wireless_ssid = htmlspecialchars($_POST['wireless_ssid'][$i]);
|
||
$lm->wireless_norm = intval($_POST['wireless_norm'][$i]);
|
||
$lm->wireless_frequency = intval($_POST['wireless_frequency'][$i]);
|
||
$lm->wireless_channel = intval($_POST['wireless_channel'][$i]);
|
||
$lm->wireless_channel_width = intval($_POST['wireless_channel_width'][$i]);
|
||
$lm->wireless_polarization = intval($_POST['wireless_polarization'][$i]);
|
||
}
|
||
|
||
$lm->save_throwable();
|
||
$im->link_id = $lm->id;
|
||
}
|
||
|
||
$im->save_throwable();
|
||
|
||
if (isset($_POST['ip'][$i]) && valid::ip_address($_POST['ip'][$i]))
|
||
{
|
||
// save IP address
|
||
$ipm = new Ip_address_Model();
|
||
$ipm->iface_id = $im->id;
|
||
$ipm->subnet_id = intval($_POST['subnet'][$i]);
|
||
$ipm->member_id = NULL;
|
||
$ipm->ip_address = htmlspecialchars($_POST['ip'][$i]);
|
||
$ipm->dhcp = ($_POST['dhcp'][$i] == 1);
|
||
$ipm->gateway = ($_POST['gateway'][$i] == 1);
|
||
$ipm->service = ($_POST['service'][$i] == 1);
|
||
$ipm->save_throwable();
|
||
|
||
// allowed subnet to added IP
|
||
$update_allowed_params[] = array
|
||
(
|
||
'member_id' => $dm->user->member_id,
|
||
'to_enable' => array($ipm->subnet_id)
|
||
);
|
||
}
|
||
}
|
||
|
||
// done
|
||
$dm->transaction_commit();
|
||
|
||
//Update allowed subnets after transaction is successfully commited
|
||
$error_added = TRUE; // throw error?
|
||
|
||
// autosave (add) link
|
||
if (isset($_POST['link_autosave'][$i]) &&
|
||
$_POST['link_autosave'][$i] && $autosave_may)
|
||
foreach ($update_allowed_params as $params)
|
||
{
|
||
$lm = new Link_Model();
|
||
$lm->name = htmlspecialchars($_POST['link_name'][$i]);
|
||
$lm->medium = intval($_POST['medium'][$i]);
|
||
$lm->comment = htmlspecialchars($_POST['link_comment'][$i]);
|
||
$lm->bitrate = network::str2bytes($_POST['bitrate'][$i]);
|
||
$lm->duplex = ($_POST['duplex'][$i] == 1);
|
||
|
||
if ($im->type == Iface_Model::TYPE_WIRELESS)
|
||
try
|
||
{
|
||
$lm->wireless_ssid = htmlspecialchars($_POST['wireless_ssid'][$i]);
|
||
$lm->wireless_norm = intval($_POST['wireless_norm'][$i]);
|
||
$lm->wireless_frequency = intval($_POST['wireless_frequency'][$i]);
|
||
$lm->wireless_channel = intval($_POST['wireless_channel'][$i]);
|
||
$lm->wireless_channel_width = intval($_POST['wireless_channel_width'][$i]);
|
||
$lm->wireless_polarization = intval($_POST['wireless_polarization'][$i]);
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$params['member_id'], $params['to_enable'],
|
||
array(), array(), $error_added
|
||
);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$error_added = FALSE;
|
||
status::warning('Error - cannot update allowed subnets of member.');
|
||
}
|
||
}
|
||
|
||
$lm->save_throwable();
|
||
$im->link_id = $lm->id;
|
||
}
|
||
status::success('Device has been successfully saved.');
|
||
url::redirect('devices/show/'.$dm->id);
|
||
}
|
||
catch (Exception $e) // failed => rollback and wait 100ms before next attempt
|
||
{
|
||
$dm->transaction_rollback();
|
||
|
||
$im->save_throwable();
|
||
|
||
if (isset($_POST['ip'][$i]) && valid::ip_address($_POST['ip'][$i]))
|
||
if (++$transaction_attempt_counter >= $max_attempts) // this was last attempt?
|
||
{
|
||
// save IP address
|
||
$ipm = new Ip_address_Model();
|
||
$ipm->iface_id = $im->id;
|
||
$ipm->subnet_id = intval($_POST['subnet'][$i]);
|
||
$ipm->member_id = NULL;
|
||
$ipm->ip_address = htmlspecialchars($_POST['ip'][$i]);
|
||
$ipm->dhcp = ($_POST['dhcp'][$i] == 1);
|
||
$ipm->gateway = ($_POST['gateway'][$i] == 1);
|
||
$ipm->service = ($_POST['service'][$i] == 1);
|
||
$ipm->save_throwable();
|
||
|
||
// allowed subnet to added IP
|
||
$update_allowed_params[] = array
|
||
(
|
||
'member_id' => $dm->user->member_id,
|
||
'to_enable' => array($ipm->subnet_id)
|
||
);
|
||
Log::add_exception($e);
|
||
status::error('Device has not been successfully saved.');
|
||
break;
|
||
}
|
||
}
|
||
|
||
// done
|
||
unset($form_data);
|
||
$dm->transaction_commit();
|
||
|
||
//Update allowed subnets after transaction is successfully commited
|
||
foreach ($update_allowed_params as $params)
|
||
{
|
||
Allowed_subnets_Controller::update_enabled(
|
||
$params['member_id'],
|
||
$params['to_enable']
|
||
);
|
||
usleep($timeout);
|
||
}
|
||
|
||
status::success('Device has been successfully saved.');
|
||
url::redirect('devices/show/'.$dm->id);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$dm->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Device has not been successfully saved.');
|
||
}
|
||
}
|
||
|
||
if (isset($user_id))
|
||
... | ... | |
$linkback = 'devices/show_all';
|
||
}
|
||
|
||
if ($device->delete())
|
||
// gets number of maximum of acceptable repeating of operation
|
||
// after reaching of deadlock and time of waiting between
|
||
// other attempt to make transaction (#254)
|
||
$transaction_attempt_counter = 0;
|
||
$max_attempts = max(1, abs(Settings::get('db_trans_deadlock_repeats_count')));
|
||
$timeout = abs(Settings::get('db_trans_deadlock_repeats_timeout'));
|
||
|
||
// try to delete
|
||
while (TRUE)
|
||
{
|
||
Allowed_subnets_Controller::update_enabled($mid, NULL, NULL, $subnets);
|
||
status::success('Device has been successfully deleted.');
|
||
}
|
||
else
|
||
{
|
||
status::error('Error - cant delete device.');
|
||
}
|
||
|
||
// redirect
|
||
url::redirect($linkback);
|
||
try // try to make DB transction
|
||
{
|
||
$device->transaction_start();
|
||
$device->delete_throwable();
|
||
$device->transaction_commit();
|
||
|
||
try
|
||
{
|
||
Allowed_subnets_Controller::update_enabled($mid, NULL, NULL, $subnets, TRUE);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
status::warning('Error - cannot update allowed subnets of member.');
|
||
}
|
||
|
||
// redirect
|
||
status::success('Device has been successfully deleted.');
|
||
$this->redirect($linkback);
|
||
}
|
||
catch (Exception $e) // failed => rollback and wait 100ms before next attempt
|
||
{
|
||
$device->transaction_rollback();
|
||
|
||
if (++$transaction_attempt_counter >= $max_attempts) // this was last attempt?
|
||
{
|
||
Log::add_exception($e);
|
||
status::error('Error - cant delete device.');
|
||
$this->redirect($linkback);
|
||
}
|
||
|
||
usleep($timeout);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
freenetis/branches/testing/application/libraries/Settings.php | ||
---|---|---|
'allowed_subnets_update_interval' => 60,
|
||
// default count of allowed subnets
|
||
'allowed_subnets_default_count' => 1,
|
||
// if a deadlock came in, whole transaction may be executed again
|
||
// this property sets the max count of repeats of execution. (#284)
|
||
'db_trans_deadlock_repeats_count' => 4,
|
||
// if repeats (previous variable) are set as greater than 1, this
|
||
// timeout in ms defines time to next repeat of execution. (#284)
|
||
'db_trans_deadlock_repeats_timeout' => 100,
|
||
// default value for prefix of subject of notification
|
||
// e-mails to members
|
||
'email_subject_prefix' => 'FreenetIS',
|
Také k dispozici: Unified diff
Opravy:
- pokus o opetovnou opravu #254 pomoci opakovani selhane transakce (merge z 1.1)