HTML_BBCodeParser
[ class tree: HTML_BBCodeParser ] [ index: HTML_BBCodeParser ] [ all elements ]

Source for file Links.php

Documentation is available at Links.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stijn de Reede <sjr@gmx.co.uk>                               |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Links.php,v 1.9 2007/06/05 20:54:42 dufuz Exp $
  20. //
  21.  
  22. /**
  23. @package  HTML_BBCodeParser
  24. @author   Stijn de Reede  <sjr@gmx.co.uk>
  25. */
  26.  
  27. /**
  28.  *
  29.  */
  30. require_once 'HTML/BBCodeParser.php';
  31.  
  32. /**
  33.  *
  34.  */
  35. class HTML_BBCodeParser_Filter_Links extends HTML_BBCodeParser
  36. {
  37.     /**
  38.      * List of allowed schemes
  39.      *
  40.      * @access  private
  41.      * @var     array 
  42.      */
  43.     var $_allowedSchemes = array('http''https''ftp');
  44.  
  45.     /**
  46.      * Default scheme
  47.      *
  48.      * @access  private
  49.      * @var     string 
  50.      */
  51.     var $_defaultScheme 'http';
  52.  
  53.     /**
  54.      * An array of tags parsed by the engine
  55.      *
  56.      * @access   private
  57.      * @var      array 
  58.      */
  59.     var $_definedTags = array(
  60.         'url' => array(
  61.             'htmlopen'  => 'a',
  62.             'htmlclose' => 'a',
  63.             'allowed'   => 'none^img',
  64.             'attributes'=> array('url' => 'href=%2$s%1$s%2$s')
  65.         )
  66.     );
  67.  
  68.  
  69.     /**
  70.      * Executes statements before the actual array building starts
  71.      *
  72.      * This method should be overwritten in a filter if you want to do
  73.      * something before the parsing process starts. This can be useful to
  74.      * allow certain short alternative tags which then can be converted into
  75.      * proper tags with preg_replace() calls.
  76.      * The main class walks through all the filters and and calls this
  77.      * method if it exists. The filters should modify their private $_text
  78.      * variable.
  79.      *
  80.      * @return   none 
  81.      * @access   private
  82.      * @see      $_text
  83.      * @author   Stijn de Reede <sjr@gmx.co.uk>
  84.      * @author   Seth Price <seth@pricepages.org>
  85.      */
  86.     function _preparse()
  87.     {
  88.         $options = PEAR::getStaticProperty('HTML_BBCodeParser''_options');
  89.         $o $options['open'];
  90.         $c $options['close'];
  91.         $oe $options['open_esc'];
  92.         $ce $options['close_esc'];
  93.  
  94.         $schemes implode('|'$this->_allowedSchemes);
  95.  
  96.         $pattern = array(   "/(?<![\"'=".$ce."\/])(".$oe."[^".$ce."]*".$ce.")?(((".$schemes."):\/\/|www)[@-a-z0-9.]+\.[a-z]{2,4}[^\s()\[\]]*)/i",
  97.                             "!".$oe."url(".$ce."|\s.*".$ce.")(.*)".$oe."/url".$ce."!iU",
  98.                             "!".$oe."url=((([a-z]*:(//)?)|www)[@-a-z0-9.]+)([^\s\[\]]*)".$ce."(.*)".$oe."/url".$ce."!i");
  99.  
  100.         $pp preg_replace_callback($pattern[0]array($this'smarterPPLinkExpand')$this->_text);
  101.         $pp preg_replace($pattern[1]$o."url=\$2\$1\$2".$o."/url".$c$pp);
  102.         $this->_preparsed preg_replace_callback($pattern[2]array($this'smarterPPLink')$pp);
  103.  
  104.     }
  105.  
  106.     /**
  107.      * Intelligently expand a URL into a link
  108.      *
  109.      * @return  string 
  110.      * @access  private
  111.      * @author  Seth Price <seth@pricepages.org>
  112.      * @author  Lorenzo Alberton <l.alberton@quipo.it>
  113.      */
  114.     function smarterPPLinkExpand($matches)
  115.     {
  116.         //echo '<hr><pre>';var_dump($matches);echo '</pre><hr>';
  117.         $options = PEAR::getStaticProperty('HTML_BBCodeParser','_options');
  118.         $o $options['open'];
  119.         $c $options['close'];
  120.  
  121.         //If we have an intro tag that is [url], then skip this match
  122.         if ($matches[1== $o.'url'.$c{
  123.             return $matches[0];
  124.         }
  125.  
  126.         $punctuation '.,;:'// Links can't end with these chars
  127.         $trailing '';
  128.         // Knock off ending punctuation
  129.         $last substr($matches[2]-1);
  130.         while (strpos($punctuation$last!== false{
  131.             // Last character is punctuation - remove it from the url
  132.             $trailing $last.$trailing;
  133.             $matches[2substr($matches[2]0-1);
  134.             $last substr($matches[2]-1);
  135.         }
  136.  
  137.         $off strpos($matches[2]':');
  138.  
  139.         //Is a ":" (therefore a scheme) defined?
  140.         if ($off === false{
  141.             /*
  142.              * Create a link with the default scheme of http. Notice that the
  143.              * text that is viewable to the user is unchanged, but the link
  144.              * itself contains the "http://".
  145.              */
  146.             return $matches[1].$o.'url='.$this->_defaultScheme.'://'.$matches[2].$c.$matches[2].$o.'/url'.$c.$trailing;
  147.         }
  148.  
  149.         $scheme substr($matches[2]0$off);
  150.  
  151.         /*
  152.          * If protocol is in the approved list than allow it. Note that this
  153.          * check isn't really needed, but the created link will just be deleted
  154.          * later in smarterPPLink() if we create it now and it isn't on the
  155.          * scheme list.
  156.          */
  157.         if (in_array($scheme$this->_allowedSchemes)) {
  158.             return $matches[1].$o.'url'.$c.$matches[2].$o.'/url'.$c.$trailing;
  159.         }
  160.         
  161.         return $matches[0];
  162.     }
  163.  
  164.     /**
  165.      * Finish preparsing URL to clean it up
  166.      *
  167.      * @return  string 
  168.      * @access  private
  169.      * @author  Seth Price <seth@pricepages.org>
  170.      */
  171.     function smarterPPLink($matches)
  172.     {
  173.         $options = PEAR::getStaticProperty('HTML_BBCodeParser','_options');
  174.         $o $options['open'];
  175.         $c $options['close'];
  176.  
  177.         $urlServ $matches[1];
  178.         $path $matches[5];
  179.  
  180.         $off strpos($urlServ':');
  181.  
  182.         if ($off === false{
  183.             //Default to http
  184.             $urlServ $this->_defaultScheme.'://'.$urlServ;
  185.             $off strpos($urlServ':');
  186.         }
  187.  
  188.         //Add trailing slash if missing (to create a valid URL)
  189.         if (!$path{
  190.             $path '/';
  191.         }
  192.  
  193.         $protocol substr($urlServ0$off);
  194.  
  195.         if (in_array($protocol$this->_allowedSchemes)) {
  196.             //If protocol is in the approved list than allow it
  197.             return $o.'url='.$urlServ.$path.$c.$matches[6].$o.'/url'.$c;
  198.         }
  199.         
  200.         //Else remove url tag
  201.         return $matches[6];
  202.     }
  203. }
  204. ?>

Documentation generated on Tue, 05 Jun 2007 17:30:06 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.