PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mybookbag/functions.php

https://bitbucket.org/s2223902/mybookbag
PHP | 336 lines | 291 code | 39 blank | 6 comment | 33 complexity | 4a01ed45074e796c8c2fbe74901bb482 MD5 | raw file
  1. <?php
  2. include('db_connection.php');
  3. function checkMessages(){ //Checks if there are any new messages or not, and returns the amount.
  4. $checkmsg = mysql_query('select m1.id, m1.title, m1.timestamp, count(m2.id) as reps, users.id as userid, users.username from pm as m1, pm as m2,users where ((m1.user1="'.$_SESSION['id'].'" and m1.user1read="no" and users.id=m1.user2) or (m1.user2="'.$_SESSION['id'].'" and m1.user2read="no" and users.id=m1.user1)) and m1.id2="1" and m2.id=m1.id group by m1.id order by m1.id desc') or die ("fetch1:".mysql_error());
  5. $amountmsg = mysql_num_rows($checkmsg);
  6. return $amountmsg;
  7. }
  8. function books_Set($type){ //Checks how many books have been set with the given type for the user
  9. $query = "select `user_book`.`entryid` from `book` natural join `user_book` where id={$_SESSION["id"]} and `book`.`type`='{$type}'";
  10. $handle = mysql_query($query) or die(mysql_error());
  11. $amount = intval(mysql_num_rows($handle));
  12. return $amount;
  13. }
  14. function loadBook($type){ //Loads the books of the given type for the user.
  15. $id=$_SESSION["id"];
  16. if(isset($_SESSION["remoteid"])){ //If it tries to load books from a different user.
  17. $addpermission = "and permission=1";
  18. $id = $_SESSION["remoteid"];
  19. }
  20. else{
  21. $addpermission = "";
  22. }
  23. $orderBy = array('title','author','isbn');
  24. if (isset($_GET['order']) && in_array($_GET['order'], $orderBy)) {
  25. $order = $_GET['order'];
  26. }
  27. else{
  28. $order = 'title asc';
  29. }
  30. if($order==$_SESSION["Order"] && $_GET["order"]){
  31. $order = "$order desc";
  32. }
  33. $_SESSION["Order"] = $order;
  34. $query = "select `title`,`author`,`isbn` from `book` natural join `author_book`,`user_book` where `author_book`.`entryid`=`user_book`.`entryid` and `user_book`.`entryid`=`book`.`entryid` and `user_book`.`id`=$id and type='{$type}' $addpermission order by $order";
  35. //echo $query;
  36. $handle = mysql_query($query) or die(mysql_error());
  37. $nr = 0;
  38. while($row = mysql_fetch_array($handle)){
  39. $userbooks[$nr]["title"] = $row["title"];
  40. $userbooks[$nr]["author"] = $row["author"];
  41. $userbooks[$nr]["isbn"] = $row["isbn"];
  42. $nr++;
  43. }
  44. $_SESSION["userbooks"] = $userbooks;
  45. return $userbooks;
  46. }
  47. function amount_Type(){ //Checks if there are any books at all with the given type, and returns False, or the corresponding number.
  48. $type = $_SESSION["currtype"];
  49. $query = "select `user_book`.`entryid` from `book` natural join `user_book` where id={$_SESSION["id"]} and `book`.`type`='{$type}'";
  50. $handle = mysql_query($query) or die(mysql_error());
  51. $amount = intval(mysql_num_rows($handle));
  52. if($amount==0){$amount=False;}
  53. return $amount;
  54. }
  55. function showBooks($userbooks){ //Prints all the books for the user page
  56. if(amount_Type()){
  57. $Table = "<table><tr><td><b><a href=\"?order=title\">Title</a></b></td><td><b><a href=\"?order=author\">Author</a></b></td><td><b><a href=\"?order=isbn\">ISBn</a></b></td></tr>";
  58. for($i=1;$i<count($userbooks);$i++){
  59. $title = $userbooks[$i]["title"];
  60. $author = $userbooks[$i]["author"];
  61. $isbn = $userbooks[$i]["isbn"];
  62. $Table = "$Table<tr><td>$title</td><td>$author</td><td>$isbn</td></tr>";
  63. }
  64. $Table ="$Table</table>";
  65. return $Table;
  66. }
  67. else{
  68. return "You have not added any books yet. Please view the MyBookBag tab and add some books so we can display them for you here.";
  69. }
  70. }
  71. /////
  72. function stripArray($arr){ //Strips the array and creates session variables of it
  73. $Keys = array_keys($arr);
  74. foreach($Keys as $Key){
  75. $_SESSION["Create"][$Key] = $arr[$Key];
  76. }
  77. if(!isset($arr["author"])){
  78. $_SESSION["Create"]["author"] = False;
  79. }
  80. if(!isset($arr["title"])){
  81. $_SESSION["Create"]["title"] = False;
  82. }
  83. //else{
  84. //$_SESSION["Create"]["title"] = $arr["title"];
  85. //}
  86. return;
  87. }
  88. function isAccepted($form){ //Checks if the inserted form will be accepted. A title is the minimum.
  89. if (isset($_GET['order']) && isset($_SESSION["results"])){
  90. return True;
  91. }
  92. $accepted = True;
  93. if(isset($_GET["sort"])){
  94. return $accepted;
  95. }
  96. if(empty($form["title"]) && empty($form["author"])){
  97. $accepted = False;
  98. }
  99. else if ($form["title"]){
  100. $list = array("of","and","in","in the","against","against the","for","to","of the","to the","and the","with","with the","by","by the","because of","vs.","for the");
  101. foreach($list as $word){
  102. if($form["title"]==$word){
  103. $accepted = False;
  104. }
  105. }
  106. return $accepted;
  107. }
  108. }
  109. function loadFriends(){ //Loads all user's friends
  110. $query = "select `friend_id`,`p_id`,`username`,`email` from `users` natural join `friend_of` where `friend_of`.`friend_id`=`users`.`id` and `p_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2";
  111. $handle = mysql_query($query);
  112. $query2 = "select `p_id`,`friend_id`,`username`,`email` from `users` natural join `friend_of` where `friend_of`.`p_id`=`users`.`id` and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2"; //For reverse cases! difference between the 'adder and the added'
  113. $handle2 = mysql_query($query2) or die(mysql_error());
  114. if(mysql_num_rows($handle)==0 && mysql_num_rows($handle2)==0 ){
  115. return False;
  116. }
  117. $friendTable = "<table><form action=\"remoteuser.php\" method=\"post\" name=\"form1\"><tr><td>Username</td><td>E-Mail</td></tr>";
  118. while($row=mysql_fetch_assoc($handle)){ //Collects all the results it can find.
  119. $username = $row["username"];
  120. $email = $row["email"];
  121. $id = $row["friend_id"];
  122. $friendTable = "$friendTable<tr><td><a href=\"remoteuser.php?id=$id\">$username</a></td><td>$email</td></tr>";
  123. }
  124. while($row=mysql_fetch_assoc($handle2)){
  125. $username = $row["username"];
  126. $email = $row["email"];
  127. $id = $row["p_id"];
  128. $friendTable = "$friendTable<tr><td><a href=\"remoteuser.php?id=$id\">$username</a></td></td><td>$email</td></tr>";
  129. }
  130. $friendTable = "$friendTable</table>";
  131. return $friendTable;
  132. }
  133. function getOutPending(){ //Retrieves outgoing friendrequests.
  134. $query = "SELECT `friend_id`
  135. FROM `friend_of`
  136. WHERE `p_id` ={$_SESSION["id"]}
  137. AND `friend_of`.`accepted` =1
  138. LIMIT 0 , 30";
  139. $handle = mysql_query($query);
  140. $pending = mysql_num_rows($handle);
  141. if($pending>0){
  142. echo $pending;
  143. }
  144. else{
  145. echo 0;
  146. }
  147. }
  148. function getInPending(){ //Retrieves incoming friendrequests.
  149. $query = "SELECT `p_id`
  150. FROM `friend_of`
  151. WHERE `friend_id` ={$_SESSION["id"]}
  152. AND `friend_of`.`accepted` =1
  153. LIMIT 0 , 30";
  154. $handle = mysql_query($query);
  155. $pending = mysql_num_rows($handle);
  156. if($pending>0){
  157. echo $pending;
  158. }
  159. else{
  160. echo 0;
  161. }
  162. }
  163. function showFriends(){ //Prints all user's friends
  164. if(!$_SESSION["friends"]){
  165. echo "<p>You currently have no friends! View the MyFriends tab to add some people you know!</p>";
  166. }
  167. else{
  168. echo "<p>These are your friends!</p>";
  169. echo $_SESSION["friends"];
  170. }
  171. }
  172. function requestForm(){ //Retrieves if any friendship requests need answering.
  173. $query = "
  174. select `username`,`users`.`id`
  175. from `users`
  176. natural join
  177. `friend_of`
  178. where `friend_of`.`friend_id`={$_SESSION["id"]}
  179. and `p_id`=`users`.`id`
  180. and `friend_of`.`accepted`=1";
  181. $handle = mysql_query($query) or die(mysql_error());
  182. if(mysql_num_rows($handle)==0){
  183. return "You currently have no requests that require answering.";
  184. }
  185. $requestForm = "<table><tr><td>Username</td></tr>";
  186. while($row = mysql_fetch_array($handle)){
  187. $request = $row["username"];
  188. $id = $row["id"];
  189. $requestForm = "<form action=\"confirmrequest.php\" method=\"post\">$requestForm<tr><td>$request</td><td><input type=\"submit\" name=\"Answer\" value=\"Accept\" ></input></td><td><input type=\"submit\" name=\"Answer\" value=\"Reject\" ></input></td><input type=\"hidden\" name=\"targetid\" value=\"{$id}\"></input></tr></form>";
  190. }
  191. return $requestForm;
  192. }
  193. function showRequests(){ //Prints all requests
  194. return $_SESSION["requests"];
  195. }
  196. function sendAnswer(){ //Sends the answer (Accept/Reject) and creates additional rows corresponding to that.
  197. if($_POST["Answer"]=="Accept"){
  198. $query = "update `friend_of` set `accepted`=2 where `p_id`={$_POST["targetid"]} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=1";
  199. $query2 = "delete from `friend_of` where `p_id`={$_POST["targetid"]} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=0";
  200. mysql_query($query) or die(mysql_error());
  201. mysql_query($query2) or die(mysql_error());
  202. $_SESSION["notify"] = "You have accepted the friendrequest! Your friend has now newly appeared in your friendslist! Check it out!";
  203. }
  204. else{
  205. $query = "update `friend_of` set `accepted`=0 where `p_id`={$_POST["targetid"]} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=1";
  206. mysql_query($query) or die(mysql_error());
  207. $_SESSION["notify"] = "You rejected the friendrequest! Boo-hoo you complex life-form with emotional issues!";
  208. }
  209. }
  210. function addFriendForm(){ //Requests friendship form
  211. $form = "<form action=\"processrequest.php\" method=\"post\">
  212. <table><tr><td><input type=\"text\" name=\"username\" value=\"username\"></input></td><td><input type=\"text\" name=\"email\" value=\"E-mail\"></input></td><td><input type=\"submit\" name=\"submit\" value=\"Send Request!\"></input></td></tr></table>";
  213. return $form;
  214. }
  215. function determineTarget($username,$email){ //Gets the userid from inserted username and/or e-mail
  216. if(empty($username)){
  217. $query = "select `id` from `users` where `email`='{$email}'";
  218. }
  219. if(empty($email)){
  220. $query = "select `id` from `users` where `username`='{$username}'";
  221. }
  222. if($username&&$email){
  223. $query = "select `id` from `users` where `email`='{$email}' or `username`='{$username}'";
  224. }
  225. $handle = mysql_query($query) or die(mysql_error());
  226. while($row = mysql_fetch_array($handle)){
  227. $id = $row["id"];
  228. }
  229. return $id;
  230. }
  231. function loadRemoteLibrary($called_id){ // Loads the books that can be seen by the current user FROM another user... So : Public books and friend books (if they are friends)
  232. $totalRemoteLib = array();
  233. if(friends($called_id)){
  234. $totalRemoteLib[]= loadRemoteBook("b",1,$called_id);
  235. $totalRemoteLib[]= loadRemoteBook("e",1,$called_id);
  236. $totalRemoteLib[]= loadRemoteBook("j",1,$called_id);
  237. }
  238. $totalRemoteLib[]= loadRemoteBook("b",2,$called_id);
  239. $totalRemoteLib[]= loadRemoteBook("e",2,$called_id);
  240. $totalRemoteLib[]= loadRemoteBook("j",2,$called_id);
  241. $RemoteTable = array();
  242. foreach($totalRemoteLib as $piece){
  243. $Table = "<table><tr><td><b>Title</b></td><td><b>Author</b></td><td><b>ISBn</b></td><td>Type</td></tr>";
  244. for($i=1;$i<count($piece);$i++){
  245. $title = $piece[$i]["title"];
  246. $author = $piece[$i]["author"];
  247. $isbn = $piece[$i]["isbn"];
  248. $Table = "$Table<tr><td>$title</td><td>$author</td><td>$isbn</td></tr>";
  249. }
  250. $Table ="$Table</table>";
  251. $RemoteTable[$i] = $Table;
  252. }
  253. return $RemoteTable;
  254. }
  255. function loadRemoteBook($type,$permission,$id){
  256. $query = "select `title`,`author`,`isbn` from `book` natural join `author_book`,`user_book` where `author_book`.`entryid`=`user_book`.`entryid` and `user_book`.`entryid`=`book`.`entryid` and `user_book`.`id`=$id and type='{$type}' and permission=$permission order by title" or die("?");
  257. $handle = mysql_query($query) or die(mysql_error());
  258. $nr = 0;
  259. while($row = mysql_fetch_array($handle)){
  260. $userbooks[$nr]["title"] = $row["title"];
  261. $userbooks[$nr]["author"] = $row["author"];
  262. $userbooks[$nr]["isbn"] = $row["isbn"];
  263. $nr++;
  264. }
  265. $_SESSION["userbooks"] = $userbooks;
  266. return $userbooks;
  267. }
  268. function friends($called_id){
  269. $query = "select `p_id` from `users` natural join `friend_of` where `friend_of`.`friend_id`={$called_id} and `p_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2";
  270. $handle = mysql_query($query);
  271. $query2 = "select `friend_id` from `users` natural join `friend_of` where `friend_of`.`p_id`={$called_id} and `friend_id`={$_SESSION["id"]} and `friend_of`.`accepted`=2"; //For reverse cases! difference between the 'adder and the added'
  272. $handle2 = mysql_query($query2) or die(mysql_error());
  273. if(mysql_num_rows($handle)==0 && mysql_num_rows($handle2)==0 ){
  274. return False;
  275. }
  276. else{
  277. return True;
  278. }
  279. }
  280. function getRemoteName($id){
  281. $query = "select username from `users` where `id`=$id";
  282. $handle = mysql_query($query) or die(mysql_error());
  283. while($row = mysql_fetch_array($handle)){
  284. $name = $row["username"];
  285. }
  286. return $name;
  287. }
  288. function getid(){
  289. //Selects the highest ID in the database so new entries can be submitted accordingly
  290. $IDQ = "select max(entryid) from entry";
  291. $handle = mysql_query($IDQ) or die(mysql_error());
  292. while($row = mysql_fetch_array($handle)){
  293. $id = $row["max(entryid)"];
  294. }
  295. return $id;
  296. }
  297. ?>