b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (Version 1.9) [ class tree: plugins ] [ index: plugins ] [ all elements ]

Source for file _smilies.plugin.php

Documentation is available at _smilies.plugin.php

  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-2006 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.     var $priority = 80;
  25.     var $version = '1.9.2';
  26.     var $apply_rendering = 'opt-out';
  27.     var $group = 'rendering';
  28.  
  29.     /**
  30.      * Text similes search array
  31.      *
  32.      * @access private
  33.      */
  34.     var $search;
  35.  
  36.     /**
  37.      * IMG replace array
  38.      *
  39.      * @access private
  40.      */
  41.     var $replace;
  42.  
  43.     /**
  44.      * Smiley definitions
  45.      *
  46.      * @access private
  47.      */
  48.     var $smilies;
  49.  
  50.     /**
  51.      * Init
  52.      */
  53.     function PluginInit$params )
  54.     {
  55.         $this->short_desc = T_('Graphical smileys');
  56.         $this->long_desc = T_('This renderer will convert text smilies like :) to graphical icons.<br />
  57.             Optionally, it will also display a toolbar for quick insertion of smilies into a post.');
  58.     }
  59.  
  60.  
  61.     /**
  62.     * Defaults for user specific settings: "Display toolbar"
  63.      *
  64.      * @return array 
  65.      */
  66.     function GetDefaultSettings()
  67.     {
  68.         global $rsc_subdir;
  69.         return array(
  70.                 'use_toolbar_default' => array(
  71.                     'label' => T_'Use smilies toolbar' ),
  72.                     'defaultvalue' => '1',
  73.                     'type' => 'checkbox',
  74.                     'note' => T_'This is the default setting. Users can override it in their profile.' ),
  75.                 ),
  76.                 'render_comments' => array(    // fp> Note: this is not a default in this version, it's an 'always' :]
  77.                     'label' => $this->T_('Render comments' ),
  78.                     'note' => $this->T_('Check to also render smilies in comments.'),
  79.                     'defaultvalue' => '0',
  80.                     'type' => 'checkbox',
  81.                 ),
  82.                 // TODO (yabs) : Display these as images and individual inputs
  83.                 'smiley_list' => array(
  84.                     'label' => $this->T_'Smiley list'),
  85.                     'note' => sprintf$this->T_'This is the list of smileys [one per line], in the format : char_sequence image_file // optional comment<br />
  86.                             To disable a smiley, just add one or more spaces to the start of its setting<br />
  87.                             You can add new smiley images by uploading the images to the %s folder.' )'<span style="font-weight:bold">'.$rsc_subdir.'smilies/</span>' ),
  88.                     'type' => 'html_textarea'// allows smilies with "<" in them
  89.                     'rows' => 10,
  90.                     'cols' => 60,
  91.                     'defaultvalue' => '
  92. =>       icon_arrow.gif
  93. :!:      icon_exclaim.gif
  94. :?:      icon_question.gif
  95. :idea:   icon_idea.gif
  96. :)       icon_smile.gif
  97. :D       icon_biggrin.gif
  98. :p       icon_razz.gif
  99. B)       icon_cool.gif
  100. ;)       icon_wink.gif
  101. :>       icon_twisted.gif
  102. :roll:   icon_rolleyes.gif
  103. :oops:   icon_redface.gif
  104. :|       icon_neutral.gif
  105. :-/      icon_confused.gif
  106. :(       icon_sad.gif
  107. >:(      icon_mad.gif
  108. :\'(      icon_cry.gif
  109. |-|      icon_wth.gif
  110. :>>      icon_mrgreen.gif
  111. :yes:    grayyes.gif,
  112. ;D       graysmilewinkgrin.gif
  113. :P       graybigrazz.gif
  114. :))      graylaugh.gif
  115. 88|      graybigeek.gif
  116. :.       grayshy.gif
  117. :no:     grayno.gif
  118. XX(      graydead.gif
  119. :lalala: icon_lalala.gif
  120. :crazy:  icon_crazy.gif
  121. >:XX     icon_censored.gif
  122.  :DD     icon_lol.gif
  123.  :o      icon_surprised.gif
  124.  8|      icon_eek.gif
  125.  >:-[    icon_evil.gif
  126.  :)      graysmile.gif
  127.  :b      grayrazz.gif
  128.  )-o     grayembarrassed.gif
  129.  U-(     grayuhoh.gif
  130.  :(      graysad.gif
  131.  :**:    graysigh.gif     // alternative: graysighw.gif
  132.  :??:    grayconfused.gif // alternative: grayconfusedw.gif
  133.  :`(     graycry.gif
  134.  >:-(    graymad.gif
  135.  :##      grayupset.gif   // alternative: grayupsetw.gif
  136.  :zz:    graysleep.gif    // alternative: graysleepw.gif
  137.  :wave:  icon_wave.gif',
  138.                 ),
  139.             );
  140. }
  141.  
  142.  
  143.     /**
  144.      * Allowing the user to override the display of the toolbar.
  145.      *
  146.      * @return array 
  147.      */
  148.     function GetDefaultUserSettings()
  149.     {
  150.         return array(
  151.                 'use_toolbar' => array(
  152.                     'label' => T_'Use smilies toolbar' ),
  153.                     'defaultvalue' => $this->Settings->get('use_toolbar_default'),
  154.                     'type' => 'checkbox',
  155.                 ),
  156.             );
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Display a toolbar in admin
  162.      *
  163.      * @param array Associative array of parameters
  164.      * @return boolean did we display a toolbar?
  165.      */
  166.     function AdminDisplayToolbar$params )
  167.     {
  168.         if$this->UserSettings->get('use_toolbar') )
  169.         {
  170.             return $this->display_smiley_bar();
  171.         }
  172.         return false;
  173.     }
  174.  
  175.  
  176.     /**
  177.      * Event handler: Called when displaying editor toolbars.
  178.      *
  179.      * @param array Associative array of parameters
  180.      * @return boolean did we display a toolbar?
  181.      */
  182.     function DisplayCommentToolbar$params )
  183.     {
  184.         if$this->Settings->get'render_comments' )
  185.         && ( ( is_logged_in(&& $this->UserSettings->get'use_toolbar' ) )
  186.             || !is_logged_in(&& $this->Settings->get'use_toolbar_default' ) ) ) )
  187.         {    
  188.             return $this->display_smiley_bar();
  189.         }
  190.         return false;
  191.     }
  192.  
  193.  
  194.     /**
  195.      * Display the smiley toolbar
  196.      *
  197.      * @return boolean did we display a toolbar?
  198.      */
  199.     function display_smiley_bar()
  200.     {
  201.         $this->InitSmilies();    // check smilies cached
  202.  
  203.         $grins '';
  204.         $smiled array();
  205.         foreach$this->smilies as $smiley )
  206.         {
  207.             if (!in_array($smiley'image' ]$smiled))
  208.             {
  209.                 $smiled[$smiley'image'];
  210.                 $smiley'code' str_replace(' '''$smiley'code' ]);
  211.                 $grins .= '<img src="'.$smiley'image' ].'" title="'.$smiley'code' ].'" alt="'.$smiley'code' ]
  212.                                     .'" class="top" onclick="grin(\''str_replace("'","\'",$smiley'code' ])'\');" /> ';
  213.             }
  214.         }
  215.  
  216.         print('<div class="edit_toolbar">'$grins'</div>');
  217.         ob_start();
  218.         ?>
  219.         <script type="text/javascript">
  220.         function grin(tag)
  221.         {
  222.             if( typeof b2evo_Callbacks == 'object' )
  223.             { // see if there's a callback registered that should handle this:
  224.                 if( b2evo_Callbacks.trigger_callback("insert_raw_into_"+b2evoCanvas.id, tag) )
  225.                 {
  226.                     return;
  227.                 }
  228.             }
  229.  
  230.             var myField = b2evoCanvas;
  231.  
  232.             if (document.selection) {
  233.                 myField.focus();
  234.                 sel = document.selection.createRange();
  235.                 sel.text = tag;
  236.                 myField.focus();
  237.             }
  238.             else if (myField.selectionStart || myField.selectionStart == '0') {
  239.                 var startPos = myField.selectionStart;
  240.                 var endPos = myField.selectionEnd;
  241.                 var cursorPos = endPos;
  242.                 myField.value = myField.value.substring(0, startPos)
  243.                                 + tag
  244.                                 + myField.value.substring(endPos, myField.value.length);
  245.                 cursorPos += tag.length;
  246.                 myField.focus();
  247.                 myField.selectionStart = cursorPos;
  248.                 myField.selectionEnd = cursorPos;
  249.             }
  250.             else {
  251.                 myField.value += tag;
  252.                 myField.focus();
  253.             }
  254.         }
  255.  
  256.         </script>
  257.         <?php
  258.         $grins ob_get_contents();
  259.         ob_end_clean();
  260.         print($grins);
  261.  
  262.         return true;
  263.     }
  264.  
  265.  
  266.     /**
  267.      * Perform rendering
  268.      *
  269.      * @see Plugin::FilterCommentContent()
  270.      */
  271.     function FilterCommentContent$params )
  272.     {
  273.         if$this->Settings->get'render_comments' ) )
  274.         {
  275.             $this->RenderItemAsHtml$params );
  276.         }
  277.     }    
  278.     
  279.  
  280.  
  281.     /**
  282.      * Perform rendering
  283.      *
  284.      * @see Plugin::RenderItemAsHtml()
  285.      */
  286.     function RenderItemAsHtml$params )
  287.     {
  288.         $this->InitSmilies();    // check smilies are already cached
  289.  
  290.  
  291.         ifisset$this->search ) )
  292.         {    // We haven't prepared the smilies yet
  293.             $this->search array();
  294.  
  295.             $tmpsmilies $this->smilies;
  296.             usort($tmpsmiliesarray(&$this'smiliescmp'));
  297.  
  298.             foreach$tmpsmilies as $smiley )
  299.             {
  300.                 $this->search[$smiley'code' ];
  301.                 $smiley_masked '';
  302.                 for ($i 0$i strlen($smiley'code' )$i++ )
  303.                 {
  304.                     $smiley_masked .=  '&#'.ord(substr($smiley'code' ]$i1)).';';
  305.                 }
  306.  
  307.                 // We don't use getimagesize() here until we have a mean
  308.                 // to preprocess smilies. It takes up to much time when
  309.                 // processing them at display time.
  310.                 $this->replace['<img src="'.$smiley'image' ].'" alt="'.$smiley_masked.'" class="middle" />';
  311.             }
  312.         }
  313.  
  314.  
  315.         // REPLACE:  But only in non-HTML blocks, totally excluding <CODE>..</CODE> and <PRE>..</PRE>
  316.  
  317.         $content $params['data'];
  318.  
  319.         // Lazy-check first, using stristr() (stripos() is only available since PHP5):
  320.         ifstristr$content'<code' !== false || stristr$content'<pre' !== false )
  321.         // Call ReplaceTagSafe() on everything outside <pre></pre> and <code></code>:
  322.             $content callback_on_non_matching_blocks$content,
  323.                     '~<(code|pre)[^>]*>.*?</\1>~is',
  324.                     array$this'ReplaceTagSafe' ) );
  325.         }
  326.         else
  327.         // No CODE or PRE blocks, replace on the whole thing
  328.             $content $this->ReplaceTagSafe($content);
  329.         }
  330.  
  331.         return true;
  332.     }
  333.  
  334.  
  335.     /**
  336.      * This callback gets called once after every tags+text chunk
  337.      * @return string Text with replaced smilies
  338.      */
  339.     function preg_insert_smilies_callback$text )
  340.     {
  341.         return str_replace$this->search$this->replace$text );
  342.     }
  343.  
  344.  
  345.     /**
  346.      * Replace smilies in non-HTML-tag portions of the text.
  347.      * @uses callback_on_non_matching_blocks()
  348.      */
  349.     function ReplaceTagSafe($text)
  350.     {
  351.         return callback_on_non_matching_blocks$text'~<[^>]*>~'array(&$this'preg_insert_smilies_callback') );
  352.     }
  353.  
  354.  
  355.     /**
  356.      * sorts the smilies' array by length
  357.      * this is important if you want :)) to superseede :) for example
  358.      */
  359.     function smiliescmp($a$b)
  360.     {
  361.         if( ($diff strlen$b'code' strlen$a'code' ) ) == 0)
  362.         {
  363.             return strcmp$a'code' ]$b'code' );
  364.         }
  365.         return $diff;
  366.     }
  367.  
  368.  
  369.     /**
  370.      * Initiates the smiley array if not already initiated
  371.      *
  372.      * Attempts to use skin specific smileys where available
  373.      *    - skins_adm/skin/rsc/smilies/
  374.      *    - skins/skin/smilies/
  375.      *
  376.      * Attempts to fallback to default smilies
  377.      *    - rsc/smilies/
  378.      *
  379.      * If no image file found the smiley is not added
  380.      *
  381.      * @return array of available smilies( code, image url )
  382.      */
  383.     function InitSmilies()
  384.     {
  385.         ifisset$this->smilies ) )
  386.         // smilies are already cached
  387.             return;
  388.         }
  389.  
  390.         global $admin_skin$adminskins_path$adminskins_url$rsc_path$rsc_url$skin$skins_path$skins_url;
  391.  
  392.         // set the skin path/url and the default (rsc) path/url
  393.         $currentskin_path is_admin_page($adminskins_path.$admin_skin.'/rsc' $skins_path.$skin ).'/smilies/';
  394.         $currentskin_url is_admin_page($adminskins_url.$admin_skin.'/rsc' $skins_url.$skin ).'/smilies/';
  395.         $default_path $rsc_path.'smilies/';
  396.         $default_url $rsc_url.'smilies/';
  397.  
  398.         $skin_has_smilies is_dir$currentskin_path );    // check if skin has a /smilies/ folder
  399.  
  400.         $this->smilies array();
  401.         $temp_list explode"\n"str_replacearray"\r""\t" )''$this->Settings->get'smiley_list' ) ) );
  402.  
  403.         foreach$temp_list as $temp_smiley )
  404.         {
  405.             $a_smiley explode'<->',    preg_replace_callback'#^(\S.+?\s)(.+?)(\/\/.*?)*$#'array$this'get_smiley' ),$temp_smiley ) );
  406.             ifisset$a_smiley[0and isset$a_smiley[1) )
  407.             {
  408.                 // lets see if the file exists
  409.                 $temp_img trim$a_smiley[1);
  410.                 if$skin_has_smilies && is_file$currentskin_path.$temp_img ) )
  411.                 {
  412.                     $temp_url $currentskin_url.$temp_img;    // skin has it's own smiley, use it
  413.                 }
  414.                 elseif is_file$default_path.$temp_img ) )
  415.                 {
  416.                     $temp_url $default_url.$temp_img// no skin image, but default smiley found so use it
  417.                 }
  418.                 else
  419.                 {
  420.                     $temp_url ''// no smiley image found, so don't add the smiley
  421.                 }
  422.  
  423.                 if$temp_url )
  424.                     $this->smilies[array'code' => trim$a_smiley[0),'image' => $temp_url );
  425.             }
  426.         }
  427.     }
  428.  
  429.     // returns the relevant smiley parts (char_code, image_file)
  430.     function get_smiley$smiley_parts )
  431.     {
  432.         return ( ( isset$smiley_parts[1&& isset$smiley_parts[2) ) $smiley_parts[1].'<->'.$smiley_parts[2'' );
  433.     }
  434. }
  435.  
  436.  
  437. /*
  438.  * $Log: _smilies.plugin.php,v $
  439.  * Revision 1.31.2.11  2006/12/28 23:18:54  fplanque
  440.  * added plugin event for displaying comment form toolbars
  441.  * used by smilies plugin
  442.  *
  443.  * Revision 1.31.2.10  2006/12/27 09:41:12  yabs
  444.  * Removed b2evocanvas, minor other changes, clarified doc
  445.  *
  446.  * Revision 1.31.2.9  2006/12/26 03:18:51  fplanque
  447.  * assigned a few significant plugin groups
  448.  *
  449.  * Revision 1.31.2.8  2006/12/16 04:30:59  fplanque
  450.  * doc
  451.  *
  452.  * Revision 1.31.2.7  2006/12/03 08:27:35  yabs
  453.  * Removed user setting - render_comments
  454.  * Changed behaviour - render_comments_default used instead
  455.  * Added skintag to display toolbar ifsmilies in comments enabled - usersetting overrides toolbar default
  456.  * Other minor changes to code
  457.  *
  458.  * Revision 1.31.2.6  2006/11/27 19:12:50  fplanque
  459.  * 1.9 POT has gone public. No more unnecessary changes.
  460.  * 1.9.x language packs should work for ALL 1.9.x versions.
  461.  * Adding strings is okay. Removing strings/changing strings for fun is not OK.
  462.  * Move to 1.10 or 2.0
  463.  *
  464.  * Revision 1.31.2.5  2006/11/27 00:32:56  blueyed
  465.  * MFH: trans fix
  466.  *
  467.  * Revision 1.31.2.4  2006/11/27 00:28:50  blueyed
  468.  * MFH: trans fix
  469.  *
  470.  * Revision 1.31.2.3  2006/11/04 19:55:12  fplanque
  471.  * Reinjected old Log blocks. Removing them from CVS was a bad idea -- especially since Daniel has decided branch 1.9 was his HEAD...
  472.  *
  473.  * Revision 1.31  2006/08/07 18:26:21  fplanque
  474.  * nuked obsolete smilies conf file
  475.  *
  476.  * Revision 1.30  2006/08/01 23:55:14  blueyed
  477.  * minor: doc; removed level of indentation
  478.  *
  479.  * Revision 1.29  2006/08/01 08:44:31  yabs
  480.  * Minor change - checks if skin has /smilies/ folder before start of checking for images
  481.  *
  482.  * Revision 1.28  2006/08/01 08:20:47  yabs
  483.  * Added ability for admin skins to override default smiley images
  484.  * Tidied up my previous code
  485.  *
  486.  * Revision 1.27  2006/07/31 16:19:04  yabs
  487.  * Moved settings to admin
  488.  * Added smilies in comments ( as user setting )
  489.  * Added ability for blog skins to override default smiley images
  490.  *
  491.  * *note*
  492.  * These changes are a tad quick 'n' dirty, I'm working on a cleaner version and will commit soon
  493.  *
  494.  * Revision 1.26  2006/07/12 21:13:17  blueyed
  495.  * Javascript callback handler (e.g., for interaction of WYSIWYG editors with toolbar plugins)
  496.  *
  497.  * Revision 1.25  2006/07/10 20:19:30  blueyed
  498.  * Fixed PluginInit behaviour. It now gets called on both installed and non-installed Plugins, but with the "is_installed" param appropriately set.
  499.  *
  500.  * Revision 1.24  2006/07/07 21:26:49  blueyed
  501.  * Bumped to 1.9-dev
  502.  *
  503.  * Revision 1.23  2006/07/06 19:56:29  fplanque
  504.  * no message
  505.  *
  506.  * Revision 1.22  2006/06/16 21:30:57  fplanque
  507.  * Started clean numbering of plugin versions (feel free do add dots...)
  508.  *
  509.  * Revision 1.21  2006/05/30 20:26:59  blueyed
  510.  * typo
  511.  *
  512.  * Revision 1.20  2006/05/30 19:39:55  fplanque
  513.  * plugin cleanup
  514.  *
  515.  * Revision 1.19  2006/04/24 20:16:08  blueyed
  516.  * Use callback_on_non_matching_blocks(); excluding PRE and CODE blocks
  517.  *
  518.  * Revision 1.18  2006/04/11 21:22:26  fplanque
  519.  * partial cleanup
  520.  *
  521.  */
  522. ?>

Documentation generated on Tue, 18 Dec 2007 22:52:37 +0100 by phpDocumentor 1.4.0