Changeset 4213 in subversion


Ignore:
Timestamp:
Nov 11, 2010 3:18:53 AM (3 years ago)
Author:
alec
Message:
  • Handle propely HTML entities. Don't replace ";)" in e.g. "")"
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/emoticons/emoticons.php

    r3807 r4213  
    66 * Sample plugin to replace emoticons in plain text message body with real icons 
    77 * 
    8  * @version 1.1.0 
     8 * @version 1.2.0 
    99 * @author Thomas Bruederli 
    1010 * @author Aleksander Machniak 
     
    1414{ 
    1515    public $task = 'mail'; 
    16     private $map; 
    1716 
    1817    function init() 
    1918    { 
    20         $this->task = 'mail'; 
    2119        $this->add_hook('message_part_after', array($this, 'replace')); 
    22    
    23         $this->map = array( 
    24             '/:\)/'  => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-smile.gif', 'title' => ':)')), 
    25             '/:-\)/' => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-smile.gif', 'title' => ':-)')), 
    26             '/(?<!mailto):D/' => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-laughing.gif', 'title' => ':D')), 
    27             '/:-D/' => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-laughing.gif', 'title' => ':-D')), 
    28             '/;\)/'  => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-wink.gif', 'title' => ';)')), 
    29             '/;-\)/' => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-wink.gif', 'title' => ';-)')), 
    30             '/:\(/'  => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-frown.gif', 'title' => ':(')), 
    31             '/:-\(/' => html::img(array('src' => './program/js/tiny_mce/plugins/emotions/img/smiley-frown.gif', 'title' => ':-(')), 
    32         ); 
    3320    } 
    3421 
    3522    function replace($args) 
    3623    { 
     24        // This is a lookbehind assertion which will exclude html entities 
     25        // E.g. situation when ";)" in "&quot;)" shouldn't be replaced by the icon 
     26        // It's so long because of assertion format restrictions 
     27        $entity = '(?<!&' 
     28            . '[a-zA-Z0-9]{2}' . '|' . '#[0-9]{2}' . '|' 
     29            . '[a-zA-Z0-9]{3}' . '|' . '#[0-9]{3}' . '|' 
     30            . '[a-zA-Z0-9]{4}' . '|' . '#[0-9]{4}' . '|' 
     31            . '[a-zA-Z0-9]{5}' . '|' 
     32            . '[a-zA-Z0-9]{6}' . '|' 
     33            . '[a-zA-Z0-9]{7}' 
     34            . ')'; 
     35 
     36        // map of emoticon replacements 
     37        $map = array( 
     38            '/:\)/' => html::img(array( 
     39                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-smile.gif', 
     40                'title' => ':)' 
     41            )), 
     42            '/:-\)/' => html::img(array( 
     43                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-smile.gif', 
     44                'title' => ':-)' 
     45            )), 
     46            '/(?<!mailto):D/' => html::img(array( 
     47                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-laughing.gif', 
     48                'title' => ':D' 
     49            )), 
     50            '/:-D/' => html::img(array( 
     51                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-laughing.gif', 
     52                'title' => ':-D' 
     53            )), 
     54            '/:\(/' => html::img(array( 
     55                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-frown.gif', 
     56                'title' => ':(' 
     57            )), 
     58            '/:-\(/' => html::img(array( 
     59                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-frown.gif', 
     60                'title' => ':-(' 
     61            )), 
     62            '/'.$entity.';\)/' => html::img(array( 
     63                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-wink.gif', 
     64                'title' => ';)' 
     65            )), 
     66            '/'.$entity.';-\)/' => html::img(array( 
     67                'src'   => './program/js/tiny_mce/plugins/emotions/img/smiley-wink.gif', 
     68                'title' => ';-)' 
     69            )), 
     70        ); 
     71 
    3772        if ($args['type'] == 'plain') { 
    3873            $args['body'] = preg_replace( 
    39                 array_keys($this->map), array_values($this->map), $args['body']); 
     74                array_keys($map), array_values($map), $args['body']); 
    4075        } 
     76 
    4177        return $args; 
    4278    } 
Note: See TracChangeset for help on using the changeset viewer.