real fix to the escape problem : quoted werent escaped correctly
[platal.git] / include / platal / smarty.plugins.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., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ function escape_html ()
23
24 /**
25 * default smarty plugin, used to auto-escape dangerous html.
26 *
27 * < --> &lt;
28 * > --> &gt;
29 * " --> &quot;
30 * & not followed by some entity --> &amp;
31 */
32 function escape_html($string)
33 {
34 if(is_string($string)) {
35 $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&apos;');
36 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&amp;" , strtr($string, $transtbl));
37 } else {
38 return $string;
39 }
40 }
41
42 // }}}
43 // {{{ function at_to_globals()
44
45 /**
46 * helper
47 */
48
49 function _to_globals($s) {
50 global $globals;
51 $t = explode('.',$s);
52 if (count($t) == 1) {
53 return var_export($globals->$t[0],true);
54 } else {
55 return var_export($globals->$t[0]->$t[1],true);
56 }
57 }
58
59 /**
60 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
61 */
62
63 function at_to_globals($tpl_source, &$smarty)
64 {
65 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
66 }
67
68 // }}}
69 // {{{ function trimwhitespace
70
71 function trimwhitespace($source, &$smarty)
72 {
73 // Pull out the script blocks
74 preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
75 $_script_blocks = $match[0];
76 $source = preg_replace("!<script[^>]+>.*?</script>!is", '@@@SMARTY:TRIM:SCRIPT@@@', $source);
77
78 // Pull out the pre blocks
79 preg_match_all("!<pre>.*?</pre>!is", $source, $match);
80 $_pre_blocks = $match[0];
81 $source = preg_replace("!<pre>.*?</pre>!is", '@@@SMARTY:TRIM:PRE@@@', $source);
82
83 // Pull out the textarea blocks
84 preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
85 $_textarea_blocks = $match[0];
86 $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is", '@@@SMARTY:TRIM:TEXTAREA@@@', $source);
87
88 // remove all leading spaces, tabs and carriage returns NOT
89 // preceeded by a php close tag.
90 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
91
92 // replace script blocks
93 foreach($_script_blocks as $curr_block) {
94 $source = preg_replace("!@@@SMARTY:TRIM:SCRIPT@@@!", $curr_block, $source, 1);
95 }
96 // replace pre blocks
97 foreach($_pre_blocks as $curr_block) {
98 $source = preg_replace("!@@@SMARTY:TRIM:PRE@@@!",$curr_block,$source,1);
99 }
100 // replace textarea blocks
101 foreach($_textarea_blocks as $curr_block) {
102 $source = preg_replace("!@@@SMARTY:TRIM:TEXTAREA@@@!",$curr_block,$source,1);
103 }
104
105 return $source;
106 }
107
108 // }}}
109 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
110 ?>