fix Textile protection / unprotection
[diogenes.git] / include / diogenes.text.inc.php
CommitLineData
6855525e
JL
1<?php
2/*
e2e369d6 3 * Copyright (C) 2003-2006 Polytechnique.org
6855525e
JL
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
6855525e
JL
21require_once 'diogenes/diogenes.misc.inc.php';
22
e2e369d6 23/** Protect a text block and encode it as base64.
6855525e 24 */
e2e369d6 25function textProtectTag($tag_open, $tag_close, $prot_open, $prot_close, $input)
6855525e 26{
e2e369d6
JL
27 $splits = preg_split("/($tag_open|$tag_close)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
28
6855525e 29 $output = "";
e2e369d6 30 $depth = 0;
6855525e 31 while ($block = array_shift($splits)) {
e2e369d6
JL
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 }
6855525e
JL
49 } else {
50 $output .= $block;
51 }
52 }
53
54 return $output;
55}
56
57
e2e369d6 58/** Unprotect base64 blocks.
6855525e 59 */
e2e369d6 60function textUnprotectTags($prot_open, $prot_close, $input)
6855525e 61{
e2e369d6 62 $splits = preg_split("/($prot_open.+$prot_close)/",$input,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
6855525e
JL
63 $output = "";
64
65 foreach ($splits as $block) {
e2e369d6
JL
66 if (preg_match("/^$prot_open(.+)$prot_close$/", $block, $match)) {
67 $output .= base64_decode($match[1]);
6855525e
JL
68 } else {
69 $output .= $block;
70 }
71 }
72
73 return $output;
74}
75
76
e2e369d6
JL
77/** Protect HTML code from Textism.
78 */
79function htmlProtectFromTextism($input)
80{
a3299126 81 return textProtectTag("<protect>", "<\/protect>", "{NOP:", ":NOP}", $input);
e2e369d6
JL
82}
83
84
85/** Restore HTML code that was protected from Textism.
86 */
87function htmlUnprotectFromTextism($input)
88{
a3299126
JL
89 $input = textUnprotectTags("<p>\s*{NOP:", ":NOP}\s*<\/p>", $input);
90 $input = textUnprotectTags("{NOP:", ":NOP}", $input);
91 return preg_replace('/<\/?protect>/', "", $input);
e2e369d6
JL
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
6855525e
JL
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
e2e369d6 126
6855525e 127?>