KT_atom_service.inc.php
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
class KT_atom_service{
const STATUS_OK = '200 OK';
const STATUS_NOT_FOUND = '204 No Content';
const STATUS_NO_CONTENT = '204 No Content';
const STATUS_NOT_ALLOWED = '204 Not Allowed';
const STATUS_CREATED = '201 Created';
const STATUS_UPDATED = '200 Updated';
const STATUS_NOT_MODIFIED = '304 Not Modified'; // For use with ETag & If-None-Match headers.
const STATUS_BAD_REQUEST = '400 Bad Request'; // Client issued a wrongly constructed request
const STATUS_NOT_AUTHENTICATED = '401 Not Authenticated';
const STATUS_PRECONDITION_FAILED = '412 Precondition Failed'; // Could not update document because another a newer version exist on the server than the one you are trying to update
const STATUS_SERVER_ERROR = '500 Internal Server Error'; // Server encountered an error processing the request
/*
Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
*/
public $responseFeed=NULL;
public $responseHeader=NULL;
public $method='';
public $params='';
public $rawContent='';
public $parsedXMLContent='';
public $headers=array();
public function __construct($method,$params,$content){
$this->method=$method;
$this->params=$params;
$this->rawContent=$content;
$this->parseHeaders();
$this->parsedXMLContent=$this->xml2array($this->rawContent);
$this->setStatus(self::STATUS_OK);
$this->responseFeed=new KT_atom_responseFeed(KT_APP_BASE_URI);
switch(strtoupper($this->method)){
case 'GET': $this->GET_action();break;
case 'PUT': $this->PUT_action();break;
case 'POST': $this->POST_action();break;
case 'DELETE': $this->DELETE_action();break;
default: $this->UNSUPPORTED_action();break;
}
}
public function GET_action(){
$this->setStatus(KT_atom_service::STATUS_OK);
}
public function PUT_action(){
$this->setStatus(KT_atom_service::STATUS_NOT_FOUND );
}
public function POST_action(){
$this->setStatus(KT_atom_service::STATUS_NOT_FOUND );
}
public function DELETE_action(){
$this->setStatus(KT_atom_service::STATUS_NOT_FOUND );
}
public function UNSUPPORTED_action(){
$this->setStatus(KT_atom_service::STATUS_NOT_FOUND );
}
public function getHeaders()
{
return $this->headers;
}
public function render(){
return $this->responseFeed->render();
}
protected function xml2array($xml)
{
if (trim($xml) == '') return array();
if(class_exists('XMLns2array'))
{
$array=XMLns2array::parse($xml);
}
else
{
$array=json_decode(json_encode(@simplexml_load_string($xml)), true);
}
return $array;
}
protected function parseHeaders(){
$headers=null;
if(function_exists('http_get_request_headers')){ //Try to use pcre_http library if it exists
$headers=http_get_request_headers();
}else{
if(function_exists('apache_request_headers')){ //If not: try to use apache specific headers
$headers=apache_request_headers();
}else{ //If not: not supported - empty headers
$headers=array();
}
}
$this->headers=$headers;
}
public function setStatus($status=NULL){
header("HTTP/1.1 ".$status);
}
public function setEtag($etagValue=NULL){
if($etagValue)header('ETag: '.$etagValue);
}
}
?>