ControllerBase.php 4.47 KB
Newer Older
1
<?php
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
/**
 *    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.
 *
 */
29 30
namespace OPNsense\Base;

31
use OPNsense\Core\Config;
32
use Phalcon\Mvc\Controller;
33
use Phalcon\Translate\Adapter\NativeArray;
34

35 36 37 38
/**
 * Class ControllerBase implements core controller for OPNsense framework
 * @package OPNsense\Base
 */
39 40
class ControllerBase extends Controller
{
41 42
    /**
     * translate a text
43
     * @return NativeArray
44 45 46 47 48
     */
    public function getTranslator()
    {
        // TODO: implement language service
        $messages = array();
49
        return new NativeArray(array(
50 51 52 53
            "content" => $messages
        ));
    }

54 55 56 57 58
    /**
     * Default action. Set the standard layout.
     */
    public function initialize()
    {
59
        // set base template
60 61
        $this->view->setTemplateBefore('default');
    }
62 63

    /**
64
     * shared functionality for all components
65
     * @param $dispatcher
66
     * @return bool
67
     * @throws \Exception
68 69 70
     */
    public function beforeExecuteRoute($dispatcher)
    {
71 72 73 74 75 76 77 78 79 80 81 82
        // only handle input validation on first request.
        if (!$dispatcher->wasForwarded()) {
            // Authentication
            // - use authentication of legacy OPNsense.
            if ($this->session->has("Username") == false) {
                $this->response->redirect("/", true);
            }
            // check for valid csrf on post requests
            if ($this->request->isPost() && !$this->security->checkToken()) {
                // post without csrf, exit.
                return false;
            }
83

84 85 86 87 88 89 90 91 92
            // REST type calls should be implemented by inheriting ApiControllerBase.
            // because we don't check for csrf on these methods, we want to make sure these aren't used.
            if ($this->request->isHead() ||
                $this->request->isPut() ||
                $this->request->isDelete() ||
                $this->request->isPatch() ||
                $this->request->isOptions()) {
                throw new \Exception('request type not supported');
            }
93 94
        }

95 96 97 98 99
        // include csrf for volt view rendering.
        $this->view->setVars([
            'csrf_tokenKey' => $this->security->getTokenKey(),
            'csrf_token' => $this->security->getToken()
        ]);
100

Ad Schellevis's avatar
Ad Schellevis committed
101
        // set translator
102
        $this->view->setVar('lang', $this->getTranslator());
103 104 105

        // link menu system to view, append /ui in uri because of rewrite
        $menu = new Menu\MenuSystem();
106 107 108 109 110

        // add interfaces to "Interfaces" menu tab... kind of a hack, may need some improvement.
        $cnf = Config::getInstance();
        $ordid = 0;
        foreach ($cnf->object()->interfaces->children() as $key => $node) {
111 112
            $menu->appendItem("Interfaces", $key, array("url"=>"/interfaces.php?if=".$key,"order"=>($ordid++),
                "visiblename"=>$node->descr?$node->descr:strtoupper($key)));
113 114
        }

115 116
        $this->view->menuSystem = $menu->getItems("/ui".$this->router->getRewriteUri());

117 118
        // append ACL object to view
        $this->view->acl = new \OPNsense\Core\ACL();
119 120 121 122 123 124 125 126 127 128
    }

    /**
     * @param $dispatcher
     */
    public function afterExecuteRoute($dispatcher)
    {
        // Executed after every found action
        // TODO: implement default behavior
    }
129
}