SettingsController.php 17.3 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
<?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\TrafficShaper\Api;

use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Config;
use \OPNsense\TrafficShaper\TrafficShaper;
34
use \OPNsense\Base\UIModelGrid;
35 36

/**
37
 * Class SettingsController Handles settings related API actions for the Traffic Shaper
38
 * @package OPNsense\TrafficShaper
39 40 41 42
 */
class SettingsController extends ApiControllerBase
{
    /**
43 44 45
     * 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 47
     * @param $mdlShaper
     * @param $node reference node, to use as relative offset
48
     * @param $reference reference for validation output, used to rename the validation output keys
49 50
     * @return array result / validation output
     */
51
    private function save($mdlShaper, $node = null, $reference = null)
52
    {
53
        $result = array("result"=>"failed","validations" => array());
54 55 56 57
        // perform validation
        $valMsgs = $mdlShaper->performValidation();
        foreach ($valMsgs as $field => $msg) {
            // replace absolute path to attribute for relative one at uuid.
58 59
            if ($node != null) {
                $fieldnm = str_replace($node->__reference, $reference, $msg->getField());
60
                $result["validations"][$fieldnm] = $msg->getMessage();
61 62 63
            } else {
                $result["validations"][$msg->getField()] = $msg->getMessage();
            }
64 65 66
        }

        // serialize model to config and save when there are no validation errors
67
        if (count($result['validations']) == 0) {
68
            // save config if validated correctly
69 70
            $mdlShaper->serializeToConfig();

71
            Config::getInstance()->save();
72
            $result = array("result" => "saved");
73 74 75 76 77 78 79
        }

        return $result;
    }

    /**
     * retrieve pipe settings or return defaults
80
     * @param $uuid item unique id
81 82
     * @return array
     */
83
    public function getPipeAction($uuid = null)
84
    {
85
        $mdlShaper = new TrafficShaper();
86 87 88
        if ($uuid != null) {
            $node = $mdlShaper->getNodeByReference('pipes.pipe.'.$uuid);
            if ($node != null) {
89 90
                // return node
                return array("pipe" => $node->getNodes());
91
            }
92 93 94 95
        } else {
            // generate new node, but don't save to disc
            $node = $mdlShaper->pipes->pipe->add() ;
            return array("pipe" => $node->getNodes());
96
        }
97
        return array();
98 99
    }

100 101 102 103 104 105 106 107 108 109 110 111 112
    /**
     * update pipe with given properties
     * @param $uuid item unique id
     * @return array
     */
    public function setPipeAction($uuid)
    {
        if ($this->request->isPost() && $this->request->hasPost("pipe")) {
            $mdlShaper = new TrafficShaper();
            if ($uuid != null) {
                $node = $mdlShaper->getNodeByReference('pipes.pipe.'.$uuid);
                if ($node != null) {
                    $node->setNodes($this->request->getPost("pipe"));
113
                    return $this->save($mdlShaper, $node, "pipe");
114 115 116
                }
            }
        }
117
        return array("result"=>"failed");
118 119 120
    }

    /**
121
     * add new pipe and set with attributes from post
122 123 124 125 126 127 128 129 130
     * @return array
     */
    public function addPipeAction()
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost() && $this->request->hasPost("pipe")) {
            $mdlShaper = new TrafficShaper();
            $node = $mdlShaper->addPipe();
            $node->setNodes($this->request->getPost("pipe"));
131
            $node->origin = "TrafficShaper"; // set origin to this component.
132
            return $this->save($mdlShaper, $node, "pipe");
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        }
        return $result;
    }

    /**
     * delete pipe by uuid
     * @param $uuid item unique id
     * @return array status
     */
    public function delPipeAction($uuid)
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost()) {
            $mdlShaper = new TrafficShaper();
            if ($uuid != null) {
                if ($mdlShaper->pipes->pipe->del($uuid)) {
                    // if item is removed, serialize to config and save
150
                    $mdlShaper->serializeToConfig();
151 152 153 154 155 156 157 158 159 160
                    Config::getInstance()->save();
                    $result['result'] = 'deleted';
                } else {
                    $result['result'] = 'not found';
                }
            }
        }
        return $result;
    }

161 162 163
    /**
     * toggle pipe by uuid (enable/disable)
     * @param $uuid item unique id
164
     * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
165 166
     * @return array status
     */
167
    public function togglePipeAction($uuid, $enabled = null)
168 169 170 171 172 173 174 175
    {

        $result = array("result" => "failed");
        if ($this->request->isPost()) {
            $mdlShaper = new TrafficShaper();
            if ($uuid != null) {
                $node = $mdlShaper->getNodeByReference('pipes.pipe.' . $uuid);
                if ($node != null) {
176 177 178
                    if ($enabled == "0" || $enabled == "1") {
                        $node->enabled = (string)$enabled;
                    } elseif ($node->enabled->__toString() == "1") {
179 180 181 182
                        $node->enabled = "0";
                    } else {
                        $node->enabled = "1";
                    }
183
                    $result['result'] = $node->enabled;
184
                    // if item has toggled, serialize to config and save
185
                    $mdlShaper->serializeToConfig();
186 187 188 189 190 191 192
                    Config::getInstance()->save();
                }
            }
        }
        return $result;
    }

193
    /**
194
     * search traffic shaper pipes
195 196 197 198 199
     * @return array
     */
    public function searchPipesAction()
    {
        if ($this->request->isPost()) {
200
            $this->sessionClose();
201 202 203 204 205
            // fetch query parameters
            $itemsPerPage = $this->request->getPost('rowCount', 'int', 9999);
            $currentPage = $this->request->getPost('current', 'int', 1);
            $sortBy = array("number");
            $sortDescending = false;
206

207
            if ($this->request->hasPost('sort') && is_array($this->request->getPost("sort"))) {
208 209 210 211 212 213
                $sortBy = array_keys($this->request->getPost("sort"));
                if ($this->request->getPost("sort")[$sortBy[0]] == "desc") {
                    $sortDescending = true;
                }
            }

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

216
            // create model and fetch query resuls
217
            $fields = array("enabled","number", "bandwidth","bandwidthMetric","burst","description","mask","origin");
218 219 220 221 222
            $mdlShaper = new TrafficShaper();
            $grid = new UIModelGrid($mdlShaper->pipes->pipe);
            return $grid->fetch($fields, $itemsPerPage, $currentPage, $sortBy, $sortDescending, $searchPhrase);
        } else {
            return array();
223 224 225
        }

    }
226

227 228 229 230 231 232 233
    /**
     * search traffic shaper queues
     * @return array
     */
    public function searchQueuesAction()
    {
        if ($this->request->isPost()) {
234
            $this->sessionClose();
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
            // 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
251
            $fields = array("enabled","number", "pipe","weight","description","mask","origin");
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
            $mdlShaper = new TrafficShaper();
            $grid = new UIModelGrid($mdlShaper->queues->queue);
            return $grid->fetch($fields, $itemsPerPage, $currentPage, $sortBy, $sortDescending, $searchPhrase);
        } else {
            return array();
        }

    }

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

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

    /**
     * add new queue and set with attributes from post
     * @return array
     */
    public function addQueueAction()
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost() && $this->request->hasPost("queue")) {
            $mdlShaper = new TrafficShaper();
            $node = $mdlShaper->addQueue();
            $node->setNodes($this->request->getPost("queue"));
            $node->origin = "TrafficShaper"; // set origin to this component.
            return $this->save($mdlShaper, $node, "queue");
        }
        return $result;
    }

    /**
     * delete queue by uuid
     * @param $uuid item unique id
     * @return array status
     */
    public function delQueueAction($uuid)
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost()) {
            $mdlShaper = new TrafficShaper();
            if ($uuid != null) {
                if ($mdlShaper->queues->queue->del($uuid)) {
                    // if item is removed, serialize to config and save
333
                    $mdlShaper->serializeToConfig();
334 335 336 337 338 339 340 341 342 343
                    Config::getInstance()->save();
                    $result['result'] = 'deleted';
                } else {
                    $result['result'] = 'not found';
                }
            }
        }
        return $result;
    }

344 345 346
    /**
     * toggle queue by uuid (enable/disable)
     * @param $uuid item unique id
347
     * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
348 349
     * @return array status
     */
350
    public function toggleQueueAction($uuid, $enabled = null)
351 352 353 354 355 356 357 358
    {

        $result = array("result" => "failed");
        if ($this->request->isPost()) {
            $mdlShaper = new TrafficShaper();
            if ($uuid != null) {
                $node = $mdlShaper->getNodeByReference('queues.queue.'.$uuid);
                if ($node != null) {
359 360 361
                    if ($enabled == "0" || $enabled == "1") {
                        $node->enabled = (string)$enabled;
                    } elseif ($node->enabled->__toString() == "1") {
362 363 364 365
                        $node->enabled = "0";
                    } else {
                        $node->enabled = "1";
                    }
366
                    $result['result'] = $node->enabled;
367
                    // if item has toggled, serialize to config and save
368
                    $mdlShaper->serializeToConfig();
369 370 371 372 373 374 375
                    Config::getInstance()->save();
                }
            }
        }
        return $result;
    }

376 377 378 379 380 381 382
    /**
     * search traffic shaper rules
     * @return array
     */
    public function searchRulesAction()
    {
        if ($this->request->isPost()) {
383
            $this->sessionClose();
384 385 386
            // fetch query parameters
            $itemsPerPage = $this->request->getPost('rowCount', 'int', 9999);
            $currentPage = $this->request->getPost('current', 'int', 1);
387
            $sortBy = array("sequence");
388 389 390 391 392 393 394 395 396 397 398 399
            $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
400
            $fields = array("interface", "proto","source","destination","description","origin","sequence","target");
401 402 403 404 405 406
            $mdlShaper = new TrafficShaper();
            $grid = new UIModelGrid($mdlShaper->rules->rule);
            return $grid->fetch($fields, $itemsPerPage, $currentPage, $sortBy, $sortDescending, $searchPhrase);
        } else {
            return array();
        }
407
    }
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    /**
     * retrieve rule settings or return defaults for new rule
     * @param $uuid item unique id
     * @return array
     */
    public function getRuleAction($uuid = null)
    {
        $mdlShaper = new TrafficShaper();
        if ($uuid != null) {
            $node = $mdlShaper->getNodeByReference('rules.rule.'.$uuid);
            if ($node != null) {
                // return node
                return array("rule" => $node->getNodes());
            }
        } else {
            // generate new node, but don't save to disc
            $node = $mdlShaper->rules->rule->add() ;
426
            $node->sequence = $mdlShaper->getMaxRuleSequence() + 10;
427 428 429
            return array("rule" => $node->getNodes());
        }
        return array();
430
    }
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

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

    /**
     * add new rule and set with attributes from post
     * @return array
     */
    public function addRuleAction()
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost() && $this->request->hasPost("rule")) {
            $mdlShaper = new TrafficShaper();
            $node = $mdlShaper->rules->rule->add();
            $node->setNodes($this->request->getPost("rule"));
            $node->origin = "TrafficShaper"; // set origin to this component.
            return $this->save($mdlShaper, $node, "rule");
        }
        return $result;
    }

    /**
     * delete rule by uuid
     * @param $uuid item unique id
     * @return array status
     */
    public function delRuleAction($uuid)
    {
        $result = array("result"=>"failed");
        if ($this->request->isPost()) {
            $mdlShaper = new TrafficShaper();
            if ($uuid != null) {
                if ($mdlShaper->rules->rule->del($uuid)) {
                    // if item is removed, serialize to config and save
482
                    $mdlShaper->serializeToConfig();
483 484 485 486 487 488 489 490 491
                    Config::getInstance()->save();
                    $result['result'] = 'deleted';
                } else {
                    $result['result'] = 'not found';
                }
            }
        }
        return $result;
    }
492
}