comms.php
3.08 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
<?php
include_once('../../ktapi/ktapi.inc.php');
/**
* Expected structure of the json-decoded request object
* [auth]
* [user]
* [passhash]
* [session]
* [appType]
* [request]
* [service]
* [function]
* [parameters]
*
*
*/
class ktjapi{
protected $kt;
protected $raw;
protected $request=array(
'auth' =>array(
'user' =>'',
'passhash' =>'',
'session' =>'',
'appType' =>''
),
'request' =>array(
'service' =>'',
'function' =>'',
'parameters' =>''
)
);
protected $session_id;
protected $session;
protected $token;
protected $response=array(
'error' =>array(
'code' =>0,
'message' =>''
),
'status' =>array(
'session_id' =>'',
'random_token' =>''
),
'data' =>array()
);
public function __construct(){
$this->kt=new KTAPI();
$this->parseRequest();
if($this->auth()){
$this->dispatch();
$this->setAuthResponse();
}
echo json_encode($this->response);
}
protected function parseRequest(){
$this->raw=@file_get_contents('php://input');
$this->req=@json_decode($this->raw,true);
if(!is_array($this->req))$this->req=array();
$this->checkSession();
$this->token=$_SESSION['token']?$_SESSION['token']:md5(rand()*rand());
$this->response['status']['random_token']=$this->token;
}
protected function checkSession(){
$this->session_id=isset($this->request['auth']['session'])?$this->request['auth']['session']:session_id();
$this->session=$this->getSession($this->session_id);
}
protected function auth(){
if(!$this->isLoggedIn()){
$this->login();
if(!$this->isLoggedIn()){
$this->response['error']['code']=100;
$this->response['error']['message']='Invalid credentials. You are not authorised on this repository.';
return false;
}else{
return true;
}
}else{
return true;
}
}
protected function setAuthResponse(){
$this->checkSession();
if(rand(0,100)>50)$this->token=md5(rand()*rand());
$_SESSION['ktjapi_token']=$this->token;
$this->response['status']['session_id']=$this->session_id;
$this->response['status']['random_token']=$this->token;
}
protected function getSession($sessId=null){
$session=$this->kt->get_active_session($sessId?$sessId:session_id());
return PEAR::isError($session)?null:$session;
}
public function isLoggedIn(){
return !is_null($session);
}
public function logout(){
if($this->isLoggedIn()){
$this->session->logout();
$this->checkSession();
}
}
public function login(){
if(!$this->isLoggedIn()){
$user=$this->kt->get_user_object_by_username($this->request['auth']['user']);
if(!PEAR::isError($user)){
$pass=$user->getPassword();
$passHash=md5($pass.$this->token);
if($passHash==$this->request['auth']['passhash']){
$uSession=KTAPI_UserSession::_check_session($user, null, $this->request['auth']['appType']);
if(!PEAR::isError($uSession)){
$session= &new KTAPI_UserSession($this->kt, $user, $uSession[0], $uSession[1], NULL);
}else{
//handle the session error
}
}
}
$this->checkSession();
}
}
protected function dispatch(){
}
}
$k=new ktjapi();
?>