<?php
/**
*
* @package adm
* @copyright (c) 2007 Kleeja.com
* @license ./docs/license.txt
*
*/


// not for directly open
if (!defined('IN_ADMIN'))
{
	exit();
}


#turn time-limit off
@set_time_limit(0);

#get current case
$case = false;
if(ig('case'))
{
	$case = g('case');
}


#set form ket
$GET_FORM_KEY = kleeja_add_form_key_get('REPAIR_FORM_KEY');


//check _GET Csrf token
if($case && in_array($case, array('clearc', 'sync_files', 'sync_images', 'sync_users', 'tables', 'sync_sizes', 'status_file')))
{
	if(!kleeja_check_form_key_get('REPAIR_FORM_KEY'))
	{
		kleeja_admin_err($lang['INVALID_GET_KEY'], true, $lang['ERROR'], true, basename(ADMIN_PATH), 2);
	}
}

switch($case):

default:

# Get real number from database right now
$all_files = get_actual_stats('files');
$all_images = get_actual_stats('imgs');
$all_users = get_actual_stats('users');
$all_sizes = readable_size(get_actual_stats('sizes'));


#links
$del_cache_link		= basename(ADMIN_PATH) . '?cp=r_repair&amp;case=clearc&amp;' . $GET_FORM_KEY;
$resync_files_link	= $config['siteurl'] . 'go.php?go=resync&amp;case=sync_files';
$resync_images_link	= $config['siteurl'] . 'go.php?go=resync&amp;case=sync_images';
$resync_users_link	= basename(ADMIN_PATH) . '?cp=r_repair&amp;case=sync_users&amp;' . $GET_FORM_KEY;
$resync_sizes_link	= basename(ADMIN_PATH) . '?cp=r_repair&amp;case=sync_sizes&amp;' . $GET_FORM_KEY;
$repair_tables_link	= basename(ADMIN_PATH) . '?cp=r_repair&amp;case=tables&amp;' . $GET_FORM_KEY;

$queue_cron_job_url = $config['siteurl'] . 'go.php?go=queue';

$stylee = "admin_repair";

break;


//
//fix tables ..
//
case 'tables':

$query	= "SHOW TABLE STATUS";
$result	= $SQL->query($query);
$text = '';
	
while($row=$SQL->fetch_array($result))
{
	$queryf	=	"REPAIR TABLE `" . $row['Name'] . "`";
	$resultf = $SQL->query($queryf);
	if ($resultf)
	{
		$text .= '<li>' . $lang['REPAIRE_TABLE'] . $row['Name'] . '</li>';
	}
}
	
$SQL->freeresult($result);

$text .= '<script type="text/javascript"> setTimeout("get_kleeja_link(\'' . basename(ADMIN_PATH) . '?cp=r_repair' .  '\');", 2000);</script>' . "\n";
$stylee = 'admin_info';


break;

//
//re-sync sizes ..
//
case 'sync_sizes':


$query_s	= array(
					'SELECT'	=> 'size',
					'FROM'		=> "{$dbprefix}files"
				);

$result_s = $SQL->build($query_s);

$files_number = $files_sizes = 0;

while($row=$SQL->fetch_array($result_s))
{
	$files_number++;
	$files_sizes = $files_sizes+$row['size'];
}

$SQL->freeresult($result_s);

$update_query	= array(
						'UPDATE'	=> "{$dbprefix}stats",
						'SET'		=> "files=" . $files_number . ", sizes=" . $files_sizes
					);

if ($SQL->build($update_query))
{
	$text .= '<li>' . $lang['REPAIRE_F_STAT'] . '</li>';
}

delete_cache('data_stats');

$stylee = 'admin_info';

break;


//
//re-sync total users number ..
//
case 'sync_users':

$query_w	= array(
					'SELECT'	=> 'name',
					'FROM'		=> "{$dbprefix}users"
				);

$result_w = $SQL->build($query_w);
		
$user_number = 0;
while($row=$SQL->fetch_array($result_w))
{
	$user_number++;
}
	
$SQL->freeresult($result_w);

$update_query	= array(
						'UPDATE'	=> "{$dbprefix}stats",
						'SET'		=> "users=" . $user_number
					);

$result = $SQL->build($update_query);

delete_cache('data_stats');
$text = sprintf($lang['SYNCING'], $lang['USERS_ST']);
$text .= '<script type="text/javascript"> setTimeout("get_kleeja_link(\'' . basename(ADMIN_PATH) . '?cp=r_repair' .  '\');", 2000);</script>' . "\n";

$stylee = 'admin_info';


break;


//
//clear all cache ..
//
case 'clearc':

#clear cache
delete_cache('', true);

#show done, msg
$text .= '<li>' . $lang['REPAIRE_CACHE'] . '</li>';
$text .= '<script type="text/javascript"> setTimeout("get_kleeja_link(\'' . basename(ADMIN_PATH) . '?cp=r_repair' .  '\');", 2000);</script>' . "\n";

$stylee = 'admin_info';

break;

endswitch;




