qa-page-message.php 6.92 KB
Newer Older
Gideon Greenspan committed
1
<?php
Scott Vivian committed
2

Gideon Greenspan committed
3 4 5 6 7
/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

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

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-app-users.php';
	require_once QA_INCLUDE_DIR.'qa-app-format.php';
Gideon Greenspan committed
35
	require_once QA_INCLUDE_DIR.'qa-app-limits.php';
Scott Vivian committed
36

Gideon Greenspan committed
37 38 39 40 41 42 43 44
	$handle=qa_request_part(1);
	$loginuserid=qa_get_logged_in_userid();


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

	if (QA_FINAL_EXTERNAL_USERS)
		qa_fatal_error('User accounts are handled by external code');
Scott Vivian committed
45

Gideon Greenspan committed
46 47 48 49 50 51 52 53 54 55 56
	if (!strlen($handle))
		qa_redirect('users');

	if (!isset($loginuserid)) {
		$qa_content=qa_content_prepare();
		$qa_content['error']=qa_insert_login_links(qa_lang_html('misc/message_must_login'), qa_request());
		return $qa_content;
	}


//	Find the user profile and questions and answers for this handle
Scott Vivian committed
57

Gideon Greenspan committed
58 59 60 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)
	);


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

Gideon Greenspan committed
67 68
	if ( (!qa_opt('allow_private_messages')) || (!is_array($toaccount)) || ($toaccount['flags'] & QA_USER_FLAGS_NO_MESSAGES) )
		return include QA_INCLUDE_DIR.'qa-page-not-found.php';
Scott Vivian committed
69

Gideon Greenspan committed
70

Gideon Greenspan committed
71 72 73
//	Check that we have permission and haven't reached the limit

	$errorhtml=null;
Scott Vivian committed
74

Gideon Greenspan committed
75 76 77 78
	switch (qa_user_permit_error(null, QA_LIMIT_MESSAGES)) {
		case 'limit':
			$errorhtml=qa_lang_html('misc/message_limit');
			break;
Scott Vivian committed
79

Gideon Greenspan committed
80 81
		case false:
			break;
Scott Vivian committed
82

Gideon Greenspan committed
83 84 85 86 87 88 89 90 91 92 93 94
		default:
			$errorhtml=qa_lang_html('users/no_permission');
			break;
	}

	if (isset($errorhtml)) {
		$qa_content=qa_content_prepare();
		$qa_content['error']=$errorhtml;
		return $qa_content;
	}


Gideon Greenspan committed
95 96
//	Process sending a message to user

Gideon Greenspan committed
97
	$messagesent=(qa_get_state()=='message-sent');
Scott Vivian committed
98

Gideon Greenspan committed
99 100
	if (qa_post_text('domessage')) {
		$inmessage=qa_post_text('message');
Scott Vivian committed
101

Gideon Greenspan committed
102 103
		if (!qa_check_form_security_code('message-'.$handle, qa_post_text('code')))
			$pageerror=qa_lang_html('misc/form_security_again');
Scott Vivian committed
104

Gideon Greenspan committed
105 106 107
		else {
			if (empty($inmessage))
				$errors['message']=qa_lang('misc/message_empty');
Scott Vivian committed
108

Gideon Greenspan committed
109 110 111
			if (empty($errors)) {
				require_once QA_INCLUDE_DIR.'qa-db-messages.php';
				require_once QA_INCLUDE_DIR.'qa-app-emails.php';
Scott Vivian committed
112

Gideon Greenspan committed
113 114 115 116
				if (qa_opt('show_message_history'))
					$messageid=qa_db_message_create($loginuserid, $toaccount['userid'], $inmessage, '', false);
				else
					$messageid=null;
Scott Vivian committed
117

Gideon Greenspan committed
118 119
				$fromhandle=qa_get_logged_in_handle();
				$canreply=!(qa_get_logged_in_flags() & QA_USER_FLAGS_NO_MESSAGES);
Scott Vivian committed
120

Gideon Greenspan committed
121 122 123 124
				$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)),
				));
Scott Vivian committed
125

Gideon Greenspan committed
126 127 128 129 130 131 132
				$subs=array(
					'^message' => $inmessage,
					'^f_handle' => $fromhandle,
					'^f_url' => qa_path_absolute('user/'.$fromhandle),
					'^more' => $more,
					'^a_url' => qa_path_absolute('account'),
				);
Scott Vivian committed
133

Gideon Greenspan committed
134 135 136 137 138
				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;
				else
					$pageerror=qa_lang_html('main/general_error');
Scott Vivian committed
139

Gideon Greenspan committed
140 141 142 143 144 145
				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,
				));
Scott Vivian committed
146

Gideon Greenspan committed
147 148 149
				if ($messagesent && qa_opt('show_message_history')) // show message as part of general history
					qa_redirect(qa_request(), array('state' => 'message-sent'));
			}
Gideon Greenspan committed
150 151 152 153 154
		}
	}


//	Prepare content for theme
Scott Vivian committed
155

Gideon Greenspan committed
156
	$qa_content=qa_content_prepare();
Scott Vivian committed
157

Gideon Greenspan committed
158 159
	$qa_content['title']=qa_lang_html('misc/private_message_title');

Gideon Greenspan committed
160
	$qa_content['error']=@$pageerror;
Gideon Greenspan committed
161 162

	$qa_content['form_message']=array(
Gideon Greenspan committed
163
		'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
164

Gideon Greenspan committed
165
		'style' => 'tall',
Scott Vivian committed
166

Gideon Greenspan committed
167 168 169 170
		'fields' => array(
			'message' => array(
				'type' => $messagesent ? 'static' : '',
				'label' => qa_lang_html_sub('misc/message_for_x', qa_get_one_user_html($handle, false)),
Gideon Greenspan committed
171
				'tags' => 'name="message" id="message"',
Gideon Greenspan committed
172 173 174 175 176 177
				'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 Vivian committed
178

Gideon Greenspan committed
179 180
		'buttons' => array(
			'send' => array(
Gideon Greenspan committed
181
				'tags' => 'onclick="qa_show_waiting_after(this, false);"',
Gideon Greenspan committed
182 183 184
				'label' => qa_lang_html('main/send_button'),
			),
		),
Scott Vivian committed
185

Gideon Greenspan committed
186 187
		'hidden' => array(
			'domessage' => '1',
Gideon Greenspan committed
188
			'code' => qa_get_form_security_code('message-'.$handle),
Gideon Greenspan committed
189 190
		),
	);
Scott Vivian committed
191

Gideon Greenspan committed
192 193 194 195 196
	$qa_content['focusid']='message';

	if ($messagesent) {
		$qa_content['form_message']['ok']=qa_lang_html('misc/message_sent');
		unset($qa_content['form_message']['buttons']);
Gideon Greenspan committed
197 198 199 200 201 202 203

		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']);
		}
Gideon Greenspan committed
204
	}
Scott Vivian committed
205

Gideon Greenspan committed
206 207 208 209 210

//	If relevant, show recent message history

	if (qa_opt('show_message_history')) {
		$recent=array_merge($torecent, $fromrecent);
Scott Vivian committed
211

Gideon Greenspan committed
212
		qa_sort_by($recent, 'created');
Scott Vivian committed
213

Gideon Greenspan committed
214
		$showmessages=array_slice(array_reverse($recent, true), 0, QA_DB_RETRIEVE_MESSAGES);
Scott Vivian committed
215

Gideon Greenspan committed
216
		if (count($showmessages)) {
Gideon Greenspan committed
217
			$qa_content['message_list']=array(
Gideon Greenspan committed
218 219
				'title' => qa_lang_html_sub('misc/message_recent_history', qa_html($toaccount['handle'])),
			);
Scott Vivian committed
220

Gideon Greenspan committed
221
			$options=qa_message_html_defaults();
Scott Vivian committed
222

Gideon Greenspan committed
223
			foreach ($showmessages as $message)
Gideon Greenspan committed
224
				$qa_content['message_list']['messages'][]=qa_message_html_fields($message, $options);
Gideon Greenspan committed
225 226 227 228 229
		}
	}


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

Gideon Greenspan committed
231 232 233 234 235 236 237

	return $qa_content;


/*
	Omit PHP closing tag to help avoid accidental output
*/