2008-03-31 01:18:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sapphire's generic RESTful server.
|
|
|
|
*
|
|
|
|
* NOTE: This is an alpha module and its API is currently very volatile. It functions, but it might change radically
|
|
|
|
* before the next release!
|
|
|
|
*
|
|
|
|
* This class gives your application a RESTful API for free. All you have to do is define static $api_access = true on
|
|
|
|
* the appropriate DataObjects. You will need to ensure that all of your data manipulation and security is defined in
|
|
|
|
* your model layer (ie, the DataObject classes) and not in your Controllers. This is the recommended design for Sapphire
|
|
|
|
* applications.
|
|
|
|
*
|
|
|
|
* - GET /api/v1/(ClassName)/(ID) - gets a database record
|
|
|
|
* - GET /api/v1/(ClassName)/(ID)/(Relation) - get all of the records linked to this database record by the given reatlion (NOT IMPLEMENTED YET)
|
|
|
|
* - GET /api/v1/(ClassName)?(Field)=(Val)&(Field)=(Val) - searches for matching database records (NOT IMPLEMENTED YET)
|
|
|
|
*
|
|
|
|
* - PUT /api/v1/(ClassName)/(ID) - updates a database record (NOT IMPLEMENTED YET)
|
|
|
|
* - PUT /api/v1/(ClassName)/(ID)/(Relation) - updates a relation, replacing the existing record(s) (NOT IMPLEMENTED YET)
|
|
|
|
* - POST /api/v1/(ClassName)/(ID)/(Relation) - updates a relation, appending to the existing record(s) (NOT IMPLEMENTED YET)
|
|
|
|
*
|
|
|
|
* - DELETE /api/v1/(ClassName)/(ID) - deletes a database record (NOT IMPLEMENTED YET)
|
|
|
|
* - DELETE /api/v1/(ClassName)/(ID)/(Relation)/(ForeignID) - remove the relationship between two database records, but don't actually delete the foreign object (NOT IMPLEMENTED YET)
|
|
|
|
*
|
|
|
|
* - POST /api/v1/(ClassName)/(ID)/(MethodName) - executes a method on the given object (e.g, publish)
|
2008-06-15 15:33:53 +02:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage api
|
2008-03-31 01:18:04 +02:00
|
|
|
*/
|
|
|
|
class RestfulServer extends Controller {
|
2008-08-09 05:19:54 +02:00
|
|
|
static $url_handlers = array(
|
|
|
|
'$ClassName/#ID' => 'handleItem',
|
|
|
|
'$ClassName' => 'handleList',
|
|
|
|
);
|
|
|
|
|
2008-03-31 01:18:04 +02:00
|
|
|
protected static $api_base = "api/v1/";
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
function handleItem($params) {
|
|
|
|
return new RestfulServer_Item(DataObject::get_by_id($params["ClassName"], $params["ID"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleList($params) {
|
|
|
|
return new RestfulServer_List(DataObject::get($params["ClassName"],""));
|
|
|
|
}
|
2008-03-31 01:18:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This handler acts as the switchboard for the controller.
|
|
|
|
* Since no $Action url-param is set, all requests are sent here.
|
|
|
|
*/
|
|
|
|
function index() {
|
|
|
|
ContentNegotiator::disable();
|
|
|
|
|
|
|
|
$requestMethod = $_SERVER['REQUEST_METHOD'];
|
2008-08-09 04:00:40 +02:00
|
|
|
|
|
|
|
if(!isset($this->urlParams['ClassName'])) return $this->notFound();
|
2008-03-31 01:18:04 +02:00
|
|
|
$className = $this->urlParams['ClassName'];
|
2008-08-09 04:00:40 +02:00
|
|
|
$id = (isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null;
|
2008-08-09 04:16:46 +02:00
|
|
|
$relation = (isset($this->urlParams['Relation'])) ? $this->urlParams['Relation'] : null;
|
|
|
|
|
|
|
|
// This is a little clumsy and should be improved with the new TokenisedURL that's coming
|
|
|
|
if(strpos($relation,'.') !== false) list($relation, $extension) = explode('.', $relation, 2);
|
|
|
|
else if(strpos($id,'.') !== false) list($id, $extension) = explode('.', $id, 2);
|
|
|
|
else if(strpos($className,'.') !== false) list($className, $extension) = explode('.', $className, 2);
|
|
|
|
else $extension = null;
|
|
|
|
|
|
|
|
// Determine mime-type from extension
|
|
|
|
$contentMap = array(
|
|
|
|
'xml' => 'text/xml',
|
|
|
|
'json' => 'text/json',
|
|
|
|
'js' => 'text/json',
|
|
|
|
'xhtml' => 'text/html',
|
|
|
|
'html' => 'text/html',
|
|
|
|
);
|
|
|
|
$contentType = isset($contentMap[$extension]) ? $contentMap[$extension] : 'text/xml';
|
2008-03-31 01:18:04 +02:00
|
|
|
|
|
|
|
switch($requestMethod) {
|
|
|
|
case 'GET':
|
2008-08-09 04:16:46 +02:00
|
|
|
return $this->getHandler($className, $id, $relation, $contentType);
|
2008-03-31 01:18:04 +02:00
|
|
|
|
|
|
|
case 'PUT':
|
2008-08-09 04:16:46 +02:00
|
|
|
return $this->putHandler($className, $id, $relation, $contentType);
|
2008-03-31 01:18:04 +02:00
|
|
|
|
|
|
|
case 'DELETE':
|
2008-08-09 04:16:46 +02:00
|
|
|
return $this->deleteHandler($className, $id, $relation, $contentType);
|
2008-03-31 01:18:04 +02:00
|
|
|
|
|
|
|
case 'POST':
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for object read.
|
|
|
|
*
|
|
|
|
* The data object will be returned in the following format:
|
|
|
|
*
|
|
|
|
* <ClassName>
|
|
|
|
* <FieldName>Value</FieldName>
|
|
|
|
* ...
|
|
|
|
* <HasOneRelName id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* ...
|
|
|
|
* <HasManyRelName>
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* </HasManyRelName>
|
|
|
|
* ...
|
|
|
|
* <ManyManyRelName>
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* <ForeignClass id="ForeignID" href="LinkToForeignRecordInAPI" />
|
|
|
|
* </ManyManyRelName>
|
|
|
|
* </ClassName>
|
|
|
|
*
|
|
|
|
* Access is controlled by two variables:
|
|
|
|
*
|
|
|
|
* - static $api_access must be set. This enables the API on a class by class basis
|
|
|
|
* - $obj->canView() must return true. This lets you implement record-level security
|
|
|
|
*/
|
2008-08-09 04:16:46 +02:00
|
|
|
protected function getHandler($className, $id, $relation, $contentType) {
|
|
|
|
if($id) {
|
|
|
|
$obj = DataObject::get_by_id($className, $id);
|
|
|
|
if(!$obj) {
|
|
|
|
return $this->notFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$obj->stat('api_access') || !$obj->canView()) {
|
|
|
|
return $this->permissionFailure();
|
2008-03-31 01:18:04 +02:00
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
|
|
|
|
if($relation) {
|
|
|
|
if($obj->hasMethod($relation)) $obj = $obj->$relation();
|
|
|
|
else return $this->notFound();
|
|
|
|
}
|
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
} else {
|
2008-08-09 04:16:46 +02:00
|
|
|
$obj = DataObject::get($className, "");
|
|
|
|
if(!singleton($className)->stat('api_access')) {
|
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TO DO - inspect that Accept header as well. $_GET['accept'] can still be checked, as it's handy for debugging
|
|
|
|
switch($contentType) {
|
|
|
|
case "text/xml":
|
|
|
|
$this->getResponse()->addHeader("Content-type", "text/xml");
|
|
|
|
if($obj instanceof DataObjectSet) return $this->dataObjectSetAsXML($obj);
|
|
|
|
else return $this->dataObjectAsXML($obj);
|
|
|
|
|
|
|
|
case "text/json":
|
|
|
|
//$this->getResponse()->addHeader("Content-type", "text/json");
|
|
|
|
if($obj instanceof DataObjectSet) return $this->dataObjectSetAsJSON($obj);
|
|
|
|
else return $this->dataObjectAsJSON($obj);
|
|
|
|
|
|
|
|
case "text/html":
|
|
|
|
case "application/xhtml+xml":
|
|
|
|
if($obj instanceof DataObjectSet) return $this->dataObjectSetAsXHTML($obj);
|
|
|
|
else return $this->dataObjectAsXHTML($obj);
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an XML representation of the given DataObject.
|
|
|
|
*/
|
2008-08-09 04:16:46 +02:00
|
|
|
protected function dataObjectAsXML(DataObject $obj, $includeHeader = true) {
|
2008-03-31 10:49:27 +02:00
|
|
|
$className = $obj->class;
|
|
|
|
$id = $obj->ID;
|
2008-08-09 04:16:46 +02:00
|
|
|
$objHref = Director::absoluteURL(self::$api_base . "$obj->class/$obj->ID");
|
2008-03-31 10:49:27 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
$json = "";
|
|
|
|
if($includeHeader) $json .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
|
$json .= "<$className href=\"$objHref.xml\">\n";
|
2008-03-31 10:49:27 +02:00
|
|
|
foreach($obj->db() as $fieldName => $fieldType) {
|
2008-08-09 04:16:46 +02:00
|
|
|
if(is_object($obj->$fieldName)) {
|
|
|
|
$json .= $obj->$fieldName->toXML();
|
|
|
|
} else {
|
|
|
|
$json .= "<$fieldName>" . Convert::raw2xml($obj->$fieldName) . "</$fieldName>\n";
|
|
|
|
}
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2008-03-31 01:18:04 +02:00
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
foreach($obj->has_one() as $relName => $relClass) {
|
|
|
|
$fieldName = $relName . 'ID';
|
|
|
|
if($obj->$fieldName) {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/" . $obj->$fieldName);
|
|
|
|
} else {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
|
2008-03-31 01:18:04 +02:00
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
$json .= "<$relName linktype=\"has_one\" href=\"$href.xml\" id=\"{$obj->$fieldName}\" />\n";
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
2008-03-31 01:18:04 +02:00
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
foreach($obj->has_many() as $relName => $relClass) {
|
2008-08-09 04:16:46 +02:00
|
|
|
$json .= "<$relName linktype=\"has_many\" href=\"$objHref/$relName.xml\">\n";
|
2008-03-31 10:49:27 +02:00
|
|
|
$items = $obj->$relName();
|
|
|
|
foreach($items as $item) {
|
|
|
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
2008-08-09 04:16:46 +02:00
|
|
|
$json .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\" />\n";
|
2008-03-31 01:18:04 +02:00
|
|
|
}
|
2008-03-31 10:49:27 +02:00
|
|
|
$json .= "</$relName>\n";
|
|
|
|
}
|
2008-03-31 01:18:04 +02:00
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
foreach($obj->many_many() as $relName => $relClass) {
|
2008-08-09 04:16:46 +02:00
|
|
|
$json .= "<$relName linktype=\"many_many\" href=\"$objHref/$relName.xml\">\n";
|
2008-03-31 10:49:27 +02:00
|
|
|
$items = $obj->$relName();
|
|
|
|
foreach($items as $item) {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
2008-08-09 04:16:46 +02:00
|
|
|
$json .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\" />\n";
|
2008-03-31 01:18:04 +02:00
|
|
|
}
|
2008-03-31 10:49:27 +02:00
|
|
|
$json .= "</$relName>\n";
|
|
|
|
}
|
2008-03-31 01:18:04 +02:00
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
$json .= "</$className>";
|
2008-03-31 01:18:04 +02:00
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
return $json;
|
2008-03-31 01:18:04 +02:00
|
|
|
}
|
2008-03-31 10:49:27 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
/**
|
|
|
|
* Generate an XML representation of the given DataObject.
|
|
|
|
*/
|
|
|
|
protected function dataObjectSetAsXML(DataObjectSet $set) {
|
|
|
|
$className = $set->class;
|
|
|
|
|
|
|
|
$json = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<$className>\n";
|
|
|
|
foreach($set as $item) {
|
|
|
|
if($item->canView()) $json .= $this->dataObjectAsXML($item, false);
|
|
|
|
}
|
|
|
|
$json .= "</$className>";
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
}
|
2008-03-31 01:18:04 +02:00
|
|
|
|
2008-03-31 10:49:27 +02:00
|
|
|
/**
|
|
|
|
* Generate an XML representation of the given DataObject.
|
|
|
|
*/
|
|
|
|
protected function dataObjectAsJSON(DataObject $obj) {
|
|
|
|
$className = $obj->class;
|
|
|
|
$id = $obj->ID;
|
|
|
|
|
|
|
|
$json = "{\n className : \"$className\",\n";
|
|
|
|
foreach($obj->db() as $fieldName => $fieldType) {
|
2008-08-09 04:16:46 +02:00
|
|
|
if(is_object($obj->$fieldName)) {
|
|
|
|
$jsonParts[] = "$fieldName : " . $obj->$fieldName->toJSON();
|
|
|
|
} else {
|
|
|
|
$jsonParts[] = "$fieldName : \"" . Convert::raw2js($obj->$fieldName) . "\"";
|
|
|
|
}
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach($obj->has_one() as $relName => $relClass) {
|
|
|
|
$fieldName = $relName . 'ID';
|
|
|
|
if($obj->$fieldName) {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/" . $obj->$fieldName);
|
|
|
|
} else {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
|
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
$jsonParts[] = "$relName : { className : \"$relClass\", href : \"$href.json\", id : \"{$obj->$fieldName}\" }";
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach($obj->has_many() as $relName => $relClass) {
|
|
|
|
$jsonInnerParts = array();
|
|
|
|
$items = $obj->$relName();
|
|
|
|
foreach($items as $item) {
|
|
|
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
2008-08-09 04:16:46 +02:00
|
|
|
$jsonInnerParts[] = "{ className : \"$relClass\", href : \"$href.json\", id : \"{$obj->$fieldName}\" }";
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
|
|
|
$jsonParts[] = "$relName : [\n " . implode(",\n ", $jsonInnerParts) . " \n ]";
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($obj->many_many() as $relName => $relClass) {
|
|
|
|
$jsonInnerParts = array();
|
|
|
|
$items = $obj->$relName();
|
|
|
|
foreach($items as $item) {
|
|
|
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
2008-08-09 04:16:46 +02:00
|
|
|
$jsonInnerParts[] = " { className : \"$relClass\", href : \"$href.json\", id : \"{$obj->$fieldName}\" }";
|
2008-03-31 10:49:27 +02:00
|
|
|
}
|
|
|
|
$jsonParts[] = "$relName : [\n " . implode(",\n ", $jsonInnerParts) . "\n ]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "{\n " . implode(",\n ", $jsonParts) . "\n}";
|
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an XML representation of the given DataObject.
|
|
|
|
*/
|
|
|
|
protected function dataObjectSetAsJSON(DataObjectSet $set) {
|
|
|
|
$jsonParts = array();
|
|
|
|
foreach($set as $item) {
|
|
|
|
if($item->canView()) $jsonParts[] = $this->dataObjectAsJSON($item);
|
|
|
|
}
|
|
|
|
return "[\n" . implode(",\n", $jsonParts) . "\n]";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-31 01:18:04 +02:00
|
|
|
/**
|
|
|
|
* Handler for object delete
|
|
|
|
*/
|
|
|
|
protected function deleteHandler($className, $id) {
|
|
|
|
if($id) {
|
|
|
|
$obj = DataObject::get_by_id($className, $id);
|
|
|
|
if($obj->stat('api_access') && $obj->canDelete()) {
|
|
|
|
$obj->delete();
|
|
|
|
} else {
|
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for object write
|
|
|
|
*/
|
|
|
|
protected function putHandler($className, $id) {
|
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for object append / method call
|
|
|
|
*/
|
|
|
|
protected function postHandler($className, $id) {
|
|
|
|
return $this->permissionFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function permissionFailure() {
|
|
|
|
// return a 401
|
|
|
|
$this->getResponse()->setStatusCode(403);
|
|
|
|
return "You don't have access to this item through the API.";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function notFound() {
|
|
|
|
// return a 404
|
|
|
|
$this->getResponse()->setStatusCode(404);
|
|
|
|
return "That object wasn't found";
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restful server handler for a DataObjectSet
|
|
|
|
*/
|
|
|
|
class RestfulServer_List {
|
|
|
|
static $url_handlers = array(
|
|
|
|
'#ID' => 'handleItem',
|
|
|
|
);
|
|
|
|
|
|
|
|
function __construct($list) {
|
|
|
|
$this->list = $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleItem($params) {
|
|
|
|
return new RestulServer_Item($this->list->getById($params['ID']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restful server handler for a single DataObject
|
|
|
|
*/
|
|
|
|
class RestfulServer_Item {
|
|
|
|
static $url_handlers = array(
|
|
|
|
'$Relation' => 'handleRelation',
|
|
|
|
);
|
|
|
|
|
|
|
|
function __construct($item) {
|
|
|
|
$this->item = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleRelation($params) {
|
|
|
|
$funcName = $params['Relation'];
|
|
|
|
$relation = $this->item->$funcName();
|
|
|
|
|
|
|
|
if($relation instanceof DataObjectSet) return new RestfulServer_List($relation);
|
|
|
|
else return new RestfulServer_Item($relation)l
|
|
|
|
}
|
|
|
|
}
|