Go to the documentation of this file.00001 <?php
00010 final class http_vars {
00011
00017 private static $instance;
00018
00022 private function __construct() {}
00023
00029 public static function getInstance() {
00030 if (is_null(self::$instance))
00031 self::$instance = new http_vars();
00032 return self::$instance;
00033 }
00034
00048 public function getVar($prm) {
00049 if (is_string($prm))
00050 $p = array('name'=>$prm);
00051 else
00052 $p = &$prm;
00053 $ret = null;
00054 if (config::initTab($p, array(
00055 'name'=>null,
00056 'nameIn'=>'',
00057 'method'=>array('post', 'get'),
00058 'trim'=>true
00059 ))) {
00060 $matches = explode('|', str_replace(
00061 array('[]', '][', '[', ']'),
00062 array('', '|', '|', ''),
00063 $p['name']
00064 ));
00065 $name = empty($matches)? array($p['name']): $matches;
00066 if (is_array($p['method'])) {
00067 for($i = 0; $i < count($p['method']) && $ret === null; $i++) {
00068 $act = '_'.strtoupper($p['method'][$i]);
00069 $ret = utils::getValInArray($GLOBALS[$act], $name);
00070 }
00071 } else {
00072 $act = '_'.strtoupper($p['method']);
00073 $ret = utils::getValInArray($GLOBALS[$act], $name);
00074 }
00075 if ($act == '_GET' && !is_null($ret))
00076 $ret = is_array($ret) ? array_map('urldecode', $ret) : urldecode($ret);
00077 if ($p['trim'] && !is_null($ret) && !is_array($ret))
00078 $ret = trim($ret);
00079 }
00080 $prm = array_merge(array('default'=>null), $p);
00081 if (is_array($prm['default'])) {
00082 if (is_null($ret) || !in_array($ret, $prm['default'])) {
00083 $ret = $prm['default'][0];
00084 }
00085 } else if (is_null($ret))
00086 $ret = $prm['default'];
00087
00088 $ret = utils::htmlIn($ret);
00089 if (is_array($ret) && !empty($p['nameIn']))
00090 return array_key_exists($p['nameIn'], $ret) ? $ret[$p['nameIn']] : null;
00091 else
00092 return $ret;
00093 }
00094
00103 public function post($name, $default=null) {
00104 return $this->getVar(array(
00105 'name'=>$name,
00106 'method'=>'post',
00107 'default'=>$default));
00108 }
00109
00116 public function getVars($method='post') {
00117 $act = '_'.strtoupper($method);
00118 return utils::htmlIn($GLOBALS[$act]);
00119 }
00120
00127 public function posts() {
00128 return $this->getvars('post');
00129 }
00130
00137 public function gets() {
00138 return $this->getvars('get');
00139 }
00140
00149 public function get($name, $default=null) {
00150 return $this->getVar(array(
00151 'name'=>$name,
00152 'method'=>'get',
00153 'default'=>$default));
00154 }
00155
00164 public function session($name, $default=null) {
00165 return $this->getVar(array(
00166 'name'=>$name,
00167 'method'=>'session',
00168 'default'=>$default));
00169 }
00170
00179 public function cookie($name, $default=null) {
00180 return $this->getVar(array(
00181 'name'=>$name,
00182 'method'=>'cookie',
00183 'default'=>$default));
00184 }
00185
00186 }