09 Better Cleanup System By X0r

Started by Mindless, July 21, 2012, 09:11:38 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Daniel

#2
i get his error not sure whats the problem is


Parse error: syntax error, unexpected T_VARIABLE in /var/www/admin/docleanup.php on line 11
$lang = array_merge( $lang, load_language('ad_docleanup') );

i also had to remove these line before the code in cleanup.php was error free nothing else on cleanup.php have error only the lines below


<!-- Your clean up code to run every xx mins/hours here -->
<!-- Your clean up code to run every xx mins/hours here -->

Mindless

Credits to x0r

Sql :

Code (sql) Select
INSERT INTO `avps` VALUES ('lastslowcleantime', '', 0, 0);
INSERT INTO `avps` VALUES ('lastslowcleantime2', '', 0, 0);
INSERT INTO `avps` VALUES ('lastoptimizedbtime', '', 0, 0);


@File include/config.php add :

Code (php) Select

$TBDEV['autoslowclean_interval'] = 28800;
$TBDEV['autoslowclean_interval2'] = 57600;
$TBDEV['optimizedb_interval'] = 172800;


Those intervals can be adjusted to what you wish and the default autoclean define you have stays as it is same with the sql only the new ones you need to add.

@File include/bittorrent.php find :

Code (php) Select
require_once("cleanup.php");

Delete that line - cleanup does not need to be included as it will be included within the new autoclean function

Next find :

Code (php) Select
function autoclean() {
    global $TBDEV;

    $now = time();
    //$docleanup = 0;

    $res = mysql_query("SELECT value_u FROM avps WHERE arg = 'lastcleantime'");
    $row = mysql_fetch_array($res);
    if (!$row) {
        mysql_query("INSERT INTO avps (arg, value_u) VALUES ('lastcleantime',$now)");
        return;
    }
    $ts = $row[0];
    if ($ts + $TBDEV['autoclean_interval'] > $now)
        return;
    mysql_query("UPDATE avps SET value_u=$now WHERE arg='lastcleantime' AND value_u = $ts");
    if (!mysql_affected_rows())
        return;

   
    docleanup();
}


Replace it with :

Code (php) Select
  function autoclean()
  {
global $TBDEV;
$now = time();
/* Better cleanup function with db-optimization and slow clean by x0r @ tbdev.net */
$w00p = mysql_query("SELECT arg, value_u FROM avps") or sqlerr(__FILE__, __LINE__);
while ($row = mysql_fetch_assoc($w00p))
{
if ($row['arg'] == "lastcleantime" && ($row['value_u'] + $TBDEV['autoclean_interval']) < $now)
{
mysql_query("UPDATE avps SET value_u = '$now' WHERE arg = 'lastcleantime'") or sqlerr(__FILE__, __LINE__);
        require_once("./include/cleanup.php");
        docleanup();
        }
else if ($row['arg'] == "lastslowcleantime" && ($row['value_u'] + $TBDEV['autoslowclean_interval']) < $now)
{
mysql_query("UPDATE avps SET value_u = '$now' WHERE arg = 'lastslowcleantime'") or sqlerr(__FILE__, __LINE__);
require_once("./include/cleanup.php");
doslowcleanup();
}
else if ($row['arg'] == "lastslowcleantime2" && ($row['value_u'] + $TBDEV['autoslowclean_interval2']) < $now)
{
mysql_query("UPDATE avps SET value_u = '$now' WHERE arg = 'lastslowcleantime2'") or sqlerr(__FILE__, __LINE__);
require_once("./include/cleanup.php");
doslowcleanup2();
}
else if ($row['arg'] == "lastoptimizedbtime" && ($row['value_u'] + $TBDEV['optimizedb_interval']) < $now)
{
mysql_query("UPDATE avps SET value_u = '$now' WHERE arg = 'lastoptimizedbtime'") or sqlerr(__FILE__, __LINE__);
require_once("./include/cleanup.php");
dooptimizedb();
}
}
mysql_free_result($w00p);
return;
  }


Now onto cleanup.php this will replace your old cleanup.php once you have all your code within the 2 sections autoclean and slowautoclean - adding more is simple  - This part is fairly straight forward - First section is autoclean which runs every 15 mins so add the code you need to be running within that section - it ends with write_log so you know to add the code between there - i've added a line to show where your code will go in each section :

Code (php) Select
<?php
/*
+------------------------------------------------
|   $Date$
|   $Revision$ 1.0
|   $Author$ x0r/Bigjoos
|   $URL$ 
+------------------------------------------------
*/
require_once("bittorrent.php");

function 
deadtime() {
    global 
$TBDEV;
    return 
time() - floor($TBDEV['announce_interval'] * 1.3);
}

function 
docleanup() {
global $TBDEV;
   
set_time_limit(1200);
   
$result = @mysql_query("show processlist") or sqlerr(__FILE____LINE__);
   while (
$row mysql_fetch_array($result)) {
   if ((
$row["Time"] > 100) || ($row["Command"] == "Sleep")) {
   
$sql "kill " $row["Id"] . "";
   @
sql_query($sql) or sqlerr(__FILE____LINE__);
   }
   }
   
ignore_user_abort(1);

do {
$res = @mysql_query("SELECT id FROM torrents");
$ar = array();
while ($row mysql_fetch_array($res,MYSQL_NUM)) {
$id $row[0];
$ar[$id] = 1;
}

if (!count($ar))
break;

$dp = @opendir($TBDEV['torrent_dir']);
if (!$dp)
break;

$ar2 = array();
while (($file readdir($dp)) !== false) {
if (!preg_match('/^(\d+)\.torrent$/'$file$m))
continue;
$id $m[1];
$ar2[$id] = 1;
if (isset($ar[$id]) && $ar[$id])
continue;
$ff $TBDEV['torrent_dir'] . "/$file";
unlink($ff);
}
closedir($dp);

if (!count($ar2))
break;

$delids = array();
foreach (array_keys($ar) as $k) {
if (isset($ar2[$k]) && $ar2[$k])
continue;
$delids[] = $k;
unset($ar[$k]);
}
if (count($delids))
@mysql_query("DELETE FROM torrents WHERE id IN (" join(","$delids) . ")");

$res = @mysql_query("SELECT torrent FROM peers GROUP BY torrent");
$delids = array();
while ($row mysql_fetch_array($res,MYSQL_NUM)) {
$id $row[0];
if (isset($ar[$id]) && $ar[$id])
continue;
$delids[] = $id;
}
if (count($delids))
@mysql_query("DELETE FROM peers WHERE torrent IN (" join(","$delids) . ")");

$res = @mysql_query("SELECT torrent FROM files GROUP BY torrent");
$delids = array();
while ($row mysql_fetch_array($res,MYSQL_NUM)) {
$id $row[0];
if (isset($ar[$id]) && $ar[$id])
continue;
$delids[] = $id;
}
if (count($delids))
@mysql_query("DELETE FROM files WHERE torrent IN (" join(","$delids) . ")");
} while (0);

$deadtime deadtime();
@mysql_query("DELETE FROM peers WHERE last_action < $deadtime");

$deadtime -= $TBDEV['max_dead_torrent_time'];
@mysql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < $deadtime");

$deadtime time() - $TBDEV['signup_timeout'];
@mysql_query("DELETE FROM users WHERE status = 'pending' AND added < $deadtime AND last_login < $deadtime AND last_access < $deadtime");

$torrents = array();
$res = @mysql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder");
while ($row mysql_fetch_assoc($res)) {
if ($row["seeder"] == "yes")
$key "seeders";
else
$key "leechers";
$torrents[$row["torrent"]][$key] = $row["c"];
}

$res = @mysql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent");
while ($row mysql_fetch_assoc($res)) {
$torrents[$row["torrent"]]["comments"] = $row["c"];
}

$fields explode(":""comments:leechers:seeders");
$res = @mysql_query("SELECT id, seeders, leechers, comments FROM torrents");
while ($row mysql_fetch_assoc($res)) {
$id $row["id"];
if(isset($torrents[$id]))
$torr $torrents[$id];
foreach ($fields as $field) {
if (!isset($torr[$field]))
$torr[$field] = 0;
}
$update = array();
foreach ($fields as $field) {
if ($torr[$field] != $row[$field])
$update[] = "$field = " $torr[$field];
}
if (count($update))
@mysql_query("UPDATE torrents SET " implode(","$update) . " WHERE id = $id");
}

  <!-- 
Your clean up code to run every 15 mins here -->
   
   
write_log("Autoclean-------------------- Auto cleanup Complete using --------------------");
   }
    
  function 
doslowcleanup()
  {
    global 
$TBDEV;
    
set_time_limit(1200);
    
$result = @mysql_query("show processlist") or sqlerr(__FILE____LINE__);
    while (
$row mysql_fetch_array($result)) {
    if ((
$row["Time"] > 100) || ($row["Command"] == "Sleep")) {
    
$sql "kill " $row["Id"] . "";
    @
mysql_query($sql) or sqlerr(__FILE____LINE__);
    }
    }
    
ignore_user_abort(1);
  
  <!-- 
Your clean up code to run every xx mins/hours here -->
  
  
write_log("Slowautoclean -------------------- Delayed cleanup Complete --------------------");
  }

  function 
doslowcleanup2()
  {
    global 
$TBDEV;
    
set_time_limit(1200);
    
$result = @mysql_query("show processlist") or sqlerr(__FILE____LINE__);
    while (
$row mysql_fetch_array($result)) {
    if ((
$row["Time"] > 100) || ($row["Command"] == "Sleep")) {
    
$sql "kill " $row["Id"] . "";
    @
mysql_query($sql) or sqlerr(__FILE____LINE__);
    }
    }
    
ignore_user_abort(1);
  
  <!-- 
Your clean up code to run every xx mins/hours here -->
  
  
write_log("Slowautoclean2 -------------------- Delayed cleanup 2 Complete --------------------");
  }

  function 
dooptimizedb()
  {
  global 
$TBDEV;
  
set_time_limit(1200);
  
$result = @mysql_query("show processlist") or sqlerr(__FILE____LINE__);
  while (
$row mysql_fetch_array($result)) {
  if ((
$row["Time"] > 100) || ($row["Command"] == "Sleep")) {
  
$sql "kill " $row["Id"] . "";
  @
mysql_query($sql) or sqlerr(__FILE____LINE__);
  }
  }
  
ignore_user_abort(1);
  
$alltables = @mysql_query("SHOW TABLES") or sqlerr(__FILE____LINE__);
  while (
$table mysql_fetch_assoc($alltables)) {
  foreach (
$table as $db => $tablename) {
  
$sql "OPTIMIZE TABLE $tablename";
  
/* Preg match the sql incase it was hijacked somewhere!(will use CHECK|ANALYZE|REPAIR|later) */
  
if (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]TABLE[[:space:]]' $tablename '$@i'$sql))
   @
mysql_query($sql) or die("<b>Something was not right!</b>.\n<br />Query: " $sql "<br />\nError: (" mysql_errno() . ") " safeChar(mysql_error()));
   }
   }
   @
mysql_free_result($alltables);
   
write_log("Auto-optimizedb--------------------Auto Optimization Complete--------------------");
   }
?>


Save and upload docleanup.php to admin folder :

Code (php) Select
<?php
if ( ! defined'IN_TBDEV_ADMIN' ) )
{
print "<h1>{$lang['text_incorrect']}</h1>{$lang['text_cannot']}";
exit();
}

require_once(
"include/user_functions.php");
require_once(
"include/html_functions.php")

$lang array_merge$langload_language('ad_docleanup') );

if (
$CURUSER['class'] < UC_SYSOP)
header"Location: {$TBDEV['baseurl']}/index.php");

$HTMLOUT ='';

function 
calctime($val)
{
    
$days intval($val 86400);
    
$val -= $days 86400;
    
$hours intval($val 3600);
    
$val -= $hours 3600;
    
$mins intval($val 60);
    
$secs $val - ($mins 60);
    return 
$days " Days, " $hours " Hours, " $mins " Minutes, " $secs " Seconds";
}

if (!
function_exists('memory_get_usage')) {
    function 
memory_get_usage()
    {
        if (
substr(PHP_OS03) == 'WIN') {
            if (
substr(PHP_OS03) == 'WIN') {
                
$output = array();
                
exec('tasklist /FI "PID eq ' getmypid() . '" /FO LIST'$output);

                return 
preg_replace('/[\D]/'''$output[5]) * 1024;
            }
        } else {
            
$pid getmypid();
            
exec("ps -eo%mem,rss,pid | grep $pid"$output);
            
$output explode(" "$output[0]);
            return 
$output[1] * 1024;
        }
    }
}

$HTMLOUT .= begin_main_frame('Cleanups');
$HTMLOUT .= begin_table();

$HTMLOUT .="<tr><td class='colhead'>Cleanup Name</td>
<td class='colhead'>Last Run</td>
<td class='colhead'>Runs every</td>
<td class='colhead'>Scheduled to run</td>
</tr>"
;

$res mysql_query("SELECT arg, value_u FROM avps");
while (
$arr mysql_fetch_assoc($res)) {
    switch (
$arr['arg']) {
        case 
'lastcleantime'$arg $TBDEV['autoclean_interval'];
            break;
        case 
'lastslowcleantime'$arg $TBDEV['autoslowclean_interval'];
            break;
        case 
'lastslowcleantime2'$arg $TBDEV['autoslowclean_interval2'];
            break;
        case 
'lastoptimizedbtime'$arg $TBDEV['optimizedb_interval'];
            break;
    }

    
$HTMLOUT .="<tr><td>".$arr['arg']."</td>
    <td>"
.get_date($arr['value_u'], 'DATE',1,0) . " (" .get_date($arr['value_u'], 'DATE',1,0) . ")</td>
    <td>" 
calctime($arg) . "</td>
    <td>" 
calctime($arr['value_u'] - (time() - $arg)) . "</td>
    </tr>"
;
}
$HTMLOUT .= end_table();


$HTMLOUT .="<form action='admin.php?action=docleanup' method='post'>
<table align='center'>
<tr>
<td class='table'>
<input type='checkbox' name='docleanup' />Do Cleanup
&nbsp;&nbsp;&nbsp;&nbsp;
<input type='checkbox' name='doslowcleanup' />Do Slow Cleanup
&nbsp;&nbsp;&nbsp;&nbsp;
<input type='checkbox' name='doslowcleanup2' />Do Slow Cleanup 2
&nbsp;&nbsp;&nbsp;&nbsp;
<input type='checkbox' name='dooptimization' />Do Optimization
<input type='submit' value='Submit' />
</td></tr></table>
</form>"
;

$now time();
if (isset(
$_POST['docleanup'])) {
    
mysql_query("UPDATE avps SET value_u = " sqlesc($now) . " WHERE arg = 'lastcleantime'") or sqlerr(__FILE____LINE__);
    require_once(
"./include/cleanup.php");
    
docleanup();
    
header('Refresh: 2; url='.$TBDEV['baseurl'].'/admin.php?action=docleanup');
    
$HTMLOUT .="<br /><h1>Cleanup Done</h1>";
}

if (isset(
$_POST['doslowcleanup'])) {
    
mysql_query("UPDATE avps SET value_u = " sqlesc($now) . " WHERE arg = 'lastslowcleantime'") or sqlerr(__FILE____LINE__);
    require_once(
"./include/cleanup.php");
    
doslowcleanup();
    
header('Refresh: 2; url='.$TBDEV['baseurl'].'/admin.php?action=docleanup');
   
$HTMLOUT .="<br /><h1>Slow Cleanup Done</h1>";
}

if (isset(
$_POST['doslowcleanup2'])) {
    
mysql_query("UPDATE avps SET value_u = " sqlesc($now) . " WHERE arg = 'lastslowcleantime2'") or sqlerr(__FILE____LINE__);
    require_once(
"./include/cleanup.php");
    
doslowcleanup2();
    
header('Refresh: 2; url='.$TBDEV['baseurl'].'/admin.php?action=docleanup');
   
$HTMLOUT .="<br /><h1>Slow Cleanup 2 Done</h1>";
}

if (isset(
$_POST['dooptimization'])) {
    
mysql_query("UPDATE avps SET value_u = " sqlesc($now) . " WHERE arg = 'lastoptimizedbtime'") or sqlerr(__FILE____LINE__);
    require_once(
"./include/cleanup.php");
    
dooptimizedb();
    
header('Refresh: 2; url='.$TBDEV['baseurl'].'/admin.php?action=docleanup');
    
$HTMLOUT .="<br /><h1>Optimization Done</h1>";
}

$HTMLOUT .="Memory usage:" memory_get_usage() . "<br /><br />";
$HTMLOUT .= end_main_frame();
print 
stdhead('Doclean Up') . $HTMLOUT stdfoot();
?>


Save and upload lang_ad_docleanup.php to /lang/en :

Code (php) Select
<?php

$lang 
= array(

#Misc
'stderr_error' => "Error",
'stderr_cleanup' => "Cleanup",

#Texts
'text_incorrect' => "Incorrect access",
'text_cannot' => "You cannot access this file directly.",
'text_denied' => "Permission denied !",
'text_done' => " Successfully done."
);

?>


@File admin.php add :

Code (php) Select
'docleanup'  => 'docleanup',

@File admin/index.php add :

Code (php) Select
<span class='btn'><a href='admin.php?action=docleanup'>{$lang['index_mcleanup']}</a></span>

@File lang/en/lang_ad_index.php add :

Code (php) Select
'index_mcleanup' => 'Manual cleanup',

Thats all - post on problems, improvements ect