qa-ajax.php 2.18 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-ajax.php
	Description: Front line of response to Ajax requests, routing as appropriate


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
23
// Output this header as early as possible
Scott committed
24

Scott committed
25
header('Content-Type: text/plain; charset=utf-8');
Scott committed
26 27


Scott committed
28
// Ensure no PHP errors are shown in the Ajax response
Scott committed
29

Scott committed
30
@ini_set('display_errors', 0);
Scott committed
31 32


Scott committed
33
// Load the Q2A base file which sets up a bunch of crucial functions
Scott committed
34

Scott committed
35
require 'qa-base.php';
Scott committed
36

Scott committed
37
qa_report_process_stage('init_ajax');
Scott committed
38 39


Scott committed
40
// Get general Ajax parameters from the POST payload, and clear $_GET
Scott committed
41

Scott committed
42
qa_set_request(qa_post_text('qa_request'), qa_post_text('qa_root'));
Scott committed
43

Scott committed
44
$_GET = array(); // for qa_self_html()
Scott committed
45 46


Scott committed
47
// Database failure handler
Scott committed
48

Scott committed
49 50 51 52 53
function qa_ajax_db_fail_handler()
{
	echo "QA_AJAX_RESPONSE\n0\nA database error occurred.";
	qa_exit('error');
}
Scott committed
54 55


Scott committed
56
// Perform the appropriate Ajax operation
Scott committed
57

Scott committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
$routing = array(
	'notice' => 'notice.php',
	'favorite' => 'favorite.php',
	'vote' => 'vote.php',
	'recalc' => 'recalc.php',
	'mailing' => 'mailing.php',
	'version' => 'version.php',
	'category' => 'category.php',
	'asktitle' => 'asktitle.php',
	'answer' => 'answer.php',
	'comment' => 'comment.php',
	'click_a' => 'click-answer.php',
	'click_c' => 'click-comment.php',
	'click_admin' => 'click-admin.php',
	'show_cs' => 'show-comments.php',
	'wallpost' => 'wallpost.php',
	'click_wall' => 'click-wall.php',
	'click_pm' => 'click-pm.php',
);
Scott committed
77

Scott committed
78
$operation = qa_post_text('qa_operation');
Scott committed
79

Scott committed
80 81
if (isset($routing[$operation])) {
	qa_db_connect('qa_ajax_db_fail_handler');
Scott committed
82

Scott committed
83 84
	qa_initialize_buffering();
	require QA_INCLUDE_DIR . 'ajax/' . $routing[$operation];
Scott committed
85

Scott committed
86 87
	qa_db_disconnect();
}