root/fURL.php

Revision 349, 6.4 kB (checked in by wbond, 2 years ago)

More fixes to fURL::makeFriendly() and fFilesystem::makeURLSafe()

LineHide Line Numbers
1 <?php
2 /**
3  * Provides functionality to retrieve and manipulate URL information
4  *
5  * This class uses `$_SERVER['REQUEST_URI']` for all operations, meaning that
6  * the original URL entered by the user will be used, or that any rewrites
7  * will **not** be reflected by this class.
8  *
9  * @copyright  Copyright (c) 2007-2008 William Bond
10  * @author     William Bond [wb] <will@flourishlib.com>
11  * @license    http://flourishlib.com/license
12  *
13  * @package    Flourish
14  * @link       http://flourishlib.com/fURL
15  *
16  * @version    1.0.0b
17  * @changes    1.0.0b  The initial implementation [wb, 2007-06-14]
18  */
19 class fURL
20 {
21     // The following constants allow for nice looking callbacks to static methods
22     const get                   = 'fURL::get';
23     const getDomain             = 'fURL::getDomain';
24     const getQueryString        = 'fURL::getQueryString';
25     const getWithQueryString    = 'fURL::getWithQueryString';
26     const makeFriendly          = 'fURL::makeFriendly';
27     const redirect              = 'fURL::redirect';
28     const removeFromQueryString = 'fURL::removeFromQueryString';
29     const replaceInQueryString  = 'fURL::replaceInQueryString';
30    
31    
32     /**
33     * Returns the requested URL, does no include the domain name or query string
34     *
35     * This will return the original URL requested by the user - ignores all
36     * rewrites.
37     *
38     * @return string  The requested URL without the query string
39     */
40     static public function get()
41     {
42         return preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']);
43     }
44    
45    
46     /**
47     * Returns the current domain name, with protcol prefix
48     *
49     * @return string  The current domain name, prefixed by `http://` or `https://`
50     */
51     static public function getDomain()
52     {
53         return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'];
54     }
55    
56    
57     /**
58     * Returns the current query string, does not include parameters added by rewrites
59     *
60     * @return string  The query string
61     */
62     static public function getQueryString()
63     {
64         return preg_replace('#^[^?]*\??#', '', $_SERVER['REQUEST_URI']);
65     }
66    
67    
68     /**
69     * Returns the current URL including query string, but without domain name - does not include query string parameters from rewrites
70     *
71     * @return string  The URL with query string
72     */
73     static public function getWithQueryString()
74     {
75         return $_SERVER['REQUEST_URI'];
76     }
77    
78    
79     /**
80     * Changes a string into a URL-friendly string
81     *
82     * @param  string $string  The string to convert
83     * @return void
84     */
85     static public function makeFriendly($string)
86     {
87         $string = fHTML::decode(fUTF8::ascii($string));
88         $string = strtolower(trim($string));
89         $string = str_replace("'", '', $string);
90         $string = preg_replace('#[^a-z0-9\-]+#', '_', $string);
91         return preg_replace('#_+$#', '', $string);
92     }
93    
94    
95     /**
96     * Redirects to the URL specified, if the URL does not start with `http://` or `https://` it redirects to current site
97     *
98     * @param  string $url  The url to redirect to
99     * @return void
100     */
101     static public function redirect($url)
102     {
103         if (strpos($url, '/') === 0) {
104             $url = self::getDomain() . $url;
105         } elseif (!preg_match('#^https?://#i', $url)) {
106             $url = self::getDomain() . self::get() . $url;
107         }
108        
109         // Strip the ? if there are no query string parameters
110         if (substr($url, -1) == '?') {
111             $url = substr($url, 0, -1);
112         }
113        
114         header('Location: ' . $url);
115         exit($url);
116     }
117    
118    
119     /**
120     * Removes one or more parameters from the query string
121     *
122     * This method uses the query string from the original URL and will not
123     * contain any parameters that are from rewrites.
124     *
125     * @param  string $parameter  A parameter to remove from the query string
126     * @param  string ...
127     * @return string  The query string with the parameter(s) specified removed, first character is `?`
128     */
129     static public function removeFromQueryString()
130     {
131         $parameters = func_get_args();
132        
133         parse_str(self::getQueryString(), $qs_array);
134         if (get_magic_quotes_gpc()) {
135             $qs_array = array_map('stripslashes', $qs_array);
136         }
137        
138         foreach ($parameters as $parameter) {
139             unset($qs_array[$parameter]);
140         }
141        
142         return '?' . http_build_query($qs_array, '', '&');
143     }
144    
145    
146     /**
147     * Replaces a value in the query string
148     *
149     * This method uses the query string from the original URL and will not
150     * contain any parameters that are from rewrites.
151     *
152     * @param  string|array  $parameter  The query string parameter
153     * @param  string|array  $value      The value to set the parameter to
154     * @return string  The full query string with the parameter replaced, first char is `?`
155     */
156     static public function replaceInQueryString($parameter, $value)
157     {
158         parse_str(self::getQueryString(), $qs_array);
159         if (get_magic_quotes_gpc()) {
160             $qs_array = array_map('stripslashes', $qs_array);
161         }
162        
163         settype($parameter, 'array');
164         settype($value, 'array');
165        
166         if (sizeof($parameter) != sizeof($value)) {
167             fCore::toss(
168                 'fProgrammerException',
169                 fGrammar::compose(
170                     "There are a different number of parameters and values.\nParameters:\n%1\$s\nValues\n%2\$s",
171                     fCore::dump($parameter),
172                     fCore::dump($value)
173                 )
174             );
175         }
176        
177         for ($i=0; $i<sizeof($parameter); $i++) {
178             $qs_array[$parameter[$i]] = $value[$i];
179         }
180        
181         return '?' . http_build_query($qs_array, '', '&');
182     }
183    
184    
185     /**
186     * Forces use as a static class
187     *
188     * @return fURL
189     */
190     private function __construct() { }
191 }
192  
193  
194  
195 /**
196  * Copyright (c) 2007-2008 William Bond <will@flourishlib.com>
197  *
198  * Permission is hereby granted, free of charge, to any person obtaining a copy
199  * of this software and associated documentation files (the "Software"), to deal
200  * in the Software without restriction, including without limitation the rights
201  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
202  * copies of the Software, and to permit persons to whom the Software is
203  * furnished to do so, subject to the following conditions:
204  *
205  * The above copyright notice and this permission notice shall be included in
206  * all copies or substantial portions of the Software.
207  *
208  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
209  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
210  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
211  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
212  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
213  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
214  * THE SOFTWARE.
215  */