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.
 
 
 
 
 
 

291 lines
7.7 KiB

  1. <?php
  2. require_once("boot.php");
  3. function poller_run(&$argv, &$argc){
  4. global $a, $db;
  5. if(is_null($a)) {
  6. $a = new App;
  7. }
  8. if(is_null($db)) {
  9. @include(".htconfig.php");
  10. require_once("include/dba.php");
  11. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  12. unset($db_host, $db_user, $db_pass, $db_data);
  13. };
  14. require_once('include/session.php');
  15. require_once('include/datetime.php');
  16. require_once('library/simplepie/simplepie.inc');
  17. require_once('include/items.php');
  18. require_once('include/Contact.php');
  19. require_once('include/email.php');
  20. require_once('include/socgraph.php');
  21. require_once('include/pidfile.php');
  22. load_config('config');
  23. load_config('system');
  24. $maxsysload = intval(get_config('system','maxloadavg'));
  25. if($maxsysload < 1)
  26. $maxsysload = 50;
  27. if(function_exists('sys_getloadavg')) {
  28. $load = sys_getloadavg();
  29. if(intval($load[0]) > $maxsysload) {
  30. logger('system: load ' . $load . ' too high. Poller deferred to next scheduled run.');
  31. return;
  32. }
  33. }
  34. $lockpath = get_lockpath();
  35. if ($lockpath != '') {
  36. $pidfile = new pidfile($lockpath, 'poller');
  37. if($pidfile->is_already_running()) {
  38. logger("poller: Already running");
  39. if ($pidfile->running_time() > 9*60) {
  40. $pidfile->kill();
  41. logger("poller: killed stale process");
  42. // Calling a new instance
  43. proc_run('php','include/poller.php');
  44. }
  45. exit;
  46. }
  47. }
  48. $a->set_baseurl(get_config('system','url'));
  49. load_hooks();
  50. logger('poller: start');
  51. // run queue delivery process in the background
  52. proc_run('php',"include/queue.php");
  53. // run diaspora photo queue process in the background
  54. proc_run('php',"include/dsprphotoq.php");
  55. // expire any expired accounts
  56. q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
  57. AND `account_expires_on` != '0000-00-00 00:00:00'
  58. AND `account_expires_on` < UTC_TIMESTAMP() ");
  59. // delete user and contact records for recently removed accounts
  60. $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  61. if ($r) {
  62. foreach($r as $user) {
  63. q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
  64. q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
  65. }
  66. }
  67. $abandon_days = intval(get_config('system','account_abandon_days'));
  68. if($abandon_days < 1)
  69. $abandon_days = 0;
  70. // Check OStatus conversations
  71. check_conversations();
  72. // To-Do: Regenerate usage statistics
  73. // q("ANALYZE TABLE `item`");
  74. // once daily run birthday_updates and then expire in background
  75. $d1 = get_config('system','last_expire_day');
  76. $d2 = intval(datetime_convert('UTC','UTC','now','d'));
  77. if($d2 != intval($d1)) {
  78. update_contact_birthdays();
  79. update_suggestions();
  80. set_config('system','last_expire_day',$d2);
  81. proc_run('php','include/expire.php');
  82. }
  83. $last = get_config('system','cache_last_cleared');
  84. if($last) {
  85. $next = $last + (3600); // Once per hour
  86. $clear_cache = ($next <= time());
  87. } else
  88. $clear_cache = true;
  89. if ($clear_cache) {
  90. // clear old cache
  91. Cache::clear();
  92. // clear old item cache files
  93. clear_cache();
  94. // clear cache for photos
  95. clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
  96. // clear smarty cache
  97. clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
  98. set_config('system','cache_last_cleared', time());
  99. }
  100. $manual_id = 0;
  101. $generation = 0;
  102. $hub_update = false;
  103. $force = false;
  104. $restart = false;
  105. if(($argc > 1) && ($argv[1] == 'force'))
  106. $force = true;
  107. if(($argc > 1) && ($argv[1] == 'restart')) {
  108. $restart = true;
  109. $generation = intval($argv[2]);
  110. if(! $generation)
  111. killme();
  112. }
  113. if(($argc > 1) && intval($argv[1])) {
  114. $manual_id = intval($argv[1]);
  115. $force = true;
  116. }
  117. $interval = intval(get_config('system','poll_interval'));
  118. if(! $interval)
  119. $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
  120. $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
  121. reload_plugins();
  122. $d = datetime_convert();
  123. if(! $restart)
  124. proc_run('php','include/cronhooks.php');
  125. // Only poll from those with suitable relationships,
  126. // and which have a polling address and ignore Diaspora since
  127. // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
  128. $abandon_sql = (($abandon_days)
  129. ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
  130. : ''
  131. );
  132. $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
  133. WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
  134. AND NOT `network` IN ( '%s', '%s', '%s' )
  135. $sql_extra
  136. AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
  137. AND `contact`.`archive` = 0
  138. AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $abandon_sql ORDER BY RAND()",
  139. intval(CONTACT_IS_SHARING),
  140. intval(CONTACT_IS_FRIEND),
  141. dbesc(NETWORK_DIASPORA),
  142. dbesc(NETWORK_FACEBOOK),
  143. dbesc(NETWORK_PUMPIO)
  144. );
  145. if(! count($contacts)) {
  146. return;
  147. }
  148. foreach($contacts as $c) {
  149. $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  150. intval($c['id'])
  151. );
  152. if((! $res) || (! count($res)))
  153. continue;
  154. foreach($res as $contact) {
  155. $xml = false;
  156. if($manual_id)
  157. $contact['last-update'] = '0000-00-00 00:00:00';
  158. if($contact['network'] === NETWORK_DFRN)
  159. $contact['priority'] = 2;
  160. if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
  161. $contact['priority'] = 2;
  162. if($contact['priority'] || $contact['subhub']) {
  163. $hub_update = true;
  164. $update = false;
  165. $t = $contact['last-update'];
  166. // We should be getting everything via a hub. But just to be sure, let's check once a day.
  167. // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
  168. // This also lets us update our subscription to the hub, and add or replace hubs in case it
  169. // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
  170. if($contact['subhub']) {
  171. $poll_interval = get_config('system','pushpoll_frequency');
  172. $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
  173. $hub_update = false;
  174. if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
  175. $hub_update = true;
  176. }
  177. else
  178. $hub_update = false;
  179. /**
  180. * Based on $contact['priority'], should we poll this site now? Or later?
  181. */
  182. switch ($contact['priority']) {
  183. case 5:
  184. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
  185. $update = true;
  186. break;
  187. case 4:
  188. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
  189. $update = true;
  190. break;
  191. case 3:
  192. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
  193. $update = true;
  194. break;
  195. case 2:
  196. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
  197. $update = true;
  198. break;
  199. case 1:
  200. default:
  201. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
  202. $update = true;
  203. break;
  204. }
  205. if((! $update) && (! $force))
  206. continue;
  207. }
  208. proc_run('php','include/onepoll.php',$contact['id']);
  209. if($interval)
  210. @time_sleep_until(microtime(true) + (float) $interval);
  211. }
  212. }
  213. return;
  214. }
  215. if (array_search(__file__,get_included_files())===0){
  216. poller_run($argv,$argc);
  217. killme();
  218. }