-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.php
462 lines (386 loc) Β· 8.38 KB
/
ajax.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
define('IN_AJAX', true);
define('BB_SCRIPT', 'ajax');
define('BB_ROOT', './');
$ajax = new ajax_common();
require(BB_ROOT .'common.php');
$ajax->init();
// Init userdata
$user->session_start();
// Load actions required modules
switch ($ajax->action)
{
case 'view_post':
require(INC_DIR . 'bbcode.php');
break;
case 'posts':
case 'post_mod_comment':
require(INC_DIR . 'bbcode.php');
require(INC_DIR . 'functions_post.php');
require(INC_DIR . 'functions_admin.php');
break;
case 'view_torrent':
case 'mod_action':
case 'change_tor_status':
case 'gen_passkey':
require(ATTACH_DIR . 'attachment_mod.php');
require(INC_DIR . 'functions_torrent.php');
break;
case 'change_torrent':
require(ATTACH_DIR . 'attachment_mod.php');
require(INC_DIR . 'functions_torrent.php');
break;
case 'user_register':
require(INC_DIR . 'functions_validate.php');
break;
case 'manage_user':
case 'manage_admin':
require(INC_DIR . 'functions_admin.php');
break;
case 'group_membership':
case 'manage_group':
require(INC_DIR . 'functions_group.php');
break;
case 'sitemap':
require(CLASS_DIR .'sitemap.php');
break;
}
// Position in $ajax->valid_actions['xxx']
define('AJAX_AUTH', 0); // 'guest', 'user', 'mod', 'admin', 'super_admin'
$ajax->exec();
//
// Ajax
//
class ajax_common
{
var $request = array();
var $response = array();
var $valid_actions = array(
// ACTION NAME AJAX_AUTH
'edit_user_profile' => array('admin'),
'change_user_rank' => array('admin'),
'change_user_opt' => array('admin'),
'manage_user' => array('admin'),
'manage_admin' => array('admin'),
'sitemap' => array('admin'),
'mod_action' => array('mod'),
'topic_tpl' => array('mod'),
'group_membership' => array('mod'),
'post_mod_comment' => array('mod'),
'avatar' => array('user'),
'gen_passkey' => array('user'),
'change_torrent' => array('user'),
'change_tor_status' => array('user'),
'manage_group' => array('user'),
'view_post' => array('guest'),
'view_torrent' => array('guest'),
'user_register' => array('guest'),
'posts' => array('guest'),
'index_data' => array('guest'),
);
var $action = null;
/**
* Constructor
*/
function ajax_common()
{
ob_start(array(&$this, 'ob_handler'));
header('Content-Type: text/plain');
}
/**
* Perform action
*/
function exec()
{
global $lang, $bb_cfg;
// Exit if we already have errors
if (!empty($this->response['error_code']))
{
$this->send();
}
// Check that requested action is valid
$action = $this->action;
// Action params
$action_params = null;
if (!$action || !is_string($action))
{
$this->ajax_die('no action specified');
}
elseif (!$action_params =& $this->valid_actions[$action])
{
$this->ajax_die('invalid action: ' . $action);
}
// Exit if board is disabled via ON/OFF trigger or by admin
if ($bb_cfg['board_disable'] || file_exists(BB_DISABLED))
{
if ($bb_cfg['board_disable'] && !DBG_USER)
{
$this->ajax_die($lang['BOARD_DISABLE']);
}
else if (file_exists(BB_DISABLED) && $this->action !== 'manage_admin')
{
$this->ajax_die($lang['BOARD_DISABLE_CRON']);
}
}
// Auth check
switch ($action_params[AJAX_AUTH])
{
// GUEST
case 'guest':
break;
// USER
case 'user':
if (IS_GUEST)
{
$this->ajax_die($lang['NEED_TO_LOGIN_FIRST']);
}
break;
// MOD
case 'mod':
if (!IS_AM)
{
$this->ajax_die($lang['ONLY_FOR_MOD']);
}
$this->check_admin_session();
break;
// ADMIN
case 'admin':
if (!IS_ADMIN)
{
$this->ajax_die($lang['ONLY_FOR_ADMIN']);
}
$this->check_admin_session();
break;
// SUPER_ADMIN
case 'super_admin':
if (!IS_SUPER_ADMIN)
{
$this->ajax_die($lang['ONLY_FOR_SUPER_ADMIN']);
}
$this->check_admin_session();
break;
default:
trigger_error("invalid auth type for $action", E_USER_ERROR);
}
// Run action
$this->$action();
// Send output
$this->send();
}
/**
* Exit on error
*/
function ajax_die($error_msg, $error_code = E_AJAX_GENERAL_ERROR)
{
$this->response['error_code'] = $error_code;
$this->response['error_msg'] = strip_tags(br2nl($error_msg));
// Get caller info
if (!empty($_COOKIE['explain']))
{
$ajax_debug = 'ajax die: ' . $this->debug_find_source();
$this->response['error_msg'] .= "\n\n" . $ajax_debug;
$this->response['console_log'] = $ajax_debug;
}
$this->send();
}
/**
* Initialization
*/
function init()
{
$this->request = $_POST;
$this->action =& $this->request['action'];
}
/**
* Send data
*/
function send()
{
$this->response['action'] = $this->action;
if (DBG_USER && SQL_DEBUG && !empty($_COOKIE['sql_log']))
{
$this->response['sql_log'] = get_sql_log();
}
// sending output will be handled by $this->ob_handler()
exit();
}
/**
* OB Handler
*/
function ob_handler($contents)
{
if (DBG_USER)
{
if ($contents)
{
$this->response['raw_output'] = $contents;
}
}
$response_js = Zend\Json\Json::encode($this->response);
if (GZIP_OUTPUT_ALLOWED && !defined('NO_GZIP'))
{
if (UA_GZIP_SUPPORTED && strlen($response_js) > 2000)
{
header('Content-Encoding: gzip');
$response_js = gzencode($response_js, 1);
}
}
return $response_js;
}
/**
* Admin session
*/
function check_admin_session()
{
global $user, $lang;
if (!$user->data['session_admin'])
{
if (empty($this->request['user_password']))
{
$this->prompt_for_password();
}
else
{
$login_args = array(
'login_username' => $user->data['username'],
'login_password' => $_POST['user_password'],
);
if (!$user->login($login_args, true))
{
$this->ajax_die($lang['ERROR_LOGIN']);
}
}
}
}
/**
* Prompt for password
*/
function prompt_for_password()
{
$this->response['prompt_password'] = 1;
$this->send();
}
/**
* Prompt for confirmation
*/
function prompt_for_confirm($confirm_msg = '')
{
global $lang;
if (empty($confirm_msg)) $confirm_msg = $lang['QUESTION'];
$this->response['prompt_confirm'] = 1;
$this->response['confirm_msg'] = strip_tags(br2nl($confirm_msg));
$this->send();
}
/**
* Verify mod rights
*/
function verify_mod_rights($forum_id)
{
global $userdata, $lang;
$is_auth = auth(AUTH_MOD, $forum_id, $userdata);
if (!$is_auth['auth_mod'])
{
$this->ajax_die($lang['ONLY_FOR_MOD']);
}
}
/**
* Find caller source
*/
function debug_find_source ($mode = '')
{
if (empty($_COOKIE['explain'])) return 'src disabled';
foreach (debug_backtrace() as $trace)
{
if (!empty($trace['file']) && $trace['file'] !== __FILE__)
{
switch ($mode)
{
case 'file': return $trace['file'];
case 'line': return $trace['line'];
default: return hide_bb_path($trace['file']) .'('. $trace['line'] .')';
}
}
}
return 'src not found';
}
function edit_user_profile()
{
require(AJAX_DIR . 'edit_user_profile.php');
}
function change_user_rank()
{
require(AJAX_DIR . 'change_user_rank.php');
}
function change_user_opt()
{
require(AJAX_DIR . 'change_user_opt.php');
}
function gen_passkey()
{
require(AJAX_DIR . 'gen_passkey.php');
}
function group_membership()
{
require(AJAX_DIR . 'group_membership.php');
}
function manage_group()
{
require(AJAX_DIR . 'edit_group_profile.php');
}
function post_mod_comment()
{
require(AJAX_DIR . 'post_mod_comment.php');
}
function view_post()
{
require(AJAX_DIR . 'view_post.php');
}
function change_tor_status()
{
require(AJAX_DIR . 'change_tor_status.php');
}
function change_torrent()
{
require(AJAX_DIR . 'change_torrent.php');
}
function view_torrent()
{
require(AJAX_DIR . 'view_torrent.php');
}
function user_register()
{
require(AJAX_DIR . 'user_register.php');
}
function mod_action()
{
require(AJAX_DIR . 'mod_action.php');
}
function posts()
{
require(AJAX_DIR . 'posts.php');
}
function manage_user()
{
require(AJAX_DIR . 'manage_user.php');
}
function manage_admin()
{
require(AJAX_DIR . 'manage_admin.php');
}
function topic_tpl()
{
require(AJAX_DIR . 'topic_tpl.php');
}
function index_data()
{
require(AJAX_DIR . 'index_data.php');
}
function avatar()
{
require(AJAX_DIR . 'avatar.php');
}
function sitemap()
{
require(AJAX_DIR .'sitemap.php');
}
}