SettingsController.php 8.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
<?php
/**
 *    Copyright (C) 2015 Deciso B.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */
namespace OPNsense\CaptivePortal\Api;

use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Config;
use \OPNsense\CaptivePortal\CaptivePortal;
use \OPNsense\Base\UIModelGrid;

/**
 * Class SettingsController Handles settings related API actions for Captive Portal
 * @package OPNsense\TrafficShaper
 */
class SettingsController extends ApiControllerBase
{
    /**
     * validate and save model after update or insertion.
     * Use the reference node and tag to rename validation output for a specific node to a new offset, which makes
     * it easier to reference specific uuids without having to use them in the frontend descriptions.
46
     * @param $mdl model reference
47 48 49 50
     * @param $node reference node, to use as relative offset
     * @param $reference reference for validation output, used to rename the validation output keys
     * @return array result / validation output
     */
51
    private function save($mdl, $node = null, $reference = null)
52 53 54
    {
        $result = array("result"=>"failed","validations" => array());
        // perform validation
55
        $valMsgs = $mdl->performValidation();
56 57 58 59 60 61 62 63 64 65 66 67 68
        foreach ($valMsgs as $field => $msg) {
            // replace absolute path to attribute for relative one at uuid.
            if ($node != null) {
                $fieldnm = str_replace($node->__reference, $reference, $msg->getField());
                $result["validations"][$fieldnm] = $msg->getMessage();
            } else {
                $result["validations"][$msg->getField()] = $msg->getMessage();
            }
        }

        // serialize model to config and save when there are no validation errors
        if (count($result['validations']) == 0) {
            // save config if validated correctly
69
            $mdl->serializeToConfig();
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

            Config::getInstance()->save();
            $result = array("result" => "saved");
        }

        return $result;
    }

    /**
     * retrieve zone settings or return defaults
     * @param $uuid item unique id
     * @return array
     */
    public function getZoneAction($uuid = null)
    {
        $mdlCP = new CaptivePortal();
        if ($uuid != null) {
            $node = $mdlCP->getNodeByReference('zones.zone.'.$uuid);
            if ($node != null) {
                // return node
                return array("zone" => $node->getNodes());
            }
        } else {
            // generate new node, but don't save to disc
            $node = $mdlCP->zones->zone->add() ;
            return array("zone" => $node->getNodes());
        }
        return array();
    }

    /**
     * update zone with given properties
     * @param $uuid item unique id
     * @return array
     */
    public function setZoneAction($uuid)
    {
        if ($this->request->isPost() && $this->request->hasPost("zone")) {
            $mdlCP = new CaptivePortal();
            if ($uuid != null) {
                $node = $mdlCP->getNodeByReference('zones.zone.'.$uuid);
                if ($node != null) {
                    $node->setNodes($this->request->getPost("zone"));
                    return $this->save($mdlCP, $node, "zone");
                }
            }
        }
        return array("result"=>"failed");
    }

    /**
     * add new zone and set with attributes from post
     * @return array
     */
    public function addZoneAction()
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost() && $this->request->hasPost("zone")) {
            $mdlCP = new CaptivePortal();
            $node = $mdlCP->zones->zone->Add();
            $node->setNodes($this->request->getPost("zone"));
            return $this->save($mdlCP, $node, "zone");
        }
        return $result;
    }

    /**
     * delete zone by uuid
     * @param $uuid item unique id
     * @return array status
     */
    public function delZoneAction($uuid)
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost()) {
            $mdlCP = new CaptivePortal();
            if ($uuid != null) {
                if ($mdlCP->zones->zone->del($uuid)) {
                    // if item is removed, serialize to config and save
                    $mdlCP->serializeToConfig();
                    Config::getInstance()->save();
                    $result['result'] = 'deleted';
                } else {
                    $result['result'] = 'not found';
                }
            }
        }
        return $result;
    }

    /**
     * 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;
    }

    /**
     * search captive portal zones
     * @return array
     */
    public function searchZonesAction()
    {
        if ($this->request->isPost()) {
            $this->sessionClose();
            // fetch query parameters
            $itemsPerPage = $this->request->getPost('rowCount', 'int', 9999);
            $currentPage = $this->request->getPost('current', 'int', 1);
            $sortBy = array("number");
            $sortDescending = false;

            if ($this->request->hasPost('sort') && is_array($this->request->getPost("sort"))) {
                $sortBy = array_keys($this->request->getPost("sort"));
                if ($this->request->getPost("sort")[$sortBy[0]] == "desc") {
                    $sortDescending = true;
                }
            }

            $searchPhrase = $this->request->getPost('searchPhrase', 'string', '');

            // create model and fetch query resuls
            $fields = array("enabled", "description", "zoneid");
            $mdlCP = new CaptivePortal();
            $grid = new UIModelGrid($mdlCP->zones->zone);
            return $grid->fetch($fields, $itemsPerPage, $currentPage, $sortBy, $sortDescending, $searchPhrase);
        } else {
            return array();
        }
    }
}