<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
*Пользователей: 
*Размер базы данных: 
*Сервер базы данных: 
*Размер кэша: 
Очистить кэш
*/
if ($_SERVER['REQUEST_METHOD']=="POST")
{
	User::checkToken();
	switch($_POST['action'])
	{
		case('purge_cache'):
			LabCMS::i()->reset_cache('all');
			refresh();
		break;
		case('purge_cache_parsed'):
			LabCMS::i()->reset_cache('parsed');
			refresh();
		break;
		case('recompile'):
			forceCompileAllTemplates();
			LabCMS::i()->reset_cache('all');
			refresh();
		break;
	}
}

function forceCompileAllTemplates()
{
	$skins=glob(LAB_PATH_TEMPLATE.'/*', GLOB_ONLYDIR);
	ob_implicit_flush(true);
	ob_end_flush();
	foreach($skins as $skin)
	{
		Tpl::i()->compile_id=basename($skin);
		Tpl::i()->setTemplateDir($skin);
		Tpl::i()->compileAllTemplates('.tpl', true, 0, 1);
	}
}

LabCMS::i()->setModuleTemplate("admin.tpl");
LabCMS::i()->setAllowFileUpload();

$admin_info=array();

list($html['size'], $html['files'])=get_dir_info(LAB_PATH_DATA.'/cache');
list($obj['size'], $obj['files'])=get_dir_info(LAB_PATH_DATA.'/objects');
list($ext['size'], $ext['files'])=get_dir_info(LAB_PATH_DATA.'/external');

$admin_info['cache_files']=intval($obj['files']+$html['files']+$ext['files']);
$admin_info['cache_dir_size']=intval($obj['size']+$html['size']+$ext['size']);

if (LabConfig::$cfg['cache']
    && (LabConfig::$cfg['use_memcached']
	|| LabConfig::$cfg['use_memcached_db']
	|| LabConfig::$cfg['use_memcached_tpl'])
    && !LabConfig::$cfg['debug_mode'])
{
	$b=Cache_Memcache::i()->getStats();
	$admin_info['cache_memcached']=get_formatted_filesize($b['bytes']);
}

//не учитываем количество гостей и удаленных пользователей
$admin_info['total_users'] = (Db::i()->selectCell('SELECT COUNT(user_id)	FROM ?_users WHERE `status`>=0 AND user_id>0'));
$admin_info['db_version'] = DEMO_MODE?'5.0.45-community-nt':mysql_get_server_info(Db::i()->link);
list($admin_info['db_size'], $admin_info['db_table_count'])=get_db_data();

$admin_info['db_size']=get_formatted_filesize($admin_info['db_size']);
$admin_info['cache_dir_size']=get_formatted_filesize($admin_info['cache_dir_size']);
include LAB_PATH_KERNEL.'/install/revision.php';
$admin_info['cms_version']=LabConfig::$cfg["version"]["current"].'.'.$revision;
$admin_info['php_version']=DEMO_MODE?'5.2.4':PHP_VERSION;

Tpl::i()->assign($admin_info);

$admin_modules=array('admin', 'admin_dumper', 'admin_groups', 'admin_modules', 'admin_pages', 'admin_redirect', 'admin_tree', 'admin_users', 'blocks_static_edit', 'lang_editor', 'admin_settings');
$admin_modules=Db::i()->selectCol("SELECT module AS ARRAY_KEY, path FROM ?_tree t JOIN ?_includes i ON t.tree_id=i.tree_id JOIN ?_modules m ON i.module_id=m.module_id WHERE module IN (?a) AND `show`=1 GROUP BY module", $admin_modules);
Tpl::i()->assign($admin_modules);

function get_db_data()
{
	$database_size=$counter=0;
	$tables=Db::i()->select("SHOW TABLE STATUS");
	foreach($tables as $table)
	{
		if (isset($table['Engine']) && ($table['Engine'] == 'MyISAM' || $table['Engine'] == 'InnoDB'))
		{
			if (strpos($table['Name'], Db::i()->_identPrefix) !== false)
			{
				$database_size += $table['Data_length'] + $table['Index_length'];
				$counter++;
			}
		}
	}
	return array($database_size, $counter);
}


function get_dir_info($dir)
{
	$dir_size=$counter=0;
	if ($hdir = @opendir($dir))
	{
		while (($file = readdir($hdir)) !== false)
		{
			if (is_file($dir.'/'.$file))
			{
				$dir_size += filesize($dir.'/'.$file);
				$counter++;
			}
		}
		closedir($hdir);
		return array($dir_size, $counter);
	}
	else return false;
}