1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
<?php
class TagHandler {
/*
* Denied tags -- used to be able to process
* all this in two passes (eg: used for tags needing information for other stuff)
*/
private static $deniedtags = Array();
/*
* UBB tag configuration params
*/
public static $tagconfig =
Array(
/* ------- b -------------------- */
'b' =>
Array('b' =>
Array('closetags' => Array('b'),
'allowedchildren' => Array(NULL),
'handler' => Array('TagHandler', 'handle_bold') ),
'br' =>
Array('closetags' => Array(NULL),
'allowedchildren' => Array(''),
'handler' => Array('TagHandler', 'handle_br') )
),
/* ------- i -------------------- */
'i' =>
Array('i' =>
Array('closetags' => Array('i'),
'allowedchildren' => Array(NULL),
'handler' => Array('TagHandler', 'handle_italic') ),
'img' =>
Array('closetags' => Array(NULL),
'allowedchildren' => Array(''),
'handler' => Array('TagHandler', 'handle_img') )
),
/* ------- u ------------------- */
'u' =>
Array('u' =>
Array('closetags' => Array('u'),
'allowedchildren' => Array(NULL),
'handler' => Array('TagHandler', 'handle_underline')),
'url' =>
Array('closetags' => Array('url'),
'allowedchildren' => Array(''),
'handler' => Array('TagHandler', 'handle_url') )
),
/* ------- q ------------------- */
'q' =>
Array('quote' =>
Array('closetags' => Array('quote'),
'allowedchildren' => Array(NULL),
'handler' => Array('TagHandler', 'handle_quote'))
)
);
/**
* Returns the tag config for a given tag
*/
static function gettagconfig($tagname) {
if ((strlen($tagname) >= 1) && (isset(TagHandler::$tagconfig[$tagname[0]][$tagname]))) {
return TagHandler::$tagconfig[$tagname[0]][$tagname];
} else {
return NULL;
} // else
} // gettagconfig
/*
* Add additional configuration for a tag
*/
static function setadditionalinfo($tagname, $name, $value) {
TagHandler::$tagconfig[$tagname[0]][$tagname][$name] = $value;
} # setadditionalinfo
/*
* Set the list of denied tags
*/
static function setdeniedtags($deniedtags) {
TagHandler::$deniedtags = $deniedtags;
} // setdeniedtags
/*
* Returns the list of denied tags
*/
static function getdeniedtags($deniedtags) {
return TagHandler::$deniedtags;
} // getdeniedtags
/*
* Processes an tag (when allowed)
*/
static function process_tag($tagname, $params, $contents) {
if (array_search($tagname, TagHandler::$deniedtags) !== FALSE) {
return NULL;
} // if denied tag
if (isset(TagHandler::$tagconfig[$tagname[0]][$tagname]['handler'])) {
return call_user_func_array(TagHandler::$tagconfig[$tagname[0]][$tagname]['handler'],
array($params, $contents));
} else {
// ??
} # if
} // process_tag
/* Returns an empty append/prepend, used for deprecated tags */
static function handle_empty($params, $contents) {
return Array('prepend' => '', 'content' => $contents, 'append' => '');
} // func. handle_empty
static function handle_bold($params, $contents) {
return Array('prepend' => '<b>',
'content' => $contents,
'append' => '</b>');
} // handle_bold
static function handle_underline($params, $contents) {
return Array('prepend' => '<u>',
'content' => $contents,
'append' => '</u>');
} // handle_underline
static function handle_italic($params, $contents) {
return Array('prepend' => '<i>',
'content' => $contents,
'append' => '</i>');
} // handle_italic
/* Handles [br] */
static function handle_br($params, $contents) {
return Array('prepend' => '<br>',
'content' => $contents,
'append' => '');
} // handle_br
/* handle the img tag */
static function handle_img($params, $contents) {
$origAppend = '';
# are only specific images allowed?
if (isset(TagHandler::$tagconfig['i']['img']['allowedimgs'])) {
if (!isset(TagHandler::$tagconfig['i']['img']['allowedimgs'][$params['params'][0]])) {
return TagHandler::handle_empty($params, $contents);
} else {
$origAppend = $contents;
$contents = TagHandler::$tagconfig['i']['img']['allowedimgs'][$params['params'][0]];
} # if
} # if
return Array('prepend' => '<img src="' . $contents . '">',
'content' => $origAppend,
'append' => '');
} // handle_img
/* handle the quote tag */
static function handle_quote($params, $contents) {
# quote it
return Array('prepend' => '<blockquote><strong>' . sprintf(_("%s commented earlier:"), substr($params['originalparams'], 1)). '</strong><br>',
'content' => $contents,
'append' => '</blockquote>');
} // handle_quote
/* handle the img tag */
static function handle_url($params, $contents) {
# are only specific images allowed?
return Array('prepend' => '<a href="' . substr($params['originalparams'], 1) . '">',
'content' => $contents,
'append' => '</a>');
} // handle_url
/* handle the noubb tag */
static function handle_noubb($params, $contents) {
return Array('prepend' => '',
'content' => $contents,
'append' => '');
} // handle_noubb
} // class TagHandler
|