<?php

/*
	[UCenter] (C)2001-2008 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$Id: frame.php 12135 2008-01-14 03:47:01Z heyond $
*/

!defined('IN_UC') && exit('Access Denied');

class control extends adminbase {

	var $members;
	var $apps;
	var $friends;

	function control() {
		$this->adminbase();
	}

	function onindex() {
		$mainurl = getgpc('mainurl');
		$mainurl = !empty($mainurl) && preg_match("/^admin\.php\?(&*\w+=\w+)*$/i", $mainurl) ? $mainurl : 'admin.php?m=frame&a=main';
		$this->view->assign('mainurl', $mainurl);
		$this->view->display('admin_frame_index');
	}

	function onmain() {
		//header('location: '.UC_API.'/admin.php?m=setting&a=ls');
		$ucinfo = '<sc'.'ript language="Jav'.'aScript" src="ht'.'tp:/'.'/cus'.'tome'.'r.disc'.'uz.n'.'et/ucn'.'ews'.'.p'.'hp?'.$this->_get_uc_info().'"></s'.'cri'.'pt>';
		$this->view->assign('ucinfo', $ucinfo);

		//note 统计信息
		$members = $this->_get_uc_members();
		$applist = $this->_get_uc_apps();
		$notes = $this->_get_uc_notes();
		$pms = $this->_get_uc_pms();
		$apps = count($applist);
		$friends = $this->_get_uc_friends();
		$this->view->assign('members', $members);
		$this->view->assign('applist', $applist);
		$this->view->assign('apps', $apps);
		$this->view->assign('friends', $friends);
		$this->view->assign('notes', $notes);
		$this->view->assign('pms', $pms);

		//note 系统信息
		$serverinfo = PHP_OS.' / PHP v'.PHP_VERSION;
		$serverinfo .= @ini_get('safe_mode') ? ' Safe Mode' : NULL;
		$dbversion = $this->db->result_first("SELECT VERSION()");
		$fileupload = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : '<font color="red">'.$lang['no'].'</font>';
		$dbsize = 0;
		$tablepre = UC_DBTABLEPRE;
		$query = $tables = $this->db->fetch_all("SHOW TABLE STATUS LIKE '$tablepre%'");
		foreach($tables as $table) {
			$dbsize += $table['Data_length'] + $table['Index_length'];
		}
		$dbsize = $dbsize ? $this->_sizecount($dbsize) : $lang['unknown'];
		$dbversion = $this->db->version();
		$magic_quote_gpc = get_magic_quotes_gpc() ? 'On' : 'Off';
		$this->view->assign('serverinfo', $serverinfo);
		$this->view->assign('fileupload', $fileupload);
		$this->view->assign('dbsize', $dbsize);
		$this->view->assign('dbversion', $dbversion);
		$this->view->assign('magic_quote_gpc', $magic_quote_gpc);

		//note 开发团队

		$this->view->display('admin_frame_main');
	}

	function onmenu() {
		$this->view->display('admin_frame_menu');
	}

	function onheader() {
		$this->load('app');
		$applist = $_ENV['app']->get_apps();
		$cparray = array(
			'UCHOME' => 'admincp.php',
			'DISCUZ' => 'admincp.php',
			'SUPESITE' => 'admincp.php',
			'XSPACE' => 'admincp.php',
			'SUPEV' => 'admincp.php',
			'ECSHOP' => 'admin/index.php',
			'ECMALL' => 'admin.php'
		);
		$admincp = '';
		if(is_array($applist)) {
			foreach($applist AS $k => $app) {
				if(isset($cparray[$app['type']])) {
					$admincp .= '<li><a href="'.(substr($app['url'], -1) == '/' ? $app['url'] : $app['url'].'/').$cparray[$app['type']].'" target="_blank">'.$app['name'].'</a></li>';
				}
			}
		}
		$this->view->assign('admincp', $admincp);
		$this->view->assign('username', $this->user['username']);
		$this->view->display('admin_frame_header');
	}

	function _get_uc_members() {
		if(!$this->members) {
			$this->members = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."members");
		}
		return $this->members;
	}

	function _get_uc_friends() {
		$friends = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."friends");
		return $friends;
	}

	function _get_uc_apps() {
		if(!$this->apps) {
			$this->apps = $this->db->fetch_all("SELECT * FROM ".UC_DBTABLEPRE."applications");
		}
		return $this->apps;
	}
	function _get_uc_pms() {
		$pms = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pms");
		return $pms;
	}

	function _get_uc_notes() {
		$notes = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."notelist WHERE closed='0'");
		return $notes;
	}

	/**
	* 格式化附件单位
	* @param $filesize - 文件大小(字节)
	* @return 返回格式化后的文本
	*/
	function _sizecount($filesize) {
		if($filesize >= 1073741824) {
			$filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
		} elseif($filesize >= 1048576) {
			$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
		} elseif($filesize >= 1024) {
			$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
		} else {
			$filesize = $filesize . ' Bytes';
		}
		return $filesize;
	}

	function _get_uc_info() {
		$update = array('uniqueid' => UC_SITEID, 'version' => UC_VERSION, 'release' => UC_RELEASE, 'php' => PHP_VERSION, 'mysql' => $this->db->version(), 'charset' => UC_CHARSET);
		$updatetime = @filemtime(UC_ROOT.'./data/updatetime.lock');
		if(empty($updatetime) || ($this->time - $updatetime > 3600 * 4)) {
			@touch(UC_ROOT.'./data/updatetime.lock');
			$update['members'] = $this->_get_uc_members();
			$update['friends'] = $this->_get_uc_friends();
			$apps = $this->_get_uc_apps();
			if($apps) {
				foreach($apps as $app) {
					$update['app_'.$app['appid']] = $app['name']."\t".$app['url']."\t".$app['type'];
				}
			}
		}

		$data = '';
		foreach($update as $key => $value) {
			$data .= $key.'='.rawurlencode($value).'&';
		}

		return 'update='.rawurlencode(base64_encode($data)).'&md5hash='.substr(md5($_SERVER['HTTP_USER_AGENT'].implode('', $update).$this->time), 8, 8).'&timestamp='.$this->time;
	}

}

?>