|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* extends the standard RestfulServer to provide better access to extended classes. |
|
4
|
|
|
* |
|
5
|
|
|
* see: http://api.silverstripe.org/2.4/sapphire/api/RestfulServer.html |
|
6
|
|
|
* |
|
7
|
|
|
* You can show JSON by hacking: RestfulServer::getDataFormatter |
|
8
|
|
|
* NOTE: JSON IS NOT AVAILABLE YET WITHIN RESTFUL SERVER |
|
9
|
|
|
* |
|
10
|
|
|
* @todo: |
|
11
|
|
|
* - fix http://site/api/ecommerce/v1/Order/123/BillingAddress.xml |
|
12
|
|
|
* - fix http://site/api/ecommerce/v1/Order/123/ShippingAddress.xml |
|
13
|
|
|
* - fix http://site/api/ecommerce/v1/Order/123/Member.xml |
|
14
|
|
|
* |
|
15
|
|
|
* <b>Test Post</b> |
|
16
|
|
|
* <code> |
|
17
|
|
|
* $baseURL = Director::absoluteBaseURL(); |
|
18
|
|
|
* // 1) My Personal Data |
|
19
|
|
|
* $className = 'EcommerceClassWithEditableFields'; |
|
20
|
|
|
* $fields = array( |
|
21
|
|
|
* 'MyField' => 1 |
|
22
|
|
|
* ); |
|
23
|
|
|
* // 2) The Query |
|
24
|
|
|
* $url = "{$baseURL}/api/ecommerce/v1/{$className}.xml"; |
|
25
|
|
|
* $body = $fields; |
|
26
|
|
|
* $c = curl_init($url); |
|
27
|
|
|
* curl_setopt($c, CURLOPT_POST, true); |
|
28
|
|
|
* curl_setopt($c, CURLOPT_POSTFIELDS, $body); |
|
29
|
|
|
* curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
|
30
|
|
|
* $page = curl_exec($c); |
|
31
|
|
|
* curl_close($c); |
|
32
|
|
|
|
|
33
|
|
|
* // 3) The XML Result |
|
34
|
|
|
|
|
35
|
|
|
* return $page; |
|
36
|
|
|
* </code> |
|
37
|
|
|
* |
|
38
|
|
|
* |
|
39
|
|
|
* @authors: Romain [at] Sunny Side Up .co.nz |
|
40
|
|
|
* @package: ecommerce |
|
41
|
|
|
* @sub-package: api |
|
42
|
|
|
* @inspiration: Silverstripe Ltd, Jeremy |
|
43
|
|
|
**/ |
|
44
|
|
|
class EcommerceRestfulServer extends RestfulServer |
|
45
|
|
|
{ |
|
46
|
|
|
public function index() |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
XMLDataFormatter::$api_base = 'api/ecommerce/v1/'; |
|
|
|
|
|
|
49
|
|
|
if (!isset($this->urlParams['ClassName'])) { |
|
50
|
|
|
return $this->notFound(); |
|
51
|
|
|
} |
|
52
|
|
|
$className = $this->urlParams['ClassName']; |
|
53
|
|
|
$id = (isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null; |
|
54
|
|
|
$relation = (isset($this->urlParams['Relation'])) ? $this->urlParams['Relation'] : null; |
|
55
|
|
|
|
|
56
|
|
|
// Check input formats |
|
57
|
|
|
if (!class_exists($className)) { |
|
58
|
|
|
return $this->notFound(); |
|
59
|
|
|
} |
|
60
|
|
|
if ($id && !is_numeric($id)) { |
|
61
|
|
|
return $this->notFound(); |
|
62
|
|
|
} |
|
63
|
|
|
if ($relation && !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)) { |
|
64
|
|
|
return $this->notFound(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// fix |
|
68
|
|
|
if ($id) { |
|
69
|
|
|
$obj = $className::get()->byID($id); |
|
70
|
|
|
if ($obj) { |
|
71
|
|
|
$className = $this->urlParams['ClassName'] = $obj->ClassName; |
|
72
|
|
|
} else { |
|
73
|
|
|
return $this->notFound(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// if api access is disabled, don't proceed |
|
78
|
|
|
$apiAccess = singleton($className)->stat('api_access'); |
|
79
|
|
|
if (!$apiAccess) { |
|
80
|
|
|
return $this->permissionFailure(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// authenticate through HTTP BasicAuth |
|
84
|
|
|
$this->member = $this->authenticate(); |
|
85
|
|
|
|
|
86
|
|
|
// handle different HTTP verbs |
|
87
|
|
|
if ($this->request->isGET() || $this->request->isHEAD()) { |
|
88
|
|
|
return $this->getHandler($className, $id, $relation); |
|
89
|
|
|
} |
|
90
|
|
|
if ($this->request->isPOST()) { |
|
91
|
|
|
return $this->postHandler($className, $id, $relation); |
|
92
|
|
|
} |
|
93
|
|
|
if ($this->request->isPUT()) { |
|
94
|
|
|
return $this->putHandler($className, $id, $relation); |
|
95
|
|
|
} |
|
96
|
|
|
if ($this->request->isDELETE()) { |
|
97
|
|
|
return $this->deleteHandler($className, $id, $relation); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// if no HTTP verb matches, return error |
|
101
|
|
|
return $this->methodNotAllowed(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.