[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/plugins/serendipity_event_xhtmlcleanup/ -> serendipity_event_xhtmlcleanup.php (source)

   1  <?php # $Id: serendipity_event_xhtmlcleanup.php 966 2006-02-21 12:36:55Z garvinhicking $
   2  
   3  // Probe for a language include with constants. Still include defines later on, if some constants were missing
   4  $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
   5  if (file_exists($probelang)) {
   6      include $probelang;
   7  }
   8  
   9  include dirname(__FILE__) . '/lang_en.inc.php';
  10  
  11  if (!function_exists('html_entity_decode')) {
  12      function html_entity_decode($given_html, $quote_style = ENT_QUOTES) {
  13          $trans_table = get_html_translation_table(HTML_SPECIALCHARS, $quote_style);
  14          if ($trans_table["'"] != '&#039;') { # some versions of PHP match single quotes to &#39;
  15            $trans_table["'"] = '&#039;';
  16          }
  17  
  18          return (strtr($given_html, array_flip($trans_table)));
  19      }
  20  }
  21  
  22  class serendipity_event_xhtmlcleanup extends serendipity_event
  23  {
  24      var $title = PLUGIN_EVENT_XHTMLCLEANUP_NAME;
  25      var $cleanup_tag, $cleanup_checkfor, $cleanup_val, $cleanup_parse;
  26  
  27      function introspect(&$propbag)
  28      {
  29          global $serendipity;
  30  
  31          $propbag->add('name',          PLUGIN_EVENT_XHTMLCLEANUP_NAME);
  32          $propbag->add('description',   PLUGIN_EVENT_XHTMLCLEANUP_DESC);
  33          $propbag->add('stackable',     false);
  34          $propbag->add('author',        'Garvin Hicking');
  35          $propbag->add('version',       '1.5');
  36          $propbag->add('requirements',  array(
  37              'serendipity' => '0.8',
  38              'smarty'      => '2.6.7',
  39              'php'         => '4.1.0'
  40          ));
  41          $propbag->add('groups', array('BACKEND_TEMPLATES'));
  42          $propbag->add('cachable_events', array('frontend_display' => true));
  43          $propbag->add('event_hooks',   array(
  44              'frontend_display' => true,
  45              'frontend_display:html:per_entry' => true,
  46              'backend_view_comment' => true
  47          ));
  48  
  49          $this->markup_elements = array(
  50              array(
  51                'name'     => 'ENTRY_BODY',
  52                'element'  => 'body',
  53              ),
  54              array(
  55                'name'     => 'EXTENDED_BODY',
  56                'element'  => 'extended',
  57              ),
  58              array(
  59                'name'     => 'COMMENT',
  60                'element'  => 'comment',
  61              ),
  62              array(
  63                'name'     => 'HTML_NUGGET',
  64                'element'  => 'html_nugget',
  65              )
  66          );
  67  
  68          $conf_array = array();
  69          foreach($this->markup_elements as $element) {
  70              $conf_array[] = $element['name'];
  71          }
  72          $conf_array[] = 'xhtml_parse';
  73          $conf_array[] = 'utf8_parse';
  74          $propbag->add('configuration', $conf_array);
  75      }
  76  
  77      function install() {
  78          serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
  79      }
  80  
  81      function uninstall() {
  82          serendipity_plugin_api::hook_event('backend_cache_purge', $this->title);
  83          serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
  84      }
  85  
  86      function generate_content(&$title) {
  87          $title = $this->title;
  88      }
  89  
  90      function introspect_config_item($name, &$propbag)
  91      {
  92          if ($name == 'utf8_parse') {
  93              $propbag->add('type',        'boolean');
  94              $propbag->add('name',        PLUGIN_EVENT_XHTMLCLEANUP_UTF8);
  95              $propbag->add('description', PLUGIN_EVENT_XHTMLCLEANUP_UTF8_DESC);
  96              $propbag->add('default',     'true');
  97          } elseif ($name == 'xhtml_parse') {
  98              $propbag->add('type',        'boolean');
  99              $propbag->add('name',        PLUGIN_EVENT_XHTMLCLEANUP_XHTML);
 100              $propbag->add('description', PLUGIN_EVENT_XHTMLCLEANUP_XHTML_DESC);
 101              $propbag->add('default',     'true');
 102          } else {
 103              $propbag->add('type',        'boolean');
 104              $propbag->add('name',        constant($name));
 105              $propbag->add('description', sprintf(APPLY_MARKUP_TO, constant($name)));
 106              $propbag->add('default',     'true');
 107          }
 108  
 109          return true;
 110      }
 111  
 112      function fixUTFEntity(&$string) {
 113          $string = preg_replace('/&amp;#(x[a-f0-9]{1,4}|[0-9]{1,5});/', '&#$1;', $string);
 114          return true;
 115      }
 116  
 117      function event_hook($event, &$bag, &$eventData, $addData = null) {
 118          global $serendipity;
 119          static $convert_fields = array(
 120              'fullBody',
 121              'summary',
 122              'title',
 123              'author',
 124          );
 125          $hooks = &$bag->get('event_hooks');
 126  
 127          if (isset($hooks[$event])) {
 128              switch($event) {
 129                  case 'backend_view_comment':
 130                      if (serendipity_db_bool($this->get_config('utf8_parse'))) {
 131                          foreach($convert_fields AS $convert_field) {
 132                              $this->fixUTFEntity($eventData[$convert_field]);
 133                          }
 134                      }
 135                      return true;
 136                      break;
 137  
 138                  case 'frontend_display':
 139                      $this->cleanup_parse = serendipity_db_bool($this->get_config('xhtml_parse'));
 140                      foreach ($this->markup_elements as $temp) {
 141                          if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']]) &&
 142                              !$eventData['properties']['ep_disable_markup_' . $this->instance] &&
 143                              !isset($serendipity['POST']['properties']['disable_markup_' . $this->instance])) {
 144                              $element = $temp['element'];
 145                              $this->cleanup_tag      = 'IMG';
 146                              $this->cleanup_checkfor = 'ALT';
 147                              $this->cleanup_val      = '';
 148                              // Basic cleanup (core s9y functionality)
 149                              $eventData[$element]    = xhtml_cleanup($eventData[$element]);
 150                              $eventData[$element]    = preg_replace_callback('@(<img.+/?>)@imsU', array($this, 'clean_tag'), $eventData[$element]);
 151                              $eventData[$element]    = preg_replace_callback("@<(a|iframe)(.*)(href|src)=(\"|')([^\"']+)(\"|')@isUm", array($this, 'clean_htmlspecialchars'), $eventData[$element]);
 152                          }
 153                      }
 154  
 155                      if (serendipity_db_bool($this->get_config('utf8_parse'))) {
 156                          $this->fixUTFEntity($eventData['author']);
 157                          $this->fixUTFEntity($eventData['comment']);
 158                      }
 159  
 160                      return true;
 161                      break;
 162  
 163                  case 'frontend_display:html:per_entry':
 164                      if (serendipity_db_bool($this->get_config('utf8_parse'))) {
 165                          $this->fixUTFEntity($eventData['author']);
 166                          $this->fixUTFEntity($eventData['title']);
 167                      }
 168  
 169                      return true;
 170                      break;
 171  
 172                  default:
 173                      return false;
 174              }
 175  
 176          } else {
 177              return false;
 178          }
 179      }
 180  
 181      // Takes an input tag and search for ommitted attributes. Expects a single tag (array, index 0)
 182      function clean_tag($data) {
 183          // Restore tags from preg_replace_callback buffer, as those can't be passed in the function header
 184          $tag      = &$this->cleanup_tag;
 185          $checkfor = &$this->cleanup_checkfor;
 186          $val      = &$this->cleanup_val;
 187  
 188          // Instead of nasty regex-mangling we use the XML parser to get the attribute list of our input tag
 189          switch(strtolower(LANG_CHARSET)) {
 190              case 'iso-8859-1':
 191              case 'utf-8':
 192                  $p = xml_parser_create(LANG_CHARSET);
 193                  break;
 194              
 195              default:
 196                  $p = xml_parser_create('');
 197          }
 198  
 199          @xml_parse_into_struct($p, $data[0], $vals, $index);
 200          xml_parser_free($p);
 201  
 202          // Check if the xml parser returned anything useful
 203          if (is_array($vals) && isset($vals[0]) && $vals[0]['tag'] == $tag) {
 204  
 205              if (!empty($vals[0]['attributes'][$checkfor])) {
 206                  // The attribute we search for already exists. Return original string.
 207                  return $data[0];
 208              }
 209  
 210              // Assign the value we submitted for the attribute to insert
 211              $vals[0]['attributes'][$checkfor] = $val;
 212  
 213              // Reconstruct XHTML tag.
 214              $atts = ' ';
 215              foreach($vals[0]['attributes'] AS $att => $att_con) {
 216                  $atts .= strtolower($att) . '="' . ($this->cleanup_parse ? htmlspecialchars($att_con) : $att_con) . '" ';
 217              }
 218  
 219              return '<' . strtolower($tag) . $atts . ' />';
 220          }
 221  
 222          return $data[0];
 223      }
 224  
 225      function clean_htmlspecialchars($given, $quote_style = ENT_QUOTES) {
 226          return '<' . $given[1] . $given[2] . $given[3] . '=' . $given[4] . htmlspecialchars(html_entity_decode($given[5], $quote_style), $quote_style) . $given[6];
 227      }
 228  }
 229  
 230  /* vim: set sts=4 ts=4 expandtab : */


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics