PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/source/function/function_editor.php

https://github.com/kuaileshike/upload
PHP | 407 lines | 358 code | 43 blank | 6 comment | 58 complexity | 3a950c03f3f159899a66b3a533bb1d9c MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_editor.php 29236 2012-03-30 05:34:47Z chenmengshu $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function absoluteurl($url) {
  12. global $_G;
  13. if($url{0} == '/') {
  14. return 'http://'.$_SERVER['HTTP_HOST'].$url;
  15. } else {
  16. return $_G['siteurl'].$url;
  17. }
  18. }
  19. function atag($aoptions, $text) {
  20. $href = getoptionvalue('href', $aoptions);
  21. if(substr($href, 0, 7) == 'mailto:') {
  22. $tag = 'email';
  23. $href = substr($href, 7);
  24. } else {
  25. $tag = 'url';
  26. if(!preg_match("/^[a-z0-9]+:/i", $href)) {
  27. $href = absoluteurl($href);
  28. }
  29. }
  30. return "[$tag=$href]".trim(recursion('a', $text, 'atag'))."[/$tag]";
  31. }
  32. function divtag($divoptions, $text) {
  33. $prepend = $append = '';
  34. parsestyle($divoptions, $prepend, $append);
  35. $align = getoptionvalue('align', $divoptions);
  36. switch($align) {
  37. case 'left':
  38. case 'center':
  39. case 'right':
  40. break;
  41. default:
  42. $align = '';
  43. }
  44. if($align) {
  45. $prepend .= "[align=$align]";
  46. $append .= "[/align]";
  47. }
  48. $append .= "\n";
  49. return $prepend.recursion('div', $text, 'divtag').$append;
  50. }
  51. function fetchoptionvalue($option, $text) {
  52. if(($position = strpos($text, $option)) !== false) {
  53. $delimiter = $position + strlen($option);
  54. if($text{$delimiter} == '"') {
  55. $delimchar = '"';
  56. } elseif($text{$delimiter} == '\'') {
  57. $delimchar = '\'';
  58. } else {
  59. $delimchar = ' ';
  60. }
  61. $delimloc = strpos($text, $delimchar, $delimiter + 1);
  62. if($delimloc === false) {
  63. $delimloc = strlen($text);
  64. } elseif($delimchar == '"' OR $delimchar == '\'') {
  65. $delimiter++;
  66. }
  67. return trim(substr($text, $delimiter, $delimloc - $delimiter));
  68. } else {
  69. return '';
  70. }
  71. }
  72. function fonttag($fontoptions, $text) {
  73. $tags = array('font' => 'face=', 'size' => 'size=', 'color' => 'color=');
  74. $prependtags = $appendtags = '';
  75. foreach($tags as $bbcode => $locate) {
  76. $optionvalue = fetchoptionvalue($locate, $fontoptions);
  77. if($optionvalue) {
  78. $prependtags .= "[$bbcode=$optionvalue]";
  79. $appendtags = "[/$bbcode]$appendtags";
  80. }
  81. }
  82. parsestyle($fontoptions, $prependtags, $appendtags);
  83. return $prependtags.recursion('font', $text, 'fonttag').$appendtags;
  84. }
  85. function getoptionvalue($option, $text) {
  86. preg_match("/$option(\s+?)?\=(\s+?)?[\"']?(.+?)([\"']|$|>)/is", $text, $matches);
  87. return isset($matches[3]) ? trim($matches[3]) : '';
  88. }
  89. function html2bbcode($text) {
  90. $text = strip_tags($text, '<table><tr><td><b><strong><i><em><u><a><div><span><p><strike><blockquote><ol><ul><li><font><img><br><br/><h1><h2><h3><h4><h5><h6><script>');
  91. if(ismozilla()) {
  92. $text = preg_replace("/(?<!<br>|<br \/>|\r)(\r\n|\n|\r)/", ' ', $text);
  93. }
  94. $pregfind = array(
  95. "/<script.*>.*<\/script>/siU",
  96. '/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i',
  97. "/(\r\n|\n|\r)/",
  98. "/<table([^>]*(width|background|background-color|bgcolor)[^>]*)>/siUe",
  99. "/<table.*>/siU",
  100. "/<tr.*>/siU",
  101. "/<td>/i",
  102. "/<td(.+)>/siUe",
  103. "/<\/td>/i",
  104. "/<\/tr>/i",
  105. "/<\/table>/i",
  106. '/<h([0-9]+)[^>]*>(.*)<\/h\\1>/siU',
  107. "/<img[^>]+smilieid=\"(\d+)\".*>/esiU",
  108. "/<img([^>]*src[^>]*)>/eiU",
  109. "/<a\s+?name=.+?\".\">(.+?)<\/a>/is",
  110. "/<br.*>/siU",
  111. "/<span\s+?style=\"float:\s+(left|right);\">(.+?)<\/span>/is",
  112. );
  113. $pregreplace = array(
  114. '',
  115. '',
  116. '',
  117. "tabletag('\\1')",
  118. '[table]',
  119. '[tr]',
  120. '[td]',
  121. "tdtag('\\1')",
  122. '[/td]',
  123. '[/tr]',
  124. '[/table]',
  125. "[size=\\1]\\2[/size]\n\n",
  126. "smileycode('\\1')",
  127. "imgtag('\\1')",
  128. '\1',
  129. "\n",
  130. "[float=\\1]\\2[/float]",
  131. );
  132. $text = preg_replace($pregfind, $pregreplace, $text);
  133. $text = recursion('b', $text, 'simpletag', 'b');
  134. $text = recursion('strong', $text, 'simpletag', 'b');
  135. $text = recursion('i', $text, 'simpletag', 'i');
  136. $text = recursion('em', $text, 'simpletag', 'i');
  137. $text = recursion('u', $text, 'simpletag', 'u');
  138. $text = recursion('a', $text, 'atag');
  139. $text = recursion('font', $text, 'fonttag');
  140. $text = recursion('blockquote', $text, 'simpletag', 'indent');
  141. $text = recursion('ol', $text, 'listtag');
  142. $text = recursion('ul', $text, 'listtag');
  143. $text = recursion('div', $text, 'divtag');
  144. $text = recursion('span', $text, 'spantag');
  145. $text = recursion('p', $text, 'ptag');
  146. $pregfind = array("/(?<!\r|\n|^)\[(\/list|list|\*)\]/", "/<li>(.*)((?=<li>)|<\/li>)/iU", "/<p.*>/iU", "/<p><\/p>/i", "/(<a>|<\/a>|<\/li>)/is", "/<\/?(A|LI|FONT|DIV|SPAN)>/siU", "/\[url[^\]]*\]\[\/url\]/i", "/\[url=javascript:[^\]]*\](.+?)\[\/url\]/is");
  147. $pregreplace = array("\n[\\1]", "\\1\n", "\n", '', '', '', '', "\\1");
  148. $text = preg_replace($pregfind, $pregreplace, $text);
  149. $strfind = array('&nbsp;', '&lt;', '&gt;', '&amp;');
  150. $strreplace = array(' ', '<', '>', '&');
  151. $text = str_replace($strfind, $strreplace, $text);
  152. return dhtmlspecialchars(trim($text));
  153. }
  154. function imgtag($attributes) {
  155. $value = array('src' => '', 'width' => '', 'height' => '');
  156. preg_match_all("/(src|width|height)=([\"|\']?)([^\"']+)(\\2)/is", dstripslashes($attributes), $matches);
  157. if(is_array($matches[1])) {
  158. foreach($matches[1] as $key => $attribute) {
  159. $value[strtolower($attribute)] = $matches[3][$key];
  160. }
  161. }
  162. @extract($value);
  163. if(!preg_match("/^http:\/\//i", $src)) {
  164. $src = absoluteurl($src);
  165. }
  166. return $src ? ($width && $height ? '[img='.$width.','.$height.']'.$src.'[/img]' : '[img]'.$src.'[/img]') : '';
  167. }
  168. function ismozilla() {
  169. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  170. if(strpos($useragent, 'gecko') !== FALSE) {
  171. preg_match("/gecko\/(\d+)/", $useragent, $regs);
  172. return $regs[1];
  173. }
  174. return FALSE;
  175. }
  176. function litag($listoptions, $text) {
  177. return '[*]'.rtrim($text);
  178. }
  179. function listtag($listoptions, $text, $tagname) {
  180. require_once libfile('function/post');
  181. $text = preg_replace('/<li>((.(?!<\/li))*)(?=<\/?ol|<\/?ul|<li|\[list|\[\/list)/siU', '<li>\\1</li>', $text).(isopera() ? '</li>' : NULL);
  182. $text = recursion('li', $text, 'litag');
  183. if($tagname == 'ol') {
  184. $listtype = fetchoptionvalue('type=', $listoptions) ? fetchoptionvalue('type=', $listoptions) : 1;
  185. if(in_array($listtype, array('1', 'a', 'A'))) {
  186. $opentag = '[list='.$listtype.']';
  187. }
  188. } else {
  189. $opentag = '[list]';
  190. }
  191. return $text ? $opentag.recursion($tagname, $text, 'listtag').'[/list]' : FALSE;
  192. }
  193. function parsestyle($tagoptions, &$prependtags, &$appendtags) {
  194. $searchlist = array(
  195. array('tag' => 'align', 'option' => TRUE, 'regex' => 'text-align:\s*(left);?', 'match' => 1),
  196. array('tag' => 'align', 'option' => TRUE, 'regex' => 'text-align:\s*(center);?', 'match' => 1),
  197. array('tag' => 'align', 'option' => TRUE, 'regex' => 'text-align:\s*(right);?', 'match' => 1),
  198. array('tag' => 'color', 'option' => TRUE, 'regex' => '(?<![a-z0-9-])color:\s*([^;]+);?', 'match' => 1),
  199. array('tag' => 'font', 'option' => TRUE, 'regex' => 'font-family:\s*([^;]+);?', 'match' => 1),
  200. array('tag' => 'size', 'option' => TRUE, 'regex' => 'font-size:\s*(\d+(\.\d+)?(px|pt|in|cm|mm|pc|em|ex|%|));?', 'match' => 1),
  201. array('tag' => 'b', 'option' => FALSE, 'regex' => 'font-weight:\s*(bold);?'),
  202. array('tag' => 'i', 'option' => FALSE, 'regex' => 'font-style:\s*(italic);?'),
  203. array('tag' => 'u', 'option' => FALSE, 'regex' => 'text-decoration:\s*(underline);?')
  204. );
  205. $style = getoptionvalue('style', $tagoptions);
  206. $style = preg_replace(
  207. "/(?<![a-z0-9-])color:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)(;?)/ie",
  208. 'sprintf("color: #%02X%02X%02X$4", $1, $2, $3)',
  209. $style
  210. );
  211. foreach($searchlist as $searchtag) {
  212. if(preg_match('/'.$searchtag['regex'].'/i', $style, $match)) {
  213. $opnvalue = $match["$searchtag[match]"];
  214. $prependtags .= '['.$searchtag['tag'].($searchtag['option'] == TRUE ? '='.$opnvalue.']' : ']');
  215. $appendtags = '[/'.$searchtag['tag']."]$appendtags";
  216. }
  217. }
  218. }
  219. function ptag($poptions, $text) {
  220. $align = getoptionvalue('align', $poptions);
  221. switch($align) {
  222. case 'left':
  223. case 'center':
  224. case 'right':
  225. break;
  226. default:
  227. $align = '';
  228. }
  229. $prepend = $append = '';
  230. parsestyle($poptions, $prepend, $append);
  231. if($align) {
  232. $prepend .= "[align=$align]";
  233. $append .= "[/align]";
  234. }
  235. $append .= "\n";
  236. return $prepend.recursion('p', $text, 'ptag').$append;
  237. }
  238. function recursion($tagname, $text, $function, $extraargs = '') {
  239. $tagname = strtolower($tagname);
  240. $open_tag = "<$tagname";
  241. $open_tag_len = strlen($open_tag);
  242. $close_tag = "</$tagname>";
  243. $close_tag_len = strlen($close_tag);
  244. $beginsearchpos = 0;
  245. do {
  246. $textlower = strtolower($text);
  247. $tagbegin = @strpos($textlower, $open_tag, $beginsearchpos);
  248. if($tagbegin === FALSE) {
  249. break;
  250. }
  251. $strlen = strlen($text);
  252. $inquote = '';
  253. $found = FALSE;
  254. $tagnameend = FALSE;
  255. for($optionend = $tagbegin; $optionend <= $strlen; $optionend++) {
  256. $char = $text{$optionend};
  257. if(($char == '"' || $char == "'") && $inquote == '') {
  258. $inquote = $char;
  259. } elseif(($char == '"' || $char == "'") && $inquote == $char) {
  260. $inquote = '';
  261. } elseif($char == '>' && !$inquote) {
  262. $found = TRUE;
  263. break;
  264. } elseif(($char == '=' || $char == ' ') && !$tagnameend) {
  265. $tagnameend = $optionend;
  266. }
  267. }
  268. if(!$found) {
  269. break;
  270. }
  271. if(!$tagnameend) {
  272. $tagnameend = $optionend;
  273. }
  274. $offset = $optionend - ($tagbegin + $open_tag_len);
  275. $tagoptions = substr($text, $tagbegin + $open_tag_len, $offset);
  276. $acttagname = substr($textlower, $tagbegin + 1, $tagnameend - $tagbegin - 1);
  277. if($acttagname != $tagname) {
  278. $beginsearchpos = $optionend;
  279. continue;
  280. }
  281. $tagend = strpos($textlower, $close_tag, $optionend);
  282. if($tagend === FALSE) {
  283. break;
  284. }
  285. $nestedopenpos = strpos($textlower, $open_tag, $optionend);
  286. while($nestedopenpos !== FALSE && $tagend !== FALSE) {
  287. if($nestedopenpos > $tagend) {
  288. break;
  289. }
  290. $tagend = strpos($textlower, $close_tag, $tagend + $close_tag_len);
  291. $nestedopenpos = strpos($textlower, $open_tag, $nestedopenpos + $open_tag_len);
  292. }
  293. if($tagend === FALSE) {
  294. $beginsearchpos = $optionend;
  295. continue;
  296. }
  297. $localbegin = $optionend + 1;
  298. $localtext = $function($tagoptions, substr($text, $localbegin, $tagend - $localbegin), $tagname, $extraargs);
  299. $text = substr_replace($text, $localtext, $tagbegin, $tagend + $close_tag_len - $tagbegin);
  300. $beginsearchpos = $tagbegin + strlen($localtext);
  301. } while($tagbegin !== FALSE);
  302. return $text;
  303. }
  304. function simpletag($options, $text, $tagname, $parseto) {
  305. if(trim($text) == '') {
  306. return '';
  307. }
  308. $text = recursion($tagname, $text, 'simpletag', $parseto);
  309. return "[$parseto]{$text}[/$parseto]";
  310. }
  311. function smileycode($smileyid) {
  312. global $_G;
  313. if(!is_array($_G['cache']['smileycodes'])) {
  314. loadcache(array('bbcodes_display', 'bbcodes', 'smileycodes', 'smilies', 'smileytypes', 'domainwhitelist'));
  315. }
  316. foreach($_G['cache']['smileycodes'] as $id => $code) {
  317. if($smileyid == $id) {
  318. return $code;
  319. }
  320. }
  321. }
  322. function spantag($spanoptions, $text) {
  323. $prependtags = $appendtags = '';
  324. parsestyle($spanoptions, $prependtags, $appendtags);
  325. return $prependtags.recursion('span', $text, 'spantag').$appendtags;
  326. }
  327. function tabletag($attributes) {
  328. $attributes = dstripslashes($attributes);
  329. $width = '';
  330. if(preg_match("/width=([\"|\']?)(\d{1,4}%?)(\\1)/is", $attributes, $matches)) {
  331. $width = substr($matches[2], -1) == '%' ? (substr($matches[2], 0, -1) <= 98 ? $matches[2] : '98%') : ($matches[2] <= 560 ? $matches[2] : '560');
  332. } elseif(preg_match("/width\s?:\s?(\d{1,4})([px|%])/is", $attributes, $matches)) {
  333. $width = $matches[2] == '%' ? ($matches[1] <= 98 ? $matches[1].'%' : '98%') : ($matches[1] <= 560 ? $matches[1] : '560');
  334. }
  335. if(preg_match("/(?:background|background-color|bgcolor)[:=]\s*([\"']?)((rgb\(\d{1,3}%?,\s*\d{1,3}%?,\s*\d{1,3}%?\))|(#[0-9a-fA-F]{3,6})|([a-zA-Z]{1,20}))(\\1)/i", $attributes, $matches)) {
  336. $bgcolor = $matches[2];
  337. $width = $width ? $width : '98%';
  338. } else {
  339. $bgcolor = '';
  340. }
  341. return $bgcolor ? "[table=$width,$bgcolor]" :($width ? "[table=$width]" : '[table]');
  342. }
  343. function tdtag($attributes) {
  344. $value = array('colspan' => 1, 'rowspan' => 1, 'width' => '');
  345. preg_match_all("/(colspan|rowspan|width)=([\"|\']?)(\d{1,4}%?)(\\2)/is", dstripslashes($attributes), $matches);
  346. if(is_array($matches[1])) {
  347. foreach($matches[1] as $key => $attribute) {
  348. $value[strtolower($attribute)] = $matches[3][$key];
  349. }
  350. }
  351. @extract($value);
  352. return $width == '' ? ($colspan == 1 && $rowspan == 1 ? '[td]' : "[td=$colspan,$rowspan]") : "[td=$colspan,$rowspan,$width]";
  353. }
  354. ?>