blob: 2d92897914e4bee9fad227b27558135ab4824441 [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 Andreev119d8a72014-01-08 15:27:53 +020094 return get_instance()->config->site_url($CI->uri->uri_string());
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
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 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200111 return get_instance()->uri->uri_string();
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113}
114
115// ------------------------------------------------------------------------
116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117if ( ! function_exists('index_page'))
118{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400119 /**
120 * Index page
121 *
122 * Returns the "index_page" from your config file
123 *
124 * @return string
125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 function index_page()
127 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200128 return get_instance()->config->item('index_page');
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130}
131
132// ------------------------------------------------------------------------
133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134if ( ! function_exists('anchor'))
135{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400136 /**
137 * Anchor Link
138 *
139 * Creates an anchor based on the local URL.
140 *
141 * @param string the URL
142 * @param string the link title
143 * @param mixed any attributes
144 * @return string
145 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 function anchor($uri = '', $title = '', $attributes = '')
147 {
148 $title = (string) $title;
149
150 if ( ! is_array($uri))
151 {
Aaron Adams16800e42012-12-07 22:39:23 -0500152 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154 else
155 {
156 $site_url = site_url($uri);
157 }
158
Alex Bilbie773ccc32012-06-02 11:11:08 +0100159 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 $title = $site_url;
162 }
163
Alex Bilbie773ccc32012-06-02 11:11:08 +0100164 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Eric Barnesacedd2b2012-07-29 00:15:40 -0400166 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
169 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
170 }
171}
172
173// ------------------------------------------------------------------------
174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175if ( ! function_exists('anchor_popup'))
176{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400177 /**
178 * Anchor Link - Pop-up version
179 *
180 * Creates an anchor based on the local URL. The link
181 * opens a new window based on the attributes specified.
182 *
183 * @param string the URL
184 * @param string the link title
185 * @param mixed any attributes
186 * @return string
187 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
189 {
190 $title = (string) $title;
Aaron Adams16800e42012-12-07 22:39:23 -0500191 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
Alex Bilbie773ccc32012-06-02 11:11:08 +0100193 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 $title = $site_url;
196 }
197
198 if ($attributes === FALSE)
199 {
Andrey Andreev81c32082012-06-16 21:21:46 +0300200 return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203 if ( ! is_array($attributes))
204 {
Andrey Andreevf0a84102012-06-16 20:52:20 +0300205 $attributes = array($attributes);
Andrey Andreev81c32082012-06-16 21:21:46 +0300206
207 // Ref: http://www.w3schools.com/jsref/met_win_open.asp
208 $window_name = '_blank';
209 }
210 elseif ( ! empty($attributes['window_name']))
211 {
212 $window_name = $attributes['window_name'];
213 unset($attributes['window_name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215
Andrey Andreev81c32082012-06-16 21:21:46 +0300216 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 +0000217 {
Andrey Andreev12220872012-03-26 21:38:56 +0300218 $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 unset($attributes[$key]);
220 }
221
Eric Barnesacedd2b2012-07-29 00:15:40 -0400222 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000223
Andrey Andreev81c32082012-06-16 21:21:46 +0300224 return '<a href="'.$site_url
Eric Barnesacedd2b2012-07-29 00:15:40 -0400225 .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
Andrey Andreev81c32082012-06-16 21:21:46 +0300226 .$attributes.'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228}
229
230// ------------------------------------------------------------------------
231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232if ( ! function_exists('mailto'))
233{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400234 /**
235 * Mailto Link
236 *
237 * @param string the email address
238 * @param string the link title
239 * @param mixed any attributes
240 * @return string
241 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 function mailto($email, $title = '', $attributes = '')
243 {
244 $title = (string) $title;
245
Alex Bilbie773ccc32012-06-02 11:11:08 +0100246 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 $title = $email;
249 }
250
Eric Barnesacedd2b2012-07-29 00:15:40 -0400251 return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253}
254
255// ------------------------------------------------------------------------
256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257if ( ! function_exists('safe_mailto'))
258{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400259 /**
260 * Encoded Mailto Link
261 *
262 * Create a spam-protected mailto link written in Javascript
263 *
264 * @param string the email address
265 * @param string the link title
266 * @param mixed any attributes
267 * @return string
268 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 function safe_mailto($email, $title = '', $attributes = '')
270 {
271 $title = (string) $title;
272
Alex Bilbie773ccc32012-06-02 11:11:08 +0100273 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 $title = $email;
276 }
277
Andrey Andreevdebcc362012-01-07 02:05:29 +0200278 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
Andrey Andreevdebcc362012-01-07 02:05:29 +0200280 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200282 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 $x[] = '"';
286
Alex Bilbie773ccc32012-06-02 11:11:08 +0100287 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 if (is_array($attributes))
290 {
291 foreach ($attributes as $key => $val)
292 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200293 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200294 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200296 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298 $x[] = '"';
299 }
300 }
301 else
302 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200303 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200305 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
308 }
309
310 $x[] = '>';
311
312 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200313 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 $ordinal = ord($title[$i]);
316
317 if ($ordinal < 128)
318 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200319 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
321 else
322 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200323 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
325 $count = ($ordinal < 224) ? 2 : 3;
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200329 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200331 $number = ($count === 3)
332 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
333 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
334 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 $count = 1;
336 $temp = array();
337 }
338 }
339 }
340
341 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
342
343 $x = array_reverse($x);
344 ob_start();
345
346 ?><script type="text/javascript">
347 //<![CDATA[
348 var l=new Array();
349 <?php
Andrey Andreevdebcc362012-01-07 02:05:29 +0200350 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 +0000351
352 for (var i = l.length-1; i >= 0; i=i-1){
Alex Bilbie773ccc32012-06-02 11:11:08 +0100353 if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 else document.write(unescape(l[i]));}
355 //]]>
356 </script><?php
357
358 $buffer = ob_get_contents();
359 ob_end_clean();
360 return $buffer;
361 }
362}
363
364// ------------------------------------------------------------------------
365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366if ( ! function_exists('auto_link'))
367{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400368 /**
369 * Auto-linker
370 *
371 * Automatically links URL and Email addresses.
372 * Note: There's a bit of extra code here to deal with
373 * URLs or emails that end in a period. We'll strip these
374 * off and add them after the link.
375 *
376 * @param string the string
377 * @param string the type: email, url, or both
378 * @param bool whether to create pop-up links
379 * @return string
380 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 function auto_link($str, $type = 'both', $popup = FALSE)
382 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600383 // Find and replace any URLs.
Andrey Andreev606fee02013-01-28 12:57:13 +0200384 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 +0000385 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600386 // Set our target HTML if using popup links.
Andrey Andreev606fee02013-01-28 12:57:13 +0200387 $target = ($popup) ? ' target="_blank"' : '';
Eric Roberts32a28f52013-01-19 02:59:14 -0600388
Eric Roberts8093bd72013-01-17 18:12:47 -0600389 // We process the links in reverse order (last -> first) so that
390 // the returned string offsets from preg_match_all() are not
391 // moved as we add more HTML.
Andrey Andreev606fee02013-01-28 12:57:13 +0200392 foreach (array_reverse($matches) as $match)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200393 {
Andrey Andreev606fee02013-01-28 12:57:13 +0200394 // $match[0] is the matched string/link
395 // $match[1] is either a protocol prefix or 'www.'
396 //
397 // With PREG_OFFSET_CAPTURE, both of the above is an array,
398 // where the actual value is held in [0] and its offset at the [1] index.
399 $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
400 $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600403
Eric Roberts8093bd72013-01-17 18:12:47 -0600404 // Find and replace any emails.
405 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 +0000406 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600407 foreach (array_reverse($matches[0]) as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600409 if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200410 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600411 $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
Andrey Andreev929e1242012-10-19 10:09:28 +0300412 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 return $str;
417 }
418}
419
420// ------------------------------------------------------------------------
421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422if ( ! function_exists('prep_url'))
423{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400424 /**
425 * Prep URL
426 *
427 * Simply adds the http:// part if no scheme is included
428 *
429 * @param string the URL
430 * @return string
431 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 function prep_url($str = '')
433 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100434 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 return '';
437 }
438
Robin Sowelld2167a02010-09-14 15:05:42 -0400439 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200440
Robin Sowelld2167a02010-09-14 15:05:42 -0400441 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200443 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445
446 return $str;
447 }
448}
449
450// ------------------------------------------------------------------------
451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452if ( ! function_exists('url_title'))
453{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400454 /**
455 * Create URL Title
456 *
457 * Takes a "title" string as input and creates a
458 * human-friendly URL string with a "separator" string
459 * as the word separator.
460 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200461 * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
462 * @param string $str Input string
463 * @param string $separator Word separator
464 * (usually '-' or '_')
465 * @param bool $lowercase Wether to transform the output string to lowercase
Timothy Warrenb75faa12012-04-27 12:03:32 -0400466 * @return string
467 */
tubalmartin1a697102012-03-04 16:01:11 +0100468 function url_title($str, $separator = '-', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200470 if ($separator === 'dash')
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreev12220872012-03-26 21:38:56 +0300472 $separator = '-';
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Andrey Andreev12220872012-03-26 21:38:56 +0300474 elseif ($separator === 'underscore')
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreev12220872012-03-26 21:38:56 +0300476 $separator = '_';
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
Andrey Andreev12220872012-03-26 21:38:56 +0300478
Andrey Andreeve4742582012-10-25 13:25:13 +0300479 $q_separator = preg_quote($separator, '#');
Derek Allard2067d1a2008-11-13 22:59:24 +0000480
481 $trans = array(
Andrey Andreev12220872012-03-26 21:38:56 +0300482 '&.+?;' => '',
483 '[^a-z0-9 _-]' => '',
484 '\s+' => $separator,
485 '('.$q_separator.')+' => $separator
486 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
488 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 foreach ($trans as $key => $val)
490 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200491 $str = preg_replace('#'.$key.'#i', $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493
Derek Jones40a2fc82008-12-09 19:41:25 +0000494 if ($lowercase === TRUE)
495 {
496 $str = strtolower($str);
497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Phil Sturgeona2bd3632012-03-04 15:32:58 +0000499 return trim(trim($str, $separator));
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501}
502
503// ------------------------------------------------------------------------
504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505if ( ! function_exists('redirect'))
506{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400507 /**
508 * Header Redirect
509 *
510 * Header redirect in two flavors
511 * For very fine grained control over headers, you could use the Output
512 * Library's set_header() function.
513 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200514 * @param string $uri URL
515 * @param string $method Redirect method
516 * 'auto', 'location' or 'refresh'
517 * @param int $code HTTP Response status code
518 * @return void
Timothy Warrenb75faa12012-04-27 12:03:32 -0400519 */
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300520 function redirect($uri = '', $method = 'auto', $code = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Aaron Adams16800e42012-12-07 22:39:23 -0500522 if ( ! preg_match('#^(\w+:)?//#i', $uri))
Derek Jones534be032009-02-10 18:47:47 +0000523 {
524 $uri = site_url($uri);
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500527 // IIS environment likely? Use 'refresh' for better compatibility
vlakoffaab26a12012-09-11 13:10:21 +0200528 if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500529 {
530 $method = 'refresh';
531 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300532 elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
533 {
534 // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
535 $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'])
536 && $_SERVER['REQUEST_METHOD'] === 'POST'
537 && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
538 ? 303 : 302;
539 }
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500540
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300541 switch ($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200543 case 'refresh':
544 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200546 default:
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300547 header('Location: '.$uri, TRUE, $code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 break;
549 }
550 exit;
551 }
552}
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554/* End of file url_helper.php */
Andrey Andreev12220872012-03-26 21:38:56 +0300555/* Location: ./system/helpers/url_helper.php */