home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Table / Valid.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  11.4 KB  |  455 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * DB_Table_Valid validates values against DB_Table column types.
  7.  * 
  8.  * PHP versions 4 and 5
  9.  *
  10.  * LICENSE:
  11.  * 
  12.  * Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
  13.  *                          David C. Morse <morse@php.net>
  14.  *                          Mark Wiesemann <wiesemann@php.net>
  15.  * All rights reserved.
  16.  *
  17.  * Redistribution and use in source and binary forms, with or without
  18.  * modification, are permitted provided that the following conditions
  19.  * are met:
  20.  *
  21.  *    * Redistributions of source code must retain the above copyright
  22.  *      notice, this list of conditions and the following disclaimer.
  23.  *    * Redistributions in binary form must reproduce the above copyright
  24.  *      notice, this list of conditions and the following disclaimer in the 
  25.  *      documentation and/or other materials provided with the distribution.
  26.  *    * The names of the authors may not be used to endorse or promote products 
  27.  *      derived from this software without specific prior written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  31.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.  *
  41.  * @category Database
  42.  * @package  DB_Table
  43.  * @author   Paul M. Jones <pmjones@php.net>
  44.  * @author   David C. Morse <morse@php.net>
  45.  * @author   Mark Wiesemann <wiesemann@php.net>
  46.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  47.  * @version  CVS: $Id: Valid.php,v 1.10 2007/12/13 16:52:15 wiesemann Exp $
  48.  * @link     http://pear.php.net/package/DB_Table
  49.  */
  50.  
  51. /**
  52. * DB_Table class for constants and other globals.
  53. */
  54. require_once 'DB/Table.php';
  55.  
  56.  
  57. /**
  58. * validation ranges for integers
  59. */
  60. if (! isset($GLOBALS['_DB_TABLE']['valid'])) {
  61.     $GLOBALS['_DB_TABLE']['valid'] = array(
  62.         'smallint' => array(pow(-2, 15), pow(+2, 15) - 1),
  63.         'integer' => array(pow(-2, 31), pow(+2, 31) - 1),
  64.         'bigint' => array(pow(-2, 63), pow(+2, 63) - 1)
  65.     );
  66. }
  67.  
  68.  
  69. /**
  70.  * DB_Table_Valid validates values against DB_Table column types.
  71.  * 
  72.  * @category Database
  73.  * @package  DB_Table
  74.  * @author   Paul M. Jones <pmjones@php.net>
  75.  * @author   David C. Morse <morse@php.net>
  76.  * @author   Mark Wiesemann <wiesemann@php.net>
  77.  * @version  Release: 1.5.5
  78.  * @link     http://pear.php.net/package/DB_Table
  79.  */
  80.  
  81. class DB_Table_Valid {
  82.     
  83.     /**
  84.     * 
  85.     * Check if a value validates against the 'boolean' data type.
  86.     * 
  87.     * @static
  88.     * 
  89.     * @access public
  90.     * 
  91.     * @param mixed $value The value to validate.
  92.     * 
  93.     * @return boolean True if the value is valid for the data type, false
  94.     * if not.
  95.     * 
  96.     */
  97.     
  98.     function isBoolean($value)
  99.     {
  100.         if ($value === true || $value === false) {
  101.             return true;
  102.         } elseif (is_numeric($value) && ($value == 0 || $value == 1)) {
  103.             return true;
  104.         } else {
  105.             return false;
  106.         }
  107.     }
  108.     
  109.     
  110.     /**
  111.     * 
  112.     * Check if a value validates against the 'char' and 'varchar' data type.
  113.     * 
  114.     * We allow most anything here, only checking that the length is in range.
  115.     * 
  116.     * @static
  117.     * 
  118.     * @access public
  119.     * 
  120.     * @param mixed $value The value to validate.
  121.     * 
  122.     * @return boolean True if the value is valid for the data type, false
  123.     * if not.
  124.     * 
  125.     */
  126.     
  127.     function isChar($value, $colsize)
  128.     {
  129.         $is_scalar = (! is_array($value) && ! is_object($value));
  130.         $in_range = (strlen($value) <= $colsize);
  131.         return $is_scalar && $in_range;
  132.     }
  133.     
  134.     
  135.     /**
  136.     * 
  137.     * Check if a value validates against the 'smallint' data type.
  138.     * 
  139.     * @static
  140.     * 
  141.     * @access public
  142.     * 
  143.     * @param mixed $value The value to validate.
  144.     * 
  145.     * @return boolean True if the value is valid for the data type, false
  146.     * if not.
  147.     * 
  148.     */
  149.     
  150.     function isSmallint($value)
  151.     {
  152.         return is_integer($value) &&
  153.             ($value >= $GLOBALS['_DB_TABLE']['valid']['smallint'][0]) &&
  154.             ($value <= $GLOBALS['_DB_TABLE']['valid']['smallint'][1]);
  155.     }
  156.     
  157.     
  158.     /**
  159.     * 
  160.     * Check if a value validates against the 'integer' data type.
  161.     * 
  162.     * @static
  163.     * 
  164.     * @access public
  165.     * 
  166.     * @param mixed $value The value to validate.
  167.     * 
  168.     * @return boolean True if the value is valid for the data type, false
  169.     * if not.
  170.     * 
  171.     */
  172.     
  173.     function isInteger($value)
  174.     {
  175.         return is_integer($value) &&
  176.             ($value >= $GLOBALS['_DB_TABLE']['valid']['integer'][0]) &&
  177.             ($value <= $GLOBALS['_DB_TABLE']['valid']['integer'][1]);
  178.     }
  179.     
  180.     
  181.     /**
  182.     * 
  183.     * Check if a value validates against the 'bigint' data type.
  184.     * 
  185.     * @static
  186.     * 
  187.     * @access public
  188.     * 
  189.     * @param mixed $value The value to validate.
  190.     * 
  191.     * @return boolean True if the value is valid for the data type, false
  192.     * if not.
  193.     * 
  194.     */
  195.     
  196.     function isBigint($value)
  197.     {
  198.         return is_integer($value) &&
  199.             ($value >= $GLOBALS['_DB_TABLE']['valid']['bigint'][0]) &&
  200.             ($value <= $GLOBALS['_DB_TABLE']['valid']['bigint'][1]);
  201.     }
  202.     
  203.     
  204.     /**
  205.     * 
  206.     * Check if a value validates against the 'decimal' data type.
  207.     * 
  208.     * For the column defined "DECIMAL(5,2)" standard SQL requires that
  209.     * the column be able to store any value with 5 digits and 2
  210.     * decimals. In this case, therefore, the range of values that can be
  211.     * stored in the column is from -999.99 to 999.99.  DB_Table attempts
  212.     * to enforce this behavior regardless of the RDBMS backend behavior.
  213.     * 
  214.     * @static
  215.     * 
  216.     * @access public
  217.     * 
  218.     * @param mixed $value The value to validate.
  219.     * 
  220.     * @param string $colsize The 'size' to use for validation (to make
  221.     * sure of min/max and decimal places).
  222.     * 
  223.     * @param string $colscope The 'scope' to use for validation (to make
  224.     * sure of min/max and decimal places).
  225.     * 
  226.     * @return boolean True if the value is valid for the data type, false
  227.     * if not.
  228.     * 
  229.     */
  230.     
  231.     function isDecimal($value, $colsize, $colscope)
  232.     {
  233.         if (! is_numeric($value)) {
  234.             return false;
  235.         }
  236.         
  237.         // maximum number of digits allowed to the left
  238.         // and right of the decimal point.
  239.         $right_max = $colscope;
  240.         $left_max = $colsize - $colscope;
  241.         
  242.         // ignore negative signs in all validation
  243.         $value = str_replace('-', '', $value);
  244.         
  245.         // find the decimal point, then get the left
  246.         // and right portions.
  247.         $pos = strpos($value, '.');
  248.         if ($pos === false) {
  249.             $left = $value;
  250.             $right = '';
  251.         } else {
  252.             $left = substr($value, 0, $pos);
  253.             $right = substr($value, $pos+1);
  254.         }
  255.         
  256.         // how long are the left and right portions?
  257.         $left_len = strlen($left);
  258.         $right_len = strlen($right);
  259.         
  260.         // do the portions exceed their maxes?
  261.         if ($left_len > $left_max ||
  262.             $right_len > $right_max) {
  263.             // one or the other exceeds the max lengths
  264.             return false;
  265.         } else {
  266.             // both are within parameters
  267.             return true;
  268.         }
  269.     }
  270.     
  271.     
  272.     /**
  273.     * 
  274.     * Check if a value validates against the 'single' data type.
  275.     * 
  276.     * @static
  277.     * 
  278.     * @access public
  279.     * 
  280.     * @param mixed $value The value to validate.
  281.     * 
  282.     * @return boolean True if the value is valid for the data type, false
  283.     * if not.
  284.     * 
  285.     */
  286.     
  287.     function isSingle($value)
  288.     {
  289.         return is_float($value);
  290.     }
  291.     
  292.     
  293.     /**
  294.     * 
  295.     * Check if a value validates against the 'double' data type.
  296.     * 
  297.     * @static
  298.     * 
  299.     * @access public
  300.     * 
  301.     * @param mixed $value The value to validate.
  302.     * 
  303.     * @return boolean True if the value is valid for the data type, false
  304.     * if not.
  305.     * 
  306.     */
  307.     
  308.     function isDouble($value)
  309.     {
  310.         return is_float($value);
  311.     }
  312.     
  313.     
  314.     /**
  315.     * 
  316.     * Check if a value validates against the 'time' data type.
  317.     * 
  318.     * @static
  319.     * 
  320.     * @access public
  321.     * 
  322.     * @param mixed $value The value to validate.
  323.     * 
  324.     * @return boolean True if the value is valid for the data type, false
  325.     * if not.
  326.     * 
  327.     */
  328.     
  329.     function isTime($value)
  330.     {
  331.         // hh:ii:ss
  332.         // 01234567
  333.         $h  = substr($value, 0, 2);
  334.         $s1 = substr($value, 2, 1);
  335.         $i  = substr($value, 3, 2);
  336.         $s2 = substr($value, 5, 1);
  337.         $s  = substr($value, 6, 2);
  338.         
  339.         // time check
  340.         if (strlen($value) != 8 ||
  341.             ! is_numeric($h) || $h < 0 || $h > 23  ||
  342.             $s1 != ':' ||
  343.             ! is_numeric($i) || $i < 0 || $i > 59 ||
  344.             $s2 != ':' ||
  345.             ! is_numeric($s) || $s < 0 || $s > 59) {
  346.             
  347.             return false;
  348.             
  349.         } else {
  350.         
  351.             return true;
  352.             
  353.         }
  354.     }
  355.     
  356.     
  357.     /**
  358.     * 
  359.     * Check if a value validates against the 'date' data type.
  360.     * 
  361.     * @static
  362.     * 
  363.     * @access public
  364.     * 
  365.     * @param mixed $value The value to validate.
  366.     * 
  367.     * @return boolean True if the value is valid for the data type, false
  368.     * if not.
  369.     * 
  370.     */
  371.     
  372.     function isDate($value)
  373.     {
  374.         // yyyy-mm-dd
  375.         // 0123456789
  376.         $y  = substr($value, 0, 4);
  377.         $s1 = substr($value, 4, 1);
  378.         $m  = substr($value, 5, 2);
  379.         $s2 = substr($value, 7, 1);
  380.         $d  = substr($value, 8, 2);
  381.         
  382.         // date check
  383.         if (strlen($value) != 10 || $s1 != '-' || $s2 != '-' ||
  384.             ! checkdate($m, $d, $y)) {
  385.             
  386.             return false;
  387.             
  388.         } else {
  389.         
  390.             return true;
  391.             
  392.         }
  393.     }
  394.     
  395.     
  396.     /**
  397.     * 
  398.     * Check if a value validates against the 'timestamp' data type.
  399.     * 
  400.     * @static
  401.     * 
  402.     * @access public
  403.     * 
  404.     * @param mixed $value The value to validate.
  405.     * 
  406.     * @return boolean True if the value is valid for the data type, false
  407.     * if not.
  408.     * 
  409.     */
  410.     
  411.     function isTimestamp($value)
  412.     {
  413.         // yyyy-mm-dd hh:ii:ss
  414.         // 0123456789012345678
  415.         $date = substr($value, 0, 10);
  416.         $sep = substr($value, 10, 1);
  417.         $time = substr($value, 11, 8);
  418.         
  419.         if (strlen($value) != 19 || $sep != ' ' ||
  420.             ! DB_Table_Valid::isDate($date) ||
  421.             ! DB_Table_Valid::isTime($time)) {
  422.             
  423.             return false;
  424.             
  425.         } else {
  426.         
  427.             return true;
  428.             
  429.         }
  430.     }
  431.     
  432.     
  433.     /**
  434.     * 
  435.     * Check if a value validates against the 'clob' data type.
  436.     * 
  437.     * @static
  438.     * 
  439.     * @access public
  440.     * 
  441.     * @param mixed $value The value to validate.
  442.     * 
  443.     * @return boolean True if the value is valid for the data type, false
  444.     * if not.
  445.     * 
  446.     */
  447.     
  448.     function isClob($value)
  449.     {
  450.         return is_string($value);
  451.     }
  452. }
  453.  
  454. ?>
  455.