You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.8 KiB
PHP
110 lines
2.8 KiB
PHP
<?php
|
|
|
|
class WebException extends Exception
|
|
{
|
|
private $excname;
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param string $reason
|
|
* @param int $code
|
|
*/
|
|
function __construct($name, $reason, $code)
|
|
{
|
|
$this->excname = $name;
|
|
parent::__construct($reason, $code);
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return $this->excname;
|
|
}
|
|
}
|
|
|
|
abstract class WebService
|
|
{
|
|
private $func = null;
|
|
|
|
private $log = '';
|
|
|
|
public function log($message, $withTime = false) {
|
|
if($withTime) {
|
|
$message = date("d-m-Y h:m:s").' '.$message;
|
|
}
|
|
|
|
$this->log .= $message."\n";
|
|
}
|
|
|
|
public function Run()
|
|
{
|
|
$this->log("------------------");
|
|
$this->log("Start request", true);
|
|
$data = array();
|
|
|
|
try {
|
|
$this->Call();
|
|
$data["result"][$this->func] = $this->Output();
|
|
} catch (WebException $e) {
|
|
$data["error"]["code"] = $e->getCode();
|
|
$data["error"]["name"] = $e->getName();
|
|
$data["error"]["reason"] = $e->getMessage();
|
|
}
|
|
|
|
$this->Send($data);
|
|
|
|
$this->log("Request finished", true);
|
|
$this->log("------------------\n\n");
|
|
|
|
if(Configuration::get('log.active')) {
|
|
file_put_contents(Configuration::get('log.file'), $this->log, FILE_APPEND | LOCK_EX);
|
|
}
|
|
}
|
|
|
|
private function Call()
|
|
{
|
|
ob_start();
|
|
session_save_path(Configuration::get('session.save_path'));
|
|
session_start();
|
|
|
|
$params = empty($_GET) ? $_POST : $_GET;
|
|
if (empty($params)) {
|
|
throw new WebException ("CallArgument", "arguments error", -1);
|
|
}
|
|
|
|
if (!array_key_exists("func", $params)) {
|
|
throw new WebException ("CallArgFunction", "no 'func' specified", -2);
|
|
}
|
|
|
|
$this->func = $params["func"];
|
|
unset($params['func']);
|
|
|
|
if (!is_callable(array($this, $this->func))) {
|
|
throw new WebException ("CallFunction", "'func' method not available", -3);
|
|
}
|
|
|
|
$rm = new ReflectionMethod ($this, $this->func);
|
|
$nbParams = count($params);
|
|
$nbArgsFix = $rm->getNumberOfRequiredParameters();
|
|
$nbArgs = $rm->getNumberOfParameters();
|
|
|
|
/* Check the number of arguments. */
|
|
if ($nbArgs != $nbParams && $nbArgsFix != $nbParams) {
|
|
throw new WebException ("CallArgNumber", "you must provide " . $nbArgsFix . " arguments", 4);
|
|
}
|
|
|
|
$this->log("Calling '".$this->func."'");
|
|
call_user_func_array(array($this, $this->func), $params);
|
|
}
|
|
|
|
abstract protected function Output();
|
|
|
|
private function Send(array $data)
|
|
{
|
|
ob_clean();
|
|
flush();
|
|
|
|
$this->log("Data: ".print_r($data, true));
|
|
echo json_encode($data);
|
|
}
|
|
}
|