blob: 32d2da80e68bfd98bad2c52a9f7c40bba13cf130 [file] [log] [blame]
Derek Allard5811bf12007-02-20 01:26:08 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard5811bf12007-02-20 01:26:08 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard5811bf12007-02-20 01:26:08 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Allardd2df9bc2007-04-15 17:41:17 +000019 * CodeIgniter URL Helpers
Derek Allard5811bf12007-02-20 01:26:08 +000020 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/url_helper.html
Derek Allard5811bf12007-02-20 01:26:08 +000026 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Site URL
32 *
33 * Create a local URL based on your basepath. Segments can be passed via the
34 * first parameter either as a string or an array.
35 *
36 * @access public
37 * @param string
38 * @return string
39 */
Derek Jones269b9422008-01-28 21:00:20 +000040if (! function_exists('site_url'))
Derek Allard5811bf12007-02-20 01:26:08 +000041{
Derek Jones269b9422008-01-28 21:00:20 +000042 function site_url($uri = '')
43 {
44 $CI =& get_instance();
45 return $CI->config->site_url($uri);
46 }
Derek Allard5811bf12007-02-20 01:26:08 +000047}
48
49// ------------------------------------------------------------------------
50
51/**
52 * Base URL
53 *
54 * Returns the "base_url" item from your config file
55 *
56 * @access public
57 * @return string
58 */
Derek Jones269b9422008-01-28 21:00:20 +000059if (! function_exists('base_url'))
Derek Allard5811bf12007-02-20 01:26:08 +000060{
Derek Jones269b9422008-01-28 21:00:20 +000061 function base_url()
62 {
63 $CI =& get_instance();
64 return $CI->config->slash_item('base_url');
65 }
Derek Allard5811bf12007-02-20 01:26:08 +000066}
67
68// ------------------------------------------------------------------------
69
70/**
71 * Index page
72 *
73 * Returns the "index_page" from your config file
74 *
75 * @access public
76 * @return string
77 */
Derek Jones269b9422008-01-28 21:00:20 +000078if (! function_exists('index_page'))
Derek Allard5811bf12007-02-20 01:26:08 +000079{
Derek Jones269b9422008-01-28 21:00:20 +000080 function index_page()
81 {
82 $CI =& get_instance();
83 return $CI->config->item('index_page');
84 }
Derek Allard5811bf12007-02-20 01:26:08 +000085}
86
87// ------------------------------------------------------------------------
88
89/**
90 * Anchor Link
91 *
92 * Creates an anchor based on the local URL.
93 *
94 * @access public
95 * @param string the URL
96 * @param string the link title
97 * @param mixed any attributes
98 * @return string
99 */
Derek Jones269b9422008-01-28 21:00:20 +0000100if (! function_exists('anchor'))
Derek Allard5811bf12007-02-20 01:26:08 +0000101{
Derek Jones269b9422008-01-28 21:00:20 +0000102 function anchor($uri = '', $title = '', $attributes = '')
103 {
104 $title = (string) $title;
Derek Jones1f2fd2d2007-07-11 21:59:12 +0000105
Derek Allard73274992008-05-05 16:39:18 +0000106 if (! is_array($uri))
Derek Jones269b9422008-01-28 21:00:20 +0000107 {
Derek Allard73274992008-05-05 16:39:18 +0000108 $site_url = (! preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri;
Derek Jones269b9422008-01-28 21:00:20 +0000109 }
110 else
111 {
112 $site_url = site_url($uri);
113 }
Derek Allard5811bf12007-02-20 01:26:08 +0000114
Derek Jones269b9422008-01-28 21:00:20 +0000115 if ($title == '')
116 {
117 $title = $site_url;
118 }
Derek Allard5811bf12007-02-20 01:26:08 +0000119
Derek Jonescd6f9cd2008-05-07 17:31:03 +0000120 if ($attributes != '')
Derek Jones269b9422008-01-28 21:00:20 +0000121 {
122 $attributes = _parse_attributes($attributes);
123 }
Derek Allard5811bf12007-02-20 01:26:08 +0000124
Derek Jones269b9422008-01-28 21:00:20 +0000125 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
126 }
Derek Allard5811bf12007-02-20 01:26:08 +0000127}
128
129// ------------------------------------------------------------------------
130
131/**
132 * Anchor Link - Pop-up version
133 *
134 * Creates an anchor based on the local URL. The link
135 * opens a new window based on the attributes specified.
136 *
137 * @access public
138 * @param string the URL
139 * @param string the link title
140 * @param mixed any attributes
141 * @return string
142 */
Derek Jones269b9422008-01-28 21:00:20 +0000143if (! function_exists('anchor_popup'))
144{
145 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
146 {
147 $title = (string) $title;
Derek Jones1f2fd2d2007-07-11 21:59:12 +0000148
Derek Allard73274992008-05-05 16:39:18 +0000149 $site_url = (! preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri;
Derek Allard5811bf12007-02-20 01:26:08 +0000150
Derek Jones269b9422008-01-28 21:00:20 +0000151 if ($title == '')
152 {
153 $title = $site_url;
154 }
Derek Allard5811bf12007-02-20 01:26:08 +0000155
Derek Jones269b9422008-01-28 21:00:20 +0000156 if ($attributes === FALSE)
157 {
158 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
159 }
Derek Allard5811bf12007-02-20 01:26:08 +0000160
Derek Allard73274992008-05-05 16:39:18 +0000161 if (! is_array($attributes))
Derek Jones269b9422008-01-28 21:00:20 +0000162 {
163 $attributes = array();
164 }
Derek Allard5811bf12007-02-20 01:26:08 +0000165
Derek Jones269b9422008-01-28 21:00:20 +0000166 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
167 {
Derek Allard73274992008-05-05 16:39:18 +0000168 $atts[$key] = (! isset($attributes[$key])) ? $val : $attributes[$key];
Derek Jones269b9422008-01-28 21:00:20 +0000169 }
Derek Allard5811bf12007-02-20 01:26:08 +0000170
Derek Jones269b9422008-01-28 21:00:20 +0000171 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\">".$title."</a>";
172 }
Derek Allard5811bf12007-02-20 01:26:08 +0000173}
174
175// ------------------------------------------------------------------------
176
177/**
178 * Mailto Link
179 *
180 * @access public
181 * @param string the email address
182 * @param string the link title
183 * @param mixed any attributes
184 * @return string
185 */
Derek Jones269b9422008-01-28 21:00:20 +0000186if (! function_exists('mailto'))
Derek Allard5811bf12007-02-20 01:26:08 +0000187{
Derek Jones269b9422008-01-28 21:00:20 +0000188 function mailto($email, $title = '', $attributes = '')
Derek Allard5811bf12007-02-20 01:26:08 +0000189 {
Derek Jones269b9422008-01-28 21:00:20 +0000190 $title = (string) $title;
191
192 if ($title == "")
193 {
194 $title = $email;
195 }
196
197 $attributes = _parse_attributes($attributes);
198
199 return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
Derek Allard5811bf12007-02-20 01:26:08 +0000200 }
Derek Allard5811bf12007-02-20 01:26:08 +0000201}
202
203// ------------------------------------------------------------------------
204
205/**
206 * Encoded Mailto Link
207 *
208 * Create a spam-protected mailto link written in Javascript
209 *
210 * @access public
211 * @param string the email address
212 * @param string the link title
213 * @param mixed any attributes
214 * @return string
215 */
Derek Jones269b9422008-01-28 21:00:20 +0000216if (! function_exists('safe_mailto'))
Derek Allard5811bf12007-02-20 01:26:08 +0000217{
Derek Jones269b9422008-01-28 21:00:20 +0000218 function safe_mailto($email, $title = '', $attributes = '')
Derek Allard5811bf12007-02-20 01:26:08 +0000219 {
Derek Jones269b9422008-01-28 21:00:20 +0000220 $title = (string) $title;
221
222 if ($title == "")
223 {
224 $title = $email;
225 }
Derek Allard5811bf12007-02-20 01:26:08 +0000226
Derek Jones269b9422008-01-28 21:00:20 +0000227 for ($i = 0; $i < 16; $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000228 {
Derek Jones269b9422008-01-28 21:00:20 +0000229 $x[] = substr('<a href="mailto:', $i, 1);
230 }
231
232 for ($i = 0; $i < strlen($email); $i++)
233 {
234 $x[] = "|".ord(substr($email, $i, 1));
235 }
236
237 $x[] = '"';
238
239 if ($attributes != '')
240 {
241 if (is_array($attributes))
Derek Allard5811bf12007-02-20 01:26:08 +0000242 {
Derek Jones269b9422008-01-28 21:00:20 +0000243 foreach ($attributes as $key => $val)
Derek Allard5811bf12007-02-20 01:26:08 +0000244 {
Derek Jones269b9422008-01-28 21:00:20 +0000245 $x[] = ' '.$key.'="';
246 for ($i = 0; $i < strlen($val); $i++)
247 {
248 $x[] = "|".ord(substr($val, $i, 1));
249 }
250 $x[] = '"';
Derek Allard5811bf12007-02-20 01:26:08 +0000251 }
Derek Allard5811bf12007-02-20 01:26:08 +0000252 }
Derek Jones269b9422008-01-28 21:00:20 +0000253 else
254 {
255 for ($i = 0; $i < strlen($attributes); $i++)
256 {
257 $x[] = substr($attributes, $i, 1);
258 }
Derek Allard5811bf12007-02-20 01:26:08 +0000259 }
Derek Jones269b9422008-01-28 21:00:20 +0000260 }
Derek Allard5811bf12007-02-20 01:26:08 +0000261
Derek Jones269b9422008-01-28 21:00:20 +0000262 $x[] = '>';
Derek Allard5811bf12007-02-20 01:26:08 +0000263
Derek Jones269b9422008-01-28 21:00:20 +0000264 $temp = array();
265 for ($i = 0; $i < strlen($title); $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000266 {
Derek Jones269b9422008-01-28 21:00:20 +0000267 $ordinal = ord($title[$i]);
268
269 if ($ordinal < 128)
Derek Allard5811bf12007-02-20 01:26:08 +0000270 {
Derek Jones269b9422008-01-28 21:00:20 +0000271 $x[] = "|".$ordinal;
Derek Allard5811bf12007-02-20 01:26:08 +0000272 }
Derek Jones269b9422008-01-28 21:00:20 +0000273 else
274 {
275 if (count($temp) == 0)
276 {
277 $count = ($ordinal < 224) ? 2 : 3;
278 }
Derek Allard5811bf12007-02-20 01:26:08 +0000279
Derek Jones269b9422008-01-28 21:00:20 +0000280 $temp[] = $ordinal;
281 if (count($temp) == $count)
282 {
283 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
284 $x[] = "|".$number;
285 $count = 1;
286 $temp = array();
287 }
Derek Allard5811bf12007-02-20 01:26:08 +0000288 }
289 }
Derek Jones269b9422008-01-28 21:00:20 +0000290
291 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
292
293 $x = array_reverse($x);
294 ob_start();
295
296 ?><script type="text/javascript">
297 //<![CDATA[
298 var l=new Array();
299 <?php
300 $i = 0;
301 foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
302
303 for (var i = l.length-1; i >= 0; i=i-1){
304 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
305 else document.write(unescape(l[i]));}
306 //]]>
307 </script><?php
308
309 $buffer = ob_get_contents();
310 ob_end_clean();
311 return $buffer;
Derek Allard5811bf12007-02-20 01:26:08 +0000312 }
Derek Allard5811bf12007-02-20 01:26:08 +0000313}
314
315// ------------------------------------------------------------------------
316
317/**
318 * Auto-linker
319 *
320 * Automatically links URL and Email addresses.
321 * Note: There's a bit of extra code here to deal with
322 * URLs or emails that end in a period. We'll strip these
323 * off and add them after the link.
324 *
325 * @access public
326 * @param string the string
327 * @param string the type: email, url, or both
328 * @param bool whether to create pop-up links
329 * @return string
330 */
Derek Jones269b9422008-01-28 21:00:20 +0000331if (! function_exists('auto_link'))
Derek Allard5811bf12007-02-20 01:26:08 +0000332{
Derek Jones269b9422008-01-28 21:00:20 +0000333 function auto_link($str, $type = 'both', $popup = FALSE)
334 {
335 if ($type != 'email')
336 {
337 if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
Derek Allard5811bf12007-02-20 01:26:08 +0000338 {
Derek Jones269b9422008-01-28 21:00:20 +0000339 $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
340
341 for ($i = 0; $i < sizeof($matches['0']); $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000342 {
Derek Jones269b9422008-01-28 21:00:20 +0000343 $period = '';
344 if (preg_match("|\.$|", $matches['6'][$i]))
345 {
346 $period = '.';
347 $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
348 }
Derek Allard5811bf12007-02-20 01:26:08 +0000349
Derek Jones269b9422008-01-28 21:00:20 +0000350 $str = str_replace($matches['0'][$i],
351 $matches['1'][$i].'<a href="http'.
352 $matches['4'][$i].'://'.
353 $matches['5'][$i].
354 $matches['6'][$i].'"'.$pop.'>http'.
355 $matches['4'][$i].'://'.
356 $matches['5'][$i].
357 $matches['6'][$i].'</a>'.
358 $period, $str);
359 }
Derek Allard5811bf12007-02-20 01:26:08 +0000360 }
361 }
Derek Allard5811bf12007-02-20 01:26:08 +0000362
Derek Jones269b9422008-01-28 21:00:20 +0000363 if ($type != 'url')
364 {
365 if (preg_match_all("/([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
Derek Allard5811bf12007-02-20 01:26:08 +0000366 {
Derek Jones269b9422008-01-28 21:00:20 +0000367 for ($i = 0; $i < sizeof($matches['0']); $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000368 {
Derek Jones269b9422008-01-28 21:00:20 +0000369 $period = '';
370 if (preg_match("|\.$|", $matches['3'][$i]))
371 {
372 $period = '.';
373 $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
374 }
Derek Allard5811bf12007-02-20 01:26:08 +0000375
Derek Jones269b9422008-01-28 21:00:20 +0000376 $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
377 }
Derek Allard5811bf12007-02-20 01:26:08 +0000378
Derek Jones269b9422008-01-28 21:00:20 +0000379 }
Derek Allard5811bf12007-02-20 01:26:08 +0000380 }
Derek Jones269b9422008-01-28 21:00:20 +0000381 return $str;
Derek Allard5811bf12007-02-20 01:26:08 +0000382 }
Derek Allard5811bf12007-02-20 01:26:08 +0000383}
384
385// ------------------------------------------------------------------------
386
387/**
388 * Prep URL
389 *
390 * Simply adds the http:// part if missing
391 *
392 * @access public
393 * @param string the URL
394 * @return string
395 */
Derek Jones269b9422008-01-28 21:00:20 +0000396if (! function_exists('prep_url'))
Derek Allard5811bf12007-02-20 01:26:08 +0000397{
Derek Jones269b9422008-01-28 21:00:20 +0000398 function prep_url($str = '')
Derek Allard5811bf12007-02-20 01:26:08 +0000399 {
Derek Jones269b9422008-01-28 21:00:20 +0000400 if ($str == 'http://' OR $str == '')
401 {
402 return '';
403 }
Derek Allard5811bf12007-02-20 01:26:08 +0000404
Derek Jones269b9422008-01-28 21:00:20 +0000405 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
406 {
407 $str = 'http://'.$str;
408 }
Derek Allard5811bf12007-02-20 01:26:08 +0000409
Derek Jones269b9422008-01-28 21:00:20 +0000410 return $str;
411 }
Derek Allard5811bf12007-02-20 01:26:08 +0000412}
413
414// ------------------------------------------------------------------------
415
416/**
417 * Create URL Title
418 *
419 * Takes a "title" string as input and creates a
420 * human-friendly URL string with either a dash
421 * or an underscore as the word separator.
422 *
423 * @access public
424 * @param string the string
425 * @param string the separator: dash, or underscore
426 * @return string
427 */
Derek Jones269b9422008-01-28 21:00:20 +0000428if (! function_exists('url_title'))
Derek Allard5811bf12007-02-20 01:26:08 +0000429{
Derek Jones269b9422008-01-28 21:00:20 +0000430 function url_title($str, $separator = 'dash')
Derek Allard5811bf12007-02-20 01:26:08 +0000431 {
Derek Jones269b9422008-01-28 21:00:20 +0000432 if ($separator == 'dash')
433 {
434 $search = '_';
435 $replace = '-';
436 }
437 else
438 {
439 $search = '-';
440 $replace = '_';
441 }
Derek Allard5811bf12007-02-20 01:26:08 +0000442
Derek Jones269b9422008-01-28 21:00:20 +0000443 $trans = array(
444 $search => $replace,
445 "\s+" => $replace,
446 "[^a-z0-9".$replace."]" => '',
447 $replace."+" => $replace,
448 $replace."$" => '',
449 "^".$replace => ''
450 );
Derek Allard5811bf12007-02-20 01:26:08 +0000451
Derek Jones269b9422008-01-28 21:00:20 +0000452 $str = strip_tags(strtolower($str));
Derek Allard5811bf12007-02-20 01:26:08 +0000453
Derek Jones269b9422008-01-28 21:00:20 +0000454 foreach ($trans as $key => $val)
455 {
456 $str = preg_replace("#".$key."#", $val, $str);
457 }
458
459 return trim(stripslashes($str));
Derek Allard5811bf12007-02-20 01:26:08 +0000460 }
Derek Allard5811bf12007-02-20 01:26:08 +0000461}
462
463// ------------------------------------------------------------------------
464
465/**
466 * Header Redirect
467 *
468 * Header redirect in two flavors
Derek Allard05f830c2008-04-27 13:35:20 +0000469 * For very fine grained control over headers, you could use the Output
470 * Library's set_header() function.
Derek Allard5811bf12007-02-20 01:26:08 +0000471 *
472 * @access public
473 * @param string the URL
474 * @param string the method: location or redirect
475 * @return string
476 */
Derek Jones269b9422008-01-28 21:00:20 +0000477if (! function_exists('redirect'))
Derek Allard5811bf12007-02-20 01:26:08 +0000478{
Derek Allard05f830c2008-04-27 13:35:20 +0000479 function redirect($uri = '', $method = 'location', $http_response_code = 302)
Derek Allard5811bf12007-02-20 01:26:08 +0000480 {
Derek Jones269b9422008-01-28 21:00:20 +0000481 switch($method)
482 {
483 case 'refresh' : header("Refresh:0;url=".site_url($uri));
484 break;
Derek Allard05f830c2008-04-27 13:35:20 +0000485 default : header("Location: ".site_url($uri), TRUE, $http_response_code);
Derek Jones269b9422008-01-28 21:00:20 +0000486 break;
487 }
488 exit;
Derek Allard5811bf12007-02-20 01:26:08 +0000489 }
Derek Allard5811bf12007-02-20 01:26:08 +0000490}
491
492// ------------------------------------------------------------------------
493
494/**
495 * Parse out the attributes
496 *
497 * Some of the functions use this
498 *
499 * @access private
500 * @param array
501 * @param bool
502 * @return string
503 */
Derek Jones269b9422008-01-28 21:00:20 +0000504if (! function_exists('_parse_attributes'))
Derek Allard5811bf12007-02-20 01:26:08 +0000505{
Derek Jones269b9422008-01-28 21:00:20 +0000506 function _parse_attributes($attributes, $javascript = FALSE)
Derek Allard5811bf12007-02-20 01:26:08 +0000507 {
Derek Jones269b9422008-01-28 21:00:20 +0000508 if (is_string($attributes))
509 {
510 return ($attributes != '') ? ' '.$attributes : '';
511 }
Derek Allard5811bf12007-02-20 01:26:08 +0000512
Derek Jones269b9422008-01-28 21:00:20 +0000513 $att = '';
514 foreach ($attributes as $key => $val)
Derek Allard5811bf12007-02-20 01:26:08 +0000515 {
Derek Jones269b9422008-01-28 21:00:20 +0000516 if ($javascript == TRUE)
517 {
518 $att .= $key . '=' . $val . ',';
519 }
520 else
521 {
522 $att .= ' ' . $key . '="' . $val . '"';
523 }
Derek Allard5811bf12007-02-20 01:26:08 +0000524 }
Derek Jones269b9422008-01-28 21:00:20 +0000525
526 if ($javascript == TRUE AND $att != '')
Derek Allard5811bf12007-02-20 01:26:08 +0000527 {
Derek Jones269b9422008-01-28 21:00:20 +0000528 $att = substr($att, 0, -1);
Derek Allard5811bf12007-02-20 01:26:08 +0000529 }
Derek Allard5811bf12007-02-20 01:26:08 +0000530
Derek Jones269b9422008-01-28 21:00:20 +0000531 return $att;
Derek Allard5811bf12007-02-20 01:26:08 +0000532 }
Derek Allard5811bf12007-02-20 01:26:08 +0000533}
534
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000535
536/* End of file url_helper.php */
537/* Location: ./system/helpers/url_helper.php */