PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/test/cometchat/cometchat_receive.php

https://github.com/chacha13/runningmate
PHP | 274 lines | 167 code | 55 blank | 52 comment | 95 complexity | a9346c769fb6919afedfc34d088b0f07 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. CometChat
  4. Copyright (c) 2009 Inscripts
  5. CometChat ('the Software') is a copyrighted work of authorship. Inscripts
  6. retains ownership of the Software and any copies of it, regardless of the
  7. form in which the copies may exist. This license is not a sale of the
  8. original Software or any copies.
  9. By installing and using CometChat on your server, you agree to the following
  10. terms and conditions. Such agreement is either on your own behalf or on behalf
  11. of any corporate entity which employs you or which you represent
  12. ('Corporate Licensee'). In this Agreement, 'you' includes both the reader
  13. and any Corporate Licensee and 'Inscripts' means Inscripts (I) Private Limited:
  14. CometChat license grants you the right to run one instance (a single installation)
  15. of the Software on one web server and one web site for each license purchased.
  16. Each license may power one instance of the Software on one domain. For each
  17. installed instance of the Software, a separate license is required.
  18. The Software is licensed only to you. You may not rent, lease, sublicense, sell,
  19. assign, pledge, transfer or otherwise dispose of the Software in any form, on
  20. a temporary or permanent basis, without the prior written consent of Inscripts.
  21. The license is effective until terminated. You may terminate it
  22. at any time by uninstalling the Software and destroying any copies in any form.
  23. The Software source code may be altered (at your risk)
  24. All Software copyright notices within the scripts must remain unchanged (and visible).
  25. The Software may not be used for anything that would represent or is associated
  26. with an Intellectual Property violation, including, but not limited to,
  27. engaging in any activity that infringes or misappropriates the intellectual property
  28. rights of others, including copyrights, trademarks, service marks, trade secrets,
  29. software piracy, and patents held by individuals, corporations, or other entities.
  30. If any of the terms of this Agreement are violated, Inscripts reserves the right
  31. to revoke the Software license at any time.
  32. The above copyright notice and this permission notice shall be included in
  33. all copies or substantial portions of the Software.
  34. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. THE SOFTWARE.
  41. */
  42. include_once "cometchat_init.php";
  43. $response = array();
  44. $messages = array();
  45. if ($userid != 0) {
  46. if (!empty($_POST['chatbox'])) {
  47. if (!empty($_SESSION['cometchat_user_'.$_POST['chatbox']])) {
  48. $messages = $_SESSION['cometchat_user_'.$_POST['chatbox']];
  49. }
  50. } else {
  51. if (!empty($_POST['buddylist']) && $_POST['buddylist'] == 1) { getBuddyList(); }
  52. if (!empty($_POST['initialize']) && $_POST['initialize'] == 1) {
  53. $sql = ("select id from cometchat order by id desc limit 1");
  54. $query = mysql_query($sql);
  55. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  56. $result = mysql_fetch_array($query);
  57. $_SESSION['timedifference'] = round((($_POST['currenttime']-getTimeStamp())/60)/30)*60*30;
  58. $response['initialize'] = $result['id'];
  59. getStatus();
  60. if (!empty($_COOKIE[$cookiePrefix.'state'])) {
  61. $states = explode(':',urldecode($_COOKIE[$cookiePrefix.'state']));
  62. $openChatboxId = '';
  63. if ($states[2] != '' && $states[2] != ' ') {
  64. $openChatboxId = $states[2];
  65. }
  66. if (!empty($openChatboxId) && !empty($_SESSION['cometchat_user_'.$openChatboxId])) {
  67. $messages = array_merge($messages,$_SESSION['cometchat_user_'.$openChatboxId]);
  68. }
  69. }
  70. }
  71. getLastTimestamp();
  72. fetchMessages();
  73. }
  74. $sql = updateLastActivity($userid);
  75. $query = mysql_query($sql);
  76. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  77. } else {
  78. $response['loggedout'] = '1';
  79. if (DO_NOT_DESTROY_SESSION != 1) {
  80. session_destroy();
  81. }
  82. }
  83. function getStatus() {
  84. global $response;
  85. global $userid;
  86. global $status;
  87. $sql = getUserStatus($userid);
  88. $query = mysql_query($sql);
  89. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  90. $chat = mysql_fetch_array($query);
  91. if (empty($chat['status'])) {
  92. $chat['status'] = 'available';
  93. } else {
  94. if ($chat['status'] == 'offline') {
  95. $_SESSION['cometchat_sessionvars']['buddylist'] = 0;
  96. }
  97. }
  98. if (empty($chat['message'])) {
  99. $chat['message'] = $status[$chat['status']];
  100. }
  101. $status = array('message' => $chat['message'], 'status' => $chat['status']);
  102. $response['userstatus'] = $status;
  103. }
  104. function getLastTimestamp() {
  105. if (empty($_POST['timestamp'])) {
  106. $_POST['timestamp'] = 0;
  107. }
  108. if ($_POST['timestamp'] == 0) {
  109. foreach ($_SESSION as $key => $value) {
  110. if (substr($key,0,15) == "cometchat_user_") {
  111. $temp = end($_SESSION[$key]);
  112. if ($_POST['timestamp'] < $temp['id']) {
  113. $_POST['timestamp'] = $temp['id'];
  114. }
  115. }
  116. }
  117. if ($_POST['timestamp'] == 0) {
  118. $sql = ("select id from cometchat order by id desc limit 1");
  119. $query = mysql_query($sql);
  120. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  121. $chat = mysql_fetch_array($query);
  122. $_POST['timestamp'] = $chat['id'];
  123. }
  124. }
  125. }
  126. function getBuddyList() {
  127. global $response;
  128. global $userid;
  129. global $db;
  130. global $status;
  131. $time = getTimeStamp();
  132. $buddyList = array();
  133. if ((empty($_SESSION['cometchat_buddytime'])) || ($_POST['initialize'] == 1) || (!empty($_SESSION['cometchat_buddytime']) && ($time-$_SESSION['cometchat_buddytime'] > REFRESH_BUDDYLIST))) {
  134. $sql = getFriendsList($userid,$time);
  135. $query = mysql_query($sql);
  136. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  137. while ($chat = mysql_fetch_array($query)) {
  138. $Online = 'offline';
  139. $onlinesql = getOnlineList($chat['user_id']);
  140. $onlinequery = mysql_query($onlinesql);
  141. while ($isonline = mysql_fetch_array($onlinequery)) {
  142. if ($chat['user_id'] == $isonline['visitor_user_id']){
  143. $Online = 'online';}
  144. }
  145. if ((($time-$chat[DB_USERTABLE_LASTACTIVITY]) < ONLINE_TIMEOUT) && $chat['status'] != 'invisible' && $chat['status'] != 'offline' && $Online != 'offline') {
  146. if ($chat['status'] != 'busy') {
  147. $chat['status'] = 'available';
  148. }
  149. } else {
  150. $chat['status'] = 'offline';
  151. }
  152. if ($chat['status'] != 'offline') {
  153. if ($chat['message'] == null) {
  154. $chat['message'] = $status[$chat['status']];
  155. }
  156. $link = getLink($chat[DB_USERTABLE_LINK]);
  157. $avatar = getAvatar($chat[DB_USERTABLE_USERID],$chat[DB_USERTABLE_AVATAR]);
  158. if (!empty($chat[DB_USERTABLE_NAME])) {
  159. $buddyList[] = array('id' => $chat[DB_USERTABLE_USERID], 'n' => $chat[DB_USERTABLE_NAME], 's' => $chat['status'], 'm' => $chat['message'], 'a' => $avatar, 'l' => $link);
  160. }
  161. }
  162. }
  163. if (function_exists('hooks_forcefriends') && is_array(hooks_forcefriends())) {
  164. $buddyList = array_merge(hooks_forcefriends(),$buddyList);
  165. }
  166. $_SESSION['cometchat_buddytime'] = $time;
  167. $response['buddylist'] = $buddyList;
  168. }
  169. }
  170. function fetchMessages() {
  171. global $response;
  172. global $userid;
  173. global $db;
  174. global $messages;
  175. $timestamp = 0;
  176. $sql = ("select cometchat.id, cometchat.from, cometchat.to, cometchat.message, cometchat.sent, cometchat.read from cometchat where ((cometchat.to = '".mysql_real_escape_string($userid)."' and cometchat.direction <> 2) or (cometchat.from = '".mysql_real_escape_string($userid)."' and cometchat.direction <> 1)) and (cometchat.id > '".mysql_real_escape_string($_POST['timestamp'])."' or (cometchat.to = '".mysql_real_escape_string($userid)."' and cometchat.read != 1)) order by cometchat.id");
  177. $query = mysql_query($sql);
  178. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  179. while ($chat = mysql_fetch_array($query)) {
  180. $self = 0;
  181. $old = 0;
  182. if ($chat['from'] == $userid) {
  183. $chat['from'] = $chat['to'];
  184. $self = 1;
  185. $old = 1;
  186. }
  187. if ($chat['read'] == 1) {
  188. $old = 1;
  189. }
  190. $messages[] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => $self, 'old' => $old, 'sent' => ($chat['sent']+$_SESSION['timedifference']));
  191. if ($self == 0 && $old == 0 && $chat['read'] != 1) {
  192. $_SESSION['cometchat_user_'.$chat['from']][] = array('id' => $chat['id'], 'from' => $chat['from'], 'message' => $chat['message'], 'self' => 0, 'old' => 1, 'sent' => ($chat['sent']+$_SESSION['timedifference']));
  193. }
  194. $timestamp = $chat['id'];
  195. }
  196. if (!empty($messages)) {
  197. $sql = ("update cometchat set cometchat.read = '1' where cometchat.to = '".mysql_real_escape_string($userid)."' and cometchat.id <= '".mysql_real_escape_string($timestamp)."'");
  198. $query = mysql_query($sql);
  199. if (defined('DEV_MODE') && DEV_MODE == '1') { echo mysql_error(); }
  200. }
  201. }
  202. if (!empty($messages)) {
  203. $response['messages'] = $messages;
  204. }
  205. header('Content-type: application/json; charset=utf-8');
  206. echo json_encode($response);
  207. exit;