PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/scheduler/tasks/jabbercheck/index.php

https://github.com/lux/sitellite
PHP | 158 lines | 110 code | 32 blank | 16 comment | 19 complexity | 6d1ef49916871fad69821e8e2236328f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. // BEGIN CLI KEEPOUT CHECKING
  3. if (php_sapi_name () !== 'cli') {
  4. // Add these lines to the very top of any file you don't want people to
  5. // be able to access directly.
  6. header ('HTTP/1.1 404 Not Found');
  7. echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  8. . "<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n"
  9. . "The requested URL " . $PHP_SELF . " was not found on this server.<p>\n<hr>\n"
  10. . $_SERVER['SERVER_SIGNATURE'] . "</body></html>";
  11. exit;
  12. }
  13. // END CLI KEEPOUT CHECKING
  14. // jabbercheck scheduler block
  15. // Checks for new incoming jabber instant messages.
  16. global $conf;
  17. if (empty ($conf['Messaging']['jabber_server'])) {
  18. return;
  19. }
  20. loader_import ('ext.Jabber');
  21. loader_import ('cms.Workspace.Message');
  22. //loader_import ('cms.Workspace.Task');
  23. $m = new WorkspaceMessage ();
  24. //$wtsk = new WorkspaceTask ();
  25. $j = new Jabber;
  26. $j->resource = 'Sitellite CMS ' . SITELLITE_VERSION;
  27. $j->server = $conf['Messaging']['jabber_server'];
  28. $j->port = $conf['Messaging']['jabber_port'];
  29. $j->username = $conf['Messaging']['jabber_username'];
  30. $j->password = $conf['Messaging']['jabber_password'];
  31. $j->enable_logging = true;
  32. if (! $j->Connect ()) {
  33. echo $j->log_array[count ($j->log_array) - 1] . NEWLINE;
  34. return;
  35. }
  36. if (! $j->SendAuth ()) {
  37. echo $j->log_array[count ($j->log_array) - 1] . NEWLINE;
  38. return;
  39. }
  40. $j->SendPresence ('available');
  41. sleep (2);
  42. $j->Listen ();
  43. foreach ($j->packet_queue as $message) {
  44. set_time_limit (30);
  45. if (! array ($message) || key ($message) != 'message') {
  46. continue;
  47. }
  48. $thread = $j->GetInfoFromMessageThread ($message);
  49. if (! $thread) {
  50. continue;
  51. }
  52. // now figure out:
  53. // - type of message (M, T, or C), for now handle only M
  54. switch (substr ($thread, 0, 1)) {
  55. case 'M':
  56. $msg_id = str_replace ('M', '', $thread);
  57. break;
  58. case 'T':
  59. case 'C':
  60. default:
  61. continue;
  62. }
  63. // - if M{id} is valid
  64. $res = $m->get ($msg_id, false);
  65. if (! $res) {
  66. echo 'no such message (' . $msg_id . ')' . NEWLINE;
  67. info ($res, true);
  68. continue;
  69. }
  70. $from = $j->GetInfoFromMessageFrom ($message);
  71. $body = $j->GetInfoFromMessageBody ($message);
  72. $from = preg_replace ('/@([^\/]+)\/.*$/', '@\1', $from);
  73. // - recipient list based on original message
  74. $list = $m->getRecipients ($msg_id);
  75. $_list = array ($res->from_user);
  76. foreach ($list as $obj) {
  77. if (! $obj->user) {
  78. $_list[] = $obj->email;
  79. } else {
  80. $_list[] = $obj->user;
  81. }
  82. }
  83. $list = $_list;
  84. // - internal user based on sitellitem_forward settings
  85. $from_user = $m->getUserFromForward ('jabber', $from);
  86. if (! $from_user) {
  87. continue;
  88. } elseif (is_object ($from_user)) {
  89. $from_user = $from_user->user;
  90. if (! in_array ($from_user, $list)) {
  91. continue; // can't send if you didn't receive
  92. }
  93. } else {
  94. foreach ($from_user as $u) {
  95. if (in_array ($u->user, $list)) {
  96. $from_user = $u->user;
  97. break;
  98. }
  99. }
  100. }
  101. // remove self, add $res->from_user
  102. foreach ($list as $k => $v) {
  103. if ($v == $from_user) {
  104. unset ($list[$k]);
  105. }
  106. }
  107. $list = array_unique ($list);
  108. // - subject based on subject of M{id}
  109. $subject = Workspace::createResponseSubject ($res->subject);
  110. // send message
  111. $res = $m->send (
  112. $subject,
  113. $body,
  114. $list,
  115. array (),
  116. $msg_id,
  117. 'normal',
  118. $from_user
  119. );
  120. if (! $res) {
  121. echo $m->error . NEWLINE;
  122. }
  123. } // end foreach
  124. $j->enable_logging = false;
  125. $j->Disconnect ();
  126. ?>