Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
6.2KB

  1. <?php
  2. require_once("boot.php");
  3. require_once('include/queue_fn.php');
  4. require_once('include/dfrn.php');
  5. function queue_run(&$argv, &$argc){
  6. global $a, $db;
  7. if(is_null($a)){
  8. $a = new App;
  9. }
  10. if(is_null($db)){
  11. @include(".htconfig.php");
  12. require_once("include/dba.php");
  13. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  14. unset($db_host, $db_user, $db_pass, $db_data);
  15. };
  16. require_once("include/session.php");
  17. require_once("include/datetime.php");
  18. require_once('include/items.php');
  19. require_once('include/bbcode.php');
  20. require_once('include/socgraph.php');
  21. load_config('config');
  22. load_config('system');
  23. // Don't check this stuff if the function is called by the poller
  24. if (App::callstack() != "poller_run")
  25. if (App::is_already_running('queue', 'include/queue.php', 540))
  26. return;
  27. $a->set_baseurl(get_config('system','url'));
  28. load_hooks();
  29. if($argc > 1)
  30. $queue_id = intval($argv[1]);
  31. else
  32. $queue_id = 0;
  33. $deadguys = array();
  34. $deadservers = array();
  35. $serverlist = array();
  36. if (!$queue_id) {
  37. logger('queue: start');
  38. // Handling the pubsubhubbub requests
  39. proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
  40. $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
  41. // If we are using the worker we don't need a delivery interval
  42. if (get_config("system", "worker"))
  43. $interval = false;
  44. $r = q("select * from deliverq where 1");
  45. if($r) {
  46. foreach($r as $rr) {
  47. logger('queue: deliverq');
  48. proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
  49. if($interval)
  50. @time_sleep_until(microtime(true) + (float) $interval);
  51. }
  52. }
  53. $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
  54. INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
  55. WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  56. if($r) {
  57. foreach($r as $rr) {
  58. logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
  59. logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
  60. }
  61. q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  62. }
  63. // For the first 12 hours we'll try to deliver every 15 minutes
  64. // After that, we'll only attempt delivery once per hour.
  65. $r = q("SELECT `id` FROM `queue` WHERE ((`created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR && `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE) OR (`last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR)) ORDER BY `cid`, `created`");
  66. } else {
  67. logger('queue: start for id '.$queue_id);
  68. $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
  69. intval($queue_id)
  70. );
  71. }
  72. if (!$r){
  73. return;
  74. }
  75. if (!$queue_id)
  76. call_hooks('queue_predeliver', $a, $r);
  77. // delivery loop
  78. require_once('include/salmon.php');
  79. require_once('include/diaspora.php');
  80. foreach($r as $q_item) {
  81. // queue_predeliver hooks may have changed the queue db details,
  82. // so check again if this entry still needs processing
  83. if($queue_id)
  84. $qi = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
  85. intval($queue_id));
  86. elseif (get_config("system", "worker")) {
  87. logger('Call queue for id '.$q_item['id']);
  88. proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
  89. continue;
  90. } else
  91. $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
  92. intval($q_item['id']));
  93. if(! count($qi))
  94. continue;
  95. $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  96. intval($qi[0]['cid'])
  97. );
  98. if(! count($c)) {
  99. remove_queue_item($q_item['id']);
  100. continue;
  101. }
  102. if(in_array($c[0]['notify'],$deadguys)) {
  103. logger('queue: skipping known dead url: ' . $c[0]['notify']);
  104. update_queue_time($q_item['id']);
  105. continue;
  106. }
  107. $server = poco_detect_server($c[0]['url']);
  108. if (($server != "") AND !in_array($server, $serverlist)) {
  109. logger("Check server ".$server." (".$c[0]["network"].")");
  110. if (!poco_check_server($server, $c[0]["network"], true))
  111. $deadservers[] = $server;
  112. $serverlist[] = $server;
  113. }
  114. if (($server != "") AND in_array($server, $deadservers)) {
  115. logger('queue: skipping known dead server: '.$server);
  116. update_queue_time($q_item['id']);
  117. continue;
  118. }
  119. $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
  120. FROM `user` WHERE `uid` = %d LIMIT 1",
  121. intval($c[0]['uid'])
  122. );
  123. if(! count($u)) {
  124. remove_queue_item($q_item['id']);
  125. continue;
  126. }
  127. $data = $qi[0]['content'];
  128. $public = $qi[0]['batch'];
  129. $contact = $c[0];
  130. $owner = $u[0];
  131. $deliver_status = 0;
  132. switch($contact['network']) {
  133. case NETWORK_DFRN:
  134. logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
  135. $deliver_status = dfrn::deliver($owner,$contact,$data);
  136. if($deliver_status == (-1)) {
  137. update_queue_time($q_item['id']);
  138. $deadguys[] = $contact['notify'];
  139. } else
  140. remove_queue_item($q_item['id']);
  141. break;
  142. case NETWORK_OSTATUS:
  143. if($contact['notify']) {
  144. logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
  145. $deliver_status = slapper($owner,$contact['notify'],$data);
  146. if($deliver_status == (-1)) {
  147. update_queue_time($q_item['id']);
  148. $deadguys[] = $contact['notify'];
  149. } else
  150. remove_queue_item($q_item['id']);
  151. }
  152. break;
  153. case NETWORK_DIASPORA:
  154. if($contact['notify']) {
  155. logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
  156. $deliver_status = diaspora::transmit($owner,$contact,$data,$public,true);
  157. if($deliver_status == (-1)) {
  158. update_queue_time($q_item['id']);
  159. $deadguys[] = $contact['notify'];
  160. } else
  161. remove_queue_item($q_item['id']);
  162. }
  163. break;
  164. default:
  165. $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
  166. call_hooks('queue_deliver', $a, $params);
  167. if($params['result'])
  168. remove_queue_item($q_item['id']);
  169. else
  170. update_queue_time($q_item['id']);
  171. break;
  172. }
  173. logger('Deliver status '.$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
  174. }
  175. return;
  176. }
  177. if (array_search(__file__,get_included_files())===0){
  178. queue_run($_SERVER["argv"],$_SERVER["argc"]);
  179. killme();
  180. }