PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/MXP3/src/scripts/privatemessages.php

http://mxprojects.googlecode.com/
PHP | 280 lines | 190 code | 52 blank | 38 comment | 29 complexity | 21725787ff3e9c91824e0fc43db758a9 MD5 | raw file
  1. <?php
  2. // Include config file
  3. include('./common.php');
  4. // Connect to database
  5. $link = dbConnect();
  6. // Attempt to auth user with database
  7. $user = auth($_POST['username'], $_POST['password']);
  8. if($user == ""){
  9. echo "output=badLogin";
  10. return;
  11. }
  12. //see where we are so we know what we gotta do
  13. switch($_POST['location']){
  14. case "inbox":
  15. case "outbox":
  16. case "trash":
  17. loadMessages($_POST['location'], $user['id']);
  18. break;
  19. case PM_READ_MESSAGE:
  20. case PM_TRASH_MESSAGE:
  21. case PM_DELETE_MESSAGE:
  22. //check if we are deleting multiples
  23. if(strrpos($_POST['messageID'], ":") !== false){
  24. deleteMultiple($user['id'], $_POST['messageID'], $_POST['location']);
  25. }else{
  26. //handle the normal status
  27. updateStatus($user['id'], $_POST['messageID'], $_POST['location'], $_POST['senderID'], $_POST['recipientID']);
  28. }
  29. //if it all went ok, update
  30. echo "output=".$_POST['location'];
  31. break;
  32. case "sendPrivateMessage":
  33. sendMessage($user['id'], $user['username']);
  34. break;
  35. case "checkMessages":
  36. $limit = $_POST['privateMessageLimit'];
  37. echo "output=success&username=".$user['username'].privateMessages($user['id'], $limit)."&admin=".checkAdminPermissions($user['id']);
  38. break;
  39. }
  40. //close the database
  41. mysql_close($link);
  42. function deleteMultiple($userID, $messages, $statusToUpdate){
  43. //we are going to build an array of the message IDs and also snag up the type they are (sender or receiver)
  44. $messageArray = explode(",", $messages);
  45. //loop through the new array and set each status
  46. for($i = 0; $i < count($messageArray); $i++){
  47. //explode again and send it off to the server
  48. $chunks = explode(":", $messageArray[$i]);
  49. //send it off
  50. $senderID = 0;
  51. $recipientID = 0;
  52. if($chunks[1] == "recipient"){
  53. $recipientID = $userID;
  54. }else{
  55. $senderID = $userID;
  56. }
  57. //time to update the status
  58. updateStatus($userID, $chunks[0], $statusToUpdate, $senderID, $recipientID);
  59. }
  60. }
  61. function updateStatus($userID, $messageID, $statusToUpdate, $sendID, $recipID){
  62. global $link;
  63. //make the IDs nice for mySQL
  64. $id = mysql_real_escape_string($messageID);
  65. $senderID = mysql_real_escape_string($sendID);
  66. $recipientID = mysql_real_escape_string($recipID);
  67. //figure out if the logged in user is the sender or the recipient
  68. $userType = "recipient";
  69. if($userID == $senderID){
  70. $userType = "sender";
  71. }
  72. //fire it off
  73. $result = mysql_query("UPDATE ".TABLE_PREFIX."_private SET ".$userType."Status = ".$statusToUpdate." WHERE messageID = ".$id);
  74. if(!$result){
  75. echo "output=mySqlError&error=".mysql_error();
  76. return;
  77. }
  78. //if they are doing a perma delete let's see if we should delete it from the dbase
  79. if($statusToUpdate == PM_DELETE_MESSAGE){
  80. $result = mysql_query("DELETE FROM ".TABLE_PREFIX."_private
  81. WHERE messageID = ".$id."
  82. AND recipientStatus = ".PM_DELETE_MESSAGE."
  83. AND senderStatus = ".PM_DELETE_MESSAGE);
  84. if(!$result){
  85. echo "output=mySqlError&error=".mysql_error();
  86. return;
  87. }
  88. }
  89. }
  90. function loadMessages($location, $userID){
  91. global $link;
  92. $privateMessagesPerPage = mysql_real_escape_string($_POST['privateMessagesPerPage']);
  93. //use this for the dBase to get the PMs
  94. if (!isset($_POST['page']) || empty($_POST['page'])) {
  95. $page = 1;
  96. }else{
  97. $page = mysql_real_escape_string($_POST['page']);
  98. }
  99. $offset = ($page - 1) * $privateMessagesPerPage;
  100. //setup some basic stuff that will help with the query
  101. $where = "";
  102. $and = "";
  103. //get the location we are in
  104. switch($location){
  105. default:
  106. //inbox is the default
  107. $where = "p.recipientID = ".$userID." AND recipientStatus != ".PM_DELETE_MESSAGE." AND recipientStatus != ".PM_TRASH_MESSAGE;
  108. $and = "u.userID = p.senderID";
  109. break;
  110. case "outbox":
  111. $where = "p.senderID = ".$userID." AND senderStatus != ".PM_DELETE_MESSAGE." AND senderStatus != ".PM_TRASH_MESSAGE;
  112. $and = "u.userID = p.recipientID";
  113. break;
  114. case "trash":
  115. //this trash one makes sure that what you see in the trash's "FROM" header is correct
  116. //so PMs from you to someone else will show from you and PMs that youv'e recieved will have the sender's name
  117. $where = "CASE WHEN p.recipientStatus = ".PM_TRASH_MESSAGE."
  118. THEN u.userID = p.senderID
  119. WHEN p.senderStatus = ".PM_TRASH_MESSAGE."
  120. THEN u.userID = p.recipientID
  121. END";
  122. $and = "p.recipientID = ".$userID."
  123. AND p.recipientStatus = ".PM_TRASH_MESSAGE."
  124. OR p.senderID = ".$userID."
  125. AND p.senderStatus = ".PM_TRASH_MESSAGE;
  126. break;
  127. }
  128. // Build query to fetch the PMs
  129. $result = mysql_query("SELECT SQL_CALC_FOUND_ROWS p.*, u.username
  130. FROM ".TABLE_PREFIX."_users u, ".TABLE_PREFIX."_private p
  131. WHERE ".$where."
  132. AND ".$and."
  133. GROUP BY messageID
  134. ORDER BY sent DESC
  135. LIMIT $offset,$privateMessagesPerPage");
  136. if(!$result){
  137. echo "output=mySqlError&error=".mysql_error();
  138. return;
  139. }
  140. //get the total PMs
  141. $totalResult = mysql_query("SELECT FOUND_ROWS() AS totalMessages");
  142. if(!$totalResult){
  143. echo "output=mySqlError&error=".mysql_error();
  144. return;
  145. }
  146. $dataTotal = mysql_fetch_object($totalResult);
  147. //if we are down here then let's start outputting
  148. $output = "output=$location&totalMessages=".$dataTotal->totalMessages;
  149. $output .= "&currentPage=".$page."&totalPages=".ceil($dataTotal->totalMessages / $privateMessagesPerPage);
  150. $output .= "&messageCount=".mysql_num_rows($result);
  151. //loop through the results
  152. for($i = 0; $i < mysql_num_rows($result); $i++){
  153. $pm = mysql_fetch_object($result);
  154. //build the rest out
  155. $output .= "&pm" . $i . "id=" . $pm->messageID;
  156. $output .= "&pm" . $i . "subject=" . urlencode(stripslashes($pm->subject));
  157. $output .= "&pm" . $i . "message=" . urlencode(htmlspecialchars(stripslashes($pm->body)));
  158. $output .= "&pm" . $i . "username=" . urlencode(stripslashes($pm->username));
  159. $output .= "&pm" . $i . "sent=" . urlencode(timeParse($pm->sent, true));
  160. $output .= "&pm" . $i . "replied=" . urlencode(timeParse($pm->repliedTime, true));
  161. $output .= "&pm" . $i . "recipientID=" . $pm->recipientID;
  162. $output .= "&pm" . $i . "recipientStatus=" . $pm->recipientStatus;
  163. $output .= "&pm" . $i . "senderID=" . $pm->senderID;
  164. $output .= "&pm" . $i . "senderStatus=" . $pm->senderStatus;
  165. }
  166. echo $output;
  167. }
  168. function sendMessage($userID, $username) {
  169. global $link;
  170. $privateMessageLimit = $_POST['privateMessageLimit'];
  171. $recipient = mysql_real_escape_string($_POST['recipient']);
  172. $subject = mysql_real_escape_string($_POST['subject']);
  173. $message = mysql_real_escape_string($_POST['message']);
  174. $replyMessageID = mysql_real_escape_string($_POST['replyMessageID']);
  175. $recipientID = getUserID($recipient);
  176. //see if we've snagged a match
  177. if($recipientID == -1){
  178. echo "output=userNotFound";
  179. return;
  180. }
  181. //get the userID of the person we are sending this to (and a current count of the PMs they have)
  182. $result = mysql_query("SELECT COUNT(p.recipientID) AS currentMessages
  183. FROM ".TABLE_PREFIX."_private p
  184. WHERE p.recipientID = ".$recipientID);
  185. if(!$result){
  186. echo "output=mySqlError&error=".mysql_error();
  187. return;
  188. }
  189. //let's see if we can send them a message
  190. $data = mysql_fetch_object($result);
  191. $currentMessageCount = $data->currentMessages;
  192. if($currentMessageCount >= $privateMessageLimit){
  193. //tell them it's full
  194. echo "output=mailboxFull";
  195. return;
  196. }
  197. //if we have the green light let's send the message!
  198. $result = mysql_query("INSERT INTO ".TABLE_PREFIX."_private (recipientID, senderID, subject, body, sent)
  199. VALUES ($recipientID, $userID, '$subject', '$message', ".time().")");
  200. if(!$result){
  201. echo "output=mySqlError&error=".mysql_error();
  202. return;
  203. }
  204. //if this was a reply message, make sure to update that messages reply time
  205. if($replyMessageID > 0){
  206. $result = mysql_query("UPDATE ".TABLE_PREFIX."_private SET repliedTime = ".time()." WHERE messageID = $replyMessageID");
  207. if(!$result){
  208. echo "output=mySqlError&error=".mysql_error();
  209. return;
  210. }
  211. }
  212. //if all is well let flash know
  213. echo "output=success";
  214. //send an email to the recipient letting them know they have a new private message
  215. $result = mysql_query("SELECT username, email FROM ".TABLE_PREFIX."_users WHERE userID = $recipientID");
  216. if(mysql_num_rows($result) > 0){
  217. $data = mysql_fetch_object($result);
  218. //send it off
  219. //$message = str_replace("\r", "\n", $message);
  220. $newPrivateMessage = $_POST['newPrivateMessage'];
  221. $clickToRead = $_POST['clickToRead'];
  222. $mailMessage = $data->username.",\n\n".$newPrivateMessage." ".$username.".\n\n".$clickToRead."\n\n";
  223. $mailMessage .= $_POST['installDirectory']."\n\n-----------------\n".$_POST['subjectWord'].": ".$_POST['subject']."\n\n";
  224. $mailMessage .= $_POST['messageWord'].":\n".$_POST['message'];
  225. sendMail($data->email, $newPrivateMessage." ".$username, $mailMessage, $_POST['boardName'], $_POST['boardEmail']);
  226. }
  227. }
  228. ?>