Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
helper
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 route
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
13
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\routing;
15
16use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17use Symfony\Component\Routing\RequestContext;
18use phpbb\filesystem\helper as filesystem_helper;
19
20/**
21* Controller helper class, contains methods that do things for controllers
22*/
23class helper
24{
25    /**
26     * config object
27     * @var \phpbb\config\config
28     */
29    protected $config;
30
31    /**
32     * phpBB router
33     * @var \phpbb\routing\router
34     */
35    protected $router;
36
37    /**
38     * @var \phpbb\symfony_request
39     */
40    protected $symfony_request;
41
42    /**
43     * @var \phpbb\request\request_interface
44     */
45    protected $request;
46
47    /**
48     * phpBB root path
49     * @var string
50     */
51    protected $phpbb_root_path;
52
53    /**
54     * PHP file extension
55     * @var string
56     */
57    protected $php_ext;
58
59    /**
60     * Constructor
61     *
62     * @param \phpbb\config\config $config Config object
63     * @param \phpbb\routing\router $router phpBB router
64     * @param \phpbb\symfony_request $symfony_request Symfony Request object
65     * @param \phpbb\request\request_interface $request phpBB request object
66     * @param string $phpbb_root_path phpBB root path
67     * @param string $php_ext PHP file extension
68     */
69    public function __construct(\phpbb\config\config $config, \phpbb\routing\router $router, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, $phpbb_root_path, $php_ext)
70    {
71        $this->config = $config;
72        $this->router = $router;
73        $this->symfony_request = $symfony_request;
74        $this->request = $request;
75        $this->phpbb_root_path = $phpbb_root_path;
76        $this->php_ext = $php_ext;
77    }
78
79    /**
80     * Generate a URL to a route
81     *
82     * @param string    $route        Name of the route to travel
83     * @param array    $params        String or array of additional url parameters
84     * @param bool    $is_amp        Is url using &amp; (true) or & (false)
85     * @param string|bool        $session_id    Possibility to use a custom session id instead of the global one
86     * @param int        $reference_type The type of reference to be generated (one of the constants)
87     * @return string The URL already passed through append_sid()
88     */
89    public function route($route, array $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
90    {
91        $anchor = '';
92        if (isset($params['#']))
93        {
94            $anchor = '#' . $params['#'];
95            unset($params['#']);
96        }
97
98        $context = new RequestContext();
99        $context->fromRequest($this->symfony_request);
100
101        if ($this->config['force_server_vars'])
102        {
103            $context->setHost($this->config['server_name']);
104            $context->setScheme(substr($this->config['server_protocol'], 0, -3));
105            $context->setHttpPort($this->config['server_port']);
106            $context->setHttpsPort($this->config['server_port']);
107            $context->setBaseUrl(rtrim($this->config['script_path'], '/'));
108        }
109
110        $script_name = $this->symfony_request->getScriptName();
111        $page_name = substr($script_name, -1, 1) == '/' ? '' : utf8_basename($script_name);
112
113        $base_url = $context->getBaseUrl();
114
115        // Append page name if base URL does not contain it
116        if (!empty($page_name) && strpos($base_url, '/' . $page_name) === false)
117        {
118            $base_url .= '/' . $page_name;
119        }
120
121        // If enable_mod_rewrite is false we need to replace the current front-end by app.php, otherwise we need to remove it.
122        $base_url = str_replace('/' . $page_name, empty($this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '', $base_url);
123
124        // We need to update the base url to move to the directory of the app.php file if the current script is not app.php
125        if ($page_name !== 'app.php' && !$this->config['force_server_vars'])
126        {
127            if (empty($this->config['enable_mod_rewrite']))
128            {
129                $base_url = str_replace('/app.' . $this->php_ext, '/' . $this->phpbb_root_path . 'app.' . $this->php_ext, $base_url);
130            }
131            else
132            {
133                $base_url .= preg_replace(get_preg_expression('path_remove_dot_trailing_slash'), '$2', $this->phpbb_root_path);
134            }
135        }
136
137        $base_url = $this->request->escape(filesystem_helper::clean_path($base_url), true);
138
139        $context->setBaseUrl($base_url);
140
141        $this->router->setContext($context);
142        $route_url = $this->router->generate($route, $params, $reference_type);
143
144        if ($is_amp)
145        {
146            $route_url = str_replace(array('&amp;', '&'), array('&', '&amp;'), $route_url);
147        }
148
149        if ($reference_type === UrlGeneratorInterface::RELATIVE_PATH && empty($this->config['enable_mod_rewrite']))
150        {
151            $route_url = 'app.' . $this->php_ext . '/' . $route_url;
152        }
153
154        return append_sid($route_url . $anchor, false, $is_amp, $session_id, true);
155    }
156}