/** * 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)); }
/** * Qos settings * * @package las * @version 1.0 */ public function qosAction() { $this->tag->setTitle(__('Settings') . ' / ' . __(ucfirst($this->dispatcher->getActionName()))); $settings = Settings::find(['category = :category: AND status = :status:', 'bind' => [':category' => $this->dispatcher->getActionName(), ':status' => Settings::ACTIVE]]); $this->view->setVars(['settings' => $settings, 'category' => __(ucfirst($this->dispatcher->getActionName())), 'priority' => \Las\Models\Services::priority(true)]); // Check if the form has been sent if ($this->request->isPost() === true && $this->request->hasPost('submit')) { // Try to update settings $valid = Settings::qos($settings); // Check if data are valid if ($valid === TRUE) { $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($this->las[$this->dispatcher->getActionName()]); } }
/** * Write method - add/edit the service * * @package las * @version 1.0 * * @param string $method type: 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(['min' => 3, 'max' => 32])); $validation->add('chain', new Validator\PresenceOf()); $validation->add('chain', new Validator\InclusionIn(['domain' => Services::chain(), 'message' => __('Field :field must be a part of list: :domain', [':domain' => join(', ', Services::chain(true))])])); $validation->add('protocol', new Validator\InclusionIn(['domain' => Services::protocol(), 'allowEmpty' => true])); $validation->add('direction', new Extension\Together(['withOr' => ['client', 'device', 'IP'], 'allowEmpty' => true])); $validation->add('client', new Extension\Together(['with' => ['direction'], 'without' => ['device', 'IP'], 'allowEmpty' => true])); $validation->add('device', new Extension\Together(['with' => ['direction'], 'without' => ['IP'], 'allowEmpty' => true])); $validation->add('IP', new Extension\Together(['with' => ['direction'], 'allowEmpty' => true])); $validation->add('client', new Validator\InclusionIn(['domain' => Arr::from_model($this->clients, null, 'id'), 'allowEmpty' => true])); $validation->add('device', new Validator\InclusionIn(['domain' => Arr::from_model($this->devices, null, 'id'), 'allowEmpty' => true])); $validation->add('IP', new Extension\Ip(['value' => $this->request->getPost('IP'), 'allowEmpty' => true])); $validation->add('string', new Validator\StringLength(['max' => 128])); $validation->add('portDirection', new Extension\Together(['with' => ['startingPort'], 'allowEmpty' => true])); $validation->add('startingPort', new Validator\StringLength(['max' => 16])); $validation->add('startingPort', new Extension\Together(['with' => ['portDirection'], 'allowEmpty' => true])); $validation->add('endingPort', new Validator\Between(['minimum' => 1, 'maximum' => 65535, 'allowEmpty' => true])); $validation->add('endingPort', new Extension\Together(['with' => ['startingPort'], 'allowEmpty' => true])); $validation->add('priority', new Validator\InclusionIn(['domain' => Services::priority()])); $validation->add('sorting', new Validator\Between(['minimum' => 1, 'maximum' => 65535, 'allowEmpty' => true])); $validation->add('description', new Validator\StringLength(['max' => 1024])); $validation->add('status', new Validator\InclusionIn(['domain' => Services::status()])); $validation->setFilters('IP', 'ip2long'); $validation->setLabels(['name' => __('Name'), 'chain' => __('Chain'), 'protocol' => __('Protocol'), 'direction' => __('Direction'), 'client' => __('Client'), 'device' => __('Device'), 'IP' => __('IP'), 'string' => __('String'), 'portDirection' => __('Port direction'), 'startingPort' => __('Starting port'), 'endingPort' => __('Ending port'), 'lengthEnd' => __('Length end'), 'lengthStart' => __('Length start'), 'priority' => __('Priority'), 'sorting' => __('Sorting'), '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->chain = $this->request->getPost('chain', 'int'); $this->protocol = $this->request->getPost('protocol', 'int', null, true); $this->direction = $this->request->getPost('direction', 'int', null, true); $this->client_id = $this->request->getPost('client', 'int', 0, true); $this->device_id = $this->request->getPost('device', 'int', 0, true); $this->IP = $this->request->getPost('IP', 'ip2long'); $this->portDirection = $this->request->getPost('portDirection', 'int'); $this->startingPort = $this->request->getPost('startingPort', 'string', null, true); $this->endingPort = $this->request->getPost('endingPort', 'int', null, true); $this->internalStartingPort = $this->request->getPost('internalStartingPort', 'int', null, true); $this->internalEndingPort = $this->request->getPost('internalEndingPort', 'int', null, true); $this->priority = $this->request->getPost('priority', 'int', 0, true); $this->sorting = $this->request->getPost('sorting', 'int', 100, true); $this->description = $this->request->getPost('description', 'string', null, true); $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(); } } }
/** * Qos method - save Qos settings * * @package las * @version 1.0 */ public static function qos($settings) { $validation = new \Las\Extension\Validation(); $validation->add('defaultClass', new Validator\InclusionIn(array('domain' => Services::priority()))); $validation->add('enableQos', new Validator\InclusionIn(array('domain' => [0, 1]))); $validation->add('highestRate', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('highestRate', null, $settings))))); $validation->add('highestRate', new Validator\Between(array('minimum' => 1, 'maximum' => Services::rate('highestRate')))); $validation->add('highestCeil', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('highestCeil', null, $settings))))); $validation->add('highRate', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('highRate', null, $settings))))); $validation->add('highRate', new Validator\Between(array('minimum' => 1, 'maximum' => Services::rate('highRate')))); $validation->add('highCeil', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('highCeil', null, $settings))))); $validation->add('mediumRate', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('mediumRate', null, $settings))))); $validation->add('mediumRate', new Validator\Between(array('minimum' => 1, 'maximum' => Services::rate('mediumRate')))); $validation->add('mediumCeil', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('mediumCeil', null, $settings))))); $validation->add('lowRate', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('lowRate', null, $settings))))); $validation->add('lowRate', new Validator\Between(array('minimum' => 1, 'maximum' => Services::rate('lowRate')))); $validation->add('lowCeil', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('lowCeil', null, $settings))))); $validation->add('lowestRate', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('lowestRate', null, $settings))))); $validation->add('lowestRate', new Validator\Between(array('minimum' => 1, 'maximum' => Services::rate('lowestRate')))); $validation->add('lowestCeil', new Validator\InclusionIn(array('domain' => array_keys(Settings::options('lowestCeil', null, $settings))))); $validation->setLabels(array('defaultClass' => __('Default class'), 'enableQos' => __('Enable qos'), 'highestRate' => __('Highest rate'), 'highestCeil' => __('Highest ceil'), 'highRate' => __('High rate'), 'highCeil' => __('High ceil'), 'mediumRate' => __('Medium rate'), 'mediumCeil' => __('Medium ceil'), 'lowRate' => __('Low rate'), 'lowCeil' => __('Low ceil'), 'lowestRate' => __('Lowest rate'), 'lowestCeil' => __('Lowest ceil'))); return Settings::updateSettings($settings, $validation); }
/** * Edit action - edit the service * * @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]) && ($service = Services::findFirst($params[0]))) { $clients = Clients::find(['status=:status:', 'bind' => ['status' => Clients::ACTIVE]]); $devices = Devices::find(['status=:status:', 'bind' => ['status' => Devices::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($devices)) { $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the device first") . ': ' . $this->tag->linkTo('admin/devices/add', __('Add'))); } // Set title, pick view and send variables $this->tag->setTitle(__('Services') . ' / ' . __('Add')); $this->view->pick('services/write'); $this->view->setVars(['clients' => $clients, 'devices' => $devices, 'chain' => Services::chain(true), 'direction' => Services::direction(true), 'status' => Services::status(true), 'priority' => Services::priority(true), 'protocol' => Services::protocol(true), 'portDirection' => Services::portDirection(true)]); // Check if the form has been sent if ($this->request->isPost() === true && $this->request->hasPost('submit')) { $service->__set('clients', $clients); $service->__set('devices', $devices); $valid = $service->write('update'); // Check if data are valid if ($valid instanceof Services) { $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 = ['client' => $service->client_id, 'device' => $service->device_id, 'IP' => $service->IP ? long2ip($service->IP) : null]; // Values to fill out the form $this->tag->setDefaults(array_merge(get_object_vars($service), $diff)); } } else { parent::notFoundAction(); } }