Files
VestaCP/web/vesta/core/Request.class.php

130 lines
2.8 KiB
PHP
Raw Normal View History

2011-06-14 00:22:25 +03:00
<?php
/**
* Request
*
* Holds parameters, decorating them and providing easy access
*
* @author Malishev Dima <dima.malishev@gmail.com>
2011-06-21 13:32:26 +03:00
* @author vesta, http://vestacp.com
* @copyright vesta 2010-2011
2011-06-14 00:22:25 +03:00
*/
class Request
{
2011-06-14 00:22:25 +03:00
protected $server = array();
protected $post = array();
protected $get = array();
protected $global = array();
protected $_merged = array();
//protected $_spell = array();
2011-06-14 00:22:25 +03:00
/**
*
*/
public function __construct()
{
2011-06-14 00:22:25 +03:00
$this->post = $_POST;
$this->get = $_GET;
$this->server = $_SERVER;
$this->mergeContainers();
}
/**
* Merge holders into single holder
*/
public function mergeContainers()
{
$this->_merged = array_merge($this->server,
2011-06-21 13:32:26 +03:00
$this->post,
$this->get,
$this->global);
//$this->_spell = json_decode($this->_merged['spell'], true);
2011-06-14 00:22:25 +03:00
}
/**
* Get parameter
*
* @param string $key
* @param mixed $default
* @return mixed
2011-06-14 00:22:25 +03:00
*/
public function getParameter($key, $default=false)
{
2011-12-29 13:41:30 +03:00
$param = isset($this->_merged[$key]) ? $this->_merged[$key] : $default;
if ($json = @json_decode($param, true)) {
return $json;
}
2011-12-29 13:41:30 +03:00
return $param;
2011-06-14 00:22:25 +03:00
}
2011-06-21 13:32:26 +03:00
/**
* Get spell variable from parameters
*
* @return array
*/
/*public function getSpell()
{
2011-06-14 00:22:25 +03:00
return $this->_spell;
}*/
2011-06-14 00:22:25 +03:00
2011-06-21 13:32:26 +03:00
/**
* Check if parameter is set
*
* @param string $key
* @param mixed $default
* @return mixed
*
*/
public function hasParameter($key, $default=false)
{
2011-06-14 00:22:25 +03:00
return isset($this->_merged[$key]);
}
/**
* Check if request is post
*
* TODO: write the method
*
* @return boolean
2011-06-14 00:22:25 +03:00
*/
public function isPost()
{
2011-06-14 00:22:25 +03:00
return true;
}
2011-12-29 13:41:30 +03:00
public function getUserIP()
{
return $_SERVER['REMOTE_ADDR'];
}
2011-06-14 00:22:25 +03:00
2011-06-21 13:32:26 +03:00
/**
* Dissassemble ajax method
* Breaks ajax requested method param into "ENTITY"."ACTION"
* for instance DNS.getList into "namespase" => DNS, "function" => "getList"
* for triggering ($DNS->getListExecuty();)
*
* TODO: write the method
*
* @return array
*/
static public function parseAjaxMethod($request)
{
if (!$request->hasParameter('jedi_method'))
{
2011-06-14 00:22:25 +03:00
throw new ProtectionException(Message::INVALID_METHOD);
}
$method = explode('.', $request->getParameter('jedi_method'));
if (count($method) != 2)
{
2011-06-14 00:22:25 +03:00
throw new ProtectionException(Message::INVALID_METHOD);
}
2011-12-29 13:41:30 +03:00
return array('namespace' => ($method[0]), 'function' => ($method[1]));
2011-06-14 00:22:25 +03:00
}
}