source: sourcecode/system/helpers/url_helper.php @ 1

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