[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/plugins/ -> _smilies.plugin.php (source)

   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-2007 by Francois PLANQUE - {@link http://fplanque.net/}

   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 = '1.10';
  33      var $apply_rendering = 'opt-out';
  34      var $group = 'rendering';
  35      var $number_of_installs = 3; // QUESTION: dh> why 3?

  36  
  37      /**

  38       * Text similes search array

  39       *

  40       * @access private

  41       */
  42      var $search;
  43  
  44      /**

  45       * IMG replace array

  46       *

  47       * @access private

  48       */
  49      var $replace;
  50  
  51      /**

  52       * Smiley definitions

  53       *

  54       * @access private

  55       */
  56      var $smilies;
  57  
  58      /**

  59       * Init

  60       */
  61  	function PluginInit( & $params )
  62      {
  63          $this->short_desc = T_('Graphical smileys');
  64          $this->long_desc = T_('This renderer will convert text smilies like :) to graphical icons.<br />
  65              Optionally, it will also display a toolbar for quick insertion of smilies into a post.');
  66      }
  67  
  68  
  69      /**

  70      * Defaults for user specific settings: "Display toolbar"

  71       *

  72       * @return array

  73       */
  74  	function GetDefaultSettings()
  75      {
  76          global $rsc_subdir;
  77          return array(
  78                  'use_toolbar_default' => array(
  79                      'label' => T_( 'Use smilies toolbar' ),
  80                      'defaultvalue' => '1',
  81                      'type' => 'checkbox',
  82                      'note' => T_( 'This is the default setting. Users can override it in their profile.' ),
  83                  ),
  84                  'render_comments' => array(    // fp> Note: this is not a default in this version, it's an 'always' :]
  85                      'label' => $this->T_('Render comments' ),
  86                      'note' => $this->T_('Check to also render smilies in comments.'),
  87                      'defaultvalue' => '0',
  88                      'type' => 'checkbox',
  89                  ),
  90                  // TODO (yabs) : Display these as images and individual inputs

  91                  'smiley_list' => array(
  92                      'label' => $this->T_( 'Smiley list'),
  93                      'note' => sprintf( $this->T_( 'This is the list of smileys [one per line], in the format : char_sequence image_file // optional comment<br />
  94                              To disable a smiley, just add one or more spaces to the start of its setting<br />
  95                              You can add new smiley images by uploading the images to the %s folder.' ), '<span style="font-weight:bold">'.$rsc_subdir.'smilies/</span>' ),
  96                      'type' => 'html_textarea', // allows smilies with "<" in them
  97                      'rows' => 10,
  98                      'cols' => 60,
  99                      'defaultvalue' => '
 100   =>       icon_arrow.gif
 101  :!:      icon_exclaim.gif
 102  :?:      icon_question.gif
 103  :idea:   icon_idea.gif
 104  :)       icon_smile.gif
 105  :D       icon_biggrin.gif
 106  :p       icon_razz.gif
 107  B)       icon_cool.gif
 108  ;)       icon_wink.gif
 109  :>       icon_twisted.gif
 110  :roll:   icon_rolleyes.gif
 111  :oops:   icon_redface.gif
 112  :|       icon_neutral.gif
 113  :-/      icon_confused.gif
 114  :(       icon_sad.gif
 115   >:(      icon_mad.gif
 116  :\'(      icon_cry.gif
 117  |-|      icon_wth.gif
 118  :>>      icon_mrgreen.gif
 119  :yes:    grayyes.gif
 120  ;D       graysmilewinkgrin.gif
 121  :P       graybigrazz.gif
 122  :))      graylaugh.gif
 123  88|      graybigeek.gif
 124  :.       grayshy.gif
 125  :no:     grayno.gif
 126  XX(      graydead.gif
 127  :lalala: icon_lalala.gif
 128  :crazy:  icon_crazy.gif
 129  >:XX     icon_censored.gif
 130   :DD     icon_lol.gif
 131   :o      icon_surprised.gif
 132   8|      icon_eek.gif
 133   >:-[    icon_evil.gif
 134   :)      graysmile.gif
 135   :b      grayrazz.gif
 136   )-o     grayembarrassed.gif
 137   U-(     grayuhoh.gif
 138   :(      graysad.gif
 139   :**:    graysigh.gif     // alternative: graysighw.gif
 140   :??:    grayconfused.gif // alternative: grayconfusedw.gif
 141   :`(     graycry.gif
 142   >:-(    graymad.gif
 143   :##      grayupset.gif   // alternative: grayupsetw.gif
 144   :zz:    graysleep.gif    // alternative: graysleepw.gif
 145   :wave:  icon_wave.gif',
 146                  ),
 147              );
 148  }
 149  
 150  
 151      /**

 152       * Allowing the user to override the display of the toolbar.

 153       *

 154       * @return array

 155       */
 156  	function GetDefaultUserSettings()
 157      {
 158          return array(
 159                  'use_toolbar' => array(
 160                      'label' => T_( 'Use smilies toolbar' ),
 161                      'defaultvalue' => $this->Settings->get('use_toolbar_default'),
 162                      'type' => 'checkbox',
 163                  ),
 164              );
 165      }
 166  
 167  
 168      /**

 169       * Display a toolbar in admin

 170       *

 171       * @param array Associative array of parameters

 172       * @return boolean did we display a toolbar?

 173       */
 174  	function AdminDisplayToolbar( & $params )
 175      {
 176          if( $this->UserSettings->get('use_toolbar') )
 177          {
 178              return $this->display_smiley_bar();
 179          }
 180          return false;
 181      }
 182  
 183  
 184      /**

 185       * Event handler: Called when displaying editor toolbars.

 186       *

 187       * @param array Associative array of parameters

 188       * @return boolean did we display a toolbar?

 189       */
 190  	function DisplayCommentToolbar( & $params )
 191      {
 192          if( $this->Settings->get( 'render_comments' )
 193          && ( ( is_logged_in() && $this->UserSettings->get( 'use_toolbar' ) )
 194              || ( !is_logged_in() && $this->Settings->get( 'use_toolbar_default' ) ) ) )
 195          {    
 196              return $this->display_smiley_bar();
 197          }
 198          return false;
 199      }
 200  
 201  
 202      /**

 203       * Display the smiley toolbar

 204       *

 205       * @return boolean did we display a toolbar?

 206       */
 207  	function display_smiley_bar()
 208      {
 209          $this->InitSmilies();    // check smilies cached

 210  
 211          $grins = '';
 212          $smiled = array();
 213          foreach( $this->smilies as $smiley )
 214          {
 215              if (!in_array($smiley[ 'image' ], $smiled))
 216              {
 217                  $smiled[] = $smiley[ 'image'];
 218                  $smiley[ 'code' ] = str_replace(' ', '', $smiley[ 'code' ]);
 219                  $grins .= '<img src="'.$smiley[ 'image' ].'" title="'.$smiley[ 'code' ].'" alt="'.$smiley[ 'code' ]
 220                                      .'" class="top" onclick="textarea_wrap_selection( b2evoCanvas, \''. str_replace("'","\'",$smiley[ 'code' ]). '\', \'\', 1 );" /> ';
 221              }
 222          }
 223  
 224          echo '<div class="edit_toolbar">'.$grins.'</div>' ;
 225  
 226          return true;
 227      }
 228  
 229  
 230      /**

 231       * Perform rendering

 232       *

 233       * @see Plugin::FilterCommentContent()

 234       */
 235  	function FilterCommentContent( & $params )
 236      {
 237          if( $this->Settings->get( 'render_comments' ) )
 238          {
 239              $this->RenderItemAsHtml( $params );
 240          }
 241      }    
 242      
 243  
 244  
 245      /**

 246       * Perform rendering

 247       *

 248       * @see Plugin::RenderItemAsHtml()

 249       */
 250  	function RenderItemAsHtml( & $params )
 251      {
 252          $this->InitSmilies();    // check smilies are already cached

 253  
 254  
 255          if( ! isset( $this->search ) )
 256          {    // We haven't prepared the smilies yet
 257              $this->search = array();
 258  
 259              $tmpsmilies = $this->smilies;
 260              usort($tmpsmilies, array(&$this, 'smiliescmp'));
 261  
 262              foreach( $tmpsmilies as $smiley )
 263              {
 264                  $this->search[] = $smiley[ 'code' ];
 265                  $smiley_masked = '';
 266                  for ($i = 0; $i < strlen($smiley[ 'code' ] ); $i++ )
 267                  {
 268                      $smiley_masked .=  '&#'.ord(substr($smiley[ 'code' ], $i, 1)).';';
 269                  }
 270  
 271                  // We don't use getimagesize() here until we have a mean

 272                  // to preprocess smilies. It takes up to much time when

 273                  // processing them at display time.

 274                  $this->replace[] = '<img src="'.$smiley[ 'image' ].'" alt="'.$smiley_masked.'" class="middle" />';
 275              }
 276          }
 277  
 278  
 279          // REPLACE:  But only in non-HTML blocks, totally excluding <CODE>..</CODE> and <PRE>..</PRE>

 280  
 281          $content = & $params['data'];
 282  
 283          // Lazy-check first, using stristr() (stripos() is only available since PHP5):

 284          if( stristr( $content, '<code' ) !== false || stristr( $content, '<pre' ) !== false )
 285          { // Call ReplaceTagSafe() on everything outside <pre></pre> and <code></code>:
 286              $content = callback_on_non_matching_blocks( $content,
 287                      '~<(code|pre)[^>]*>.*?</\1>~is',
 288                      array( & $this, 'ReplaceTagSafe' ) );
 289          }
 290          else
 291          { // No CODE or PRE blocks, replace on the whole thing
 292              $content = $this->ReplaceTagSafe($content);
 293          }
 294  
 295          return true;
 296      }
 297  
 298  
 299      /**

 300       * This callback gets called once after every tags+text chunk

 301       * @return string Text with replaced smilies

 302       */
 303  	function preg_insert_smilies_callback( $text )
 304      {
 305          return str_replace( $this->search, $this->replace, $text );
 306      }
 307  
 308  
 309      /**

 310       * Replace smilies in non-HTML-tag portions of the text.

 311       * @uses callback_on_non_matching_blocks()

 312       */
 313  	function ReplaceTagSafe($text)
 314      {
 315          return callback_on_non_matching_blocks( $text, '~<[^>]*>~', array(&$this, 'preg_insert_smilies_callback') );
 316      }
 317  
 318  
 319      /**

 320       * sorts the smilies' array by length

 321       * this is important if you want :)) to superseede :) for example

 322       */
 323  	function smiliescmp($a, $b)
 324      {
 325          if( ($diff = strlen( $b[ 'code' ] ) - strlen( $a[ 'code' ] ) ) == 0)
 326          {
 327              return strcmp( $a[ 'code' ], $b[ 'code' ] );
 328          }
 329          return $diff;
 330      }
 331  
 332  
 333      /**

 334       * Initiates the smiley array if not already initiated

 335       *

 336       * Attempts to use skin specific smileys where available

 337       *    - skins_adm/skin/rsc/smilies/

 338       *    - skins/skin/smilies/

 339       *

 340       * Attempts to fallback to default smilies

 341       *    - rsc/smilies/

 342       *

 343       * If no image file found the smiley is not added

 344       *

 345       * @return array of available smilies( code, image url )

 346       */
 347  	function InitSmilies()
 348      {
 349          if( isset( $this->smilies ) )
 350          { // smilies are already cached
 351              return;
 352          }
 353  
 354          global $admin_skin, $adminskins_path, $adminskins_url, $rsc_path, $rsc_url, $skin, $skins_path, $skins_url;
 355  
 356          // set the skin path/url and the default (rsc) path/url

 357          $currentskin_path = ( is_admin_page() ? $adminskins_path.$admin_skin.'/rsc' : $skins_path.$skin ).'/smilies/';
 358          $currentskin_url = ( is_admin_page() ? $adminskins_url.$admin_skin.'/rsc' : $skins_url.$skin ).'/smilies/';
 359          $default_path = $rsc_path.'smilies/';
 360          $default_url = $rsc_url.'smilies/';
 361  
 362          $skin_has_smilies = is_dir( $currentskin_path );    // check if skin has a /smilies/ folder

 363  
 364          $this->smilies = array();
 365          $temp_list = explode( "\n", str_replace( array( "\r", "\t" ), '', $this->Settings->get( 'smiley_list' ) ) );
 366  
 367          foreach( $temp_list as $temp_smiley )
 368          {
 369              $a_smiley = explode( '<->',    preg_replace_callback( '#^(\S.+?\s)(.+?)(\/\/.*?)*$#', array( $this, 'get_smiley' ),$temp_smiley ) );
 370              if( isset( $a_smiley[0] ) and isset( $a_smiley[1] ) )
 371              {
 372                  // lets see if the file exists

 373                  $temp_img = trim( $a_smiley[1] );
 374                  if( $skin_has_smilies && is_file( $currentskin_path.$temp_img ) )
 375                  {
 376                      $temp_url = $currentskin_url.$temp_img;    // skin has it's own smiley, use it

 377                  }
 378                  elseif ( is_file( $default_path.$temp_img ) )
 379                  {
 380                      $temp_url = $default_url.$temp_img; // no skin image, but default smiley found so use it

 381                  }
 382                  else
 383                  {
 384                      $temp_url = ''; // no smiley image found, so don't add the smiley

 385                  }
 386  
 387                  if( $temp_url )
 388                      $this->smilies[] = array( 'code' => trim( $a_smiley[0] ),'image' => $temp_url );
 389              }
 390          }
 391      }
 392  
 393      // returns the relevant smiley parts (char_code, image_file)

 394  	function get_smiley( $smiley_parts )
 395      {
 396          return ( ( isset( $smiley_parts[1] ) && isset( $smiley_parts[2] ) ) ? $smiley_parts[1].'<->'.$smiley_parts[2] : '' );
 397      }
 398  }
 399  
 400  
 401  /*

 402   * $Log: _smilies.plugin.php,v $

 403   * Revision 1.43  2007/08/12 19:44:41  blueyed

 404   * Fixed :yes: smilie

 405   *

 406   * Revision 1.42  2007/06/18 21:21:57  fplanque

 407   * doc

 408   *

 409   * Revision 1.41  2007/06/16 20:26:44  blueyed

 410   * doc

 411   *

 412   * Revision 1.40  2007/04/26 00:11:04  fplanque

 413   * (c) 2007

 414   *

 415   * Revision 1.39  2007/04/20 01:42:32  fplanque

 416   * removed excess javascript

 417   *

 418   * Revision 1.38  2007/01/15 03:55:22  fplanque

 419   * lowered priority so there is no conflict with other replacements generating smileys.

 420   *

 421   * Revision 1.37  2006/12/28 23:20:40  fplanque

 422   * added plugin event for displaying comment form toolbars

 423   * used by smilies plugin

 424   *

 425   * Revision 1.31.2.10  2006/12/27 09:41:12  yabs

 426   * Removed b2evocanvas, minor other changes, clarified doc

 427   *

 428   * Revision 1.31.2.9  2006/12/26 03:18:51  fplanque

 429   * assigned a few significant plugin groups

 430   *

 431   * Revision 1.31.2.8  2006/12/16 04:30:59  fplanque

 432   * doc

 433   *

 434   * Revision 1.31.2.7  2006/12/03 08:27:35  yabs

 435   * Removed user setting - render_comments

 436   * Changed behaviour - render_comments_default used instead

 437   * Added skintag to display toolbar ifsmilies in comments enabled - usersetting overrides toolbar default

 438   * Other minor changes to code

 439   *

 440   * Revision 1.31.2.6  2006/11/27 19:12:50  fplanque

 441   * 1.9 POT has gone public. No more unnecessary changes.

 442   * 1.9.x language packs should work for ALL 1.9.x versions.

 443   * Adding strings is okay. Removing strings/changing strings for fun is not OK.

 444   * Move to 1.10 or 2.0

 445   *

 446   * Revision 1.34  2006/11/27 00:28:36  blueyed

 447   * trans fix

 448   *

 449   * Revision 1.33  2006/08/10 09:07:12  yabs

 450   * minor mods + added note re smilies folder

 451   *

 452   * Revision 1.32  2006/08/09 07:33:46  yabs

 453   * Redid the format of smiley settings, added ability to comment out a smiley and add optional comments to the end of a definition

 454   *

 455   * Revision 1.31  2006/08/07 18:26:21  fplanque

 456   * nuked obsolete smilies conf file

 457   *

 458   * Revision 1.30  2006/08/01 23:55:14  blueyed

 459   * minor: doc; removed level of indentation

 460   *

 461   * Revision 1.29  2006/08/01 08:44:31  yabs

 462   * Minor change - checks if skin has /smilies/ folder before start of checking for images

 463   *

 464   * Revision 1.28  2006/08/01 08:20:47  yabs

 465   * Added ability for admin skins to override default smiley images

 466   * Tidied up my previous code

 467   *

 468   * Revision 1.27  2006/07/31 16:19:04  yabs

 469   * Moved settings to admin

 470   * Added smilies in comments ( as user setting )

 471   * Added ability for blog skins to override default smiley images

 472   *

 473   * *note*

 474   * These changes are a tad quick 'n' dirty, I'm working on a cleaner version and will commit soon

 475   *

 476   * Revision 1.26  2006/07/12 21:13:17  blueyed

 477   * Javascript callback handler (e.g., for interaction of WYSIWYG editors with toolbar plugins)

 478   *

 479   * Revision 1.25  2006/07/10 20:19:30  blueyed

 480   * Fixed PluginInit behaviour. It now gets called on both installed and non-installed Plugins, but with the "is_installed" param appropriately set.

 481   *

 482   * Revision 1.24  2006/07/07 21:26:49  blueyed

 483   * Bumped to 1.9-dev

 484   *

 485   * Revision 1.23  2006/07/06 19:56:29  fplanque

 486   * no message

 487   *

 488   * Revision 1.22  2006/06/16 21:30:57  fplanque

 489   * Started clean numbering of plugin versions (feel free do add dots...)

 490   *

 491   * Revision 1.21  2006/05/30 20:26:59  blueyed

 492   * typo

 493   *

 494   * Revision 1.20  2006/05/30 19:39:55  fplanque

 495   * plugin cleanup

 496   *

 497   * Revision 1.19  2006/04/24 20:16:08  blueyed

 498   * Use callback_on_non_matching_blocks(); excluding PRE and CODE blocks

 499   *

 500   * Revision 1.18  2006/04/11 21:22:26  fplanque

 501   * partial cleanup

 502   *

 503   */
 504  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics