blob: f9650cd04c416b831faa34035761f49fb9873930 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevdebcc362012-01-07 02:05:29 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevdebcc362012-01-07 02:05:29 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter URL Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/url_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('site_url'))
42{
Timothy Warrenb75faa12012-04-27 12:03:32 -040043 /**
44 * Site URL
45 *
46 * Create a local URL based on your basepath. Segments can be passed via the
47 * first parameter either as a string or an array.
48 *
Andrey Andreev2023c3d2013-07-18 03:19:59 +030049 * @param string $uri
50 * @param string $protocol
Timothy Warrenb75faa12012-04-27 12:03:32 -040051 * @return string
52 */
Andrey Andreev56c8ca62013-07-18 03:21:16 +030053 function site_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
vlakoff4c07fce2013-10-25 01:20:32 +020055 return get_instance()->config->site_url($uri, $protocol);
Derek Allard2067d1a2008-11-13 22:59:24 +000056 }
57}
58
59// ------------------------------------------------------------------------
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061if ( ! function_exists('base_url'))
62{
Timothy Warrenb75faa12012-04-27 12:03:32 -040063 /**
64 * Base URL
65 *
66 * Create a local URL based on your basepath.
67 * Segments can be passed in as a string or an array, same as site_url
68 * or a URL to a file can be passed in, e.g. to an image file.
69 *
Andrey Andreev2023c3d2013-07-18 03:19:59 +030070 * @param string $uri
71 * @param string $protocol
Timothy Warrenb75faa12012-04-27 12:03:32 -040072 * @return string
73 */
Andrey Andreev2023c3d2013-07-18 03:19:59 +030074 function base_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
vlakoff4c07fce2013-10-25 01:20:32 +020076 return get_instance()->config->base_url($uri, $protocol);
Derek Allard2067d1a2008-11-13 22:59:24 +000077 }
78}
79
80// ------------------------------------------------------------------------
81
Derek Allard2067d1a2008-11-13 22:59:24 +000082if ( ! function_exists('current_url'))
83{
Timothy Warrenb75faa12012-04-27 12:03:32 -040084 /**
85 * Current URL
86 *
87 * Returns the full URL (including segments) of the page where this
88 * function is placed
89 *
90 * @return string
91 */
Derek Allard2067d1a2008-11-13 22:59:24 +000092 function current_url()
93 {
Andrey Andreev4ea76cc2014-01-08 21:49:23 +020094 $CI =& get_instance();
95 return $CI->config->site_url($CI->uri->uri_string());
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
97}
98
99// ------------------------------------------------------------------------
Timothy Warrenb75faa12012-04-27 12:03:32 -0400100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101if ( ! function_exists('uri_string'))
102{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400103 /**
104 * URL String
105 *
106 * Returns the URI segments.
107 *
108 * @return string
109 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 function uri_string()
111 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200112 return get_instance()->uri->uri_string();
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114}
115
116// ------------------------------------------------------------------------
117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118if ( ! function_exists('index_page'))
119{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400120 /**
121 * Index page
122 *
123 * Returns the "index_page" from your config file
124 *
125 * @return string
126 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 function index_page()
128 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200129 return get_instance()->config->item('index_page');
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131}
132
133// ------------------------------------------------------------------------
134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135if ( ! function_exists('anchor'))
136{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400137 /**
138 * Anchor Link
139 *
140 * Creates an anchor based on the local URL.
141 *
142 * @param string the URL
143 * @param string the link title
144 * @param mixed any attributes
145 * @return string
146 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 function anchor($uri = '', $title = '', $attributes = '')
148 {
149 $title = (string) $title;
150
151 if ( ! is_array($uri))
152 {
Aaron Adams16800e42012-12-07 22:39:23 -0500153 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155 else
156 {
157 $site_url = site_url($uri);
158 }
159
Alex Bilbie773ccc32012-06-02 11:11:08 +0100160 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 $title = $site_url;
163 }
164
Alex Bilbie773ccc32012-06-02 11:11:08 +0100165 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Eric Barnesacedd2b2012-07-29 00:15:40 -0400167 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
171 }
172}
173
174// ------------------------------------------------------------------------
175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176if ( ! function_exists('anchor_popup'))
177{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400178 /**
179 * Anchor Link - Pop-up version
180 *
181 * Creates an anchor based on the local URL. The link
182 * opens a new window based on the attributes specified.
183 *
184 * @param string the URL
185 * @param string the link title
186 * @param mixed any attributes
187 * @return string
188 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
190 {
191 $title = (string) $title;
Aaron Adams16800e42012-12-07 22:39:23 -0500192 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
Alex Bilbie773ccc32012-06-02 11:11:08 +0100194 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 $title = $site_url;
197 }
198
199 if ($attributes === FALSE)
200 {
Andrey Andreev81c32082012-06-16 21:21:46 +0300201 return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 if ( ! is_array($attributes))
205 {
Andrey Andreevf0a84102012-06-16 20:52:20 +0300206 $attributes = array($attributes);
Andrey Andreev81c32082012-06-16 21:21:46 +0300207
208 // Ref: http://www.w3schools.com/jsref/met_win_open.asp
209 $window_name = '_blank';
210 }
211 elseif ( ! empty($attributes['window_name']))
212 {
213 $window_name = $attributes['window_name'];
214 unset($attributes['window_name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
Andrey Andreev81c32082012-06-16 21:21:46 +0300217 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0') as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev12220872012-03-26 21:38:56 +0300219 $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 unset($attributes[$key]);
221 }
222
Eric Barnesacedd2b2012-07-29 00:15:40 -0400223 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000224
Andrey Andreev81c32082012-06-16 21:21:46 +0300225 return '<a href="'.$site_url
Eric Barnesacedd2b2012-07-29 00:15:40 -0400226 .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
Andrey Andreev81c32082012-06-16 21:21:46 +0300227 .$attributes.'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229}
230
231// ------------------------------------------------------------------------
232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233if ( ! function_exists('mailto'))
234{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400235 /**
236 * Mailto Link
237 *
238 * @param string the email address
239 * @param string the link title
240 * @param mixed any attributes
241 * @return string
242 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 function mailto($email, $title = '', $attributes = '')
244 {
245 $title = (string) $title;
246
Alex Bilbie773ccc32012-06-02 11:11:08 +0100247 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 $title = $email;
250 }
251
Eric Barnesacedd2b2012-07-29 00:15:40 -0400252 return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254}
255
256// ------------------------------------------------------------------------
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258if ( ! function_exists('safe_mailto'))
259{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400260 /**
261 * Encoded Mailto Link
262 *
263 * Create a spam-protected mailto link written in Javascript
264 *
265 * @param string the email address
266 * @param string the link title
267 * @param mixed any attributes
268 * @return string
269 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 function safe_mailto($email, $title = '', $attributes = '')
271 {
272 $title = (string) $title;
273
Alex Bilbie773ccc32012-06-02 11:11:08 +0100274 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 $title = $email;
277 }
278
Andrey Andreevdebcc362012-01-07 02:05:29 +0200279 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280
Andrey Andreevdebcc362012-01-07 02:05:29 +0200281 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200283 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
285
286 $x[] = '"';
287
Alex Bilbie773ccc32012-06-02 11:11:08 +0100288 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
290 if (is_array($attributes))
291 {
292 foreach ($attributes as $key => $val)
293 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200294 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200295 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200297 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299 $x[] = '"';
300 }
301 }
302 else
303 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200304 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200306 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 }
309 }
310
311 $x[] = '>';
312
313 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200314 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 $ordinal = ord($title[$i]);
317
318 if ($ordinal < 128)
319 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200320 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322 else
323 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200324 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
326 $count = ($ordinal < 224) ? 2 : 3;
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200330 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200332 $number = ($count === 3)
333 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
334 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
335 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 $count = 1;
337 $temp = array();
338 }
339 }
340 }
341
342 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
343
344 $x = array_reverse($x);
345 ob_start();
346
347 ?><script type="text/javascript">
348 //<![CDATA[
349 var l=new Array();
350 <?php
Andrey Andreevdebcc362012-01-07 02:05:29 +0200351 for ($i = 0, $c = count($x); $i < $c; $i++) { ?>l[<?php echo $i; ?>]='<?php echo $x[$i]; ?>';<?php } ?>
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 for (var i = l.length-1; i >= 0; i=i-1){
Alex Bilbie773ccc32012-06-02 11:11:08 +0100354 if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 else document.write(unescape(l[i]));}
356 //]]>
357 </script><?php
358
359 $buffer = ob_get_contents();
360 ob_end_clean();
361 return $buffer;
362 }
363}
364
365// ------------------------------------------------------------------------
366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367if ( ! function_exists('auto_link'))
368{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400369 /**
370 * Auto-linker
371 *
372 * Automatically links URL and Email addresses.
373 * Note: There's a bit of extra code here to deal with
374 * URLs or emails that end in a period. We'll strip these
375 * off and add them after the link.
376 *
377 * @param string the string
378 * @param string the type: email, url, or both
379 * @param bool whether to create pop-up links
380 * @return string
381 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 function auto_link($str, $type = 'both', $popup = FALSE)
383 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600384 // Find and replace any URLs.
Andrey Andreev606fee02013-01-28 12:57:13 +0200385 if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER))
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600387 // Set our target HTML if using popup links.
Andrey Andreev606fee02013-01-28 12:57:13 +0200388 $target = ($popup) ? ' target="_blank"' : '';
Eric Roberts32a28f52013-01-19 02:59:14 -0600389
Eric Roberts8093bd72013-01-17 18:12:47 -0600390 // We process the links in reverse order (last -> first) so that
391 // the returned string offsets from preg_match_all() are not
392 // moved as we add more HTML.
Andrey Andreev606fee02013-01-28 12:57:13 +0200393 foreach (array_reverse($matches) as $match)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200394 {
Andrey Andreev606fee02013-01-28 12:57:13 +0200395 // $match[0] is the matched string/link
396 // $match[1] is either a protocol prefix or 'www.'
397 //
398 // With PREG_OFFSET_CAPTURE, both of the above is an array,
399 // where the actual value is held in [0] and its offset at the [1] index.
400 $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
401 $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600404
Eric Roberts8093bd72013-01-17 18:12:47 -0600405 // Find and replace any emails.
406 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 +0000407 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600408 foreach (array_reverse($matches[0]) as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600410 if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200411 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600412 $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
Andrey Andreev929e1242012-10-19 10:09:28 +0300413 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 return $str;
418 }
419}
420
421// ------------------------------------------------------------------------
422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423if ( ! function_exists('prep_url'))
424{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400425 /**
426 * Prep URL
427 *
428 * Simply adds the http:// part if no scheme is included
429 *
430 * @param string the URL
431 * @return string
432 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 function prep_url($str = '')
434 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100435 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
437 return '';
438 }
439
Robin Sowelld2167a02010-09-14 15:05:42 -0400440 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200441
Robin Sowelld2167a02010-09-14 15:05:42 -0400442 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200444 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446
447 return $str;
448 }
449}
450
451// ------------------------------------------------------------------------
452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453if ( ! function_exists('url_title'))
454{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400455 /**
456 * Create URL Title
457 *
458 * Takes a "title" string as input and creates a
459 * human-friendly URL string with a "separator" string
460 * as the word separator.
461 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200462 * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
463 * @param string $str Input string
464 * @param string $separator Word separator
465 * (usually '-' or '_')
466 * @param bool $lowercase Wether to transform the output string to lowercase
Timothy Warrenb75faa12012-04-27 12:03:32 -0400467 * @return string
468 */
tubalmartin1a697102012-03-04 16:01:11 +0100469 function url_title($str, $separator = '-', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200471 if ($separator === 'dash')
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Andrey Andreev12220872012-03-26 21:38:56 +0300473 $separator = '-';
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Andrey Andreev12220872012-03-26 21:38:56 +0300475 elseif ($separator === 'underscore')
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreev12220872012-03-26 21:38:56 +0300477 $separator = '_';
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Andrey Andreev12220872012-03-26 21:38:56 +0300479
Andrey Andreeve4742582012-10-25 13:25:13 +0300480 $q_separator = preg_quote($separator, '#');
Derek Allard2067d1a2008-11-13 22:59:24 +0000481
482 $trans = array(
Andrey Andreev12220872012-03-26 21:38:56 +0300483 '&.+?;' => '',
484 '[^a-z0-9 _-]' => '',
485 '\s+' => $separator,
486 '('.$q_separator.')+' => $separator
487 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000488
489 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 foreach ($trans as $key => $val)
491 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200492 $str = preg_replace('#'.$key.'#i', $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
494
Derek Jones40a2fc82008-12-09 19:41:25 +0000495 if ($lowercase === TRUE)
496 {
497 $str = strtolower($str);
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Phil Sturgeona2bd3632012-03-04 15:32:58 +0000500 return trim(trim($str, $separator));
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502}
503
504// ------------------------------------------------------------------------
505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506if ( ! function_exists('redirect'))
507{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400508 /**
509 * Header Redirect
510 *
511 * Header redirect in two flavors
512 * For very fine grained control over headers, you could use the Output
513 * Library's set_header() function.
514 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200515 * @param string $uri URL
516 * @param string $method Redirect method
517 * 'auto', 'location' or 'refresh'
518 * @param int $code HTTP Response status code
519 * @return void
Timothy Warrenb75faa12012-04-27 12:03:32 -0400520 */
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300521 function redirect($uri = '', $method = 'auto', $code = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
Aaron Adams16800e42012-12-07 22:39:23 -0500523 if ( ! preg_match('#^(\w+:)?//#i', $uri))
Derek Jones534be032009-02-10 18:47:47 +0000524 {
525 $uri = site_url($uri);
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500528 // IIS environment likely? Use 'refresh' for better compatibility
vlakoffaab26a12012-09-11 13:10:21 +0200529 if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500530 {
531 $method = 'refresh';
532 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300533 elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
534 {
Andrey Andreeva0a73c92014-01-09 19:21:26 +0200535 if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
536 {
537 $code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
538 ? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
539 : 307;
540 }
541 else
542 {
543 $code = 302;
544 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300545 }
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500546
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300547 switch ($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200549 case 'refresh':
550 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200552 default:
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300553 header('Location: '.$uri, TRUE, $code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 break;
555 }
556 exit;
557 }
558}
559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560/* End of file url_helper.php */
Andrey Andreev12220872012-03-26 21:38:56 +0300561/* Location: ./system/helpers/url_helper.php */