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.

242 lines
6.2 KiB

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