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.
 
 
 
 
 
 

248 lines
6.8 KiB

  1. <?php
  2. use \Friendica\Core\Config;
  3. function cron_run(&$argv, &$argc){
  4. global $a;
  5. require_once('include/datetime.php');
  6. // Poll contacts with specific parameters
  7. if ($argc > 1) {
  8. cron_poll_contacts($argc, $argv);
  9. return;
  10. }
  11. $last = get_config('system','last_cron');
  12. $poll_interval = intval(get_config('system','cron_interval'));
  13. if (! $poll_interval) {
  14. $poll_interval = 10;
  15. }
  16. if ($last) {
  17. $next = $last + ($poll_interval * 60);
  18. if ($next > time()) {
  19. logger('cron intervall not reached');
  20. return;
  21. }
  22. }
  23. logger('cron: start');
  24. // run queue delivery process in the background
  25. proc_run(PRIORITY_NEGLIGIBLE, "include/queue.php");
  26. // run the process to discover global contacts in the background
  27. proc_run(PRIORITY_LOW, "include/discover_poco.php");
  28. // run the process to update locally stored global contacts in the background
  29. proc_run(PRIORITY_LOW, "include/discover_poco.php", "checkcontact");
  30. // Expire and remove user entries
  31. proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "expire_and_remove_users");
  32. // Check OStatus conversations
  33. proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_mentions");
  34. // Check every conversation
  35. proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_conversations");
  36. // Call possible post update functions
  37. proc_run(PRIORITY_LOW, "include/cronjobs.php", "post_update");
  38. // update nodeinfo data
  39. proc_run(PRIORITY_LOW, "include/cronjobs.php", "nodeinfo");
  40. // Clear cache entries
  41. proc_run(PRIORITY_LOW, "include/cronjobs.php", "clear_cache");
  42. // Repair missing Diaspora values in contacts
  43. proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_diaspora");
  44. // Repair entries in the database
  45. proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_database");
  46. // once daily run birthday_updates and then expire in background
  47. $d1 = get_config('system','last_expire_day');
  48. $d2 = intval(datetime_convert('UTC','UTC','now','d'));
  49. if ($d2 != intval($d1)) {
  50. proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_contact_birthdays");
  51. proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server");
  52. proc_run(PRIORITY_LOW, "include/discover_poco.php", "suggestions");
  53. set_config('system','last_expire_day',$d2);
  54. proc_run(PRIORITY_LOW, 'include/expire.php');
  55. proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
  56. proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_photo_albums");
  57. }
  58. // Poll contacts
  59. cron_poll_contacts($argc, $argv);
  60. logger('cron: end');
  61. set_config('system','last_cron', time());
  62. return;
  63. }
  64. /**
  65. * @brief Poll contacts for unreceived messages
  66. *
  67. * @param Integer $argc Number of command line arguments
  68. * @param Array $argv Array of command line arguments
  69. */
  70. function cron_poll_contacts($argc, $argv) {
  71. $manual_id = 0;
  72. $generation = 0;
  73. $force = false;
  74. $restart = false;
  75. if (($argc > 1) && ($argv[1] == 'force')) {
  76. $force = true;
  77. }
  78. if (($argc > 1) && ($argv[1] == 'restart')) {
  79. $restart = true;
  80. $generation = intval($argv[2]);
  81. if (!$generation) {
  82. killme();
  83. }
  84. }
  85. if (($argc > 1) && intval($argv[1])) {
  86. $manual_id = intval($argv[1]);
  87. $force = true;
  88. }
  89. $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
  90. reload_plugins();
  91. $d = datetime_convert();
  92. // Only poll from those with suitable relationships,
  93. // and which have a polling address and ignore Diaspora since
  94. // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
  95. $abandon_days = intval(get_config('system','account_abandon_days'));
  96. if ($abandon_days < 1) {
  97. $abandon_days = 0;
  98. }
  99. $abandon_sql = (($abandon_days)
  100. ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
  101. : ''
  102. );
  103. $contacts = q("SELECT `contact`.`id` FROM `user`
  104. STRAIGHT_JOIN `contact`
  105. ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
  106. AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
  107. AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
  108. AND NOT `contact`.`archive`
  109. WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
  110. intval(CONTACT_IS_SHARING),
  111. intval(CONTACT_IS_FRIEND),
  112. dbesc(NETWORK_DFRN),
  113. dbesc(NETWORK_ZOT),
  114. dbesc(NETWORK_OSTATUS),
  115. dbesc(NETWORK_FEED),
  116. dbesc(NETWORK_MAIL),
  117. dbesc(NETWORK_MAIL2)
  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 (!dbm::is_result($res)) {
  127. continue;
  128. }
  129. foreach ($res as $contact) {
  130. $xml = false;
  131. if ($manual_id) {
  132. $contact['last-update'] = NULL_DATE;
  133. }
  134. if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
  135. $contact['priority'] = 2;
  136. }
  137. if ($contact['subhub'] AND in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
  138. // We should be getting everything via a hub. But just to be sure, let's check once a day.
  139. // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
  140. // This also lets us update our subscription to the hub, and add or replace hubs in case it
  141. // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
  142. $poll_interval = get_config('system','pushpoll_frequency');
  143. $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
  144. }
  145. if ($contact['priority'] AND !$force) {
  146. $update = false;
  147. $t = $contact['last-update'];
  148. /**
  149. * Based on $contact['priority'], should we poll this site now? Or later?
  150. */
  151. switch ($contact['priority']) {
  152. case 5:
  153. if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month")) {
  154. $update = true;
  155. }
  156. break;
  157. case 4:
  158. if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week")) {
  159. $update = true;
  160. }
  161. break;
  162. case 3:
  163. if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) {
  164. $update = true;
  165. }
  166. break;
  167. case 2:
  168. if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour")) {
  169. $update = true;
  170. }
  171. break;
  172. case 1:
  173. default:
  174. if (datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour")) {
  175. $update = true;
  176. }
  177. break;
  178. }
  179. if (!$update) {
  180. continue;
  181. }
  182. }
  183. logger("Polling ".$contact["network"]." ".$contact["id"]." ".$contact["nick"]." ".$contact["name"]);
  184. if (($contact['network'] == NETWORK_FEED) AND ($contact['priority'] <= 3)) {
  185. proc_run(PRIORITY_MEDIUM, 'include/onepoll.php', intval($contact['id']));
  186. } else {
  187. proc_run(PRIORITY_LOW, 'include/onepoll.php', intval($contact['id']));
  188. }
  189. }
  190. }
  191. }