コード例 #1
0
 /**
  * toggle zone by uuid (enable/disable)
  * @param $uuid item unique id
  * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
  * @return array status
  */
 public function toggleZoneAction($uuid, $enabled = null)
 {
     $result = array("result" => "failed");
     if ($this->request->isPost()) {
         $mdlCP = new CaptivePortal();
         if ($uuid != null) {
             $node = $mdlCP->getNodeByReference('zones.zone.' . $uuid);
             if ($node != null) {
                 if ($enabled == "0" || $enabled == "1") {
                     $node->enabled = (string) $enabled;
                 } elseif ((string) $node->enabled == "1") {
                     $node->enabled = "0";
                 } else {
                     $node->enabled = "1";
                 }
                 $result['result'] = $node->enabled;
                 // if item has toggled, serialize to config and save
                 $mdlCP->serializeToConfig();
                 Config::getInstance()->save();
             }
         }
     }
     return $result;
 }
コード例 #2
0
 /**
  * save template, updates existing or create new.
  * @return string
  */
 public function saveTemplateAction()
 {
     if ($this->request->isPost() && $this->request->hasPost("name")) {
         $this->sessionClose();
         $templateName = $this->request->getPost("name", "striptags");
         $mdlCP = new CaptivePortal();
         if ($this->request->hasPost("uuid")) {
             $uuid = $this->request->getPost("uuid", "striptags");
             $template = $mdlCP->getNodeByReference('templates.template.' . $uuid);
             if ($template == null) {
                 return array("name" => $templateName, "error" => "node not found");
             }
         } else {
             $template = $mdlCP->getTemplateByName($templateName);
         }
         // cleanse input content, we only want to save changed files into our config
         if (strlen($this->request->getPost("content", "striptags", "")) > 20 || strlen((string) $template->content) == 0) {
             $temp_filename = 'cp_' . (string) $template->getAttributes()['uuid'] . '.tmp';
             file_put_contents('/tmp/' . $temp_filename, $this->request->getPost("content", "striptags", ""));
             // strip defaults and unchanged files from template (standard js libs, etc)
             $backend = new Backend();
             $response = $backend->configdpRun("captiveportal strip_template", array($temp_filename));
             unlink('/tmp/' . $temp_filename);
             $result = json_decode($response, true);
             if ($result != null && !array_key_exists('error', $result)) {
                 $template->content = $result['payload'];
             } else {
                 return array("name" => $templateName, "error" => $result['error']);
             }
         }
         $template->name = $templateName;
         $valMsgs = $mdlCP->performValidation();
         $errorMsg = "";
         foreach ($valMsgs as $field => $msg) {
             if ($errorMsg != "") {
                 $errorMsg .= " , ";
             }
             $errorMsg .= $msg->getMessage();
         }
         if ($errorMsg != "") {
             return array("name" => (string) $template->name, "error" => $errorMsg);
         } else {
             // data is valid, save and return.
             $mdlCP->serializeToConfig();
             Config::getInstance()->save();
             return array("name" => (string) $template->name);
         }
     }
     return null;
 }