ea-Geiercode-base
[ class tree: ea-Geier ] [ index: ea-Geier ] [ all elements ]

Source for file input.class.php

Documentation is available at input.class.php

  1. <?php 
  2. /**
  3.  * user input wrapper class
  4.  *
  5.  * LICENSE:
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (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.
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program; if not, write to the Free Software Foundation, Inc.,
  16.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17.  *
  18.  * @package       ea-Geier
  19.  * @subpackage code/base
  20.  * @author       m2mtech <tech@m2m.at>
  21.  * @copyright  2007 m2m server software gmbh
  22.  * @license       http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
  23.  * @version       $Id: input.class.php 147 2007-09-06 16:08:45Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * validation wrapper
  29.  */
  30. require_once('code/base/check.class.php');
  31.  
  32.  
  33. /**
  34.  * wrapper for user inpput
  35.  *
  36.  * @package       ea-Geier
  37.  */
  38. class eaInput {
  39.  
  40.     /**
  41.      * validator class
  42.      *
  43.      * @var eaCheck 
  44.      */
  45.     var $_check = false;
  46.  
  47.     /**
  48.      * validated post vars
  49.      *
  50.      * @var array 
  51.      */
  52.     var $post = false;
  53.  
  54.     /**
  55.      * validated get vars
  56.      *
  57.      * @var array 
  58.      */
  59.     var $get = false;
  60.  
  61.     /**
  62.      * errors for post or get vars
  63.      *
  64.      * @var array 
  65.      */
  66.     var $error = false;
  67.  
  68.     /**
  69.      * constructor
  70.      * 
  71.      * defines validator
  72.      */
  73.     function eaInput(
  74.         $this->_check = new eaCheck();
  75.         return true;
  76.     }
  77.             
  78.     /**
  79.      * validate input variables
  80.      * 
  81.      * parses $_GET and $_POST according data definition
  82.      * @param    array/string    data defnition
  83.      * @return    boolean            status (existing variables)
  84.      */
  85.     function check($dsn{
  86.         // get test pattern
  87.         $pattern array();
  88.         $test array();
  89.         if (is_array($dsn)) 
  90.             foreach ($dsn as $dsnItem)
  91.                 if (!$this->createTestPattern($dsnItem$pattern$test)) return false;
  92.         else {
  93.             if (!$this->createTestPattern($dsn$pattern$test)) return false;
  94.         }
  95.         // test
  96.         $result false;
  97.         foreach ($pattern as $method => $pItem{
  98.             switch($method{
  99.                 case 'p'$varType 'post'$varArray =$_POSTbreak;
  100.                 case 'g'$varType 'get'$varArray =$_GETbreak;
  101.                 defaultreturn false// should not happen here
  102.             }
  103.             // finalize pattern
  104.             $pItem '/^(' $pItem ')/';
  105.             $match false;
  106.             foreach ($varArray as $key => $var{
  107.                 if (!preg_match($pItem$key$match)) continue;
  108.                 if (is_array($var)) {
  109.                     foreach ($var as $k2 => $v2{
  110.                         if (get_magic_quotes_gpc()) $v2 stripslashes($v2);
  111.                         if (!$this->_check->$test[$method][$match[1]]($v2)) 
  112.                             $this->error[$key][$k2$this->_check->error;
  113.                         $this->{$varType}[$key][$k2$v2;
  114.                         $result true;
  115.                     }
  116.                 else {
  117.                     if (get_magic_quotes_gpc()) $var stripslashes($var);
  118.                     if (!$this->_check->$test[$method][$match[1]]($var)) 
  119.                         $this->error[$key$this->_check->error;
  120.                     $this->{$varType}[$key$var;
  121.                     $result true;
  122.                 }
  123.             }
  124.         }
  125.         return $result;
  126.     }
  127.  
  128.     /**
  129.      * create test pattern
  130.      * 
  131.      * breaks data definition into valid parts
  132.      * and generates a test array and a test pattern
  133.      * @param    string    data definition
  134.      * @param    array    test pattern
  135.      * @param    array    test array
  136.      * @return    boolean status
  137.      */
  138.     function createTestPattern($dsn&$pattern&$test{
  139.         $dsn trim ($dsn);
  140.         $match false;
  141.         // break dsn string
  142.         if (!preg_match('/(^[gp]+):([a-zA-Z]+):([a-zA-Z,]+)$/'$dsn$match)) return false;
  143.         list($method$function$prefix$match;
  144.         // check if function exists
  145.         if (!method_exists($this->_check$function)) return false;
  146.         // loop through methods
  147.         $l strlen($method);
  148.         for ($i 0$i $l$i++{
  149.             // define test functions for given prefix
  150.             $prefixArray explode(','$prefix);
  151.             foreach ($prefixArray as $pre{
  152.                 $test[$method[$i]][$pre$function;
  153.                 if (!isset($pattern[$method[$i]])) $pattern[$method[$i]] $pre;
  154.                 else $pattern[$method[$i]] .= '|' $pre;
  155.             }
  156.         }
  157.         return true;
  158.     }
  159.  
  160.     /**
  161.      * provides parsed $_GET variable
  162.      * 
  163.      * @param    string    variable name
  164.      * @return    mixed    variable value | false (!exist | error)
  165.      */
  166.     function get($var{
  167.         if (!isset($this->get[$var])) return false;
  168.         if (isset($this->error[$var])) return false;
  169.         return $this->get[$var];
  170.     }
  171.  
  172.     /** 
  173.      * provides parsed $_POST variable
  174.      * 
  175.      * @param    string    variable name
  176.      * @return    mixed    variable value | false (!exist | error)
  177.      */
  178.     function post($var{
  179.         if (!isset($this->post[$var])) return false;
  180.         if (isset($this->error[$var])) return false;
  181.         return $this->post[$var];
  182.     }
  183.  
  184.     /**
  185.      * returns & sets current client
  186.      * 
  187.      * The client is fetched either from the url or from the session.
  188.      * The url overwrites the session.
  189.      
  190.      * @param    eaDB            database
  191.      * @return    integer/boolean clientId || false
  192.      */
  193.     function getClient(&$db{
  194.         $opt =$this->options;
  195.         
  196.         if (!isset($opt['clients'])) return false;
  197.         
  198.         if (count($opt['clients']== 1{
  199.             $client key($opt['clients']);
  200.             $opt['client'$client;
  201.             $_SESSION['client'$client;
  202.             return $db->loadClientConf($client);
  203.         }
  204.         
  205.         // check url    
  206.         if (($state $this->get('state')) 
  207.             && isset($_SERVER['REQUEST_URI']&& ($url $_SERVER['REQUEST_URI'])) {
  208.             $match false;
  209.             $state preg_quote($state'/');
  210.             if (preg_match('/' $state '(\/|&)([0-9]+)/'$url$match)) {
  211.                 $client = (integer) $match[2];
  212.                 if (isset($opt['clients'][$client])) {        
  213.                     $opt['client'$client;
  214.                     $_SESSION['client'$client;
  215.                     return $db->loadClientConf($client);
  216.                 }
  217.             }
  218.         }
  219.  
  220.         // check post        
  221.         if (isset($_POST['buttonNavClients']&& isset($_POST['numClient'])) {
  222.             $client preg_replace('/[^0-9]/'''$_POST['numClient']);
  223.             if (isset($opt['clients'][$client])) {        
  224.                 $opt['client'$client;
  225.                 $_SESSION['client'$client;
  226.                 return $db->loadClientConf($client);
  227.             }
  228.         }
  229.  
  230.         // check session        
  231.         if (isset($_SESSION['client'])) {
  232.             $client $_SESSION['client'];
  233.             if (isset($opt['clients'][$client])) {
  234.                 $opt['client'$client;
  235.                 return $db->loadClientConf($client);
  236.             }
  237.         }
  238.         
  239.         // if no client is selected force first client        
  240.         $client key($opt['clients']);
  241.         $opt['client'$client;
  242.         $_SESSION['client'$client;
  243.         return $db->loadClientConf($client);
  244.     }
  245.  
  246.     /**
  247.      * sets client manually
  248.      *
  249.      * @param    integer clientID
  250.      * @return    boolean status
  251.      */
  252.     function setClient($id false{
  253.         if (!$idreturn false;
  254.         
  255.         $this->options['client'$id;
  256.         $_SESSION['client'$id;
  257.  
  258.         return true;
  259.     }
  260.  
  261.     /**
  262.      * looks for a get variable hidden in the url
  263.      * 
  264.      * @param    string            variable
  265.      * @param    string            type
  266.      * @return    string/boolean    variable content || false
  267.      */
  268.     function getGet($what$type 'num'{
  269.         if (!isset($_SERVER['REQUEST_URI'])) return false;
  270.         if (!$url $_SERVER['REQUEST_URI']return false;
  271.  
  272.         $what preg_quote($what'/');
  273.         $match false;
  274.         
  275.         if (!preg_match('/(\/|&)' $what '([^\/\&]+)/'$url$match)) return false;
  276.  
  277.         if (!isset($match[2])) return false;
  278.         
  279.         if (!method_exists($this->_check$type)) $type 'num';
  280.         
  281.         $return urldecode($match[2]);
  282.  
  283.         if (!$this->_check->$type($return)) return false;
  284.         
  285.         return $return;
  286.     }
  287.  
  288.     /**
  289.      * looks for a sort variable hidden in the url or stored in session
  290.      * 
  291.      * @param    array            allowed sort variables
  292.      * @return    string/boolean    sort string || false
  293.      */
  294.     function getSort($allowed false$prefix ''{
  295.     
  296.         if (!is_array($allowed)) return false;
  297.         
  298.         $client $this->options['client'];
  299.         
  300.         $sort array('what' => 'id''dir' => 'desc');
  301.  
  302.         foreach ($sort as $key => $val)
  303.             if (isset($_SESSION['sort'][$client][$prefix $key])) $sort[$key$_SESSION['sort'][$client][$prefix $key];
  304.  
  305.         if ($get $this->getGet('sort''txt')) {
  306.             if ($get == $sort['what']{
  307.                 if ($sort['dir'== 'asc'$sort['dir''desc';
  308.                 else $sort['dir''asc';
  309.             else {
  310.                 $sort['what'$get;
  311.                 $sort['dir''asc';
  312.             }
  313.             if (isset($_SESSION['page'][$client][$prefix]))
  314.                 unset($_SESSION['page'][$client][$prefix]);
  315.         }
  316.         
  317.         if (!in_array($sort['what']$allowed)) return false;
  318.         
  319.         foreach ($sort as $key => $val)
  320.             $_SESSION['sort'][$client][$prefix $key$sort[$key];
  321.  
  322.         return $sort['what'' ' $sort['dir'];
  323.     }
  324.  
  325.     /**
  326.      * looks for a page variable hidden in the url or stored in session
  327.      * 
  328.      * @return    string/boolean    sort string || false
  329.      */
  330.     function getPage($prefix{
  331.         if (!$prefixreturn false;
  332.         
  333.         $client $this->options['client'];
  334.         
  335.         $page $this->getGet('page');
  336.         if (!$page && isset($_SESSION['page'][$client][$prefix])) 
  337.             $page $_SESSION['page'][$client][$prefix];
  338.         
  339.         if ($page$_SESSION['page'][$client][$prefix$page;
  340.  
  341.         return $page;
  342.     }
  343.  
  344. }
  345.         
  346. ?>

Documentation generated on Sun, 09 Sep 2007 17:08:55 +0200 by phpDocumentor 1.3.1