JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed file_run to use _main postfix, added enc_url_path(), enc_ddmmyyyhhmm(), allowed...
[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 function enc_jsdq($str) {
31         $str = enc_sql($str);
32         $str = str_replace("\n", "\\n", $str);
33         return str_replace("\r", "\\r", $str);
34 }
35
36 # encode for putting within double-quotes in SQL
37 function enc_sql($str) {
38         $str = str_replace("\\", "\\\\", $str);
39         $str = str_replace('"', "\\\"", $str);
40         return $str;
41 }
42
43 # Encode for output in html. does nothing with whitespace
44 #
45 # Example: <p>~foo.html~</p>
46 function enc_html($str) {
47         $str = str_replace('&', '&amp;', $str);
48         $str = str_replace('<', '&lt;', $str);
49         $str = str_replace('>', '&gt;', $str);
50         return $str;
51 }
52
53 # Encode for output in html. converts \n to <br />
54 #
55 # Example: <p>~foo.html~</p>
56 function enc_htmlbr($str) {
57         $str = enc_html($str);
58         $str = str_replace("\n", "<br />\n", $str);
59         return $str;
60 }
61
62
63 # HTML attribute.
64 #
65 # Example: <input name="foo" value="~foo.attr~">
66 function enc_attr($str) {
67         $str = str_replace('&', '&amp;', $str);
68         $str = str_replace('"', '&quot;', $str);
69         return $str;
70 }
71
72 # URI agument value.
73 #
74 # Example:  <a href="http://example.com?foo=~foo.url_val.attr~">http://example.com?foo=~foo.url_val~</a>
75 function enc_url_val($str) {
76         return rawurlencode($str);
77 }
78
79 # FIXME
80 function enc_url_path($str) {
81         $str = rawurlencode($str);
82         $str = str_replace('%2F', '/', $str);
83         return $str;
84 }
85
86
87 # This is a hack to work around html's stupid syntax for checkboxes.
88 #
89 # Place the template marker just before a " somewhere.
90 #
91 # Example: <input type="checkbox" name="foo~foo.checked~">
92 function enc_checked($str) {
93         if($str == 'Yes') {
94                 return '" checked="checked';
95         } else {
96                 return '';
97         }
98 }
99
100 # add a tab at the begining of each non-empty line
101 function enc_tab($str) {
102         $lines = explode("\n", $str);
103         $out = '';
104         foreach($lines as $line) {
105                 if($line) {
106                         $out .= "\t$line";
107                 }
108                 $out .= "\n";
109         }
110
111         # remove the extra newline added above
112         return substr($out, 0, -1);
113 }
114
115 function enc_upper($str) {
116         return strtoupper($str);
117 }
118
119 function enc_ddmmyyyyhhmm($seconds) {
120         return date('m/d/Y g:ia', (int)$seconds);
121 }
122
123
124 # display <option>s
125 function enc_states($str) {
126         $states_assoc = array("AL" => "Alabama", "AK" => "Alaska", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "FL" => "Florida", "GA" => "Georgia", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MD" => "Maryland", "MA" => "Massachusetts", "MI" => "Michigan", "MN" => "Minnesota", "MS" => "Mississippi", "MO" => "Missouri", "MT" => "Montana", "NE" => "Nebraska", "NV" => "Nevada", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NY" => "New York", "NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VA" => "Virginia", "WA" => "Washington", "DC" => "Washington, DC", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming");
127         $ret = '';
128
129         return encode_options($str, $states_assoc, $use_keys = true);
130 }
131
132
133 define('PULLDOWN_ARRAY', 0); define('PULLDOWN_HASH', 1); define('PULLDOWN_2D', 2);
134
135 function pulldown_options_to_hash($options, $keys_from) {
136         # convert other types of input to value=>display hash
137         switch($keys_from) {
138                 case PULLDOWN_HASH:
139                         return $options;
140                 case PULLDOWN_ARRAY:
141                         $new_options = array();
142                         foreach($options as $opt) {
143                                 $new_options[$opt] = $opt;
144                         }
145                         return $new_options;
146                 break;
147                 case PULLDOWN_2D:
148                         $new_options = array();
149                         foreach($options as $opt) {
150                                 $new_options[$opt[0]] = $opt[1];
151                         }
152                         return $new_options;
153                 break;
154                 default:
155                         die('unknown value: "' . print_r($keys_from) . '" passed in $keys_from parameter');
156         }
157 }
158
159
160 # call this function before you run() the template so enc_options() knows what
161 # to do
162 #
163 # Parameters:
164 #
165 #   name: the name of the html control
166 #
167 #   options: an array of options to display in the pulldown/selectbox
168 #
169 #   keys_from: Set to one of:
170 #        PULLDOWN_ARRAY: (default) values of $options are displayd and posted
171 #        PULLDOWN_HASH: values of $options are display, keys are posted
172 #        PULLDOWN_2D: $options is a 2 dimensional array.
173 #                     $options[0][1] is displayed, $options[0][0] is posted.
174 #                     $options[1][1] is displayed, $options[1][0] is posted.
175 #
176 #   multiple: UNTESTED set to true for multiple-select boxes. 
177
178 function pulldown($name, $options, $keys_from = PULLDOWN_ARRAY, $multiple = false) {
179         $options = pulldown_options_to_hash($options, $keys_from);
180         $GLOBALS[$name . '_options'] = array();
181         $GLOBALS[$name . '_options']['options'] = $options;
182         $GLOBALS[$name . '_options']['multiple'] = $multiple;
183 }
184
185 # output a bunch of <option> tags
186 function enc_options($values, $name) {
187         if(!isset($GLOBALS[$name . '_options'])) {
188                 die('pulldown() must be called before this template can be run. See code/wfpl/encode.php');
189         }
190         if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
191                 $values = explode(', ', $values);
192         }
193         return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_HASH);
194 }
195
196 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
197 #
198 # Parameters:
199 #
200 #    selected: can be a string or (for multiple-selects) an array
201 #
202 #    options, keys_from: see documentation for pulldown() above
203 function encode_options($selected, $options, $keys_from) {
204         if(!is_array($selected)) {
205                 $selected = array($selected);
206         }
207
208         $options = pulldown_options_to_hash($options, $keys_from);
209
210         $out = '';
211         foreach($options as $value => $display) {
212                 $out .= '<option';
213
214                 if(in_array($value, $selected, $strict = true)) {
215                         $out .= ' selected="selected"';
216                 }
217
218                 if($value !== $display) {
219                         $out .= ' value="';
220                         $out .= enc_attr($value);
221                         $out .= '"';
222                 }
223                         
224                 $out .= '>';
225
226                 $out .= enc_html($display);
227
228                 $out .= "</option>\n";
229         }
230
231         return $out;
232 }
233
234 ?>