Streamline app compatibility between Solr and WS
parent
1ee3aa305d
commit
018846e567
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BSR\Lib\Exception;
|
namespace BSR\Lib\Exception;
|
||||||
|
|
||||||
class BookNotFoundException extends WebException {
|
class BookNotFoundException extends WebException {
|
||||||
public function __construct($code) {
|
public function __construct($code) {
|
||||||
parent::__construct('BookNotFound', "The book with code $code was not found", -404);
|
parent::__construct('BookNotFound', "The book with code $code was not found", -404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BSR\Lib\Exception;
|
namespace BSR\Lib\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception raised when an invalid attribute name is accessed
|
* Exception raised when an invalid attribute name is accessed
|
||||||
*/
|
*/
|
||||||
class InvalidAttributeException extends \Exception { }
|
class InvalidAttributeException extends \Exception { }
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BSR\Lib\Exception;
|
namespace BSR\Lib\Exception;
|
||||||
|
|
||||||
class SqlException extends \Exception
|
class SqlException extends \Exception
|
||||||
{
|
{
|
||||||
private $query;
|
private $query;
|
||||||
|
|
||||||
public function __construct($message = "Sql Error", $query = "")
|
public function __construct($message = "Sql Error", $query = "")
|
||||||
{
|
{
|
||||||
$this->query = $query;
|
$this->query = $query;
|
||||||
parent::__construct($message, 0);
|
parent::__construct($message, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSqlError()
|
public function getSqlError()
|
||||||
{
|
{
|
||||||
return $this->getMessage().' while executing: '.$this->query;
|
return $this->getMessage().' while executing: '.$this->query;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BSR\Lib\Exception;
|
namespace BSR\Lib\Exception;
|
||||||
|
|
||||||
class WebException extends \Exception
|
class WebException extends \Exception
|
||||||
{
|
{
|
||||||
private $excname;
|
private $excname;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $reason
|
* @param string $reason
|
||||||
* @param int $code
|
* @param int $code
|
||||||
*/
|
*/
|
||||||
function __construct($name, $reason, $code)
|
function __construct($name, $reason, $code)
|
||||||
{
|
{
|
||||||
$this->excname = $name;
|
$this->excname = $name;
|
||||||
parent::__construct($reason, $code);
|
parent::__construct($reason, $code);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->excname;
|
return $this->excname;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,122 +1,122 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BSR\Lib;
|
namespace BSR\Lib;
|
||||||
|
|
||||||
use BSR\Lib\Exception\WebException;
|
use BSR\Lib\Exception\WebException;
|
||||||
|
|
||||||
abstract class WebService
|
abstract class WebService
|
||||||
{
|
{
|
||||||
private $func = null;
|
private $func = null;
|
||||||
private $status = 200;
|
private $status = 200;
|
||||||
|
|
||||||
private $log = '';
|
private $log = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $message
|
* @param $message
|
||||||
* @param int $verbosity
|
* @param int $verbosity
|
||||||
* @param bool $withTime
|
* @param bool $withTime
|
||||||
*/
|
*/
|
||||||
public function log($message, $verbosity = 1, $withTime = false) {
|
public function log($message, $verbosity = 1, $withTime = false) {
|
||||||
if(Configuration::get('log.verbosity') < $verbosity) {
|
if(Configuration::get('log.verbosity') < $verbosity) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($withTime) {
|
if($withTime) {
|
||||||
$message = date("d-m-Y h:m:s").' '.$message;
|
$message = date("d-m-Y h:m:s").' '.$message;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->log .= $message."\n";
|
$this->log .= $message."\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Run()
|
public function Run()
|
||||||
{
|
{
|
||||||
$this->log("------------------");
|
$this->log("------------------");
|
||||||
$this->log("Start request", 1, true);
|
$this->log("Start request", 1, true);
|
||||||
$data = array();
|
$data = array();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = $this->Call();
|
$result = $this->Call();
|
||||||
$data["result"][$this->func] = $result;
|
$data["result"][$this->func] = $result;
|
||||||
} catch (WebException $e) {
|
} catch (WebException $e) {
|
||||||
$data["error"]["code"] = $e->getCode();
|
$data["error"]["code"] = $e->getCode();
|
||||||
$data["error"]["name"] = $e->getName();
|
$data["error"]["name"] = $e->getName();
|
||||||
$data["error"]["reason"] = $e->getMessage();
|
$data["error"]["reason"] = $e->getMessage();
|
||||||
$this->status = 400;
|
$this->status = 400;
|
||||||
|
|
||||||
$this->log(sprintf("Error : [%s] %s", $e->getCode(), $e->getName()));
|
$this->log(sprintf("Error : [%s] %s", $e->getCode(), $e->getName()));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$data["failure"]["message"] = $e->getMessage();
|
$data["failure"]["message"] = $e->getMessage();
|
||||||
$this->status = 500;
|
$this->status = 500;
|
||||||
|
|
||||||
$this->log(sprintf("Failure : %s", $e->getMessage()));
|
$this->log(sprintf("Failure : %s", $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->Send($data);
|
$this->Send($data);
|
||||||
|
|
||||||
$this->log("Request finished", 1, true);
|
$this->log("Request finished", 1, true);
|
||||||
$this->log("------------------\n\n");
|
$this->log("------------------\n\n");
|
||||||
|
|
||||||
if(Configuration::get('log.verbosity') > 0) {
|
if(Configuration::get('log.verbosity') > 0) {
|
||||||
file_put_contents(Configuration::get('log.file'), $this->log, FILE_APPEND | LOCK_EX);
|
file_put_contents(Configuration::get('log.file'), $this->log, FILE_APPEND | LOCK_EX);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function Call()
|
private function Call()
|
||||||
{
|
{
|
||||||
ob_start();
|
ob_start();
|
||||||
session_save_path(Configuration::get('session.save_path'));
|
session_save_path(Configuration::get('session.save_path'));
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
$params = empty($_GET) ? $_POST : $_GET;
|
$params = empty($_GET) ? $_POST : $_GET;
|
||||||
if (empty($params)) {
|
if (empty($params)) {
|
||||||
throw new WebException ("CallArgument", "arguments error", -1);
|
throw new WebException ("CallArgument", "arguments error", -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!array_key_exists("func", $params)) {
|
if (!array_key_exists("func", $params)) {
|
||||||
throw new WebException ("CallArgFunction", "no 'func' specified", -2);
|
throw new WebException ("CallArgFunction", "no 'func' specified", -2);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->func = $params["func"];
|
$this->func = $params["func"];
|
||||||
unset($params['func']);
|
unset($params['func']);
|
||||||
|
|
||||||
if (!is_callable(array($this, $this->func))) {
|
if (!is_callable(array($this, $this->func))) {
|
||||||
throw new WebException ("CallFunction", "'func' method not available", -3);
|
throw new WebException ("CallFunction", "'func' method not available", -3);
|
||||||
}
|
}
|
||||||
|
|
||||||
$rm = new \ReflectionMethod($this, $this->func);
|
$rm = new \ReflectionMethod($this, $this->func);
|
||||||
$nbParams = count($params);
|
$nbParams = count($params);
|
||||||
$nbArgsFix = $rm->getNumberOfRequiredParameters();
|
$nbArgsFix = $rm->getNumberOfRequiredParameters();
|
||||||
$nbArgs = $rm->getNumberOfParameters();
|
$nbArgs = $rm->getNumberOfParameters();
|
||||||
|
|
||||||
/* Check the number of arguments. */
|
/* Check the number of arguments. */
|
||||||
if ($nbParams < $nbArgsFix) {
|
if ($nbParams < $nbArgsFix) {
|
||||||
throw new WebException ("CallArgNumber", "you must provide at least " . $nbArgsFix . " arguments", 4);
|
throw new WebException ("CallArgNumber", "you must provide at least " . $nbArgsFix . " arguments", 4);
|
||||||
}
|
}
|
||||||
if ($nbParams > $nbArgs) {
|
if ($nbParams > $nbArgs) {
|
||||||
throw new WebException ("CallArgNumber", "you must provide at most " . $nbArgs . " arguments", 4);
|
throw new WebException ("CallArgNumber", "you must provide at most " . $nbArgs . " arguments", 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->log("Calling '".$this->func."'");
|
$this->log("Calling '".$this->func."'");
|
||||||
$this->log("Params: ".print_r($params, true), 2);
|
$this->log("Params: ".print_r($params, true), 2);
|
||||||
return call_user_func_array(array($this, $this->func), $params);
|
return call_user_func_array(array($this, $this->func), $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function Send(array $data)
|
private function Send(array $data)
|
||||||
{
|
{
|
||||||
static $status_messages = array(
|
static $status_messages = array(
|
||||||
200 => 'Ok',
|
200 => 'Ok',
|
||||||
400 => 'Bad request',
|
400 => 'Bad request',
|
||||||
404 => 'Not Found',
|
404 => 'Not Found',
|
||||||
403 => 'Not Authorized',
|
403 => 'Not Authorized',
|
||||||
500 => 'Server Error',
|
500 => 'Server Error',
|
||||||
);
|
);
|
||||||
|
|
||||||
header(sprintf('HTTP/1.0 %s %s', $this->status, $status_messages[$this->status]));
|
header(sprintf('HTTP/1.0 %s %s', $this->status, $status_messages[$this->status]));
|
||||||
|
|
||||||
ob_clean();
|
ob_clean();
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
$this->log("Data: ".print_r($data, true), 2);
|
$this->log("Data: ".print_r($data, true), 2);
|
||||||
echo json_encode($data);
|
echo json_encode($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,5 @@
|
|||||||
# README #
|
# README #
|
||||||
|
|
||||||
This repository contains the code for the WebService used internally at the [BSR](http://bibliothequesonore.ch/) by the Drupal website and both the iOS and Android applications.
|
This repository contains the code for the WebService used internally at the [BSR](http://bibliothequesonore.ch/) by the Drupal website and both the iOS and Android applications.
|
||||||
|
|
||||||
For more information, please consult the internal documentation : http://192.168.1.250/dokuwiki/doku.php?id=webservice
|
For more information, please consult the internal documentation : http://192.168.1.250/dokuwiki/doku.php?id=webservice
|
||||||
@ -1,20 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BSR;
|
namespace BSR;
|
||||||
|
|
||||||
ini_set('display_errors', 'On');
|
ini_set('display_errors', 'On');
|
||||||
|
|
||||||
// register an autoloader to automatically load classes
|
// register an autoloader to automatically load classes
|
||||||
// the namespace for the class must begin with BSR and
|
// the namespace for the class must begin with BSR and
|
||||||
// otherwise respect the PSR-4 standard
|
// otherwise respect the PSR-4 standard
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
$class = substr($class, strlen('BSR'));
|
$class = substr($class, strlen('BSR'));
|
||||||
$path = sprintf('%s/%s.php', __DIR__, str_replace('\\', '/', $class));
|
$path = sprintf('%s/%s.php', __DIR__, str_replace('\\', '/', $class));
|
||||||
|
|
||||||
if (file_exists($path)) {
|
if (file_exists($path)) {
|
||||||
require $path;
|
require $path;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$web = new NetBiblio();
|
$web = new NetBiblio();
|
||||||
$web->Run();
|
$web->Run();
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// this file is here for compatibility purpose, do not delete
|
// this file is here for compatibility purpose, do not delete
|
||||||
require_once('index.php');
|
require_once('index.php');
|
||||||
Loading…
Reference in New Issue