2013-02-26 01:09:40 +00:00
< ? php /** @file */
2011-04-16 06:40:43 +00:00
2012-12-31 00:14:29 +00:00
require_once ( 'boot.php' );
require_once ( 'include/cli_startup.php' );
2011-01-28 13:04:18 +00:00
2011-07-01 04:56:07 +00:00
2011-01-28 13:04:18 +00:00
function poller_run ( $argv , $argc ){
2010-08-17 03:47:40 +00:00
2012-12-31 00:14:29 +00:00
cli_startup ();
2011-04-16 06:40:43 +00:00
2012-12-31 00:14:29 +00:00
$a = get_app ();
2011-06-30 08:15:18 +00:00
2012-05-04 04:50:48 +00:00
$maxsysload = intval ( get_config ( 'system' , 'maxloadavg' ));
if ( $maxsysload < 1 )
$maxsysload = 50 ;
if ( function_exists ( 'sys_getloadavg' )) {
$load = sys_getloadavg ();
if ( intval ( $load [ 0 ]) > $maxsysload ) {
logger ( 'system: load ' . $load . ' too high. Poller deferred to next scheduled run.' );
return ;
}
}
2013-10-28 07:11:44 +00:00
$interval = intval ( get_config ( 'system' , 'poll_interval' ));
if ( ! $interval )
$interval = (( get_config ( 'system' , 'delivery_interval' ) === false ) ? 3 : intval ( get_config ( 'system' , 'delivery_interval' )));
2010-11-26 02:31:33 +00:00
logger ( 'poller: start' );
2011-01-24 21:01:56 +00:00
2010-11-26 02:22:54 +00:00
// run queue delivery process in the background
2010-11-22 23:30:52 +00:00
2011-02-23 23:16:12 +00:00
proc_run ( 'php' , " include/queue.php " );
2011-01-31 16:16:35 +00:00
2013-11-07 02:28:36 +00:00
// expire any expired mail
q ( " delete from mail where expires != '0000-00-00 00:00:00' and expires < UTC_TIMESTAMP() " );
2013-11-07 08:47:50 +00:00
2014-02-11 22:35:02 +00:00
// expire any expired items
2013-11-08 06:45:13 +00:00
$r = q ( " select id from item where expires != '0000-00-00 00:00:00' and expires < UTC_TIMESTAMP()
and not ( item_restrict & % d ) " ,
intval ( ITEM_DELETED )
);
2013-11-07 08:47:50 +00:00
if ( $r ) {
require_once ( 'include/items.php' );
foreach ( $r as $rr )
drop_item ( $rr [ 'id' ], false );
}
2014-02-11 20:14:45 +00:00
2013-10-28 07:11:44 +00:00
// Ensure that every channel pings a directory server once a month. This way we can discover
// channels and sites that quietly vanished and prevent the directory from accumulating stale
// or dead entries.
$r = q ( " select channel_id from channel where channel_dirdate < UTC_TIMESTAMP() - INTERVAL 30 DAY " );
if ( $r ) {
foreach ( $r as $rr ) {
2014-02-11 22:19:20 +00:00
proc_run ( 'php' , 'include/directory.php' , $rr [ 'channel_id' ], 'force' );
2013-10-28 07:11:44 +00:00
if ( $interval )
@ time_sleep_until ( microtime ( true ) + ( float ) $interval );
}
}
2013-05-24 00:24:15 +00:00
// publish any applicable items that were set to be published in the future
// (time travel posts)
2013-05-27 12:27:35 +00:00
$r = q ( " select id from item where ( item_restrict & %d ) and created <= UTC_TIMESTAMP() " ,
2013-05-24 00:24:15 +00:00
intval ( ITEM_DELAYED_PUBLISH )
);
2013-05-27 12:27:35 +00:00
if ( $r ) {
foreach ( $r as $rr ) {
$x = q ( " update item set item_restrict = ( item_restrict ^ %d ) where id = %d limit 1 " ,
intval ( ITEM_DELAYED_PUBLISH ),
intval ( $rr [ 'id' ])
);
if ( $x ) {
2014-02-27 08:54:07 +00:00
proc_run ( 'php' , 'include/notifier.php' , 'wall-new' , $rr [ 'id' ]);
2013-05-27 12:27:35 +00:00
}
}
}
2013-05-24 00:24:15 +00:00
2011-09-30 04:20:19 +00:00
$abandon_days = intval ( get_config ( 'system' , 'account_abandon_days' ));
if ( $abandon_days < 1 )
$abandon_days = 0 ;
2011-10-14 07:20:37 +00:00
// once daily run birthday_updates and then expire in background
2011-03-16 00:31:49 +00:00
2013-06-30 07:38:02 +00:00
// FIXME: add birthday updates, both locally and for xprof for use
// by directory servers
2011-03-16 00:31:49 +00:00
$d1 = get_config ( 'system' , 'last_expire_day' );
$d2 = intval ( datetime_convert ( 'UTC' , 'UTC' , 'now' , 'd' ));
2013-10-01 01:33:27 +00:00
$dirmode = get_config ( 'system' , 'directory_mode' );
2014-03-04 03:15:11 +00:00
/**
* Cron Daily
*
* Actions in the following block are executed once per day , not on every poller run
*
*/
2014-02-11 22:35:02 +00:00
2011-03-16 00:31:49 +00:00
if ( $d2 != intval ( $d1 )) {
2011-10-14 07:20:37 +00:00
2014-03-20 02:53:14 +00:00
$d3 = intval ( datetime_convert ( 'UTC' , 'UTC' , 'now' , 'N' ));
if ( $d3 == 7 ) {
/**
* Cron Weekly
*
* Actions in the following block are executed once per day only on Sunday ( once per week ) .
*
*/
require_once ( 'include/hubloc.php' );
prune_hub_reinstalls ();
}
2014-02-11 22:35:02 +00:00
// expire any read notifications over a month old
q ( " delete from notify where seen = 1 and date < UTC_TIMESTAMP() - INTERVAL 30 DAY " );
2014-02-18 23:17:18 +00:00
// expire any expired accounts
require_once ( 'include/account.php' );
downgrade_accounts ();
2014-02-11 22:35:02 +00:00
2013-04-19 03:47:35 +00:00
// If this is a directory server, request a sync with an upstream
// directory at least once a day, up to once every poll interval.
// Pull remote changes and push local changes.
// potential issue: how do we keep from creating an endless update loop?
if ( $dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY ) {
require_once ( 'include/dir_fns.php' );
sync_directories ( $dirmode );
}
2011-03-16 00:31:49 +00:00
set_config ( 'system' , 'last_expire_day' , $d2 );
2013-11-18 00:50:32 +00:00
2014-03-04 03:15:11 +00:00
proc_run ( 'php' , 'include/expire.php' );
2013-08-06 03:08:35 +00:00
proc_run ( 'php' , 'include/cli_suggest.php' );
2011-03-16 00:31:49 +00:00
}
2013-06-04 05:34:35 +00:00
// update any photos which didn't get imported properly
// This should be rare
$r = q ( " select xchan_photo_l, xchan_hash from xchan where xchan_photo_l != '' and xchan_photo_m = ''
2013-06-06 23:12:34 +00:00
and xchan_photo_date < UTC_TIMESTAMP () - INTERVAL 1 DAY " );
2013-06-04 05:34:35 +00:00
if ( $r ) {
require_once ( 'include/photo/photo_driver.php' );
foreach ( $r as $rr ) {
$photos = import_profile_photo ( $rr [ 'xchan_photo_l' ], $rr [ 'xchan_hash' ]);
$x = q ( " update xchan set xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s'
where xchan_hash = '%s' limit 1 " ,
dbesc ( $photos [ 0 ]),
dbesc ( $photos [ 1 ]),
dbesc ( $photos [ 2 ]),
dbesc ( $photos [ 3 ]),
dbesc ( $rr [ 'xchan_hash' ])
);
}
}
2014-03-27 01:45:01 +00:00
2014-03-27 22:35:29 +00:00
// pull in some public posts
2014-03-30 21:59:37 +00:00
if ( ! get_config ( 'system' , 'disable_discover_tab' ))
2014-03-30 02:20:43 +00:00
proc_run ( 'php' , 'include/externals.php' );
2014-03-27 01:45:01 +00:00
2012-03-11 18:11:25 +00:00
2011-02-03 03:56:27 +00:00
$manual_id = 0 ;
2011-07-01 04:56:07 +00:00
$generation = 0 ;
2012-08-31 01:47:07 +00:00
2011-02-03 03:56:27 +00:00
$force = false ;
2011-07-01 04:56:07 +00:00
$restart = false ;
2011-01-10 21:45:42 +00:00
2013-08-16 00:36:00 +00:00
if (( $argc > 1 ) && ( $argv [ 1 ] == 'force' ))
2010-11-05 03:47:44 +00:00
$force = true ;
2013-08-16 00:36:00 +00:00
if (( $argc > 1 ) && ( $argv [ 1 ] == 'restart' )) {
2011-07-01 04:56:07 +00:00
$restart = true ;
2013-08-16 00:36:00 +00:00
$generation = intval ( $argv [ 2 ]);
2011-07-01 04:56:07 +00:00
if ( ! $generation )
killme ();
}
2013-08-16 00:36:00 +00:00
if (( $argc > 1 ) && intval ( $argv [ 1 ])) {
$manual_id = intval ( $argv [ 1 ]);
2011-02-03 03:56:27 +00:00
$force = true ;
2011-01-24 04:09:34 +00:00
}
2012-04-28 06:17:40 +00:00
2012-12-31 00:14:29 +00:00
$sql_extra = (( $manual_id ) ? " AND abook_id = $manual_id " : " " );
2011-01-24 04:09:34 +00:00
2011-04-26 11:39:27 +00:00
reload_plugins ();
2011-03-09 10:12:32 +00:00
$d = datetime_convert ();
2011-04-26 11:39:27 +00:00
2012-12-31 00:14:29 +00:00
//TODO check to see if there are any cronhooks before wasting a process
2011-07-01 04:56:07 +00:00
if ( ! $restart )
2011-08-15 05:59:34 +00:00
proc_run ( 'php' , 'include/cronhooks.php' );
2011-03-09 10:12:32 +00:00
2014-03-04 22:35:42 +00:00
// Only poll from those with suitable relationships
2011-08-24 01:17:35 +00:00
2011-09-30 04:20:19 +00:00
$abandon_sql = (( $abandon_days )
2012-12-31 00:14:29 +00:00
? sprintf ( " AND account_lastlog > UTC_TIMESTAMP() - INTERVAL %d DAY " , intval ( $abandon_days ))
2011-09-30 04:20:19 +00:00
: ''
);
2013-08-16 00:36:00 +00:00
2013-09-13 04:54:15 +00:00
$contacts = q ( " SELECT abook_id, abook_flags, abook_updated, abook_connected, abook_closeness, abook_channel
2012-12-31 07:51:39 +00:00
FROM abook LEFT JOIN account on abook_account = account_id where 1
2011-01-24 04:09:34 +00:00
$sql_extra
2014-03-04 22:35:42 +00:00
AND (( abook_flags & % d ) OR ( abook_flags = % d ))
2012-12-31 07:51:39 +00:00
AND (( account_flags = % d ) OR ( account_flags = % d )) $abandon_sql ORDER BY RAND () " ,
2014-03-04 22:35:42 +00:00
intval ( ABOOK_FLAG_HIDDEN | ABOOK_FLAG_PENDING | ABOOK_FLAG_UNCONNECTED ),
2012-12-31 07:51:39 +00:00
intval ( 0 ),
intval ( ACCOUNT_OK ),
intval ( ACCOUNT_UNVERIFIED ) // FIXME
2012-12-31 00:14:29 +00:00
2011-03-05 04:55:32 +00:00
);
2010-07-19 03:49:54 +00:00
2013-10-01 01:33:27 +00:00
if ( $contacts ) {
2010-07-19 03:49:54 +00:00
2013-10-01 01:33:27 +00:00
foreach ( $contacts as $contact ) {
2012-12-31 00:14:29 +00:00
2013-10-01 01:33:27 +00:00
$update = false ;
2012-12-31 00:14:29 +00:00
2013-10-01 01:33:27 +00:00
$t = $contact [ 'abook_updated' ];
$c = $contact [ 'abook_connected' ];
2012-12-31 00:14:29 +00:00
2013-01-30 03:28:19 +00:00
2013-10-01 01:33:27 +00:00
if ( $c == $t ) {
if ( datetime_convert ( 'UTC' , 'UTC' , 'now' ) > datetime_convert ( 'UTC' , 'UTC' , $t . " + 1 day " ))
$update = true ;
2013-01-30 03:28:19 +00:00
}
2013-10-01 01:33:27 +00:00
else {
// if we've never connected with them, start the mark for death countdown from now
if ( $c == '0000-00-00 00:00:00' ) {
$r = q ( " update abook set abook_connected = '%s' where abook_id = %d limit 1 " ,
dbesc ( datetime_convert ()),
intval ( $contact [ 'abook_id' ])
);
$c = datetime_convert ();
$update = true ;
}
// He's dead, Jim
if ( strcmp ( datetime_convert ( 'UTC' , 'UTC' , 'now' ), datetime_convert ( 'UTC' , 'UTC' , $c . " + 30 day " )) > 0 ) {
$r = q ( " update abook set abook_flags = (abook_flags | %d) where abook_id = %d limit 1 " ,
intval ( ABOOK_FLAG_ARCHIVED ),
intval ( $contact [ 'abook_id' ])
);
$update = false ;
continue ;
}
if ( $contact [ 'abook_flags' ] & ABOOK_FLAG_ARCHIVED ) {
$update = false ;
continue ;
}
// might be dead, so maybe don't poll quite so often
// recently deceased, so keep up the regular schedule for 3 days
if (( strcmp ( datetime_convert ( 'UTC' , 'UTC' , 'now' ), datetime_convert ( 'UTC' , 'UTC' , $c . " + 3 day " )) > 0 )
&& ( strcmp ( datetime_convert ( 'UTC' , 'UTC' , 'now' ), datetime_convert ( 'UTC' , 'UTC' , $t . " + 1 day " )) > 0 ))
$update = true ;
2013-01-30 03:28:19 +00:00
2013-10-01 01:33:27 +00:00
// After that back off and put them on a morphine drip
if ( strcmp ( datetime_convert ( 'UTC' , 'UTC' , 'now' ), datetime_convert ( 'UTC' , 'UTC' , $t . " + 2 day " )) > 0 ) {
$update = true ;
}
2013-01-30 03:28:19 +00:00
2013-09-13 04:54:15 +00:00
}
2013-10-01 01:33:27 +00:00
if (( ! $update ) && ( ! $force ))
continue ;
2013-01-30 03:28:19 +00:00
2013-10-01 01:33:27 +00:00
proc_run ( 'php' , 'include/onepoll.php' , $contact [ 'abook_id' ]);
if ( $interval )
@ time_sleep_until ( microtime ( true ) + ( float ) $interval );
2013-01-30 03:28:19 +00:00
2013-10-01 01:33:27 +00:00
}
}
2013-01-30 03:28:19 +00:00
2013-10-01 01:33:27 +00:00
if ( $dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY ) {
2013-10-15 09:33:35 +00:00
$r = q ( " select distinct ud_addr, updates.* from updates where not ( ud_flags & %d ) and ud_addr != '' and ( ud_last = '0000-00-00 00:00:00' OR ud_last > UTC_TIMESTAMP() - INTERVAL 7 DAY ) group by ud_addr " ,
2013-10-01 01:33:27 +00:00
intval ( UPDATE_FLAGS_UPDATED )
);
if ( $r ) {
foreach ( $r as $rr ) {
// If they didn't respond when we attempted before, back off to once a day
// After 7 days we won't bother anymore
if ( $rr [ 'ud_last' ] != '0000-00-00 00:00:00' )
if ( $rr [ 'ud_last' ] > datetime_convert ( 'UTC' , 'UTC' , 'now - 1 day' ))
continue ;
proc_run ( 'php' , 'include/onedirsync.php' , $rr [ 'ud_id' ]);
if ( $interval )
@ time_sleep_until ( microtime ( true ) + ( float ) $interval );
2013-01-30 03:28:19 +00:00
}
}
2011-03-05 04:55:32 +00:00
}
2013-10-01 01:33:27 +00:00
2011-01-28 13:04:18 +00:00
return ;
}
2010-07-19 03:49:54 +00:00
2011-01-28 13:04:18 +00:00
if ( array_search ( __file__ , get_included_files ()) === 0 ){
poller_run ( $argv , $argc );
killme ();
}