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