7f415d6434b43d5b26bd86e0fa07a2596bb31dd9
[diogenes.git] / include / diogenes.text.inc.php
1 <?php
2 /*
3 * Copyright (C) 2003-2006 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 require_once 'diogenes/diogenes.misc.inc.php';
22
23 /** Protect a text block and encode it as base64.
24 */
25 function textProtectTag($tag_open, $tag_close, $prot_open, $prot_close, $input)
26 {
27 $splits = preg_split("/($tag_open|$tag_close)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
28
29 $output = "";
30 $depth = 0;
31 while ($block = array_shift($splits)) {
32 if (preg_match("/^$tag_open$/", $block)) {
33 if ($depth == 0) {
34 $save = "";
35 }
36 $save .= $block;
37 $depth++;
38 } else if ($depth > 0) {
39 $save .= $block;
40 if (preg_match("/^$tag_close$/", $block))
41 {
42 $depth--;
43 if ($depth == 0)
44 {
45 $output .= $prot_open.base64_encode($save).$prot_close;
46 $save = "";
47 }
48 }
49 } else {
50 $output .= $block;
51 }
52 }
53
54 return $output;
55 }
56
57
58 /** Unprotect base64 blocks.
59 */
60 function textUnprotectTags($prot_open, $prot_close, $input)
61 {
62 $splits = preg_split("/($prot_open.+$prot_close)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
63 $output = "";
64
65 foreach ($splits as $block) {
66 if (preg_match("/^$prot_open(.+)$prot_close$/", $block, $match)) {
67 $output .= base64_decode($match[1]);
68 } else {
69 $output .= $block;
70 }
71 }
72
73 return $output;
74 }
75
76
77 /** Protect HTML code from Textism.
78 */
79 function htmlProtectFromTextism($input)
80 {
81 return textProtectTag("<table\s*?[^>]*>", "<\/table>", "{NOP:", ":NOP}", $input);
82 }
83
84
85 /** Restore HTML code that was protected from Textism.
86 */
87 function htmlUnprotectFromTextism($input)
88 {
89 return textUnprotectTags("{NOP:", ":NOP}", $input);
90 }
91
92
93 /** Protect PHP code.
94 */
95 function phpProtect($input)
96 {
97 return textProtectTag("<\?php", "\?>", "{PHP:", ":PHP}", $input);
98 }
99
100
101 /** Unprotect PHP code.
102 */
103 function phpUnprotect($input)
104 {
105 return textUnprotectTags("{PHP:", ":PHP}", $input);
106 }
107
108
109 /** Convert XHTML-compliant tags to plain HTML.
110 */
111 function xhtmlToHtml($input)
112 {
113 return html_accent(preg_replace("/<(br|img|input|p)( [^\/]*)?\/>/","<\$1\$2>",$input));
114 }
115
116
117 /** Restore XHTML-compliant tags.
118 */
119 function htmlToXhtml($input)
120 {
121 return preg_replace("/<(br|img|input)( [^>]+)?>/","<\$1\$2/>",$input);
122 }
123
124
125 ?>