File indexing completed on 2024-05-12 06:02:05

0001 <?php
0002 
0003 /**
0004  * Injector that converts configuration directive syntax %Namespace.Directive
0005  * to links
0006  */
0007 class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector
0008 {
0009     /**
0010      * @type string
0011      */
0012     public $name = 'PurifierLinkify';
0013 
0014     /**
0015      * @type string
0016      */
0017     public $docURL;
0018 
0019     /**
0020      * @type array
0021      */
0022     public $needed = array('a' => array('href'));
0023 
0024     /**
0025      * @param HTMLPurifier_Config $config
0026      * @param HTMLPurifier_Context $context
0027      * @return string
0028      */
0029     public function prepare($config, $context)
0030     {
0031         $this->docURL = $config->get('AutoFormat.PurifierLinkify.DocURL');
0032         return parent::prepare($config, $context);
0033     }
0034 
0035     /**
0036      * @param HTMLPurifier_Token $token
0037      */
0038     public function handleText(&$token)
0039     {
0040         if (!$this->allowsElement('a')) {
0041             return;
0042         }
0043         if (strpos($token->data, '%') === false) {
0044             return;
0045         }
0046 
0047         $bits = preg_split('#%([a-z0-9]+\.[a-z0-9]+)#Si', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
0048         $token = array();
0049 
0050         // $i = index
0051         // $c = count
0052         // $l = is link
0053         for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
0054             if (!$l) {
0055                 if ($bits[$i] === '') {
0056                     continue;
0057                 }
0058                 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
0059             } else {
0060                 $token[] = new HTMLPurifier_Token_Start(
0061                     'a',
0062                     array('href' => str_replace('%s', $bits[$i], $this->docURL))
0063                 );
0064                 $token[] = new HTMLPurifier_Token_Text('%' . $bits[$i]);
0065                 $token[] = new HTMLPurifier_Token_End('a');
0066             }
0067         }
0068     }
0069 }
0070 
0071 // vim: et sw=4 sts=4