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.
 
 
 
 
 
 

214 lines
5.3 KiB

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