3 # Copyright (C) 2005 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # This file contains basic encodings. These are used by the encoder. You can
20 # specify any template tag to be encoded with this syntax: ~variable encoding~
22 # this example: <p>~foo html~</p>
23 # will encode foo (using enc_html()) before displaying it, so that characters
24 # such as < will display properly.
26 function enc_cap($str) {
31 function enc_jsdq($str) {
33 $str = str_replace("\n", "\\n", $str);
34 return str_replace("\r", "\\r", $str);
37 # encode for putting within double-quotes in SQL
38 function enc_sql($str) {
39 $str = str_replace("\\", "\\\\", $str);
40 $str = str_replace('"', "\\\"", $str);
44 # Encode for output in html. does nothing with whitespace
46 # Example: <p>~foo html~</p>
47 function enc_html($str) {
48 $str = str_replace('&', '&', $str);
49 $str = str_replace('<', '<', $str);
50 $str = str_replace('>', '>', $str);
54 # Encode for output in html. Convert newlines to <br>
56 # Example: <p>~foo htmlbr~</p>
57 function enc_htmlbr($str) {
58 $str = enc_html($str);
59 $str = str_replace("\n", "<br>\n", $str);
63 # Encode for output in html. Preserves newlines and indentation by converting
64 # newlines to <br> and spaces at the begining of lines to
66 # Example: <p>~foo htmlbrtab~</p>
67 function enc_htmlbrtab($str) {
68 $str = enc_htmlbr($str);
69 $space_to_nbsp = create_function('$matches', 'return str_repeat(\' \', strlen($matches[0]) * 2);');
70 $str = preg_replace_callback("|^ *|m", $space_to_nbsp, $str);
74 # Encode for output in html. Spaces converted to and \n to <br>
76 # Example: <option value="12">~foo htmlbrnbsp~</option>
77 function enc_htmlbrnbsp($str) {
78 $str = enc_htmlbr($str);
79 $str = str_replace(' ', ' ', $str);
83 # Encode for output in html. Spaces converted to
85 # Example: <option value="12">~foo htmlnbsp~</option>
86 function enc_htmlnbsp($str) {
87 $str = enc_html($str);
88 $str = str_replace(' ', ' ', $str);
95 # Example: <input name="foo" value="~foo attr~">
96 function enc_attr($str) {
97 $str = str_replace('&', '&', $str);
98 $str = str_replace('"', '"', $str);
104 # Example: <a href="http://example.com?foo=~foo url_val attr~">http://example.com?foo=~foo url_val~</a>
105 function enc_url_val($str) {
106 return rawurlencode($str);
110 function enc_url_path($str) {
111 $str = rawurlencode($str);
112 $str = str_replace('%2F', '/', $str);
117 # This is a hack to work around html's stupid syntax for checkboxes.
119 # Place the template marker just before a " somewhere.
121 # Example: <input type="checkbox" name="foo~foo checked~">
122 function enc_checked($str) {
123 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
124 return '" checked="checked';
130 # normally, checkboxes values from get/post to 0 or 1, and stored in the database this way. enc_yesno() can be used in your templates to display this as "Yes" or "No".
131 # Example template: Subscribe to mailing list?: ~subscribe yesno~
132 function enc_yesno($str) {
133 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
141 # add a tab at the begining of each line
142 function enc_tab($str) {
143 if('' . $str === '') {
146 return "\t" . implode("\n\t", explode("\n", $str));
149 function enc_uppercase($str) {
150 return strtoupper($str);
152 function enc_upper($str) { # depricated
153 return enc_uppercase($str);
156 function enc_lowercase($str) {
157 return strtolower($str);
160 # pass date in the form 2008-05-23
161 # ercodes date as 05/23/2008
162 function enc_mmddyyyy($yyyy_mm_dd) {
163 if($yyyy_mm_dd == '') {
166 if(strlen($yyyy_mm_dd) != 10) {
167 return date('m/d/Y');
169 return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4);
172 # depricated. call enc_mmddyyyy() instead
173 function enc_mdy($str) {
174 return enc_mmddyyyy($str);
177 # pass unix timestamp or "2012-12-20 22:23:34"
178 function enc_mmddyyyyhhmm($str) {
179 if(is_numeric($str)) {
180 return date('m/d/Y g:ia', (int)$str);
182 return enc_mmddyyyy(substr($str, 0, 10)) . substr($str, 10, 6);
186 # takes decimal number of hours
188 function enc_hhmm($str) {
189 if(strlen($str) == 0) {
192 $hours = floor($str);
193 $minutes = round(($str - $hours) * 60);
194 $str = sprintf("%d:%02d", $hours, $minutes);
198 # takes decimal number of hours
199 # returns hh:mm followed by "am" or "pm" with no space
200 function enc_12hr($str) {
201 if(strlen($str) == 0) {
204 $hours = floor($str);
205 $minutes = round(($str - $hours) * 60);
213 $str = sprintf("%d:%02d", $hours, $minutes);
220 # These are depricated! All but PULLDOWN_HASH still work, but you should update your code.
221 define('PULLDOWN_AUTO', 0); define('PULLDOWN_ARRAY', 1); define('PULLDOWN_HASH', 2); define('PULLDOWN_2D', 3);
224 # call this function before you run() the template so enc_options() knows what
229 # name: the name of the html control
231 # options: an array of options to display in the pulldown/selectbox. Each
232 # element can be either a string, or an array with two elements (first the
233 # value to post, and second the value to display in the pulldown)
235 # multiple: UNTESTED set to true for multiple-select boxes.
237 function pulldown($name, $in_options, $multiple = false) {
238 if($multiple === PULLDOWN_HASH) {
239 die('Webmaster error: PULLDOWN_HASH is depricated. Pass array(a,b) not a=>b');
241 if($multiple !== true) {
242 # Probably due to API change (removing 3rd argument) but don't bother
243 # emitting a warning, because the above warning handles the only
248 foreach($in_options as $option) {
249 if(is_array($option)) {
250 $options[] = $option;
252 $options[] = array($option, $option);
255 $GLOBALS[$name . '_options'] = array(
256 'options' => $options,
257 'multiple' => $multiple);
260 # output a bunch of <option> tags
261 function enc_options($values, $name) {
262 if(!isset($GLOBALS[$name . '_options'])) {
263 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
265 if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
266 $values = explode(', ', $values);
268 return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_2D);
271 # for radios and pulldowns:
273 # returns what the user sees in the pulldown or on the radio button caption
274 function enc_pulled($str, $name) {
275 if(!isset($GLOBALS[$name . '_options'])) {
276 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
278 foreach($GLOBALS[$name . '_options']['options'] as &$kv) {
286 function enc_radio_n($str, $name, $n) {
287 if(!isset($GLOBALS[$name . '_options'])) {
288 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
291 if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
292 die("Template error: pulldown('$name') does not have element # $n");
295 $value = enc_attr($GLOBALS[$name . '_options']['options'][$n][0]);
297 if($str === $value) {
298 $value .= '" checked="checked';
303 function enc_radio_0($str, $name) { return enc_radio_n($str, $name, 0); }
304 function enc_radio_1($str, $name) { return enc_radio_n($str, $name, 1); }
305 function enc_radio_2($str, $name) { return enc_radio_n($str, $name, 2); }
306 function enc_radio_3($str, $name) { return enc_radio_n($str, $name, 3); }
307 function enc_radio_4($str, $name) { return enc_radio_n($str, $name, 4); }
308 function enc_radio_5($str, $name) { return enc_radio_n($str, $name, 5); }
309 function enc_radio_6($str, $name) { return enc_radio_n($str, $name, 6); }
310 function enc_radio_7($str, $name) { return enc_radio_n($str, $name, 7); }
313 function enc_radio_caption_n($str, $name, $n) {
314 if(!isset($GLOBALS[$name . '_options'])) {
315 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
318 if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
319 die("Template error: pulldown('$name') does not have element #$n");
322 return $GLOBALS[$name . '_options']['options'][$n][1];
324 function enc_radio_caption_0($str, $name) { return enc_radio_caption_n($str, $name, 0); }
325 function enc_radio_caption_1($str, $name) { return enc_radio_caption_n($str, $name, 1); }
326 function enc_radio_caption_2($str, $name) { return enc_radio_caption_n($str, $name, 2); }
327 function enc_radio_caption_3($str, $name) { return enc_radio_caption_n($str, $name, 3); }
328 function enc_radio_caption_4($str, $name) { return enc_radio_caption_n($str, $name, 4); }
329 function enc_radio_caption_5($str, $name) { return enc_radio_caption_n($str, $name, 5); }
330 function enc_radio_caption_6($str, $name) { return enc_radio_caption_n($str, $name, 6); }
331 function enc_radio_caption_7($str, $name) { return enc_radio_caption_n($str, $name, 7); }
334 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
338 # selected: can be a string or (for multiple-selects) an array
340 # options, keys_from: see documentation for pulldown() above
341 function encode_options($selected, $options, $keys_from) {
342 if(!is_array($selected)) {
343 $selected = array($selected);
346 if($keys_from != PULLDOWN_2D) {
347 $options = pulldown_options_to_2d($options, $keys_from);
351 foreach($options as $option) {
352 list($value, $display, $arg3) = $option;
355 if($arg3 == 'disabled') {
357 } elseif(in_array($value, $selected)) {
361 if($value !== $display || strpos($value, ' ') !== false) {
363 $out .= enc_attr($value);
369 $out .= enc_htmlnbsp($display);
371 $out .= "</option>\n";
377 $GLOBALS['wfpl_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");
380 function enc_states($str) {
383 return encode_options($str, $GLOBALS['wfpl_states_assoc'], PULLDOWN_HASH);
386 $GLOBALS['wfpl_provinces_assoc'] = array("AB" => "Alberta", "BC" => "British Columbia", "MB" => "Manitoba", "NF" => "Newfoundland", "NB" => "New Brunswick", "NS" => "Nova Scotia", "NT" => "Northwest Territories", "NU" => "Nunavut", "ON" => "Ontario", "PE" => "Prince Edward Island", "QC" => "Quebec", "SK" => "Saskatchewan", "YT" => "Yukon Territory");
389 function enc_provinces($str) {
392 return encode_options($str, $GLOBALS['wfpl_provinces_assoc'], PULLDOWN_HASH);
395 # returns "odd", then "even", then "odd" etc.
396 function enc_evenodd($values, $name) {
397 if(!isset($GLOBALS['wfpl_even_odds'])) {
398 $GLOBALS['wfpl_even_odds'] = array();
401 if($GLOBALS['wfpl_even_odds'][$name]) {
402 $GLOBALS['wfpl_even_odds'][$name] = false;
405 $GLOBALS['wfpl_even_odds'][$name] = true;
410 function enc_image_src($str) {
411 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
415 function enc_image_width($str) {
416 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
420 function enc_image_height($str) {
421 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
425 function enc_thumb_src($str) {
426 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
430 function enc_thumb_width($str) {
431 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
434 function enc_thumb_height($str) {
435 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
439 # example template: Length: ~length html~ day~length s~
440 function enc_s($str) {
448 # turn http/ftp (s) urls into html links (and encode everything for html)
449 # does not encode without protocol (eg "www.foo.com")
450 # does not linkify email addresses
451 function enc_linkify($str) {
454 $pieces = preg_split("/((?:ht|f)tps?:\/\/[^ \,\"\n\r\t<]+)/is", $str, null, PREG_SPLIT_DELIM_CAPTURE);
455 foreach($pieces as $piece) {
457 $ret .= enc_html($piece);
459 $ret .= '<a href="' . enc_attr($piece) . '">' . enc_html($piece) . '</a>';
466 # turns a filename into the unix timestamp of that files modification date
467 function enc_mtime($dummy, $filename) {
468 $stat = stat($filename);
469 if ($stat === false) {
472 return '' . $stat['mtime'];