summaryrefslogtreecommitdiff
path: root/app/Core/Http/Router.php
blob: 8b58a947d3f4808bc68fcebe69b61f39995e2334 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
<?php

namespace Kanboard\Core\Http;

use RuntimeException;
use Kanboard\Core\Base;

/**
 * Route Dispatcher
 *
 * @package http
 * @author  Frederic Guillot
 */
class Router extends Base
{
    /**
     * Plugin name
     *
     * @access private
     * @var string
     */
    private $plugin = '';

    /**
     * Controller
     *
     * @access private
     * @var string
     */
    private $controller = '';

    /**
     * Action
     *
     * @access private
     * @var string
     */
    private $action = '';

    /**
     * Get plugin name
     *
     * @access public
     * @return string
     */
    public function getPlugin()
    {
        return $this->plugin;
    }

    /**
     * Get controller
     *
     * @access public
     * @return string
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * Get action
     *
     * @access public
     * @return string
     */
    public function getAction()
    {
        return $this->action;
    }

    /**
     * Get the path to compare patterns
     *
     * @access public
     * @return string
     */
    public function getPath()
    {
        $path = substr($this->request->getUri(), strlen($this->helper->url->dir()));

        if ($this->request->getQueryString() !== '') {
            $path = substr($path, 0, - strlen($this->request->getQueryString()) - 1);
        }

        if ($path !== '' && $path{0} === '/') {
            $path = substr($path, 1);
        }

        return $path;
    }

    /**
     * Find controller/action from the route table or from get arguments
     *
     * @access public
     */
    public function dispatch()
    {
        $controller = $this->request->getStringParam('controller');
        $action = $this->request->getStringParam('action');
        $plugin = $this->request->getStringParam('plugin');

        if ($controller === '') {
            $route = $this->route->findRoute($this->getPath());
            $controller = $route['controller'];
            $action = $route['action'];
            $plugin = $route['plugin'];
        }

        $this->controller = ucfirst($this->sanitize($controller, 'app'));
        $this->action = $this->sanitize($action, 'index');
        $this->plugin = ucfirst($this->sanitize($plugin));

        return $this->executeAction();
    }

    /**
     * Check controller and action parameter
     *
     * @access public
     * @param  string $value
     * @param  string $default
     * @return string
     */
    public function sanitize($value, $default = '')
    {
        return preg_match('/^[a-zA-Z_0-9]+$/', $value) ? $value : $default;
    }

    /**
     * Execute controller action
     *
     * @access private
     */
    private function executeAction()
    {
        $class = $this->getControllerClassName();

        if (! class_exists($class)) {
            throw new RuntimeException('Controller not found');
        }

        if (! method_exists($class, $this->action)) {
            throw new RuntimeException('Action not implemented');
        }

        $instance = new $class($this->container);
        $instance->beforeAction($this->controller, $this->action);
        $instance->{$this->action}();
        return $instance;
    }

    /**
     * Get controller class name
     *
     * @access private
     * @return string
     */
    private function getControllerClassName()
    {
        if ($this->plugin !== '') {
            return '\Kanboard\Plugin\\'.$this->plugin.'\Controller\\'.$this->controller;
        }

        return '\Kanboard\Controller\\'.$this->controller;
    }
}