/classes/friends.php

https://github.com/veb/lifelitup · PHP · 293 lines · 288 code · 5 blank · 0 comment · 8 complexity · 09b8228fe89e1a3ace663888239086fa MD5 · raw file

  1. <?php
  2. require_once('classes/profile.php');
  3. class Friends {
  4. function addFriend($id, $userid) {
  5. $db = Database::obtain();
  6. if ($this->checkPendingFriendship($id, $userid) == true) {
  7. $msg = "This friendship already has a pending request. Check your emails.";
  8. return $msg;
  9. }
  10. if ($this->checkHaveAlready($id, $userid) == true) {
  11. $msg = 'You are already friends with this person!';
  12. return $msg;
  13. }
  14. if ($this->checkFriendExists($id) == false) {
  15. $msg = 'This memeber does not exist.';
  16. return $msg;
  17. }
  18. if ($this->checkBlankProfile($id) == false) {
  19. $msg = "This user hasn't completed his/her profile so cannot be currently added to your friends.";
  20. return $msg;
  21. }
  22. if ($this->checkBlankProfile($userid) == false) {
  23. $msg = "You need to fill out your profile before being able to add friends.";
  24. return $msg;
  25. }
  26. $data['user_id'] = $userid;
  27. $data['friend_id'] = $id;
  28. $data['date'] = time();
  29. $data['verified'] = 0;
  30. $pid = $db->insert(tbl_friends, $data);
  31. }
  32. function removeFriend($friendid, $userid) {
  33. $db = Database::obtain();
  34. if ($this->checkHaveAlready($friendid, $userid) == false) {
  35. $msg = 'You are not currently friends with this person silly';
  36. return $msg;
  37. }
  38. if ($this->checkFriendExists($friendid) == false) {
  39. $msg = 'This user doesn\'t exist in our system';
  40. return $msg;
  41. }
  42. $sql = "DELETE FROM " . tbl_friends . " WHERE user_id = " . (int)$userid . " AND friend_id = " . (int)$friendid . " AND verified = 1";
  43. $q = $db->query($sql);
  44. if ($q > 0)
  45. {
  46. $sqlRemFriend = "DELETE FROM " . tbl_friends . " WHERE friend_id = " . (int)$userid . " AND user_id = " . (int)$friendid;
  47. $q = $db->query($sqlRemFriend);
  48. }
  49. else
  50. {
  51. $sqlIndFriend = "DELETE FROM " . tbl_friends . " WHERE user_id = " . (int)$userid . " AND friend_id = " . (int)$friendid;
  52. $q = $db->query($sqlIndFriend);
  53. }
  54. }
  55. function checkBlankProfile($id) {
  56. $db = Database::obtain();
  57. $sql = "SELECT user_id
  58. FROM " . tbl_profile . "
  59. WHERE user_id = " . (int)$id . " AND first_name != ''";
  60. $row = $db->query_first($sql);
  61. if (!empty($row)) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. function checkPendingFriendship($id, $userid) {
  67. $db = Database::obtain();
  68. $sql = "SELECT user_id
  69. FROM " . tbl_friends . "
  70. WHERE user_id = " . (int)$userid . " AND friend_id = " . (int)$id . " AND verified='0'";
  71. $row = $db->query_first($sql);
  72. if (!empty($row)) {
  73. return true;
  74. }
  75. $sql = "SELECT user_id
  76. FROM " . tbl_friends . "
  77. WHERE user_id = " . (int)$id . " AND friend_id = " . (int)$userid . " AND verified='0'";
  78. $row = $db->query_first($sql);
  79. if (!empty($row)) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. function checkFriendExists($id) {
  85. $db = Database::obtain();
  86. $sql = "SELECT user_id
  87. FROM " . tbl_profile . "
  88. WHERE user_id = " . (int)$id;
  89. $row = $db->query_first($sql);
  90. if (!empty($row)) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. function checkHaveAlready($id, $userid) {
  96. $db = Database::obtain();
  97. $sql = "SELECT friend_id
  98. FROM " . tbl_friends . "
  99. WHERE friend_id = " . (int)$id . "
  100. AND user_id = " . (int)$userid;
  101. $row = $db->query_first($sql);
  102. if ($row > 0) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. function getFriends($userid) {
  108. $db = Database::obtain();
  109. $sql = "SELECT " . tbl_users . ".id, " . tbl_users . ".email, " . tbl_profile . ".first_name, " . tbl_profile . ".last_name, " . tbl_profile . ".twitter, " . tbl_profile . ".facebook
  110. FROM " . tbl_friends . "
  111. JOIN " . tbl_users ." ON " . tbl_friends . ".friend_id = " . tbl_users . ".id
  112. JOIN " . tbl_profile . " ON " . tbl_friends . ".friend_id = " . tbl_profile . ".user_id
  113. WHERE " . tbl_friends . ".user_id = " . $userid . " AND " . tbl_friends . ".verified = '1'";
  114. $friends = $db->fetch_array($sql);
  115. return $friends;
  116. }
  117. function checkIsFriend($friendid, $userid) {
  118. $db = Database::obtain();
  119. $sql = "SELECT friend_id
  120. FROM " . tbl_friends . "
  121. WHERE friend_id = " . (int)$friendid . " AND user_id = " . (int)$userid;
  122. $pid = $db->query_first($sql);
  123. if ($pid > 0)
  124. {
  125. return true;
  126. }
  127. return false;
  128. }
  129. function checkAlreadyVerified($userid, $friendid) {
  130. $db = Database::obtain();
  131. $sql = "SELECT user_id, friend_id
  132. FROM " . tbl_friends . "
  133. WHERE friend_id = " . (int)$friendid . " AND user_id = " . (int)$userid . " AND verified='1'";
  134. $pid = $db->query_first($sql);
  135. if ($pid > 0)
  136. {
  137. return true;
  138. }
  139. return false;
  140. }
  141. function checkAlreadyIgnored($userid, $friendid) {
  142. $db = Database::obtain();
  143. $sql = "SELECT friend_id
  144. FROM " . tbl_friends . "
  145. WHERE friend_id = " . (int)$friendid . " AND user_id = " . (int)$userid;
  146. $pid = $db->query_first($sql);
  147. if ($pid > 0)
  148. {
  149. return true;
  150. }
  151. return false;
  152. }
  153. function sendFriendVerification($userid, $friendid) {
  154. $profile = new Profile;
  155. $user = $profile->get(intval($userid));
  156. $friend = $profile->get(intval($friendid));
  157. $subject = "You have a new friend request over at LifeLitUp.com";
  158. $emailMsg = "Hi " . $friend["first_name"] . ",\n"
  159. ."" . $user["first_name"] . " " . $user["last_name"] . " wants to become your friend on LifeLitUp\n\n"
  160. ."If you know this person and want to confirm this friendship, then please click here:\n"
  161. ."http://www.lifelitup.com/alpha/profile.php?action=confirmFriend&userid=" . $friend["user_id"] . "&friendid=" . $user["user_id"] . "\n\n"
  162. ."If you do not know this person or want to ignore this friend request, then click the link below:\n"
  163. ."http://www.lifelitup.com/alpha/profile.php?action=ignoreFriend&userid=" . $friend["user_id"] . "&friendid=" . $user["user_id"] . "\n\n"
  164. ."Regards,\n"
  165. ."The LLU Team!";
  166. $headers = 'From: no-reply@lifelitup.com' . "\r\n" .
  167. 'Reply-To: no-reply@lifelitup.com' . "\r\n" .
  168. 'X-Mailer: PHP/' . phpversion();
  169. mail($friend['email'], $subject, $emailMsg, $headers);
  170. }
  171. function verifyFriend($friendid, $userid) {
  172. $db = Database::obtain();
  173. if ($this->checkIsFriend($friendid, $userid) == false) {
  174. $msg = 'This person hasn\'t previously added you to their friends list.';
  175. return $msg;
  176. }
  177. if ($this->checkAlreadyVerified($userid, $friendid) == true) {
  178. $msg = 'This friendship has already been verified.';
  179. return $msg;
  180. }
  181. $udata["verified"] = 1;
  182. $db->update(tbl_friends, $udata, "user_id=" . $userid ."");
  183. $fdata["user_id"] = $friendid;
  184. $fdata["friend_id"] = $userid;
  185. $fdata["date"] = time();
  186. $fdata["verified"] = 1;
  187. $pid = $db->insert(tbl_friends, $fdata);
  188. return $pid;
  189. }
  190. function ignoreFriend($friendid, $userid) {
  191. $db = Database::obtain();
  192. if ($this->checkAlreadyIgnored($userid, $friendid) == false) {
  193. $msg = 'This friendship has already been ignored.';
  194. return $msg;
  195. }
  196. if ($this->checkIsFriend($friendid, $userid) == false) {
  197. $msg = 'This person hasn\'t previously added you to their friends list.';
  198. return $msg;
  199. }
  200. if ($this->checkAlreadyVerified($userid, $friendid) == true) {
  201. $msg = 'This friendship has already been verified.';
  202. return $msg;
  203. }
  204. $sql = "DELETE FROM " . tbl_friends . " WHERE user_id = " . $userid . " AND friend_id = " . $friendid;
  205. $row = $db->query($sql);
  206. return $row;
  207. }
  208. function getFriendRequests($userid) {
  209. $db = Database::obtain();
  210. $sql = "SELECT " . tbl_friends . ".user_id, " . tbl_profile . ".first_name, " . tbl_profile . ".last_name
  211. FROM " . tbl_friends . "
  212. JOIN " . tbl_profile . " ON " . tbl_friends . ".user_id = " . tbl_profile . ".user_id
  213. WHERE " . tbl_friends . ".friend_id = " . $userid . " AND verified='0'";
  214. $requests = $db->fetch_array($sql);
  215. return $requests;
  216. }
  217. }
  218. ?>