<?php
if (!defined('IN_MYBB')) die('This file should not be accessed directly');
$plugins -> add_hook('parse_message', 'tables_run');
function tables_info () {
return array(
'name' => 'Table MyCode',
'description' => 'Allows using [table], [tr] and [td] for defining tables, and also validates proper tag nesting to avoid breaking the layout.',
'author' => 'aaaaaa123456789',
'version' => '1.00',
'codename' => 'tables',
'compatibility' => '16*,18*'
);
}
function tables_activate () {
// do nothing
}
function tables_deactivate () {
// do nothing
}
function tables_run ($contents) {
return tables_parse_tags($contents, array('table', 'tr', 'td'));
}
function tables_parse_tags ($contents, $tags) {
if (!count($tags)) return $contents;
$tag = array_shift($tags);
$result = '';
$matches = null;
$count = preg_match_all('{\[' . $tag . '(=[^\]<>\"]*)?\](.*?)\[/' . $tag . '\]}si', $contents, $matches, PREG_OFFSET_CAPTURE);
if (!$count) return $contents;
$last_offset = 0;
foreach ($matches[0] as $match) {
if ($last_offset != $match[1]) {
$result .= substr($contents, $last_offset, $match[1] - $last_offset);
$last_offset = $match[1];
}
$matched = $match[0];
$style = tables_get_style_from_tag($matched);
$innerHTML = tables_parse_tags(tables_get_content_from_tag($matched), $tags);
$result .= "<$tag " . (($style != null) ? ('style="' . $style . '"') : '') . ">$innerHTML</$tag>";
$last_offset += strlen($matched);
}
if ($last_offset < strlen($contents)) $result .= substr($contents, $last_offset);
return $result;
}
function tables_get_style_from_tag ($data) {
$bracket = strpos($data, ']');
$equals = strpos(substr($data, 0, $bracket), '=');
if ($equals === false) return null;
return substr($data, $equals + 1, $bracket - $equals - 1);
}
function tables_get_content_from_tag ($data) {
$closing_bracket = strpos($data, ']');
$opening_bracket = strrpos($data, '[');
return substr($data, $closing_bracket + 1, $opening_bracket - $closing_bracket - 1);
}
?>
Comments