Source for file url_functions.php

Documentation is available at url_functions.php

  1. <?php
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package        CodeIgniter
  8.  * @subpackage     Helpers
  9.  * @author        ExpressionEngine Dev Team
  10.  * @copyright    Copyright (c) 2006, EllisLab, Inc.
  11.  * @license        http://codeigniter.com/user_guide/license.html
  12.  * @link        http://codeigniter.com
  13.  * @since        Version 1.0
  14.  * @filesource
  15.  */
  16.  
  17. // ------------------------------------------------------------------------
  18.  
  19. /**
  20.  * CodeIgniter URL Helpers
  21.  *
  22.  * @author        ExpressionEngine Dev Team
  23.  * @link        http://codeigniter.com/user_guide/helpers/url_helper.html
  24.  */
  25.  
  26. // ------------------------------------------------------------------------
  27.  
  28. if (function_exists('site_url')) {
  29.     /**
  30.      * Site URL
  31.      *
  32.      * Create a local URL based on your basepath. Segments can be passed via the
  33.      * first parameter either as a string or an array.
  34.      *
  35.      * @access    public
  36.      * @param    string 
  37.      * @return    string 
  38.      */    
  39.     function site_url($uri '')
  40.     {
  41.         trigger_error('Función site_url() no implementada.'E_USER_ERROR);
  42.         $CI =get_instance();
  43.         return $CI->config->site_url($uri);
  44.     }
  45. }
  46.     
  47. // ------------------------------------------------------------------------
  48.  
  49. if (function_exists('base_url')) {
  50.     /**
  51.      * Base URL
  52.      *
  53.      * Returns the "base_url" item from your config file
  54.      *
  55.      * @access    public
  56.      * @return    string 
  57.      */    
  58.     function base_url()
  59.     {
  60.         trigger_error('Función base_url() no implementada.'E_USER_ERROR);
  61.         $CI =get_instance();
  62.         return $CI->config->slash_item('base_url');
  63.     }
  64. }
  65.     
  66. // ------------------------------------------------------------------------
  67.  
  68. if (function_exists('index_page')) {
  69.     /**
  70.      * Index page
  71.      *
  72.      * Returns the "index_page" from your config file
  73.      *
  74.      * @access    public
  75.      * @return    string 
  76.      */    
  77.     function index_page()
  78.     {
  79.         trigger_error('Función index_page() no implementada.'E_USER_ERROR);
  80.         $CI =get_instance();
  81.         return $CI->config->item('index_page');
  82.     }
  83. }
  84.     
  85. // ------------------------------------------------------------------------
  86.  
  87. if (function_exists('anchor')) {
  88.     /**
  89.      * Anchor Link
  90.      *
  91.      * Creates an anchor based on the local URL.
  92.      *
  93.      * @access    public
  94.      * @param    string    the URL
  95.      * @param    string    the link title
  96.      * @param    mixed    any attributes
  97.      * @return    string 
  98.      */    
  99.     function anchor($uri ''$title ''$attributes '')
  100.     {
  101.         $title = (string) $title;
  102.     
  103.         if is_array($uri))
  104.         {
  105.             $site_url preg_match('!^\w+://!i'$uri)) site_url($uri$uri;
  106.         }
  107.         else
  108.         {
  109.             $site_url site_url($uri);
  110.         }
  111.     
  112.         if ($title == '')
  113.         {
  114.             $title $site_url;
  115.         }
  116.  
  117.         if ($attributes == '')
  118.         {
  119.             $attributes ' title="'.$title.'"';
  120.         }
  121.         else
  122.         {
  123.             $attributes _parse_attributes($attributes);
  124.         }
  125.  
  126.         return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
  127.     }
  128. }
  129.     
  130. // ------------------------------------------------------------------------
  131.  
  132. if (function_exists('anchor_popup')) {
  133.     /**
  134.      * Anchor Link - Pop-up version
  135.      *
  136.      * Creates an anchor based on the local URL. The link
  137.      * opens a new window based on the attributes specified.
  138.      *
  139.      * @access    public
  140.      * @param    string    the URL
  141.      * @param    string    the link title
  142.      * @param    mixed    any attributes
  143.      * @return    string 
  144.      */
  145.     function anchor_popup($uri ''$title ''$attributes FALSE)
  146.     {    
  147.         $title = (string) $title;
  148.     
  149.         $site_url preg_match('!^\w+://!i'$uri)) site_url($uri$uri;
  150.     
  151.         if ($title == '')
  152.         {
  153.             $title $site_url;
  154.         }
  155.     
  156.         if ($attributes === FALSE)
  157.         {
  158.             return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
  159.         }
  160.     
  161.         if is_array($attributes))
  162.         {
  163.             $attributes array();
  164.         }
  165.         
  166.         foreach (array('width' => '800''height' => '600''scrollbars' => 'yes''status' => 'yes''resizable' => 'yes''screenx' => '0''screeny' => '0'as $key => $val)
  167.         {
  168.             $atts[$keyisset($attributes[$key])) $val $attributes[$key];
  169.         }
  170.  
  171.         return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($attsTRUE)."');\">".$title."</a>";
  172.     }
  173. }
  174.     
  175. // ------------------------------------------------------------------------
  176.  
  177. if (function_exists('mailto')) {
  178.     /**
  179.      * Mailto Link
  180.      *
  181.      * @access    public
  182.      * @param    string    the email address
  183.      * @param    string    the link title
  184.      * @param    mixed     any attributes
  185.      * @return    string 
  186.      */
  187.     function mailto($email$title ''$attributes '')
  188.     {
  189.         $title = (string) $title;
  190.     
  191.         if ($title == "")
  192.         {
  193.             $title $email;
  194.         }
  195.     
  196.         $attributes _parse_attributes($attributes);
  197.     
  198.         return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
  199.     }
  200. }
  201.     
  202. // ------------------------------------------------------------------------
  203.  
  204. if (function_exists('safe_mailto')) {
  205.     /**
  206.      * Encoded Mailto Link
  207.      *
  208.      * Create a spam-protected mailto link written in Javascript
  209.      *
  210.      * @access    public
  211.      * @param    string    the email address
  212.      * @param    string    the link title
  213.      * @param    mixed     any attributes
  214.      * @return    string 
  215.      */
  216.     function safe_mailto($email$title ''$attributes '')
  217.     {
  218.         $title = (string) $title;
  219.     
  220.         if ($title == "")
  221.         {
  222.             $title $email;
  223.         }
  224.                     
  225.         for ($i 0$i 16$i++)
  226.         {
  227.             $x[substr('<a href="mailto:'$i1);
  228.         }
  229.     
  230.         for ($i 0$i strlen($email)$i++)
  231.         {
  232.             $x["|".ord(substr($email$i1));
  233.         }
  234.  
  235.         $x['"';
  236.  
  237.         if ($attributes != '')
  238.         {
  239.             if (is_array($attributes))
  240.             {
  241.                 foreach ($attributes as $key => $val)
  242.                 {
  243.                     $x[=  ' '.$key.'="';
  244.                     for ($i 0$i strlen($val)$i++)
  245.                     {
  246.                         $x["|".ord(substr($val$i1));
  247.                     }
  248.                     $x['"';
  249.                 }
  250.             }
  251.             else
  252.             {    
  253.                 for ($i 0$i strlen($attributes)$i++)
  254.                 {
  255.                     $x[substr($attributes$i1);
  256.                 }
  257.             }
  258.         }    
  259.     
  260.         $x['>';
  261.     
  262.         $temp array();
  263.         for ($i 0$i strlen($title)$i++)
  264.         {
  265.             $ordinal ord($title[$i]);
  266.     
  267.             if ($ordinal 128)
  268.             {
  269.                 $x["|".$ordinal;
  270.             }
  271.             else
  272.             {
  273.                 if (count($temp== 0)
  274.                 {
  275.                     $count ($ordinal 2243;
  276.                 }
  277.         
  278.                 $temp[$ordinal;
  279.                 if (count($temp== $count)
  280.                 {
  281.                     $number ($count == 3(($temp['0'164096(($temp['1'6464($temp['2'64(($temp['0'3264($temp['1'64);
  282.                     $x["|".$number;
  283.                     $count 1;
  284.                     $temp array();
  285.                 }
  286.             }
  287.         }
  288.     
  289.         $x['<'$x['/'$x['a'$x['>';
  290.     
  291.         $x array_reverse($x);
  292.         ob_start();
  293.     
  294.     ?><script type="text/javascript">
  295.     //<![CDATA[
  296.     var l=new Array();
  297.     <?php
  298.     $i 0;
  299.     foreach ($x as $val)?>l[<?php echo $i++?>]='<?php echo $val?>';<?php ?>
  300.  
  301.     for (var i = l.length-1; i >= 0; i=i-1){
  302.     if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
  303.     else document.write(unescape(l[i]));}
  304.     //]]>
  305.     </script><?php
  306.  
  307.         $buffer ob_get_contents();
  308.         ob_end_clean();
  309.         return $buffer;
  310.     }
  311. }
  312.     
  313. // ------------------------------------------------------------------------
  314.  
  315. if (function_exists('auto_link')) {
  316.     /**
  317.      * Auto-linker
  318.      *
  319.      * Automatically links URL and Email addresses.
  320.      * Note: There's a bit of extra code here to deal with
  321.      * URLs or emails that end in a period.  We'll strip these
  322.      * off and add them after the link.
  323.      *
  324.      * @access    public
  325.      * @param    string    the string
  326.      * @param    string    the type: email, url, or both
  327.      * @param    bool     whether to create pop-up links
  328.      * @return    string 
  329.      */
  330.     function auto_link($str$type 'both'$popup FALSE)
  331.     {
  332.         if ($type != 'email')
  333.         {        
  334.             if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i"$str$matches))
  335.             {
  336.                 $pop ($popup == TRUE" target=\"_blank\" " "";
  337.         
  338.                 for ($i 0$i sizeof($matches['0'])$i++)
  339.                 {
  340.                     $period '';
  341.                     if (preg_match("|\.$|"$matches['6'][$i]))
  342.                     {
  343.                         $period '.';
  344.                         $matches['6'][$isubstr($matches['6'][$i]0-1);
  345.                     }
  346.             
  347.                     $str str_replace($matches['0'][$i],
  348.                                         $matches['1'][$i].'<a href="http'.
  349.                                         $matches['4'][$i].'://'.
  350.                                         $matches['5'][$i].
  351.                                         $matches['6'][$i].'"'.$pop.'>http'.
  352.                                         $matches['4'][$i].'://'.
  353.                                         $matches['5'][$i].
  354.                                         $matches['6'][$i].'</a>'.
  355.                                         $period$str);
  356.                 }
  357.             }
  358.         }
  359.  
  360.         if ($type != 'url')
  361.         {    
  362.             if (preg_match_all("/([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i"$str$matches))
  363.             {
  364.                 for ($i 0$i sizeof($matches['0'])$i++)
  365.                 {
  366.                     $period '';
  367.                     if (preg_match("|\.$|"$matches['3'][$i]))
  368.                     {
  369.                         $period '.';
  370.                         $matches['3'][$isubstr($matches['3'][$i]0-1);
  371.                     }
  372.             
  373.                     $str str_replace($matches['0'][$i]safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period$str);
  374.                 }
  375.         
  376.             }
  377.         }
  378.         return $str;
  379.     }
  380. }
  381.     
  382. // ------------------------------------------------------------------------
  383.  
  384. if (function_exists('prep_url')) {
  385.     /**
  386.      * Prep URL
  387.      *
  388.      * Simply adds the http:// part if missing
  389.      *
  390.      * @access    public
  391.      * @param    string    the URL
  392.      * @return    string 
  393.      */
  394.     function prep_url($str '')
  395.     {
  396.         if ($str == 'http://' OR $str == '')
  397.         {
  398.             return '';
  399.         }
  400.     
  401.         if (substr($str07!= 'http://' && substr($str08!= 'https://')
  402.         {
  403.             $str 'http://'.$str;
  404.         }
  405.     
  406.         return $str;
  407.     }
  408. }
  409.     
  410. // ------------------------------------------------------------------------
  411.  
  412. if (function_exists('url_title')) {
  413.     /**
  414.      * Create URL Title
  415.      *
  416.      * Takes a "title" string as input and creates a
  417.      * human-friendly URL string with either a dash
  418.      * or an underscore as the word separator.
  419.      *
  420.      * @access    public
  421.      * @param    string    the string
  422.      * @param    string    the separator: dash, or underscore
  423.      * @return    string 
  424.      */
  425.     function url_title($str$separator 'dash'{
  426.         $utf is_utf($str);
  427.         
  428.         if ($separator == 'dash'{
  429.             $search        '_';
  430.             $replace    '-';
  431.         }
  432.         else {
  433.             $search        '-';
  434.             $replace    '_';
  435.         }
  436.         
  437.         if ($utf{
  438.             $str utf8_decode($str);
  439.         }
  440.         
  441.         // Los caracteres a transformar están en UTF (la condificación del archivo).
  442.         $aOrigen  array_map('utf8_decode'array('á''é''í''ó''ú''ñ''ü'));
  443.         $aDestino array_map('utf8_decode'array('a''e''i''o''u''n''u'));
  444.         $str str_ireplace($aOrigen$aDestino$str);
  445.         
  446.         
  447.         $trans array(
  448.             $search                                => $replace,
  449.             "\s+"                                    => $replace,
  450.             "[^a-z0-9".$replace."]"            => '',
  451.             $replace."+"                        => $replace,
  452.             $replace."$"                        => '',
  453.             "^".$replace                        => ''
  454.        );
  455.        
  456.         $str strip_tags(strtolower($str));
  457.     
  458.         foreach ($trans as $key => $val{
  459.             $str preg_replace("#".$key."#"$val$str);
  460.         }
  461.         
  462.         return trim(stripslashes($str));
  463.     }
  464. }
  465.     
  466. // ------------------------------------------------------------------------
  467.  
  468. if (function_exists('redirect')) {
  469.     /**
  470.      * Header Redirect
  471.      *
  472.      * Header redirect in two flavors
  473.      *
  474.      * @access    public
  475.      * @param    string    the URL
  476.      * @param    string    the method: location or redirect
  477.      * @return    string 
  478.      */
  479.     function redirect($uri ''$method 'location')
  480.     {
  481.         switch($method)
  482.         {
  483.             case 'refresh'    header("Refresh:0;url=".site_url($uri));
  484.                 break;
  485.             default            header("Location: ".site_url($uri));
  486.                 break;
  487.         }
  488.         exit;
  489.     }
  490. }
  491.     
  492. // ------------------------------------------------------------------------
  493.  
  494. if (function_exists('_parse_attributes')) {
  495.     /**
  496.      * Parse out the attributes
  497.      *
  498.      * Some of the functions use this
  499.      *
  500.      * @access    private
  501.      * @param    array 
  502.      * @param    bool 
  503.      * @return    string 
  504.      */
  505.     function _parse_attributes($attributes$javascript FALSE)
  506.     {
  507.         if (is_string($attributes))
  508.         {
  509.             return ($attributes != ''' '.$attributes '';
  510.         }
  511.  
  512.         $att '';
  513.         foreach ($attributes as $key => $val)
  514.         {
  515.             if ($javascript == TRUE)
  516.             {
  517.                 $att .= $key '=' $val ',';
  518.             }
  519.             else
  520.             {
  521.                 $att .= ' ' $key '="' $val '"';
  522.             }
  523.         }
  524.     
  525.         if ($javascript == TRUE AND $att != '')
  526.         {
  527.             $att substr($att0-1);
  528.         }
  529.     
  530.         return $att;
  531.     }
  532. }
  533.  
  534.  
  535.  
  536.  
  537.  
  538. function is_utf($t)
  539. {
  540.     if @preg_match ('/.+/u'$t) ) {
  541.          
  542.         return true;
  543.     }
  544. }
  545.  
  546.  
  547. function isEmpty($word)
  548. {
  549.     $isEmpty false;
  550.        
  551.     $empty array(
  552.         "a""ante""bajo""con""contra""de""desde""en",
  553.            "entre""hacia""hasta""para""por""segun""sin""so",
  554.         "sobre""tras""durante""mediante","la","las","los","el",
  555.         "un","uno","una","unos","unas","y","del","este","esto""esta""estos",
  556.         "estas""ese""esa""esos""esas""aquel""aquella""aquellos""aquellas",
  557.         "las","se","va","le","les"
  558.     );
  559.                            
  560.     foreach($empty as $v)
  561.        {
  562.            if($v==$word)
  563.           {
  564.               $isEmpty true;
  565.               break;
  566.         }
  567.       }
  568.       
  569.       return $isEmpty;
  570. }
  571.  
  572. function drop_special_chars($str{
  573.     
  574.     //if(!$sep) $sep = "/";
  575.     //Quita los carateres especiales del array
  576.     $tofind array("/","(",",","|","'","|","\\",".",")","/","[","]","\"",'"',"¿","?","!","¡","-","_","=","%",":"," ");
  577.     $str str_replace$tofind' '$str );
  578.     $tofind '';
  579.     
  580.     $dict array(
  581.         'á' => 'a',
  582.         '�?' => 'A',
  583.         'ç' => 'c',
  584.         'Ç' => 'C',
  585.         'é' => 'e',
  586.         'É' => 'E',
  587.         'í' => 'i',
  588.         '�?' => 'I',
  589.         'ñ' => 'n',
  590.         'Ñ' => 'N',
  591.         'ó' => 'o',
  592.         'Ó' => 'O',
  593.         'ú' => 'u',
  594.         'Ú' => 'U',
  595.         'Ü' => 'U',
  596.         'ü' => 'u'
  597.     );
  598.     
  599.     $str strtr($str$dict);
  600.     
  601.     $str strtolower($str);
  602.     
  603.     return $str;
  604. }
  605.  
  606.  
  607.  
  608.  
  609. function strtosem$str$deleteEmpty=true$id FALSE$sep='/' )
  610. {    
  611.     //quitar caracteres especiales
  612.     $str drop_special_chars$str );
  613.  
  614.     //Obtener un array con las palabras
  615.     $parts explode(' 'trim($str));
  616.     
  617.     if($deleteEmpty)
  618.     {
  619.         $parts_destination array();
  620.             
  621.         foreach($parts as $part)
  622.         {
  623.             
  624.             $parte trim($part);
  625.                 
  626.             if(!isEmpty($part&& !empty($part))
  627.             {
  628.                 $parts_destination[$part;
  629.             }
  630.         }
  631.             
  632.         $str implode($sep,$parts_destination);
  633.     }
  634.     else
  635.     {
  636.         $str implode($sep,$parts);
  637.     }
  638.         
  639.     if($id)
  640.         return $str $sep $id;
  641.     else
  642.         return $str;
  643. }
  644.  
  645.  
  646. /*
  647.  * Convertir una cadena apta para la url
  648.  */
  649. function replace$str$union='/' {
  650.  
  651.     $esc drop_special_chars($str);
  652.     $esc trim($esc);
  653.  
  654.     return str_replace(' ',$union,$esc);
  655.  
  656. }
  657.  
  658. if (function_exists('get_query_string')) {
  659.     /**
  660.      * Get Query String
  661.      *
  662.      * Devuelve una cadena de Query String basada en $_GET.
  663.      * Si se pasan cadenas como parametros se omiten las respectivas
  664.      * variables de $_GET del retorno de la funcion.
  665.      *
  666.      * @access    public
  667.      * @return    string 
  668.      */
  669.     function get_query_string({
  670.         // Obtiene los argumentos que indican que variables ignorar.
  671.         $aArgs func_get_args();
  672.         
  673.         // Buffer de retorno.
  674.         $aRet array();
  675.         
  676.         foreach ($_GET as $var => $item{
  677.             if (in_array($var$aArgs)) {
  678.                 continue;
  679.             }
  680.             else {
  681.                 if (is_array($item)) {
  682.                     foreach ($item as $key => $value{
  683.                         $aRet["{$var}[{$key}]={$value}";
  684.                     }
  685.                 }
  686.                 else {
  687.                     $aRet["{$var}={$item}";
  688.                 }
  689.             }
  690.         }
  691.         
  692.         return join('&amp;'$aRet);
  693.     }
  694. }
  695. ?>

Documentation generated on Tue, 22 Nov 2011 13:29:03 -0200 by phpDocumentor 1.4.3