File: /var/www/ilya/data/www/kamforum.ru/sources/tasks/deleteinactive.php
<?php
/*
+--------------------------------------------------------------------------
| Invision Power Board v2.1.x
| =============================================
+---------------------------------------------------------------------------
|
| > TASK SCRIPT: Delete Inactive Members
| > Script written by d1pro
| > Date started: 19th July 2004
|
+--------------------------------------------------------------------------
*/
//+--------------------------------------------------------------------------
// THIS TASKS OPERATIONS:
// Prune inactive members if they are registered $day days ago and have less than $post posts.
//+--------------------------------------------------------------------------
if ( ! defined( 'IN_IPB' ) )
{
print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
exit();
}
class task_item
{
var $class = "";
var $root_path = "";
var $task = "";
/*-------------------------------------------------------------------------*/
// Our 'auto_run' function
// ADD CODE HERE
/*-------------------------------------------------------------------------*/
function run_task()
{
$max_per_run = 500; // Maximum allowed members to prune and warn per run (big boards optimization)
// Get our settings. We aren't using cache, 'cause it will cause unusefull garbage in memory.
// More universal code, perhaps I'll use it in my other modules :)
$this->ipsclass->DB->simple_construct( array ( 'select' => 'conf_key, conf_value, conf_default', 'from' => 'conf_settings', 'where' => "conf_key LIKE 'autoprune_%'" ) );
$this->ipsclass->DB->simple_exec();
while ( $r = $this->ipsclass->DB->fetch_row() )
{
$r['conf_value'] = $r['conf_value'] ? $r['conf_value'] : $r['conf_default'];
$taskinfo[ $r['conf_key'] ] = $r['conf_value'];
}
if ( ! $taskinfo['autoprune_enabled'] )
{
$this->class->append_task_log( $this->task, "Task execution disabled :'(" );
return;
}
// Load up some cache data
$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'cache_store', 'where' => "cs_key = 'autoprune' " ) );
$this->ipsclass->DB->simple_exec();
while ( $r = $this->ipsclass->DB->fetch_row() )
{
if ( $r['cs_array'] )
{
$this->ipsclass->cache[ $r['cs_key'] ] = unserialize(stripslashes($r['cs_value']));
}
else
{
$this->ipsclass->cache[ $r['cs_key'] ] = $r['cs_value'];
}
}
// The rest...
$search = array( "/%name%/i",
"/%posts%/i",
"/%warn_days%/i",
"/%prune_days%/i",
"/%board_name%/i",
"/%board_url%/i", );
$board_name = $this->ipsclass->vars['board_name'];
$board_url = $this->ipsclass->vars['board_url'];
$posts = intval( $taskinfo['autoprune_posts'] );
$q = "";
if ( strlen( $taskinfo['autoprune_g_exclude'] ) )
{
$q .= " AND mgroup NOT IN (".$taskinfo['autoprune_g_exclude'].") ";
}
if ( strlen( $taskinfo['autoprune_m_exclude'] ) )
{
$q .= " AND id NOT IN (".$taskinfo['autoprune_m_exclude'].") ";
}
$period = time()-(60*60*24*$taskinfo['autoprune_delete']);
$period_type = $taskinfo['autoprune_time'] ? 'last_visit' : 'joined';
//---------------------------------------
// Count how much we should delete
//---------------------------------------
$this->ipsclass->DB->simple_construct( array( 'select' => "count(*) as del_count",
'from' => 'members',
'where' => "posts <= $posts AND $period_type < $period".$q,
'order' => 'id ASC', ) );
$this->ipsclass->DB->simple_exec();
$tmp = $this->ipsclass->DB->fetch_row();
if ( ! $this->ipsclass->cache['autoprune']['total_del'] )
{
$this->ipsclass->cache['autoprune']['total_del'] = $tmp['del_count'];
}
//---------------------------------------
// Delete inactive
//---------------------------------------
$this->ipsclass->DB->simple_construct( array( 'select' => "id, name, members_display_name, posts, $period_type",
'from' => 'members',
'where' => "posts <= $posts AND $period_type < $period".$q,
'order' => 'id ASC',
'limit' => array(0, $max_per_run) ) );
$this->ipsclass->DB->simple_exec();
$names = "";
$names_a = array();
$ids = array();
while( $r = $this->ipsclass->DB->fetch_row() )
{
$ids[] = $r['id'];
$names_a[] = $r['members_display_name']." (".$r['name'].", ".$r['id'].", ".$r['posts'].", ".$this->ipsclass->get_date( $r[$period_type], 'LONG', 1 ).")";
}
if ( count( $ids ) )
{
define( 'IN_ACP', 1 );
require_once $this->root_path."sources/action_admin/member.php";
$member_func = new ad_member;
$member_func->ipsclass =& $this->ipsclass;
if ( USE_MODULES == 1 )
{
require_once $this->root_path."modules/ipb_member_sync.php";
$member_func->modules = new ipb_member_sync();
$member_func->modules->ipsclass =& $this->ipsclass;
}
$member_func->member_delete_do($ids);
}
if ( count( $names_a ) )
{
$names = implode(",<br />", $names_a);
}
$log = "Members who have $period_type before ".$this->ipsclass->get_date( $period, 'LONG', 1 )." (".count($ids)."):<br /><br />Name (id, posts, $period_type)<br /><br />".$names."<br /><br />were deleted.<br /><br /><br />";
unset( $names_a );
//---------------------------------------
// Warn inactive
//---------------------------------------
$period = time()-(60*60*24*$taskinfo['autoprune_warn']);
$p_start = gmmktime(0,0,0,gmdate("m",$period),gmdate("d",$period), gmdate("Y",$period));
$p_end = gmmktime(0,0,0,gmdate("m",$period),gmdate("d",$period)+1, gmdate("Y",$period));
//---------------------------------------
// Count how much we should warn
//---------------------------------------
$this->ipsclass->DB->simple_construct( array( 'select' => "count(*) as warn_count",
'from' => 'members',
'where' => "posts <= $posts AND $period_type BETWEEN $p_start AND $p_end ".$q,
'order' => 'id ASC', ) );
$this->ipsclass->DB->simple_exec();
$tmp += $this->ipsclass->DB->fetch_row();
//---------------------------------------
// Get members to warn
//---------------------------------------
$this->ipsclass->DB->simple_construct( array( 'select' => "id, name, members_display_name, $period_type, email, posts",
'from' => 'members',
'where' => "posts <= $posts AND $period_type BETWEEN $p_start AND $p_end ".$q,
'order' => 'id ASC',
'limit' => array( intval( $this->ipsclass->cache['autoprune']['total_warn'] ), $max_per_run ) ) );
$outer = $this->ipsclass->DB->simple_exec();
$names = "";
$names_a = array();
while ( $r = $this->ipsclass->DB->fetch_row( $outer ) )
{
$replace = array( $r['members_display_name'],
$taskinfo['autoprune_posts']+1,
$taskinfo['autoprune_warn'],
$taskinfo['autoprune_delete'],
$board_name, $board_url, );
$title = preg_replace( $search, $replace, $taskinfo['autoprune_title'] );
$message = preg_replace( $search, $replace, $taskinfo['autoprune_message'] );
$this->ipsclass->DB->do_insert( 'mail_queue', array( 'mail_date ' => time(),
'mail_to' => $r['email'],
'mail_from' => $this->ipsclass->vars['email_in'],
'mail_subject' => $title,
'mail_content' => $message, ) );
$names_a[] = $r['members_display_name']." (".$r['name'].", ".$r['id'].", ".$r['posts'].", ".$this->ipsclass->get_date( $r[$period_type], 'LONG', 1 ).")";
}
if (!empty($names_a))
{
$names = implode(",<br />", $names_a);
}
$log .= "Members who have $period_type on ".date("d M Y",$period)." (".count($names_a)."):<br /><br />Name (id, posts, $period_type)<br /><br />".$names."<br /><br />were warned via e-mail.";
$warn_times = ceil( $tmp['warn_count'] / $max_per_run );
$delete_times = ceil( intval( $this->ipsclass->cache['autoprune']['total_del'] ) / $max_per_run );
$max_times = ( $delete_times > $warn_times ) ? $delete_times : $warn_times;
$this->ipsclass->cache['autoprune']['total_warn'] += $max_per_run;
$this->ipsclass->cache['autoprune']['visit_times'] += 1;
$log .= "<br /><br />".$this->ipsclass->cache['autoprune']['visit_times']." / ".$max_times." completed";
//Took decision - finish or revisit
if ( $tmp['del_count'] < $max_per_run and $this->ipsclass->cache['autoprune']['total_warn'] >= $tmp['warn_count'] )
{
$this->ipsclass->cache['autoprune']['total_warn'] = 0;
$this->ipsclass->cache['autoprune']['total_del'] = 0;
$this->ipsclass->cache['autoprune']['visit_times'] = 0;
}
else // revisit 1 minute later
{
$this->ipsclass->DB->simple_construct( array( 'update' => 'task_manager', 'set' => 'task_next_run = '.( time()+60 ), 'where' => "task_key = 'autoprune'" ) );
$this->ipsclass->DB->simple_exec();
// update caches (from task_functions.php)
$this_task = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'task_next_run', 'from' => 'task_manager', 'where' => 'task_enabled = 1', 'order' => 'task_next_run ASC', 'limit' => array(0,1) ) );
if ( ! $this_task['task_next_run'] )
{
//-----------------------------------------
// Fail safe...
//-----------------------------------------
$this_task['task_next_run'] = time() + 3600;
}
$cache_array = array();
$cache = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'cache_store', 'where' => "cs_key='systemvars'" ) );
$cache_array = unserialize(stripslashes($cache['cs_value']));
$cache_array['task_next_run'] = $this_task['task_next_run'];
$this->ipsclass->DB->do_update( 'cache_store', array( 'cs_value' => addslashes(serialize($cache_array)) ), "cs_key='systemvars'" );
}
$this->ipsclass->update_cache( array( 'name' => 'autoprune', 'array' => 1, 'deletefirst' => 1 ) );
//---------------------------------------
// Log to log table - modify but dont delete
//---------------------------------------
$this->class->append_task_log( $this->task, $log );
//---------------------------------------
// Send log to admin - modify but dont delete
//---------------------------------------
if ( $taskinfo['autoprune_log'] )
{
require_once $this->root_path.'sources/classes/class_email.php';
$email = new emailer;
$email->ipsclass =& $this->ipsclass;
$email->email_init();
$log = eregi_replace("<br />", "\n", $log);
$email->from = $this->ipsclass->vars['email_in'];
$email->to = $this->ipsclass->vars['email_in'];
$email->message = "Hello Mr. Admin Sir!\n\n".$log;
$email->subject = "Prune Inactive Log";
$email->send_mail();
}
//-----------------------------------------
// Unlock Task: DO NOT MODIFY!
//-----------------------------------------
$this->class->unlock_task( $this->task );
}
/*-------------------------------------------------------------------------*/
// register_class
// LEAVE ALONE
/*-------------------------------------------------------------------------*/
function register_class(&$class)
{
$this->class = $class;
$this->ipsclass =& $class->ipsclass;
$this->root_path = $this->class->root_path;
}
/*-------------------------------------------------------------------------*/
// pass_task
// LEAVE ALONE
/*-------------------------------------------------------------------------*/
function pass_task( $this_task )
{
$this->task = $this_task;
}
}
?>