JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / encode.php
index ca4a919..a395a10 100644 (file)
@@ -1,19 +1,9 @@
 <?php
 
-#  Copyright (C) 2005 Jason Woofenden
-#
-#  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 program is in the public domain within the United States. Additionally,
+# we waive copyright and related rights in the work worldwide through the CC0
+# 1.0 Universal public domain dedication, which can be found at
+# http://creativecommons.org/publicdomain/zero/1.0/
 
 
 # This file contains basic encodings. These are used by the encoder. You can
@@ -42,6 +32,10 @@ function enc_jsdq($str) {
        return $str;
 }
 
+function enc_json($str) {
+       return json_encode($str);
+}
+
 # encode for putting within double-quotes in SQL
 function enc_sql($str) {
        $str = str_replace("\\", "\\\\", $str);
@@ -74,7 +68,7 @@ function enc_htmlbr($str) {
 # 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);');
+       $whitespace_to_nbsp = create_function('$matches', '$count = 0; $chars = str_split($matches[0]); foreach ($chars as $c) { if ($c == " ") { $count += 1; } else if ($c == "\t") { $count += 8; } } return str_repeat("&nbsp;", $count);');
        $str = preg_replace_callback("|^[ \t]+|m", $whitespace_to_nbsp, $str);
        return $str;
 }
@@ -268,7 +262,7 @@ function pulldown($name, $in_options, $multiple = false) {
 # output a bunch of <option> tags
 function enc_options($values, $name) {
        if(!isset($GLOBALS[$name . '_options'])) {
-               die("pulldown('$name') 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 wfpl/encode.php");
        }
        if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
                $values = explode(', ', $values);
@@ -281,7 +275,7 @@ function enc_options($values, $name) {
 # 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");
+               die("pulldown('$name') must be called before this template can be run. See wfpl/encode.php");
        }
        foreach($GLOBALS[$name . '_options']['options'] as &$kv) {
                if($kv[0] == $str) {
@@ -293,7 +287,7 @@ function enc_pulled($str, $name) {
 
 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");
+               die("pulldown('$name') must be called before this template can be run. See wfpl/encode.php");
        }
 
        if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
@@ -316,11 +310,24 @@ 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_8($str, $name) { return enc_radio_n($str, $name, 8); }
+function enc_radio_9($str, $name) { return enc_radio_n($str, $name, 9); }
+function enc_radio_10($str, $name) { return enc_radio_n($str, $name, 10); }
+function enc_radio_11($str, $name) { return enc_radio_n($str, $name, 11); }
+function enc_radio_12($str, $name) { return enc_radio_n($str, $name, 12); }
+function enc_radio_13($str, $name) { return enc_radio_n($str, $name, 13); }
+function enc_radio_14($str, $name) { return enc_radio_n($str, $name, 14); }
+function enc_radio_15($str, $name) { return enc_radio_n($str, $name, 15); }
+function enc_radio_16($str, $name) { return enc_radio_n($str, $name, 16); }
+function enc_radio_17($str, $name) { return enc_radio_n($str, $name, 17); }
+function enc_radio_18($str, $name) { return enc_radio_n($str, $name, 18); }
+function enc_radio_19($str, $name) { return enc_radio_n($str, $name, 19); }
+function enc_radio_20($str, $name) { return enc_radio_n($str, $name, 20); }
 
 
 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");
+               die("pulldown('$name') must be called before this template can be run. See wfpl/encode.php");
        }
 
        if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
@@ -337,6 +344,19 @@ function enc_radio_caption_4($str, $name) { return enc_radio_caption_n($str, $na
 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); }
+function enc_radio_caption_8($str, $name) { return enc_radio_caption_n($str, $name, 8); }
+function enc_radio_caption_9($str, $name) { return enc_radio_caption_n($str, $name, 9); }
+function enc_radio_caption_10($str, $name) { return enc_radio_caption_n($str, $name, 10); }
+function enc_radio_caption_11($str, $name) { return enc_radio_caption_n($str, $name, 11); }
+function enc_radio_caption_12($str, $name) { return enc_radio_caption_n($str, $name, 12); }
+function enc_radio_caption_13($str, $name) { return enc_radio_caption_n($str, $name, 13); }
+function enc_radio_caption_14($str, $name) { return enc_radio_caption_n($str, $name, 14); }
+function enc_radio_caption_15($str, $name) { return enc_radio_caption_n($str, $name, 15); }
+function enc_radio_caption_16($str, $name) { return enc_radio_caption_n($str, $name, 16); }
+function enc_radio_caption_17($str, $name) { return enc_radio_caption_n($str, $name, 17); }
+function enc_radio_caption_18($str, $name) { return enc_radio_caption_n($str, $name, 18); }
+function enc_radio_caption_19($str, $name) { return enc_radio_caption_n($str, $name, 19); }
+function enc_radio_caption_20($str, $name) { return enc_radio_caption_n($str, $name, 20); }
 
 
 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
@@ -353,10 +373,10 @@ function encode_options($selected, $options) {
 
        $out = '';
        foreach($options as $option) {
-               list($value, $display, $arg3) = $option;
+               list($value, $display) = $option;
                $out .= '<option';
 
-               if($arg3 == 'disabled') {
+               if(isset($option[2]) && $option[2] == 'disabled') {
                        $out .= ' disabled';
                } elseif(in_array($value, $selected)) {
                        $out .= ' selected';
@@ -411,34 +431,42 @@ function enc_evenodd($values, $name) {
        }
 }
 
-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 wfpl_nth_word($str, $n) {
+       $a = explode(' ', $str);
+       return isset($a[$n]) ? $a[$n] : null;
 }
 
-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;
+# encoding is a space separated list of:
+# image_filename width height thumb_filename thumb_width thumb_height
+function enc_image_src($str) { return wfpl_nth_word($str, 0); }
+function _enc_image_src_at_width($str, $width) {
+       $src = enc_image_src($str);
+       if ($src) {
+               return
+                       substr($src, 0, -4)
+                       . 'w'
+                       . $width
+                       . substr($src, -4);
+       }
+       return '';
+}
+# define these width constants in your config.php
+function enc_image_src_full($str) { return _enc_image_src_at_width($str, WFPL_IMAGE_WIDTH_FULL); }
+function enc_image_src_small($str) { return _enc_image_src_at_width($str, WFPL_IMAGE_WIDTH_SMALL); }
+function enc_image_src_thumb($str) { return _enc_image_src_at_width($str, WFPL_IMAGE_WIDTH_THUMB); }
+function enc_image_width($str) { return wfpl_nth_word($str, 1); }
+function enc_image_height($str) { return wfpl_nth_word($str, 2); }
+function enc_image_aspect($str) {
+       $a = explode(' ', $str);
+       if (count($a) < 3) {
+               return '';
+       }
+       return ''.(round(100000 * ((int)$a[2]) / ((int)$a[1]) / 1000)).'%';
 }
+# obsolete:
+function enc_thumb_src($str) { return wfpl_nth_word($str, 3); }
+function enc_thumb_width($str) { return wfpl_nth_word($str, 4); }
+function enc_thumb_height($str) { return wfpl_nth_word($str, 5); }
 
 # example template: Length: ~length html~ day~length s~
 function enc_s($str) {