ajaxhandler.php
1.97 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
<?php
class ajaxHandler{
public $ret=NULL;
public $req=NULL;
public $version=NULL;
public $auth=NULL;
public $request=NULL;
public function __construct(&$ret=NULL){
// 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);
//Make sure a token exists before continuing
if(!$this->verifyToken())return $this->render();
if(!$this->verifySession()){
$this->doLogin();
if(!$this->isAuthenticated())return $this->render();
}
return $this->render();
}
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');
}else{
throw new Exception('Service could not be found: '.$serviceName);
}
}
}
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 false;
}
protected function isAuthenticated(){
}
protected function doLogin(){
$this->loadService('auth');
$auth=new auth($this->ret);
if(!$auth->login($this->auth)){
return true;
}else{
$this->ret->addError('Unsuccesful Login');
return false;
}
}
public function render(){
echo $this->ret->getJson();
return true;
}
}
?>