blob: ad71caa4569de5d43ea30bfb8aa163770288b6fe [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 Jones269b9422008-01-28 21:00:20 +0000106 if ( ! is_array($uri))
107 {
108 $site_url = ( ! preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri;
109 }
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 Jones269b9422008-01-28 21:00:20 +0000120 if ($attributes == '')
121 {
122 $attributes = ' title="'.$title.'"';
123 }
124 else
125 {
126 $attributes = _parse_attributes($attributes);
127 }
Derek Allard5811bf12007-02-20 01:26:08 +0000128
Derek Jones269b9422008-01-28 21:00:20 +0000129 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
130 }
Derek Allard5811bf12007-02-20 01:26:08 +0000131}
132
133// ------------------------------------------------------------------------
134
135/**
136 * Anchor Link - Pop-up version
137 *
138 * Creates an anchor based on the local URL. The link
139 * opens a new window based on the attributes specified.
140 *
141 * @access public
142 * @param string the URL
143 * @param string the link title
144 * @param mixed any attributes
145 * @return string
146 */
Derek Jones269b9422008-01-28 21:00:20 +0000147if (! function_exists('anchor_popup'))
148{
149 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
150 {
151 $title = (string) $title;
Derek Jones1f2fd2d2007-07-11 21:59:12 +0000152
Derek Jones269b9422008-01-28 21:00:20 +0000153 $site_url = ( ! preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri;
Derek Allard5811bf12007-02-20 01:26:08 +0000154
Derek Jones269b9422008-01-28 21:00:20 +0000155 if ($title == '')
156 {
157 $title = $site_url;
158 }
Derek Allard5811bf12007-02-20 01:26:08 +0000159
Derek Jones269b9422008-01-28 21:00:20 +0000160 if ($attributes === FALSE)
161 {
162 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
163 }
Derek Allard5811bf12007-02-20 01:26:08 +0000164
Derek Jones269b9422008-01-28 21:00:20 +0000165 if ( ! is_array($attributes))
166 {
167 $attributes = array();
168 }
Derek Allard5811bf12007-02-20 01:26:08 +0000169
Derek Jones269b9422008-01-28 21:00:20 +0000170 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
171 {
172 $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
173 }
Derek Allard5811bf12007-02-20 01:26:08 +0000174
Derek Jones269b9422008-01-28 21:00:20 +0000175 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\">".$title."</a>";
176 }
Derek Allard5811bf12007-02-20 01:26:08 +0000177}
178
179// ------------------------------------------------------------------------
180
181/**
182 * Mailto Link
183 *
184 * @access public
185 * @param string the email address
186 * @param string the link title
187 * @param mixed any attributes
188 * @return string
189 */
Derek Jones269b9422008-01-28 21:00:20 +0000190if (! function_exists('mailto'))
Derek Allard5811bf12007-02-20 01:26:08 +0000191{
Derek Jones269b9422008-01-28 21:00:20 +0000192 function mailto($email, $title = '', $attributes = '')
Derek Allard5811bf12007-02-20 01:26:08 +0000193 {
Derek Jones269b9422008-01-28 21:00:20 +0000194 $title = (string) $title;
195
196 if ($title == "")
197 {
198 $title = $email;
199 }
200
201 $attributes = _parse_attributes($attributes);
202
203 return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
Derek Allard5811bf12007-02-20 01:26:08 +0000204 }
Derek Allard5811bf12007-02-20 01:26:08 +0000205}
206
207// ------------------------------------------------------------------------
208
209/**
210 * Encoded Mailto Link
211 *
212 * Create a spam-protected mailto link written in Javascript
213 *
214 * @access public
215 * @param string the email address
216 * @param string the link title
217 * @param mixed any attributes
218 * @return string
219 */
Derek Jones269b9422008-01-28 21:00:20 +0000220if (! function_exists('safe_mailto'))
Derek Allard5811bf12007-02-20 01:26:08 +0000221{
Derek Jones269b9422008-01-28 21:00:20 +0000222 function safe_mailto($email, $title = '', $attributes = '')
Derek Allard5811bf12007-02-20 01:26:08 +0000223 {
Derek Jones269b9422008-01-28 21:00:20 +0000224 $title = (string) $title;
225
226 if ($title == "")
227 {
228 $title = $email;
229 }
Derek Allard5811bf12007-02-20 01:26:08 +0000230
Derek Jones269b9422008-01-28 21:00:20 +0000231 for ($i = 0; $i < 16; $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000232 {
Derek Jones269b9422008-01-28 21:00:20 +0000233 $x[] = substr('<a href="mailto:', $i, 1);
234 }
235
236 for ($i = 0; $i < strlen($email); $i++)
237 {
238 $x[] = "|".ord(substr($email, $i, 1));
239 }
240
241 $x[] = '"';
242
243 if ($attributes != '')
244 {
245 if (is_array($attributes))
Derek Allard5811bf12007-02-20 01:26:08 +0000246 {
Derek Jones269b9422008-01-28 21:00:20 +0000247 foreach ($attributes as $key => $val)
Derek Allard5811bf12007-02-20 01:26:08 +0000248 {
Derek Jones269b9422008-01-28 21:00:20 +0000249 $x[] = ' '.$key.'="';
250 for ($i = 0; $i < strlen($val); $i++)
251 {
252 $x[] = "|".ord(substr($val, $i, 1));
253 }
254 $x[] = '"';
Derek Allard5811bf12007-02-20 01:26:08 +0000255 }
Derek Allard5811bf12007-02-20 01:26:08 +0000256 }
Derek Jones269b9422008-01-28 21:00:20 +0000257 else
258 {
259 for ($i = 0; $i < strlen($attributes); $i++)
260 {
261 $x[] = substr($attributes, $i, 1);
262 }
Derek Allard5811bf12007-02-20 01:26:08 +0000263 }
Derek Jones269b9422008-01-28 21:00:20 +0000264 }
Derek Allard5811bf12007-02-20 01:26:08 +0000265
Derek Jones269b9422008-01-28 21:00:20 +0000266 $x[] = '>';
Derek Allard5811bf12007-02-20 01:26:08 +0000267
Derek Jones269b9422008-01-28 21:00:20 +0000268 $temp = array();
269 for ($i = 0; $i < strlen($title); $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000270 {
Derek Jones269b9422008-01-28 21:00:20 +0000271 $ordinal = ord($title[$i]);
272
273 if ($ordinal < 128)
Derek Allard5811bf12007-02-20 01:26:08 +0000274 {
Derek Jones269b9422008-01-28 21:00:20 +0000275 $x[] = "|".$ordinal;
Derek Allard5811bf12007-02-20 01:26:08 +0000276 }
Derek Jones269b9422008-01-28 21:00:20 +0000277 else
278 {
279 if (count($temp) == 0)
280 {
281 $count = ($ordinal < 224) ? 2 : 3;
282 }
Derek Allard5811bf12007-02-20 01:26:08 +0000283
Derek Jones269b9422008-01-28 21:00:20 +0000284 $temp[] = $ordinal;
285 if (count($temp) == $count)
286 {
287 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
288 $x[] = "|".$number;
289 $count = 1;
290 $temp = array();
291 }
Derek Allard5811bf12007-02-20 01:26:08 +0000292 }
293 }
Derek Jones269b9422008-01-28 21:00:20 +0000294
295 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
296
297 $x = array_reverse($x);
298 ob_start();
299
300 ?><script type="text/javascript">
301 //<![CDATA[
302 var l=new Array();
303 <?php
304 $i = 0;
305 foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
306
307 for (var i = l.length-1; i >= 0; i=i-1){
308 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
309 else document.write(unescape(l[i]));}
310 //]]>
311 </script><?php
312
313 $buffer = ob_get_contents();
314 ob_end_clean();
315 return $buffer;
Derek Allard5811bf12007-02-20 01:26:08 +0000316 }
Derek Allard5811bf12007-02-20 01:26:08 +0000317}
318
319// ------------------------------------------------------------------------
320
321/**
322 * Auto-linker
323 *
324 * Automatically links URL and Email addresses.
325 * Note: There's a bit of extra code here to deal with
326 * URLs or emails that end in a period. We'll strip these
327 * off and add them after the link.
328 *
329 * @access public
330 * @param string the string
331 * @param string the type: email, url, or both
332 * @param bool whether to create pop-up links
333 * @return string
334 */
Derek Jones269b9422008-01-28 21:00:20 +0000335if (! function_exists('auto_link'))
Derek Allard5811bf12007-02-20 01:26:08 +0000336{
Derek Jones269b9422008-01-28 21:00:20 +0000337 function auto_link($str, $type = 'both', $popup = FALSE)
338 {
339 if ($type != 'email')
340 {
341 if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
Derek Allard5811bf12007-02-20 01:26:08 +0000342 {
Derek Jones269b9422008-01-28 21:00:20 +0000343 $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
344
345 for ($i = 0; $i < sizeof($matches['0']); $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000346 {
Derek Jones269b9422008-01-28 21:00:20 +0000347 $period = '';
348 if (preg_match("|\.$|", $matches['6'][$i]))
349 {
350 $period = '.';
351 $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
352 }
Derek Allard5811bf12007-02-20 01:26:08 +0000353
Derek Jones269b9422008-01-28 21:00:20 +0000354 $str = str_replace($matches['0'][$i],
355 $matches['1'][$i].'<a href="http'.
356 $matches['4'][$i].'://'.
357 $matches['5'][$i].
358 $matches['6'][$i].'"'.$pop.'>http'.
359 $matches['4'][$i].'://'.
360 $matches['5'][$i].
361 $matches['6'][$i].'</a>'.
362 $period, $str);
363 }
Derek Allard5811bf12007-02-20 01:26:08 +0000364 }
365 }
Derek Allard5811bf12007-02-20 01:26:08 +0000366
Derek Jones269b9422008-01-28 21:00:20 +0000367 if ($type != 'url')
368 {
369 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 +0000370 {
Derek Jones269b9422008-01-28 21:00:20 +0000371 for ($i = 0; $i < sizeof($matches['0']); $i++)
Derek Allard5811bf12007-02-20 01:26:08 +0000372 {
Derek Jones269b9422008-01-28 21:00:20 +0000373 $period = '';
374 if (preg_match("|\.$|", $matches['3'][$i]))
375 {
376 $period = '.';
377 $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
378 }
Derek Allard5811bf12007-02-20 01:26:08 +0000379
Derek Jones269b9422008-01-28 21:00:20 +0000380 $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
381 }
Derek Allard5811bf12007-02-20 01:26:08 +0000382
Derek Jones269b9422008-01-28 21:00:20 +0000383 }
Derek Allard5811bf12007-02-20 01:26:08 +0000384 }
Derek Jones269b9422008-01-28 21:00:20 +0000385 return $str;
Derek Allard5811bf12007-02-20 01:26:08 +0000386 }
Derek Allard5811bf12007-02-20 01:26:08 +0000387}
388
389// ------------------------------------------------------------------------
390
391/**
392 * Prep URL
393 *
394 * Simply adds the http:// part if missing
395 *
396 * @access public
397 * @param string the URL
398 * @return string
399 */
Derek Jones269b9422008-01-28 21:00:20 +0000400if (! function_exists('prep_url'))
Derek Allard5811bf12007-02-20 01:26:08 +0000401{
Derek Jones269b9422008-01-28 21:00:20 +0000402 function prep_url($str = '')
Derek Allard5811bf12007-02-20 01:26:08 +0000403 {
Derek Jones269b9422008-01-28 21:00:20 +0000404 if ($str == 'http://' OR $str == '')
405 {
406 return '';
407 }
Derek Allard5811bf12007-02-20 01:26:08 +0000408
Derek Jones269b9422008-01-28 21:00:20 +0000409 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
410 {
411 $str = 'http://'.$str;
412 }
Derek Allard5811bf12007-02-20 01:26:08 +0000413
Derek Jones269b9422008-01-28 21:00:20 +0000414 return $str;
415 }
Derek Allard5811bf12007-02-20 01:26:08 +0000416}
417
418// ------------------------------------------------------------------------
419
420/**
421 * Create URL Title
422 *
423 * Takes a "title" string as input and creates a
424 * human-friendly URL string with either a dash
425 * or an underscore as the word separator.
426 *
427 * @access public
428 * @param string the string
429 * @param string the separator: dash, or underscore
430 * @return string
431 */
Derek Jones269b9422008-01-28 21:00:20 +0000432if (! function_exists('url_title'))
Derek Allard5811bf12007-02-20 01:26:08 +0000433{
Derek Jones269b9422008-01-28 21:00:20 +0000434 function url_title($str, $separator = 'dash')
Derek Allard5811bf12007-02-20 01:26:08 +0000435 {
Derek Jones269b9422008-01-28 21:00:20 +0000436 if ($separator == 'dash')
437 {
438 $search = '_';
439 $replace = '-';
440 }
441 else
442 {
443 $search = '-';
444 $replace = '_';
445 }
Derek Allard5811bf12007-02-20 01:26:08 +0000446
Derek Jones269b9422008-01-28 21:00:20 +0000447 $trans = array(
448 $search => $replace,
449 "\s+" => $replace,
450 "[^a-z0-9".$replace."]" => '',
451 $replace."+" => $replace,
452 $replace."$" => '',
453 "^".$replace => ''
454 );
Derek Allard5811bf12007-02-20 01:26:08 +0000455
Derek Jones269b9422008-01-28 21:00:20 +0000456 $str = strip_tags(strtolower($str));
Derek Allard5811bf12007-02-20 01:26:08 +0000457
Derek Jones269b9422008-01-28 21:00:20 +0000458 foreach ($trans as $key => $val)
459 {
460 $str = preg_replace("#".$key."#", $val, $str);
461 }
462
463 return trim(stripslashes($str));
Derek Allard5811bf12007-02-20 01:26:08 +0000464 }
Derek Allard5811bf12007-02-20 01:26:08 +0000465}
466
467// ------------------------------------------------------------------------
468
469/**
470 * Header Redirect
471 *
472 * Header redirect in two flavors
473 *
474 * @access public
475 * @param string the URL
476 * @param string the method: location or redirect
477 * @return string
478 */
Derek Jones269b9422008-01-28 21:00:20 +0000479if (! function_exists('redirect'))
Derek Allard5811bf12007-02-20 01:26:08 +0000480{
Derek Jones269b9422008-01-28 21:00:20 +0000481 function redirect($uri = '', $method = 'location')
Derek Allard5811bf12007-02-20 01:26:08 +0000482 {
Derek Jones269b9422008-01-28 21:00:20 +0000483 switch($method)
484 {
485 case 'refresh' : header("Refresh:0;url=".site_url($uri));
486 break;
487 default : header("Location: ".site_url($uri));
488 break;
489 }
490 exit;
Derek Allard5811bf12007-02-20 01:26:08 +0000491 }
Derek Allard5811bf12007-02-20 01:26:08 +0000492}
493
494// ------------------------------------------------------------------------
495
496/**
497 * Parse out the attributes
498 *
499 * Some of the functions use this
500 *
501 * @access private
502 * @param array
503 * @param bool
504 * @return string
505 */
Derek Jones269b9422008-01-28 21:00:20 +0000506if (! function_exists('_parse_attributes'))
Derek Allard5811bf12007-02-20 01:26:08 +0000507{
Derek Jones269b9422008-01-28 21:00:20 +0000508 function _parse_attributes($attributes, $javascript = FALSE)
Derek Allard5811bf12007-02-20 01:26:08 +0000509 {
Derek Jones269b9422008-01-28 21:00:20 +0000510 if (is_string($attributes))
511 {
512 return ($attributes != '') ? ' '.$attributes : '';
513 }
Derek Allard5811bf12007-02-20 01:26:08 +0000514
Derek Jones269b9422008-01-28 21:00:20 +0000515 $att = '';
516 foreach ($attributes as $key => $val)
Derek Allard5811bf12007-02-20 01:26:08 +0000517 {
Derek Jones269b9422008-01-28 21:00:20 +0000518 if ($javascript == TRUE)
519 {
520 $att .= $key . '=' . $val . ',';
521 }
522 else
523 {
524 $att .= ' ' . $key . '="' . $val . '"';
525 }
Derek Allard5811bf12007-02-20 01:26:08 +0000526 }
Derek Jones269b9422008-01-28 21:00:20 +0000527
528 if ($javascript == TRUE AND $att != '')
Derek Allard5811bf12007-02-20 01:26:08 +0000529 {
Derek Jones269b9422008-01-28 21:00:20 +0000530 $att = substr($att, 0, -1);
Derek Allard5811bf12007-02-20 01:26:08 +0000531 }
Derek Allard5811bf12007-02-20 01:26:08 +0000532
Derek Jones269b9422008-01-28 21:00:20 +0000533 return $att;
Derek Allard5811bf12007-02-20 01:26:08 +0000534 }
Derek Allard5811bf12007-02-20 01:26:08 +0000535}
536
adminb0dd10f2006-08-25 17:25:49 +0000537?>