3 require_once("boot.php");
6 function poller_run(&$argv, &$argc){
14 @include(".htconfig.php");
15 require_once("include/dba.php");
16 $db = new dba($db_host, $db_user, $db_pass, $db_data);
17 unset($db_host, $db_user, $db_pass, $db_data);
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');
30 load_config('config');
31 load_config('system');
33 $maxsysload = intval(get_config('system','maxloadavg'));
36 if(function_exists('sys_getloadavg')) {
37 $load = sys_getloadavg();
38 if(intval($load[0]) > $maxsysload) {
39 logger('system: load ' . $load . ' too high. Poller deferred to next scheduled run.');
44 $lockpath = get_lockpath();
45 if ($lockpath != '') {
46 $pidfile = new pidfile($lockpath, 'poller');
47 if($pidfile->is_already_running()) {
48 logger("poller: Already running");
49 if ($pidfile->running_time() > 9*60) {
51 logger("poller: killed stale process");
52 // Calling a new instance
53 proc_run('php','include/poller.php');
61 $a->set_baseurl(get_config('system','url'));
65 logger('poller: start');
67 // run queue delivery process in the background
69 proc_run('php',"include/queue.php");
71 // run diaspora photo queue process in the background
73 proc_run('php',"include/dsprphotoq.php");
75 // expire any expired accounts
77 q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
78 AND `account_expires_on` != '0000-00-00 00:00:00'
79 AND `account_expires_on` < UTC_TIMESTAMP() ");
81 // 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");
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']));
91 $abandon_days = intval(get_config('system','account_abandon_days'));
95 // Check OStatus conversations
96 check_conversations();
98 // To-Do: Regenerate usage statistics
99 // q("ANALYZE TABLE `item`");
101 // once daily run birthday_updates and then expire in background
103 $d1 = get_config('system','last_expire_day');
104 $d2 = intval(datetime_convert('UTC','UTC','now','d'));
106 if($d2 != intval($d1)) {
108 update_contact_birthdays();
110 update_suggestions();
112 set_config('system','last_expire_day',$d2);
113 proc_run('php','include/expire.php');
116 $last = get_config('system','cache_last_cleared');
119 $next = $last + (3600); // Once per hour
120 $clear_cache = ($next <= time());
128 // clear old item cache files
131 // clear cache for photos
132 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
134 // clear smarty cache
135 clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
137 set_config('system','cache_last_cleared', time());
146 if(($argc > 1) && ($argv[1] == 'force'))
149 if(($argc > 1) && ($argv[1] == 'restart')) {
151 $generation = intval($argv[2]);
156 if(($argc > 1) && intval($argv[1])) {
157 $manual_id = intval($argv[1]);
161 $interval = intval(get_config('system','poll_interval'));
163 $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
165 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
169 $d = datetime_convert();
172 proc_run('php','include/cronhooks.php');
174 // Only poll from those with suitable relationships,
175 // and which have a polling address and ignore Diaspora since
176 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
178 $abandon_sql = (($abandon_days)
179 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
183 $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
184 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
185 AND NOT `network` IN ( '%s', '%s', '%s' )
187 AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
188 AND `contact`.`archive` = 0
189 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $abandon_sql ORDER BY RAND()",
190 intval(CONTACT_IS_SHARING),
191 intval(CONTACT_IS_FRIEND),
192 dbesc(NETWORK_DIASPORA),
193 dbesc(NETWORK_FACEBOOK),
194 dbesc(NETWORK_PUMPIO)
197 if(! count($contacts)) {
201 foreach($contacts as $c) {
203 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
207 if((! $res) || (! count($res)))
210 foreach($res as $contact) {
215 $contact['last-update'] = '0000-00-00 00:00:00';
217 if($contact['network'] === NETWORK_DFRN)
218 $contact['priority'] = 2;
220 if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
221 $contact['priority'] = 2;
223 if($contact['priority'] || $contact['subhub']) {
228 $t = $contact['last-update'];
230 // We should be getting everything via a hub. But just to be sure, let's check once a day.
231 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
232 // This also lets us update our subscription to the hub, and add or replace hubs in case it
233 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
236 if($contact['subhub']) {
237 $poll_interval = get_config('system','pushpoll_frequency');
238 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
241 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
248 * Based on $contact['priority'], should we poll this site now? Or later?
251 switch ($contact['priority']) {
253 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
257 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
261 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
265 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
270 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
274 if((! $update) && (! $force))
278 proc_run('php','include/onepoll.php',$contact['id']);
280 @time_sleep_until(microtime(true) + (float) $interval);
287 if (array_search(__file__,get_included_files())===0){
288 poller_run($argv,$argc);