示例#1
0
 /**
  * Tmp action - run the temporarily firewall
  *
  * @package     las
  * @version     1.0
  */
 public function tmpAction()
 {
     if ($task = Tasks::findFirst('status=' . Tasks::ACTIVE . ' AND when="@tmp"')) {
         $firewall = $task->getFirewall();
         if ($firewall) {
             if ($firewall->status == Firewalls::RELOAD) {
                 // Display notice flash
                 $this->tag->setTitle(__('Notice'));
                 $this->view->setVar('title', __('Notice'));
                 $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("The :name firewall was scheduled to reload.", [':name' => '<i>' . $firewall->name . '</i>']));
             } elseif ($firewall->status == Firewalls::COMPILED) {
                 $this->tag->setTitle(__('Turn on the temporary access'));
                 $this->view->pick('msg');
                 // Reload at real time or trigger reload
                 if ($this->las['general']['realTime'] && $this->las['general']['rootPassword']) {
                     try {
                         // Try to run command as root
                         Las::cmd('php ' . ROOT_PATH . '/private/index.php firewall display ' . $firewall->id . ' | sh', true);
                         $this->tag->setTitle(__('Success'));
                         $this->view->setVar('title', __('Success'));
                         $this->flashSession->success($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Success') . '!</strong> ' . __("The :name firewall was reloaded.", [':name' => '<i>' . $firewall->name . '</i>']));
                     } catch (Exception $e) {
                         $errors = ['error' => get_class($e) . '[' . $e->getCode() . ']: ' . $e->getMessage(), 'info' => $e->getFile() . '[' . $e->getLine() . ']', 'debug' => "Trace: \n" . $e->getTraceAsString() . "\n"];
                         \Las\Bootstrap::log($errors);
                         // Display warning flash
                         $this->tag->setTitle(__('Warning'));
                         $this->view->setVar('title', __('Warning'));
                         $this->flashSession->warning($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Warning') . '!</strong> ' . __("Something is wrong!"));
                     }
                 } else {
                     // Trigger reload - update firewall's status
                     $firewall->status = Firewalls::RELOAD;
                     $firewall->update();
                     // Display notice flash
                     $this->tag->setTitle(__('Notice'));
                     $this->view->setVar('title', __('Notice'));
                     $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("The :name firewall was scheduled to reload.", [':name' => '<i>' . $firewall->name . '</i>']));
                 }
             }
         } else {
             $this->response->redirect(null);
         }
     } else {
         parent::notFoundAction();
     }
 }
示例#2
0
文件: Tasks.php 项目: RobBickel/las
 /**
  * Write method - add/edit the task
  *
  * @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->setDefaultMessages(['TogetherWith' => 'Field :field must occur together with :with', 'TogetherWithOr' => 'Field :field must occur together with one of: :with', 'TogetherWithout' => 'Field :field must not occur together with :with']);
     $validation->add('name', new Validator\PresenceOf());
     $validation->add('name', new Validator\StringLength(['max' => 32]));
     $validation->add('when', new Validator\PresenceOf());
     $validation->add('when', new Validator\StringLength(['max' => 64]));
     $validation->add('type', new Validator\PresenceOf());
     $validation->add('type', new Validator\InclusionIn(['domain' => Tasks::type()]));
     if ($this->request->getPost('type', 'int') == Tasks::FIREWALL) {
         $validation->add('type', new Extension\Together(['with' => ['firewall']]));
     }
     $validation->add('firewall', new Extension\Together(['with' => ['type' => Tasks::FIREWALL], 'allowEmpty' => true]));
     $validation->add('firewall', new Validator\InclusionIn(['domain' => Arr::from_model($this->firewalls, null, 'id'), 'allowEmpty' => true]));
     $validation->add('next', new Validator\Regex(['pattern' => '/\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}/', 'message' => 'Required format: YYYY-MM-DD hh:mm', 'allowEmpty' => true]));
     $validation->add('description', new Validator\StringLength(['max' => 1024]));
     $validation->add('status', new Validator\InclusionIn(['domain' => Tasks::status()]));
     $validation->setLabels(['name' => __('Name'), 'when' => __('When'), 'type' => __('Type'), 'firewall' => __('Firewall'), 'next' => __('Next'), '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->when = $this->request->getPost('when', 'string');
         $this->type = $this->request->getPost('type', 'int');
         $this->firewall_id = $this->request->getPost('firewall', 'int', 0, true);
         $this->next = strtotime($this->request->getPost('next', 'string'));
         $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();
         }
     }
 }
示例#3
0
 /**
  * Tmp Action - for running temporarily tasks
  *
  * @package     las
  * @version     1.0
  */
 public function tmpAction()
 {
     // Only first ACTIVE task is running at temporarily
     if ($task = Tasks::findFirst('status=' . Tasks::ACTIVE . ' AND when="@tmp"')) {
         $this->dispatcher->forward(['task' => 'firewall', 'action' => 'display', 'params' => [$task->firewall_id]]);
     }
 }
示例#4
0
 /**
  * Edit action - edit the task
  *
  * @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]) && ($task = Tasks::findFirst($params[0]))) {
         $firewalls = Firewalls::find();
         if (!count($firewalls)) {
             $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the firewall first") . ': ' . $this->tag->linkTo('admin/firewalls/add', __('Add')));
         }
         // Set title, pick view and send variables
         $this->tag->setTitle(__('Tasks') . ' / ' . __('Edit'));
         $this->view->pick('tasks/write');
         $this->view->setVars(['type' => Tasks::type(true), 'status' => Tasks::status(true), 'firewalls' => $firewalls]);
         // Check if the form has been sent
         if ($this->request->isPost() === true && $this->request->hasPost('submit')) {
             $task->__set('firewalls', $firewalls);
             $valid = $task->write('update');
             // Check if data are valid
             if ($valid instanceof Tasks) {
                 $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 = ['firewall' => $task->firewall_id, 'next' => $task->next ? date('Y-m-d H:i', $task->next) : null];
             // Values to fill out the form
             $this->tag->setDefaults(array_merge(get_object_vars($task), $diff));
         }
     } else {
         parent::notFoundAction();
     }
 }