PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/class/bwbbcode.class.php

http://firmkernel.googlecode.com/
PHP | 287 lines | 258 code | 17 blank | 12 comment | 29 complexity | 236c4dbd7ca1b6e5ef9524f14567779d MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------------+
  4. | $Id: bwbbcode.class.php 2009-08-18 08:02:23Z Bleakwind $
  5. | Parse BBCODE
  6. | Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
  7. | http://www.weaverdream.com/
  8. | Release under the GNU Lesser General Public License Version 3 (LGPLv3):
  9. | http://www.gnu.org/licenses/lgpl.html
  10. +-----------------------------------------------------------------------------+
  11. */
  12. class bwbbcode{
  13. var $bw_tag = array();
  14. var $bw_js_han = "";
  15. var $bw_js_fun = "";
  16. var $bw_serial = 0;
  17. // "yes" = halt with message, "no" = ignore errors quietly, "report" = ignore errror, but spit a warning
  18. var $halt_on_error = "yes";
  19. var $adminemail = "Administrator";
  20. var $setting;
  21. function bwbbcode()
  22. {
  23. $this->setting = array(
  24. 'entities_p' => false,
  25. 'entities_v' => false,
  26. 'color_bg' => "#FFFFFF",
  27. 'color_comment' => "#BBBBBB",
  28. 'color_default' => "#0000BB",
  29. 'color_html' => "#000000",
  30. 'color_keyword' => "#007700",
  31. 'color_string' => "#DD0000",
  32. );
  33. $this->set_color();
  34. return;
  35. }
  36. function set_color()
  37. {
  38. @ini_set("highlight.bg", $this->setting['color_bg']);
  39. @ini_set("highlight.comment", $this->setting['color_comment']);
  40. @ini_set("highlight.default", $this->setting['color_default']);
  41. @ini_set("highlight.html", $this->setting['color_html']);
  42. @ini_set("highlight.keyword", $this->setting['color_keyword']);
  43. @ini_set("highlight.string", $this->setting['color_string']);
  44. return;
  45. }
  46. function add_code_copyfun($str)
  47. {
  48. $this->bw_js_fun = $str;
  49. return;
  50. }
  51. function add_tag($pattern)
  52. {
  53. if(!preg_match("/^[a-z]{1,30}$/i", $pattern['bw_bb_name'])) {
  54. $this->halt("bw_bb_name error...");
  55. }
  56. if(array_key_exists($pattern['bw_bb_name'], $this->bw_tag)) {
  57. $this->halt("bw_bb_name[".$pattern['bw_bb_name']."] is already in use...");
  58. }
  59. if ($pattern['bw_if_value'] != "no") {
  60. $pattern['bw_if_value'] = "yes";
  61. if(!preg_match("/^[a-z<>\/]{1,255}$/i", $pattern['bw_html_end'])) {
  62. $this->halt("bw_html_end error...");
  63. }
  64. }
  65. if( $pattern['bw_if_param'] == "yes" || $pattern['bw_if_param'] == "both" ) {
  66. if(empty($pattern['bw_param_regex'])) {
  67. $pattern['bw_param_regex'] = '[^\\]]+';
  68. }
  69. } else {
  70. $pattern['bw_if_param'] = "no";
  71. }
  72. if(empty($pattern['bw_param_replace'])) {
  73. $pattern['bw_param_replace'] = array();
  74. }
  75. $this->bw_tag[$pattern['bw_bb_name']] = $pattern;
  76. return true;
  77. }
  78. function add_alias($name, $aliasof)
  79. {
  80. if(!array_key_exists($aliasof, $this->bw_tag) || array_key_exists($name, $this->bw_tag)) {
  81. return false;
  82. }else{
  83. $this->bw_tag[$name] = array('alias' => $aliasof);
  84. return true;
  85. }
  86. }
  87. function get_data($name, $alias = "")
  88. {
  89. if(!array_key_exists($name, $this->bw_tag)){
  90. return "";
  91. } else {
  92. $data = $this->bw_tag[$name];
  93. $this_name = $alias == "" ? $name : $alias;
  94. $data['bw_bb_name'] = $this_name;
  95. return $data;
  96. }
  97. }
  98. function clear_param($param, $array)
  99. {
  100. if (!empty($array)) {
  101. $pattern = array_keys($array);
  102. $replace = array_values($array);
  103. $param = preg_replace($pattern, $replace, $param);
  104. }
  105. if($this->setting['entities_p']){
  106. $param = htmlentities($param);
  107. }
  108. return $param;
  109. }
  110. function clear_value($value)
  111. {
  112. if($this->setting['entities_v']){
  113. $value = htmlentities($value);
  114. }
  115. return $value;
  116. }
  117. function make_value($bw_html_begin, $value, $bw_html_end, $func)
  118. {
  119. $this->bw_serial++;
  120. $value = str_replace("\\\"", "\"", $value);
  121. list($bw_html_begin, $bw_html_end) = str_ireplace('#s#', $this->bw_serial, array($bw_html_begin, $bw_html_end));
  122. if (!empty($func)) {
  123. $value = $this->$func($value, $this->bw_serial);
  124. }
  125. return $bw_html_begin.$value.$bw_html_end;
  126. }
  127. function parse_bwbbcode($content)
  128. {
  129. $this->bw_js_han = "";
  130. foreach($this->bw_tag as $tag_name => $tag_data){
  131. if (array_key_exists('alias', $tag_data)) {
  132. $tag_data = $this->get_data($tag_data['alias'], $tag_name);
  133. }
  134. if( $tag_data['bw_if_param'] == "yes" || $tag_data['bw_if_param'] == "both" ) {
  135. $pattern = "/\\[".$tag_data['bw_bb_name']."=(".$tag_data['bw_param_regex'].")\\](.+)\\[\\/".$tag_data['bw_bb_name']."\\]/Uise";
  136. if ($tag_data['bw_if_value'] == "no") {
  137. $replace = str_ireplace(array('#p#','#v#'),
  138. array('\'.$this->clear_param(\'$1\', $tag_data[\'bw_param_replace\']).\'','\'.$this->clear_value(\'$2\').\''),
  139. "'".$tag_data['bw_html_begin'].$tag_data['bw_html_end']."'");
  140. } else {
  141. $replace = str_ireplace(array('#p#','#v#'),
  142. array('\'.$this->clear_param(\'$1\', $tag_data[\'bw_param_replace\']).\'','\'.$this->clear_value(\'$2\').\''),
  143. "\$this->make_value('".$tag_data['bw_html_begin']."', '\$2', '".$tag_data['bw_html_end']."', '".$tag_data['bw_value_replace']."')");
  144. }
  145. $content = preg_replace($pattern, $replace, $content);
  146. }
  147. if( $tag_data['bw_if_param'] == "no" || $tag_data['bw_if_param'] == "both" ) {
  148. $pattern = "/\\[".$tag_data['bw_bb_name']."\\](.+)\\[\\/".$tag_data['bw_bb_name']."\\]/Uise";
  149. if ($tag_data['bw_if_value'] == "no") {
  150. $replace = str_ireplace(array('#p#','#v#'),
  151. array('\'.$this->clear_value(\'$1\').\'','\'.$this->clear_value(\'$1\').\''),
  152. "'".$tag_data['bw_html_begin'].$tag_data['bw_html_end']."'");
  153. } else {
  154. $replace = str_ireplace(array('#p#','#v#'),
  155. array('\'.$this->clear_value(\'$1\').\'','\'.$this->clear_value(\'$1\').\''),
  156. "\$this->make_value('".$tag_data['bw_html_begin']."', '\$1', '".$tag_data['bw_html_end']."', '".$tag_data['bw_value_replace']."')");
  157. }
  158. $content = preg_replace($pattern, $replace, $content);
  159. }
  160. }
  161. if (!empty($this->bw_js_han)) {
  162. if (empty($this->bw_js_fun)) {
  163. $this->bw_js_han = "\n<script type=\"text/javascript\">\n<!-- \n$(document).ready(function() {\n".$this->bw_js_han."});\n //-->\n</script>\n";
  164. } else {
  165. $this->bw_js_han = "\n<script type=\"text/javascript\">\n<!-- \n".$this->bw_js_fun."$(document).ready(function() {\n".$this->bw_js_han."});\n //-->\n</script>\n";
  166. $this->bw_js_fun = "";
  167. }
  168. }
  169. $content = $content.$this->bw_js_han;
  170. return $content;
  171. }
  172. function handle_code($str, $serial)
  173. {
  174. $str_source = $this->str_detag($str, "space,br");
  175. $str_highlight = highlight_string($str_source, "1");
  176. $str_highlight = preg_replace("/^\s?\<code\>\s?(\<span style\=\"color\: |\<font color\=\")".$this->setting['color_html']."\"\>(.*)(\<\/span\>|\<\/font\>)\s?\<\/code\>\s?$/is", "\$2", $str_highlight);
  177. $str_highlight = preg_replace("/(\<span style\=\"color\: |\<font color\=\")\#([a-f0-9]{6})\"\>(.*)(\<\/span\>|\<\/font\>)/Uise", "\$this->handle_code_addtag('\$3', '\$2')", $str_highlight);
  178. $str_highlight_array = preg_split("/\<br \/\>|\<br\>/is", $str_highlight);
  179. $str_highlight_count = count($str_highlight_array);
  180. $result = "<ul>\n";
  181. for ($i = 0; $i < $str_highlight_count; $i++) {
  182. $result .= "<li><span style=\"color: ".$this->setting['color_html']."\">";
  183. $result .= $str_highlight_array[$i];
  184. $result .= "</span></li>\n";
  185. }
  186. $result .= "</ul>\n";
  187. $this->bw_js_han .= "$('#block_code_".$serial." .block_code_title_right').bwcopyer({id: 'code_".$serial."', get: 'bbcode_code_get', ret: 'bbcode_code_ret'});\n";
  188. $this->bw_js_han .= "$('#block_code_".$serial." .block_code_title_right').append('<div style=\"display:none;\"><textarea style=\"width:0px;height:0px;\" id=\"code_".$serial."\"></textarea></div>');";
  189. $this->bw_js_han .= "xajax_bwbbcode_code_js('code_".$serial."', $('#block_code_".$serial." .block_code_content').html());\n";
  190. return $result;
  191. }
  192. function handle_code_addtag($str, $color)
  193. {
  194. $str = str_replace("\\\"", "\"", $str);
  195. $str_array = preg_split("/\<br \/\>|\<br\>/is", $str);
  196. if (is_array($str_array) && !empty($str_array)){
  197. foreach ($str_array as $k => $v) {
  198. $str_array[$k] = "<span style=\"color: #".$color."\">".$v."</span>";
  199. }
  200. $result = implode("<br />", $str_array);
  201. } else {
  202. $result = $str;
  203. }
  204. return $result;
  205. }
  206. // de tag
  207. function str_detag($str, $array="")
  208. {
  209. $check['space'] = "0";
  210. $check['br'] = "0";
  211. if (!empty($array)) {
  212. if (!is_array($array)) {
  213. $array = explode(",", $array);
  214. }
  215. foreach ($array as $v){
  216. if ($v == "space") {
  217. $check['space'] = "1";
  218. } elseif ($v == "br") {
  219. $check['br'] = "1";
  220. }
  221. }
  222. }
  223. if ($check['br'] == "1") {
  224. $search = array("<br />");
  225. $replace = array("\n");
  226. }
  227. $search = array_merge($search, array("&quot;", "&#39;", "&lt;", "&gt;", "&#36;"));
  228. $replace = array_merge($replace, array("\"", "'", "<", ">", "$",));
  229. if ($check['space'] == "1") {
  230. $search = array_merge($search, array("&nbsp;"));
  231. $replace = array_merge($replace, array(" "));
  232. }
  233. $search = array_merge($search, array("&amp;"));
  234. $replace = array_merge($replace, array("&"));
  235. $str = str_replace($search, $replace, $str);
  236. return $str;
  237. }
  238. // Debug of print var
  239. function fp($var, $type="")
  240. {
  241. $print = !empty($type) ? "var_dump" : "print_r";
  242. echo "\n<pre>";
  243. $print($var);
  244. echo "</pre>\n";
  245. }
  246. // error halt
  247. function halt($msg)
  248. {
  249. if ($this->halt_on_error != "no") {
  250. echo "<div style='font-family:sans-serif;font-size: 11px;'><b>Weaverdream info:</b> BBCode Error</div>\n";
  251. echo "<div style='width:600px;padding: 3px;background-color:#D6DFF7;border:#1151BF 1px solid;font-family:sans-serif;font-size: 11px;'>\n";
  252. printf("<b>Page:</b> ".$_SERVER['PHP_SELF']."<br>\n");
  253. printf("<b>Error:</b> %s<br>\n", $msg);
  254. echo "</div>\n";
  255. echo "<div style='font-family:sans-serif;font-size: 11px;'>You can try to <a href='javascript:location.reload()' target='_self'><font color='#1151BF' style='text-decoration:none;'>refresh</font></a> the page, if this doesn't fix the error, please contact <font color='#1151BF'>".$this->adminemail."</font> and report the error message.</div>\n";
  256. }
  257. if ($this->halt_on_error == "yes") {
  258. exit("<div style='font-family:sans-serif;font-size: 11px;'>BBCode halted.</div>\n");
  259. }
  260. return false;
  261. }
  262. }
  263. ?>