message.php 7.11 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 private messaging 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 29 30
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
require_once QA_INCLUDE_DIR . 'app/limits.php';
Scott committed
31

Scott committed
32 33 34
$handle = qa_request_part(1);
$loginuserid = qa_get_logged_in_userid();
$fromhandle = qa_get_logged_in_handle();
Scott committed
35

Scott committed
36
$qa_content = qa_content_prepare();
Scott committed
37 38


Scott committed
39
// Check we have a handle, we're not using Q2A's single-sign on integration and that we're logged in
Scott committed
40

Scott committed
41 42
if (QA_FINAL_EXTERNAL_USERS)
	qa_fatal_error('User accounts are handled by external code');
Scott committed
43

Scott committed
44 45
if (!strlen($handle))
	qa_redirect('users');
Scott committed
46

Scott committed
47 48 49 50
if (!isset($loginuserid)) {
	$qa_content['error'] = qa_insert_login_links(qa_lang_html('misc/message_must_login'), qa_request());
	return $qa_content;
}
Scott committed
51

Scott committed
52 53 54 55 56
if ($handle === $fromhandle) {
	// prevent users sending messages to themselves
	$qa_content['error'] = qa_lang_html('users/no_permission');
	return $qa_content;
}
Scott committed
57 58


Scott committed
59
// Find the user profile and their recent private messages
Scott committed
60

Scott committed
61 62 63 64 65
list($toaccount, $torecent, $fromrecent) = qa_db_select_with_pending(
	qa_db_user_account_selectspec($handle, false),
	qa_db_recent_messages_selectspec($loginuserid, true, $handle, false),
	qa_db_recent_messages_selectspec($handle, false, $loginuserid, true)
);
Scott committed
66 67


Scott committed
68
// Check the user exists and work out what can and can't be set (if not using single sign-on)
Scott committed
69

Scott committed
70 71
if (!qa_opt('allow_private_messages') || !is_array($toaccount))
	return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
Scott committed
72

73 74
//  Check the target user has enabled private messages and inform the current user in case they haven't

Scott committed
75 76 77 78 79 80 81
if ($toaccount['flags'] & QA_USER_FLAGS_NO_MESSAGES) {
	$qa_content['error'] = qa_lang_html_sub(
		'profile/user_x_disabled_pms',
		sprintf('<a href="%s">%s</a>', qa_path_html('user/' . $handle), qa_html($handle))
	);
	return $qa_content;
}
Scott committed
82

Scott committed
83
// Check that we have permission and haven't reached the limit, but don't quit just yet
Scott committed
84

Scott committed
85 86 87 88
switch (qa_user_permit_error(null, QA_LIMIT_MESSAGES)) {
	case 'limit':
		$pageerror = qa_lang_html('misc/message_limit');
		break;
Scott committed
89

Scott committed
90 91
	case false:
		break;
Scott committed
92

Scott committed
93 94 95 96
	default:
		$pageerror = qa_lang_html('users/no_permission');
		break;
}
Scott committed
97 98


Scott committed
99
// Process sending a message to user
Scott committed
100

Scott committed
101 102 103 104 105
// check for messages or errors
$state = qa_get_state();
$messagesent = $state == 'message-sent';
if ($state == 'email-error')
	$pageerror = qa_lang_html('main/email_error');
Scott committed
106

Scott committed
107 108
if (qa_post_text('domessage')) {
	$inmessage = qa_post_text('message');
Scott committed
109

Scott committed
110 111 112 113 114 115 116 117 118 119 120 121
	if (isset($pageerror)) {
		// not permitted to post, so quit here
		$qa_content['error'] = $pageerror;
		return $qa_content;
	}

	if (!qa_check_form_security_code('message-' . $handle, qa_post_text('code')))
		$pageerror = qa_lang_html('misc/form_security_again');

	else {
		if (empty($inmessage))
			$errors['message'] = qa_lang('misc/message_empty');
Scott committed
122

Scott committed
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
		if (empty($errors)) {
			require_once QA_INCLUDE_DIR . 'db/messages.php';
			require_once QA_INCLUDE_DIR . 'app/emails.php';

			if (qa_opt('show_message_history'))
				$messageid = qa_db_message_create($loginuserid, $toaccount['userid'], $inmessage, '', false);
			else
				$messageid = null;

			$canreply = !(qa_get_logged_in_flags() & QA_USER_FLAGS_NO_MESSAGES);

			$more = strtr(qa_lang($canreply ? 'emails/private_message_reply' : 'emails/private_message_info'), array(
				'^f_handle' => $fromhandle,
				'^url' => qa_path_absolute($canreply ? ('message/' . $fromhandle) : ('user/' . $fromhandle)),
			));

			$subs = array(
				'^message' => $inmessage,
				'^f_handle' => $fromhandle,
				'^f_url' => qa_path_absolute('user/' . $fromhandle),
				'^more' => $more,
				'^a_url' => qa_path_absolute('account'),
			);

			if (qa_send_notification($toaccount['userid'], $toaccount['email'], $toaccount['handle'],
				qa_lang('emails/private_message_subject'), qa_lang('emails/private_message_body'), $subs))
				$messagesent = true;

			qa_report_event('u_message', $loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), array(
				'userid' => $toaccount['userid'],
				'handle' => $toaccount['handle'],
				'messageid' => $messageid,
				'message' => $inmessage,
			));

			// show message as part of general history
			if (qa_opt('show_message_history'))
				qa_redirect(qa_request(), array('state' => ($messagesent ? 'message-sent' : 'email-error')));
Scott committed
161 162
		}
	}
Scott committed
163
}
Scott committed
164 165


Scott committed
166
// Prepare content for theme
Scott committed
167

Scott committed
168
$hideForm = !empty($pageerror) || $messagesent;
Scott committed
169

Scott committed
170
$qa_content['title'] = qa_lang_html('misc/private_message_title');
Scott committed
171

Scott committed
172
$qa_content['error'] = @$pageerror;
Scott committed
173

Scott committed
174 175
$qa_content['form_message'] = array(
	'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
176

Scott committed
177
	'style' => 'tall',
Scott committed
178

Scott committed
179
	'ok' => $messagesent ? qa_lang_html('misc/message_sent') : null,
Scott committed
180

Scott committed
181 182 183 184 185 186 187 188 189
	'fields' => array(
		'message' => array(
			'type' => $hideForm ? 'static' : '',
			'label' => qa_lang_html_sub('misc/message_for_x', qa_get_one_user_html($handle, false)),
			'tags' => 'name="message" id="message"',
			'value' => qa_html(@$inmessage, $messagesent),
			'rows' => 8,
			'note' => qa_lang_html_sub('misc/message_explanation', qa_html(qa_opt('site_title'))),
			'error' => qa_html(@$errors['message']),
Scott committed
190
		),
Scott committed
191
	),
Scott committed
192

Scott committed
193 194 195 196
	'buttons' => array(
		'send' => array(
			'tags' => 'onclick="qa_show_waiting_after(this, false);"',
			'label' => qa_lang_html('main/send_button'),
Scott committed
197
		),
Scott committed
198
	),
Scott committed
199

Scott committed
200 201 202 203 204
	'hidden' => array(
		'domessage' => '1',
		'code' => qa_get_form_security_code('message-' . $handle),
	),
);
Scott committed
205

Scott committed
206
$qa_content['focusid'] = 'message';
Scott committed
207

Scott committed
208 209
if ($hideForm) {
	unset($qa_content['form_message']['buttons']);
Scott committed
210

Scott committed
211 212 213 214 215
	if (qa_opt('show_message_history'))
		unset($qa_content['form_message']['fields']['message']);
	else {
		unset($qa_content['form_message']['fields']['message']['note']);
		unset($qa_content['form_message']['fields']['message']['label']);
Scott committed
216
	}
Scott committed
217
}
Scott committed
218 219


Scott committed
220
// If relevant, show recent message history
Scott committed
221

Scott committed
222 223
if (qa_opt('show_message_history')) {
	$recent = array_merge($torecent, $fromrecent);
Scott committed
224

Scott committed
225
	qa_sort_by($recent, 'created');
Scott committed
226

Scott committed
227
	$showmessages = array_slice(array_reverse($recent, true), 0, QA_DB_RETRIEVE_MESSAGES);
Scott committed
228

Scott committed
229 230 231 232
	if (count($showmessages)) {
		$qa_content['message_list'] = array(
			'title' => qa_lang_html_sub('misc/message_recent_history', qa_html($toaccount['handle'])),
		);
Scott committed
233

Scott committed
234
		$options = qa_message_html_defaults();
Scott committed
235

Scott committed
236 237
		foreach ($showmessages as $message)
			$qa_content['message_list']['messages'][] = qa_message_html_fields($message, $options);
Scott committed
238 239
	}

240
	$qa_content['navigation']['sub'] = qa_user_sub_navigation($fromhandle, 'messages', true);
Scott committed
241
}
Scott committed
242 243


Scott committed
244
$qa_content['raw']['account'] = $toaccount; // for plugin layers to access
Scott committed
245

Scott committed
246
return $qa_content;