[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

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

   1  <?php # $Id: serendipity_event_bbcode.php 1651 2007-03-20 09:00:40Z garvinhicking $
   2  
   3  
   4  if (IN_serendipity !== true) {
   5      die ("Don't hack!");
   6  }
   7  
   8  // Probe for a language include with constants. Still include defines later on, if some constants were missing
   9  $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
  10  if (file_exists($probelang)) {
  11      include $probelang;
  12  }
  13  
  14  include dirname(__FILE__) . '/lang_en.inc.php';
  15  
  16  class serendipity_event_bbcode extends serendipity_event
  17  {
  18      var $title = PLUGIN_EVENT_BBCODE_NAME;
  19      function introspect(&$propbag)
  20      {
  21          global $serendipity;
  22  
  23          $propbag->add('name',          PLUGIN_EVENT_BBCODE_NAME);
  24          $propbag->add('description',   PLUGIN_EVENT_BBCODE_DESC);
  25          $propbag->add('stackable',     false);
  26          $propbag->add('author',        'Jez Hancock, Garvin Hicking');
  27          $propbag->add('version',       '2.09');
  28          $propbag->add('requirements',  array(
  29              'serendipity' => '0.8',
  30              'smarty'      => '2.6.7',
  31              'php'         => '4.1.0'
  32          ));
  33          $propbag->add('cachable_events', array('frontend_display' => true));
  34          $propbag->add('event_hooks',   array('frontend_display' => true, 'frontend_comment' => true, 'css' => true));
  35          $propbag->add('groups', array('MARKUP'));
  36  
  37          $this->markup_elements = array(
  38              array(
  39                'name'     => 'ENTRY_BODY',
  40                'element'  => 'body',
  41              ),
  42              array(
  43                'name'     => 'EXTENDED_BODY',
  44                'element'  => 'extended',
  45              ),
  46              array(
  47                'name'     => 'COMMENT',
  48                'element'  => 'comment',
  49              ),
  50              array(
  51                'name'     => 'HTML_NUGGET',
  52                'element'  => 'html_nugget',
  53              )
  54          );
  55  
  56          $conf_array = array();
  57          $conf_array[] = 'info';
  58          $conf_array[] = 'target';
  59          foreach($this->markup_elements as $element) {
  60              $conf_array[] = $element['name'];
  61          }
  62          $propbag->add('configuration', $conf_array);
  63      }
  64  
  65  
  66      function bbcode_callback($matches) {
  67          $type  = $matches[1];
  68          $input = trim($matches[2], "\r\n");
  69  
  70          switch ($type) {
  71              case 'code':
  72                  $search_replace = array(
  73                      '&'         => '&amp;',
  74                      ' '         => '&#160;',
  75                      '&lt;'      => '&#60;',
  76                      '<'         => '&#60;',
  77                      '&gt;'      => '&#62;',
  78                      '>'         => '&#62;',
  79                      '&quot;'    => '&#34;',
  80                      ':'         => '&#58;',
  81                      '['         => '&#91;',
  82                      ']'         => '&#93;',
  83                      ')'         => '&#41;',
  84                      '('         => '&#40;',
  85                      '*'         => '&#42;',
  86                      '\t'        => '&#160;&#160;&#160;&#160;',
  87                      '\\"'       => '"',
  88                      "\\'"       => "'"
  89                  );
  90  
  91                  $input = strtr($input, $search_replace);
  92                  break;
  93  
  94              case 'php':
  95                  if (substr($input, 0, 2) != '<?') {
  96                      $input = "<?php\n\n$input\n\n?>";
  97                  }
  98  
  99                  ob_start();
 100                  highlight_string($input);
 101                  $input = ob_get_contents();
 102                  ob_end_clean();
 103  
 104                  $input = str_replace('<br />', "\n", $input);
 105                  break;
 106  
 107              default:
 108                  return false;
 109          }
 110  
 111          $input = "<div class=\"bb-$type-title\">" . strtoupper($type) . ":</div>"
 112                  . "<div class=\"bb-$type\">$input</div>";
 113          return($input);
 114      }
 115  
 116      function generate_content(&$title) {
 117          $title = $this->title;
 118      }
 119  
 120  
 121      function introspect_config_item($name, &$propbag)
 122      {
 123          switch($name) {
 124              case 'target':
 125                  $propbag->add('type', 'boolean');
 126                  $propbag->add('name', PLUGIN_EVENT_BBCODE_TARGET);
 127                  $propbag->add('default', 'false');
 128                  break;
 129  
 130              case 'info':
 131                  $propbag->add('type',        'info');
 132                  $propbag->add('description',  PLUGIN_EVENT_BBCODE_TRANSFORM);
 133                  break;
 134  
 135              default:
 136                  $propbag->add('type',        'boolean');
 137                  $propbag->add('name',        constant($name));
 138                  $propbag->add('description', sprintf(APPLY_MARKUP_TO, constant($name)));
 139                  $propbag->add('default', 'true');
 140                  break;
 141          }
 142          return true;
 143      }
 144  
 145      function install() {
 146          serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
 147      }
 148  
 149      function uninstall() {
 150          serendipity_plugin_api::hook_event('backend_cache_purge', $this->title);
 151          serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
 152      }
 153  
 154      function bbcode($input) {
 155          static $bbcodes = null;
 156  
 157          // Only allow numbers and characters for CSS: "red", "#FF0000", ...
 158          static $pattern_css   = '([ 0-9a-z#-]+?)';
 159  
 160          // Only allow strings occuring in emails: .-_@, 0-9, a-z
 161          static $pattern_mail  = '([\.\-\+~@_0-9a-z]+?)';
 162  
 163          // Only allow strings occuring in URLs: &;?:.-_@/, 0-9, a-z
 164          static $pattern_url   = '([@!=~\?:&;0-9a-z#\.\-_\/,%\+]+?)';
 165  
 166          // Disallow possibly evil HTML characters which may lead to Javascript XSS: '"();
 167          static $pattern_query = '([^"\'\(\);]+?)';
 168  
 169          static $target = null;
 170  
 171          if ($target === null) {
 172              $target = serendipity_db_bool($this->get_config('target'));
 173          }
 174  
 175          // Note:
 176          //  * Anything between <xxx>...</xxx> tags will be caught by htmlspecialchars() and disallows custom HTML tags.
 177          //  * (?::\w+)? means "non capturing" match on any word character.
 178          //  * (?<!\\\\) means any bbcode which is not prefixed by \[...]
 179  
 180          if ($bbcodes === null) {
 181              $bbcodes = array(
 182                '/(?<!\\\\)\[color(?::\w+)?=' . $pattern_css . '\](.*?)\[\/color(?::\w+)?\]/si'                 => "<span style=\"color:\\1\">\\2</span>",
 183                '/(?<!\\\\)\[size(?::\w+)?='  . $pattern_css . '\](.*?)\[\/size(?::\w+)?\]/si'                  => "<span style=\"font-size:\\1\">\\2</span>",
 184                '/(?<!\\\\)\[font(?::\w+)?='  . $pattern_css . '\](.*?)\[\/font(?::\w+)?\]/si'                  => "<span style=\"font-family:\\1\">\\2</span>",
 185                '/(?<!\\\\)\[align(?::\w+)?=' . $pattern_css . '\](.*?)\[\/align(?::\w+)?\]/si'                 => "<div style=\"text-align:\\1\">\\2</div>",
 186  
 187                '/(?<!\\\\)\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'                                              => "<span style=\"font-weight:bold\">\\1</span>",
 188                '/(?<!\\\\)\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si'                                              => "<span style=\"font-style:italic\">\\1</span>",
 189                '/(?<!\\\\)\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si'                                              => "<span style=\"text-decoration:underline\">\\1</span>",
 190                '/(?<!\\\\)\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si'                                    => "<div style=\"text-align:center\">\\1</div>",
 191                '/(?<!\\\\)\[strike(?::\w+)?\](.*?)\[\/strike(?::\w+)?\]/si'                                    => "<span style=\"text-decoration:line-through\">\\1</span>",
 192                // [email]
 193                '/(?<!\\\\)\[email(?::\w+)?\]' . $pattern_mail . '\[\/email(?::\w+)?\]/si'                      => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>",
 194                '/(?<!\\\\)\[email(?::\w+)?='  . $pattern_mail . '\](.*?)\[\/email(?::\w+)?\]/si'               => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>",
 195  
 196                // [url]
 197                '/(?<!\\\\)\[(google|search)\]'   . $pattern_query . '\[\/(google|search)\]/si'                 => "<a href=\"http://www.google.com/search?q=\\2\" " . ($target ? "target=\"_blank\"" : "") . " class=\"bb-url\">\\2</a>",
 198                '/(?<!\\\\)\[url(?::\w+)?\]www\.' . $pattern_url   . '\[\/url(?::\w+)?\]/si'                    => "<a href=\"http://www.\\1\" " . ($target ? "target=\"_blank\"" : "") . " class=\"bb-url\">\\1</a>",
 199                '/(?<!\\\\)\[url(?::\w+)?\]'      . $pattern_url   . '\[\/url(?::\w+)?\]/si'                    => "<a href=\"\\1\" " . ($target ? "target=\"_blank\"" : "") . " class=\"bb-url\">\\1</a>",
 200                '/(?<!\\\\)\[url(?::\w+)?='       . $pattern_url   . '?\](.*?)\[\/url(?::\w+)?\]/si'            => "<a href=\"\\1\" " . ($target ? "target=\"_blank\"" : "") . " class=\"bb-url\">\\2</a>",
 201  
 202                // [img]
 203                '/(?<!\\\\)\[img(?::\w+)?\]' . $pattern_url . '\[\/img(?::\w+)?\]/si'                           => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />",
 204                '/(?<!\\\\)\[img(?::\w+)?=([0-9]*?)x([0-9]*?)\]' . $pattern_url . '\[\/img(?::\w+)?\]/si'       => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" class=\"bb-image\" />",
 205  
 206                // [quote]
 207                '/(?<!\\\\)\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si'                                      => "<div class=\"bb-code-title\">QUOTE:</div><div class=\"bb-quote\">\\1</div>",
 208                '/(?<!\\\\)\[quote(?::\w+)?=(?:&quot;|"|\')?(.*?)["\']?(?:&quot;|"|\')?\](.*?)\[\/quote\]/si'   => "<div class=\"bb-code-title\">QUOTE \\1:</div><div class=\"bb-quote\">\\2</div>",
 209  
 210                // [list]
 211                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?list)/si' => "\n<li class=\"bb-listitem\">\\1</li>",
 212                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si'    => "\n</ul>",
 213                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:u(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ul>",
 214                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:o(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ul>",
 215                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si'   => "\n<ul class=\"bb-list-unordered\">",
 216                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ul class=\"bb-list-unordered\">",
 217                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ul class=\"bb-list-ordered\">",
 218                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ul class=\"bb-list-ordered bb-list-ordered-d\">",
 219                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s'  => "\n<ul class=\"bb-list-ordered bb-list-ordered-lr\">",
 220                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s'  => "\n<ul class=\"bb-list-ordered bb-list-ordered-ur\">",
 221                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s'  => "\n<ul class=\"bb-list-ordered bb-list-ordered-la\">",
 222                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s'  => "\n<ul class=\"bb-list-ordered bb-list-ordered-ua\">",
 223  
 224                // escaped tags like \[b], \[color], \[url], ...
 225                '/\\\\(\[\/?\w+(?::\w+)*\])/'                                      => "\\1"
 226              );
 227          }
 228  
 229          /* Regular expressions taken from http://smarty.incutio.com/?page=BBCodePlugin Wiki (Andre Rabold) */
 230          $input = preg_replace(array_keys($bbcodes), array_values($bbcodes), $input);
 231  
 232          // [code] & [php]
 233          $input = preg_replace_callback('/(?<!\\\\)\[(code|php)(?::\w+)?\](.*?)\[\/\\1(?::\w+)?\]/si', array($this, 'bbcode_callback'), $input);
 234          return $input;
 235  
 236      }
 237  
 238      function event_hook($event, &$bag, &$eventData) {
 239          global $serendipity;
 240  
 241          $hooks = &$bag->get('event_hooks');
 242  
 243          if (isset($hooks[$event])) {
 244              switch($event) {
 245                  case 'frontend_display':
 246  
 247                      foreach ($this->markup_elements as $temp) {
 248                          if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']]) &&
 249                              !$eventData['properties']['ep_disable_markup_' . $this->instance] &&
 250                              !isset($serendipity['POST']['properties']['disable_markup_' . $this->instance])) {
 251                              $element = $temp['element'];
 252                              $eventData[$element] = $this->bbcode($eventData[$element]);
 253                          }
 254                      }
 255                      return true;
 256                      break;
 257  
 258                  case 'frontend_comment':
 259                      if (serendipity_db_bool($this->get_config('COMMENT', true))) {
 260                          echo '<div class="serendipity_commentDirection serendipity_comment_bbcode">' . PLUGIN_EVENT_BBCODE_TRANSFORM . '</div>';
 261                      }
 262                      return true;
 263                      break;
 264  
 265                  case 'css':
 266                      if (strpos($eventData, '.bb-code') !== false) {
 267                          // class exists in CSS, so a user has customized it and we don't need default
 268                          return true;
 269                      }
 270  ?>
 271  .bb-quote, .bb-code, .bb-php, .bb-code-title, .bb-php-title {
 272      margin-left: 20px;
 273      margin-right: 20px;
 274      color: black;
 275      direction: ltr;
 276  }
 277  
 278  .bb-code-title, .bb-php-title {
 279      margin-bottom: 2px;
 280      background-color:#CCCCCC;
 281      font-weight: bold;
 282      padding-left: 5px;
 283  }
 284  
 285  .bb-code, .bb-php {
 286      font-family: courier, "courier new";
 287      background-color: #DDDDDD;
 288      padding: 10px;
 289      white-space: pre;
 290      overflow: auto;
 291      max-height: 24em;
 292  }
 293  
 294  .bb-quote {
 295      background-color: #DDDDDD;
 296      padding: 10px;
 297  }
 298  
 299  .bb-list-ordered-d {
 300      list-style-type: decimal;
 301  }
 302  .bb-list-ordered-la {
 303      list-style-type: lower-alpha;
 304  }
 305  .bb-list-ordered-ua {
 306      list-style-type: upper-alpha;
 307  }
 308  <?php
 309                  return true;
 310                  break;
 311  
 312                  default:
 313                    return false;
 314              }
 315          } else {
 316              return false;
 317          }
 318      }
 319  }
 320  
 321  /* vim: set sts=4 ts=4 expandtab : */
 322  ?>


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