<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: core.class.php 39 2011-03-25 02:48:43Z along $
#
#	Copyright (C) 2008-2011 PHPDisk Team. All Rights Reserved.
#
*/
!defined('IN_PHPDISK') && exit('[PHPDisk] Access Denied');

class phpdisk_core{
	function init_core(){
		phpdisk_core::__init_env();
		phpdisk_core::__gzcompress_open();
	}
	function init_db_connect(){
		global $C,$configs;
		if(empty($C['dbc'])){
			$C['dbc'] = new cls_mysql();
			$C['dbc']->connect($configs['dbhost'],$configs['dbuser'],$configs['dbpasswd'],$configs['dbname'],$configs['pconnect']);
			unset($configs['dbhost'],$configs['dbuser'],$configs['dbpasswd'],$configs['pconnect']);
		}
		return $C['dbc'];
	}
	function __init_env(){
		$env_arr = array('system/','system/logs/','system/cache/','system/plugins/','system/logs/admincp/','system/templates/');
		for ($i=0;$i<count($env_arr);$i++){
			if(!is_dir(PHPDISK_ROOT.$env_arr[$i])){
				mkdir(PHPDISK_ROOT.$env_arr[$i],0777);
				write_file(PHPDISK_ROOT.$env_arr[$i].'index.htm','PHPDisk');
			}
		}
	}
	function __gzcompress_open(){
		global $C;
		if ($C['gz']['open'] && function_exists('ob_gzhandler')) {
			ob_start('ob_gzhandler');
			$C['gz']['status'] = 'Enabled';
		} else {
			ob_start();
			$C['gz']['status'] = 'Disabled';
		}
	}
	function init_lang_tpl(){
		global $db,$tpf,$C,$settings;
		$tpl_name = @$db->result_first("select tpl_name from {$tpf}templates where tpl_type='user' and actived=1");
		$user_tpl_dir = $tpl_name ? "templates/$tpl_name/" : 'templates/default/';
		$tpl_name = @$db->result_first("select tpl_name from {$tpf}templates where tpl_type='admin' and actived=1");
		$admin_tpl_dir = $tpl_name ? "templates/$tpl_name/" : 'templates/admin/';

		if($settings['open_switch_langs']){
			$mylang = gpc('mylang','C','');
			$q = $db->query("select * from {$tpf}langs order by actived desc");
			while($rs = $db->fetch_array($q)){
				$langs_str .= $rs['lang_name'].',';
			}
			$db->free($q);
			unset($rs);
			if($langs_str){
				$langs_arr = explode(',',substr($langs_str,0,-1));
			}
			$uri = $_SERVER['REQUEST_URI'];
			$t_arr = explode('?lang=',$uri);
			if(in_array($t_arr[1],$langs_arr)){
				pd_setcookie('mylang',$t_arr[1]);
				$mylang = $t_arr[1];
			}
			$lang_name = $mylang ? $mylang : 'zh-cn';
		}else{
			$lang_name = @$db->result_first("select lang_name from {$tpf}langs where actived=1");
			$lang_name = $lang_name ? $lang_name : 'zh-cn';
		}
		unset($tpl_name);
		return array('user_tpl_dir'=>$user_tpl_dir,'admin_tpl_dir'=>$admin_tpl_dir,'lang_name'=>$lang_name);
	}
	function user_login(){
		global $pd_uid,$pd_pwd;
		if(!$pd_uid || !$pd_pwd){
			header("Location: ".urr("account","action=login&ref=".$_SERVER['REQUEST_URI']));
		}
	}
	function admin_login(){
		global $C,$db,$tpf,$pd_uid,$pd_pwd,$pd_gid;
		$admin_not_login =0;
		$rs = $db->fetch_one_array("select * from {$tpf}adminsession where userid='$pd_uid' limit 1");
		if(!$_SESSION['pd_sid'] || $rs['hashcode'] != $_SESSION['pd_sid']){
			$admin_not_login =1;
		}
		unset($rs);
		if(!$pd_uid || !$pd_pwd || $pd_gid !=1 || $admin_not_login){
			header("Location: ".urr("account","action=adminlogin&ref=".$_SERVER['REQUEST_URI']));
			exit;
		}
	}

}

?>