routeSQL : add REST support (without auth, nor chained mode)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2037 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-03-21 15:56:35 +00:00
parent cb5f651247
commit db4f3da2f8

View File

@@ -66,6 +66,8 @@ class routeSQL
public $rendererHTMLmethod = false;
/** The layout HTML to use for HTML pages */
public $rendererHTMLlayout = false;
/** The extensions allowed in REST */
public $extensionsAllowed = array ("json", "xml");
/** The model file containing the database description */
private $model_file = "";
@@ -278,7 +280,128 @@ class routeSQL
return $content;
}
/** Create the routes and the associated actions */
/** Create the routes for REST pages and the associated actions */
public function routesREST ()
{
$route = new route ();
$route->debug = $this->debug;;
$route->allowSlashes=false;
$route->get ("rest/".$this->url_prefix."(\.{extension})?".
"(\?({p1}=({v1})?)(&{p2}=({v2})?(&{p3}=({v3})?)?)?)?",
function ($extension, $p1, $v1, $p2, $v2, $p3, $v3, $chain)
{
if ($p1 === "search") $search = $v1;
if ($p2 === "search") $search = $v2;
if ($p3 === "search") $search = $v3;
if (!isset ($search) || $search === null || $search === "") $search = "";
if (!isset ($extension) || $extension === null || $extension === "")
$extension = reset ($this->extensionsAllowed);
if (!in_array ($extension, $this->extensionsAllowed))
throw new Exception (dgettext("domframework","Extension not allowed"),
403);
$search = rawurldecode ($search);
$this->connect();
$titles = $this->objectDB->titles ();
unset ($titles[$this->chainedForeign]);
$foreignSelect = null;
if ($this->chained !== null)
$foreignSelect = array (array ($this->chainedForeign, $chain));
if ($search === "")
$datas = $this->objectDB->read (null, array_keys($titles), null, null,
$foreignSelect);
else
{
$criteria = array ();
foreach (array_keys($titles) as $column)
{
$s = $search;
if ($search[0] === "^")
$s = substr ($s, 1);
else
$s = "%$s";
if (substr ($search, -1) === "$")
$s = substr ($s, 0, -1);
else
$s = "$s%";
$criteria[] = array ($column, "$s", "LIKE");
}
$datas = $this->objectDB->read ($criteria, array_keys ($titles), null,
true, $foreignSelect);
}
$this->renderrest ($extension, $datas);
});
$route->post ("rest/".$this->url_prefix."(\.{extension})?",
function ($extension)
{
if (!isset ($extension) || $extension === null || $extension === "")
$extension = reset ($this->extensionsAllowed);
if (!in_array ($extension, $this->extensionsAllowed))
throw new Exception (dgettext("domframework","Extension not allowed"),
403);
$this->connect();
$values = $_POST;
$errors = $this->objectDB->verify ($values);
if (count ($errors) > 0)
$this->renderrest ($extension, $errors, 400);
try
{
$this->objectDB->insert ($values);
$this->renderrest ($extension, "OK", 200);
}
catch (Exception $e)
{
$this->renderrest ($extension, $e->getMessage(), 400);
}
});
$route->put ("rest/".$this->url_prefix."(\.{extension})?/{id}",
function ($extension, $id)
{
if (!isset ($extension) || $extension === null || $extension === "")
$extension = reset ($this->extensionsAllowed);
if (!in_array ($extension, $this->extensionsAllowed))
throw new Exception (dgettext("domframework","Extension not allowed"),
403);
$this->connect();
parse_str (file_get_contents ("php://input"), $values);
$errors = $this->objectDB->verify ($values, $id);
if (count ($errors) > 0)
$this->renderrest ($extension, $errors, 400);
try
{
$this->objectDB->update ($id, $values);
$this->renderrest ($extension, "OK", 200);
}
catch (Exception $e)
{
$this->renderrest ($extension, $e->getMessage(), 400);
}
});
$route->delete ("rest/".$this->url_prefix."(\.{extension})?/{id}",
function ($extension, $id)
{
if (!isset ($extension) || $extension === null || $extension === "")
$extension = reset ($this->extensionsAllowed);
if (!in_array ($extension, $this->extensionsAllowed))
throw new Exception (dgettext("domframework","Extension not allowed"),
403);
$this->connect();
try
{
$this->objectDB->delete ($id);
$this->renderrest ($extension, "OK", 200);
}
catch (Exception $e)
{
$this->renderrest ($extension, $e->getMessage(), 400);
}
});
}
/** Create the routes for HTML pages and the associated actions */
public function routesHTML ()
{
// If chained routeSQL, the url_prefix must be adapted
@@ -1108,6 +1231,18 @@ class routeSQL
echo $html->out ($data, FALSE,
$this->rendererHTMLclass, $this->rendererHTMLmethod,
$this->rendererHTMLlayout, $replacement);
exit;
}
private function renderrest ($extension, $data, $getCode=200)
{
require_once ("domframework/output$extension.php");
$http = new http ();
@header ($_SERVER["SERVER_PROTOCOL"]." $getCode ".
$http->codetext ($getCode));
$class = "output$extension";
$json = new $class ();
echo $json->out ($data);
exit;
}
}