/Server/index.php

https://github.com/teambravo/Instant-Message · PHP · 346 lines · 252 code · 61 blank · 33 comment · 49 complexity · c5b0d5d2459464e072d20d16d6c913c7 MD5 · raw file

  1. <?php
  2. /****************************************
  3. * Server of Android IM Application
  4. *
  5. * Author: ahmet oguz mermerkaya
  6. * Email: ahmetmermerkaya@hotmail.com
  7. * Date: Dec, 4, 2008
  8. *
  9. * Supported actions:
  10. * 1. authenticateUser
  11. * if user is authentiated return friend list
  12. *
  13. * 2. signUpUser
  14. *
  15. * 3. addNewFriend
  16. *
  17. * 4. responseOfFriendReqs
  18. *************************************/
  19. //TODO: show error off
  20. require_once("mysql.class.php");
  21. $dbHost = "localhost";
  22. $dbUsername = "root";
  23. $dbPassword = "21236161";
  24. $dbName = "test";
  25. $db = new MySQL($dbHost,$dbUsername,$dbPassword,$dbName);
  26. // if operation is failed by unknown reason
  27. define("FAILED", 0);
  28. define("SUCCESSFUL", 1);
  29. // when signing up, if username is already taken, return this error
  30. define("SIGN_UP_USERNAME_CRASHED", 2);
  31. // when add new friend request, if friend is not found, return this error
  32. define("ADD_NEW_USERNAME_NOT_FOUND", 2);
  33. // TIME_INTERVAL_FOR_USER_STATUS: if last authentication time of user is older
  34. // than NOW - TIME_INTERVAL_FOR_USER_STATUS, then user is considered offline
  35. define("TIME_INTERVAL_FOR_USER_STATUS", 60);
  36. define("USER_APPROVED", 1);
  37. define("USER_UNAPPROVED", 0);
  38. $username = (isset($_REQUEST['username']) && count($_REQUEST['username']) > 0)
  39. ? $_REQUEST['username']
  40. : NULL;
  41. $password = isset($_REQUEST['password']) ? md5($_REQUEST['password']) : NULL;
  42. $port = isset($_REQUEST['port']) ? $_REQUEST['port'] : NULL;
  43. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : NULL;
  44. if ($username == NULL || $password == NULL)
  45. {
  46. echo FAILED;
  47. exit;
  48. }
  49. $out = NULL;
  50. error_log($action."\r\n", 3, "error.log");
  51. switch($action)
  52. {
  53. case "authenticateUser":
  54. if ($port != NULL
  55. && ($userId = authenticateUser($db, $username, $password, $port)) != NULL)
  56. {
  57. // providerId and requestId is Id of a friend pair,
  58. // providerId is the Id of making first friend request
  59. // requestId is the Id of the friend approved the friend request made by providerId
  60. // fetching friends,
  61. // left join expression is a bit different,
  62. // it is required to fetch the friend, not the users itself
  63. $sql = "select u.Id, u.username, (NOW()-u.authenticationTime) as authenticateTimeDifference, u.IP,
  64. f.providerId, f.requestId, f.status, u.port
  65. from friends f
  66. left join users u on
  67. u.Id = if ( f.providerId = ".$userId.", f.requestId, f.providerId )
  68. where (f.providerId = ".$userId." and f.status=".USER_APPROVED.") or
  69. f.requestId = ".$userId." ";
  70. if ($result = $db->query($sql))
  71. {
  72. $out .= "<data>";
  73. $out .= "<user userKey='".$userId."' />";
  74. while ($row = $db->fetchObject($result))
  75. {
  76. $status = "offline";
  77. if (((int)$row->status) == USER_UNAPPROVED)
  78. {
  79. $status = "unApproved";
  80. }
  81. else if (((int)$row->authenticateTimeDifference) < TIME_INTERVAL_FOR_USER_STATUS)
  82. {
  83. $status = "online";
  84. }
  85. $out .= "<friend username = '".$row->username."' status='".$status."' IP='".$row->IP."'
  86. userKey = '".$row->Id."' port='".$row->port."'/>";
  87. // to increase security, we need to change userKey periodically and pay more attention
  88. // receiving message and sending message
  89. }
  90. $out .= "</data>";
  91. }
  92. else
  93. {
  94. $out = FAILED;
  95. }
  96. }
  97. else
  98. {
  99. // exit application if not authenticated user
  100. $out = FAILED;
  101. }
  102. break;
  103. case "signUpUser":
  104. if (isset($_REQUEST['email']))
  105. {
  106. $email = $_REQUEST['email'];
  107. $sql = "select Id from users
  108. where username = '".$username."' limit 1";
  109. if ($result = $db->query($sql))
  110. {
  111. if ($db->numRows($result) == 0)
  112. {
  113. $sql = "insert into users(username, password, email)
  114. values ('".$username."', '".$password."', '".$email."') ";
  115. error_log("$sql", 3 , "error_log");
  116. if ($db->query($sql))
  117. {
  118. $out = SUCCESSFUL;
  119. }
  120. else {
  121. $out = FAILED;
  122. }
  123. }
  124. else
  125. {
  126. $out = SIGN_UP_USERNAME_CRASHED;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. $out = FAILED;
  133. }
  134. break;
  135. case "addNewFriend":
  136. $userId = authenticateUser($db, $username, $password);
  137. if ($userId != NULL)
  138. {
  139. if (isset($_REQUEST['friendUserName']))
  140. {
  141. $friendUserName = $_REQUEST['friendUserName'];
  142. $sql = "select Id from users
  143. where username='".$friendUserName."'
  144. limit 1";
  145. if ($result = $db->query($sql))
  146. {
  147. if ($row = $db->fetchObject($result))
  148. {
  149. $requestId = $row->Id;
  150. if ($row->Id != $userId)
  151. {
  152. $sql = "insert into friends(providerId, requestId, status)
  153. values(".$userId.", ".$requestId.", ".USER_UNAPPROVED.")";
  154. if ($db->query($sql))
  155. {
  156. $out = SUCCESSFUL;
  157. }
  158. else
  159. {
  160. $out = FAILED;
  161. }
  162. }
  163. else
  164. {
  165. $out = FAILED; // user add itself as a friend
  166. }
  167. }
  168. else
  169. {
  170. $out = FAILED;
  171. }
  172. }
  173. else
  174. {
  175. $out = FAILED;
  176. }
  177. }
  178. else
  179. {
  180. $out = FAILED;
  181. }
  182. }
  183. else
  184. {
  185. $out = FAILED;
  186. }
  187. break;
  188. case "responseOfFriendReqs":
  189. $userId = authenticateUser($db, $username, $password);
  190. if ($userId != NULL)
  191. {
  192. $sqlApprove = NULL;
  193. $sqlDiscard = NULL;
  194. if (isset($_REQUEST['approvedFriends']))
  195. {
  196. $friendNames = split(",", $_REQUEST['approvedFriends']);
  197. $friendCount = count($friendNames);
  198. $friendNamesQueryPart = NULL;
  199. for ($i = 0; $i < $friendCount; $i++)
  200. {
  201. if (strlen($friendNames[$i]) > 0)
  202. {
  203. if ($i > 0 )
  204. {
  205. $friendNamesQueryPart .= ",";
  206. }
  207. $friendNamesQueryPart .= "'".$friendNames[$i]."'";
  208. }
  209. }
  210. if ($friendNamesQueryPart != NULL)
  211. {
  212. $sqlApprove = "update friends set status = ".USER_APPROVED."
  213. where requestId = ".$userId." and
  214. providerId in (select Id from users where username in (".$friendNamesQueryPart."));
  215. ";
  216. }
  217. }
  218. if (isset($_REQUEST['discardedFriends']))
  219. {
  220. $friendNames = split(",", $_REQUEST['discardedFriends']);
  221. $friendCount = count($friendNames);
  222. $friendNamesQueryPart = NULL;
  223. for ($i = 0; $i < $friendCount; $i++)
  224. {
  225. if (strlen($friendNames[$i]) > 0)
  226. {
  227. if ($i > 0 )
  228. {
  229. $friendNamesQueryPart .= ",";
  230. }
  231. $friendNamesQueryPart .= "'".$friendNames[$i]."'";
  232. }
  233. }
  234. if ($friendNamesQueryPart != NULL)
  235. {
  236. $sqlDiscard = "delete from friends
  237. where requestId = ".$userId." and
  238. providerId in (select Id from users where username in (".$friendNamesQueryPart."));
  239. ";
  240. }
  241. }
  242. if ( ($sqlApprove != NULL ? $db->query($sqlApprove) : true) &&
  243. ($sqlDiscard != NULL ? $db->query($sqlDiscard) : true)
  244. )
  245. {
  246. $out = SUCCESSFUL;
  247. }
  248. else
  249. {
  250. $out = FAILED;
  251. }
  252. }
  253. else
  254. {
  255. $out = FAILED;
  256. }
  257. break;
  258. default:
  259. $out = FAILED;
  260. break;
  261. }
  262. echo $out;
  263. ///////////////////////////////////////////////////////////////
  264. function authenticateUser($db, $username, $password, $port)
  265. {
  266. $sql = "select Id from users
  267. where username = '".$username."' and password = '".$password."'
  268. limit 1";
  269. $out = NULL;
  270. if ($result = $db->query($sql))
  271. {
  272. if ($row = $db->fetchObject($result))
  273. {
  274. $out = $row->Id;
  275. $sql = "update users set authenticationTime = NOW(),
  276. IP = '".$_SERVER["REMOTE_ADDR"]."' ,
  277. port = ".$port."
  278. where Id = ".$row->Id."
  279. limit 1";
  280. $db->query($sql);
  281. }
  282. }
  283. return $out;
  284. }
  285. ?>