PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/chatAPI.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 221 lines | 168 code | 44 blank | 9 comment | 16 complexity | 498192f8651f72265643178b3aff241c MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * TABLEPREFIX: is configured in the include/config.php file included by the general_include.php
  4. */
  5. include("general_include.php");
  6. include_once("adodb/dbconfig.php");
  7. include "checklogin.php";
  8. function getPostOrGetParam($param){
  9. if(isset($_POST[$param]))
  10. return $_POST[$param];
  11. else{
  12. if(isset($_GET[$param]))
  13. return $_GET[$param];
  14. else
  15. return "";
  16. }
  17. }
  18. function markReadMessages($messageIds, $fromUserId=null) {
  19. // Accesing the global object inside the local scope
  20. global $UserManagerObjAjax;
  21. if (isset($messageIds) && is_array($messageIds))
  22. $messageIds = implode(",",$messageIds);
  23. $sqlQuery = "UPDATE ". TABLEPREFIX ."_message SET is_read='Y' ";
  24. if (isset($messageIds))
  25. $sqlQuery .= "WHERE message_id in (". $messageIds .")";
  26. elseif (isset($fromUserId))
  27. $sqlQuery .= "WHERE user_id_from=". $fromUserId ." AND reply_of_msg=-1";
  28. if (isset($messageIds) || isset($fromUserId))
  29. $UserManagerObjAjax->Execute($sqlQuery);
  30. }
  31. function messagesPending() {
  32. // Accesing the global object inside the local scope
  33. global $UserManagerObjAjax;
  34. $userId = $_SESSION['user_id'];
  35. $sqlQuery = "SELECT u.user_id, u.name, m.message_id, m.message_text, ups.phono_session_id "
  36. ."FROM ". TABLEPREFIX ."_message m, ". TABLEPREFIX ."_user u, ". TABLEPREFIX ."_user_phono_session ups "
  37. ."WHERE m.user_id_from=u.user_id AND ups.nk_user_id=u.user_id AND user_id_to=". $userId ." AND is_read='N' AND reply_of_msg=-1";
  38. $messages = $UserManagerObjAjax->GetRecords("All",$sqlQuery);
  39. $ret = array();
  40. $messagesIdToUpdate = array();
  41. for($i=0;$i<count($messages);$i++) {
  42. $theFromChatSession = &$ret[$messages[$i]['user_id']];
  43. if (!isset($theFromChatSession)) {
  44. $theFromChatSession = array(
  45. "fromUserId" => $messages[$i]['user_id'],
  46. "fromName" => $messages[$i]['name'],
  47. "sessionId" => $messages[$i]['phono_session_id'],
  48. "messages" => array(),
  49. );
  50. $ret[$messages[$i]['user_id']] = &$theFromChatSession;
  51. }
  52. $theFromChatSession['messages'][]=$messages[$i]['message_text'];
  53. $messagesIdToUpdate[]=$messages[$i]['message_id'];
  54. }
  55. markReadMessages($messagesIdToUpdate);
  56. return $ret;
  57. }
  58. function friendList() {
  59. // Accesing the global object inside the local scope
  60. global $UserManagerObjAjax;
  61. $userId = $_SESSION['user_id'];
  62. $sqlQuery = "SELECT u.user_id userId, u.name userName, ufl.friend_status status, ups.status connectionStatus "
  63. ."FROM ". TABLEPREFIX ."_user_phono_session ups, ". TABLEPREFIX ."_user u, ". TABLEPREFIX ."_user_friends_list ufl "
  64. ."WHERE ufl.nk_user_id_friend=u.user_id AND ups.nk_user_id=u.user_id AND ufl.nk_user_id_owner=". $userId;
  65. $friendsList = $UserManagerObjAjax->GetRecords("All",$sqlQuery);
  66. $ret=array();
  67. for($i=0;$i<count($friendsList);$i++) {
  68. $ret[$friendsList[$i]["userId"]]=array(
  69. "userId" =>$friendsList[$i]["userId"],
  70. "userName" =>$friendsList[$i]["userName"],
  71. "status" =>$friendsList[$i]["status"],
  72. "connectionStatus" =>$friendsList[$i]["connectionStatus"],
  73. );
  74. }
  75. return $ret;
  76. }
  77. function connect($sId) {
  78. // Accesing the global object inside the local scope
  79. global $UserManagerObjAjax;
  80. $userId = $_SESSION['user_id'];
  81. $db = new DBConnection();
  82. $db->getConnection();
  83. $sqlQuery = "SELECT * FROM ". TABLEPREFIX ."_user_phono_session WHERE nk_user_id=". $userId;
  84. $handle = mysql_query($sqlQuery);
  85. $record = mysql_fetch_row($handle);
  86. if ($record=="") {
  87. $sqlQuery = "INSERT ". TABLEPREFIX ."_user_phono_session(nk_user_id, phono_session_id, status) values (". $userId .", '". $sId ."', 1)";
  88. } else {
  89. $sqlQuery = "UPDATE ". TABLEPREFIX ."_user_phono_session SET phono_session_id='". $sId ."', status=1 WHERE nk_user_id=". $userId;
  90. }
  91. mysql_query($sqlQuery);
  92. $sqlQuery = "SELECT ups.phono_session_id sessionId, u.name FROM ". TABLEPREFIX ."_user_phono_session ups, ". TABLEPREFIX ."_user u "
  93. ."WHERE u.user_id = ups.nk_user_id AND ups.nk_user_id=". $userId;
  94. $handle = mysql_query($sqlQuery);
  95. $record = mysql_fetch_object($handle);
  96. $ret['myInfo'] = array(
  97. "myUserId" => $userId,
  98. "myName" => $record->name,
  99. "mySessionId" => $record->sessionId,
  100. "friendsList" => friendList(),
  101. "messagesPending" => messagesPending(),
  102. );
  103. return $ret;
  104. }
  105. function toSessionInfo($toUserId) {
  106. // Accesing the global object inside the local scope
  107. global $UserManagerObjAjax;
  108. $db = new DBConnection();
  109. $db->getConnection();
  110. $sqlQuery = "SELECT ups.phono_session_id sessionId, u.name FROM ". TABLEPREFIX ."_user_phono_session ups, ". TABLEPREFIX ."_user u "
  111. ."WHERE u.user_id = ups.nk_user_id AND ups.nk_user_id=". $toUserId;
  112. $handle = mysql_query($sqlQuery);
  113. if (mysql_num_rows($handle) > 0) {
  114. $record = mysql_fetch_object($handle);
  115. $sessionId = $record->sessionId;
  116. $toName = $record->name;
  117. }
  118. $ret['chatSession'] = array(
  119. 'sessionId' => $sessionId,
  120. 'toName' => $toName,
  121. 'toUserId' => $toUserId,
  122. );
  123. return $ret;
  124. }
  125. function registerMessageTo($chatSession, $msgText) {
  126. // Accesing the global object inside the local scope
  127. global $UserManagerObjAjax;
  128. $userId = $_SESSION['user_id'];
  129. $table_name = TABLEPREFIX."_message ";
  130. $dateAdded = date('Y-m-d H:i:s');
  131. $fields_values = array(
  132. 'user_id_to' => $chatSession['toUserId'],
  133. 'user_id_from' => $userId,
  134. 'reply_of_msg' => "-1", // THE -1 will indicate the chat type messages.
  135. 'message_text' => "'$msgText'",
  136. 'date_added' => "'$dateAdded'"
  137. );
  138. $values=array();
  139. $keys=array();
  140. foreach($fields_values as $key=>$value) {
  141. $keys[]=$key;
  142. $values[]=$value;
  143. }
  144. $sqlQuery = "INSERT INTO ". TABLEPREFIX ."_message(". implode(",", $keys) .") VALUES (". implode(",", $values) .")";
  145. $msgreport = $UserManagerObjAjax->Execute($sqlQuery);
  146. $message_id=mysql_insert_id();
  147. return $message_id;
  148. }
  149. $method = getPostOrGetParam("method");
  150. if($method != ""){
  151. header('Content-type:text/javascript;charset=UTF-8');
  152. switch ($method) {
  153. case "connect":
  154. $sId = getPostOrGetParam("sId");
  155. $ret = connect($sId);
  156. break;
  157. case "refreshSession":
  158. break;
  159. case "chatSessionInfo":
  160. $toUserId = getPostOrGetParam("uId");
  161. $ret = toSessionInfo($toUserId);
  162. break;
  163. case "outbound":
  164. $chatSession = getPostOrGetParam("toChatSession");
  165. $msgText = getPostOrGetParam("message");
  166. $ret = isset($chatSession['toUserId']) ? toSessionInfo($chatSession['toUserId']) : toSessionInfo($chatSession['fromUserId']);
  167. $ret['messageIdGen'] = registerMessageTo($chatSession, $msgText);
  168. break;
  169. case "inbound":
  170. $fromUserId = getPostOrGetParam("fromUserId");
  171. $messageToMark = getPostOrGetParam("messageIdGen");
  172. markReadMessages($messageToMark, $fromUserId);
  173. break;
  174. }
  175. }
  176. echo json_encode($ret);
  177. ?>