Advertisement
aaaaaa123456789

MyBB tables plugin

Aug 20th, 2015
132
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. if (!defined('IN_MYBB')) die('This file should not be accessed directly');
  4.  
  5. $plugins -> add_hook('parse_message', 'tables_run');
  6.  
  7. function tables_info () {
  8.   return array(
  9.     'name'          => 'Table MyCode',
  10.     'description'   => 'Allows using [table], [tr] and [td] for defining tables, and also validates proper tag nesting to avoid breaking the layout.',
  11.     'author'        => 'aaaaaa123456789',
  12.     'version'       => '1.00',
  13.     'codename'      => 'tables',
  14.     'compatibility' => '16*,18*'
  15.   );
  16. }
  17.  
  18. function tables_activate () {
  19.   // do nothing
  20. }
  21.  
  22. function tables_deactivate () {
  23.   // do nothing
  24. }
  25.  
  26. function tables_run ($contents) {
  27.   return tables_parse_tags($contents, array('table', 'tr', 'td'));
  28. }
  29.  
  30. function tables_parse_tags ($contents, $tags) {
  31.   if (!count($tags)) return $contents;
  32.   $tag = array_shift($tags);
  33.   $result = '';
  34.   $matches = null;
  35.   $count = preg_match_all('{\[' . $tag . '(=[^\]<>\"]*)?\](.*?)\[/' . $tag . '\]}si', $contents, $matches, PREG_OFFSET_CAPTURE);
  36.   if (!$count) return $contents;
  37.   $last_offset = 0;
  38.   foreach ($matches[0] as $match) {
  39.     if ($last_offset != $match[1]) {
  40.       $result .= substr($contents, $last_offset, $match[1] - $last_offset);
  41.       $last_offset = $match[1];
  42.     }
  43.     $matched = $match[0];
  44.     $style = tables_get_style_from_tag($matched);
  45.     $innerHTML = tables_parse_tags(tables_get_content_from_tag($matched), $tags);
  46.     $result .= "<$tag " . (($style != null) ? ('style="' . $style . '"') : '') . ">$innerHTML</$tag>";
  47.     $last_offset += strlen($matched);
  48.   }
  49.   if ($last_offset < strlen($contents)) $result .= substr($contents, $last_offset);
  50.   return $result;
  51. }
  52.  
  53. function tables_get_style_from_tag ($data) {
  54.   $bracket = strpos($data, ']');
  55.   $equals = strpos(substr($data, 0, $bracket), '=');
  56.   if ($equals === false) return null;
  57.   return substr($data, $equals + 1, $bracket - $equals - 1);
  58. }
  59.  
  60. function tables_get_content_from_tag ($data) {
  61.   $closing_bracket = strpos($data, ']');
  62.   $opening_bracket = strrpos($data, '[');
  63.   return substr($data, $closing_bracket + 1, $opening_bracket - $closing_bracket - 1);
  64. }
  65.  
  66. ?>
Advertisement
RAW Paste Data Copied
Advertisement