ajaxhandler.php
2.89 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
<?php
class ajaxHandler{
public $ret=NULL;
public $req=NULL;
public $version=NULL;
public $auth=NULL;
public $request=NULL;
public $kt=NULL;
public $authenticator=NULL;
public function __construct(&$ret=NULL,&$kt){
// set a local copy of the json request wrapper
$this->req=new jsonWrapper(isset($_GET['request'])?$_GET['request']:(isset($_POST['request'])?$_POST['request']:''));
$this->auth=$this->req->jsonArray['auth'];
$this->request=$this->req->jsonArray['request'];
// set the response object
if(get_class($ret)=='jsonResponseObject'){
$this->ret=&$ret;
}else{
$this->ret=new jsonResponseObject();
}
$this->ret->setRequest($this->req->jsonArray);
if(get_class($kt)=='KTAPI'){
$this->kt=&$kt;
}else{
$this->ret->addError('KnowledgeTree Object not Received in '.__CLASS__.' constructor. Quitting.');
return $this->render();
}
// Prepar
$this->loadService('auth');
$this->authenticator=new auth($this->ret,$this->kt,$this->request,$this->auth);
//Make sure a token exists before continuing
if(!$this->verifyToken())return $this->render();
if(!$this->verifySession()){
$this->doLogin();
if(!$this->isAuthenticated())return $this->render();
}
$this->dispatch();
return $this->render();
}
public function dispatch(){
$request=$this->request;
$this->loadService($request['service']);
$service=new $request['service']($this->ret,$this->kt,$this->request,$this->auth);
$this->ret->setTitle($request['service'].'::'.$request['function']);
if(method_exists($service,$request['function'])){
//$this->ret->setDebug('got here');
$service->$request['function']($request['parameters']);
}else{
$this->ret->addError("Service {$request['service']} does not contain the method: {$request['function']}");
return false;
}
}
public function loadService($serviceName=NULL){
$version=$this->getVersion();
if(!class_exists($serviceName)){
if(file_exists('services/'.$version.'/'.$serviceName.'.php')){
require_once('services/'.$version.'/'.$serviceName.'.php');
return true;
}else{
throw new Exception('Service could not be found: '.$serviceName);
return false;
}
}
}
protected function verifyToken(){
$token=isset($this->auth['token'])?$this->auth['token']:NULL;
if(!$token){
$token=md5(rand()*rand());
$this->ret->setStatus('random_token',$token);
return false;
}
return true;
}
protected function getVersion(){
if(!$this->version)$this->version=$this->req->getVersion();
return $this->version;
}
protected function verifySession(){
return $this->authenticator->pickup_session();
}
protected function isAuthenticated(){
return true;
}
protected function doLogin(){
if($this->authenticator->login()){
return true;
}else{
$this->ret->addError('Unsuccesful Login');
return false;
}
}
public function render(){
echo $this->ret->getJson();
return true;
}
}
?>