JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added enc_tab(), fixed metaform php template so it looks right
[wfpl.git] / encode.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file contains basic encodings. These are used by the encoder. You can
24 # specify any template tag to be encoded with this syntax: ~variable.encoding~
25 #
26 # this example: <p>~foo.html~</p>
27 # will encode foo (using enc_html()) before displaying it, so that characters
28 # such as < will display properly.
29
30
31 # encode for putting within double-quotes in SQL
32 function enc_sql($str) {
33         $str = str_replace("\\", "\\\\", $str);
34         $str = str_replace('"', "\\\"", $str);
35         return $str;
36 }
37
38 # encode for output in html. does nothing with whitespace
39 function enc_html($str) {
40         $str = str_replace('&', '&amp;', $str);
41         $str = str_replace('<', '&lt;', $str);
42         $str = str_replace('>', '&gt;', $str);
43         return $str;
44 }
45
46
47 # html attributes (eg  <input value="...."
48 function enc_attr($str) {
49         $str = str_replace('&', '&amp;', $str);
50         $str = str_replace('"', '&quot;', $str);
51         return $str;
52 }
53
54 # this is a stupid hack to work around html's stupid syntax for checkboxes
55 function enc_checked($str) {
56         if($str == 'Yes') {
57                 return '" checked="checked';
58         } else {
59                 return '';
60         }
61 }
62
63 # add a tab at the begining of each non-empty line
64 function enc_tab($str) {
65         $lines = explode("\n", $str);
66         $out = '';
67         foreach($lines as $line) {
68                 if($line) {
69                         $out .= "\t$line";
70                 }
71                 $out .= "\n";
72         }
73
74         # remove the extra newline added above
75         return substr($out, 0, -1);
76 }
77
78 ?>