make diogenes depends compatible with sarge
[diogenes.git] / include / diogenes.text.inc.php
... / ...
CommitLineData
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
21require_once 'diogenes/diogenes.misc.inc.php';
22
23/** Protect a text block and encode it as base64.
24 */
25function 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 */
60function 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 */
79function htmlProtectFromTextism($input)
80{
81 return textProtectTag("<protect>", "<\/protect>", "{NOP:", ":NOP}", $input);
82}
83
84
85/** Restore HTML code that was protected from Textism.
86 */
87function htmlUnprotectFromTextism($input)
88{
89 $input = textUnprotectTags("<p>\s*{NOP:", ":NOP}\s*<\/p>", $input);
90 $input = textUnprotectTags("{NOP:", ":NOP}", $input);
91 return preg_replace('/<\/?protect>/', "", $input);
92}
93
94
95/** Protect PHP code.
96 */
97function phpProtect($input)
98{
99 return textProtectTag("<\?php", "\?>", "{PHP:", ":PHP}", $input);
100}
101
102
103/** Unprotect PHP code.
104 */
105function phpUnprotect($input)
106{
107 return textUnprotectTags("{PHP:", ":PHP}", $input);
108}
109
110
111/** Convert XHTML-compliant tags to plain HTML.
112 */
113function xhtmlToHtml($input)
114{
115 return html_accent(preg_replace("/<(br|img|input|p)( [^\/]*)?\/>/","<\$1\$2>",$input));
116}
117
118
119/** Restore XHTML-compliant tags.
120 */
121function htmlToXhtml($input)
122{
123 return preg_replace("/<(br|img|input)( [^>]+)?>/","<\$1\$2/>",$input);
124}
125
126
127?>