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.
 
 
 
 
 
 

379 lines
10 KiB

  1. <?php
  2. if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
  3. $directory = dirname($_SERVER["argv"][0]);
  4. if (substr($directory, 0, 1) != "/")
  5. $directory = $_SERVER["PWD"]."/".$directory;
  6. $directory = realpath($directory."/..");
  7. chdir($directory);
  8. }
  9. require_once("boot.php");
  10. function cron_run(&$argv, &$argc){
  11. global $a, $db;
  12. if(is_null($a)) {
  13. $a = new App;
  14. }
  15. if(is_null($db)) {
  16. @include(".htconfig.php");
  17. require_once("include/dba.php");
  18. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  19. unset($db_host, $db_user, $db_pass, $db_data);
  20. };
  21. require_once('include/session.php');
  22. require_once('include/datetime.php');
  23. require_once('library/simplepie/simplepie.inc');
  24. require_once('include/items.php');
  25. require_once('include/Contact.php');
  26. require_once('include/email.php');
  27. require_once('include/socgraph.php');
  28. require_once('include/pidfile.php');
  29. require_once('mod/nodeinfo.php');
  30. load_config('config');
  31. load_config('system');
  32. $maxsysload = intval(get_config('system','maxloadavg'));
  33. if($maxsysload < 1)
  34. $maxsysload = 50;
  35. if(function_exists('sys_getloadavg')) {
  36. $load = sys_getloadavg();
  37. if(intval($load[0]) > $maxsysload) {
  38. logger('system: load ' . $load[0] . ' too high. cron deferred to next scheduled run.');
  39. return;
  40. }
  41. }
  42. $last = get_config('system','last_cron');
  43. $poll_interval = intval(get_config('system','cron_interval'));
  44. if(! $poll_interval)
  45. $poll_interval = 10;
  46. if($last) {
  47. $next = $last + ($poll_interval * 60);
  48. if($next > time()) {
  49. logger('cron intervall not reached');
  50. return;
  51. }
  52. }
  53. $lockpath = get_lockpath();
  54. if ($lockpath != '') {
  55. $pidfile = new pidfile($lockpath, 'cron');
  56. if($pidfile->is_already_running()) {
  57. logger("cron: Already running");
  58. if ($pidfile->running_time() > 9*60) {
  59. $pidfile->kill();
  60. logger("cron: killed stale process");
  61. // Calling a new instance
  62. proc_run('php','include/cron.php');
  63. }
  64. exit;
  65. }
  66. }
  67. $a->set_baseurl(get_config('system','url'));
  68. load_hooks();
  69. logger('cron: start');
  70. // run queue delivery process in the background
  71. proc_run('php',"include/queue.php");
  72. // run diaspora photo queue process in the background
  73. proc_run('php',"include/dsprphotoq.php");
  74. // run the process to discover global contacts in the background
  75. proc_run('php',"include/discover_poco.php");
  76. // run the process to update locally stored global contacts in the background
  77. proc_run('php',"include/discover_poco.php", "checkcontact");
  78. // expire any expired accounts
  79. q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
  80. AND `account_expires_on` != '0000-00-00 00:00:00'
  81. AND `account_expires_on` < UTC_TIMESTAMP() ");
  82. // delete user and contact records for recently removed accounts
  83. $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
  84. if ($r) {
  85. foreach($r as $user) {
  86. q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
  87. q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
  88. }
  89. }
  90. $abandon_days = intval(get_config('system','account_abandon_days'));
  91. if($abandon_days < 1)
  92. $abandon_days = 0;
  93. // Check OStatus conversations
  94. // Check only conversations with mentions (for a longer time)
  95. check_conversations(true);
  96. // Check every conversation
  97. check_conversations(false);
  98. // Follow your friends from your legacy OStatus account
  99. // Doesn't work
  100. // ostatus_check_follow_friends();
  101. // update nodeinfo data
  102. nodeinfo_cron();
  103. // To-Do: Regenerate usage statistics
  104. // q("ANALYZE TABLE `item`");
  105. // once daily run birthday_updates and then expire in background
  106. $d1 = get_config('system','last_expire_day');
  107. $d2 = intval(datetime_convert('UTC','UTC','now','d'));
  108. if($d2 != intval($d1)) {
  109. update_contact_birthdays();
  110. proc_run('php',"include/discover_poco.php", "suggestions");
  111. set_config('system','last_expire_day',$d2);
  112. proc_run('php','include/expire.php');
  113. }
  114. $last = get_config('system','cache_last_cleared');
  115. if($last) {
  116. $next = $last + (3600); // Once per hour
  117. $clear_cache = ($next <= time());
  118. } else
  119. $clear_cache = true;
  120. if ($clear_cache) {
  121. // clear old cache
  122. Cache::clear();
  123. // clear old item cache files
  124. clear_cache();
  125. // clear cache for photos
  126. clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
  127. // clear smarty cache
  128. clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
  129. // clear cache for image proxy
  130. if (!get_config("system", "proxy_disabled")) {
  131. clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
  132. $cachetime = get_config('system','proxy_cache_time');
  133. if (!$cachetime) $cachetime = PROXY_DEFAULT_TIME;
  134. q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
  135. }
  136. // Maximum table size in megabyte
  137. $max_tablesize = intval(get_config('system','optimize_max_tablesize')) * 1000000;
  138. if ($max_tablesize == 0)
  139. $max_tablesize = 100 * 1000000; // Default are 100 MB
  140. // Minimum fragmentation level in percent
  141. $fragmentation_level = intval(get_config('system','optimize_fragmentation')) / 100;
  142. if ($fragmentation_level == 0)
  143. $fragmentation_level = 0.3; // Default value is 30%
  144. // Optimize some tables that need to be optimized
  145. $r = q("SHOW TABLE STATUS");
  146. foreach($r as $table) {
  147. // Don't optimize tables that are too large
  148. if ($table["Data_length"] > $max_tablesize)
  149. continue;
  150. // Don't optimize empty tables
  151. if ($table["Data_length"] == 0)
  152. continue;
  153. // Calculate fragmentation
  154. $fragmentation = $table["Data_free"] / $table["Data_length"];
  155. logger("Table ".$table["Name"]." - Fragmentation level: ".round($fragmentation * 100, 2), LOGGER_DEBUG);
  156. // Don't optimize tables that needn't to be optimized
  157. if ($fragmentation < $fragmentation_level)
  158. continue;
  159. // So optimize it
  160. logger("Optimize Table ".$table["Name"], LOGGER_DEBUG);
  161. q("OPTIMIZE TABLE `%s`", dbesc($table["Name"]));
  162. }
  163. set_config('system','cache_last_cleared', time());
  164. }
  165. $manual_id = 0;
  166. $generation = 0;
  167. $force = false;
  168. $restart = false;
  169. if(($argc > 1) && ($argv[1] == 'force'))
  170. $force = true;
  171. if(($argc > 1) && ($argv[1] == 'restart')) {
  172. $restart = true;
  173. $generation = intval($argv[2]);
  174. if(! $generation)
  175. killme();
  176. }
  177. if(($argc > 1) && intval($argv[1])) {
  178. $manual_id = intval($argv[1]);
  179. $force = true;
  180. }
  181. $interval = intval(get_config('system','poll_interval'));
  182. if(! $interval)
  183. $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
  184. // If we are using the worker we don't need a delivery interval
  185. if (get_config("system", "worker"))
  186. $interval = false;
  187. $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
  188. reload_plugins();
  189. $d = datetime_convert();
  190. // Only poll from those with suitable relationships,
  191. // and which have a polling address and ignore Diaspora since
  192. // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
  193. $abandon_sql = (($abandon_days)
  194. ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
  195. : ''
  196. );
  197. $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
  198. WHERE `rel` IN (%d, %d) AND `poll` != '' AND `network` IN ('%s', '%s', '%s', '%s', '%s', '%s')
  199. $sql_extra
  200. AND NOT `self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly` AND NOT `contact`.`archive`
  201. AND NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
  202. intval(CONTACT_IS_SHARING),
  203. intval(CONTACT_IS_FRIEND),
  204. dbesc(NETWORK_DFRN),
  205. dbesc(NETWORK_ZOT),
  206. dbesc(NETWORK_OSTATUS),
  207. dbesc(NETWORK_FEED),
  208. dbesc(NETWORK_MAIL),
  209. dbesc(NETWORK_MAIL2)
  210. );
  211. if(! count($contacts)) {
  212. return;
  213. }
  214. foreach($contacts as $c) {
  215. $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
  216. intval($c['id'])
  217. );
  218. if((! $res) || (! count($res)))
  219. continue;
  220. foreach($res as $contact) {
  221. $xml = false;
  222. if($manual_id)
  223. $contact['last-update'] = '0000-00-00 00:00:00';
  224. if(in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS)))
  225. $contact['priority'] = 2;
  226. if($contact['subhub'] AND in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
  227. // We should be getting everything via a hub. But just to be sure, let's check once a day.
  228. // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
  229. // This also lets us update our subscription to the hub, and add or replace hubs in case it
  230. // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
  231. $poll_interval = get_config('system','pushpoll_frequency');
  232. $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
  233. }
  234. if($contact['priority'] AND !$force) {
  235. $update = false;
  236. $t = $contact['last-update'];
  237. /**
  238. * Based on $contact['priority'], should we poll this site now? Or later?
  239. */
  240. switch ($contact['priority']) {
  241. case 5:
  242. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
  243. $update = true;
  244. break;
  245. case 4:
  246. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
  247. $update = true;
  248. break;
  249. case 3:
  250. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
  251. $update = true;
  252. break;
  253. case 2:
  254. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
  255. $update = true;
  256. break;
  257. case 1:
  258. default:
  259. if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
  260. $update = true;
  261. break;
  262. }
  263. if(!$update)
  264. continue;
  265. }
  266. logger("Polling ".$contact["network"]." ".$contact["id"]." ".$contact["nick"]." ".$contact["name"]);
  267. proc_run('php','include/onepoll.php',$contact['id']);
  268. if($interval)
  269. @time_sleep_until(microtime(true) + (float) $interval);
  270. }
  271. }
  272. logger('cron: end');
  273. set_config('system','last_cron', time());
  274. return;
  275. }
  276. if (array_search(__file__,get_included_files())===0){
  277. cron_run($_SERVER["argv"],$_SERVER["argc"]);
  278. killme();
  279. }