summaryrefslogtreecommitdiff
path: root/app/Core/Http/Router.php
blob: 4de276a00f4253da614fa91b5bc5e016fd5a6e8a (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
<?php

namespace Kanboard\Core\Http;

use Kanboard\Core\Base;

/**
 * Route Dispatcher
 *
 * @package http
 * @author  Frederic Guillot
 */
class Router extends Base
{
    const DEFAULT_CONTROLLER = 'DashboardController';
    const DEFAULT_METHOD = 'show';

    /**
     * Plugin name
     *
     * @access private
     * @var string
     */
    private $currentPluginName = '';

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

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

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

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

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

    /**
     * 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->currentControllerName = ucfirst($this->sanitize($controller, self::DEFAULT_CONTROLLER));
        $this->currentActionName = $this->sanitize($action, self::DEFAULT_METHOD);
        $this->currentPluginName = ucfirst($this->sanitize($plugin));
    }

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