url_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter URL Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/url_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Site URL
00032  *
00033  * Create a local URL based on your basepath. Segments can be passed via the
00034  * first parameter either as a string or an array.
00035  *
00036  * @access      public
00037  * @param       string
00038  * @return      string
00039  */
00040 if ( ! function_exists('site_url'))
00041 {
00042         function site_url($uri = '')
00043         {
00044                 $CI =& get_instance();
00045                 return $CI->config->site_url($uri);
00046         }
00047 }
00048 
00049 // ------------------------------------------------------------------------
00050 
00051 /**
00052  * Base URL
00053  *
00054  * Returns the "base_url" item from your config file
00055  *
00056  * @access      public
00057  * @return      string
00058  */
00059 if ( ! function_exists('base_url'))
00060 {
00061         function base_url()
00062         {
00063                 $CI =& get_instance();
00064                 return $CI->config->slash_item('base_url');
00065         }
00066 }
00067 
00068 // ------------------------------------------------------------------------
00069 
00070 /**
00071  * Current URL
00072  *
00073  * Returns the full URL (including segments) of the page where this 
00074  * function is placed
00075  *
00076  * @access      public
00077  * @return      string
00078  */
00079 if ( ! function_exists('current_url'))
00080 {
00081         function current_url()
00082         {
00083                 $CI =& get_instance();
00084                 return $CI->config->site_url($CI->uri->uri_string());
00085         }
00086 }
00087 
00088 // ------------------------------------------------------------------------
00089 /**
00090  * URL String
00091  *
00092  * Returns the URI segments.
00093  *
00094  * @access      public
00095  * @return      string
00096  */
00097 if ( ! function_exists('uri_string'))
00098 {
00099         function uri_string()
00100         {
00101                 $CI =& get_instance();
00102                 return $CI->uri->uri_string();
00103         }
00104 }
00105 
00106 // ------------------------------------------------------------------------
00107 
00108 /**
00109  * Index page
00110  *
00111  * Returns the "index_page" from your config file
00112  *
00113  * @access      public
00114  * @return      string
00115  */
00116 if ( ! function_exists('index_page'))
00117 {
00118         function index_page()
00119         {
00120                 $CI =& get_instance();
00121                 return $CI->config->item('index_page');
00122         }
00123 }
00124 
00125 // ------------------------------------------------------------------------
00126 
00127 /**
00128  * Anchor Link
00129  *
00130  * Creates an anchor based on the local URL.
00131  *
00132  * @access      public
00133  * @param       string  the URL
00134  * @param       string  the link title
00135  * @param       mixed   any attributes
00136  * @return      string
00137  */
00138 if ( ! function_exists('anchor'))
00139 {
00140         function anchor($uri = '', $title = '', $attributes = '')
00141         {
00142                 $title = (string) $title;
00143 
00144                 if ( ! is_array($uri))
00145                 {
00146                         $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
00147                 }
00148                 else
00149                 {
00150                         $site_url = site_url($uri);
00151                 }
00152 
00153                 if ($title == '')
00154                 {
00155                         $title = $site_url;
00156                 }
00157 
00158                 if ($attributes != '')
00159                 {
00160                         $attributes = _parse_attributes($attributes);
00161                 }
00162 
00163                 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
00164         }
00165 }
00166 
00167 // ------------------------------------------------------------------------
00168 
00169 /**
00170  * Anchor Link - Pop-up version
00171  *
00172  * Creates an anchor based on the local URL. The link
00173  * opens a new window based on the attributes specified.
00174  *
00175  * @access      public
00176  * @param       string  the URL
00177  * @param       string  the link title
00178  * @param       mixed   any attributes
00179  * @return      string
00180  */
00181 if ( ! function_exists('anchor_popup'))
00182 {
00183         function anchor_popup($uri = '', $title = '', $attributes = FALSE)
00184         {
00185                 $title = (string) $title;
00186 
00187                 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
00188 
00189                 if ($title == '')
00190                 {
00191                         $title = $site_url;
00192                 }
00193 
00194                 if ($attributes === FALSE)
00195                 {
00196                         return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
00197                 }
00198 
00199                 if ( ! is_array($attributes))
00200                 {
00201                         $attributes = array();
00202                 }
00203 
00204                 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
00205                 {
00206                         $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
00207                         unset($attributes[$key]);
00208                 }
00209 
00210                 if ($attributes != '')
00211                 {
00212                         $attributes = _parse_attributes($attributes);
00213                 }
00214 
00215                 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
00216         }
00217 }
00218 
00219 // ------------------------------------------------------------------------
00220 
00221 /**
00222  * Mailto Link
00223  *
00224  * @access      public
00225  * @param       string  the email address
00226  * @param       string  the link title
00227  * @param       mixed   any attributes
00228  * @return      string
00229  */
00230 if ( ! function_exists('mailto'))
00231 {
00232         function mailto($email, $title = '', $attributes = '')
00233         {
00234                 $title = (string) $title;
00235 
00236                 if ($title == "")
00237                 {
00238                         $title = $email;
00239                 }
00240 
00241                 $attributes = _parse_attributes($attributes);
00242 
00243                 return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
00244         }
00245 }
00246 
00247 // ------------------------------------------------------------------------
00248 
00249 /**
00250  * Encoded Mailto Link
00251  *
00252  * Create a spam-protected mailto link written in Javascript
00253  *
00254  * @access      public
00255  * @param       string  the email address
00256  * @param       string  the link title
00257  * @param       mixed   any attributes
00258  * @return      string
00259  */
00260 if ( ! function_exists('safe_mailto'))
00261 {
00262         function safe_mailto($email, $title = '', $attributes = '')
00263         {
00264                 $title = (string) $title;
00265 
00266                 if ($title == "")
00267                 {
00268                         $title = $email;
00269                 }
00270 
00271                 for ($i = 0; $i < 16; $i++)
00272                 {
00273                         $x[] = substr('<a href="mailto:', $i, 1);
00274                 }
00275 
00276                 for ($i = 0; $i < strlen($email); $i++)
00277                 {
00278                         $x[] = "|".ord(substr($email, $i, 1));
00279                 }
00280 
00281                 $x[] = '"';
00282 
00283                 if ($attributes != '')
00284                 {
00285                         if (is_array($attributes))
00286                         {
00287                                 foreach ($attributes as $key => $val)
00288                                 {
00289                                         $x[] =  ' '.$key.'="';
00290                                         for ($i = 0; $i < strlen($val); $i++)
00291                                         {
00292                                                 $x[] = "|".ord(substr($val, $i, 1));
00293                                         }
00294                                         $x[] = '"';
00295                                 }
00296                         }
00297                         else
00298                         {
00299                                 for ($i = 0; $i < strlen($attributes); $i++)
00300                                 {
00301                                         $x[] = substr($attributes, $i, 1);
00302                                 }
00303                         }
00304                 }
00305 
00306                 $x[] = '>';
00307 
00308                 $temp = array();
00309                 for ($i = 0; $i < strlen($title); $i++)
00310                 {
00311                         $ordinal = ord($title[$i]);
00312 
00313                         if ($ordinal < 128)
00314                         {
00315                                 $x[] = "|".$ordinal;
00316                         }
00317                         else
00318                         {
00319                                 if (count($temp) == 0)
00320                                 {
00321                                         $count = ($ordinal < 224) ? 2 : 3;
00322                                 }
00323         
00324                                 $temp[] = $ordinal;
00325                                 if (count($temp) == $count)
00326                                 {
00327                                         $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
00328                                         $x[] = "|".$number;
00329                                         $count = 1;
00330                                         $temp = array();
00331                                 }
00332                         }
00333                 }
00334 
00335                 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
00336 
00337                 $x = array_reverse($x);
00338                 ob_start();
00339 
00340         ?><script type="text/javascript">
00341         //<![CDATA[
00342         var l=new Array();
00343         <?php
00344         $i = 0;
00345         foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
00346 
00347         for (var i = l.length-1; i >= 0; i=i-1){
00348         if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
00349         else document.write(unescape(l[i]));}
00350         //]]>
00351         </script><?php
00352 
00353                 $buffer = ob_get_contents();
00354                 ob_end_clean();
00355                 return $buffer;
00356         }
00357 }
00358 
00359 // ------------------------------------------------------------------------
00360 
00361 /**
00362  * Auto-linker
00363  *
00364  * Automatically links URL and Email addresses.
00365  * Note: There's a bit of extra code here to deal with
00366  * URLs or emails that end in a period.  We'll strip these
00367  * off and add them after the link.
00368  *
00369  * @access      public
00370  * @param       string  the string
00371  * @param       string  the type: email, url, or both
00372  * @param       bool    whether to create pop-up links
00373  * @return      string
00374  */
00375 if ( ! function_exists('auto_link'))
00376 {
00377         function auto_link($str, $type = 'both', $popup = FALSE)
00378         {
00379                 if ($type != 'email')
00380                 {
00381                         if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)<]+)#i", $str, $matches))
00382                         {
00383                                 $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
00384         
00385                                 for ($i = 0; $i < sizeof($matches['0']); $i++)
00386                                 {
00387                                         $period = '';
00388                                         if (preg_match("|\.$|", $matches['6'][$i]))
00389                                         {
00390                                                 $period = '.';
00391                                                 $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
00392                                         }
00393                 
00394                                         $str = str_replace($matches['0'][$i],
00395                                                                                 $matches['1'][$i].'<a href="http'.
00396                                                                                 $matches['4'][$i].'://'.
00397                                                                                 $matches['5'][$i].
00398                                                                                 $matches['6'][$i].'"'.$pop.'>http'.
00399                                                                                 $matches['4'][$i].'://'.
00400                                                                                 $matches['5'][$i].
00401                                                                                 $matches['6'][$i].'</a>'.
00402                                                                                 $period, $str);
00403                                 }
00404                         }
00405                 }
00406 
00407                 if ($type != 'url')
00408                 {
00409                         if (preg_match_all("/([a-zA-Z0-9_\.\-\+Å]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
00410                         {
00411                                 for ($i = 0; $i < sizeof($matches['0']); $i++)
00412                                 {
00413                                         $period = '';
00414                                         if (preg_match("|\.$|", $matches['3'][$i]))
00415                                         {
00416                                                 $period = '.';
00417                                                 $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
00418                                         }
00419                 
00420                                         $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
00421                                 }
00422                         }
00423                 }
00424 
00425                 return $str;
00426         }
00427 }
00428 
00429 // ------------------------------------------------------------------------
00430 
00431 /**
00432  * Prep URL
00433  *
00434  * Simply adds the http:// part if missing
00435  *
00436  * @access      public
00437  * @param       string  the URL
00438  * @return      string
00439  */
00440 if ( ! function_exists('prep_url'))
00441 {
00442         function prep_url($str = '')
00443         {
00444                 if ($str == 'http://' OR $str == '')
00445                 {
00446                         return '';
00447                 }
00448 
00449                 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
00450                 {
00451                         $str = 'http://'.$str;
00452                 }
00453 
00454                 return $str;
00455         }
00456 }
00457 
00458 // ------------------------------------------------------------------------
00459 
00460 /**
00461  * Create URL Title
00462  *
00463  * Takes a "title" string as input and creates a
00464  * human-friendly URL string with either a dash
00465  * or an underscore as the word separator.
00466  *
00467  * @access      public
00468  * @param       string  the string
00469  * @param       string  the separator: dash, or underscore
00470  * @return      string
00471  */
00472 if ( ! function_exists('url_title'))
00473 {
00474         function url_title($str, $separator = 'dash')
00475         {
00476                 if ($separator == 'dash')
00477                 {
00478                         $search         = '_';
00479                         $replace        = '-';
00480                 }
00481                 else
00482                 {
00483                         $search         = '-';
00484                         $replace        = '_';
00485                 }
00486 
00487                 $trans = array(
00488                                                 '&\#\d+?;'                              => '',
00489                                                 '&\S+?;'                                => '',
00490                                                 '\s+'                                   => $replace,
00491                                                 '[^a-z0-9\-\._]'                => '',
00492                                                 $replace.'+'                    => $replace,
00493                                                 $replace.'$'                    => $replace,
00494                                                 '^'.$replace                    => $replace
00495                                           );
00496 
00497                 $str = strip_tags($str);
00498 
00499                 foreach ($trans as $key => $val)
00500                 {
00501                         $str = preg_replace("#".$key."#i", $val, $str);
00502                 }
00503 
00504                 return trim(stripslashes($str));
00505         }
00506 }
00507 
00508 // ------------------------------------------------------------------------
00509 
00510 /**
00511  * Header Redirect
00512  *
00513  * Header redirect in two flavors
00514  * For very fine grained control over headers, you could use the Output
00515  * Library's set_header() function.
00516  *
00517  * @access      public
00518  * @param       string  the URL
00519  * @param       string  the method: location or redirect
00520  * @return      string
00521  */
00522 if ( ! function_exists('redirect'))
00523 {
00524         function redirect($uri = '', $method = 'location', $http_response_code = 302)
00525         {
00526                 switch($method)
00527                 {
00528                         case 'refresh'  : header("Refresh:0;url=".site_url($uri));
00529                                 break;
00530                         default                 : header("Location: ".site_url($uri), TRUE, $http_response_code);
00531                                 break;
00532                 }
00533                 exit;
00534         }
00535 }
00536 
00537 // ------------------------------------------------------------------------
00538 
00539 /**
00540  * Parse out the attributes
00541  *
00542  * Some of the functions use this
00543  *
00544  * @access      private
00545  * @param       array
00546  * @param       bool
00547  * @return      string
00548  */
00549 if ( ! function_exists('_parse_attributes'))
00550 {
00551         function _parse_attributes($attributes, $javascript = FALSE)
00552         {
00553                 if (is_string($attributes))
00554                 {
00555                         return ($attributes != '') ? ' '.$attributes : '';
00556                 }
00557 
00558                 $att = '';
00559                 foreach ($attributes as $key => $val)
00560                 {
00561                         if ($javascript == TRUE)
00562                         {
00563                                 $att .= $key . '=' . $val . ',';
00564                         }
00565                         else
00566                         {
00567                                 $att .= ' ' . $key . '="' . $val . '"';
00568                         }
00569                 }
00570 
00571                 if ($javascript == TRUE AND $att != '')
00572                 {
00573                         $att = substr($att, 0, -1);
00574                 }
00575 
00576                 return $att;
00577         }
00578 }
00579 
00580 
00581 /* End of file url_helper.php */
00582 /* Location: ./system/helpers/url_helper.php */