Source for file input.class.php
Documentation is available at input.class.php
* user input wrapper class
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author m2mtech <tech@m2m.at>
* @copyright 2007 m2m server software gmbh
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
* @version $Id: input.class.php 147 2007-09-06 16:08:45Z m2mtech $
* @link http://www.ea-geier.at/
require_once('code/base/check.class.php');
* wrapper for user inpput
* errors for post or get vars
* validate input variables
* parses $_GET and $_POST according data definition
* @param array/string data defnition
* @return boolean status (existing variables)
foreach ($dsn as $dsnItem)
foreach ($pattern as $method => $pItem) {
case 'p': $varType = 'post'; $varArray = & $_POST; break;
case 'g': $varType = 'get'; $varArray = & $_GET; break;
default: return false; // should not happen here
$pItem = '/^(' . $pItem . ')/';
foreach ($varArray as $key => $var) {
foreach ($var as $k2 => $v2) {
if (!$this->_check->$test[$method][$match[1]]($v2))
$this->{$varType}[$key][$k2] = $v2;
if (!$this->_check->$test[$method][$match[1]]($var))
$this->{$varType}[$key] = $var;
* breaks data definition into valid parts
* and generates a test array and a test pattern
* @param string data definition
* @param array test pattern
* @param array test array
if (!preg_match('/(^[gp]+):([a-zA-Z]+):([a-zA-Z,]+)$/', $dsn, $match)) return false;
list (, $method, $function, $prefix) = $match;
// check if function exists
for ($i = 0; $i < $l; $i++ ) {
// define test functions for given prefix
$prefixArray = explode(',', $prefix);
foreach ($prefixArray as $pre) {
$test[$method[$i]][$pre] = $function;
if (!isset ($pattern[$method[$i]])) $pattern[$method[$i]] = $pre;
else $pattern[$method[$i]] .= '|' . $pre;
* provides parsed $_GET variable
* @param string variable name
* @return mixed variable value | false (!exist | error)
if (!isset ($this->get[$var])) return false;
if (isset ($this->error[$var])) return false;
* provides parsed $_POST variable
* @param string variable name
* @return mixed variable value | false (!exist | error)
if (!isset ($this->post[$var])) return false;
if (isset ($this->error[$var])) return false;
return $this->post[$var];
* returns & sets current client
* The client is fetched either from the url or from the session.
* The url overwrites the session.
* @return integer/boolean clientId || false
if (!isset ($opt['clients'])) return false;
if (count($opt['clients']) == 1) {
$client = key($opt['clients']);
$opt['client'] = $client;
$_SESSION['client'] = $client;
return $db->loadClientConf($client);
if (($state = $this->get('state'))
&& isset ($_SERVER['REQUEST_URI']) && ($url = $_SERVER['REQUEST_URI'])) {
if (preg_match('/' . $state . '(\/|&)([0-9]+)/', $url, $match)) {
$client = (integer) $match[2];
if (isset ($opt['clients'][$client])) {
$opt['client'] = $client;
$_SESSION['client'] = $client;
return $db->loadClientConf($client);
if (isset ($_POST['buttonNavClients']) && isset ($_POST['numClient'])) {
$client = preg_replace('/[^0-9]/', '', $_POST['numClient']);
if (isset ($opt['clients'][$client])) {
$opt['client'] = $client;
$_SESSION['client'] = $client;
return $db->loadClientConf($client);
if (isset ($_SESSION['client'])) {
$client = $_SESSION['client'];
if (isset ($opt['clients'][$client])) {
$opt['client'] = $client;
return $db->loadClientConf($client);
// if no client is selected force first client
$client = key($opt['clients']);
$opt['client'] = $client;
$_SESSION['client'] = $client;
return $db->loadClientConf($client);
* @param integer clientID
$this->options['client'] = $id;
$_SESSION['client'] = $id;
* looks for a get variable hidden in the url
* @return string/boolean variable content || false
function getGet($what, $type = 'num') {
if (!isset ($_SERVER['REQUEST_URI'])) return false;
if (!$url = $_SERVER['REQUEST_URI']) return false;
if (!preg_match('/(\/|&)' . $what . '([^\/\&]+)/', $url, $match)) return false;
if (!isset ($match[2])) return false;
if (!$this->_check->$type($return)) return false;
* looks for a sort variable hidden in the url or stored in session
* @param array allowed sort variables
* @return string/boolean sort string || false
function getSort($allowed = false, $prefix = '') {
$client = $this->options['client'];
$sort = array('what' => 'id', 'dir' => 'desc');
foreach ($sort as $key => $val)
if (isset ($_SESSION['sort'][$client][$prefix . $key])) $sort[$key] = $_SESSION['sort'][$client][$prefix . $key];
if ($get = $this->getGet('sort', 'txt')) {
if ($get == $sort['what']) {
if ($sort['dir'] == 'asc') $sort['dir'] = 'desc';
else $sort['dir'] = 'asc';
if (isset ($_SESSION['page'][$client][$prefix]))
unset ($_SESSION['page'][$client][$prefix]);
if (!in_array($sort['what'], $allowed)) return false;
foreach ($sort as $key => $val)
$_SESSION['sort'][$client][$prefix . $key] = $sort[$key];
return $sort['what'] . ' ' . $sort['dir'];
* looks for a page variable hidden in the url or stored in session
* @return string/boolean sort string || false
if (!$prefix) return false;
$client = $this->options['client'];
$page = $this->getGet('page');
if (!$page && isset ($_SESSION['page'][$client][$prefix]))
$page = $_SESSION['page'][$client][$prefix];
if ($page) $_SESSION['page'][$client][$prefix] = $page;
|