Esempio n. 1
0
 /**
  * Display firewall code
  *
  * @package     las
  * @version     1.0
  */
 public static function display($path, $vars = [])
 {
     $di = \Phalcon\DI::getDefault();
     if ($di->getShared('router')->getModuleName() == 'cli') {
         $view = $di->getShared('view');
     } else {
         $view = new \Phalcon\Mvc\View();
         $view->setDI($di);
         $view->registerEngines(\Las\Library\Tool::registerEngines($view, $di));
     }
     $settings = json_decode(json_encode(\Las\Library\Arr::from_model(Settings::find('status=' . Settings::ACTIVE), 'name', 'value')));
     $lans = Networks::find('status = ' . Networks::ACTIVE . ' AND type = ' . Networks::LAN);
     $wans = Networks::find('status = ' . Networks::ACTIVE . ' AND type = ' . Networks::WAN);
     $vars = ['clients' => Clients::find(), 'devices' => Devices::find('status=' . Devices::ACTIVE), 'messages' => Messages::find('status=' . Messages::ACTIVE), 'tariffs' => Tariffs::find('status=' . Tariffs::ACTIVE), 'settings' => $settings, 'redirects' => Redirects::find('status=' . Redirects::ACTIVE), 'services' => Services::find('status=' . Services::ACTIVE . ' AND client_id=0 AND device_id=0'), 'lans' => $lans, 'lan' => $lans->getFirst(), 'wans' => $wans, 'wan' => $wans->getFirst(), 'ipt' => $settings->iptables, 'tc' => $settings->tc, 'EOL' => PHP_EOL];
     $view->setViewsDir(ROOT_PATH . '/app/common/cache/volt/app/cli/views/');
     ob_start();
     $view->partial($path, $vars);
     return preg_replace(['/^\\s+|^[\\t\\s]*\\n+/m', "/\r/"], '', ob_get_clean());
     //return preg_replace('/^\s+|^[\t\s]*\n+/m', "/\x0D/"], '', $view->partial($path, $vars, false));
 }
Esempio n. 2
0
 /**
  * Edit action - edit the device
  *
  * @package     las
  * @version     1.0
  */
 public function editAction()
 {
     // Get id from url params and check if record exist
     $params = $this->router->getParams();
     if (isset($params[0]) && ($device = Devices::findFirst($params[0]))) {
         $clients = Clients::find(['status!=:status:', 'bind' => ['status' => Clients::UNACTIVE]]);
         $networks = Networks::find(['status=:status:', 'bind' => ['status' => Networks::ACTIVE]]);
         if (!count($clients)) {
             $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the client first") . ': ' . $this->tag->linkTo('admin/clients/add', __('Add')));
         }
         if (!count($networks)) {
             $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the network first") . ': ' . $this->tag->linkTo('admin/networks/add', __('Add')));
         }
         // Set title, pick view and send variables
         $this->tag->setTitle(__('Devices') . ' / ' . __('Edit'));
         $this->view->pick('devices/write');
         $this->view->setVars(['clients' => $clients, 'networks' => $networks, 'type' => Devices::type(true), 'status' => Devices::status(true)]);
         // Check if the form has been sent
         if ($this->request->isPost() === true && $this->request->hasPost('submit')) {
             $device->__set('clients', $clients);
             $device->__set('networks', $networks);
             $valid = $device->write('update');
             // Check if data are valid
             if ($valid instanceof Devices) {
                 $this->flashSession->success($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Success') . '!</strong> ' . __("The data has been saved."));
             } else {
                 $this->view->setVar('errors', $valid);
                 $this->flashSession->warning($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Warning') . '!</strong> ' . __("Please correct the errors."));
             }
         } else {
             // Values to fill out the form
             $this->tag->setDefaults(['name' => $device->name, 'network' => $device->network_id, 'client' => $device->client_id, 'type' => $device->type, 'IP' => long2ip($device->IP), 'MAC' => $device->MAC, 'description' => $device->description, 'status' => $device->status]);
         }
     } else {
         parent::notFoundAction();
     }
 }
Esempio n. 3
0
 /**
  * Edit action - edit the network
  *
  * @package     las
  * @version     1.0
  */
 public function editAction()
 {
     // Get id from url params and check if record exist
     $params = $this->router->getParams();
     if (isset($params[0]) && ($network = Networks::findFirst($params[0]))) {
         // Set title, pick view and send variables
         $this->tag->setTitle(__('Networks') . ' / ' . __('Edit'));
         $this->view->pick('networks/write');
         $this->view->setVars(['type' => Networks::type(true), 'status' => Networks::status(true), 'bitRate' => Settings::options('bitRate', $this->las['general']['bitRate'])]);
         // Check if the form has been sent
         if ($this->request->isPost() === true && $this->request->hasPost('submit')) {
             $valid = $network->write('update');
             // Check if data are valid
             if ($valid instanceof Networks) {
                 $this->flashSession->success($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Success') . '!</strong> ' . __("The data has been saved."));
             } else {
                 $this->view->setVar('errors', $valid);
                 $this->flashSession->warning($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Warning') . '!</strong> ' . __("Please correct the errors."));
             }
         } else {
             $diff = ['subnetwork' => long2ip($network->subnetwork), 'IP' => long2ip($network->IP), 'gateway' => long2ip($network->gateway)];
             // Values to fill out the form
             $this->tag->setDefaults(array_merge(get_object_vars($network), $diff));
         }
     } else {
         parent::notFoundAction();
     }
 }
Esempio n. 4
0
 /**
  * Write method - add/edit the device
  *
  * @package     las
  * @version     1.0
  *
  * @param string $method type: create/update
  * @return mixed
  */
 public function write($method = 'create')
 {
     $validation = new Extension\Validation();
     $this->getDI()->getShared('filter')->add('mac', function ($mac) {
         return strtoupper(str_replace('-', ':', $mac));
     });
     $validation->add('name', new Validator\PresenceOf());
     $validation->add('name', new Validator\StringLength(['min' => 3, 'max' => 32]));
     $validation->add('name', new Validator\Regex(['pattern' => '/([A-Z][A-Z0-9_-]{2,})/']));
     $validation->add('network', new Validator\PresenceOf());
     $validation->add('network', new Validator\InclusionIn(['domain' => Arr::from_model($this->networks, null, 'id')]));
     $validation->add('client', new Validator\PresenceOf());
     $validation->add('client', new Validator\InclusionIn(['domain' => Arr::from_model($this->clients, null, 'id')]));
     $validation->add('type', new Validator\PresenceOf());
     $validation->add('type', new Validator\InclusionIn(['domain' => Devices::type()]));
     $validation->add('IP', new Validator\PresenceOf());
     $validation->add('IP', new Extension\Ip(['value' => $this->request->getPost('IP')]));
     $validation->add('IP', new Validator\ExclusionIn(['domain' => Arr::from_model($this->networks, null, 'IP'), 'message' => __('Field :field is reserved')]));
     $validation->add('IP', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->IP) ? $this->IP : null]));
     $network = Networks::findFirst($this->request->getPost('network', 'int'));
     $validation->add('IP', new Extension\Cidr(['cidr' => $network->subnetwork . $network->mask]));
     $validation->add('MAC', new Validator\PresenceOf());
     $validation->add('MAC', new Validator\Regex(['pattern' => '/([0-9a-fA-F]{2}[:|\\-]){5}[0-9a-fA-F]{2}/']));
     $validation->add('MAC', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->MAC) ? $this->MAC : null]));
     $validation->add('description', new Validator\StringLength(['max' => 1024]));
     $validation->add('status', new Validator\InclusionIn(['domain' => Devices::status()]));
     $validation->setFilters('IP', 'ip2long');
     $validation->setFilters('MAC', 'mac');
     $validation->setLabels(['name' => __('Name'), 'network' => __('Network'), 'client' => __('Client'), 'type' => __('Type'), 'IP' => __('IP'), 'MAC' => __('MAC'), 'description' => __('Description'), 'status' => __('Status')]);
     $messages = $validation->validate($_POST);
     // Return messages if validation not pass
     if (count($messages)) {
         return $validation->getMessages();
     } else {
         $this->name = $this->request->getPost('name', 'string');
         $this->network_id = $this->request->getPost('network', 'int');
         $this->client_id = $this->request->getPost('client', 'int');
         $this->type = $this->request->getPost('type', 'int');
         $this->IP = $this->request->getPost('IP', 'ip2long');
         $this->MAC = $this->request->getPost('MAC', 'mac');
         $this->description = $this->request->getPost('description', 'string');
         $this->status = $this->request->getPost('status', 'int');
         $this->date = date('Y-m-d H:i:s');
         // Try to write the record
         if ($this->{$method}() === true) {
             return $this;
         } else {
             \Las\Bootstrap::log($this->getMessages());
             return $this->getMessages();
         }
     }
 }
Esempio n. 5
0
 /**
  * Write method - add/edit the network
  *
  * @package     las
  * @version     1.0
  *
  * @param string $method type of method: create/update
  * @return mixed
  */
 public function write($method = 'create')
 {
     $validation = new Extension\Validation();
     $validation->add('name', new Validator\PresenceOf());
     $validation->add('name', new Validator\StringLength(['max' => 32]));
     $validation->add('interface', new Validator\PresenceOf());
     $validation->add('interface', new Validator\StringLength(['max' => 32]));
     $validation->add('interface', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->interface) ? $this->interface : null]));
     $validation->add('subnetwork', new Validator\PresenceOf());
     $validation->add('subnetwork', new Extension\Ip(['value' => $this->request->getPost('subnetwork')]));
     $validation->add('subnetwork', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->subnetwork) ? $this->subnetwork : null]));
     $validation->add('type', new Validator\PresenceOf());
     $validation->add('type', new Validator\InclusionIn(['domain' => Networks::type()]));
     $validation->add('IP', new Validator\PresenceOf());
     $validation->add('IP', new Extension\Ip(['value' => $this->request->getPost('IP')]));
     $validation->add('IP', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->IP) ? $this->IP : null]));
     $validation->add('gateway', new Validator\PresenceOf());
     $validation->add('gateway', new \Las\Extension\Ip(['value' => $this->request->getPost('gateway')]));
     $validation->add('DNS', new Extension\Dns(['allowEmpty' => true]));
     $validation->add('DNS', new Extension\Together(['with' => ['type' => Networks::WAN], 'allowEmpty' => true]));
     $validation->add('DHCP', new Extension\Dhcp(['allowEmpty' => true]));
     $validation->add('DHCP', new Extension\Together(['with' => ['type' => Networks::LAN], 'allowEmpty' => true]));
     $validation->add('mask', new Validator\InclusionIn(['domain' => array_keys(Networks::mask())]));
     $validation->add('download', new Validator\Between(['minimum' => 0, 'maximum' => 1000]));
     $validation->add('upload', new Validator\Between(['minimum' => 0, 'maximum' => 1000]));
     $validation->add('description', new Validator\StringLength(['max' => 1024]));
     $validation->add('status', new Validator\InclusionIn(['domain' => Networks::status()]));
     $validation->setFilters('subnetwork', 'ip2long');
     $validation->setFilters('IP', 'ip2long');
     $validation->setFilters('gateway', 'ip2long');
     $validation->setLabels(['name' => __('Name'), 'interface' => __('Interface'), 'subnetwork' => __('Subnetwork'), 'type' => __('Type'), 'IP' => __('IP'), 'gateway' => __('Gateway'), 'DNS' => __('DNS'), 'DHCP' => __('DHCP'), 'mask' => __('Mask'), 'download' => __('Download'), 'upload' => __('Upload'), 'description' => __('Description'), 'status' => __('Status')]);
     $messages = $validation->validate($_POST);
     // Return messages if validation not pass
     if (count($messages)) {
         return $validation->getMessages();
     } else {
         $this->name = $this->request->getPost('name', 'string');
         $this->interface = $this->request->getPost('interface', 'string');
         $this->subnetwork = $this->request->getPost('subnetwork', 'ip2long');
         $this->type = $this->request->getPost('type', 'int');
         $this->IP = $this->request->getPost('IP', 'ip2long');
         $this->mask = $this->request->getPost('mask', 'string');
         $this->gateway = $this->request->getPost('gateway', 'ip2long');
         $this->DNS = $this->request->getPost('DNS', 'string');
         $this->DHCP = $this->request->getPost('DHCP', 'string');
         $this->download = $this->request->getPost('download', 'int', 0, true);
         $this->upload = $this->request->getPost('upload', 'int', 0, true);
         $this->description = $this->request->getPost('description', 'string');
         $this->status = $this->request->getPost('status', 'int');
         $this->date = date('Y-m-d H:i:s');
         // Try to write the record
         if ($this->{$method}() === true) {
             return $this;
         } else {
             \Las\Bootstrap::log($this->getMessages());
             return $this->getMessages();
         }
     }
 }
Esempio n. 6
0
 /**
  * Edit action - edit the tariff
  *
  * @package     las
  * @version     1.0
  */
 public function editAction()
 {
     // Get id from url params and check if record exist
     $params = $this->router->getParams();
     if (isset($params[0]) && ($tariff = Tariffs::findFirst($params[0]))) {
         $networks = Networks::find(['type=:type:', 'bind' => ['type' => Networks::WAN]]);
         if (!count($networks)) {
             $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the network first") . ': ' . $this->tag->linkTo('admin/networks/add', __('Add')));
         }
         // Set title, pick view and send variables
         $this->tag->setTitle(__('Tariffs') . ' / ' . __('Edit'));
         $this->view->pick('tariffs/write');
         $this->view->setVars(['status' => Tariffs::status(true), 'bitRate' => \Las\Models\Settings::options('bitRate', $this->las['general']['bitRate'])]);
         // Check if the form has been sent
         if ($this->request->isPost() === true && $this->request->hasPost('submit')) {
             $valid = $tariff->write('update');
             // Check if data are valid
             if ($valid instanceof Tariffs) {
                 $this->flashSession->success($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Success') . '!</strong> ' . __("The data has been saved."));
             } else {
                 $this->view->setVar('errors', $valid);
                 $this->flashSession->warning($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Warning') . '!</strong> ' . __("Please correct the errors."));
             }
         } else {
             // Values to fill out the form
             $this->tag->setDefaults(get_object_vars($tariff));
         }
     } else {
         parent::notFoundAction();
     }
 }
Esempio n. 7
0
 /**
  * Write method - add/edit the tariff
  *
  * @package     las
  * @version     1.0
  *
  * @param string $method type: create/update
  * @return mixed
  */
 public function write($method = 'create')
 {
     $validation = new \Las\Extension\Validation();
     $validation->add('name', new Validator\PresenceOf());
     $validation->add('name', new Validator\StringLength(['min' => 3, 'max' => 32]));
     $validation->add('amount', new Validator\PresenceOf());
     $validation->add('amount', new Validator\Regex(['pattern' => '/\\d+(\\.\\d{2})?/']));
     $validation->add('priority', new Validator\Between(['minimum' => 10, 'maximum' => 99]));
     $validation->add('downloadRate', new Validator\Between(['minimum' => 0, 'maximum' => Tariffs::rate('download')]));
     $validation->add('downloadCeil', new Validator\Between(['minimum' => 0, 'maximum' => Networks::ceil('download')]));
     $validation->add('uploadRate', new Validator\Between(['minimum' => 0, 'maximum' => Tariffs::rate('upload')]));
     $validation->add('uploadCeil', new Validator\Between(['minimum' => 0, 'maximum' => Networks::ceil('upload')]));
     $validation->add('limit', new Validator\Regex(['allowEmpty' => true, 'pattern' => '/\\d+/']));
     $validation->add('description', new Validator\StringLength(['max' => 1024]));
     $validation->add('status', new Validator\InclusionIn(['domain' => Tariffs::status()]));
     $validation->setLabels(['name' => __('Name'), 'amount' => __('Amount'), 'priority' => __('Priority'), 'downloadRate' => __('Download rate'), 'downloadCeil' => __('Download ceil'), 'uploadRate' => __('Upload rate'), 'uploadCeil' => __('Upload ceil'), 'limit' => __('Limit'), 'description' => __('Description'), 'status' => __('Status')]);
     $messages = $validation->validate($_POST);
     // Return messages if validation not pass
     if (count($messages)) {
         return $validation->getMessages();
     } else {
         $this->name = $this->request->getPost('name', 'string');
         $this->amount = $this->request->getPost('amount');
         $this->priority = $this->request->getPost('priority', 'int');
         $this->downloadRate = $this->request->getPost('downloadRate', 'float');
         $this->downloadCeil = $this->request->getPost('downloadCeil', 'float');
         $this->uploadRate = $this->request->getPost('uploadRate', 'float');
         $this->uploadCeil = $this->request->getPost('uploadCeil', 'float');
         $this->limit = $this->request->getPost('limit', 'int', null, ture);
         $this->description = $this->request->getPost('description');
         $this->status = $this->request->getPost('status', 'int');
         $this->date = date('Y-m-d H:i:s');
         // Try to write the record
         if ($this->{$method}() === true) {
             return $this;
         } else {
             \Las\Bootstrap::log($this->getMessages());
             return $this->getMessages();
         }
     }
 }