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 # quote for placing between single quotes in php
32 function enc_phpsq($str) {
33 $str = str_replace("\\", "\\\\", $str);
34 $str = str_replace("'", "\\'", $str);
38 function enc_jsdq($str) {
40 $str = str_replace("\n", "\\n", $str);
41 $str = str_replace("\r", "\\r", $str);
45 # encode for putting within double-quotes in SQL
46 function enc_sql($str) {
47 $str = str_replace("\\", "\\\\", $str);
48 $str = str_replace('"', "\\\"", $str);
52 # Encode for output in html. does nothing with whitespace
54 # Example: <p>~foo html~</p>
55 function enc_html($str) {
56 $str = str_replace('&', '&', $str);
57 $str = str_replace('<', '<', $str);
58 $str = str_replace('>', '>', $str);
62 # Encode for output in html. Convert newlines to <br>
64 # Example: <p>~foo htmlbr~</p>
65 function enc_htmlbr($str) {
66 $str = enc_html($str);
67 $str = str_replace("\n", "<br>\n", $str);
71 # Encode for output in html. Preserves newlines and indentation by converting
72 # newlines to <br> and spaces/tabs at the begining of lines to s
74 # Example: <p>~foo htmlbrtab~</p>
75 function enc_htmlbrtab($str) {
76 $str = enc_htmlbr($str);
77 $whitespace_to_nbsp = create_function('$matches', '$count = 0; $chars = str_split($matches[0]); foreach ($chars as $c) { if ($c == " ") { $count += 2; } else if ($c == "\t") { $count += 8; } } return str_repeat(" ", $count);');
78 $str = preg_replace_callback("|^[ \t]+|m", $whitespace_to_nbsp, $str);
82 # Encode for output in html. Spaces converted to and \n to <br>
84 # Example: <option value="12">~foo htmlbrnbsp~</option>
85 function enc_htmlbrnbsp($str) {
86 $str = enc_htmlbr($str);
87 $str = str_replace(' ', ' ', $str);
91 # Encode for output in html. Spaces converted to
93 # Example: <option value="12">~foo htmlnbsp~</option>
94 function enc_htmlnbsp($str) {
95 $str = enc_html($str);
96 $str = str_replace(' ', ' ', $str);
103 # Example: <input name="foo" value="~foo attr~">
104 function enc_attr($str) {
105 $str = str_replace('&', '&', $str);
106 $str = str_replace('"', '"', $str);
112 # Example: <a href="http://example.com?foo=~foo url_val attr~">http://example.com?foo=~foo url_val~</a>
113 function enc_url_val($str) {
114 return rawurlencode($str);
118 function enc_url_path($str) {
119 $str = rawurlencode($str);
120 $str = str_replace('%2F', '/', $str);
125 # This is a hack to work around html's stupid syntax for checkboxes.
127 # Place the template marker just before a " somewhere.
129 # Example: <input type="checkbox" name="foo~foo checked~">
130 function enc_checked($str) {
131 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
132 return '" checked="checked';
138 # 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".
139 # Example template: Subscribe to mailing list?: ~subscribe yesno~
140 function enc_yesno($str) {
141 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
149 # add a tab at the begining of each line
150 function enc_tab($str) {
151 if('' . $str === '') {
154 return "\t" . implode("\n\t", explode("\n", $str));
157 function enc_uppercase($str) {
158 return strtoupper($str);
160 function enc_upper($str) { # depricated
161 return enc_uppercase($str);
164 function enc_lowercase($str) {
165 return strtolower($str);
168 # pass date in the form 2008-05-23
169 # ercodes date as 05/23/2008
170 function enc_mmddyyyy($yyyy_mm_dd) {
171 if($yyyy_mm_dd == '') {
174 if(strlen($yyyy_mm_dd) != 10) {
175 return date('m/d/Y');
177 return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4);
180 # depricated. call enc_mmddyyyy() instead
181 function enc_mdy($str) {
182 return enc_mmddyyyy($str);
185 # pass unix timestamp or "2012-12-20 22:23:34"
186 function enc_mmddyyyyhhmm($str) {
187 if(is_numeric($str)) {
188 return date('m/d/Y g:ia', (int)$str);
190 return enc_mmddyyyy(substr($str, 0, 10)) . substr($str, 10, 6);
194 # takes decimal number of hours
196 function enc_hhmm($str) {
197 if(strlen($str) == 0) {
200 $hours = floor($str);
201 $minutes = round(($str - $hours) * 60);
202 $str = sprintf("%d:%02d", $hours, $minutes);
206 # takes decimal number of hours
207 # returns hh:mm followed by "am" or "pm" with no space
208 function enc_12hr($str) {
209 if(strlen($str) == 0) {
212 $hours = floor($str);
213 $minutes = round(($str - $hours) * 60);
221 $str = sprintf("%d:%02d", $hours, $minutes);
228 # These are depricated! All but PULLDOWN_HASH still work, but you should update your code.
229 define('PULLDOWN_AUTO', 0); define('PULLDOWN_ARRAY', 1); define('PULLDOWN_HASH', 2); define('PULLDOWN_2D', 3);
232 # call this function before you run() the template so enc_options() knows what
237 # name: the name of the html control
239 # options: an array of options to display in the pulldown/selectbox. Each
240 # element can be either a string, or an array with two elements (first the
241 # value to post, and second the value to display in the pulldown)
243 # multiple: UNTESTED set to true for multiple-select boxes.
245 function pulldown($name, $in_options, $multiple = false) {
246 if($multiple === PULLDOWN_HASH) {
247 die('Webmaster error: PULLDOWN_HASH is depricated. Pass array(a,b) not a=>b');
249 if($multiple !== true) {
250 # Probably due to API change (removing 3rd argument) but don't bother
251 # emitting a warning, because the above warning handles the only
256 foreach($in_options as $option) {
257 if(is_array($option)) {
258 $options[] = $option;
260 $options[] = array($option, $option);
263 $GLOBALS[$name . '_options'] = array(
264 'options' => $options,
265 'multiple' => $multiple);
268 # output a bunch of <option> tags
269 function enc_options($values, $name) {
270 if(!isset($GLOBALS[$name . '_options'])) {
271 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
273 if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
274 $values = explode(', ', $values);
276 return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_2D);
279 # for radios and pulldowns:
281 # returns what the user sees in the pulldown or on the radio button caption
282 function enc_pulled($str, $name) {
283 if(!isset($GLOBALS[$name . '_options'])) {
284 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
286 foreach($GLOBALS[$name . '_options']['options'] as &$kv) {
294 function enc_radio_n($str, $name, $n) {
295 if(!isset($GLOBALS[$name . '_options'])) {
296 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
299 if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
300 die("Template error: pulldown('$name') does not have element # $n");
303 $value = enc_attr($GLOBALS[$name . '_options']['options'][$n][0]);
305 if($str === $value) {
306 $value .= '" checked="checked';
311 function enc_radio_0($str, $name) { return enc_radio_n($str, $name, 0); }
312 function enc_radio_1($str, $name) { return enc_radio_n($str, $name, 1); }
313 function enc_radio_2($str, $name) { return enc_radio_n($str, $name, 2); }
314 function enc_radio_3($str, $name) { return enc_radio_n($str, $name, 3); }
315 function enc_radio_4($str, $name) { return enc_radio_n($str, $name, 4); }
316 function enc_radio_5($str, $name) { return enc_radio_n($str, $name, 5); }
317 function enc_radio_6($str, $name) { return enc_radio_n($str, $name, 6); }
318 function enc_radio_7($str, $name) { return enc_radio_n($str, $name, 7); }
321 function enc_radio_caption_n($str, $name, $n) {
322 if(!isset($GLOBALS[$name . '_options'])) {
323 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
326 if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
327 die("Template error: pulldown('$name') does not have element #$n");
330 return $GLOBALS[$name . '_options']['options'][$n][1];
332 function enc_radio_caption_0($str, $name) { return enc_radio_caption_n($str, $name, 0); }
333 function enc_radio_caption_1($str, $name) { return enc_radio_caption_n($str, $name, 1); }
334 function enc_radio_caption_2($str, $name) { return enc_radio_caption_n($str, $name, 2); }
335 function enc_radio_caption_3($str, $name) { return enc_radio_caption_n($str, $name, 3); }
336 function enc_radio_caption_4($str, $name) { return enc_radio_caption_n($str, $name, 4); }
337 function enc_radio_caption_5($str, $name) { return enc_radio_caption_n($str, $name, 5); }
338 function enc_radio_caption_6($str, $name) { return enc_radio_caption_n($str, $name, 6); }
339 function enc_radio_caption_7($str, $name) { return enc_radio_caption_n($str, $name, 7); }
342 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
346 # selected: can be a string or (for multiple-selects) an array
348 # options, keys_from: see documentation for pulldown() above
349 function encode_options($selected, $options, $keys_from) {
350 if(!is_array($selected)) {
351 $selected = array($selected);
354 if($keys_from != PULLDOWN_2D) {
355 $options = pulldown_options_to_2d($options, $keys_from);
359 foreach($options as $option) {
360 list($value, $display, $arg3) = $option;
363 if($arg3 == 'disabled') {
365 } elseif(in_array($value, $selected)) {
369 if($value !== $display || strpos($value, ' ') !== false) {
371 $out .= enc_attr($value);
377 $out .= enc_htmlnbsp($display);
379 $out .= "</option>\n";
385 $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");
388 function enc_states($str) {
391 return encode_options($str, $GLOBALS['wfpl_states_assoc'], PULLDOWN_HASH);
394 $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");
397 function enc_provinces($str) {
400 return encode_options($str, $GLOBALS['wfpl_provinces_assoc'], PULLDOWN_HASH);
403 # returns "odd", then "even", then "odd" etc.
404 function enc_evenodd($values, $name) {
405 if(!isset($GLOBALS['wfpl_even_odds'])) {
406 $GLOBALS['wfpl_even_odds'] = array();
409 if($GLOBALS['wfpl_even_odds'][$name]) {
410 $GLOBALS['wfpl_even_odds'][$name] = false;
413 $GLOBALS['wfpl_even_odds'][$name] = true;
418 function enc_image_src($str) {
419 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
423 function enc_image_width($str) {
424 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
428 function enc_image_height($str) {
429 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
433 function enc_thumb_src($str) {
434 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
438 function enc_thumb_width($str) {
439 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
442 function enc_thumb_height($str) {
443 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
447 # example template: Length: ~length html~ day~length s~
448 function enc_s($str) {
456 # turn http/ftp (s) urls into html links (and encode everything for html)
457 # does not encode without protocol (eg "www.foo.com")
458 # does not linkify email addresses
459 function enc_linkify($str) {
462 $pieces = preg_split("/((?:ht|f)tps?:\/\/[^ \,\"\n\r\t<]+)/is", $str, null, PREG_SPLIT_DELIM_CAPTURE);
463 foreach($pieces as $piece) {
465 $ret .= enc_html($piece);
467 $ret .= '<a href="' . enc_attr($piece) . '">' . enc_html($piece) . '</a>';
474 # turns a filename into the unix timestamp of that files modification date
475 function enc_mtime($dummy, $filename) {
476 $stat = stat($filename);
477 if ($stat === false) {
480 return '' . $stat['mtime'];