blob: bebfd25835706ae74d953312f3d624fb43659f61 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevdebcc362012-01-07 02:05:29 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreevdebcc362012-01-07 02:05:29 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter URL Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/url_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('site_url'))
53{
Timothy Warrenb75faa12012-04-27 12:03:32 -040054 /**
55 * Site URL
56 *
57 * Create a local URL based on your basepath. Segments can be passed via the
58 * first parameter either as a string or an array.
59 *
Andrey Andreev2023c3d2013-07-18 03:19:59 +030060 * @param string $uri
61 * @param string $protocol
Timothy Warrenb75faa12012-04-27 12:03:32 -040062 * @return string
63 */
Andrey Andreev56c8ca62013-07-18 03:21:16 +030064 function site_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
vlakoff4c07fce2013-10-25 01:20:32 +020066 return get_instance()->config->site_url($uri, $protocol);
Derek Allard2067d1a2008-11-13 22:59:24 +000067 }
68}
69
70// ------------------------------------------------------------------------
71
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('base_url'))
73{
Timothy Warrenb75faa12012-04-27 12:03:32 -040074 /**
75 * Base URL
76 *
77 * Create a local URL based on your basepath.
78 * Segments can be passed in as a string or an array, same as site_url
79 * or a URL to a file can be passed in, e.g. to an image file.
80 *
Andrey Andreev2023c3d2013-07-18 03:19:59 +030081 * @param string $uri
82 * @param string $protocol
Timothy Warrenb75faa12012-04-27 12:03:32 -040083 * @return string
84 */
Andrey Andreev2023c3d2013-07-18 03:19:59 +030085 function base_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
vlakoff4c07fce2013-10-25 01:20:32 +020087 return get_instance()->config->base_url($uri, $protocol);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
89}
90
91// ------------------------------------------------------------------------
92
Derek Allard2067d1a2008-11-13 22:59:24 +000093if ( ! function_exists('current_url'))
94{
Timothy Warrenb75faa12012-04-27 12:03:32 -040095 /**
96 * Current URL
97 *
98 * Returns the full URL (including segments) of the page where this
99 * function is placed
100 *
101 * @return string
102 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 function current_url()
104 {
Andrey Andreev4ea76cc2014-01-08 21:49:23 +0200105 $CI =& get_instance();
106 return $CI->config->site_url($CI->uri->uri_string());
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
108}
109
110// ------------------------------------------------------------------------
Timothy Warrenb75faa12012-04-27 12:03:32 -0400111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112if ( ! function_exists('uri_string'))
113{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400114 /**
115 * URL String
116 *
117 * Returns the URI segments.
118 *
119 * @return string
120 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 function uri_string()
122 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200123 return get_instance()->uri->uri_string();
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
125}
126
127// ------------------------------------------------------------------------
128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129if ( ! function_exists('index_page'))
130{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400131 /**
132 * Index page
133 *
134 * Returns the "index_page" from your config file
135 *
136 * @return string
137 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 function index_page()
139 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200140 return get_instance()->config->item('index_page');
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142}
143
144// ------------------------------------------------------------------------
145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146if ( ! function_exists('anchor'))
147{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400148 /**
149 * Anchor Link
150 *
151 * Creates an anchor based on the local URL.
152 *
153 * @param string the URL
154 * @param string the link title
155 * @param mixed any attributes
156 * @return string
157 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 function anchor($uri = '', $title = '', $attributes = '')
159 {
160 $title = (string) $title;
161
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200162 $site_url = is_array($uri)
163 ? site_url($uri)
Andrey Andreevc14aa9b2015-04-12 14:23:47 +0300164 : (preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri));
Derek Allard2067d1a2008-11-13 22:59:24 +0000165
Alex Bilbie773ccc32012-06-02 11:11:08 +0100166 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
168 $title = $site_url;
169 }
170
Alex Bilbie773ccc32012-06-02 11:11:08 +0100171 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Eric Barnesacedd2b2012-07-29 00:15:40 -0400173 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
177 }
178}
179
180// ------------------------------------------------------------------------
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182if ( ! function_exists('anchor_popup'))
183{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400184 /**
185 * Anchor Link - Pop-up version
186 *
187 * Creates an anchor based on the local URL. The link
188 * opens a new window based on the attributes specified.
189 *
190 * @param string the URL
191 * @param string the link title
192 * @param mixed any attributes
193 * @return string
194 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
196 {
197 $title = (string) $title;
Aaron Adams16800e42012-12-07 22:39:23 -0500198 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Alex Bilbie773ccc32012-06-02 11:11:08 +0100200 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $title = $site_url;
203 }
204
205 if ($attributes === FALSE)
206 {
Andrey Andreev81c32082012-06-16 21:21:46 +0300207 return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 if ( ! is_array($attributes))
211 {
Andrey Andreevf0a84102012-06-16 20:52:20 +0300212 $attributes = array($attributes);
Andrey Andreev81c32082012-06-16 21:21:46 +0300213
214 // Ref: http://www.w3schools.com/jsref/met_win_open.asp
215 $window_name = '_blank';
216 }
217 elseif ( ! empty($attributes['window_name']))
218 {
219 $window_name = $attributes['window_name'];
220 unset($attributes['window_name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
Andrey Andreev5286ef02014-07-06 19:57:59 +0300222 else
223 {
224 $window_name = '_blank';
225 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000226
Mian Saleemfc88a5f2014-07-31 22:08:07 +0800227 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'menubar' => 'no', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0') as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Andrey Andreev12220872012-03-26 21:38:56 +0300229 $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 unset($attributes[$key]);
231 }
232
Eric Barnesacedd2b2012-07-29 00:15:40 -0400233 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
Andrey Andreev81c32082012-06-16 21:21:46 +0300235 return '<a href="'.$site_url
Eric Barnesacedd2b2012-07-29 00:15:40 -0400236 .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
Andrey Andreev81c32082012-06-16 21:21:46 +0300237 .$attributes.'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239}
240
241// ------------------------------------------------------------------------
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243if ( ! function_exists('mailto'))
244{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400245 /**
246 * Mailto Link
247 *
248 * @param string the email address
249 * @param string the link title
250 * @param mixed any attributes
251 * @return string
252 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 function mailto($email, $title = '', $attributes = '')
254 {
255 $title = (string) $title;
256
Alex Bilbie773ccc32012-06-02 11:11:08 +0100257 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $title = $email;
260 }
261
Eric Barnesacedd2b2012-07-29 00:15:40 -0400262 return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264}
265
266// ------------------------------------------------------------------------
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268if ( ! function_exists('safe_mailto'))
269{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400270 /**
271 * Encoded Mailto Link
272 *
273 * Create a spam-protected mailto link written in Javascript
274 *
275 * @param string the email address
276 * @param string the link title
277 * @param mixed any attributes
278 * @return string
279 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 function safe_mailto($email, $title = '', $attributes = '')
281 {
282 $title = (string) $title;
283
Alex Bilbie773ccc32012-06-02 11:11:08 +0100284 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 $title = $email;
287 }
288
Andrey Andreevdebcc362012-01-07 02:05:29 +0200289 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000290
Andrey Andreevdebcc362012-01-07 02:05:29 +0200291 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200293 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295
296 $x[] = '"';
297
Alex Bilbie773ccc32012-06-02 11:11:08 +0100298 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
300 if (is_array($attributes))
301 {
302 foreach ($attributes as $key => $val)
303 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200304 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200305 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200307 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 $x[] = '"';
310 }
311 }
312 else
313 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200314 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200316 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 }
319 }
320
321 $x[] = '>';
322
323 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200324 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 $ordinal = ord($title[$i]);
327
328 if ($ordinal < 128)
329 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200330 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332 else
333 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200334 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $count = ($ordinal < 224) ? 2 : 3;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200340 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200342 $number = ($count === 3)
343 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
344 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
345 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $count = 1;
347 $temp = array();
348 }
349 }
350 }
351
352 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
353
354 $x = array_reverse($x);
Derek Allard2067d1a2008-11-13 22:59:24 +0000355
Andrey Andreevcfaf8c42014-02-15 21:58:45 +0200356 $output = "<script type=\"text/javascript\">\n"
357 ."\t//<![CDATA[\n"
358 ."\tvar l=new Array();\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000359
Andrey Andreevcfaf8c42014-02-15 21:58:45 +0200360 for ($i = 0, $c = count($x); $i < $c; $i++)
361 {
362 $output .= "\tl[".$i."] = '".$x[$i]."';\n";
363 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
Andrey Andreevcfaf8c42014-02-15 21:58:45 +0200365 $output .= "\n\tfor (var i = l.length-1; i >= 0; i=i-1) {\n"
366 ."\t\tif (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");\n"
367 ."\t\telse document.write(unescape(l[i]));\n"
368 ."\t}\n"
369 ."\t//]]>\n"
370 .'</script>';
371
372 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374}
375
376// ------------------------------------------------------------------------
377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378if ( ! function_exists('auto_link'))
379{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400380 /**
381 * Auto-linker
382 *
383 * Automatically links URL and Email addresses.
384 * Note: There's a bit of extra code here to deal with
385 * URLs or emails that end in a period. We'll strip these
386 * off and add them after the link.
387 *
388 * @param string the string
389 * @param string the type: email, url, or both
390 * @param bool whether to create pop-up links
391 * @return string
392 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 function auto_link($str, $type = 'both', $popup = FALSE)
394 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600395 // Find and replace any URLs.
Andrey Andreev8c9e5102017-11-10 15:02:42 +0200396 if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[a-z0-9]+(-+[a-z0-9]+)*(\.[a-z0-9]+(-+[a-z0-9]+)*)+(/([^\s()<>;]+\w)?/?)?#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER))
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600398 // Set our target HTML if using popup links.
Andrey Andreev4dab9f82018-01-31 23:56:21 +0200399 $target = ($popup) ? ' target="_blank" rel="noopener"' : '';
Eric Roberts32a28f52013-01-19 02:59:14 -0600400
Eric Roberts8093bd72013-01-17 18:12:47 -0600401 // We process the links in reverse order (last -> first) so that
402 // the returned string offsets from preg_match_all() are not
403 // moved as we add more HTML.
Andrey Andreev606fee02013-01-28 12:57:13 +0200404 foreach (array_reverse($matches) as $match)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200405 {
Andrey Andreev606fee02013-01-28 12:57:13 +0200406 // $match[0] is the matched string/link
407 // $match[1] is either a protocol prefix or 'www.'
408 //
409 // With PREG_OFFSET_CAPTURE, both of the above is an array,
410 // where the actual value is held in [0] and its offset at the [1] index.
411 $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
412 $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600415
Eric Roberts8093bd72013-01-17 18:12:47 -0600416 // Find and replace any emails.
417 if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600419 foreach (array_reverse($matches[0]) as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600421 if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200422 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600423 $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
Andrey Andreev929e1242012-10-19 10:09:28 +0300424 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 }
426 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 return $str;
429 }
430}
431
432// ------------------------------------------------------------------------
433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434if ( ! function_exists('prep_url'))
435{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400436 /**
437 * Prep URL
438 *
439 * Simply adds the http:// part if no scheme is included
440 *
441 * @param string the URL
442 * @return string
443 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 function prep_url($str = '')
445 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100446 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 return '';
449 }
450
Robin Sowelld2167a02010-09-14 15:05:42 -0400451 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200452
Robin Sowelld2167a02010-09-14 15:05:42 -0400453 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200455 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457
458 return $str;
459 }
460}
461
462// ------------------------------------------------------------------------
463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464if ( ! function_exists('url_title'))
465{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400466 /**
467 * Create URL Title
468 *
469 * Takes a "title" string as input and creates a
470 * human-friendly URL string with a "separator" string
471 * as the word separator.
472 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200473 * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
474 * @param string $str Input string
475 * @param string $separator Word separator
476 * (usually '-' or '_')
Michael Foss9409a2a2015-06-12 20:22:39 -0400477 * @param bool $lowercase Whether to transform the output string to lowercase
Timothy Warrenb75faa12012-04-27 12:03:32 -0400478 * @return string
479 */
tubalmartin1a697102012-03-04 16:01:11 +0100480 function url_title($str, $separator = '-', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200482 if ($separator === 'dash')
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreev12220872012-03-26 21:38:56 +0300484 $separator = '-';
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Andrey Andreev12220872012-03-26 21:38:56 +0300486 elseif ($separator === 'underscore')
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev12220872012-03-26 21:38:56 +0300488 $separator = '_';
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Andrey Andreev12220872012-03-26 21:38:56 +0300490
Andrey Andreeve4742582012-10-25 13:25:13 +0300491 $q_separator = preg_quote($separator, '#');
Derek Allard2067d1a2008-11-13 22:59:24 +0000492
493 $trans = array(
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200494 '&.+?;' => '',
dimonneon32bdf1c2015-07-10 21:47:04 +0300495 '[^\w\d _-]' => '',
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200496 '\s+' => $separator,
497 '('.$q_separator.')+' => $separator
498 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
500 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 foreach ($trans as $key => $val)
502 {
dimonneon28c83052015-07-13 11:54:05 +0300503 $str = preg_replace('#'.$key.'#i'.(UTF8_ENABLED ? 'u' : ''), $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
Derek Jones40a2fc82008-12-09 19:41:25 +0000506 if ($lowercase === TRUE)
507 {
508 $str = strtolower($str);
509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Phil Sturgeona2bd3632012-03-04 15:32:58 +0000511 return trim(trim($str, $separator));
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513}
514
515// ------------------------------------------------------------------------
516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517if ( ! function_exists('redirect'))
518{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400519 /**
520 * Header Redirect
521 *
522 * Header redirect in two flavors
523 * For very fine grained control over headers, you could use the Output
524 * Library's set_header() function.
525 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200526 * @param string $uri URL
527 * @param string $method Redirect method
528 * 'auto', 'location' or 'refresh'
529 * @param int $code HTTP Response status code
530 * @return void
Timothy Warrenb75faa12012-04-27 12:03:32 -0400531 */
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300532 function redirect($uri = '', $method = 'auto', $code = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Aaron Adams16800e42012-12-07 22:39:23 -0500534 if ( ! preg_match('#^(\w+:)?//#i', $uri))
Derek Jones534be032009-02-10 18:47:47 +0000535 {
536 $uri = site_url($uri);
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500539 // IIS environment likely? Use 'refresh' for better compatibility
vlakoffaab26a12012-09-11 13:10:21 +0200540 if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500541 {
542 $method = 'refresh';
543 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300544 elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
545 {
Andrey Andreeva0a73c92014-01-09 19:21:26 +0200546 if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
547 {
548 $code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
549 ? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
550 : 307;
551 }
552 else
553 {
554 $code = 302;
555 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300556 }
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500557
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300558 switch ($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200560 case 'refresh':
561 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200563 default:
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300564 header('Location: '.$uri, TRUE, $code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 break;
566 }
567 exit;
568 }
569}