PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/qa-include/qa-page-message.php

http://github.com/q2a/question2answer
PHP | 230 lines | 135 code | 61 blank | 34 comment | 17 complexity | 238e4f169be364840bc03ef2cd5b9d4b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. Question2Answer (c) Gideon Greenspan
  4. http://www.question2answer.org/
  5. File: qa-include/qa-page-message.php
  6. Version: See define()s at top of qa-include/qa-base.php
  7. Description: Controller for private messaging page
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. More about this license: http://www.question2answer.org/license.php
  17. */
  18. if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
  19. header('Location: ../');
  20. exit;
  21. }
  22. require_once QA_INCLUDE_DIR.'qa-db-selects.php';
  23. require_once QA_INCLUDE_DIR.'qa-app-users.php';
  24. require_once QA_INCLUDE_DIR.'qa-app-format.php';
  25. $handle=qa_request_part(1);
  26. $loginuserid=qa_get_logged_in_userid();
  27. // Check we have a handle, we're not using Q2A's single-sign on integration and that we're logged in
  28. if (QA_FINAL_EXTERNAL_USERS)
  29. qa_fatal_error('User accounts are handled by external code');
  30. if (!strlen($handle))
  31. qa_redirect('users');
  32. if (!isset($loginuserid)) {
  33. $qa_content=qa_content_prepare();
  34. $qa_content['error']=qa_insert_login_links(qa_lang_html('misc/message_must_login'), qa_request());
  35. return $qa_content;
  36. }
  37. // Find the user profile and questions and answers for this handle
  38. list($toaccount, $torecent, $fromrecent)=qa_db_select_with_pending(
  39. qa_db_user_account_selectspec($handle, false),
  40. qa_db_recent_messages_selectspec($loginuserid, true, $handle, false),
  41. qa_db_recent_messages_selectspec($handle, false, $loginuserid, true)
  42. );
  43. // Check the user exists and work out what can and can't be set (if not using single sign-on)
  44. if ( (!qa_opt('allow_private_messages')) || (!is_array($toaccount)) || ($toaccount['flags'] & QA_USER_FLAGS_NO_MESSAGES) )
  45. return include QA_INCLUDE_DIR.'qa-page-not-found.php';
  46. // Process sending a message to user
  47. $messagesent=false;
  48. if (qa_post_text('domessage')) {
  49. require_once QA_INCLUDE_DIR.'qa-app-limits.php';
  50. // Check that we haven't been blocked on volume
  51. $errorhtml=null;
  52. switch (qa_user_permit_error(null, QA_LIMIT_MESSAGES)) {
  53. case 'limit':
  54. $errorhtml=qa_lang_html('misc/message_limit');
  55. break;
  56. case false:
  57. break;
  58. default:
  59. $errorhtml=qa_lang_html('users/no_permission');
  60. break;
  61. }
  62. if (isset($errorhtml)) {
  63. $qa_content=qa_content_prepare();
  64. $qa_content['error']=$errorhtml;
  65. return $qa_content;
  66. }
  67. // Proceed...
  68. $inmessage=qa_post_text('message');
  69. if (empty($inmessage))
  70. $errors['message']=qa_lang('misc/message_empty');
  71. if (empty($errors)) {
  72. require_once QA_INCLUDE_DIR.'qa-db-messages.php';
  73. require_once QA_INCLUDE_DIR.'qa-app-emails.php';
  74. if (qa_opt('show_message_history'))
  75. $messageid=qa_db_message_create($loginuserid, $toaccount['userid'], $inmessage, '');
  76. else
  77. $messageid=null;
  78. $fromhandle=qa_get_logged_in_handle();
  79. $canreply=!(qa_get_logged_in_flags() & QA_USER_FLAGS_NO_MESSAGES);
  80. $more=strtr(qa_lang($canreply ? 'emails/private_message_reply' : 'emails/private_message_info'), array(
  81. '^f_handle' => $fromhandle,
  82. '^url' => qa_path($canreply ? ('message/'.$fromhandle) : ('user/'.$fromhandle), null, qa_opt('site_url')),
  83. ));
  84. $subs=array(
  85. '^message' => $inmessage,
  86. '^f_handle' => $fromhandle,
  87. '^f_url' => qa_path('user/'.$fromhandle, null, qa_opt('site_url')),
  88. '^more' => $more,
  89. '^a_url' => qa_path_html('account', null, qa_opt('site_url')),
  90. );
  91. if (qa_send_notification($toaccount['userid'], $toaccount['email'], $toaccount['handle'],
  92. qa_lang('emails/private_message_subject'), qa_lang('emails/private_message_body'), $subs))
  93. $messagesent=true;
  94. else
  95. $page_error=qa_lang_html('main/general_error');
  96. qa_report_event('u_message', $loginuserid, qa_get_logged_in_handle(), qa_cookie_get(), array(
  97. 'userid' => $toaccount['userid'],
  98. 'handle' => $toaccount['handle'],
  99. 'message' => $inmessage,
  100. ));
  101. }
  102. }
  103. // Prepare content for theme
  104. $qa_content=qa_content_prepare();
  105. $qa_content['title']=qa_lang_html('misc/private_message_title');
  106. $qa_content['error']=@$page_error;
  107. $qa_content['form_message']=array(
  108. 'tags' => 'METHOD="POST" ACTION="'.qa_self_html().'"',
  109. 'style' => 'tall',
  110. 'fields' => array(
  111. 'message' => array(
  112. 'type' => $messagesent ? 'static' : '',
  113. 'label' => qa_lang_html_sub('misc/message_for_x', qa_get_one_user_html($handle, false)),
  114. 'tags' => 'NAME="message" ID="message"',
  115. 'value' => qa_html(@$inmessage, $messagesent),
  116. 'rows' => 8,
  117. 'note' => qa_lang_html_sub('misc/message_explanation', qa_html(qa_opt('site_title'))),
  118. 'error' => qa_html(@$errors['message']),
  119. ),
  120. ),
  121. 'buttons' => array(
  122. 'send' => array(
  123. 'label' => qa_lang_html('main/send_button'),
  124. ),
  125. ),
  126. 'hidden' => array(
  127. 'domessage' => '1',
  128. ),
  129. );
  130. $qa_content['focusid']='message';
  131. if ($messagesent) {
  132. $qa_content['form_message']['ok']=qa_lang_html('misc/message_sent');
  133. unset($qa_content['form_message']['fields']['message']['note']);
  134. unset($qa_content['form_message']['buttons']);
  135. }
  136. // If relevant, show recent message history
  137. if (qa_opt('show_message_history')) {
  138. $recent=array_merge($torecent, $fromrecent);
  139. qa_sort_by($recent, 'created');
  140. $showmessages=array_slice(array_reverse($recent, true), 0, QA_DB_RETRIEVE_MESSAGES);
  141. if (count($showmessages)) {
  142. $qa_content['form_recent']=array(
  143. 'title' => qa_lang_html_sub('misc/message_recent_history', qa_html($toaccount['handle'])),
  144. 'style' => 'tall',
  145. 'fields' => array(),
  146. );
  147. foreach ($showmessages as $message) {
  148. $qa_content['form_recent']['fields'][]=array(
  149. 'label' => qa_lang_html_sub(
  150. ($message['touserid']==$toaccount['userid']) ? 'misc/message_sent_x_ago' : 'misc/message_received_x_ago',
  151. qa_html(qa_time_to_string(qa_opt('db_time')-$message['created']))),
  152. 'type' => 'static',
  153. 'value' => qa_viewer_html($message['content'], $message['format']),
  154. );
  155. }
  156. }
  157. }
  158. $qa_content['raw']['account']=$toaccount; // for plugin layers to access
  159. return $qa_content;
  160. /*
  161. Omit PHP closing tag to help avoid accidental output
  162. */