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