b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/_smilies.plugin.php - 435 lines - 11841 bytes - Summary - Text - Print

Description: This file implements the Image Smilies Renderer plugin for b2evolution b2evolution - {@link http://b2evolution.net/} Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   1  <?php
   2  /**
   3   * This file implements the Image Smilies Renderer plugin for b2evolution
   4   *
   5   * b2evolution - {@link http://b2evolution.net/}
   6   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
   7   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   8   *
   9   * @author fplanque: Francois PLANQUE.
  10   * @author gorgeb: Bertrand GORGE / EPISTEMA
  11   *
  12   * @package plugins
  13   */
  14  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  15  
  16  
  17  /**
  18   * @package plugins
  19   */
  20  class smilies_plugin extends Plugin
  21  {
  22      var $code = 'b2evSmil';
  23      var $name = 'Smilies';
  24      /**
  25       * @todo dh> Should get a low priority (e.g. 80) so it does not create icon image
  26       *           tags which then get processed by another plugin.
  27       *           Is there any benefit from a high prio like now? So that we do not
  28       *           match "generated" simlies later?
  29       * fp> There is... I can't remember the exact problem thouh. Probably some interaction with the code highlight or the video plugins.
  30       */
  31      var $priority = 15;
  32      var $version = '5.0.0';
  33      var $group = 'rendering';
  34      var $number_of_installs = 3; // QUESTION: dh> why 3?
  35  
  36      /**
  37       * Text similes search array
  38       *
  39       * @access private
  40       */
  41      var $search;
  42  
  43      /**
  44       * IMG replace array
  45       *
  46       * @access private
  47       */
  48      var $replace;
  49  
  50      /**
  51       * Smiley definitions
  52       *
  53       * @access private
  54       */
  55      var $smilies;
  56  
  57      /**
  58       * Init
  59       */
  60  	function PluginInit( & $params )
  61      {
  62          $this->short_desc = T_('Graphical smileys');
  63          $this->long_desc = T_('This renderer will convert text smilies like :) to graphical icons.<br />
  64              Optionally, it will also display a toolbar for quick insertion of smilies into a post.');
  65      }
  66  
  67  
  68      /**
  69      * Defaults for user specific settings: "Display toolbar"
  70       *
  71       * @return array
  72       */
  73  	function GetDefaultSettings()
  74      {
  75          global $rsc_subdir;
  76          return array(
  77                  'use_toolbar_default' => array(
  78                      'label' => T_('Use smilies toolbar'),
  79                      'defaultvalue' => 0,
  80                      'type' => 'checkbox',
  81                      'note' => T_('This is the default setting. Users can override it in their profile.'),
  82                  ),
  83                  // TODO (yabs) : Display these as images and individual inputs
  84                  'smiley_list' => array(
  85                      'label' => $this->T_('Smiley list'),
  86                      'note' => sprintf( $this->T_('This is the list of smileys [one per line], in the format : char_sequence image_file // optional comment<br />
  87                              To disable a smiley, just add one or more spaces to the start of its setting<br />
  88                              You can add new smiley images by uploading the images to the %s folder.' ), '<span style="font-weight:bold">'.$rsc_subdir.'smilies/</span>' ),
  89                      'type' => 'html_textarea', // allows smilies with "<" in them
  90                      'rows' => 10,
  91                      'cols' => 60,
  92                      'defaultvalue' => '
  93   =>       icon_arrow.gif
  94  :!:      icon_exclaim.gif
  95  :?:      icon_question.gif
  96  :idea:   icon_idea.gif
  97  :)       icon_smile.gif
  98  :D       icon_biggrin.gif
  99  :p       icon_razz.gif
 100  B)       icon_cool.gif
 101  ;)       icon_wink.gif
 102  :>       icon_twisted.gif
 103  :roll:   icon_rolleyes.gif
 104  :oops:   icon_redface.gif
 105  :|       icon_neutral.gif
 106  :-/      icon_confused.gif
 107  :(       icon_sad.gif
 108   >:(      icon_mad.gif
 109  :\'(      icon_cry.gif
 110  |-|      icon_wth.gif
 111  :>>      icon_mrgreen.gif
 112  :yes:    grayyes.gif
 113  ;D       graysmilewinkgrin.gif
 114  :P       graybigrazz.gif
 115  :))      graylaugh.gif
 116  88|      graybigeek.gif
 117  :.       grayshy.gif
 118  :no:     grayno.gif
 119  XX(      graydead.gif
 120  :lalala: icon_lalala.gif
 121  :crazy:  icon_crazy.gif
 122  >:XX     icon_censored.gif
 123   :DD     icon_lol.gif
 124   :o      icon_surprised.gif
 125   8|      icon_eek.gif
 126   >:-[    icon_evil.gif
 127   :)      graysmile.gif
 128   :b      grayrazz.gif
 129   )-o     grayembarrassed.gif
 130   U-(     grayuhoh.gif
 131   :(      graysad.gif
 132   :**:    graysigh.gif     // alternative: graysighw.gif
 133   :??:    grayconfused.gif // alternative: grayconfusedw.gif
 134   :`(     graycry.gif
 135   >:-(    graymad.gif
 136   :##      grayupset.gif   // alternative: grayupsetw.gif
 137   :zz:    graysleep.gif    // alternative: graysleepw.gif
 138   :wave:  icon_wave.gif',
 139                  ),
 140              );
 141  }
 142  
 143  
 144      /**
 145       * Allowing the user to override the display of the toolbar.
 146       *
 147       * @return array
 148       */
 149  	function GetDefaultUserSettings()
 150      {
 151          return array(
 152                  'use_toolbar' => array(
 153                      'label' => T_('Use smilies toolbar'),
 154                      'defaultvalue' => $this->Settings->get('use_toolbar_default'),
 155                      'type' => 'checkbox',
 156                  ),
 157              );
 158      }
 159  
 160  
 161      /**
 162       * Define here default collection/blog settings that are to be made available in the backoffice.
 163       *
 164       * @param array Associative array of parameters.
 165       * @return array See {@link Plugin::get_coll_setting_definitions()}.
 166       */
 167  	function get_coll_setting_definitions( & $params )
 168      {
 169          $default_params = array_merge( $params, array( 'default_post_rendering' => 'opt-in' ) );
 170          return parent::get_coll_setting_definitions( $default_params );
 171      }
 172  
 173  
 174      /**
 175       * Display a toolbar in admin
 176       *
 177       * @param array Associative array of parameters
 178       * @return boolean did we display a toolbar?
 179       */
 180  	function AdminDisplayToolbar( & $params )
 181      {
 182          if( $this->UserSettings->get('use_toolbar') )
 183          {
 184              return $this->display_smiley_bar();
 185          }
 186          return false;
 187      }
 188  
 189  
 190      /**
 191       * Event handler: Called when displaying editor toolbars.
 192       *
 193       * @param array Associative array of parameters
 194       * @return boolean did we display a toolbar?
 195       */
 196  	function DisplayCommentToolbar( & $params )
 197      {
 198          if( !empty( $params['Comment'] ) )
 199          { // Comment is set, get Blog from comment
 200              $Comment = & $params['Comment'];
 201              if( !empty( $Comment->item_ID ) )
 202              {
 203                  $comment_Item = & $Comment->get_Item();
 204                  $Blog = & $comment_Item->get_Blog();
 205              }
 206          }
 207  
 208          if( empty( $Blog ) )
 209          { // Comment is not set, try global Blog
 210              global $Blog;
 211              if( empty( $Blog ) )
 212              { // We can't get a Blog, this way "apply_comment_rendering" plugin collection setting is not available
 213                  return false;
 214              }
 215          }
 216  
 217          if( $this->get_coll_setting( 'coll_apply_comment_rendering', $Blog )
 218          && ( ( is_logged_in() && $this->UserSettings->get( 'use_toolbar' ) )
 219              || ( !is_logged_in() && $this->Settings->get( 'use_toolbar_default' ) ) ) )
 220          {
 221              return $this->display_smiley_bar();
 222          }
 223          return false;
 224      }
 225  
 226  
 227      /**
 228       * Display the smiley toolbar
 229       *
 230       * @return boolean did we display a toolbar?
 231       */
 232  	function display_smiley_bar()
 233      {
 234          $this->InitSmilies();    // check smilies cached
 235  
 236          $grins = '';
 237          $smiled = array();
 238          foreach( $this->smilies as $smiley )
 239          {
 240              if( ! in_array($smiley['image'], $smiled) )
 241              { // include any smiley only once
 242                  $smiled[] = $smiley['image'];
 243  
 244                  $grins .= $this->get_smiley_img_tag( $smiley, array(
 245                      'class' => 'top',
 246                      'onclick' => 'textarea_wrap_selection( b2evoCanvas, \''. str_replace("'", "\'", $smiley['code']). '\', \'\', 1 );' ) )
 247                      .' ';
 248              }
 249          }
 250  
 251          echo '<div class="edit_toolbar" id="smiley_toolbar">'.$grins.'</div>' ;
 252  
 253          return true;
 254      }
 255  
 256  
 257      /**
 258       * Perform rendering
 259       *
 260       * @see Plugin::RenderItemAsHtml()
 261       */
 262  	function RenderItemAsHtml( & $params )
 263      {
 264          $this->InitSmilies();    // check smilies are already cached
 265  
 266  
 267          if( ! isset( $this->search ) )
 268          {    // We haven't prepared the smilies yet
 269              $this->search = array();
 270  
 271              $tmpsmilies = $this->smilies;
 272              usort($tmpsmilies, array(&$this, 'smiliescmp'));
 273  
 274              foreach( $tmpsmilies as $smiley )
 275              {
 276                  $this->search[] = $smiley['code'];
 277                  $smiley_masked = '';
 278                  for( $i = 0; $i < strlen($smiley['code'] ); $i++ )
 279                  {
 280                      $smiley_masked .= '&#'.ord(substr($smiley['code'], $i, 1)).';';
 281                  }
 282                  $smiley['code'] = $smiley_masked;
 283                  $this->replace[] = $this->get_smiley_img_tag($smiley);
 284              }
 285          }
 286  
 287  
 288          // REPLACE:  But only in non-HTML blocks, totally excluding <CODE>..</CODE> and <PRE>..</PRE>
 289  
 290          $content = & $params['data'];
 291  
 292          // Lazy-check first, using stristr() (stripos() is only available since PHP5):
 293          if( stristr( $content, '<code' ) !== false || stristr( $content, '<pre' ) !== false )
 294          { // Call ReplaceTagSafe() on everything outside code/pre:
 295              $content = callback_on_non_matching_blocks( $content,
 296                      '~<(code|pre)[^>]*>.*?</\1>~is',
 297                      array( & $this, 'ReplaceTagSafe' ) );
 298          }
 299          else
 300          { // No code/pre blocks, replace on the whole thing
 301              $content = $this->ReplaceTagSafe($content);
 302          }
 303  
 304          return true;
 305      }
 306  
 307  
 308      /**
 309       * @param array Smiley
 310       * @param array Override params, e.g. "class"
 311       */
 312  	function get_smiley_img_tag($smiley, $override_fields = array())
 313      {
 314          $attribs = array(
 315              'src' => $smiley['image'],
 316              'title' => htmlspecialchars($smiley['code']),
 317              'alt' => htmlspecialchars($smiley['code']),
 318              'class' => 'middle',
 319              );
 320  
 321          if( $smiley_wh = imgsize($smiley['path'], 'widthheight_assoc') )
 322              $attribs += $smiley_wh;
 323  
 324          if( $override_fields )
 325              $attribs = $override_fields + $attribs;
 326  
 327          return '<img'.get_field_attribs_as_string($attribs).' />';
 328      }
 329  
 330  
 331      /**
 332       * This callback gets called once after every tags+text chunk
 333       * @return string Text with replaced smilies
 334       */
 335  	function preg_insert_smilies_callback( $text )
 336      {
 337          return str_replace( $this->search, $this->replace, $text );
 338      }
 339  
 340  
 341      /**
 342       * Replace smilies in non-HTML-tag portions of the text.
 343       * @uses callback_on_non_matching_blocks()
 344       */
 345  	function ReplaceTagSafe($text)
 346      {
 347          return callback_on_non_matching_blocks( $text, '~<[^>]*>~', array(&$this, 'preg_insert_smilies_callback') );
 348      }
 349  
 350  
 351      /**
 352       * sorts the smilies' array by length
 353       * this is important if you want :)) to superseede :) for example
 354       */
 355  	function smiliescmp($a, $b)
 356      {
 357          if( ($diff = strlen( $b[ 'code' ] ) - strlen( $a[ 'code' ] ) ) == 0)
 358          {
 359              return strcmp( $a[ 'code' ], $b[ 'code' ] );
 360          }
 361          return $diff;
 362      }
 363  
 364  
 365      /**
 366       * Initiates the smiley array if not already initiated
 367       *
 368       * Attempts to use skin specific smileys where available
 369       *    - skins_adm/skin/rsc/smilies/
 370       *    - skins/skin/smilies/
 371       *
 372       * Attempts to fallback to default smilies
 373       *    - rsc/smilies/
 374       *
 375       * If no image file found the smiley is not added
 376       *
 377       * @return array of available smilies( code, image url )
 378       */
 379  	function InitSmilies()
 380      {
 381          if( isset( $this->smilies ) )
 382          { // smilies are already cached
 383              return;
 384          }
 385  
 386          global $admin_skin, $adminskins_path, $adminskins_url, $rsc_path, $rsc_url, $skin, $skins_path, $skins_url;
 387  
 388          // set the skin path/url and the default (rsc) path/url
 389          $currentskin_path = ( is_admin_page() ? $adminskins_path.$admin_skin.'/rsc' : $skins_path.$skin ).'/smilies/';
 390          $currentskin_url = ( is_admin_page() ? $adminskins_url.$admin_skin.'/rsc' : $skins_url.$skin ).'/smilies/';
 391          $default_path = $rsc_path.'smilies/';
 392          $default_url = $rsc_url.'smilies/';
 393  
 394          $skin_has_smilies = is_dir( $currentskin_path );    // check if skin has a /smilies/ folder
 395  
 396          $this->smilies = array();
 397          $temp_list = explode( "\n", str_replace( array( "\r", "\t" ), '', $this->Settings->get( 'smiley_list' ) ) );
 398  
 399          foreach( $temp_list as $temp_smiley )
 400          {
 401              $a_smiley = explode( '<->',    preg_replace_callback( '#^(\S.+?\s)(.+?)(\/\/.*?)*$#', array( $this, 'get_smiley' ),$temp_smiley ) );
 402              if( isset( $a_smiley[0] ) and isset( $a_smiley[1] ) )
 403              {
 404                  // lets see if the file exists
 405                  $temp_img = trim( $a_smiley[1] );
 406                  if( $skin_has_smilies && is_file( $currentskin_path.$temp_img ) )
 407                  {
 408                      $temp_url = $currentskin_url.$temp_img;    // skin has it's own smiley, use it
 409                      $temp_path = $currentskin_path.$temp_img;
 410                  }
 411                  elseif ( is_file( $default_path.$temp_img ) )
 412                  {
 413                      $temp_url = $default_url.$temp_img; // no skin image, but default smiley found so use it
 414                      $temp_path = $default_path.$temp_img;
 415                  }
 416                  else
 417                  {
 418                      $temp_url = ''; // no smiley image found, so don't add the smiley
 419                  }
 420  
 421                  if( $temp_url )
 422                      $this->smilies[] = array( 'code' => trim( $a_smiley[0] ), 'image' => $temp_url, 'path' => $temp_path );
 423              }
 424          }
 425      }
 426  
 427      // returns the relevant smiley parts (char_code, image_file)
 428  	function get_smiley( $smiley_parts )
 429      {
 430          return ( ( isset( $smiley_parts[1] ) && isset( $smiley_parts[2] ) ) ? $smiley_parts[1].'<->'.$smiley_parts[2] : '' );
 431      }
 432  }
 433  
 434  
 435  ?>

title

Description

title

Description

title

Description

title

title

Body