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_config('system','lockpath');
45 if ($lockpath != '') {
46 $pidfile = new pidfile($lockpath, 'poller.lck');
47 if($pidfile->is_already_running()) {
48 logger("poller: Already running");
55 $a->set_baseurl(get_config('system','url'));
59 logger('poller: start');
61 // run queue delivery process in the background
63 proc_run('php',"include/queue.php");
65 // expire any expired accounts
67 q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
68 AND `account_expires_on` != '0000-00-00 00:00:00'
69 AND `account_expires_on` < UTC_TIMESTAMP() ");
71 // delete user and contact records for recently removed accounts
73 $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
75 foreach($r as $user) {
76 q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
77 q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
81 $abandon_days = intval(get_config('system','account_abandon_days'));
87 // once daily run birthday_updates and then expire in background
89 $d1 = get_config('system','last_expire_day');
90 $d2 = intval(datetime_convert('UTC','UTC','now','d'));
92 if($d2 != intval($d1)) {
94 update_contact_birthdays();
98 set_config('system','last_expire_day',$d2);
99 proc_run('php','include/expire.php');
102 $last = get_config('system','cache_last_cleared');
105 $next = $last + (3600); // Once per hour
106 $clear_cache = ($next <= time());
114 // clear old item cache files
117 // clear cache for photos
118 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
120 set_config('system','cache_last_cleared', time());
129 if(($argc > 1) && ($argv[1] == 'force'))
132 if(($argc > 1) && ($argv[1] == 'restart')) {
134 $generation = intval($argv[2]);
139 if(($argc > 1) && intval($argv[1])) {
140 $manual_id = intval($argv[1]);
144 $interval = intval(get_config('system','poll_interval'));
146 $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
148 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
152 $d = datetime_convert();
155 proc_run('php','include/cronhooks.php');
157 // Only poll from those with suitable relationships,
158 // and which have a polling address and ignore Diaspora since
159 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
161 $abandon_sql = (($abandon_days)
162 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
166 $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
167 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
168 AND NOT `network` IN ( '%s', '%s' )
170 AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
171 AND `contact`.`archive` = 0
172 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $abandon_sql ORDER BY RAND()",
173 intval(CONTACT_IS_SHARING),
174 intval(CONTACT_IS_FRIEND),
175 dbesc(NETWORK_DIASPORA),
176 dbesc(NETWORK_FACEBOOK)
179 if(! count($contacts)) {
183 foreach($contacts as $c) {
185 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
189 if((! $res) || (! count($res)))
192 foreach($res as $contact) {
197 $contact['last-update'] = '0000-00-00 00:00:00';
199 if($contact['network'] === NETWORK_DFRN)
200 $contact['priority'] = 2;
202 if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
203 $contact['priority'] = 2;
205 if($contact['priority'] || $contact['subhub']) {
210 $t = $contact['last-update'];
212 // We should be getting everything via a hub. But just to be sure, let's check once a day.
213 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
214 // This also lets us update our subscription to the hub, and add or replace hubs in case it
215 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
218 if($contact['subhub']) {
219 $poll_interval = get_config('system','pushpoll_frequency');
220 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
223 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
230 * Based on $contact['priority'], should we poll this site now? Or later?
233 switch ($contact['priority']) {
235 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
239 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
243 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
247 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
252 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
256 if((! $update) && (! $force))
260 proc_run('php','include/onepoll.php',$contact['id']);
262 @time_sleep_until(microtime(true) + (float) $interval);
269 if (array_search(__file__,get_included_files())===0){
270 poller_run($argv,$argc);