blob: 2cbcd9dbfe538df28c685b09e3af4d3b7c133d79 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter URL Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/url_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
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 * @access public
49 * @param string
50 * @return string
51 */
52if ( ! function_exists('site_url'))
53{
54 function site_url($uri = '')
55 {
56 $CI =& get_instance();
57 return $CI->config->site_url($uri);
58 }
59}
60
61// ------------------------------------------------------------------------
62
63/**
64 * Base URL
Andrey Andreevdebcc362012-01-07 02:05:29 +020065 *
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080066 * 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.
Derek Allard2067d1a2008-11-13 22:59:24 +000069 *
70 * @access public
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080071 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +000072 * @return string
73 */
74if ( ! function_exists('base_url'))
75{
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080076 function base_url($uri = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000077 {
78 $CI =& get_instance();
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080079 return $CI->config->base_url($uri);
Derek Allard2067d1a2008-11-13 22:59:24 +000080 }
81}
82
83// ------------------------------------------------------------------------
84
85/**
86 * Current URL
87 *
Barry Mienydd671972010-10-04 16:33:58 +020088 * Returns the full URL (including segments) of the page where this
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * function is placed
90 *
91 * @access public
92 * @return string
93 */
94if ( ! function_exists('current_url'))
95{
96 function current_url()
97 {
98 $CI =& get_instance();
99 return $CI->config->site_url($CI->uri->uri_string());
100 }
101}
102
103// ------------------------------------------------------------------------
104/**
105 * URL String
106 *
107 * Returns the URI segments.
108 *
109 * @access public
110 * @return string
111 */
112if ( ! function_exists('uri_string'))
113{
114 function uri_string()
115 {
116 $CI =& get_instance();
117 return $CI->uri->uri_string();
118 }
119}
120
121// ------------------------------------------------------------------------
122
123/**
124 * Index page
125 *
126 * Returns the "index_page" from your config file
127 *
128 * @access public
129 * @return string
130 */
131if ( ! function_exists('index_page'))
132{
133 function index_page()
134 {
135 $CI =& get_instance();
136 return $CI->config->item('index_page');
137 }
138}
139
140// ------------------------------------------------------------------------
141
142/**
143 * Anchor Link
144 *
145 * Creates an anchor based on the local URL.
146 *
147 * @access public
148 * @param string the URL
149 * @param string the link title
150 * @param mixed any attributes
151 * @return string
152 */
153if ( ! function_exists('anchor'))
154{
155 function anchor($uri = '', $title = '', $attributes = '')
156 {
157 $title = (string) $title;
158
159 if ( ! is_array($uri))
160 {
161 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
162 }
163 else
164 {
165 $site_url = site_url($uri);
166 }
167
168 if ($title == '')
169 {
170 $title = $site_url;
171 }
172
173 if ($attributes != '')
174 {
175 $attributes = _parse_attributes($attributes);
176 }
177
178 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
179 }
180}
181
182// ------------------------------------------------------------------------
183
184/**
185 * Anchor Link - Pop-up version
186 *
187 * Creates an anchor based on the local URL. The link
188 * opens a new window based on the attributes specified.
189 *
190 * @access public
191 * @param string the URL
192 * @param string the link title
193 * @param mixed any attributes
194 * @return string
195 */
196if ( ! function_exists('anchor_popup'))
197{
198 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
199 {
200 $title = (string) $title;
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
202
203 if ($title == '')
204 {
205 $title = $site_url;
206 }
207
208 if ($attributes === FALSE)
209 {
210 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
211 }
212
213 if ( ! is_array($attributes))
214 {
215 $attributes = array();
216 }
217
218 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
219 {
220 $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
221 unset($attributes[$key]);
222 }
223
224 if ($attributes != '')
225 {
226 $attributes = _parse_attributes($attributes);
227 }
228
229 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
230 }
231}
232
233// ------------------------------------------------------------------------
234
235/**
236 * Mailto Link
237 *
238 * @access public
239 * @param string the email address
240 * @param string the link title
Barry Mienydd671972010-10-04 16:33:58 +0200241 * @param mixed any attributes
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @return string
243 */
244if ( ! function_exists('mailto'))
245{
246 function mailto($email, $title = '', $attributes = '')
247 {
248 $title = (string) $title;
249
Andrey Andreevdebcc362012-01-07 02:05:29 +0200250 if ($title == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 $title = $email;
253 }
254
Andrey Andreevdebcc362012-01-07 02:05:29 +0200255 return '<a href="mailto:'.$email.'"'._parse_attributes($attributes).'>'.$title.'</a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
257}
258
259// ------------------------------------------------------------------------
260
261/**
262 * Encoded Mailto Link
263 *
264 * Create a spam-protected mailto link written in Javascript
265 *
266 * @access public
267 * @param string the email address
268 * @param string the link title
Barry Mienydd671972010-10-04 16:33:58 +0200269 * @param mixed any attributes
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @return string
271 */
272if ( ! function_exists('safe_mailto'))
273{
274 function safe_mailto($email, $title = '', $attributes = '')
275 {
276 $title = (string) $title;
277
Andrey Andreevdebcc362012-01-07 02:05:29 +0200278 if ($title == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $title = $email;
281 }
282
Andrey Andreevdebcc362012-01-07 02:05:29 +0200283 $x = str_split('<a href="mailto:', 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Andrey Andreevdebcc362012-01-07 02:05:29 +0200285 for ($i = 0, $l = strlen($email); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200287 $x[] = '|'.ord($email[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289
290 $x[] = '"';
291
292 if ($attributes != '')
293 {
294 if (is_array($attributes))
295 {
296 foreach ($attributes as $key => $val)
297 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500298 $x[] = ' '.$key.'="';
Andrey Andreevdebcc362012-01-07 02:05:29 +0200299 for ($i = 0, $l = strlen($val); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200301 $x[] = '|'.ord($val[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303 $x[] = '"';
304 }
305 }
306 else
307 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200308 for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200310 $x[] = $attributes[$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 }
313 }
314
315 $x[] = '>';
316
317 $temp = array();
Andrey Andreevdebcc362012-01-07 02:05:29 +0200318 for ($i = 0, $l = strlen($title); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 $ordinal = ord($title[$i]);
321
322 if ($ordinal < 128)
323 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200324 $x[] = '|'.$ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326 else
327 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200328 if (count($temp) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $count = ($ordinal < 224) ? 2 : 3;
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 $temp[] = $ordinal;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200334 if (count($temp) === $count)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200336 $number = ($count === 3)
337 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
338 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
339 $x[] = '|'.$number;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 $count = 1;
341 $temp = array();
342 }
343 }
344 }
345
346 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
347
348 $x = array_reverse($x);
349 ob_start();
350
351 ?><script type="text/javascript">
352 //<![CDATA[
353 var l=new Array();
354 <?php
Andrey Andreevdebcc362012-01-07 02:05:29 +0200355 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 +0000356
357 for (var i = l.length-1; i >= 0; i=i-1){
358 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
359 else document.write(unescape(l[i]));}
360 //]]>
361 </script><?php
362
363 $buffer = ob_get_contents();
364 ob_end_clean();
365 return $buffer;
366 }
367}
368
369// ------------------------------------------------------------------------
370
371/**
372 * Auto-linker
373 *
374 * Automatically links URL and Email addresses.
375 * Note: There's a bit of extra code here to deal with
Derek Jones4b9c6292011-07-01 17:40:48 -0500376 * URLs or emails that end in a period. We'll strip these
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * off and add them after the link.
378 *
379 * @access public
380 * @param string the string
381 * @param string the type: email, url, or both
Barry Mienydd671972010-10-04 16:33:58 +0200382 * @param bool whether to create pop-up links
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @return string
384 */
385if ( ! function_exists('auto_link'))
386{
387 function auto_link($str, $type = 'both', $popup = FALSE)
388 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200389 if ($type !== 'email' && preg_match_all('#(^|\s|\(|\b)((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200391 $pop = ($popup) ? ' target="_blank" ' : '';
Barry Mienydd671972010-10-04 16:33:58 +0200392
Andrey Andreevdebcc362012-01-07 02:05:29 +0200393 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
394 {
395 if (preg_match('|\.$|', $matches[6][$i]))
396 {
397 $period = '.';
398 $matches[6][$i] = substr($matches[6][$i], 0, -1);
399 }
400 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 $period = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Andrey Andreevdebcc362012-01-07 02:05:29 +0200404
405 $str = str_replace($matches[0][$i],
406 $matches[1][$i].'<a href="http'.$matches[4][$i].'://'
407 .$matches[5][$i].$matches[6][$i].'"'.$pop.'>http'
408 .$matches[4][$i].'://'.$matches[5][$i]
409 .$matches[6][$i].'</a>'.$period,
410 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
412 }
413
Andrey Andreevdebcc362012-01-07 02:05:29 +0200414 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 +0000415 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200416 for ($i = 0, $c = count($matches); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200418 if (preg_match('|\.$|', $matches[3][$i]))
419 {
420 $period = '.';
421 $matches[3][$i] = substr($matches[3][$i], 0, -1);
422 }
423 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 $period = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Andrey Andreevdebcc362012-01-07 02:05:29 +0200427
428 $str = str_replace($matches[0][$i], safe_mailto($matches[1][$i].'@'.$matches[2][$i].'.'.$matches[3][$i]).$period, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430 }
431
432 return $str;
433 }
434}
435
436// ------------------------------------------------------------------------
437
438/**
439 * Prep URL
440 *
Derek Jonesd2658712010-03-22 11:05:01 -0500441 * Simply adds the http:// part if no scheme is included
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 *
443 * @access public
444 * @param string the URL
445 * @return string
446 */
447if ( ! function_exists('prep_url'))
448{
449 function prep_url($str = '')
450 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200451 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
469/**
470 * Create URL Title
471 *
472 * Takes a "title" string as input and creates a
473 * human-friendly URL string with either a dash
474 * or an underscore as the word separator.
475 *
476 * @access public
477 * @param string the string
478 * @param string the separator: dash, or underscore
479 * @return string
480 */
481if ( ! function_exists('url_title'))
482{
Derek Jones40a2fc82008-12-09 19:41:25 +0000483 function url_title($str, $separator = 'dash', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200485 if ($separator === 'dash')
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 $search = '_';
488 $replace = '-';
489 }
490 else
491 {
492 $search = '-';
493 $replace = '_';
494 }
495
496 $trans = array(
497 '&\#\d+?;' => '',
498 '&\S+?;' => '',
499 '\s+' => $replace,
500 '[^a-z0-9\-\._]' => '',
501 $replace.'+' => $replace,
502 $replace.'$' => $replace,
Derek Jones0b2145f2009-02-10 18:56:01 +0000503 '^'.$replace => $replace,
504 '\.+$' => ''
Barry Mienydd671972010-10-04 16:33:58 +0200505 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000506
507 $str = strip_tags($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 foreach ($trans as $key => $val)
509 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200510 $str = preg_replace('#'.$key.'#i', $val, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
Derek Jones40a2fc82008-12-09 19:41:25 +0000513 if ($lowercase === TRUE)
514 {
515 $str = strtolower($str);
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
David Behler75bc58b2011-08-21 15:03:47 +0200518 return trim(trim(stripslashes($str)), $replace);
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520}
521
522// ------------------------------------------------------------------------
523
524/**
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 *
531 * @access public
532 * @param string the URL
Eric Barnesf3189502011-08-23 21:40:59 -0400533 * @param string the method: location or refresh
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @return string
535 */
536if ( ! function_exists('redirect'))
537{
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500538 function redirect($uri = '', $method = 'auto', $http_response_code = 302)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
Derek Jones534be032009-02-10 18:47:47 +0000540 if ( ! preg_match('#^https?://#i', $uri))
541 {
542 $uri = site_url($uri);
543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500545 // IIS environment likely? Use 'refresh' for better compatibility
Andrey Andreevdebcc362012-01-07 02:05:29 +0200546 if (DIRECTORY_SEPARATOR !== '/' && $method === 'auto')
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500547 {
548 $method = 'refresh';
549 }
550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 switch($method)
552 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200553 case 'refresh':
554 header('Refresh:0;url='.$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 break;
Andrey Andreevdebcc362012-01-07 02:05:29 +0200556 default:
557 header('Location: '.$uri, TRUE, $http_response_code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 break;
559 }
560 exit;
561 }
562}
563
564// ------------------------------------------------------------------------
565
566/**
567 * Parse out the attributes
568 *
569 * Some of the functions use this
570 *
571 * @access private
572 * @param array
573 * @param bool
574 * @return string
575 */
576if ( ! function_exists('_parse_attributes'))
577{
578 function _parse_attributes($attributes, $javascript = FALSE)
579 {
580 if (is_string($attributes))
581 {
582 return ($attributes != '') ? ' '.$attributes : '';
583 }
584
585 $att = '';
586 foreach ($attributes as $key => $val)
587 {
588 if ($javascript == TRUE)
589 {
590 $att .= $key . '=' . $val . ',';
591 }
592 else
593 {
594 $att .= ' ' . $key . '="' . $val . '"';
595 }
596 }
597
598 if ($javascript == TRUE AND $att != '')
599 {
Andrey Andreevdebcc362012-01-07 02:05:29 +0200600 return substr($att, 0, -1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
602
603 return $att;
604 }
605}
606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607/* End of file url_helper.php */
Andrey Andreevdebcc362012-01-07 02:05:29 +0200608/* Location: ./system/helpers/url_helper.php */