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