1d5bef0b04c9a2ede9aa86486d3bc4fa79ef148e
[diogenes.git] / include / diogenes.compose.inc.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 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
22 require_once 'diogenes/diogenes.misc.inc.php';
23
24 /** Protect PHP code.
25 */
26 function phpProtect($input)
27 {
28 $splits = preg_split("/(<\?php|\?>)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
29 $output = "";
30
31 while ($block = array_shift($splits)) {
32 if ($block == "<?php") {
33 $code = array_shift($splits);
34 $end = array_shift($splits);
35 if ($end != "?>")
36 die("phpProtect : parse error");
37 $output .= "{PHP:".base64_encode($code).":PHP}";
38 } else {
39 $output .= $block;
40 }
41 }
42
43 return $output;
44 }
45
46
47 /** Unprotect PHP code.
48 */
49 function phpUnprotect($input)
50 {
51 $splits = preg_split("/({PHP:.+:PHP})/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
52 $output = "";
53
54 foreach ($splits as $block) {
55 if (preg_match("/{PHP:(.+):PHP}/",$block,$match)) {
56 $output .= "<?php".base64_decode($match[1])."?>";
57 } else {
58 $output .= $block;
59 }
60 }
61
62 return $output;
63 }
64
65
66 /** Convert XHTML-compliant tags to plain HTML.
67 */
68 function xhtmlToHtml($input)
69 {
70 return html_accent(preg_replace("/<(br|img|input|p)( [^\/]*)?\/>/","<\$1\$2>",$input));
71 }
72
73
74 /** Restore XHTML-compliant tags.
75 */
76 function htmlToXhtml($input)
77 {
78 return preg_replace("/<(br|img|input)( [^>]+)?>/","<\$1\$2/>",$input);
79 }
80
81 ?>