[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Request
Страницы: 1, 2
OleKh
Было предложение использовать для выполнения задания пример реализации Yii, но у меня реализовано на книжном примере с моими доработками.

Есть сомнения по поводу надежности, т.к. открывается доступ к глобальным переменным, может быть использование секретного ключа закроет "дыру" в безопасности приложения.

index.php

require_once('Request.php');

new Request;

$request = RequestRegistry::getRequest();

//http://HTTP_HOST/index.php?arg=value&id=123
//http://HTTP_HOST/index.php?arg=value&id=ddd


e($request->getValue('get', 'arg'));

e($request->getValue('server', 'REQUEST_URI', 123));

$request->setValue('get', 'arg', 'newValue');

e($request->getValue('get', 'arg'));


function e($val)
{
echo "<pre>" . print_r($val, 1) . "</pre>";
}


Request.php
require_once('registry/Registry.php');
require_once('registry/RequestRegistry.php');

class Request
{

private $_variables;

function __construct()
{

$this->init();

$this->validateInput();

RequestRegistry::setRequest($this);

}

function init()
{

$this->_variables['server'] = $_SERVER;

if($_GET) {
$this->_variables['get'] = $_GET;
}

if($_POST) {
$this->_variables['post'] = $_POST;
}

if($_COOKIE) {
$this->_variables['cookie'] = $_COOKIE;
}

if($_FILES) {
$this->_variables['files'] = $_FILES;
}

}


function getValue($regKey, $valKey, $secKey = null)
{
$this->secKey($regKey, $secKey);
if (isset($this->_variables[$regKey])) {
return $this->_variables[$regKey][$valKey];
}
}


function setValue($regKey, $valKey, $val, $secKey = null)
{
$this->secKey($regKey, $secKey);
$this->_variables[$regKey][$valKey] = $val;
}

private function secKey($regKey, $secKey)
{
if(in_array($regKey, array('server', 'cookie')) ){
if ($secKey != '123') {
throw new Exception('Secret key is missing');
}
}
}


private function validateInput(){

foreach ($this->_variables as $regKey=>$vars) {

if(in_array($regKey, array('get', 'post'))) {

foreach ($vars as $key=>$val){
if (!$this->validateValue($key, $val)) {
throw new Exception('Input is not valid');
};
}

}

}
}


private function validateValue($key, $val){

switch ($key){
case 'id':
if(!is_numeric($val)) {
return false;
}
break;
default:
break;
}

return true;

}
}


Registry.php
abstract class Registry
{

abstract protected function get($key);

abstract protected function set($key, $val);

}


RequestRegistry.php
require_once('Registry.php');

class RequestRegistry extends Registry
{

private $_values = array();

private static $_instance;

private function __construct(){}

static function instance(){
if(!isset(self::$_instance)){
self::$_instance = new self();
}
return self::$_instance;
}

protected function get ($key){
if (isset($this->_values[$key])){
return $this->_values[$key];
}
return null;
}

protected function set ($key, $val){
$this->_values[$key] = $val;
}

static function getRequest (){
return self::instance()->get('request');
}

static function setRequest (Request $request){
self::instance()->set('request', $request);
}

}
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.