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.
 
 
 
 
 
 

256 lines
6.5 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("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_config('system','lockpath');
  35. if ($lockpath != '') {
  36. $pidfile = new pidfile($lockpath, 'poller.lck');
  37. if($pidfile->is_already_running()) {
  38. logger("poller: Already running");
  39. exit;
  40. }
  41. }
  42. $a->set_baseurl(get_config('system','url'));
  43. load_hooks();
  44. logger('poller: start');
  45. // run queue delivery process in the background
  46. proc_run('php',"include/queue.php");
  47. // expire any expired accounts
  48. q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
  49. AND `account_expires_on` != '0000-00-00 00:00:00'
  50. AND `account_expires_on` < UTC_TIMESTAMP() ");
  51. $abandon_days = intval(get_config('system','account_abandon_days'));
  52. if($abandon_days < 1)
  53. $abandon_days = 0;
  54. // once daily run birthday_updates and then expire in background
  55. $d1 = get_config('system','last_expire_day');
  56. $d2 = intval(datetime_convert('UTC','UTC','now','d'));
  57. if($d2 != intval($d1)) {
  58. update_contact_birthdays();
  59. update_suggestions();
  60. set_config('system','last_expire_day',$d2);
  61. proc_run('php','include/expire.php');
  62. }
  63. // clear old cache
  64. Cache::clear();
  65. // clear item cache files if they are older than one day
  66. $cache = get_config('system','itemcache');
  67. if (($cache != '') and is_dir($cache)) {
  68. if ($dh = opendir($cache)) {
  69. while (($file = readdir($dh)) !== false) {
  70. $fullpath = $cache."/".$file;
  71. if ((filetype($fullpath) == "file") and filectime($fullpath) < (time() - 86400))
  72. unlink($fullpath);
  73. }
  74. closedir($dh);
  75. }
  76. }
  77. $manual_id = 0;
  78. $generation = 0;
  79. $hub_update = false;
  80. $force = false;
  81. $restart = false;
  82. if(($argc > 1) && ($argv[1] == 'force'))
  83. $force = true;
  84. if(($argc > 1) && ($argv[1] == 'restart')) {
  85. $restart = true;
  86. $generation = intval($argv[2]);
  87. if(! $generation)
  88. killme();
  89. }
  90. if(($argc > 1) && intval($argv[1])) {
  91. $manual_id = intval($argv[1]);
  92. $force = true;
  93. }
  94. $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
  95. $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
  96. reload_plugins();
  97. $d = datetime_convert();
  98. if(! $restart)
  99. proc_run('php','include/cronhooks.php');
  100. // Only poll from those with suitable relationships,
  101. // and which have a polling address and ignore Diaspora since
  102. // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
  103. $abandon_sql = (($abandon_days)
  104. ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
  105. : ''
  106. );
  107. $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
  108. WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
  109. AND NOT `network` IN ( '%s', '%s' )
  110. $sql_extra
  111. AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
  112. AND `contact`.`archive` = 0
  113. AND `user`.`account_expired` = 0 $abandon_sql ORDER BY RAND()",
  114. intval(CONTACT_IS_SHARING),
  115. intval(CONTACT_IS_FRIEND),
  116. dbesc(NETWORK_DIASPORA),
  117. dbesc(NETWORK_FACEBOOK)
  118. );
  119. if(! count($contacts)) {
  120. return;
  121. }
  122. foreach($contacts as $c) {
  123. $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  124. intval($c['id'])
  125. );
  126. if((! $res) || (! count($res)))
  127. continue;
  128. foreach($res as $contact) {
  129. $xml = false;
  130. if($manual_id)
  131. $contact['last-update'] = '0000-00-00 00:00:00';
  132. if($contact['network'] === NETWORK_DFRN)
  133. $contact['priority'] = 2;
  134. if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
  135. $contact['priority'] = 2;
  136. if($contact['priority'] || $contact['subhub']) {
  137. $hub_update = true;
  138. $update = false;
  139. $t = $contact['last-update'];
  140. // We should be getting everything via a hub. But just to be sure, let's check once a day.
  141. // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
  142. // This also lets us update our subscription to the hub, and add or replace hubs in case it
  143. // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
  144. if($contact['subhub']) {
  145. $interval = get_config('system','pushpoll_frequency');
  146. $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
  147. $hub_update = false;
  148. if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
  149. $hub_update = true;
  150. }
  151. else
  152. $hub_update = false;
  153. /**
  154. * Based on $contact['priority'], should we poll this site now? Or later?
  155. */
  156. switch ($contact['priority']) {
  157. case 5:
  158. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
  159. $update = true;
  160. break;
  161. case 4:
  162. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
  163. $update = true;
  164. break;
  165. case 3:
  166. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
  167. $update = true;
  168. break;
  169. case 2:
  170. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
  171. $update = true;
  172. break;
  173. case 1:
  174. default:
  175. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
  176. $update = true;
  177. break;
  178. }
  179. if((! $update) && (! $force))
  180. continue;
  181. }
  182. proc_run('php','include/onepoll.php',$contact['id']);
  183. if($interval)
  184. @time_sleep_until(microtime(true) + (float) $interval);
  185. }
  186. }
  187. return;
  188. }
  189. if (array_search(__file__,get_included_files())===0){
  190. poller_run($argv,$argc);
  191. killme();
  192. }