-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
172 lines (142 loc) · 5.93 KB
/
api.php
File metadata and controls
172 lines (142 loc) · 5.93 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
//@error_reporting(E_ALL | E_NOTICE);
//@ini_set('display_errors', 1);
require('./include/config.php');
require('./include/functions.php');
require('./include/apiresponse.class.php');
require('./include/authmanager.class.php');
$action = isset($_POST['action']) ? $_POST['action'] : '';
$format = isset($_REQUEST['format']) ? $_REQUEST['format'] : 'xml';
if($format == 'json') {
$response = new ApiResponseJson();
} else {
$response = new ApiResponseXml();
}
$db = new mysqli($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);
if ($db->connect_error) {
$response->error(900, 'Database connection failure.');
exit;
}
$auth = new AuthManager($db);
/****************** GLOBAL SESSION HANDLING ********************/
$session = array();
if( isset($_POST['sessionId']) ) {
$session_id = clean($db, $_POST['sessionId']);
if(($session = $auth->getSession($session_id)) == NULL) {
$response->error(201, 'Expired or nonexistent session!');
exit;
}
if($session['terminated'] == 1) {
$response->error(202, 'Session terminated!');
exit;
}
} else {
// only create session can be called without a session id.
if($action != 'createSession') {
$response->error(100, 'Session ID required to call this command.');
exit;
}
}
/***************************************************************/
switch($action) {
case 'createSession':
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['programId'])) {
$username = clean($db, $_POST['username']);
$password = clean($db, $_POST['password']);
$program_id = (int)clean($db, $_POST['programId']);
if(($userdata = $auth->getUser($username, $password)) != NULL) {
if(($programdata = $auth->getProgram($program_id)) != NULL) {
$result = $db->query(sprintf("SELECT * FROM `permissions`
WHERE (`permissions`.`program_id` = %d OR `permissions`.`program_id` = -1) AND `permissions`.`user_id` = %d;", $program_id, $userdata['id']));
if($permission = $result->fetch_assoc()) {
$session_id = $auth->generateSessionId();
$auth->createSession($session_id, $programdata['id'], $permission['user_id'], $_SERVER['REMOTE_ADDR']);
$auth->logActivity($session_id, $programdata['id'], $permission['user_id'], $_SERVER['REMOTE_ADDR'], 'create_session');
$response->appendSection('AuthorizationResponse', array(
'ResponseCode' => 'OK',
'ProgramID' => $programdata['id'],
'LatestMajorVersion' => $programdata['major_version'],
'LatestMinorVersion' => $programdata['minor_version'],
'LatestRevisionVersion' => $programdata['revision_version'],
'SessionID' => $session_id,
'KeepAlive' => 900
));
$response->display();
} else {
$auth->logActivity('', $program_id, 0, $_SERVER['REMOTE_ADDR'], 'denied_access');
$response->error(102, 'Access denied.');
}
} else {
$auth->logActivity('', $program_id, 0, $_SERVER['REMOTE_ADDR'], 'bad_pid');
$response->error(101, 'Invalid or deactivated program.');
}
} else {
$auth->logActivity('', $program_id, 0, $_SERVER['REMOTE_ADDR'], 'login_error');
$response->error(101, 'Account deactivated or login invalid.');
}
} else {
$auth->logActivity('', 0, 0, $_SERVER['REMOTE_ADDR'], 'bad_request');
$response->error(100, 'Missing required variable.');
}
break;
case 'destroySession':
// set the last ping time to 0 so it expires instantly
$db->query('UPDATE `sessions` SET `last_ping_time` = 0 WHERE `session_id` = \''.$session['session_id'].'\';');
$auth->logActivity($session['session_id'], $session['program_id'], $session['user_id'], $_SERVER['REMOTE_ADDR'], 'destroy_session');
$response->appendSection('AuthorizationResponse', array(
'ResponseCode' => 'OK',
'Message' => 'Session has been destroyed.',
'LastPingTime' => 0,
'ProgramID' => $session['program_id'],
'SessionID' => $session['session_id']
));
$response->display();
break;
case 'ping':
$os_username = clean($db, $_POST['os_username']);
$db->query('UPDATE `sessions` SET `last_ping_time` = UNIX_TIMESTAMP(), `os_username` = \''.$os_username.'\' WHERE `session_id` = \''.$session['session_id'].'\';');
$auth->logActivity($session['session_id'], $session['program_id'], $session['user_id'], $_SERVER['REMOTE_ADDR'], 'ping');
$response->appendSection('AuthorizationResponse', array(
'ResponseCode' => 'OK',
'LastPingTime' => $session['last_ping_time'],
'ProgramID' => $session['program_id'],
'SessionID' => $session['session_id']
));
$response->display();
break;
case 'link':
if( isset( $_POST['url'] ) ) {
$url = clean($db, $_POST['url']);
$auth->logActivity($session['session_id'], $session['program_id'], $session['user_id'], $_SERVER['REMOTE_ADDR'], 'link');
// insert all the id's for the link log
$db->query( sprintf("INSERT INTO `link_logs` (`session_id`, `user_id`, `program_id`, `server_time`, `url`) VALUES ('%s', %d, %d, UNIX_TIMESTAMP(), '%s')",
$session['session_id'], $session['user_id'], $session['program_id'], $url) );
$response->appendSection('AuthorizationResponse', array(
'ResponseCode' => 'OK',
'ProgramID' => $session['program_id'],
'SessionID' => $session['session_id']
));
$response->display();
} else {
$response->error(100, 'Missing required variable.');
}
break;
case 'update':
$auth->logActivity($session['session_id'], $session['program_id'], $session['user_id'], $_SERVER['REMOTE_ADDR'], 'update');
$program_file = get_program_filename($session['program_id']);
if($zp = gzopen('./update/'.$program_file, 'r')) {
header('Content-Disposition: attachment; filename=update.exe');
header('Content-Type: application/octet-stream');
while (!gzeof($zp)) {
echo gzread($zp, 8192);
}
gzclose($zp);
} else {
$response->error(300, 'No update file found.');
}
break;
default:
$response->error(100, 'Missing required action.');
break;
}
?>