3 * Copyright (C) 2003-2006 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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
21 require_once 'diogenes/diogenes.misc.inc.php';
23 /** Protect a text block and encode it as base64.
25 function textProtectTag($tag_open, $tag_close, $prot_open, $prot_close, $input)
27 $splits = preg_split("/($tag_open|$tag_close)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
31 while ($block = array_shift($splits)) {
32 if (preg_match("/^$tag_open$/", $block)) {
38 } else if ($depth > 0) {
40 if (preg_match("/^$tag_close$/", $block))
45 $output .= $prot_open.base64_encode($save).$prot_close;
58 /** Unprotect base64 blocks.
60 function textUnprotectTags($prot_open, $prot_close, $input)
62 $splits = preg_split("/($prot_open.+$prot_close)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
65 foreach ($splits as $block) {
66 if (preg_match("/^$prot_open(.+)$prot_close$/", $block, $match)) {
67 $output .= base64_decode($match[1]);
77 /** Protect HTML code from Textism.
79 function htmlProtectFromTextism($input)
81 return textProtectTag("<protect>", "<\/protect>", "{NOP:", ":NOP}", $input);
85 /** Restore HTML code that was protected from Textism.
87 function htmlUnprotectFromTextism($input)
89 $input = textUnprotectTags("<p>\s*{NOP:", ":NOP}\s*<\/p>", $input);
90 $input = textUnprotectTags("{NOP:", ":NOP}", $input);
91 return preg_replace('/<\/?protect>/', "", $input);
97 function phpProtect($input)
99 return textProtectTag("<\?php", "\?>", "{PHP:", ":PHP}", $input);
103 /** Unprotect PHP code.
105 function phpUnprotect($input)
107 return textUnprotectTags("{PHP:", ":PHP}", $input);
111 /** Convert XHTML-compliant tags to plain HTML.
113 function xhtmlToHtml($input)
115 return html_accent(preg_replace("/<(br|img|input|p)( [^\/]*)?\/>/","<\$1\$2>",$input));
119 /** Restore XHTML-compliant tags.
121 function htmlToXhtml($input)
123 return preg_replace("/<(br|img|input)( [^>]+)?>/","<\$1\$2/>",$input);