PageRenderTime 65ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/include/queue.php

https://gitlab.com/sim6/friendica
PHP | 270 lines | 202 code | 57 blank | 11 comment | 36 complexity | ca0e9736173d414a8be6a419f4d7af65 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. require_once("boot.php");
  3. require_once('include/queue_fn.php');
  4. function handle_pubsubhubbub() {
  5. global $a, $db;
  6. logger('queue [pubsubhubbub]: start');
  7. // We'll push to each subscriber that has push > 0,
  8. // i.e. there has been an update (set in notifier.php).
  9. $r = q("SELECT * FROM `push_subscriber` WHERE `push` > 0");
  10. foreach($r as $rr) {
  11. $params = get_feed_for($a, '', $rr['nickname'], $rr['last_update']);
  12. $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
  13. $headers = array("Content-type: application/atom+xml",
  14. sprintf("Link: <%s>;rel=hub," .
  15. "<%s>;rel=self",
  16. $a->get_baseurl() . '/pubsubhubbub',
  17. $rr['topic']),
  18. "X-Hub-Signature: sha1=" . $hmac_sig);
  19. logger('queue [pubsubhubbub]: POST', $headers);
  20. post_url($rr['callback_url'], $params, $headers);
  21. $ret = $a->get_curl_code();
  22. if ($ret >= 200 && $ret <= 299) {
  23. logger('queue [pubsubhubbub]: successfully pushed to ' .
  24. $rr['callback_url']);
  25. // set last_update to "now", and reset push=0
  26. $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
  27. q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' " .
  28. "WHERE id = %d",
  29. dbesc($date_now),
  30. intval($rr['id']));
  31. } else {
  32. logger('queue [pubsubhubbub]: error when pushing to ' .
  33. $rr['callback_url'] . 'HTTP: ', $ret);
  34. // we use the push variable also as a counter, if we failed we
  35. // increment this until some upper limit where we give up
  36. $new_push = intval($rr['push']) + 1;
  37. if ($new_push > 30) // OK, let's give up
  38. $new_push = 0;
  39. q("UPDATE `push_subscriber` SET `push` = %d, last_update = '%s' " .
  40. "WHERE id = %d",
  41. $new_push,
  42. dbesc($date_now),
  43. intval($rr['id']));
  44. }
  45. }
  46. }
  47. function queue_run(&$argv, &$argc){
  48. global $a, $db;
  49. if(is_null($a)){
  50. $a = new App;
  51. }
  52. if(is_null($db)){
  53. @include(".htconfig.php");
  54. require_once("include/dba.php");
  55. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  56. unset($db_host, $db_user, $db_pass, $db_data);
  57. };
  58. require_once("include/session.php");
  59. require_once("include/datetime.php");
  60. require_once('include/items.php');
  61. require_once('include/bbcode.php');
  62. require_once('include/pidfile.php');
  63. load_config('config');
  64. load_config('system');
  65. $lockpath = get_config('system','lockpath');
  66. if ($lockpath != '') {
  67. $pidfile = new pidfile($lockpath, 'queue');
  68. if($pidfile->is_already_running()) {
  69. logger("queue: Already running");
  70. if ($pidfile->running_time() > 9*60) {
  71. $pidfile->kill();
  72. logger("queue: killed stale process");
  73. // Calling a new instance
  74. proc_run('php',"include/queue.php");
  75. }
  76. return;
  77. }
  78. }
  79. $a->set_baseurl(get_config('system','url'));
  80. load_hooks();
  81. if($argc > 1)
  82. $queue_id = intval($argv[1]);
  83. else
  84. $queue_id = 0;
  85. $deadguys = array();
  86. logger('queue: start');
  87. handle_pubsubhubbub();
  88. $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
  89. $r = q("select * from deliverq where 1");
  90. if($r) {
  91. foreach($r as $rr) {
  92. logger('queue: deliverq');
  93. proc_run('php','include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
  94. if($interval)
  95. @time_sleep_until(microtime(true) + (float) $interval);
  96. }
  97. }
  98. $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
  99. INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
  100. WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  101. if($r) {
  102. foreach($r as $rr) {
  103. logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
  104. logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
  105. }
  106. q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  107. }
  108. if($queue_id) {
  109. $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
  110. intval($queue_id)
  111. );
  112. }
  113. else {
  114. // For the first 12 hours we'll try to deliver every 15 minutes
  115. // After that, we'll only attempt delivery once per hour.
  116. $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 ))");
  117. }
  118. if(! $r){
  119. return;
  120. }
  121. if(! $queue_id)
  122. call_hooks('queue_predeliver', $a, $r);
  123. // delivery loop
  124. require_once('include/salmon.php');
  125. require_once('include/diaspora.php');
  126. foreach($r as $q_item) {
  127. // queue_predeliver hooks may have changed the queue db details,
  128. // so check again if this entry still needs processing
  129. if($queue_id) {
  130. $qi = q("select * from queue where `id` = %d limit 1",
  131. intval($queue_id)
  132. );
  133. }
  134. else {
  135. $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
  136. intval($q_item['id'])
  137. );
  138. }
  139. if(! count($qi))
  140. continue;
  141. $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  142. intval($qi[0]['cid'])
  143. );
  144. if(! count($c)) {
  145. remove_queue_item($q_item['id']);
  146. continue;
  147. }
  148. if(in_array($c[0]['notify'],$deadguys)) {
  149. logger('queue: skipping known dead url: ' . $c[0]['notify']);
  150. update_queue_time($q_item['id']);
  151. continue;
  152. }
  153. $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
  154. FROM `user` WHERE `uid` = %d LIMIT 1",
  155. intval($c[0]['uid'])
  156. );
  157. if(! count($u)) {
  158. remove_queue_item($q_item['id']);
  159. continue;
  160. }
  161. $data = $qi[0]['content'];
  162. $public = $qi[0]['batch'];
  163. $contact = $c[0];
  164. $owner = $u[0];
  165. $deliver_status = 0;
  166. switch($contact['network']) {
  167. case NETWORK_DFRN:
  168. logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
  169. $deliver_status = dfrn_deliver($owner,$contact,$data);
  170. if($deliver_status == (-1)) {
  171. update_queue_time($q_item['id']);
  172. $deadguys[] = $contact['notify'];
  173. }
  174. else {
  175. remove_queue_item($q_item['id']);
  176. }
  177. break;
  178. case NETWORK_OSTATUS:
  179. if($contact['notify']) {
  180. logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
  181. $deliver_status = slapper($owner,$contact['notify'],$data);
  182. if($deliver_status == (-1))
  183. update_queue_time($q_item['id']);
  184. else
  185. remove_queue_item($q_item['id']);
  186. }
  187. break;
  188. case NETWORK_DIASPORA:
  189. if($contact['notify']) {
  190. logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
  191. $deliver_status = diaspora_transmit($owner,$contact,$data,$public,true);
  192. if($deliver_status == (-1))
  193. update_queue_time($q_item['id']);
  194. else
  195. remove_queue_item($q_item['id']);
  196. }
  197. break;
  198. default:
  199. $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
  200. call_hooks('queue_deliver', $a, $params);
  201. if($params['result'])
  202. remove_queue_item($q_item['id']);
  203. else
  204. update_queue_time($q_item['id']);
  205. break;
  206. }
  207. }
  208. return;
  209. }
  210. if (array_search(__file__,get_included_files())===0){
  211. queue_run($argv,$argc);
  212. killme();
  213. }