blob: e82a51dcc5f54d38082966b3913b809c317c0fa5 [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 {
Andrey Andreev2023c3d2013-07-18 03:19:59 +030055 $site_url = get_instance()->config->site_url($uri);
56
57 if (isset($protocol))
58 {
59 $site_url = $protocol.substr($site_url, strpos($site_url, '://'));
60 }
61
62 return $site_url;
Derek Allard2067d1a2008-11-13 22:59:24 +000063 }
64}
65
66// ------------------------------------------------------------------------
67
Derek Allard2067d1a2008-11-13 22:59:24 +000068if ( ! function_exists('base_url'))
69{
Timothy Warrenb75faa12012-04-27 12:03:32 -040070 /**
71 * Base URL
72 *
73 * Create a local URL based on your basepath.
74 * Segments can be passed in as a string or an array, same as site_url
75 * or a URL to a file can be passed in, e.g. to an image file.
76 *
Andrey Andreev2023c3d2013-07-18 03:19:59 +030077 * @param string $uri
78 * @param string $protocol
Timothy Warrenb75faa12012-04-27 12:03:32 -040079 * @return string
80 */
Andrey Andreev2023c3d2013-07-18 03:19:59 +030081 function base_url($uri = '', $protocol = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Andrey Andreev2023c3d2013-07-18 03:19:59 +030083 $base_url = get_instance()->config->base_url($uri);
84
85 if (isset($protocol))
86 {
87 $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
88 }
89
90 return $base_url;
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
92}
93
94// ------------------------------------------------------------------------
95
Derek Allard2067d1a2008-11-13 22:59:24 +000096if ( ! function_exists('current_url'))
97{
Timothy Warrenb75faa12012-04-27 12:03:32 -040098 /**
99 * Current URL
100 *
101 * Returns the full URL (including segments) of the page where this
102 * function is placed
103 *
104 * @return string
105 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 function current_url()
107 {
108 $CI =& get_instance();
109 return $CI->config->site_url($CI->uri->uri_string());
110 }
111}
112
113// ------------------------------------------------------------------------
Timothy Warrenb75faa12012-04-27 12:03:32 -0400114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115if ( ! function_exists('uri_string'))
116{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400117 /**
118 * URL String
119 *
120 * Returns the URI segments.
121 *
122 * @return string
123 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 function uri_string()
125 {
126 $CI =& get_instance();
127 return $CI->uri->uri_string();
128 }
129}
130
131// ------------------------------------------------------------------------
132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133if ( ! function_exists('index_page'))
134{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400135 /**
136 * Index page
137 *
138 * Returns the "index_page" from your config file
139 *
140 * @return string
141 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 function index_page()
143 {
144 $CI =& get_instance();
145 return $CI->config->item('index_page');
146 }
147}
148
149// ------------------------------------------------------------------------
150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151if ( ! function_exists('anchor'))
152{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400153 /**
154 * Anchor Link
155 *
156 * Creates an anchor based on the local URL.
157 *
158 * @param string the URL
159 * @param string the link title
160 * @param mixed any attributes
161 * @return string
162 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 function anchor($uri = '', $title = '', $attributes = '')
164 {
165 $title = (string) $title;
166
167 if ( ! is_array($uri))
168 {
Aaron Adams16800e42012-12-07 22:39:23 -0500169 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171 else
172 {
173 $site_url = site_url($uri);
174 }
175
Alex Bilbie773ccc32012-06-02 11:11:08 +0100176 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 $title = $site_url;
179 }
180
Alex Bilbie773ccc32012-06-02 11:11:08 +0100181 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Eric Barnesacedd2b2012-07-29 00:15:40 -0400183 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185
186 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
187 }
188}
189
190// ------------------------------------------------------------------------
191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192if ( ! function_exists('anchor_popup'))
193{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400194 /**
195 * Anchor Link - Pop-up version
196 *
197 * Creates an anchor based on the local URL. The link
198 * opens a new window based on the attributes specified.
199 *
200 * @param string the URL
201 * @param string the link title
202 * @param mixed any attributes
203 * @return string
204 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
206 {
207 $title = (string) $title;
Aaron Adams16800e42012-12-07 22:39:23 -0500208 $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
Alex Bilbie773ccc32012-06-02 11:11:08 +0100210 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 $title = $site_url;
213 }
214
215 if ($attributes === FALSE)
216 {
Andrey Andreev81c32082012-06-16 21:21:46 +0300217 return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 if ( ! is_array($attributes))
221 {
Andrey Andreevf0a84102012-06-16 20:52:20 +0300222 $attributes = array($attributes);
Andrey Andreev81c32082012-06-16 21:21:46 +0300223
224 // Ref: http://www.w3schools.com/jsref/met_win_open.asp
225 $window_name = '_blank';
226 }
227 elseif ( ! empty($attributes['window_name']))
228 {
229 $window_name = $attributes['window_name'];
230 unset($attributes['window_name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232
Andrey Andreev81c32082012-06-16 21:21:46 +0300233 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 +0000234 {
Andrey Andreev12220872012-03-26 21:38:56 +0300235 $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 unset($attributes[$key]);
237 }
238
Eric Barnesacedd2b2012-07-29 00:15:40 -0400239 $attributes = _stringify_attributes($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240
Andrey Andreev81c32082012-06-16 21:21:46 +0300241 return '<a href="'.$site_url
Eric Barnesacedd2b2012-07-29 00:15:40 -0400242 .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
Andrey Andreev81c32082012-06-16 21:21:46 +0300243 .$attributes.'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
245}
246
247// ------------------------------------------------------------------------
248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249if ( ! function_exists('mailto'))
250{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400251 /**
252 * Mailto Link
253 *
254 * @param string the email address
255 * @param string the link title
256 * @param mixed any attributes
257 * @return string
258 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 function mailto($email, $title = '', $attributes = '')
260 {
261 $title = (string) $title;
262
Alex Bilbie773ccc32012-06-02 11:11:08 +0100263 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 $title = $email;
266 }
267
Eric Barnesacedd2b2012-07-29 00:15:40 -0400268 return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270}
271
272// ------------------------------------------------------------------------
273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274if ( ! function_exists('safe_mailto'))
275{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400276 /**
277 * Encoded Mailto Link
278 *
279 * Create a spam-protected mailto link written in Javascript
280 *
281 * @param string the email address
282 * @param string the link title
283 * @param mixed any attributes
284 * @return string
285 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 function safe_mailto($email, $title = '', $attributes = '')
287 {
288 $title = (string) $title;
289
Alex Bilbie773ccc32012-06-02 11:11:08 +0100290 if ($title === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 $title = $email;
293 }
294
Andrey Andreevdebcc362012-01-07 02:05:29 +0200295 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000296
Andrey Andreevdebcc362012-01-07 02:05:29 +0200297 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200299 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301
302 $x[] = '"';
303
Alex Bilbie773ccc32012-06-02 11:11:08 +0100304 if ($attributes !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
306 if (is_array($attributes))
307 {
308 foreach ($attributes as $key => $val)
309 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200310 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200311 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200313 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
315 $x[] = '"';
316 }
317 }
318 else
319 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200320 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200322 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324 }
325 }
326
327 $x[] = '>';
328
329 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200330 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $ordinal = ord($title[$i]);
333
334 if ($ordinal < 128)
335 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200336 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
338 else
339 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200340 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $count = ($ordinal < 224) ? 2 : 3;
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200346 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200348 $number = ($count === 3)
349 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
350 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
351 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 $count = 1;
353 $temp = array();
354 }
355 }
356 }
357
358 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
359
360 $x = array_reverse($x);
361 ob_start();
362
363 ?><script type="text/javascript">
364 //<![CDATA[
365 var l=new Array();
366 <?php
Andrey Andreevdebcc362012-01-07 02:05:29 +0200367 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 +0000368
369 for (var i = l.length-1; i >= 0; i=i-1){
Alex Bilbie773ccc32012-06-02 11:11:08 +0100370 if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 else document.write(unescape(l[i]));}
372 //]]>
373 </script><?php
374
375 $buffer = ob_get_contents();
376 ob_end_clean();
377 return $buffer;
378 }
379}
380
381// ------------------------------------------------------------------------
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383if ( ! function_exists('auto_link'))
384{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400385 /**
386 * Auto-linker
387 *
388 * Automatically links URL and Email addresses.
389 * Note: There's a bit of extra code here to deal with
390 * URLs or emails that end in a period. We'll strip these
391 * off and add them after the link.
392 *
393 * @param string the string
394 * @param string the type: email, url, or both
395 * @param bool whether to create pop-up links
396 * @return string
397 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 function auto_link($str, $type = 'both', $popup = FALSE)
399 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600400 // Find and replace any URLs.
Andrey Andreev606fee02013-01-28 12:57:13 +0200401 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 +0000402 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600403 // Set our target HTML if using popup links.
Andrey Andreev606fee02013-01-28 12:57:13 +0200404 $target = ($popup) ? ' target="_blank"' : '';
Eric Roberts32a28f52013-01-19 02:59:14 -0600405
Eric Roberts8093bd72013-01-17 18:12:47 -0600406 // We process the links in reverse order (last -> first) so that
407 // the returned string offsets from preg_match_all() are not
408 // moved as we add more HTML.
Andrey Andreev606fee02013-01-28 12:57:13 +0200409 foreach (array_reverse($matches) as $match)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200410 {
Andrey Andreev606fee02013-01-28 12:57:13 +0200411 // $match[0] is the matched string/link
412 // $match[1] is either a protocol prefix or 'www.'
413 //
414 // With PREG_OFFSET_CAPTURE, both of the above is an array,
415 // where the actual value is held in [0] and its offset at the [1] index.
416 $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
417 $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600420
Eric Roberts8093bd72013-01-17 18:12:47 -0600421 // Find and replace any emails.
422 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 +0000423 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600424 foreach (array_reverse($matches[0]) as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600426 if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
Andrey Andreevdebcc362012-01-07 02:05:29 +0200427 {
Eric Roberts8093bd72013-01-17 18:12:47 -0600428 $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
Andrey Andreev929e1242012-10-19 10:09:28 +0300429 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431 }
Eric Roberts32a28f52013-01-19 02:59:14 -0600432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 return $str;
434 }
435}
436
437// ------------------------------------------------------------------------
438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439if ( ! function_exists('prep_url'))
440{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400441 /**
442 * Prep URL
443 *
444 * Simply adds the http:// part if no scheme is included
445 *
446 * @param string the URL
447 * @return string
448 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 function prep_url($str = '')
450 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100451 if ($str === 'http://' OR $str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 return '';
454 }
455
Robin Sowelld2167a02010-09-14 15:05:42 -0400456 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200457
Robin Sowelld2167a02010-09-14 15:05:42 -0400458 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200460 return 'http://'.$str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 return $str;
464 }
465}
466
467// ------------------------------------------------------------------------
468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469if ( ! function_exists('url_title'))
470{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400471 /**
472 * Create URL Title
473 *
474 * Takes a "title" string as input and creates a
475 * human-friendly URL string with a "separator" string
476 * as the word separator.
477 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200478 * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
479 * @param string $str Input string
480 * @param string $separator Word separator
481 * (usually '-' or '_')
482 * @param bool $lowercase Wether to transform the output string to lowercase
Timothy Warrenb75faa12012-04-27 12:03:32 -0400483 * @return string
484 */
tubalmartin1a697102012-03-04 16:01:11 +0100485 function url_title($str, $separator = '-', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200487 if ($separator === 'dash')
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 elseif ($separator === 'underscore')
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreev12220872012-03-26 21:38:56 +0300493 $separator = '_';
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Andrey Andreev12220872012-03-26 21:38:56 +0300495
Andrey Andreeve4742582012-10-25 13:25:13 +0300496 $q_separator = preg_quote($separator, '#');
Derek Allard2067d1a2008-11-13 22:59:24 +0000497
498 $trans = array(
Andrey Andreev12220872012-03-26 21:38:56 +0300499 '&.+?;' => '',
500 '[^a-z0-9 _-]' => '',
501 '\s+' => $separator,
502 '('.$q_separator.')+' => $separator
503 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000504
505 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 foreach ($trans as $key => $val)
507 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200508 $str = preg_replace('#'.$key.'#i', $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
510
Derek Jones40a2fc82008-12-09 19:41:25 +0000511 if ($lowercase === TRUE)
512 {
513 $str = strtolower($str);
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Phil Sturgeona2bd3632012-03-04 15:32:58 +0000516 return trim(trim($str, $separator));
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518}
519
520// ------------------------------------------------------------------------
521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522if ( ! function_exists('redirect'))
523{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400524 /**
525 * Header Redirect
526 *
527 * Header redirect in two flavors
528 * For very fine grained control over headers, you could use the Output
529 * Library's set_header() function.
530 *
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200531 * @param string $uri URL
532 * @param string $method Redirect method
533 * 'auto', 'location' or 'refresh'
534 * @param int $code HTTP Response status code
535 * @return void
Timothy Warrenb75faa12012-04-27 12:03:32 -0400536 */
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300537 function redirect($uri = '', $method = 'auto', $code = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Aaron Adams16800e42012-12-07 22:39:23 -0500539 if ( ! preg_match('#^(\w+:)?//#i', $uri))
Derek Jones534be032009-02-10 18:47:47 +0000540 {
541 $uri = site_url($uri);
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500544 // IIS environment likely? Use 'refresh' for better compatibility
vlakoffaab26a12012-09-11 13:10:21 +0200545 if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500546 {
547 $method = 'refresh';
548 }
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300549 elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
550 {
551 // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
552 $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'])
553 && $_SERVER['REQUEST_METHOD'] === 'POST'
554 && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
555 ? 303 : 302;
556 }
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500557
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300558 switch ($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200560 case 'refresh':
561 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200563 default:
Andrey Andreev2fce2a92012-06-27 01:07:56 +0300564 header('Location: '.$uri, TRUE, $code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 break;
566 }
567 exit;
568 }
569}
570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571/* End of file url_helper.php */
Andrey Andreev12220872012-03-26 21:38:56 +0300572/* Location: ./system/helpers/url_helper.php */