PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/mybookbag/readmsg.php

https://bitbucket.org/s2223902/mybookbag
PHP | 190 lines | 189 code | 0 blank | 1 comment | 1 complexity | e91bee4b96bf32d6a6e933615687b942 MD5 | raw file
  1. <?php
  2. session_start();
  3. include('db_connection.php');
  4. include('functions.php');
  5. //We check if the user is logged
  6. if(isset($_SESSION['username']))
  7. {
  8. ?>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  13. <link rel="stylesheet" href="css/formstyles.css" type="text/css" />
  14. <link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
  15. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
  16. <link href="css/toolbar.css" rel="stylesheet" type="text/css" />
  17. <script type="text/javascript" src="js/Placeholders.js"></script>
  18. <script type="text/javascript">
  19. Placeholders.init({
  20. live: true,
  21. hideOnFocus: true});
  22. </script>
  23. <title><?php echo $_SESSION['username'];?>'s Messages</title>
  24. </head>
  25. <body>
  26. <?php include ("php/random-bg.php"); ?>
  27. <div id="mainContainer">
  28. <div id="carbonForm2">
  29. <div id="logo2">
  30. <img src="img/logo.png" />
  31. <p>Welcome <b><?php echo $_SESSION['username'];?></b></p>
  32. </div>
  33. <ul id="nav">
  34. <a href="page.php">Home</a></li>
  35. <li><a href="mybooks.php">MyBookBag</a>
  36. <ul>
  37. <li><a href="mybooks.php">My Books</a></li>
  38. <li><a href="myebooks.php">My eBooks</a></li>
  39. <li><a href="myjournals.php">My journals</a></li>
  40. <li><a href="adddoc.php">Add Books</a></li>
  41. </ul>
  42. </li>
  43. <li class="current"><a href="friends.php">My Friends</a>
  44. <ul>
  45. <li><a href="friends.php">My Friends</a></li>
  46. <li><a href="messages.php">Messages (<?php echo checkMessages();?>)</a></li>
  47. <li><a href="addfriend.php">Add friends</a></li>
  48. <li><a href="requests.php">Friend Requests</a></li>
  49. </ul>
  50. </li>
  51. <li><a href="settings.php">Settings</a>
  52. <ul>
  53. <li><a href="passreset.php">Change Password</a></li>
  54. </ul>
  55. </li>
  56. <li><a href="contact.php">Contact</a></li>
  57. <li><a href="logout.php">Log Out</a></li>
  58. </ul>
  59. <div class="fieldContainer">
  60. <?php
  61. //We check if the ID of the discussion is defined
  62. if(isset($_GET['id']))
  63. {
  64. $id = intval($_GET['id']);
  65. //We get the title and the narators of the discussion
  66. $req1 = mysql_query('select title, user1, user2 from pm where id="'.$id.'" and id2="1"') or die ("fetch1".mysql_error());
  67. $dn1 = mysql_fetch_array($req1);
  68. //We check if the discussion exists
  69. if(mysql_num_rows($req1)==1)
  70. {
  71. //We check if the user have the right to read this discussion
  72. if($dn1['user1']==$_SESSION['id'] or $dn1['user2']==$_SESSION['id'])
  73. {
  74. //The discussion will be placed in read messages
  75. if($dn1['user1']==$_SESSION['id'])
  76. {
  77. mysql_query('update pm set user1read="yes" where id="'.$id.'" and id2="1"') or die ("fetch2".mysql_error());
  78. $user_partic = 2;
  79. }
  80. else
  81. {
  82. mysql_query('update pm set user2read="yes" where id="'.$id.'" and id2="1"') or die ("fetch3".mysql_error());
  83. $user_partic = 1;
  84. }
  85. //We get the list of the messages
  86. $req2 = mysql_query('select pm.timestamp, pm.message, users.id as userid, users.username from pm, users where pm.id="'.$id.'" and users.id=pm.user1 order by pm.id2') or die ("fetch3".mysql_error());
  87. //We check if the form has been sent
  88. if(isset($_POST['message']) and $_POST['message']!='')
  89. {
  90. $message = $_POST['message'];
  91. //We remove slashes depending on the configuration
  92. if(get_magic_quotes_gpc())
  93. {
  94. $message = stripslashes($message);
  95. }
  96. //We protect the variables
  97. $message = mysql_real_escape_string(nl2br(htmlentities($message, ENT_QUOTES, 'UTF-8')));
  98. //We send the message and we change the status of the discussion to unread for the recipient
  99. if(mysql_query('insert into pm (id, id2, title, user1, user2, message, timestamp, user1read, user2read)values
  100. ("'.$id.'",
  101. "'.(intval(mysql_num_rows($req2))+1).'",
  102. "",
  103. "'.$_SESSION['id'].'",
  104. "6",
  105. "'.$message.'",
  106. "'.time().'",
  107. "",
  108. "")')
  109. and mysql_query('update pm set user'.$user_partic.'read="yes" where id="'.$id.'" and id2="1"') or die ("fetch4".mysql_error()))
  110. {
  111. ?>
  112. <div class="message">Your message has successfully been sent.<br />
  113. <a href="readmsg.php?id=<?php echo $id; ?>">Go to the discussion</a></div>
  114. <?php
  115. }
  116. else
  117. {
  118. ?>
  119. <div class="message">An error occurred while sending the message.<br />
  120. <a href="readmsg.php?id=<?php echo $id; ?>">Go to the discussion</a></div>
  121. <?php
  122. }
  123. }
  124. else
  125. {
  126. //We display the messages
  127. ?>
  128. <div class="content">
  129. <h1><?php echo $dn1['title']; ?></h1>
  130. <table class="messages_table">
  131. <tr>
  132. <th class="author">User</th>
  133. <th>Message</th>
  134. </tr>
  135. <?php
  136. while($dn2 = mysql_fetch_array($req2))
  137. {
  138. ?>
  139. <tr>
  140. <td class="author center">
  141. <br /><a href="profile.php?id=<?php echo $dn2['id']; ?>"><?php echo $dn2['username']; ?></a></td>
  142. <td class="left"><div class="date">Sent: <?php echo date('m/d/Y H:i:s' ,$dn2['timestamp']); ?></div>
  143. <?php echo $dn2['message']; ?></td>
  144. </tr>
  145. <?php
  146. }
  147. //We display the reply form
  148. ?>
  149. </table><br />
  150. <h2>Reply</h2>
  151. <div class="center">
  152. <form action="readmsg.php?id=<?php echo $id; ?>" method="post">
  153. <label for="message" class="center">Message</label><br />
  154. <textarea cols="40" rows="5" name="message" id="message"></textarea><br />
  155. <br /><input type="submit" id="submit" value="Send" /><br />
  156. </form>
  157. </div>
  158. </div>
  159. <?php
  160. }
  161. }
  162. else
  163. {
  164. echo '<div class="message">You dont have the rights to access this page.</div>';
  165. }
  166. }
  167. else
  168. {
  169. echo '<div class="message">This discussion does not exists.</div>';
  170. }
  171. }
  172. else
  173. {
  174. echo '<div class="message">The discussion ID is not defined.</div>';
  175. }
  176. }
  177. else
  178. {
  179. echo '<div class="message">You must be logged to access this page.</div>';
  180. }
  181. ?>
  182. <div class="foot"><a href="messages.php">Go to my Personal messages</a></div>
  183. </div>
  184. </div>
  185. </div>
  186. </body>
  187. </html>