blob: e59efffec92c6bdeb9246e9a8805aa244141afcb [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter URL Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/url_helper.html
26 */
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 */
40if ( ! function_exists('site_url'))
41{
42 function site_url($uri = '')
43 {
44 $CI =& get_instance();
45 return $CI->config->site_url($uri);
46 }
47}
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 */
59if ( ! function_exists('base_url'))
60{
61 function base_url()
62 {
63 $CI =& get_instance();
64 return $CI->config->slash_item('base_url');
65 }
66}
67
68// ------------------------------------------------------------------------
69
70/**
71 * Current URL
72 *
Barry Mienydd671972010-10-04 16:33:58 +020073 * Returns the full URL (including segments) of the page where this
Derek Allard2067d1a2008-11-13 22:59:24 +000074 * function is placed
75 *
76 * @access public
77 * @return string
78 */
79if ( ! function_exists('current_url'))
80{
81 function current_url()
82 {
83 $CI =& get_instance();
84 return $CI->config->site_url($CI->uri->uri_string());
85 }
86}
87
88// ------------------------------------------------------------------------
89/**
90 * URL String
91 *
92 * Returns the URI segments.
93 *
94 * @access public
95 * @return string
96 */
97if ( ! function_exists('uri_string'))
98{
99 function uri_string()
100 {
101 $CI =& get_instance();
102 return $CI->uri->uri_string();
103 }
104}
105
106// ------------------------------------------------------------------------
107
108/**
109 * Index page
110 *
111 * Returns the "index_page" from your config file
112 *
113 * @access public
114 * @return string
115 */
116if ( ! function_exists('index_page'))
117{
118 function index_page()
119 {
120 $CI =& get_instance();
121 return $CI->config->item('index_page');
122 }
123}
124
125// ------------------------------------------------------------------------
126
127/**
128 * Anchor Link
129 *
130 * Creates an anchor based on the local URL.
131 *
132 * @access public
133 * @param string the URL
134 * @param string the link title
135 * @param mixed any attributes
136 * @return string
137 */
138if ( ! function_exists('anchor'))
139{
140 function anchor($uri = '', $title = '', $attributes = '')
141 {
142 $title = (string) $title;
143
144 if ( ! is_array($uri))
145 {
146 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
147 }
148 else
149 {
150 $site_url = site_url($uri);
151 }
152
153 if ($title == '')
154 {
155 $title = $site_url;
156 }
157
158 if ($attributes != '')
159 {
160 $attributes = _parse_attributes($attributes);
161 }
162
163 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
164 }
165}
166
167// ------------------------------------------------------------------------
168
169/**
170 * Anchor Link - Pop-up version
171 *
172 * Creates an anchor based on the local URL. The link
173 * opens a new window based on the attributes specified.
174 *
175 * @access public
176 * @param string the URL
177 * @param string the link title
178 * @param mixed any attributes
179 * @return string
180 */
181if ( ! function_exists('anchor_popup'))
182{
183 function anchor_popup($uri = '', $title = '', $attributes = FALSE)
184 {
185 $title = (string) $title;
186
187 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
188
189 if ($title == '')
190 {
191 $title = $site_url;
192 }
193
194 if ($attributes === FALSE)
195 {
196 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
197 }
198
199 if ( ! is_array($attributes))
200 {
201 $attributes = array();
202 }
203
204 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
205 {
206 $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
207 unset($attributes[$key]);
208 }
209
210 if ($attributes != '')
211 {
212 $attributes = _parse_attributes($attributes);
213 }
214
215 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
216 }
217}
218
219// ------------------------------------------------------------------------
220
221/**
222 * Mailto Link
223 *
224 * @access public
225 * @param string the email address
226 * @param string the link title
Barry Mienydd671972010-10-04 16:33:58 +0200227 * @param mixed any attributes
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @return string
229 */
230if ( ! function_exists('mailto'))
231{
232 function mailto($email, $title = '', $attributes = '')
233 {
234 $title = (string) $title;
235
236 if ($title == "")
237 {
238 $title = $email;
239 }
240
241 $attributes = _parse_attributes($attributes);
242
243 return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
244 }
245}
246
247// ------------------------------------------------------------------------
248
249/**
250 * Encoded Mailto Link
251 *
252 * Create a spam-protected mailto link written in Javascript
253 *
254 * @access public
255 * @param string the email address
256 * @param string the link title
Barry Mienydd671972010-10-04 16:33:58 +0200257 * @param mixed any attributes
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @return string
259 */
260if ( ! function_exists('safe_mailto'))
261{
262 function safe_mailto($email, $title = '', $attributes = '')
263 {
264 $title = (string) $title;
265
266 if ($title == "")
267 {
268 $title = $email;
269 }
270
271 for ($i = 0; $i < 16; $i++)
272 {
273 $x[] = substr('<a href="mailto:', $i, 1);
274 }
275
276 for ($i = 0; $i < strlen($email); $i++)
277 {
278 $x[] = "|".ord(substr($email, $i, 1));
279 }
280
281 $x[] = '"';
282
283 if ($attributes != '')
284 {
285 if (is_array($attributes))
286 {
287 foreach ($attributes as $key => $val)
288 {
289 $x[] = ' '.$key.'="';
290 for ($i = 0; $i < strlen($val); $i++)
291 {
292 $x[] = "|".ord(substr($val, $i, 1));
293 }
294 $x[] = '"';
295 }
296 }
297 else
298 {
299 for ($i = 0; $i < strlen($attributes); $i++)
300 {
301 $x[] = substr($attributes, $i, 1);
302 }
303 }
304 }
305
306 $x[] = '>';
307
308 $temp = array();
309 for ($i = 0; $i < strlen($title); $i++)
310 {
311 $ordinal = ord($title[$i]);
312
313 if ($ordinal < 128)
314 {
315 $x[] = "|".$ordinal;
316 }
317 else
318 {
319 if (count($temp) == 0)
320 {
321 $count = ($ordinal < 224) ? 2 : 3;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 $temp[] = $ordinal;
325 if (count($temp) == $count)
326 {
327 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
328 $x[] = "|".$number;
329 $count = 1;
330 $temp = array();
331 }
332 }
333 }
334
335 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
336
337 $x = array_reverse($x);
338 ob_start();
339
340 ?><script type="text/javascript">
341 //<![CDATA[
342 var l=new Array();
343 <?php
344 $i = 0;
345 foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
346
347 for (var i = l.length-1; i >= 0; i=i-1){
348 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
349 else document.write(unescape(l[i]));}
350 //]]>
351 </script><?php
352
353 $buffer = ob_get_contents();
354 ob_end_clean();
355 return $buffer;
356 }
357}
358
359// ------------------------------------------------------------------------
360
361/**
362 * Auto-linker
363 *
364 * Automatically links URL and Email addresses.
365 * Note: There's a bit of extra code here to deal with
366 * URLs or emails that end in a period. We'll strip these
367 * off and add them after the link.
368 *
369 * @access public
370 * @param string the string
371 * @param string the type: email, url, or both
Barry Mienydd671972010-10-04 16:33:58 +0200372 * @param bool whether to create pop-up links
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @return string
374 */
375if ( ! function_exists('auto_link'))
376{
377 function auto_link($str, $type = 'both', $popup = FALSE)
378 {
379 if ($type != 'email')
380 {
381 if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
382 {
383 $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Jones33559102009-02-02 18:50:38 +0000385 for ($i = 0; $i < count($matches['0']); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
387 $period = '';
388 if (preg_match("|\.$|", $matches['6'][$i]))
389 {
390 $period = '.';
391 $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 $str = str_replace($matches['0'][$i],
395 $matches['1'][$i].'<a href="http'.
396 $matches['4'][$i].'://'.
397 $matches['5'][$i].
398 $matches['6'][$i].'"'.$pop.'>http'.
399 $matches['4'][$i].'://'.
400 $matches['5'][$i].
401 $matches['6'][$i].'</a>'.
402 $period, $str);
403 }
404 }
405 }
406
407 if ($type != 'url')
408 {
Derek Jones0b2145f2009-02-10 18:56:01 +0000409 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 +0000410 {
Derek Jones33559102009-02-02 18:50:38 +0000411 for ($i = 0; $i < count($matches['0']); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 $period = '';
414 if (preg_match("|\.$|", $matches['3'][$i]))
415 {
416 $period = '.';
417 $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
421 }
422 }
423 }
424
425 return $str;
426 }
427}
428
429// ------------------------------------------------------------------------
430
431/**
432 * Prep URL
433 *
Derek Jonesd2658712010-03-22 11:05:01 -0500434 * Simply adds the http:// part if no scheme is included
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
436 * @access public
437 * @param string the URL
438 * @return string
439 */
440if ( ! function_exists('prep_url'))
441{
442 function prep_url($str = '')
443 {
444 if ($str == 'http://' OR $str == '')
445 {
446 return '';
447 }
448
Robin Sowelld2167a02010-09-14 15:05:42 -0400449 $url = parse_url($str);
Barry Mienydd671972010-10-04 16:33:58 +0200450
Robin Sowelld2167a02010-09-14 15:05:42 -0400451 if ( ! $url OR ! isset($url['scheme']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 $str = 'http://'.$str;
454 }
455
456 return $str;
457 }
458}
459
460// ------------------------------------------------------------------------
461
462/**
463 * Create URL Title
464 *
465 * Takes a "title" string as input and creates a
466 * human-friendly URL string with either a dash
467 * or an underscore as the word separator.
468 *
469 * @access public
470 * @param string the string
471 * @param string the separator: dash, or underscore
472 * @return string
473 */
474if ( ! function_exists('url_title'))
475{
Derek Jones40a2fc82008-12-09 19:41:25 +0000476 function url_title($str, $separator = 'dash', $lowercase = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 if ($separator == 'dash')
479 {
480 $search = '_';
481 $replace = '-';
482 }
483 else
484 {
485 $search = '-';
486 $replace = '_';
487 }
488
489 $trans = array(
490 '&\#\d+?;' => '',
491 '&\S+?;' => '',
492 '\s+' => $replace,
493 '[^a-z0-9\-\._]' => '',
494 $replace.'+' => $replace,
495 $replace.'$' => $replace,
Derek Jones0b2145f2009-02-10 18:56:01 +0000496 '^'.$replace => $replace,
497 '\.+$' => ''
Barry Mienydd671972010-10-04 16:33:58 +0200498 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
500 $str = strip_tags($str);
501
502 foreach ($trans as $key => $val)
503 {
504 $str = preg_replace("#".$key."#i", $val, $str);
505 }
506
Derek Jones40a2fc82008-12-09 19:41:25 +0000507 if ($lowercase === TRUE)
508 {
509 $str = strtolower($str);
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 return trim(stripslashes($str));
513 }
514}
515
516// ------------------------------------------------------------------------
517
518/**
519 * Header Redirect
520 *
521 * Header redirect in two flavors
522 * For very fine grained control over headers, you could use the Output
523 * Library's set_header() function.
524 *
525 * @access public
526 * @param string the URL
527 * @param string the method: location or redirect
528 * @return string
529 */
530if ( ! function_exists('redirect'))
531{
532 function redirect($uri = '', $method = 'location', $http_response_code = 302)
533 {
Derek Jones534be032009-02-10 18:47:47 +0000534 if ( ! preg_match('#^https?://#i', $uri))
535 {
536 $uri = site_url($uri);
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 switch($method)
540 {
Derek Jones534be032009-02-10 18:47:47 +0000541 case 'refresh' : header("Refresh:0;url=".$uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 break;
Derek Jones534be032009-02-10 18:47:47 +0000543 default : header("Location: ".$uri, TRUE, $http_response_code);
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 break;
545 }
546 exit;
547 }
548}
549
550// ------------------------------------------------------------------------
551
552/**
553 * Parse out the attributes
554 *
555 * Some of the functions use this
556 *
557 * @access private
558 * @param array
559 * @param bool
560 * @return string
561 */
562if ( ! function_exists('_parse_attributes'))
563{
564 function _parse_attributes($attributes, $javascript = FALSE)
565 {
566 if (is_string($attributes))
567 {
568 return ($attributes != '') ? ' '.$attributes : '';
569 }
570
571 $att = '';
572 foreach ($attributes as $key => $val)
573 {
574 if ($javascript == TRUE)
575 {
576 $att .= $key . '=' . $val . ',';
577 }
578 else
579 {
580 $att .= ' ' . $key . '="' . $val . '"';
581 }
582 }
583
584 if ($javascript == TRUE AND $att != '')
585 {
586 $att = substr($att, 0, -1);
587 }
588
589 return $att;
590 }
591}
592
593
594/* End of file url_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000595/* Location: ./system/helpers/url_helper.php */