blob: 8be327276518b61a46dec66fdad9d6d440ffa696 [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 *
49 * @param string
50 * @return string
51 */
Derek Allard2067d1a2008-11-13 22:59:24 +000052 function site_url($uri = '')
53 {
54 $CI =& get_instance();
55 return $CI->config->site_url($uri);
56 }
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 *
70 * @param string
71 * @return string
72 */
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080073 function base_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
75 $CI =& get_instance();
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080076 return $CI->config->base_url($uri);
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 {
94 $CI =& get_instance();
95 return $CI->config->site_url($CI->uri->uri_string());
96 }
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 {
112 $CI =& get_instance();
113 return $CI->uri->uri_string();
114 }
115}
116
117// ------------------------------------------------------------------------
118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119if ( ! function_exists('index_page'))
120{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400121 /**
122 * Index page
123 *
124 * Returns the "index_page" from your config file
125 *
126 * @return string
127 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 function index_page()
129 {
130 $CI =& get_instance();
131 return $CI->config->item('index_page');
132 }
133}
134
135// ------------------------------------------------------------------------
136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137if ( ! function_exists('anchor'))
138{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400139 /**
140 * Anchor Link
141 *
142 * Creates an anchor based on the local URL.
143 *
144 * @param string the URL
145 * @param string the link title
146 * @param mixed any attributes
147 * @return string
148 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 function anchor($uri = '', $title = '', $attributes = '')
150 {
151 $title = (string) $title;
152
153 if ( ! is_array($uri))
154 {
Aaron Adams16800e42012-12-07 22:39:23 -0500155 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157 else
158 {
159 $site_url = site_url($uri);
160 }
161
Alex Bilbie773ccc32012-06-02 11:11:08 +0100162 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 $title = $site_url;
165 }
166
Alex Bilbie773ccc32012-06-02 11:11:08 +0100167 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Eric Barnesacedd2b2012-07-29 00:15:40 -0400169 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
173 }
174}
175
176// ------------------------------------------------------------------------
177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178if ( ! function_exists('anchor_popup'))
179{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400180 /**
181 * Anchor Link - Pop-up version
182 *
183 * Creates an anchor based on the local URL. The link
184 * opens a new window based on the attributes specified.
185 *
186 * @param string the URL
187 * @param string the link title
188 * @param mixed any attributes
189 * @return string
190 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
192 {
193 $title = (string) $title;
Aaron Adams16800e42012-12-07 22:39:23 -0500194 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
Alex Bilbie773ccc32012-06-02 11:11:08 +0100196 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 $title = $site_url;
199 }
200
201 if ($attributes === FALSE)
202 {
Andrey Andreev81c32082012-06-16 21:21:46 +0300203 return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
205
206 if ( ! is_array($attributes))
207 {
Andrey Andreevf0a84102012-06-16 20:52:20 +0300208 $attributes = array($attributes);
Andrey Andreev81c32082012-06-16 21:21:46 +0300209
210 // Ref: http://www.w3schools.com/jsref/met_win_open.asp
211 $window_name = '_blank';
212 }
213 elseif ( ! empty($attributes['window_name']))
214 {
215 $window_name = $attributes['window_name'];
216 unset($attributes['window_name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 }
218
Andrey Andreev81c32082012-06-16 21:21:46 +0300219 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 +0000220 {
Andrey Andreev12220872012-03-26 21:38:56 +0300221 $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 unset($attributes[$key]);
223 }
224
Eric Barnesacedd2b2012-07-29 00:15:40 -0400225 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226
Andrey Andreev81c32082012-06-16 21:21:46 +0300227 return '<a href="'.$site_url
Eric Barnesacedd2b2012-07-29 00:15:40 -0400228 .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
Andrey Andreev81c32082012-06-16 21:21:46 +0300229 .$attributes.'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231}
232
233// ------------------------------------------------------------------------
234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235if ( ! function_exists('mailto'))
236{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400237 /**
238 * Mailto Link
239 *
240 * @param string the email address
241 * @param string the link title
242 * @param mixed any attributes
243 * @return string
244 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 function mailto($email, $title = '', $attributes = '')
246 {
247 $title = (string) $title;
248
Alex Bilbie773ccc32012-06-02 11:11:08 +0100249 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 $title = $email;
252 }
253
Eric Barnesacedd2b2012-07-29 00:15:40 -0400254 return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256}
257
258// ------------------------------------------------------------------------
259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260if ( ! function_exists('safe_mailto'))
261{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400262 /**
263 * Encoded Mailto Link
264 *
265 * Create a spam-protected mailto link written in Javascript
266 *
267 * @param string the email address
268 * @param string the link title
269 * @param mixed any attributes
270 * @return string
271 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 function safe_mailto($email, $title = '', $attributes = '')
273 {
274 $title = (string) $title;
275
Alex Bilbie773ccc32012-06-02 11:11:08 +0100276 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 $title = $email;
279 }
280
Andrey Andreevdebcc362012-01-07 02:05:29 +0200281 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282
Andrey Andreevdebcc362012-01-07 02:05:29 +0200283 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200285 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287
288 $x[] = '"';
289
Alex Bilbie773ccc32012-06-02 11:11:08 +0100290 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 if (is_array($attributes))
293 {
294 foreach ($attributes as $key => $val)
295 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200296 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200297 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200299 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301 $x[] = '"';
302 }
303 }
304 else
305 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200306 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200308 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310 }
311 }
312
313 $x[] = '>';
314
315 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200316 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 $ordinal = ord($title[$i]);
319
320 if ($ordinal < 128)
321 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200322 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324 else
325 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200326 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $count = ($ordinal < 224) ? 2 : 3;
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200332 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200334 $number = ($count === 3)
335 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
336 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
337 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 $count = 1;
339 $temp = array();
340 }
341 }
342 }
343
344 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
345
346 $x = array_reverse($x);
347 ob_start();
348
349 ?><script type="text/javascript">
350 //<![CDATA[
351 var l=new Array();
352 <?php
Andrey Andreevdebcc362012-01-07 02:05:29 +0200353 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 +0000354
355 for (var i = l.length-1; i >= 0; i=i-1){
Alex Bilbie773ccc32012-06-02 11:11:08 +0100356 if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 else document.write(unescape(l[i]));}
358 //]]>
359 </script><?php
360
361 $buffer = ob_get_contents();
362 ob_end_clean();
363 return $buffer;
364 }
365}
366
367// ------------------------------------------------------------------------
368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369if ( ! function_exists('auto_link'))
370{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400371 /**
372 * Auto-linker
373 *
374 * Automatically links URL and Email addresses.
375 * Note: There's a bit of extra code here to deal with
376 * URLs or emails that end in a period. We'll strip these
377 * off and add them after the link.
378 *
379 * @param string the string
380 * @param string the type: email, url, or both
381 * @param bool whether to create pop-up links
382 * @return string
383 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 function auto_link($str, $type = 'both', $popup = FALSE)
385 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600386 // Find and replace any URLs.
387 if ($type !== 'email' && preg_match_all('#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#', $str, $matches, PREG_OFFSET_CAPTURE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600389 // Set our target HTML if using popup links.
390 $target = ($popup) ? 'target="_blank"' : '';
391
392 // We process the links in reverse order (last -> first) so that
393 // the returned string offsets from preg_match_all() are not
394 // moved as we add more HTML.
395 foreach (array_reverse($matches[0]) as $match)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200396 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600397 // $match is an array generated by the PREG_OFFSET_CAPTURE flag.
398 // $match[0] is the matched string, $match[1] is the string offset.
399
400 $anchor = anchor($match[0], '', $target);
401
402 $str = substr_replace($str, $anchor, $match[1], strlen($match[0]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404 }
Eric Roberts8093bd72013-01-17 18:12:47 -0600405
406 // Find and replace any emails.
407 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 +0000408 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600409 foreach (array_reverse($matches[0]) as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600411 if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200412 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600413 $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
Andrey Andreev929e1242012-10-19 10:09:28 +0300414 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416 }
Eric Roberts8093bd72013-01-17 18:12:47 -0600417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 return $str;
419 }
420}
421
422// ------------------------------------------------------------------------
423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424if ( ! function_exists('prep_url'))
425{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400426 /**
427 * Prep URL
428 *
429 * Simply adds the http:// part if no scheme is included
430 *
431 * @param string the URL
432 * @return string
433 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 function prep_url($str = '')
435 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100436 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 return '';
439 }
440
Robin Sowelld2167a02010-09-14 15:05:42 -0400441 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200442
Robin Sowelld2167a02010-09-14 15:05:42 -0400443 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200445 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 return $str;
449 }
450}
451
452// ------------------------------------------------------------------------
453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454if ( ! function_exists('url_title'))
455{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400456 /**
457 * Create URL Title
458 *
459 * Takes a "title" string as input and creates a
460 * human-friendly URL string with a "separator" string
461 * as the word separator.
462 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200463 * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
464 * @param string $str Input string
465 * @param string $separator Word separator
466 * (usually '-' or '_')
467 * @param bool $lowercase Wether to transform the output string to lowercase
Timothy Warrenb75faa12012-04-27 12:03:32 -0400468 * @return string
469 */
tubalmartin1a697102012-03-04 16:01:11 +0100470 function url_title($str, $separator = '-', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200472 if ($separator === 'dash')
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev12220872012-03-26 21:38:56 +0300474 $separator = '-';
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Andrey Andreev12220872012-03-26 21:38:56 +0300476 elseif ($separator === 'underscore')
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreev12220872012-03-26 21:38:56 +0300478 $separator = '_';
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
Andrey Andreev12220872012-03-26 21:38:56 +0300480
Andrey Andreeve4742582012-10-25 13:25:13 +0300481 $q_separator = preg_quote($separator, '#');
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
483 $trans = array(
Andrey Andreev12220872012-03-26 21:38:56 +0300484 '&.+?;' => '',
485 '[^a-z0-9 _-]' => '',
486 '\s+' => $separator,
487 '('.$q_separator.')+' => $separator
488 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000489
490 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 foreach ($trans as $key => $val)
492 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200493 $str = preg_replace('#'.$key.'#i', $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495
Derek Jones40a2fc82008-12-09 19:41:25 +0000496 if ($lowercase === TRUE)
497 {
498 $str = strtolower($str);
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Phil Sturgeona2bd3632012-03-04 15:32:58 +0000501 return trim(trim($str, $separator));
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
503}
504
505// ------------------------------------------------------------------------
506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507if ( ! function_exists('redirect'))
508{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400509 /**
510 * Header Redirect
511 *
512 * Header redirect in two flavors
513 * For very fine grained control over headers, you could use the Output
514 * Library's set_header() function.
515 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200516 * @param string $uri URL
517 * @param string $method Redirect method
518 * 'auto', 'location' or 'refresh'
519 * @param int $code HTTP Response status code
520 * @return void
Timothy Warrenb75faa12012-04-27 12:03:32 -0400521 */
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300522 function redirect($uri = '', $method = 'auto', $code = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Aaron Adams16800e42012-12-07 22:39:23 -0500524 if ( ! preg_match('#^(\w+:)?//#i', $uri))
Derek Jones534be032009-02-10 18:47:47 +0000525 {
526 $uri = site_url($uri);
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500529 // IIS environment likely? Use 'refresh' for better compatibility
vlakoffaab26a12012-09-11 13:10:21 +0200530 if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500531 {
532 $method = 'refresh';
533 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300534 elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
535 {
536 // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
537 $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'])
538 && $_SERVER['REQUEST_METHOD'] === 'POST'
539 && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
540 ? 303 : 302;
541 }
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500542
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300543 switch ($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200545 case 'refresh':
546 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200548 default:
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300549 header('Location: '.$uri, TRUE, $code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 break;
551 }
552 exit;
553 }
554}
555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556/* End of file url_helper.php */
Andrey Andreev12220872012-03-26 21:38:56 +0300557/* Location: ./system/helpers/url_helper.php */