Completed
Push — master ( fff73d...f066e6 )
by Thierry
01:44
created

src/Utils/URI.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * URI.php - Jaxon request URI detector
5
 *
6
 * Detect and parse the URI of the Jaxon request being processed.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Utils;
16
17
class URI
18
{
19
    /**
20
     * Detect the URI of the current request
21
     *
22
     * @return string        The URI
23
     */
24
    public static function detect()
25
    {
26
        $aURL = [];
27
        // Try to get the request URL
28
        if(!empty($_SERVER['REQUEST_URI']))
29
        {
30
            $_SERVER['REQUEST_URI'] = str_replace(array('"',"'",'<','>'), array('%22','%27','%3C','%3E'), $_SERVER['REQUEST_URI']);
31
            $aURL = parse_url($_SERVER['REQUEST_URI']);
32
        }
33
34
        // Fill in the empty values
35
        if(empty($aURL['scheme']))
36
        {
37
            if(!empty($_SERVER['HTTP_SCHEME']))
38
            {
39
                $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
40
            }
41
            else
42
            {
43
                $aURL['scheme'] = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http');
44
            }
45
        }
46
47
        if(empty($aURL['host']))
48
        {
49
            if(!empty($_SERVER['HTTP_X_FORWARDED_HOST']))
50
            {
51 View Code Duplication
                if(strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ':') > 0)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                {
53
                    list($aURL['host'], $aURL['port']) = explode(':', $_SERVER['HTTP_X_FORWARDED_HOST']);
54
                }
55
                else
56
                {
57
                    $aURL['host'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
58
                }
59
            }
60 View Code Duplication
            elseif(!empty($_SERVER['HTTP_HOST']))
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            {
62
                if(strpos($_SERVER['HTTP_HOST'], ':') > 0)
63
                {
64
                    list($aURL['host'], $aURL['port']) = explode(':', $_SERVER['HTTP_HOST']);
65
                }
66
                else
67
                {
68
                    $aURL['host'] = $_SERVER['HTTP_HOST'];
69
                }
70
            }
71
            elseif(!empty($_SERVER['SERVER_NAME']))
72
            {
73
                $aURL['host'] = $_SERVER['SERVER_NAME'];
74
            }
75
            else
76
            {
77
                throw new \Jaxon\Exception\URI();
78
            }
79
        }
80
81
        if(empty($aURL['port']) && !empty($_SERVER['SERVER_PORT']))
82
        {
83
            $aURL['port'] = $_SERVER['SERVER_PORT'];
84
        }
85
86
        if(!empty($aURL['path']) && strlen(basename($aURL['path'])) == 0)
87
        {
88
            unset($aURL['path']);
89
        }
90
91
        if(empty($aURL['path']))
92
        {
93
            if(!empty($_SERVER['PATH_INFO']))
94
            {
95
                $sPath = parse_url($_SERVER['PATH_INFO']);
96
            }
97
            else
98
            {
99
                $sPath = parse_url($_SERVER['PHP_SELF']);
100
            }
101
            if(isset($sPath['path']))
102
            {
103
                $aURL['path'] = str_replace(array('"',"'",'<','>'), array('%22','%27','%3C','%3E'), $sPath['path']);
104
            }
105
            unset($sPath);
106
        }
107
108
        if(empty($aURL['query']) && !empty($_SERVER['QUERY_STRING']))
109
        {
110
            $aURL['query'] = $_SERVER['QUERY_STRING'];
111
        }
112
113
        if(!empty($aURL['query']))
114
        {
115
            $aURL['query'] = '?'.$aURL['query'];
116
        }
117
118
        // Build the URL: Start with scheme, user and pass
119
        $sURL = $aURL['scheme'].'://';
120
        if(!empty($aURL['user']))
121
        {
122
            $sURL.= $aURL['user'];
123
            if(!empty($aURL['pass']))
124
            {
125
                $sURL.= ':'.$aURL['pass'];
126
            }
127
            $sURL.= '@';
128
        }
129
130
        // Add the host
131
        $sURL.= $aURL['host'];
132
133
        // Add the port if needed
134
        if(!empty($aURL['port'])
135
                && (($aURL['scheme'] == 'http' && $aURL['port'] != 80)
136
                        || ($aURL['scheme'] == 'https' && $aURL['port'] != 443)))
137
        {
138
            $sURL.= ':'.$aURL['port'];
139
        }
140
141
        // Add the path and the query string
142
        $sURL.= $aURL['path'].@$aURL['query'];
143
144
        // Clean up
145
        unset($aURL);
146
147
        $aURL = explode("?", $sURL);
148
149
        if(1 < count($aURL))
150
        {
151
            $aQueries = explode("&", $aURL[1]);
152
153
            foreach($aQueries as $sKey => $sQuery)
154
            {
155
                if("jxnGenerate" == substr($sQuery, 0, 11))
156
                    unset($aQueries[$sKey]);
157
            }
158
159
            $sQueries = implode("&", $aQueries);
160
161
            $aURL[1] = $sQueries;
162
163
            $sURL = implode("?", $aURL);
164
        }
165
166
        return $sURL;
167
    }
168
}
169