blob: dc77246dc369fa569bd4b52538928dd1748e748b [file] [log] [blame]
Andrey Andreevdebcc362012-01-07 02:05:29 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter URL Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/url_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('site_url'))
41{
Timothy Warrenb75faa12012-04-27 12:03:32 -040042 /**
43 * Site URL
44 *
45 * Create a local URL based on your basepath. Segments can be passed via the
46 * first parameter either as a string or an array.
47 *
48 * @param string
49 * @return string
50 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051 function site_url($uri = '')
52 {
53 $CI =& get_instance();
54 return $CI->config->site_url($uri);
55 }
56}
57
58// ------------------------------------------------------------------------
59
Derek Allard2067d1a2008-11-13 22:59:24 +000060if ( ! function_exists('base_url'))
61{
Timothy Warrenb75faa12012-04-27 12:03:32 -040062 /**
63 * Base URL
64 *
65 * Create a local URL based on your basepath.
66 * Segments can be passed in as a string or an array, same as site_url
67 * or a URL to a file can be passed in, e.g. to an image file.
68 *
69 * @param string
70 * @return string
71 */
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080072 function base_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
74 $CI =& get_instance();
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080075 return $CI->config->base_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
77}
78
79// ------------------------------------------------------------------------
80
Derek Allard2067d1a2008-11-13 22:59:24 +000081if ( ! function_exists('current_url'))
82{
Timothy Warrenb75faa12012-04-27 12:03:32 -040083 /**
84 * Current URL
85 *
86 * Returns the full URL (including segments) of the page where this
87 * function is placed
88 *
89 * @return string
90 */
Derek Allard2067d1a2008-11-13 22:59:24 +000091 function current_url()
92 {
93 $CI =& get_instance();
94 return $CI->config->site_url($CI->uri->uri_string());
95 }
96}
97
98// ------------------------------------------------------------------------
Timothy Warrenb75faa12012-04-27 12:03:32 -040099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100if ( ! function_exists('uri_string'))
101{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400102 /**
103 * URL String
104 *
105 * Returns the URI segments.
106 *
107 * @return string
108 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 function uri_string()
110 {
111 $CI =& get_instance();
112 return $CI->uri->uri_string();
113 }
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 {
129 $CI =& get_instance();
130 return $CI->config->item('index_page');
131 }
132}
133
134// ------------------------------------------------------------------------
135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136if ( ! function_exists('anchor'))
137{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400138 /**
139 * Anchor Link
140 *
141 * Creates an anchor based on the local URL.
142 *
143 * @param string the URL
144 * @param string the link title
145 * @param mixed any attributes
146 * @return string
147 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 function anchor($uri = '', $title = '', $attributes = '')
149 {
150 $title = (string) $title;
151
152 if ( ! is_array($uri))
153 {
Andrey Andreev12220872012-03-26 21:38:56 +0300154 $site_url = preg_match('!^\w+://! i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156 else
157 {
158 $site_url = site_url($uri);
159 }
160
Alex Bilbie773ccc32012-06-02 11:11:08 +0100161 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 $title = $site_url;
164 }
165
Alex Bilbie773ccc32012-06-02 11:11:08 +0100166 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Eric Barnesacedd2b2012-07-29 00:15:40 -0400168 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
172 }
173}
174
175// ------------------------------------------------------------------------
176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177if ( ! function_exists('anchor_popup'))
178{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400179 /**
180 * Anchor Link - Pop-up version
181 *
182 * Creates an anchor based on the local URL. The link
183 * opens a new window based on the attributes specified.
184 *
185 * @param string the URL
186 * @param string the link title
187 * @param mixed any attributes
188 * @return string
189 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
191 {
192 $title = (string) $title;
Andrey Andreev12220872012-03-26 21:38:56 +0300193 $site_url = preg_match('!^\w+://! i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
Alex Bilbie773ccc32012-06-02 11:11:08 +0100195 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 $title = $site_url;
198 }
199
200 if ($attributes === FALSE)
201 {
Andrey Andreev81c32082012-06-16 21:21:46 +0300202 return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 if ( ! is_array($attributes))
206 {
Andrey Andreevf0a84102012-06-16 20:52:20 +0300207 $attributes = array($attributes);
Andrey Andreev81c32082012-06-16 21:21:46 +0300208
209 // Ref: http://www.w3schools.com/jsref/met_win_open.asp
210 $window_name = '_blank';
211 }
212 elseif ( ! empty($attributes['window_name']))
213 {
214 $window_name = $attributes['window_name'];
215 unset($attributes['window_name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
217
Andrey Andreev81c32082012-06-16 21:21:46 +0300218 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 +0000219 {
Andrey Andreev12220872012-03-26 21:38:56 +0300220 $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 unset($attributes[$key]);
222 }
223
Eric Barnesacedd2b2012-07-29 00:15:40 -0400224 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225
Andrey Andreev81c32082012-06-16 21:21:46 +0300226 return '<a href="'.$site_url
Eric Barnesacedd2b2012-07-29 00:15:40 -0400227 .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
Andrey Andreev81c32082012-06-16 21:21:46 +0300228 .$attributes.'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230}
231
232// ------------------------------------------------------------------------
233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234if ( ! function_exists('mailto'))
235{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400236 /**
237 * Mailto Link
238 *
239 * @param string the email address
240 * @param string the link title
241 * @param mixed any attributes
242 * @return string
243 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 function mailto($email, $title = '', $attributes = '')
245 {
246 $title = (string) $title;
247
Alex Bilbie773ccc32012-06-02 11:11:08 +0100248 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 $title = $email;
251 }
252
Eric Barnesacedd2b2012-07-29 00:15:40 -0400253 return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
255}
256
257// ------------------------------------------------------------------------
258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259if ( ! function_exists('safe_mailto'))
260{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400261 /**
262 * Encoded Mailto Link
263 *
264 * Create a spam-protected mailto link written in Javascript
265 *
266 * @param string the email address
267 * @param string the link title
268 * @param mixed any attributes
269 * @return string
270 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 function safe_mailto($email, $title = '', $attributes = '')
272 {
273 $title = (string) $title;
274
Alex Bilbie773ccc32012-06-02 11:11:08 +0100275 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 $title = $email;
278 }
279
Andrey Andreevdebcc362012-01-07 02:05:29 +0200280 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000281
Andrey Andreevdebcc362012-01-07 02:05:29 +0200282 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200284 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286
287 $x[] = '"';
288
Alex Bilbie773ccc32012-06-02 11:11:08 +0100289 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 if (is_array($attributes))
292 {
293 foreach ($attributes as $key => $val)
294 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500295 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200296 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200298 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300 $x[] = '"';
301 }
302 }
303 else
304 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200305 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200307 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 }
310 }
311
312 $x[] = '>';
313
314 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200315 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 $ordinal = ord($title[$i]);
318
319 if ($ordinal < 128)
320 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200321 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323 else
324 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200325 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 $count = ($ordinal < 224) ? 2 : 3;
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200331 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200333 $number = ($count === 3)
334 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
335 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
336 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 $count = 1;
338 $temp = array();
339 }
340 }
341 }
342
343 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
344
345 $x = array_reverse($x);
346 ob_start();
347
348 ?><script type="text/javascript">
349 //<![CDATA[
350 var l=new Array();
351 <?php
Andrey Andreevdebcc362012-01-07 02:05:29 +0200352 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 +0000353
354 for (var i = l.length-1; i >= 0; i=i-1){
Alex Bilbie773ccc32012-06-02 11:11:08 +0100355 if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 else document.write(unescape(l[i]));}
357 //]]>
358 </script><?php
359
360 $buffer = ob_get_contents();
361 ob_end_clean();
362 return $buffer;
363 }
364}
365
366// ------------------------------------------------------------------------
367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368if ( ! function_exists('auto_link'))
369{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400370 /**
371 * Auto-linker
372 *
373 * Automatically links URL and Email addresses.
374 * Note: There's a bit of extra code here to deal with
375 * URLs or emails that end in a period. We'll strip these
376 * off and add them after the link.
377 *
378 * @param string the string
379 * @param string the type: email, url, or both
380 * @param bool whether to create pop-up links
381 * @return string
382 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 function auto_link($str, $type = 'both', $popup = FALSE)
384 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200385 if ($type !== 'email' && preg_match_all('#(^|\s|\(|\b)((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200387 $pop = ($popup) ? ' target="_blank" ' : '';
Barry Mienydd671972010-10-04 16:33:58 +0200388
Andrey Andreevdebcc362012-01-07 02:05:29 +0200389 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
390 {
Andrey Andreev929e1242012-10-19 10:09:28 +0300391 if (preg_match('/(\.|\,)$/i', $matches[6][$i], $m))
Andrey Andreevdebcc362012-01-07 02:05:29 +0200392 {
Andrey Andreev929e1242012-10-19 10:09:28 +0300393 $punct = $m[1];
Andrey Andreevdebcc362012-01-07 02:05:29 +0200394 $matches[6][$i] = substr($matches[6][$i], 0, -1);
395 }
396 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Andrey Andreev929e1242012-10-19 10:09:28 +0300398 $punct = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
Andrey Andreevdebcc362012-01-07 02:05:29 +0200400
401 $str = str_replace($matches[0][$i],
402 $matches[1][$i].'<a href="http'.$matches[4][$i].'://'
403 .$matches[5][$i].$matches[6][$i].'"'.$pop.'>http'
404 .$matches[4][$i].'://'.$matches[5][$i]
Andrey Andreev929e1242012-10-19 10:09:28 +0300405 .$matches[6][$i].'</a>'.$punct,
Andrey Andreevdebcc362012-01-07 02:05:29 +0200406 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408 }
409
Andrey Andreev929e1242012-10-19 10:09:28 +0300410 if ($type !== 'url' && preg_match_all('/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]+)/i', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200412 for ($i = 0, $c = count($matches); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev929e1242012-10-19 10:09:28 +0300414 if (preg_match('/(\.|\,)$/i', $matches[3][$i], $m))
Andrey Andreevdebcc362012-01-07 02:05:29 +0200415 {
Andrey Andreev929e1242012-10-19 10:09:28 +0300416 $punct = $m[1];
Andrey Andreevdebcc362012-01-07 02:05:29 +0200417 $matches[3][$i] = substr($matches[3][$i], 0, -1);
418 }
419 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreev929e1242012-10-19 10:09:28 +0300421 $punct = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Andrey Andreevdebcc362012-01-07 02:05:29 +0200423
Andrey Andreev929e1242012-10-19 10:09:28 +0300424 if (filter_var(($m = $matches[1][$i].'@'.$matches[2][$i].'.'.$matches[3][$i]), FILTER_VALIDATE_EMAIL) !== FALSE)
425 {
426 $str = str_replace($matches[0][$i], safe_mailto($m).$punct, $str);
427 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429 }
430
431 return $str;
432 }
433}
434
435// ------------------------------------------------------------------------
436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437if ( ! function_exists('prep_url'))
438{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400439 /**
440 * Prep URL
441 *
442 * Simply adds the http:// part if no scheme is included
443 *
444 * @param string the URL
445 * @return string
446 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 function prep_url($str = '')
448 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100449 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 return '';
452 }
453
Robin Sowelld2167a02010-09-14 15:05:42 -0400454 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200455
Robin Sowelld2167a02010-09-14 15:05:42 -0400456 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200458 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460
461 return $str;
462 }
463}
464
465// ------------------------------------------------------------------------
466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467if ( ! function_exists('url_title'))
468{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400469 /**
470 * Create URL Title
471 *
472 * Takes a "title" string as input and creates a
473 * human-friendly URL string with a "separator" string
474 * as the word separator.
475 *
476 * @param string the string
477 * @param string the separator
478 * @param bool
479 * @return string
480 */
tubalmartin1a697102012-03-04 16:01:11 +0100481 function url_title($str, $separator = '-', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200483 if ($separator === 'dash')
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreev12220872012-03-26 21:38:56 +0300485 $separator = '-';
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
Andrey Andreev12220872012-03-26 21:38:56 +0300487 elseif ($separator === 'underscore')
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Andrey Andreev12220872012-03-26 21:38:56 +0300489 $separator = '_';
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
Andrey Andreev12220872012-03-26 21:38:56 +0300491
Andrey Andreeve4742582012-10-25 13:25:13 +0300492 $q_separator = preg_quote($separator, '#');
Derek Allard2067d1a2008-11-13 22:59:24 +0000493
494 $trans = array(
Andrey Andreev12220872012-03-26 21:38:56 +0300495 '&.+?;' => '',
496 '[^a-z0-9 _-]' => '',
497 '\s+' => $separator,
498 '('.$q_separator.')+' => $separator
499 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000500
501 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 foreach ($trans as $key => $val)
503 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200504 $str = preg_replace('#'.$key.'#i', $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506
Derek Jones40a2fc82008-12-09 19:41:25 +0000507 if ($lowercase === TRUE)
508 {
509 $str = strtolower($str);
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Phil Sturgeona2bd3632012-03-04 15:32:58 +0000512 return trim(trim($str, $separator));
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514}
515
516// ------------------------------------------------------------------------
517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518if ( ! function_exists('redirect'))
519{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400520 /**
521 * Header Redirect
522 *
523 * Header redirect in two flavors
524 * For very fine grained control over headers, you could use the Output
525 * Library's set_header() function.
526 *
527 * @param string the URL
528 * @param string the method: location or refresh
529 * @param int
530 * @return string
531 */
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300532 function redirect($uri = '', $method = 'auto', $code = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Derek Jones534be032009-02-10 18:47:47 +0000534 if ( ! preg_match('#^https?://#i', $uri))
535 {
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 {
546 // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
547 $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'])
548 && $_SERVER['REQUEST_METHOD'] === 'POST'
549 && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
550 ? 303 : 302;
551 }
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500552
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300553 switch ($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200555 case 'refresh':
556 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200558 default:
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300559 header('Location: '.$uri, TRUE, $code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 break;
561 }
562 exit;
563 }
564}
565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566/* End of file url_helper.php */
Andrey Andreev12220872012-03-26 21:38:56 +0300567/* Location: ./system/helpers/url_helper.php */