comment.php 3.21 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Server-side response to Ajax create comment requests


	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
22 23 24
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/limits.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
25 26


Scott committed
27
// Load relevant information about this question and the comment parent
Scott committed
28

Scott committed
29 30 31
$questionid = qa_post_text('c_questionid');
$parentid = qa_post_text('c_parentid');
$userid = qa_get_logged_in_userid();
Scott committed
32

Scott committed
33 34 35 36 37
list($question, $parent, $children) = qa_db_select_with_pending(
	qa_db_full_post_selectspec($userid, $questionid),
	qa_db_full_post_selectspec($userid, $parentid),
	qa_db_full_child_posts_selectspec($userid, $parentid)
);
Scott committed
38 39


Scott committed
40
// Check if the question and parent exist, and whether the user has permission to do this
Scott committed
41

42
if (@$question['basetype'] == 'Q' && (@$parent['basetype'] == 'Q' || @$parent['basetype'] == 'A') &&
Scott committed
43 44 45 46 47 48 49 50 51
	!qa_user_post_permit_error('permit_post_c', $parent, QA_LIMIT_COMMENTS)
) {
	require_once QA_INCLUDE_DIR . 'app/captcha.php';
	require_once QA_INCLUDE_DIR . 'app/format.php';
	require_once QA_INCLUDE_DIR . 'app/post-create.php';
	require_once QA_INCLUDE_DIR . 'app/cookies.php';
	require_once QA_INCLUDE_DIR . 'pages/question-view.php';
	require_once QA_INCLUDE_DIR . 'pages/question-submit.php';
	require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
52 53


Scott committed
54
	// Try to create the new comment
Scott committed
55

Scott committed
56 57
	$usecaptcha = qa_user_use_captcha(qa_user_level_for_post($question));
	$commentid = qa_page_q_add_c_submit($question, $parent, $children, $usecaptcha, $in, $errors);
Scott committed
58 59


Scott committed
60
	// If successful, page content will be updated via Ajax
Scott committed
61

Scott committed
62 63
	if (isset($commentid)) {
		$children = qa_db_select_with_pending(qa_db_full_child_posts_selectspec($userid, $parentid));
Scott committed
64

Scott committed
65 66
		$parent = $parent + qa_page_q_post_rules($parent, ($questionid == $parentid) ? null : $question, null, $children);
		// in theory we should retrieve the parent's siblings for the above, but they're not going to be relevant
Scott committed
67

Scott committed
68 69 70
		foreach ($children as $key => $child) {
			$children[$key] = $child + qa_page_q_post_rules($child, $parent, $children, null);
		}
Scott committed
71

Scott committed
72
		$usershtml = qa_userids_handles_html($children, true);
Scott committed
73

Scott committed
74
		qa_sort_by($children, 'created');
Scott committed
75

Scott committed
76
		$c_list = qa_page_q_comment_follow_list($question, $parent, $children, true, $usershtml, false, null);
Scott committed
77

Scott committed
78 79
		$themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-comments', null, null);
		$themeclass->initialize();
Scott committed
80

Scott committed
81
		echo "QA_AJAX_RESPONSE\n1\n";
Scott committed
82 83


Scott committed
84 85
		// send back the ID of the new comment
		echo qa_anchor('C', $commentid) . "\n";
Scott committed
86 87


Scott committed
88 89
		// send back the HTML
		$themeclass->c_list_items($c_list['cs']);
Scott committed
90

Scott committed
91
		return;
Scott committed
92
	}
Scott committed
93
}
Scott committed
94

Scott committed
95
echo "QA_AJAX_RESPONSE\n0\n"; // fall back to non-Ajax submission if there were any problems