feedback.php 5.01 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: Controller for feedback page


	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
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28
require_once QA_INCLUDE_DIR . 'app/captcha.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
29 30


Scott committed
31
// Get useful information on the logged in user
Scott committed
32

Scott committed
33
$userid = qa_get_logged_in_userid();
Scott committed
34

Scott committed
35 36 37 38 39 40
if (isset($userid) && !QA_FINAL_EXTERNAL_USERS) {
	list($useraccount, $userprofile) = qa_db_select_with_pending(
		qa_db_user_account_selectspec($userid, true),
		qa_db_user_profile_selectspec($userid, true)
	);
}
Scott committed
41

Scott committed
42
$usecaptcha = qa_opt('captcha_on_feedback') && qa_user_use_captcha();
Scott committed
43 44


Scott committed
45
// Check feedback is enabled and the person isn't blocked
Scott committed
46

Scott committed
47 48
if (!qa_opt('feedback_enabled'))
	return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
Scott committed
49

Scott committed
50 51 52 53 54
if (qa_user_permit_error()) {
	$qa_content = qa_content_prepare();
	$qa_content['error'] = qa_lang_html('users/no_permission');
	return $qa_content;
}
Scott committed
55 56


Scott committed
57
// Send the feedback form
Scott committed
58

Scott committed
59

Scott committed
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
$feedbacksent = false;

if (qa_clicked('dofeedback')) {
	require_once QA_INCLUDE_DIR . 'app/emails.php';
	require_once QA_INCLUDE_DIR . 'util/string.php';

	$inmessage = qa_post_text('message');
	$inname = qa_post_text('name');
	$inemail = qa_post_text('email');
	$inreferer = qa_post_text('referer');

	if (!qa_check_form_security_code('feedback', qa_post_text('code')))
		$pageerror = qa_lang_html('misc/form_security_again');

	else {
		if (empty($inmessage))
			$errors['message'] = qa_lang('misc/feedback_empty');

		if ($usecaptcha)
			qa_captcha_validate_post($errors);

		if (empty($errors)) {
			$subs = array(
				'^message' => $inmessage,
				'^name' => empty($inname) ? '-' : $inname,
				'^email' => empty($inemail) ? '-' : $inemail,
				'^previous' => empty($inreferer) ? '-' : $inreferer,
				'^url' => isset($userid) ? qa_path_absolute('user/' . qa_get_logged_in_handle()) : '-',
				'^ip' => qa_remote_ip_address(),
				'^browser' => @$_SERVER['HTTP_USER_AGENT'],
			);

			if (qa_send_email(array(
Scott committed
93
				'fromemail' => qa_opt('from_email'),
Scott committed
94
				'fromname' => $inname,
Scott committed
95 96
				'replytoemail' => qa_email_validate(@$inemail) ? $inemail : null,
				'replytoname' => $inname,
Scott committed
97 98 99 100 101 102 103 104 105
				'toemail' => qa_opt('feedback_email'),
				'toname' => qa_opt('site_title'),
				'subject' => qa_lang_sub('emails/feedback_subject', qa_opt('site_title')),
				'body' => strtr(qa_lang('emails/feedback_body'), $subs),
				'html' => false,
			))) {
				$feedbacksent = true;
			} else {
				$pageerror = qa_lang_html('main/general_error');
Scott committed
106
			}
Scott committed
107 108 109 110 111 112 113 114

			qa_report_event('feedback', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
				'email' => $inemail,
				'name' => $inname,
				'message' => $inmessage,
				'previous' => $inreferer,
				'browser' => @$_SERVER['HTTP_USER_AGENT'],
			));
Scott committed
115 116
		}
	}
Scott committed
117
}
Scott committed
118 119


Scott committed
120
// Prepare content for theme
Scott committed
121

Scott committed
122 123 124 125 126 127 128 129 130 131 132 133 134
$qa_content = qa_content_prepare();

$qa_content['title'] = qa_lang_html('misc/feedback_title');

$qa_content['error'] = @$pageerror;

$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_self_html() . '"',

	'style' => 'tall',

	'fields' => array(
		'message' => array(
135
			'type' => $feedbacksent ? 'static' : 'text',
Scott committed
136 137 138 139 140
			'label' => qa_lang_html_sub('misc/feedback_message', qa_opt('site_title')),
			'tags' => 'name="message" id="message"',
			'value' => qa_html(@$inmessage),
			'rows' => 8,
			'error' => qa_html(@$errors['message']),
Scott committed
141 142
		),

Scott committed
143
		'name' => array(
144
			'type' => $feedbacksent ? 'static' : 'text',
Scott committed
145 146 147
			'label' => qa_lang_html('misc/feedback_name'),
			'tags' => 'name="name"',
			'value' => qa_html(isset($inname) ? $inname : @$userprofile['name']),
Scott committed
148 149
		),

Scott committed
150
		'email' => array(
151
			'type' => $feedbacksent ? 'static' : 'email',
Scott committed
152 153 154 155
			'label' => qa_lang_html('misc/feedback_email'),
			'tags' => 'name="email"',
			'value' => qa_html(isset($inemail) ? $inemail : qa_get_logged_in_email()),
			'note' => $feedbacksent ? null : qa_opt('email_privacy'),
Scott committed
156
		),
Scott committed
157
	),
Scott committed
158

Scott committed
159 160 161 162 163
	'buttons' => array(
		'send' => array(
			'label' => qa_lang_html('main/send_button'),
		),
	),
Scott committed
164

Scott committed
165 166 167 168 169 170
	'hidden' => array(
		'dofeedback' => '1',
		'code' => qa_get_form_security_code('feedback'),
		'referer' => qa_html(isset($inreferer) ? $inreferer : @$_SERVER['HTTP_REFERER']),
	),
);
Scott committed
171

Scott committed
172 173
if ($usecaptcha && !$feedbacksent)
	qa_set_up_captcha_field($qa_content, $qa_content['form']['fields'], @$errors);
Scott committed
174 175


Scott committed
176
$qa_content['focusid'] = 'message';
Scott committed
177

Scott committed
178 179 180 181
if ($feedbacksent) {
	$qa_content['form']['ok'] = qa_lang_html('misc/feedback_sent');
	unset($qa_content['form']['buttons']);
}
Scott committed
182 183


Scott committed
184
return $qa_content;