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']);
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: see documentation for pulldown() above
349 function encode_options($selected, $options) {
350 if(!is_array($selected)) {
351 $selected = array($selected);
355 foreach($options as $option) {
356 list($value, $display, $arg3) = $option;
359 if($arg3 == 'disabled') {
361 } elseif(in_array($value, $selected)) {
365 if($value !== $display || strpos($value, ' ') !== false) {
367 $out .= enc_attr($value);
373 $out .= enc_htmlnbsp($display);
375 $out .= "</option>\n";
381 $GLOBALS['wfpl_states_assoc'] = array(array("AL", "Alabama"), array("AK", "Alaska"), array("AZ", "Arizona"), array("AR", "Arkansas"), array("CA", "California"), array("CO", "Colorado"), array("CT", "Connecticut"), array("DE", "Delaware"), array("FL", "Florida"), array("GA", "Georgia"), array("HI", "Hawaii"), array("ID", "Idaho"), array("IL", "Illinois"), array("IN", "Indiana"), array("IA", "Iowa"), array("KS", "Kansas"), array("KY", "Kentucky"), array("LA", "Louisiana"), array("ME", "Maine"), array("MD", "Maryland"), array("MA", "Massachusetts"), array("MI", "Michigan"), array("MN", "Minnesota"), array("MS", "Mississippi"), array("MO", "Missouri"), array("MT", "Montana"), array("NE", "Nebraska"), array("NV", "Nevada"), array("NH", "New Hampshire"), array("NJ", "New Jersey"), array("NM", "New Mexico"), array("NY", "New York"), array("NC", "North Carolina"), array("ND", "North Dakota"), array("OH", "Ohio"), array("OK", "Oklahoma"), array("OR", "Oregon"), array("PA", "Pennsylvania"), array("RI", "Rhode Island"), array("SC", "South Carolina"), array("SD", "South Dakota"), array("TN", "Tennessee"), array("TX", "Texas"), array("UT", "Utah"), array("VT", "Vermont"), array("VA", "Virginia"), array("WA", "Washington"), array("DC", "Washington, DC"), array("WV", "West Virginia"), array("WI", "Wisconsin"), array("WY", "Wyoming"));
384 function enc_states($str) {
387 return encode_options($str, $GLOBALS['wfpl_states_assoc']);
390 $GLOBALS['wfpl_provinces_assoc'] = array(array("AB", "Alberta"), array("BC", "British Columbia"), array("MB", "Manitoba"), array("NF", "Newfoundland"), array("NB", "New Brunswick"), array("NS", "Nova Scotia"), array("NT", "Northwest Territories"), array("NU", "Nunavut"), array("ON", "Ontario"), array("PE", "Prince Edward Island"), array("QC", "Quebec"), array("SK", "Saskatchewan"), array("YT", "Yukon Territory"));
393 function enc_provinces($str) {
396 return encode_options($str, $GLOBALS['wfpl_provinces_assoc']);
399 # returns "odd", then "even", then "odd" etc.
400 function enc_evenodd($values, $name) {
401 if(!isset($GLOBALS['wfpl_even_odds'])) {
402 $GLOBALS['wfpl_even_odds'] = array();
405 if($GLOBALS['wfpl_even_odds'][$name]) {
406 $GLOBALS['wfpl_even_odds'][$name] = false;
409 $GLOBALS['wfpl_even_odds'][$name] = true;
414 function enc_image_src($str) {
415 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
419 function enc_image_width($str) {
420 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
424 function enc_image_height($str) {
425 list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
429 function enc_thumb_src($str) {
430 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
434 function enc_thumb_width($str) {
435 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
438 function enc_thumb_height($str) {
439 list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
443 # example template: Length: ~length html~ day~length s~
444 function enc_s($str) {
452 # turn http/ftp (s) urls into html links (and encode everything for html)
453 # does not encode without protocol (eg "www.foo.com")
454 # does not linkify email addresses
455 function enc_linkify($str) {
458 $pieces = preg_split("/((?:ht|f)tps?:\/\/[^ \,\"\n\r\t<]+)/is", $str, null, PREG_SPLIT_DELIM_CAPTURE);
459 foreach($pieces as $piece) {
461 $ret .= enc_html($piece);
463 $ret .= '<a href="' . enc_attr($piece) . '">' . enc_html($piece) . '</a>';
470 # turns a filename into the unix timestamp of that files modification date
471 function enc_mtime($dummy, $filename) {
472 $stat = stat($filename);
473 if ($stat === false) {
476 return '' . $stat['mtime'];