2 if (sizeof($_SERVER["argv"]) == 0)
5 $directory = dirname($_SERVER["argv"][0]);
7 if (substr($directory, 0, 1) != "/")
8 $directory = $_SERVER["PWD"]."/".$directory;
10 $directory = realpath($directory."/..");
14 require_once("boot.php");
17 function poller_run(&$argv, &$argc){
25 @include(".htconfig.php");
26 require_once("include/dba.php");
27 $db = new dba($db_host, $db_user, $db_pass, $db_data);
28 unset($db_host, $db_user, $db_pass, $db_data);
32 require_once('include/session.php');
33 require_once('include/datetime.php');
34 require_once('library/simplepie/simplepie.inc');
35 require_once('include/items.php');
36 require_once('include/Contact.php');
37 require_once('include/email.php');
38 require_once('include/socgraph.php');
39 require_once('include/pidfile.php');
41 load_config('config');
42 load_config('system');
44 $maxsysload = intval(get_config('system','maxloadavg'));
47 if(function_exists('sys_getloadavg')) {
48 $load = sys_getloadavg();
49 if(intval($load[0]) > $maxsysload) {
50 logger('system: load ' . $load . ' too high. Poller deferred to next scheduled run.');
55 $lockpath = get_lockpath();
56 if ($lockpath != '') {
57 $pidfile = new pidfile($lockpath, 'poller');
58 if($pidfile->is_already_running()) {
59 logger("poller: Already running");
60 if ($pidfile->running_time() > 9*60) {
62 logger("poller: killed stale process");
63 // Calling a new instance
64 proc_run('php','include/poller.php');
72 $a->set_baseurl(get_config('system','url'));
76 logger('poller: start');
78 // run queue delivery process in the background
80 proc_run('php',"include/queue.php");
82 // run diaspora photo queue process in the background
84 proc_run('php',"include/dsprphotoq.php");
86 // expire any expired accounts
88 q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
89 AND `account_expires_on` != '0000-00-00 00:00:00'
90 AND `account_expires_on` < UTC_TIMESTAMP() ");
92 // delete user and contact records for recently removed accounts
94 $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
96 foreach($r as $user) {
97 q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
98 q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
102 $abandon_days = intval(get_config('system','account_abandon_days'));
103 if($abandon_days < 1)
106 // Check OStatus conversations
107 check_conversations();
109 // To-Do: Regenerate usage statistics
110 // q("ANALYZE TABLE `item`");
112 // once daily run birthday_updates and then expire in background
114 $d1 = get_config('system','last_expire_day');
115 $d2 = intval(datetime_convert('UTC','UTC','now','d'));
117 if($d2 != intval($d1)) {
119 update_contact_birthdays();
121 update_suggestions();
123 set_config('system','last_expire_day',$d2);
124 proc_run('php','include/expire.php');
127 $last = get_config('system','cache_last_cleared');
130 $next = $last + (3600); // Once per hour
131 $clear_cache = ($next <= time());
139 // clear old item cache files
142 // clear cache for photos
143 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
145 // clear smarty cache
146 clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
148 // clear cache for image proxy
149 if (!get_config("system", "proxy_disabled")) {
150 clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
152 $cachetime = get_config('system','proxy_cache_time');
153 if (!$cachetime) $cachetime = PROXY_DEFAULT_TIME;
155 q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
158 set_config('system','cache_last_cleared', time());
167 if(($argc > 1) && ($argv[1] == 'force'))
170 if(($argc > 1) && ($argv[1] == 'restart')) {
172 $generation = intval($argv[2]);
177 if(($argc > 1) && intval($argv[1])) {
178 $manual_id = intval($argv[1]);
182 $interval = intval(get_config('system','poll_interval'));
184 $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
186 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
190 $d = datetime_convert();
193 proc_run('php','include/cronhooks.php');
195 // Only poll from those with suitable relationships,
196 // and which have a polling address and ignore Diaspora since
197 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
199 $abandon_sql = (($abandon_days)
200 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
204 $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
205 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
206 AND NOT `network` IN ( '%s', '%s', '%s' )
208 AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0
209 AND `contact`.`archive` = 0
210 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $abandon_sql ORDER BY RAND()",
211 intval(CONTACT_IS_SHARING),
212 intval(CONTACT_IS_FRIEND),
213 dbesc(NETWORK_DIASPORA),
214 dbesc(NETWORK_FACEBOOK),
215 dbesc(NETWORK_PUMPIO)
218 if(! count($contacts)) {
222 foreach($contacts as $c) {
224 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
228 if((! $res) || (! count($res)))
231 foreach($res as $contact) {
236 $contact['last-update'] = '0000-00-00 00:00:00';
238 if($contact['network'] === NETWORK_DFRN)
239 $contact['priority'] = 2;
241 if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
242 $contact['priority'] = 2;
244 if($contact['priority'] || $contact['subhub']) {
249 $t = $contact['last-update'];
251 // We should be getting everything via a hub. But just to be sure, let's check once a day.
252 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
253 // This also lets us update our subscription to the hub, and add or replace hubs in case it
254 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
257 if($contact['subhub']) {
258 $poll_interval = get_config('system','pushpoll_frequency');
259 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
262 if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
269 * Based on $contact['priority'], should we poll this site now? Or later?
272 switch ($contact['priority']) {
274 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
278 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
282 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
286 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
291 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
295 if((! $update) && (! $force))
299 // Don't run onepoll.php if the contact isn't pollable
300 // This check also is inside the onepoll.php - but this will reduce the load
301 if (in_array($contact["rel"], array(CONTACT_IS_SHARING, CONTACT_IS_FRIEND)) AND ($contact["poll"] != "")
302 AND !in_array($contact['network'], array(NETWORK_DIASPORA, NETWORK_FACEBOOK, NETWORK_PUMPIO, NETWORK_TWITTER, NETWORK_APPNET))
303 AND !$contact["self"] AND !$contact["blocked"] AND !$contact["readonly"] AND !$contact["archive"])
304 proc_run('php','include/onepoll.php',$contact['id']);
307 @time_sleep_until(microtime(true) + (float) $interval);
314 if (array_search(__file__,get_included_files())===0){
315 poller_run($_SERVER["argv"],$_SERVER["argc"]);