blob: 5d907d00e9c54f69b391ab60b56c65af6083aef1 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
anaxamaxan@blackdog.locald09c51a2011-02-02 23:00:16 -080065 *
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.
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;
201
202 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
203
204 if ($title == '')
205 {
206 $title = $site_url;
207 }
208
209 if ($attributes === FALSE)
210 {
211 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
212 }
213
214 if ( ! is_array($attributes))
215 {
216 $attributes = array();
217 }
218
219 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
220 {
221 $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
222 unset($attributes[$key]);
223 }
224
225 if ($attributes != '')
226 {
227 $attributes = _parse_attributes($attributes);
228 }
229
230 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
231 }
232}
233
234// ------------------------------------------------------------------------
235
236/**
237 * Mailto Link
238 *
239 * @access public
240 * @param string the email address
241 * @param string the link title
Barry Mienydd671972010-10-04 16:33:58 +0200242 * @param mixed any attributes
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * @return string
244 */
245if ( ! function_exists('mailto'))
246{
247 function mailto($email, $title = '', $attributes = '')
248 {
249 $title = (string) $title;
250
251 if ($title == "")
252 {
253 $title = $email;
254 }
255
256 $attributes = _parse_attributes($attributes);
257
258 return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
259 }
260}
261
262// ------------------------------------------------------------------------
263
264/**
265 * Encoded Mailto Link
266 *
267 * Create a spam-protected mailto link written in Javascript
268 *
269 * @access public
270 * @param string the email address
271 * @param string the link title
Barry Mienydd671972010-10-04 16:33:58 +0200272 * @param mixed any attributes
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @return string
274 */
275if ( ! function_exists('safe_mailto'))
276{
277 function safe_mailto($email, $title = '', $attributes = '')
278 {
279 $title = (string) $title;
280
281 if ($title == "")
282 {
283 $title = $email;
284 }
285
286 for ($i = 0; $i < 16; $i++)
287 {
288 $x[] = substr('<a href="mailto:', $i, 1);
289 }
290
291 for ($i = 0; $i < strlen($email); $i++)
292 {
293 $x[] = "|".ord(substr($email, $i, 1));
294 }
295
296 $x[] = '"';
297
298 if ($attributes != '')
299 {
300 if (is_array($attributes))
301 {
302 foreach ($attributes as $key => $val)
303 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500304 $x[] = ' '.$key.'="';
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 for ($i = 0; $i < strlen($val); $i++)
306 {
307 $x[] = "|".ord(substr($val, $i, 1));
308 }
309 $x[] = '"';
310 }
311 }
312 else
313 {
314 for ($i = 0; $i < strlen($attributes); $i++)
315 {
316 $x[] = substr($attributes, $i, 1);
317 }
318 }
319 }
320
321 $x[] = '>';
322
323 $temp = array();
324 for ($i = 0; $i < strlen($title); $i++)
325 {
326 $ordinal = ord($title[$i]);
327
328 if ($ordinal < 128)
329 {
330 $x[] = "|".$ordinal;
331 }
332 else
333 {
334 if (count($temp) == 0)
335 {
336 $count = ($ordinal < 224) ? 2 : 3;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 $temp[] = $ordinal;
340 if (count($temp) == $count)
341 {
342 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
343 $x[] = "|".$number;
344 $count = 1;
345 $temp = array();
346 }
347 }
348 }
349
350 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
351
352 $x = array_reverse($x);
353 ob_start();
354
355 ?><script type="text/javascript">
356 //<![CDATA[
357 var l=new Array();
358 <?php
359 $i = 0;
360 foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
361
362 for (var i = l.length-1; i >= 0; i=i-1){
363 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
364 else document.write(unescape(l[i]));}
365 //]]>
366 </script><?php
367
368 $buffer = ob_get_contents();
369 ob_end_clean();
370 return $buffer;
371 }
372}
373
374// ------------------------------------------------------------------------
375
376/**
377 * Auto-linker
378 *
379 * Automatically links URL and Email addresses.
380 * Note: There's a bit of extra code here to deal with
Derek Jones4b9c6292011-07-01 17:40:48 -0500381 * URLs or emails that end in a period. We'll strip these
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * off and add them after the link.
383 *
384 * @access public
385 * @param string the string
386 * @param string the type: email, url, or both
Barry Mienydd671972010-10-04 16:33:58 +0200387 * @param bool whether to create pop-up links
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return string
389 */
390if ( ! function_exists('auto_link'))
391{
392 function auto_link($str, $type = 'both', $popup = FALSE)
393 {
394 if ($type != 'email')
395 {
396 if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
397 {
398 $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Jones33559102009-02-02 18:50:38 +0000400 for ($i = 0; $i < count($matches['0']); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 $period = '';
403 if (preg_match("|\.$|", $matches['6'][$i]))
404 {
405 $period = '.';
406 $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 $str = str_replace($matches['0'][$i],
410 $matches['1'][$i].'<a href="http'.
411 $matches['4'][$i].'://'.
412 $matches['5'][$i].
413 $matches['6'][$i].'"'.$pop.'>http'.
414 $matches['4'][$i].'://'.
415 $matches['5'][$i].
416 $matches['6'][$i].'</a>'.
417 $period, $str);
418 }
419 }
420 }
421
422 if ($type != 'url')
423 {
Derek Jones0b2145f2009-02-10 18:56:01 +0000424 if (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 +0000425 {
Derek Jones33559102009-02-02 18:50:38 +0000426 for ($i = 0; $i < count($matches['0']); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 $period = '';
429 if (preg_match("|\.$|", $matches['3'][$i]))
430 {
431 $period = '.';
432 $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
436 }
437 }
438 }
439
440 return $str;
441 }
442}
443
444// ------------------------------------------------------------------------
445
446/**
447 * Prep URL
448 *
Derek Jonesd2658712010-03-22 11:05:01 -0500449 * Simply adds the http:// part if no scheme is included
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
451 * @access public
452 * @param string the URL
453 * @return string
454 */
455if ( ! function_exists('prep_url'))
456{
457 function prep_url($str = '')
458 {
459 if ($str == 'http://' OR $str == '')
460 {
461 return '';
462 }
463
Robin Sowelld2167a02010-09-14 15:05:42 -0400464 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200465
Robin Sowelld2167a02010-09-14 15:05:42 -0400466 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
468 $str = 'http://'.$str;
469 }
470
471 return $str;
472 }
473}
474
475// ------------------------------------------------------------------------
476
477/**
478 * Create URL Title
479 *
480 * Takes a "title" string as input and creates a
481 * human-friendly URL string with either a dash
482 * or an underscore as the word separator.
483 *
484 * @access public
485 * @param string the string
486 * @param string the separator: dash, or underscore
487 * @return string
488 */
489if ( ! function_exists('url_title'))
490{
Derek Jones40a2fc82008-12-09 19:41:25 +0000491 function url_title($str, $separator = 'dash', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 if ($separator == 'dash')
494 {
495 $search = '_';
496 $replace = '-';
497 }
498 else
499 {
500 $search = '-';
501 $replace = '_';
502 }
503
504 $trans = array(
505 '&\#\d+?;' => '',
506 '&\S+?;' => '',
507 '\s+' => $replace,
508 '[^a-z0-9\-\._]' => '',
509 $replace.'+' => $replace,
510 $replace.'$' => $replace,
Derek Jones0b2145f2009-02-10 18:56:01 +0000511 '^'.$replace => $replace,
512 '\.+$' => ''
Barry Mienydd671972010-10-04 16:33:58 +0200513 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000514
515 $str = strip_tags($str);
516
517 foreach ($trans as $key => $val)
518 {
519 $str = preg_replace("#".$key."#i", $val, $str);
520 }
521
Derek Jones40a2fc82008-12-09 19:41:25 +0000522 if ($lowercase === TRUE)
523 {
524 $str = strtolower($str);
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
David Behler75bc58b2011-08-21 15:03:47 +0200527 return trim(trim(stripslashes($str)), $replace);
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529}
530
531// ------------------------------------------------------------------------
532
533/**
534 * Header Redirect
535 *
536 * Header redirect in two flavors
537 * For very fine grained control over headers, you could use the Output
538 * Library's set_header() function.
539 *
540 * @access public
541 * @param string the URL
Eric Barnesf3189502011-08-23 21:40:59 -0400542 * @param string the method: location or refresh
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @return string
544 */
545if ( ! function_exists('redirect'))
546{
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500547 function redirect($uri = '', $method = 'auto', $http_response_code = 302)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Derek Jones534be032009-02-10 18:47:47 +0000549 if ( ! preg_match('#^https?://#i', $uri))
550 {
551 $uri = site_url($uri);
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500554 // IIS environment likely? Use 'refresh' for better compatibility
555 if (DIRECTORY_SEPARATOR != '/' && $method == 'auto')
556 {
557 $method = 'refresh';
558 }
559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 switch($method)
561 {
Derek Jones534be032009-02-10 18:47:47 +0000562 case 'refresh' : header("Refresh:0;url=".$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 break;
Derek Jones534be032009-02-10 18:47:47 +0000564 default : header("Location: ".$uri, TRUE, $http_response_code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 break;
566 }
567 exit;
568 }
569}
570
571// ------------------------------------------------------------------------
572
573/**
574 * Parse out the attributes
575 *
576 * Some of the functions use this
577 *
578 * @access private
579 * @param array
580 * @param bool
581 * @return string
582 */
583if ( ! function_exists('_parse_attributes'))
584{
585 function _parse_attributes($attributes, $javascript = FALSE)
586 {
587 if (is_string($attributes))
588 {
589 return ($attributes != '') ? ' '.$attributes : '';
590 }
591
592 $att = '';
593 foreach ($attributes as $key => $val)
594 {
595 if ($javascript == TRUE)
596 {
597 $att .= $key . '=' . $val . ',';
598 }
599 else
600 {
601 $att .= ' ' . $key . '="' . $val . '"';
602 }
603 }
604
605 if ($javascript == TRUE AND $att != '')
606 {
607 $att = substr($att, 0, -1);
608 }
609
610 return $att;
611 }
612}
613
614
615/* End of file url_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000616/* Location: ./system/helpers/url_helper.php */