/application/controllers/message.php

https://github.com/dcompute/seaforium · PHP · 210 lines · 137 code · 44 blank · 29 comment · 27 complexity · 02f4c471444e8b0b8aa477172dea6704 MD5 · raw file

  1. <?php
  2. class Message extends Controller {
  3. function Message()
  4. {
  5. parent::Controller();
  6. $this->load->helper(array('form', 'url', 'content_render'));
  7. $this->load->library('form_validation');
  8. $this->load->model('message_dal');
  9. if (!$this->sauth->is_logged_in()) {
  10. redirect('/');
  11. }
  12. }
  13. function index(){}
  14. function load($message_id)
  15. {
  16. $user_id = (int)$this->session->userdata('user_id');
  17. $message = $this->message_dal->get_message($user_id, $message_id);
  18. // data is only returned if the user requesting is the recipient or the sender
  19. if ($message != FALSE) {
  20. $message = $message->row();
  21. // if the recipient is reading the message
  22. if ((int)$message->to_id == $user_id) {
  23. // if a read receipt was requested
  24. if ($message->read_receipt == '1' && $message->read == '0') {
  25. $receipt = array(
  26. 'sender' => $user_id, // reader of the original message
  27. 'recipient' => $message->sender_id, // user who sent the original message
  28. 'subject' => 'Receipt for: '. $message->subject,
  29. 'content' => 'Your message has been read.'
  30. );
  31. // new message in the database
  32. $receipt['id'] = $this->message_dal->new_message(array(
  33. 'subject' => $message->subject,
  34. 'content' => $message->content
  35. ));
  36. // send the receipt to the inbox of the original sender
  37. $this->message_dal->new_inbox($receipt['recipient'], $receipt, '');
  38. // and put an outbox notification in the outbox of the original recipient
  39. $this->message_dal->new_outbox($receipt['recipient'], $receipt);
  40. }
  41. // if this message is unread, change that
  42. if ($message->read == '0') {
  43. $this->message_dal->set_read($user_id, $message_id);
  44. }
  45. }
  46. $this->load->view('shared/header');
  47. $this->load->view('messages/message', array('message' => $message));
  48. $this->load->view('shared/footer');
  49. } else {
  50. show_404('/message/' . $message_id);
  51. }
  52. }
  53. function send($to = '')
  54. {
  55. $user_id = (int)$this->session->userdata('user_id');
  56. $data = array(
  57. 'to' => str_replace('-', ' ', $to),
  58. // this is only used by /reply but we're using the same view to make things easier
  59. 'message' => array(
  60. 'recipients' => str_replace('-', ' ', $to),
  61. 'subject' => '',
  62. 'content' => ''
  63. )
  64. );
  65. $this->form_validation->set_message('required', "%s is required");
  66. $this->form_validation->set_error_delimiters('<li>', '</li>');
  67. $this->form_validation->set_rules('recipients', 'At least one recipient', 'trim|required|xss_clean');
  68. $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
  69. $this->form_validation->set_rules('save_sent', 'Save sent');
  70. $this->form_validation->set_rules('read_receipt', 'Read receipt');
  71. $this->form_validation->set_rules('content', 'Content', 'trim|required|xss_clean');
  72. // process the error checking on the form
  73. if ($this->form_validation->run()) {
  74. // array of user names
  75. $usernames = explode(',', $this->form_validation->set_value('recipients'));
  76. // TODO: remember why I limited it to 10.
  77. if (count($usernames) < 11) {
  78. // translated into user ids
  79. $user_ids = $this->user_dal->get_user_ids_from_array($this->session->userdata('user_id'), $usernames);
  80. // make sure the amount of users returned by the database
  81. // matches how many users we want to message
  82. if (count($usernames) === $user_ids->num_rows) {
  83. $recipient_ids = array();
  84. $have_me_enemied = array();
  85. // loop through the results to pull out user ids and see if anyone
  86. // has me enemied
  87. foreach($user_ids->result() as $row) {
  88. $recipient_ids[] = (int)$row->id;
  89. if ($row->type == '2') { // this user has me as their enemy
  90. $have_me_enemied[] = $row->username;
  91. }
  92. }
  93. // if no one has me enemied
  94. if (count($have_me_enemied) === 0) {
  95. // put together the data for a new pm
  96. $message = array(
  97. 'sender' => (int)$this->session->userdata('user_id'),
  98. 'recipients' => $recipient_ids,
  99. 'subject' => $this->form_validation->set_value('subject'),
  100. 'content' => $this->form_validation->set_value('content')
  101. );
  102. // insert a new PM into the database
  103. $message['id'] = $this->message_dal->new_message($message);
  104. // loop through all recipients
  105. foreach($message['recipients'] as $recipient) {
  106. // send the message and increment the message counter
  107. $receipt = $this->form_validation->set_value('read_receipt');
  108. $this->message_dal->new_inbox($recipient, $message, $receipt);
  109. // if we want to save a message to our outbox
  110. if ($this->form_validation->set_value('save_sent') == 'save') {
  111. $this->message_dal->new_outbox($recipient, $message);
  112. }
  113. }
  114. // redirect them to the inbox
  115. redirect('/messages/inbox');
  116. }
  117. // if there was 1 recipient and that 1 person has me enemied
  118. elseif (count($have_me_enemied) === 1 && count($recipient_ids) === 1) {
  119. $data['errors'] = "<li>That user has you enemied.</li>";
  120. }
  121. // if there were multiple recipients and 1 or more of them have me enemied
  122. else {
  123. $data['errors'] = "<li>The following users have you enemied:<ul>";
  124. foreach($have_me_enemied as $jerk) {
  125. $data['errors'] .= '<li>'. $jerk .'</li>';
  126. }
  127. $data['errors'] .= '</ul></li>';
  128. }
  129. }
  130. // the amount of usernames returned by the database does not
  131. // match the amount of users we put in the 'to' field
  132. else {
  133. $data['errors'] = "<li>One or more of the recipients does not exist</li>";
  134. }
  135. } else {
  136. $data['errors'] = validation_errors();
  137. }
  138. }
  139. $this->load->view('shared/header');
  140. $this->load->view('messages/send', $data);
  141. $this->load->view('shared/footer');
  142. }
  143. function reply($message_id = 0)
  144. {
  145. $user_id = (int)$this->session->userdata('user_id');
  146. if ($message = $this->message_dal->get_message($user_id, $message_id)) {
  147. $message = $message->row();
  148. $subject = $message->subject;
  149. if (!preg_match('/^RE:/', $subject)) {
  150. $subject = "RE: " . $subject;
  151. }
  152. $data['message'] = array(
  153. 'recipients' => $message->username,
  154. 'subject' => $subject,
  155. 'content' => "\n\n\n-----------------------------\n\n". $message->content
  156. );
  157. } else {
  158. show_404('/message/reply/' . $message_id);
  159. }
  160. $this->load->view('shared/header');
  161. $this->load->view('messages/send', $data);
  162. $this->load->view('shared/footer');
  163. }
  164. }
  165. /* End of file message.php */
  166. /* Location: ./application/controllers/message.php */