qa-ajax-comment.php 3.46 KB
Newer Older
Gideon Greenspan committed
1 2 3
<?php

/*
Gideon Greenspan committed
4
	Question2Answer by Gideon Greenspan and contributors
Gideon Greenspan committed
5 6 7

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-ajax-comment.php
	Version: See define()s at top of qa-include/qa-base.php
	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28
	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
*/

	require_once QA_INCLUDE_DIR.'qa-app-users.php';
	require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Gideon Greenspan committed
29
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
Gideon Greenspan committed
30 31


Gideon Greenspan committed
32
//	Load relevant information about this question and the comment parent
Gideon Greenspan committed
33

Gideon Greenspan committed
34 35 36
	$questionid=qa_post_text('c_questionid');
	$parentid=qa_post_text('c_parentid');
	$userid=qa_get_logged_in_userid();
Scott Vivian committed
37

Gideon Greenspan committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51
	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)
	);


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

	if (
		(@$question['basetype']=='Q') &&
		((@$parent['basetype']=='Q') || (@$parent['basetype']=='A')) &&
		!qa_user_post_permit_error('permit_post_c', $parent, QA_LIMIT_COMMENTS))
	{
Gideon Greenspan committed
52 53 54 55 56 57 58 59 60
		require_once QA_INCLUDE_DIR.'qa-app-captcha.php';
		require_once QA_INCLUDE_DIR.'qa-app-format.php';
		require_once QA_INCLUDE_DIR.'qa-app-post-create.php';
		require_once QA_INCLUDE_DIR.'qa-app-cookies.php';
		require_once QA_INCLUDE_DIR.'qa-page-question-view.php';
		require_once QA_INCLUDE_DIR.'qa-page-question-submit.php';
		require_once QA_INCLUDE_DIR.'qa-util-sort.php';


Gideon Greenspan committed
61
	//	Try to create the new comment
Scott Vivian committed
62

Gideon Greenspan committed
63 64
		$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 Vivian committed
65

Gideon Greenspan committed
66

Gideon Greenspan committed
67 68 69 70
	//	If successful, page content will be updated via Ajax

		if (isset($commentid)) {
			$children=qa_db_select_with_pending(qa_db_full_child_posts_selectspec($userid, $parentid));
Scott Vivian committed
71 72

			$parent=$parent+qa_page_q_post_rules($parent, ($questionid==$parentid) ? null : $question, null, $children);
Gideon Greenspan committed
73 74 75 76
				// in theory we should retrieve the parent's siblings for the above, but they're not going to be relevant

			foreach ($children as $key => $child)
				$children[$key]=$child+qa_page_q_post_rules($child, $parent, $children, null);
Scott Vivian committed
77

Gideon Greenspan committed
78
			$usershtml=qa_userids_handles_html($children, true);
Scott Vivian committed
79

Gideon Greenspan committed
80
			qa_sort_by($children, 'created');
Scott Vivian committed
81

Gideon Greenspan committed
82
			$c_list=qa_page_q_comment_follow_list($question, $parent, $children, true, $usershtml, false, null);
Scott Vivian committed
83

Gideon Greenspan committed
84
			$themeclass=qa_load_theme_class(qa_get_site_theme(), 'ajax-comments', null, null);
Scott Vivian committed
85

Gideon Greenspan committed
86
			echo "QA_AJAX_RESPONSE\n1\n";
Scott Vivian committed
87

Gideon Greenspan committed
88 89

		//	Send back the ID of the new comment
Scott Vivian committed
90

Gideon Greenspan committed
91
			echo qa_anchor('C', $commentid)."\n";
Scott Vivian committed
92

Gideon Greenspan committed
93

Gideon Greenspan committed
94
		//	Send back the HTML
Gideon Greenspan committed
95

Gideon Greenspan committed
96
			$themeclass->c_list_items($c_list['cs']);
Gideon Greenspan committed
97

Gideon Greenspan committed
98
			return;
Gideon Greenspan committed
99 100
		}
	}
Scott Vivian committed
101

Gideon Greenspan committed
102 103
	echo "QA_AJAX_RESPONSE\n0\n"; // fall back to non-Ajax submission if there were any problems

Scott Vivian committed
104

Gideon Greenspan committed
105 106 107
/*
	Omit PHP closing tag to help avoid accidental output
*/