1: <?php
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: function F_decode_tcecode($text_to_decode)
39: {
40: require_once('../config/tce_config.php');
41: global $l, $db;
42:
43:
44: $pattern = array();
45: $replacement = array();
46: $i=0;
47:
48:
49: $newtext = htmlspecialchars($text_to_decode, ENT_NOQUOTES, $l['a_meta_charset']);
50:
51:
52:
53: $newtext = preg_replace("'\[\*\](.*?)\n'i", "[li]\\1[/li]", $newtext);
54:
55: $newtext = preg_replace("'\[img\](.*?)\[/img\]'si", "[object]\\1[/object]", $newtext);
56:
57: $newtext = preg_replace("'\[img=(.*?)x(.*?)\](.*?)\[/img\]'si", "[object]\\3[/object:\\1:\\2]", $newtext);
58:
59:
60:
61: $newtext = preg_replace_callback("#\[tex\](.*?)\[/tex\]#si", 'F_latex_callback', $newtext);
62:
63:
64: $newtext = preg_replace_callback("#\[mathml\](.*?)\[/mathml\]#si", 'F_mathml_callback', $newtext);
65:
66:
67: $newtext = preg_replace_callback("#\[object\](.*?)\.(.*?)\[/object\:(.*?)\:(.*?)\:(.*?)\]#si", 'F_objects_callback', $newtext);
68:
69: $newtext = preg_replace_callback("#\[object\](.*?)\.(.*?)\[/object\:(.*?)\:(.*?)\]#si", 'F_objects_callback', $newtext);
70:
71: $newtext = preg_replace_callback("#\[object\](.*?)\.(.*?)\[/object\]#si", 'F_objects_callback', $newtext);
72:
73:
74:
75:
76: while (preg_match("'\[code\](.*?) (.*?)\[/code\]'si", $newtext)) {
77: $newtext = preg_replace("'\[code\](.*?) (.*?)\[/code\]'si", "[code]\\1 \\2[/code]", $newtext);
78: }
79: 80: 81: 82:
83:
84:
85: $pattern[++$i] = "#\[url\](.*?)\[/url\]#si";
86: $replacement[++$i] = '<a class="tcecode" href="\1">\1</a>';
87:
88:
89: $pattern[++$i] = "#\[url=(.*?)\](.*?)\[/url\]#si";
90: $replacement[++$i] = '<a class="tcecode" href="\1">\2</a>';
91:
92:
93: $pattern[++$i] = "#\[dir=(.*?)\](.*?)\[/dir\]#si";
94: $replacement[++$i] = '<span dir="\1">\2</span>';
95:
96:
97: $pattern[++$i] = "#\[align=(.*?)\](.*?)\[/align\]#si";
98: $replacement[++$i] = '<span style="text-align:\1;">\2</span>';
99:
100:
101: $pattern[++$i] = "#\[code\](.*?)\[/code\]#si";
102: $replacement[++$i] = '<div class="tcecodepre">\1</div>';
103:
104:
105: $pattern[++$i] = "#\[small\](.*?)\[/small\]#si";
106: $replacement[++$i] = '<small class="tcecode">\1</small>';
107:
108:
109: $pattern[++$i] = "#\[b\](.*?)\[/b\]#si";
110: $replacement[++$i] = '<strong class="tcecode">\1</strong>';
111:
112:
113: $pattern[++$i] = "#\[i\](.*?)\[/i\]#si";
114: $replacement[++$i] = '<em class="tcecode">\1</em>';
115:
116:
117: $pattern[++$i] = "#\[s\](.*?)\[/s\]#si";
118: $replacement[++$i] = '<span style="text-decoration:line-through;">\1</span>';
119:
120:
121: $pattern[++$i] = "#\[u\](.*?)\[/u\]#si";
122: $replacement[++$i] = '<span style="text-decoration:underline;">\1</span>';
123:
124:
125: $pattern[++$i] = "#\[o\](.*?)\[/o\]#si";
126: $replacement[++$i] = '<span style="text-decoration:overline;">\1</span>';
127:
128:
129: $pattern[++$i] = "#\[sub\](.*?)\[/sub\]#si";
130: $replacement[++$i] = '<sub class="tcecode">\1</sub>';
131:
132:
133: $pattern[++$i] = "#\[sup\](.*?)\[/sup\]#si";
134: $replacement[++$i] = '<sup class="tcecode">\1</sup>';
135:
136:
137: $pattern[++$i] = "#\[ulist\](.*?)\[/ulist\]#si";
138: $replacement[++$i] = '<ul class="tcecode">\1</ul>';
139:
140:
141: $pattern[++$i] = "#\[olist\](.*?)\[/olist\]#si";
142: $replacement[++$i] = '<ol class="tcecode">\1</ol>';
143:
144:
145: $pattern[++$i] = "#\[olist=1\](.*?)\[/olist\]#si";
146: $replacement[++$i] = '<ol class="tcecode" style="list-style-type:arabic-numbers">\1</ol>';
147:
148:
149: $pattern[++$i] = "#\[olist=a\](.*?)\[/olist\]#si";
150: $replacement[++$i] = '<ol class="tcecode" style="list-style-type:lower-alpha">\1</ol>';
151:
152:
153: $pattern[++$i] = "#\[li\](.*?)\[/li\]#si";
154: $replacement[++$i] = '<li class="tcecode">\1</li>';
155:
156:
157:
158:
159: $pattern[++$i] = "#\[color=(.*?)\](.*?)\[/color\]#si";
160: $replacement[++$i] = '<span style="color:\1">\2</span>';
161:
162:
163:
164:
165: $pattern[++$i] = "#\[bgcolor=(.*?)\](.*?)\[/bgcolor\]#si";
166: $replacement[++$i] = '<span style="background-color:\1">\2</span>';
167:
168:
169: $pattern[++$i] = "#\[font=(.*?)\](.*?)\[/font\]#si";
170: $replacement[++$i] = '<span style="font-family:\1">\2</span>';
171:
172:
173:
174:
175: $pattern[++$i] = "#\[size=([+\-]?[0-9a-z\-]+[%]?)\](.*?)\[/size\]#si";
176: $replacement[++$i] = '<span style="font-size:\1">\2</span>';
177:
178: $newtext = preg_replace($pattern, $replacement, $newtext);
179:
180:
181: $newtext = preg_replace("'(\r\n|\n|\r)'", '<br />', $newtext);
182: $newtext = str_replace('<br /><li', '<li', $newtext);
183: $newtext = str_replace('</li><br />', '</li>', $newtext);
184: $newtext = str_replace('<br /><param', '<param', $newtext);
185:
186:
187:
188:
189: return ($newtext);
190: }
191:
192: 193: 194: 195: 196: 197:
198: function F_latex_callback($matches)
199: {
200: require_once('../../shared/config/tce_latex.php');
201:
202: $latex = unhtmlentities($matches[1]);
203: $latex = str_replace(">", '>', $latex);
204: $latex = str_replace("<", '<', $latex);
205: $dr = 3;
206:
207: $filename = K_LATEX_IMG_PREFIX.md5($latex);
208: $imgpath = K_LATEX_PATH_PICTURE.$filename;
209: $imgurl = false;
210: $error = '';
211:
212: if (is_file($imgpath.'.'.K_LATEX_IMG_FORMAT)) {
213: $imgurl = K_LATEX_PATH_PICTURE_HTTPD.$filename.'.'.K_LATEX_IMG_FORMAT;
214: } else {
215:
216: if (strlen($latex) > K_LATEX_MAX_LENGHT) {
217: $error = 'the formula is too long';
218: } elseif (preg_match('/(include|def|command|loop|repeat|open|toks|output|input|catcode|name|[\^]{2}|\\\\every|\\\\errhelp|\\\\errorstopmode|\\\\scrollmode|\\\\nonstopmode|\\\\batchmode|\\\\read|\\\\write|csname|\\\\newhelp|\\\\uppercase|\\\\lowercase|\\\\relax|\\\\aftergroup|\\\\afterassignment|\\\\expandafter|\\\\noexpand|\\\\special)/i', $latex) > 0) {
219: $error = 'invalid command';
220: } else {
221:
222: $ltx = '\nonstopmode'."\n";
223: $ltx .= '\documentclass{'.K_LATEX_CLASS.'}'."\n";
224: $ltx .= '\usepackage[T1]{fontenc}'."\n";
225: $ltx .= '\usepackage{amsmath,amsfonts,amssymb,wasysym,latexsym,marvosym,txfonts}'."\n";
226: $ltx .= '\usepackage[pdftex]{color}'."\n";
227: $ltx .= '\pagestyle{empty}'."\n";
228: $ltx .= '\begin{document}'."\n";
229: $ltx .= '\fontsize{'.K_LATEX_FONT_SIZE.'}{'.(2 * K_LATEX_FONT_SIZE).'}'."\n";
230: $ltx .= '\selectfont'."\n";
231: $ltx .= '\color{black}'."\n";
232: $ltx .= '\pagecolor{white}'."\n";
233: $ltx .= '$'.$latex.'$'."\n";
234: $ltx .= '\end{document}'."\n";
235: if (file_put_contents($imgpath.'.tex', $ltx) === false) {
236: $error = 'unable to write on the cache folder';
237: } else {
238: $cmd = 'cd '.K_LATEX_PATH_PICTURE.' && '.K_LATEX_PDFLATEX.' '.$imgpath.'.tex';
239: $sts = exec($cmd, $out, $ret);
240: if (!$sts) {
241: $error = implode("\n", $out);
242: } else {
243:
244: $cmd = 'cd '.K_LATEX_PATH_PICTURE.' && '.K_LATEX_PATH_CONVERT.' -density '.(K_LATEX_FORMULA_DENSITY * $dr).' -trim +repage '.$imgpath.'.pdf -depth 8 -quality 100 '.$imgpath.'.'.K_LATEX_IMG_FORMAT;
245: unset($out);
246: $sts = exec($cmd, $out, $ret);
247: if ($ret != 0) {
248: $error = implode("\n", $out);
249: } else {
250: $imsize = @getimagesize($imgpath.'.'.K_LATEX_IMG_FORMAT);
251: list($w, $h) = $imsize;
252: if ((($w / $dr) > K_LATEX_MAX_WIDTH) or (($h / $dr) > K_LATEX_MAX_HEIGHT)) {
253: $error = 'image size exceed limits';
254: } else {
255: $imgurl = K_LATEX_PATH_PICTURE_HTTPD.$filename.'.'.K_LATEX_IMG_FORMAT;
256: }
257: }
258: }
259: }
260:
261: $tmpext = array('tex', 'aux', 'log', 'pdf');
262: foreach ($tmpext as $ext) {
263: if (file_exists($imgpath.'.'.$ext)) {
264: @unlink($imgpath.'.'.$ext);
265: }
266: }
267: }
268: }
269: if ($imgurl === false) {
270: $newtext = '[LaTeX: ERROR '.$error.']';
271: } else {
272:
273: $alt_latex = '[LaTeX]'."\n".htmlentities($latex, ENT_QUOTES);
274: $replaceTable = array("\r" => ' ', "\n" => ' ');
275: $alt_latex = strtr($alt_latex, $replaceTable);
276:
277: $imsize = @getimagesize($imgpath.'.'.K_LATEX_IMG_FORMAT);
278: list($w, $h) = $imsize;
279: $newtext = '<img src="'.$imgurl.'" alt="'.$alt_latex.'" class="tcecode" width="'.round($w / $dr).'" height="'.round($h / $dr).'" />';
280: }
281: return $newtext;
282: }
283:
284: 285: 286: 287: 288: 289:
290: function F_mathml_callback($matches)
291: {
292: $mathml_tags = '<abs><and><annotation><annotation-xml><apply><approx><arccos><arccosh><arccot><arccoth><arccsc><arccsch><arcsec><arcsech><arcsin><arcsinh><arctan><arctanh><arg><bind><bvar><card><cartesianproduct><cbytes><ceiling><cerror><ci><cn><codomain><complexes><compose><condition><conjugate><cos><cosh><cot><coth><cs><csc><csch><csymbol><curl><declare><degree><determinant><diff><divergence><divide><domain><domainofapplication><el><emptyset><eq><equivalent><eulergamma><exists><exp><exponentiale><factorial><factorof><false><floor><fn><forall><gcd><geq><grad><gt><ident><image><imaginary><imaginaryi><implies><in><infinity><int><integers><intersect><interval><inverse><lambda><laplacian><lcm><leq><limit><list><ln><log><logbase><lowlimit><lt><maction><malign><maligngroup><malignmark><malignscope><math><matrix><matrixrow><max><mean><median><menclose><merror><mfenced><mfrac><mfraction><mglyph><mi><min><minus><mlabeledtr><mlongdiv><mmultiscripts><mn><mo><mode><moment><momentabout><mover><mpadded><mphantom><mprescripts><mroot><mrow><ms><mscarries><mscarry><msgroup><msline><mspace><msqrt><msrow><mstack><mstyle><msub><msubsup><msup><mtable><mtd><mtext><mtr><munder><munderover><naturalnumbers><neq><none><not><notanumber><note><notin><notprsubset><notsubset><or><otherwise><outerproduct><partialdiff><pi><piece><piecewise><plus><power><primes><product><prsubset><quotient><rationals><real><reals><reln><rem><root><scalarproduct><sdev><sec><sech><selector><semantics><sep><set><setdiff><share><sin><sinh><subset><sum><tan><tanh><tendsto><times><transpose><true><union><uplimit><variance><vector><vectorproduct><xor>';
293:
294: $mathml = unhtmlentities($matches[1]);
295: $mathml = str_replace(">", '>', $mathml);
296: $mathml = str_replace("<", '<', $mathml);
297:
298: $mathml = strip_tags($mathml, $mathml_tags);
299: $mathml = preg_replace("/[\n\r\s]+/", ' ', $mathml);
300: $mathml = trim($mathml);
301: if (strpos($mathml, '<math') !== 0) {
302:
303: $mathml = '<math xmlns="http://www.w3.org/1998/Math/MathML">'.$mathml.'</math>';
304: }
305: return $mathml;
306: }
307:
308: 309: 310: 311: 312: 313:
314: function F_objects_callback($matches)
315: {
316: $width = 0;
317: $height = 0;
318: $alt = '';
319: if (isset($matches[3]) and ($matches[3] > 0)) {
320: $width = $matches[3];
321: }
322: if (isset($matches[4]) and ($matches[4] > 0)) {
323: $height = $matches[4];
324: }
325: if (isset($matches[5]) and (!empty($matches[5]))) {
326: $alt = F_tcecodeToTitle($matches[5]);
327: }
328: return F_objects_replacement($matches[1], $matches[2], $width, $height, $alt);
329: }
330:
331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341:
342: function F_objects_replacement($name, $extension, $width = 0, $height = 0, $alt = '', &$maxwidth = 0, &$maxheight = 0)
343: {
344: require_once('../config/tce_config.php');
345: global $l, $db;
346: $filename = $name.'.'.$extension;
347: $extension = strtolower($extension);
348: $htmlcode = '';
349: switch ($extension) {
350: case 'gif':
351: case 'jpg':
352: case 'jpeg':
353: case 'png':
354: case 'svg': {
355: $htmlcode = '<img src="'.K_PATH_URL_CACHE.$filename.'"';
356: if (!empty($alt)) {
357: $htmlcode .= ' alt="'.$alt.'"';
358: } else {
359: $htmlcode .= ' alt="image:'.$filename.'"';
360: }
361: $imsize = @getimagesize(K_PATH_CACHE.$filename);
362: if ($imsize !== false) {
363: list($pixw, $pixh) = $imsize;
364: if (($width <= 0) and ($height <= 0)) {
365:
366: $width = $pixw;
367: $height = $pixh;
368: } elseif ($width <= 0) {
369: $width = $height * $pixw / $pixh;
370: } elseif ($height <= 0) {
371: $height = $width * $pixh / $pixw;
372: }
373: }
374: $ratio = 1;
375: if (($width > 0) and ($height > 0)) {
376: $ratio = $width / $height;
377: }
378:
379: if (($maxwidth > 0) and ($width > $maxwidth)) {
380: $width = $maxwidth;
381: $height = round($width / $ratio);
382: $maxheight = min($maxheight, $height);
383: }
384: if (($maxheight > 0) and ($height > $maxheight)) {
385: $height = $maxheight;
386: $width = round($height * $ratio);
387: }
388:
389: if ($width > 0) {
390: $htmlcode .= ' width="'.$width.'"';
391: }
392: if ($height > 0) {
393: $htmlcode .= ' height="'.$height.'"';
394: }
395: $htmlcode .= ' class="tcecode" />';
396: if ($imsize !== false) {
397: $maxwidth = $pixw;
398: $maxheight = $pixh;
399: }
400: break;
401: }
402: default: {
403: include('../../shared/config/tce_mime.php');
404: if (isset($mime[$extension])) {
405: $htmlcode = '<object type="'.$mime[$extension].'" data="'.K_PATH_URL_CACHE.$filename.'"';
406: if ($width >0) {
407: $htmlcode .= ' width="'.$width.'"';
408: } elseif ($maxwidth > 0) {
409: $htmlcode .= ' width="'.$maxwidth.'"';
410: }
411: if ($height >0) {
412: $htmlcode .= ' height="'.$height.'"';
413: } elseif ($maxheight > 0) {
414: $htmlcode .= ' height="'.$maxheight.'"';
415: }
416: $htmlcode .= '>';
417: $htmlcode .= '<param name="type" value="'.$mime[$extension].'" />';
418: $htmlcode .= '<param name="src" value="'.K_PATH_URL_CACHE.$filename.'" />';
419: $htmlcode .= '<param name="filename" value="'.K_PATH_URL_CACHE.$filename.'" />';
420: if ($width > 0) {
421: $htmlcode .= '<param name="width" value="'.$width.'" />';
422: } elseif ($maxwidth > 0) {
423: $htmlcode .= '<param name="width" value="'.$maxwidth.'" />';
424: }
425: if ($height > 0) {
426: $htmlcode .= '<param name="height" value="'.$height.'" />';
427: } elseif ($maxheight > 0) {
428: $htmlcode .= '<param name="height" value="'.$maxheight.'" />';
429: }
430: if (!empty($alt)) {
431: $htmlcode .= ''.$alt.'';
432: } else {
433: $htmlcode .= '['.$mime[$extension].']:'.$filename.'';
434: }
435: $htmlcode .= '</object>';
436: } else {
437: $htmlcode = '[ERROR - UNKNOW MIME TYPE FOR: '.$extension.']';
438: }
439: break;
440: }
441: }
442: return $htmlcode;
443: }
444:
445: 446: 447: 448: 449:
450: function F_remove_tcecode($str)
451: {
452: $str = preg_replace("'\[object\](.*?)\[/object([^\]]*?)\]'si", '[OBJ]', $str);
453: $str = preg_replace("'\[img([^\]]*?)\](.*?)\[/img\]'si", '[IMG]', $str);
454: $str = preg_replace("'\[code\](.*?)\[/code\]'si", '\1', $str);
455: $str = preg_replace("'\[b\](.*?)\[/b\]'si", '\1', $str);
456: $str = preg_replace("'\[i\](.*?)\[/i\]'si", '\1', $str);
457: $str = preg_replace("'\[s\](.*?)\[/s\]'si", '\1', $str);
458: $str = preg_replace("'\[u\](.*?)\[/u\]'si", '\1', $str);
459: $str = preg_replace("'\[o\](.*?)\[/o\]'si", '\1', $str);
460: $str = preg_replace("'\[color([^\]]*?)\](.*?)\[/color\]'si", '\2', $str);
461: $str = preg_replace("'\[bgcolor([^\]]*?)\](.*?)\[/bgcolor\]'si", '\2', $str);
462: $str = preg_replace("'\[font([^\]]*?)\](.*?)\[/font\]'si", '\2', $str);
463: $str = preg_replace("'\[size([^\]]*?)\](.*?)\[/size\]'si", '\2', $str);
464: $str = preg_replace("'\[small\](.*?)\[/small\]'si", '\1', $str);
465: $str = preg_replace("'\[sub\](.*?)\[/sub\]'si", '\1', $str);
466: $str = preg_replace("'\[sup\](.*?)\[/sup\]'si", '\1', $str);
467: $str = preg_replace("'\[url([^\]]*?)\](.*?)\[/url\]'si", '\2', $str);
468: $str = preg_replace("'\[li\](.*?)\[/li\]'si", ' * \1', $str);
469: $str = preg_replace("'\[\*\](.*?)\n'i", ' * \1', $str);
470: $str = preg_replace("'\[ulist\](.*?)\[/ulist\]'si", '\1', $str);
471: $str = preg_replace("'\[olist([^\]]*?)\](.*?)\[/olist\]'si", '\2', $str);
472: $str = preg_replace("'\[tex\](.*?)\[/tex\]'si", '[TEX]', $str);
473: return $str;
474: }
475:
476: 477: 478: 479: 480:
481: function F_tcecodeToLine($str)
482: {
483: $str = preg_replace("'\[object\](.*?)\[/object([^\]]*?)\]'si", '[OBJ]', $str);
484: $str = preg_replace("'\[img([^\]]*?)\](.*?)\[/img\]'si", '[IMG]', $str);
485: $str = preg_replace("'\[code\](.*?)\[/code\]'si", '\1', $str);
486: $str = preg_replace("'\[li\](.*?)\[/li\]'si", ' * \1', $str);
487: $str = preg_replace("'\[\*\](.*?)\n'i", ' * \1', $str);
488: $str = preg_replace("'\[ulist\](.*?)\[/ulist\]'si", '\1', $str);
489: $str = preg_replace("'\[olist([^\]]*?)\](.*?)\[/olist\]'si", '\2', $str);
490: $str = preg_replace("'\[url([^\]]*?)\](.*?)\[/url\]'si", '\2', $str);
491: $str = preg_replace("'\[tex\](.*?)\[/tex\]'si", '[TEX]', $str);
492: $str = F_compact_string($str);
493: $str = F_decode_tcecode($str);
494: $str = F_compact_string($str);
495: if (strlen($str) > K_QUESTION_LINE_MAX_LENGTH) {
496: $str = F_substrHTML($str, K_QUESTION_LINE_MAX_LENGTH, 20).' ...';
497: }
498: return $str;
499: }
500:
501: 502: 503: 504: 505:
506: function F_tcecodeToTitle($str)
507: {
508: require_once('../config/tce_config.php');
509: global $l;
510: $str = F_remove_tcecode($str);
511: $str = F_compact_string($str);
512: $str = htmlspecialchars($str, ENT_COMPAT, $l['a_meta_charset']);
513: return $str;
514: }
515:
516: 517: 518: 519: 520: 521: 522: 523:
524: function F_substrHTML($htmltext, $min_length = 100, $offset_length = 20)
525: {
526:
527: $tag_counter = 0;
528: $quotes_on = false;
529:
530: if (strlen($htmltext) > $min_length) {
531:
532: $c = 0;
533: for ($i = 0; $i < strlen($htmltext); $i++) {
534:
535: $current_char = substr($htmltext, $i, 1);
536: if ($i < strlen($htmltext) - 1) {
537: $next_char = substr($htmltext, $i + 1, 1);
538: } else {
539: $next_char = '';
540: }
541:
542: if (!$quotes_on) {
543:
544: if ($current_char == '<') {
545: if ($next_char == '/') {
546: $tag_counter += 1;
547: } else {
548: $tag_counter += 3;
549: }
550: }
551:
552: if (($current_char == '/') and ($tag_counter != 0)) {
553: $tag_counter -= 2;
554: }
555:
556: if ($current_char == '>') {
557: $tag_counter -= 1;
558: }
559:
560: if ($current_char == '"') {
561: $quotes_on = true;
562: }
563: } else {
564:
565: if ($current_char == '"') {
566: $quotes_on = false;
567: }
568: }
569:
570: if (($tag_counter == 2) or ($tag_counter == 0)) {
571: $c++;
572: }
573:
574:
575: if (($c > $min_length - $offset_length) and ($tag_counter == 0) and ($next_char == ' ')) {
576: $htmltext = substr($htmltext, 0, $i + 1);
577: return $htmltext;
578: }
579: }
580: }
581: return $htmltext;
582: }
583:
584:
585:
586:
587: