JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: make run.php include (instead of being a symlink)
[wfpl.git] / encode.php
index 24a150d..f5c1e24 100644 (file)
@@ -2,35 +2,44 @@
 
 #  Copyright (C) 2005 Jason Woofenden
 #
-#  This file is part of wfpl.
-#
-#  wfpl is free software; you can redistribute it and/or modify it under the
-#  terms of the GNU Lesser General Public License as published by the Free
-#  Software Foundation; either version 2.1 of the License, or (at your option)
-#  any later version.
-#
-#  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
-#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-#  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
-#  more details.
-#
-#  You should have received a copy of the GNU Lesser General Public License
-#  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
-#  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#  
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#  
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 # This file contains basic encodings. These are used by the encoder. You can
-# specify any template tag to be encoded with this syntax: ~variable.encoding~
+# specify any template tag to be encoded with this syntax: ~variable encoding~
 #
-# this example: <p>~foo.html~</p>
+# this example: <p>~foo html~</p>
 # will encode foo (using enc_html()) before displaying it, so that characters
 # such as < will display properly.
 
+function enc_cap($str) {
+       $str = ucfirst($str);
+       return $str;
+}
+
+# quote for placing between single quotes in php
+function enc_phpsq($str) {
+       $str = str_replace("\\", "\\\\", $str);
+       $str = str_replace("'", "\\'", $str);
+       return $str;
+}
 
 function enc_jsdq($str) {
        $str = enc_sql($str);
        $str = str_replace("\n", "\\n", $str);
-       return str_replace("\r", "\\r", $str);
+       $str = str_replace("\r", "\\r", $str);
+       return $str;
 }
 
 # encode for putting within double-quotes in SQL
@@ -42,7 +51,7 @@ function enc_sql($str) {
 
 # Encode for output in html. does nothing with whitespace
 #
-# Example: <p>~foo.html~</p>
+# Example: <p>~foo html~</p>
 function enc_html($str) {
        $str = str_replace('&', '&amp;', $str);
        $str = str_replace('<', '&lt;', $str);
@@ -50,18 +59,38 @@ function enc_html($str) {
        return $str;
 }
 
-# Encode for output in html. Convert newlines to <br />
+# Encode for output in html. Convert newlines to <br>
 #
-# Example: <p>~foo.html~</p>
+# Example: <p>~foo htmlbr~</p>
 function enc_htmlbr($str) {
        $str = enc_html($str);
-       $str = str_replace("\n", "<br />\n", $str);
+       $str = str_replace("\n", "<br>\n", $str);
+       return $str;
+}
+
+# Encode for output in html. Preserves newlines and indentation by converting
+# newlines to <br> and spaces/tabs at the begining of lines to &nbsp;s
+#
+# Example: <p>~foo htmlbrtab~</p>
+function enc_htmlbrtab($str) {
+       $str = enc_htmlbr($str);
+       $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("&nbsp;", $count);');
+       $str = preg_replace_callback("|^[ \t]+|m", $whitespace_to_nbsp, $str);
+       return $str;
+}
+
+# Encode for output in html. Spaces converted to &nbsp; and \n to <br>
+#
+# Example: <option value="12">~foo htmlbrnbsp~</option>
+function enc_htmlbrnbsp($str) {
+       $str = enc_htmlbr($str);
+       $str = str_replace(' ', '&nbsp;', $str);
        return $str;
 }
 
 # Encode for output in html. Spaces converted to &nbsp;
 #
-# Example: <option value="12">~foo.htmlnbsp~</option>
+# Example: <option value="12">~foo htmlnbsp~</option>
 function enc_htmlnbsp($str) {
        $str = enc_html($str);
        $str = str_replace(' ', '&nbsp;', $str);
@@ -71,7 +100,7 @@ function enc_htmlnbsp($str) {
 
 # HTML attribute.
 #
-# Example: <input name="foo" value="~foo.attr~">
+# Example: <input name="foo" value="~foo attr~">
 function enc_attr($str) {
        $str = str_replace('&', '&amp;', $str);
        $str = str_replace('"', '&quot;', $str);
@@ -80,7 +109,7 @@ function enc_attr($str) {
 
 # URI agument value.
 #
-# Example:  <a href="http://example.com?foo=~foo.url_val.attr~">http://example.com?foo=~foo.url_val~</a>
+# Example:  <a href="http://example.com?foo=~foo url_val attr~">http://example.com?foo=~foo url_val~</a>
 function enc_url_val($str) {
        return rawurlencode($str);
 }
@@ -97,84 +126,107 @@ function enc_url_path($str) {
 #
 # Place the template marker just before a " somewhere.
 #
-# Example: <input type="checkbox" name="foo~foo.checked~">
+# Example: <input type="checkbox" name="foo~foo checked~">
 function enc_checked($str) {
-       if($str == 'Yes') {
+       if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
                return '" checked="checked';
        } else {
                return '';
        }
 }
 
-# add a tab at the begining of each non-empty line
-function enc_tab($str) {
-       $lines = explode("\n", $str);
-       $out = '';
-       foreach($lines as $line) {
-               if($line) {
-                       $out .= "\t$line";
-               }
-               $out .= "\n";
+# 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".
+# Example template:  Subscribe to mailing list?: ~subscribe yesno~
+function enc_yesno($str) {
+       if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
+               return 'Yes';
+       } else {
+               return 'No';
        }
+}
+
 
-       # remove the extra newline added above
-       return substr($out, 0, -1);
+# add a tab at the begining of each line
+function enc_tab($str) {
+       if('' . $str === '') {
+               return '';
+       }
+       return "\t" . implode("\n\t", explode("\n", $str));
 }
 
-function enc_upper($str) {
+function enc_uppercase($str) {
        return strtoupper($str);
 }
-
-function enc_ddmmyyyyhhmm($seconds) {
-       return date('m/d/Y g:ia', (int)$seconds);
+function enc_upper($str) { # depricated
+       return enc_uppercase($str);
 }
 
-
-# display <option>s
-function enc_states($str) {
-       $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");
-       $ret = '';
-
-       return encode_options($str, $states_assoc, $use_keys = true);
+function enc_lowercase($str) {
+       return strtolower($str);
 }
 
-# display <option>s
-function enc_provinces($str) {
-       $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");
-       $ret = '';
+# pass date in the form 2008-05-23
+# ercodes date as 05/23/2008
+function enc_mmddyyyy($yyyy_mm_dd) {
+       if($yyyy_mm_dd == '') {
+               return '';
+       }
+       if(strlen($yyyy_mm_dd) != 10) {
+               return date('m/d/Y');
+       }
+       return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4);
+}
 
-       return encode_options($str, $provinces_assoc, $use_keys = true);
+# depricated. call enc_mmddyyyy() instead
+function enc_mdy($str) {
+       return enc_mmddyyyy($str);
 }
 
+# pass unix timestamp or "2012-12-20 22:23:34"
+function enc_mmddyyyyhhmm($str) {
+       if(is_numeric($str)) {
+               return date('m/d/Y g:ia', (int)$str);
+       } else {
+               return enc_mmddyyyy(substr($str, 0, 10)) . substr($str, 10, 6);
+       }
+}
 
+# takes decimal number of hours
+# returns hh:mm
+function enc_hhmm($str) {
+       if(strlen($str) == 0) {
+               return '';
+       }
+       $hours = floor($str);
+       $minutes = round(($str - $hours) * 60);
+       $str = sprintf("%d:%02d", $hours, $minutes);
+       return $str;
+}
 
+# takes decimal number of hours
+# returns hh:mm followed by "am" or "pm" with no space
+function enc_12hr($str) {
+       if(strlen($str) == 0) {
+               return '';
+       }
+       $hours = floor($str);
+       $minutes = round(($str - $hours) * 60);
+       $suffix = 'am';
+       if($hours >= 12.0) {
+               $suffix = 'pm';
+               if($hours > 12.0) {
+                       $hours -= 12.0;
+               }
+       }
+       $str = sprintf("%d:%02d", $hours, $minutes);
+       $str .= $suffix;
+       return $str;
+}
 
 
-define('PULLDOWN_ARRAY', 0); define('PULLDOWN_HASH', 1); define('PULLDOWN_2D', 2);
 
-function pulldown_options_to_hash($options, $keys_from) {
-       # convert other types of input to value=>display hash
-       switch($keys_from) {
-               case PULLDOWN_HASH:
-                       return $options;
-               case PULLDOWN_ARRAY:
-                       $new_options = array();
-                       foreach($options as $opt) {
-                               $new_options[$opt] = $opt;
-                       }
-                       return $new_options;
-               break;
-               case PULLDOWN_2D:
-                       $new_options = array();
-                       foreach($options as $opt) {
-                               $new_options[$opt[0]] = $opt[1];
-                       }
-                       return $new_options;
-               break;
-               default:
-                       die('unknown value: "' . print_r($keys_from) . '" passed in $keys_from parameter');
-       }
-}
+# These are depricated! All but PULLDOWN_HASH still work, but you should update your code.
+define('PULLDOWN_AUTO', 0); define('PULLDOWN_ARRAY', 1); define('PULLDOWN_HASH', 2); define('PULLDOWN_2D', 3);
 
 
 # call this function before you run() the template so enc_options() knows what
@@ -184,35 +236,109 @@ function pulldown_options_to_hash($options, $keys_from) {
 #
 #   name: the name of the html control
 #
-#   options: an array of options to display in the pulldown/selectbox
-#
-#   keys_from: Set to one of:
-#        PULLDOWN_ARRAY: (default) values of $options are displayd and posted
-#        PULLDOWN_HASH: values of $options are display, keys are posted
-#        PULLDOWN_2D: $options is a 2 dimensional array.
-#                     $options[0][1] is displayed, $options[0][0] is posted.
-#                     $options[1][1] is displayed, $options[1][0] is posted.
+#   options: an array of options to display in the pulldown/selectbox. Each
+#   element can be either a string, or an array with two elements (first the
+#   value to post, and second the value to display in the pulldown)
 #
 #   multiple: UNTESTED set to true for multiple-select boxes. 
 
-function pulldown($name, $options, $keys_from = PULLDOWN_ARRAY, $multiple = false) {
-       $options = pulldown_options_to_hash($options, $keys_from);
-       $GLOBALS[$name . '_options'] = array();
-       $GLOBALS[$name . '_options']['options'] = $options;
-       $GLOBALS[$name . '_options']['multiple'] = $multiple;
+function pulldown($name, $in_options, $multiple = false) {
+       if($multiple === PULLDOWN_HASH) {
+               die('Webmaster error: PULLDOWN_HASH is depricated. Pass array(a,b) not a=>b');
+       }
+       if($multiple !== true) {
+               # Probably due to API change (removing 3rd argument) but don't bother
+               # emitting a warning, because the above warning handles the only
+               # important case.
+               $multiple = false;
+       }
+       $options = array();
+       foreach($in_options as $option) {
+               if(is_array($option)) {
+                       $options[] = $option;
+               } else {
+                       $options[] = array($option, $option);
+               }
+       }
+       $GLOBALS[$name . '_options'] = array(
+               'options' => $options,
+               'multiple' => $multiple);
 }
 
 # output a bunch of <option> tags
 function enc_options($values, $name) {
        if(!isset($GLOBALS[$name . '_options'])) {
-               die('pulldown() must be called before this template can be run. See code/wfpl/encode.php');
+               die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
        }
        if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
                $values = explode(', ', $values);
        }
-       return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_HASH);
+       return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_2D);
+}
+
+# for radios and pulldowns:
+# pass posted value
+# returns what the user sees in the pulldown or on the radio button caption
+function enc_pulled($str, $name) {
+       if(!isset($GLOBALS[$name . '_options'])) {
+               die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
+       }
+       foreach($GLOBALS[$name . '_options']['options'] as &$kv) {
+               if($kv[0] == $str) {
+                       return $kv[1];
+               }
+       }
+       return $str;
 }
 
+function enc_radio_n($str, $name, $n) {
+       if(!isset($GLOBALS[$name . '_options'])) {
+               die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
+       }
+
+       if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
+               die("Template error: pulldown('$name') does not have element # $n");
+       }
+
+       $value = enc_attr($GLOBALS[$name . '_options']['options'][$n][0]);
+
+       if($str === $value) {
+               $value .= '" checked="checked';
+       }
+
+       return $value;
+}
+function enc_radio_0($str, $name) { return enc_radio_n($str, $name, 0); }
+function enc_radio_1($str, $name) { return enc_radio_n($str, $name, 1); }
+function enc_radio_2($str, $name) { return enc_radio_n($str, $name, 2); }
+function enc_radio_3($str, $name) { return enc_radio_n($str, $name, 3); }
+function enc_radio_4($str, $name) { return enc_radio_n($str, $name, 4); }
+function enc_radio_5($str, $name) { return enc_radio_n($str, $name, 5); }
+function enc_radio_6($str, $name) { return enc_radio_n($str, $name, 6); }
+function enc_radio_7($str, $name) { return enc_radio_n($str, $name, 7); }
+
+
+function enc_radio_caption_n($str, $name, $n) {
+       if(!isset($GLOBALS[$name . '_options'])) {
+               die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
+       }
+
+       if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
+               die("Template error: pulldown('$name') does not have element #$n");
+       }
+
+       return $GLOBALS[$name . '_options']['options'][$n][1];
+}
+function enc_radio_caption_0($str, $name) { return enc_radio_caption_n($str, $name, 0); }
+function enc_radio_caption_1($str, $name) { return enc_radio_caption_n($str, $name, 1); }
+function enc_radio_caption_2($str, $name) { return enc_radio_caption_n($str, $name, 2); }
+function enc_radio_caption_3($str, $name) { return enc_radio_caption_n($str, $name, 3); }
+function enc_radio_caption_4($str, $name) { return enc_radio_caption_n($str, $name, 4); }
+function enc_radio_caption_5($str, $name) { return enc_radio_caption_n($str, $name, 5); }
+function enc_radio_caption_6($str, $name) { return enc_radio_caption_n($str, $name, 6); }
+function enc_radio_caption_7($str, $name) { return enc_radio_caption_n($str, $name, 7); }
+
+
 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
 #
 # Parameters:
@@ -225,17 +351,22 @@ function encode_options($selected, $options, $keys_from) {
                $selected = array($selected);
        }
 
-       $options = pulldown_options_to_hash($options, $keys_from);
+       if($keys_from != PULLDOWN_2D) {
+               $options = pulldown_options_to_2d($options, $keys_from);
+       }
 
        $out = '';
-       foreach($options as $value => $display) {
+       foreach($options as $option) {
+               list($value, $display, $arg3) = $option;
                $out .= '<option';
 
-               if(in_array($value, $selected)) {
-                       $out .= ' selected="selected"';
+               if($arg3 == 'disabled') {
+                       $out .= ' disabled';
+               } elseif(in_array($value, $selected)) {
+                       $out .= ' selected';
                }
 
-               if($value !== $display) {
+               if($value !== $display || strpos($value, ' ') !== false) {
                        $out .= ' value="';
                        $out .= enc_attr($value);
                        $out .= '"';
@@ -251,4 +382,100 @@ function encode_options($selected, $options, $keys_from) {
        return $out;
 }
 
-?>
+$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");
+
+# display <option>s
+function enc_states($str) {
+       $ret = '';
+
+       return encode_options($str, $GLOBALS['wfpl_states_assoc'], PULLDOWN_HASH);
+}
+
+$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");
+
+# display <option>s
+function enc_provinces($str) {
+       $ret = '';
+
+       return encode_options($str, $GLOBALS['wfpl_provinces_assoc'], PULLDOWN_HASH);
+}
+
+# returns "odd", then "even", then "odd" etc.
+function enc_evenodd($values, $name) {
+       if(!isset($GLOBALS['wfpl_even_odds'])) {
+               $GLOBALS['wfpl_even_odds'] = array();
+       }
+
+       if($GLOBALS['wfpl_even_odds'][$name]) {
+               $GLOBALS['wfpl_even_odds'][$name] = false;
+               return 'even';
+       } else {
+               $GLOBALS['wfpl_even_odds'][$name] = true;
+               return 'odd';
+       }
+}
+
+function enc_image_src($str) {
+       list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
+       return $src;
+}
+
+function enc_image_width($str) {
+       list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
+       return $width;
+}
+
+function enc_image_height($str) {
+       list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
+       return $height;
+}
+
+function enc_thumb_src($str) {
+       list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
+       return $src;
+}
+
+function enc_thumb_width($str) {
+       list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
+       return $width;
+}
+function enc_thumb_height($str) {
+       list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
+       return $height;
+}
+
+# example template: Length: ~length html~ day~length s~
+function enc_s($str) {
+       if($str == '1') {
+               return '';
+       }
+
+       return 's';
+}
+
+# turn http/ftp (s) urls into html links (and encode everything for html)
+# does not encode without protocol (eg "www.foo.com")
+# does not linkify email addresses
+function enc_linkify($str) {
+       $ret = '';
+       $even = true;
+       $pieces = preg_split("/((?:ht|f)tps?:\/\/[^ \,\"\n\r\t<]+)/is", $str, null, PREG_SPLIT_DELIM_CAPTURE);
+       foreach($pieces as $piece) {
+               if($even) {
+                       $ret .= enc_html($piece);
+               } else {
+                       $ret .= '<a href="' . enc_attr($piece) . '">' . enc_html($piece) . '</a>';
+               }
+               $even = !$even;
+       }
+       return $ret;
+}
+
+# turns a filename into the unix timestamp of that files modification date
+function enc_mtime($dummy, $filename) {
+       $stat = stat($filename);
+       if ($stat === false) {
+               return '';
+       }
+       return '' . $stat['mtime'];
+}