[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/filter/emoticon/ -> filter.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Filter converting emoticon texts into images
  20   *
  21   * This filter uses the emoticon settings in Site admin > Appearance > HTML settings
  22   * and replaces emoticon texts with images.
  23   *
  24   * @package    filter
  25   * @subpackage emoticon
  26   * @see        emoticon_manager
  27   * @copyright  2010 David Mudrak <david@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  class filter_emoticon extends moodle_text_filter {
  34  
  35      /**
  36       * Apply the filter to the text
  37       *
  38       * @see filter_manager::apply_filter_chain()
  39       * @param string $text to be processed by the text
  40       * @param array $options filter options
  41       * @return string text after processing
  42       */
  43      public function filter($text, array $options = array()) {
  44  
  45          if (!isset($options['originalformat'])) {
  46              // if the format is not specified, we are probably called by {@see format_string()}
  47              // in that case, it would be dangerous to replace text with the image because it could
  48              // be stripped. therefore, we do nothing
  49              return $text;
  50          }
  51          if (in_array($options['originalformat'], explode(',', get_config('filter_emoticon', 'formats')))) {
  52              $this->replace_emoticons($text);
  53          }
  54          return $text;
  55      }
  56  
  57      ////////////////////////////////////////////////////////////////////////////
  58      // internal implementation starts here
  59      ////////////////////////////////////////////////////////////////////////////
  60  
  61      /**
  62       * Replace emoticons found in the text with their images
  63       *
  64       * @param string $text to modify
  65       * @return void
  66       */
  67      protected function replace_emoticons(&$text) {
  68          global $CFG, $OUTPUT, $PAGE;
  69          static $emoticontexts = array();    // internal cache used for replacing
  70          static $emoticonimgs = array();     // internal cache used for replacing
  71  
  72          $lang = current_language();
  73          $theme = $PAGE->theme->name;
  74  
  75          if (!isset($emoticontexts[$lang][$theme]) or !isset($emoticonimgs[$lang][$theme])) {
  76              // prepare internal caches
  77              $manager = get_emoticon_manager();
  78              $emoticons = $manager->get_emoticons();
  79              $emoticontexts[$lang][$theme] = array();
  80              $emoticonimgs[$lang][$theme] = array();
  81              foreach ($emoticons as $emoticon) {
  82                  $emoticontexts[$lang][$theme][] = $emoticon->text;
  83                  $emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
  84              }
  85              unset($emoticons);
  86          }
  87  
  88          if (empty($emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here
  89              return;
  90          }
  91  
  92          // detect all the <script> zones to take out
  93          $excludes = array();
  94          preg_match_all('/<script language(.+?)<\/script>/is', $text, $listofexcludes);
  95  
  96          // take out all the <script> zones from text
  97          foreach (array_unique($listofexcludes[0]) as $key => $value) {
  98              $excludes['<+'.$key.'+>'] = $value;
  99          }
 100          if ($excludes) {
 101              $text = str_replace($excludes, array_keys($excludes), $text);
 102          }
 103  
 104          // this is the meat of the code - this is run every time
 105          $text = str_replace($emoticontexts[$lang][$theme], $emoticonimgs[$lang][$theme], $text);
 106  
 107          // Recover all the <script> zones to text
 108          if ($excludes) {
 109              $text = str_replace(array_keys($excludes), $excludes, $text);
 110          }
 111      }
 112  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1