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.

181 lines
4.9 KiB

10 years ago
10 years ago
10 years ago
  1. <?php
  2. use Friendica\Core\Config;
  3. require_once('include/queue_fn.php');
  4. require_once('include/dfrn.php');
  5. require_once("include/datetime.php");
  6. require_once('include/items.php');
  7. require_once('include/bbcode.php');
  8. require_once('include/socgraph.php');
  9. require_once('include/cache.php');
  10. function queue_run(&$argv, &$argc){
  11. global $a;
  12. if ($argc > 1) {
  13. $queue_id = intval($argv[1]);
  14. } else {
  15. $queue_id = 0;
  16. }
  17. $cachekey_deadguy = 'queue_run:deadguy:';
  18. $cachekey_server = 'queue_run:server:';
  19. if (!$queue_id) {
  20. logger('queue: start');
  21. // Handling the pubsubhubbub requests
  22. proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'include/pubsubpublish.php');
  23. $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
  24. INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
  25. WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  26. if (dbm::is_result($r)) {
  27. foreach ($r as $rr) {
  28. logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
  29. logger('Expired queue data: ' . $rr['content'], LOGGER_DATA);
  30. }
  31. q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  32. }
  33. // For the first 12 hours we'll try to deliver every 15 minutes
  34. // After that, we'll only attempt delivery once per hour.
  35. $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`");
  36. call_hooks('queue_predeliver', $a, $r);
  37. if (dbm::is_result($r)) {
  38. foreach ($r as $q_item) {
  39. logger('Call queue for id '.$q_item['id']);
  40. proc_run(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "include/queue.php", $q_item['id']);
  41. }
  42. }
  43. return;
  44. }
  45. // delivering
  46. require_once('include/salmon.php');
  47. require_once('include/diaspora.php');
  48. $r = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
  49. intval($queue_id));
  50. if (!dbm::is_result($r)) {
  51. return;
  52. }
  53. $q_item = $r[0];
  54. $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  55. intval($q_item['cid'])
  56. );
  57. if (!dbm::is_result($c)) {
  58. remove_queue_item($q_item['id']);
  59. return;
  60. }
  61. $dead = Cache::get($cachekey_deadguy.$c[0]['notify']);
  62. if (!is_null($dead) AND $dead) {
  63. logger('queue: skipping known dead url: '.$c[0]['notify']);
  64. update_queue_time($q_item['id']);
  65. return;
  66. }
  67. $server = poco_detect_server($c[0]['url']);
  68. if ($server != "") {
  69. $vital = Cache::get($cachekey_server.$server);
  70. if (is_null($vital)) {
  71. logger("Check server ".$server." (".$c[0]["network"].")");
  72. $vital = poco_check_server($server, $c[0]["network"], true);
  73. Cache::set($cachekey_server.$server, $vital, CACHE_QUARTER_HOUR);
  74. }
  75. if (!is_null($vital) AND !$vital) {
  76. logger('queue: skipping dead server: '.$server);
  77. update_queue_time($q_item['id']);
  78. return;
  79. }
  80. }
  81. $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
  82. FROM `user` WHERE `uid` = %d LIMIT 1",
  83. intval($c[0]['uid'])
  84. );
  85. if (!dbm::is_result($u)) {
  86. remove_queue_item($q_item['id']);
  87. return;
  88. }
  89. $data = $q_item['content'];
  90. $public = $q_item['batch'];
  91. $contact = $c[0];
  92. $owner = $u[0];
  93. $deliver_status = 0;
  94. switch ($contact['network']) {
  95. case NETWORK_DFRN:
  96. logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
  97. $deliver_status = dfrn::deliver($owner, $contact, $data);
  98. if ($deliver_status == (-1)) {
  99. update_queue_time($q_item['id']);
  100. Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
  101. } else {
  102. remove_queue_item($q_item['id']);
  103. }
  104. break;
  105. case NETWORK_OSTATUS:
  106. if ($contact['notify']) {
  107. logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
  108. $deliver_status = slapper($owner, $contact['notify'], $data);
  109. if ($deliver_status == (-1)) {
  110. update_queue_time($q_item['id']);
  111. Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
  112. } else {
  113. remove_queue_item($q_item['id']);
  114. }
  115. }
  116. break;
  117. case NETWORK_DIASPORA:
  118. if ($contact['notify']) {
  119. logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
  120. $deliver_status = Diaspora::transmit($owner, $contact, $data, $public, true);
  121. if ($deliver_status == (-1)) {
  122. update_queue_time($q_item['id']);
  123. Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
  124. } else {
  125. remove_queue_item($q_item['id']);
  126. }
  127. }
  128. break;
  129. default:
  130. $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
  131. call_hooks('queue_deliver', $a, $params);
  132. if ($params['result']) {
  133. remove_queue_item($q_item['id']);
  134. } else {
  135. update_queue_time($q_item['id']);
  136. }
  137. break;
  138. }
  139. logger('Deliver status '.(int)$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
  140. return;
  141. }