KT_atom_server.inc.php
4.18 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
<?php
class KT_atom_server {
public $output='';
protected $services=array();
protected $workspaceDetail=array();
protected $errors=array();
protected $queryArray=array();
protected $serviceName='';
protected $method='';
protected $workspace='';
protected $serviceObject = null;
protected $renderBody=true;
public function __construct(){
}
protected function hook_beforeDocCreate($doc){return true;}
protected function hook_beforeDocRender($doc){return true;}
/**
* Run the server switchboard - find the correct service class to instantiate, execute & render that class with the passed parameteres
*
*/
public function execute(){
$reqMethod=trim(strtoupper($_SERVER['REQUEST_METHOD']));
// Use preg_split to split on both / and & characters, for hybrid uri format
// e.g. ?dms/servicename/id&arg=val will be extracted to [dms], [id], [arg=val]
// This is mainly so that we properly extract the initial parameters, the remainder
// could be extracted from the $_GET array anyway, this is just making things cleaner
$queryArray=preg_split('/\/|&/',trim($_SERVER['QUERY_STRING'],'/'));
$rawRequest=@file_get_contents('php://input');
$workspace=strtolower(trim($queryArray[0]));
if (empty($workspace)) {
$workspace = 'servicedocument';
}
$serviceName=strtolower(trim($queryArray[1]));
$requestParams=array_slice($queryArray,2);
$this->queryArray=$queryArray;
$this->serviceName=$serviceName;
$this->method=$reqMethod;
$this->workspace=$workspace;
if($workspace=='servicedocument'){
$this->serviceDocument();
return;
}
$service=$this->getRegisteredService($workspace,$serviceName);
if(is_array($service)){
$serviceClass=$service['serviceClass'];
$serviceObject=new $serviceClass($reqMethod,$requestParams,$rawRequest);
if($this->hook_beforeDocRender($serviceObject)) $this->output=$serviceObject->render();
}else{
$serviceObject=new KT_atom_service($requestParams,$rawRequest);
$serviceObject->setStatus(KT_atom_service::STATUS_NOT_FOUND);
if($this->hook_beforeDocRender($serviceObject)) $this->output=$serviceObject->render();
}
$this->serviceObject=$serviceObject;
return $serviceObject;
}
public function registerService($workspaceCode=NULL,$serviceName=NULL,$serviceClass=NULL,$title=NULL){
$workspaceCode=strtolower(trim($workspaceCode));
$serviceName=strtolower(trim($serviceName));
$serviceRecord=array(
'fileName' =>$fileName,
'serviceClass' =>$serviceClass,
'title' =>$title
);
$this->services[$workspaceCode][$serviceName]=$serviceRecord;
}
public function addWorkspaceTag($workspaceCode=NULL,$TagName=NULL,$tagValue=NULL){
$workspaceCode=strtolower(trim($workspaceCode));
if(!isset($this->workspaceDetail[$workspaceCode]))$this->workspaceDetail[$workspaceCode]=array();
$this->workspaceDetail[$workspaceCode][$TagName]=$tagValue;
}
public function getRegisteredService($workspace,$serviceName=NULL){
$serviceName=strtolower(trim($serviceName));
if(isset($this->services[$workspace][$serviceName]))return $this->services[$workspace][$serviceName];
return false;
}
public function serviceDocument(){
$service=new KT_atom_serviceDoc(KT_APP_BASE_URI);
foreach($this->services as $workspace=>$collection){
//Creating the Default Workspace for use with standard atomPub Clients
$ws=$service->newWorkspace();
$hadDetail=false;
if(isset($this->workspaceDetail[$workspace]))if(is_array($this->workspaceDetail[$workspace])){
foreach ($this->workspaceDetail[$workspace] as $wsTag=>$wsValue){
$ws->appendChild($service->newElement($wsTag,$wsValue));
$hadDetail=true;
}
}
if(!$hadDetail){
$ws->appendChild($service->newElement('atom:title',$workspace));
}
foreach($collection as $serviceName=>$serviceInstance){
$col=$service->newCollection(KT_APP_BASE_URI.$workspace.'/'.$serviceName.'/',$serviceInstance['title'],$ws);
}
}
$this->output=$service->getAPPdoc();
}
public function render(){
ob_end_clean();
header('Content-type: text/xml');
if($this->renderBody)echo $this->output;
}
public function setNoContent($flag=false){
$this->renderBody=$flag?false:true;
}
}
?>