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.
 
 
 
 
 
 

235 lines
6.3 KiB

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