<?php 
##
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: cache.func.php 25 2011-03-04 07:36:51Z along $
#
#	Copyright (C) 2008-2011 PHPDisk Team. All Rights Reserved.
#
##

if(!defined('IN_PHPDISK')) {
	exit('[PHPDisk] Access Denied');
}
function stats_cache($arr=0){
	global $db,$tpf;
	if(is_array($arr)){
		foreach($arr as $k => $v){
			$sqls .= "('$k','".$db->escape(trim($v))."'),";
		}
		$sqls = substr($sqls,0,-1);
		$db->query("replace into {$tpf}stats (vars,value) values $sqls;");
	}
	
	$q = $db->query("select * from {$tpf}stats order by vars ");
	while($rs = $db->fetch_array($q)){
		$str_c .= "\t'".$rs['vars']."' => '".$rs['value']."',".LF;
	}
	$db->free($q);
	unset($rs);
	
	$str = "<?php".LF.LF;
	$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
	$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
	$str .= "\$stats = array(".LF;
	$str .= $str_c;
	$str .= ");".LF.LF;
	$str .= "?>".LF;
	
	write_file(PHPDISK_ROOT."./system/stats.inc.php",$str);

}
function settings_cache($arr=0){
	global $db,$tpf,$configs,$settings;
	if(is_array($arr)){
		foreach($arr as $k => $v){
			$sqls .= "('$k','".$db->escape(trim($v))."'),";
		}
		$sqls = substr($sqls,0,-1);
		$db->query("replace into `{$configs['dbname']}`.{$tpf}settings (vars,value) values $sqls;");
	}
	$q = $db->query("select * from `{$configs['dbname']}`.{$tpf}settings order by vars ");
	while($rs = $db->fetch_array($q)){
		$str_c .= "\t'".$rs['vars']."' => '".$rs['value']."',".LF;
	}
	$db->free($q);
	unset($rs);
	
	$str = "<?php".LF.LF;
	$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
	$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
	$str .= "\$settings = array(".LF;
	$str .= $str_c;
	$str .= ");".LF.LF;
	$str .= "?>".LF;
	
	write_file(PHPDISK_ROOT."./system/settings.inc.php",$str);
	
	$q = $db->query("select server_host,server_key from {$tpf}servers where server_oid>1",'SILENT');
	while($q && $rs = $db->fetch_array($q)){
		echo '<script type="text/javascript" src="'.$rs['server_host'].'update_configs.php?code='.pd_encode($rs['server_key']).'"></script>';
	}
	$db->free($q);
	unset($rs);
}

function group_settings_cache(){
	global $db,$tpf;

	$q = $db->query("select * from {$tpf}groups order by gid");
	while($rs = $db->fetch_array($q)){
		$str_c .= "\t'".$rs['gid']."' => array(".LF;
		$str_c .= "\t\t'max_messages' => '".$rs['max_messages']."',".LF;
		$str_c .= "\t\t'max_flow_down' => '".$rs['max_flow_down']."',".LF;
		$str_c .= "\t\t'max_flow_view' => '".$rs['max_flow_view']."',".LF;
		$str_c .= "\t\t'max_storage' => '".$rs['max_storage']."',".LF;
		$str_c .= "\t\t'max_filesize' => '".$rs['max_filesize']."',".LF;
		$str_c .= "\t\t'group_file_types' => '".$rs['group_file_types']."',".LF;
		$str_c .= "\t\t'max_folders' => '".$rs['max_folders']."',".LF;
		$str_c .= "\t\t'max_files' => '".$rs['max_files']."',".LF;
		$str_c .= "\t\t'can_share' => '".$rs['can_share']."',".LF;
		$str_c .= "\t\t'secs_loading' => '".$rs['secs_loading']."',".LF;
		$str_c .= "\t\t'server_ids' => '".$rs['server_ids']."',".LF;
		$str_c .= "\t),".LF.LF;
		
	}
	$db->free($q);
	unset($rs);
	
	$str = "<?php".LF.LF;
	$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
	$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
	$str .= "\$group_settings = array(".LF;
	$str .= $str_c;
	$str .= ");".LF.LF;
	$str .= "?>".LF;
	
	write_file(PHPDISK_ROOT."./system/group_settings.inc.php",$str);
	
	$q = $db->query("select server_host,server_key from {$tpf}servers where server_oid>1",'SILENT');
	while($q && $rs = $db->fetch_array($q)){
		echo '<script type="text/javascript" src="'.$rs['server_host'].'update_configs.php?code='.pd_encode($rs['server_key']).'"></script>';
	}
	$db->free($q);
	unset($rs);
	
}

function get_option_folders($deep=4,$update=0){
	global $db,$tpf,$L,$pd_uid;
	
	$q = $db->query("select folder_node,folder_id,folder_name,folder_description,parent_id from {$tpf}folders where userid='$pd_uid' and in_recycle=0 order by folder_order asc,in_time asc");
	while($rs = $db->fetch_array($q)){
		$folders[] = $rs;
	}
	$db->free($q);
	unset($rs);
	
	$str_c = '<option value=\'0\' style=\'color:#0000FF\' id=\'fd_0\'>'.$L['root_folder'].'</option>'.LF;
	for($i = 0; $i < count($folders); $i++) {
		if($folders[$i]['folder_node'] == 1) {
			$str_c .= '<option value=\''.$folders[$i]['folder_id'].'\' id=\'fd_'.$folders[$i]['folder_id'].'\'>'.$folders[$i]['folder_name'].'</option>'.LF;
			for($j = 0; $j < count($folders); $j++) {
				if($folders[$j]['parent_id'] == $folders[$i]['folder_id'] && $folders[$j]['folder_node'] == 2) {
					$str_c .= '<option value=\''.$folders[$j]['folder_id'].'\' id=\'fd_'.$folders[$j]['folder_id'].'\'>'.str_repeat('&nbsp;',4).$folders[$j]['folder_name'].'</option>'.LF;
					for($k = 0; $k < count($folders); $k++) {
						if($folders[$k]['parent_id'] == $folders[$j]['folder_id'] && $folders[$k]['folder_node'] == 3) {
							$str_c .= '<option value=\''.$folders[$k]['folder_id'].'\' id=\'fd_'.$folders[$k]['folder_id'].'\'>'.str_repeat('&nbsp;',8).$folders[$k]['folder_name'].'</option>'.LF;
							if($deep ==4){
								for($l=0;$l<count($folders);$l++){
									if($folders[$l]['parent_id'] == $folders[$k]['folder_id'] && $folders[$l]['folder_node'] == 4) {
										$str_c .= '<option value=\''.$folders[$l]['folder_id'].'\' id=\'fd_'.$folders[$l]['folder_id'].'\'>'.str_repeat('&nbsp;',12).$folders[$l]['folder_name'].'</option>'.LF;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	$cache_dir = PHPDISK_ROOT."system/cache/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	$cache_dir = PHPDISK_ROOT."system/cache/$pd_uid/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	
	$cache_file = PHPDISK_ROOT."system/cache/$pd_uid/my_folders_{$deep}.inc.php";
	
	if(!file_exists($cache_file) || $update){
		$str = "<?php".LF.LF;
		$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
		$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
		$str .=	"if(!defined('IN_PHPDISK')){".LF;
		$str .= "\texit('[PHPDisk] Access Denied');".LF;
		$str .= "}".LF.LF;
		$str .= "\$option_folder_{$deep} = \"".$str_c."\";".LF;
		$str .= "?>".LF;
		
		write_file($cache_file,$str);
	}
	if(!$update){
		return $str_c;
	}
}

function folders_menu_cache($update=0){
	global $db,$tpf,$L,$pd_uid;
	
	$num = (int)@$db->result_first("select count(*) from {$tpf}files where folder_id=0 and in_recycle=0 and userid='$pd_uid'");
	$folder_size = @$db->result_first("select sum(file_size) from {$tpf}files where folder_id=0 and in_recycle=0 and userid='$pd_uid'");
	$count = $num ? $L['all_file']."$num , ".$L['folder_size'].get_size($folder_size) : ''; 
	
	$str_c = "tr.add(0,-1,'{$L['mydisk_cp_manage']}','".urr("mydisk","item=files&action=index")."','{$count}','mainframe');".LF;
	
	$q = $db->query("select folder_id,parent_id,folder_name,folder_node,folder_size from {$tpf}folders where userid='$pd_uid' and in_recycle=0 order by folder_order asc,in_time asc");
	while($rs = $db->fetch_array($q)){
		$num = (int)@$db->result_first("select count(*) from {$tpf}files where folder_id='{$rs['folder_id']}' and userid='$pd_uid'");
		$count = $num ? $L['all_file']."$num , ".$L['folder_size'].get_size($rs['folder_size']) : ''; 
		$str_c .= "tr.add({$rs['folder_id']},{$rs['parent_id']},'{$rs['folder_name']}','".urr("mydisk","item=files&action=index&folder_node={$rs['folder_node']}&folder_id={$rs['folder_id']}")."','{$count}','mainframe');".LF;
	}
	$db->free($q);
	unset($rs);
	$cache_dir = PHPDISK_ROOT."system/cache/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	$cache_dir = PHPDISK_ROOT."system/cache/$pd_uid/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	
	$cache_file = PHPDISK_ROOT."system/cache/$pd_uid/my_menu.inc.php";
	
	if(!file_exists($cache_file) || $update){
		$str = "<?php".LF.LF;
		$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
		$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
		$str .=	"if(!defined('IN_PHPDISK')){".LF;
		$str .= "\texit('[PHPDisk] Access Denied');".LF;
		$str .= "}".LF.LF;
		$str .= "\$folder_list = \"".$str_c."\";".LF;
		$str .= "?>".LF;
		
		write_file($cache_file,$str);
	}
	if(!$update){
		return $str_c;
	}
}
function public_menu_cache($update=0,$idx=0){
	global $db,$tpf,$L,$pd_uid;
	if($idx){
		$str_c = "tr.add(0,-1,'{$L['public_file_list']}','".urr("public","")."');".LF;
	}else{
		$str_c = "tr.add(0,-1,'{$L['public_file_manage']}','###');".LF;
	}
	
	$q = $db->query("select * from {$tpf}categories where is_hidden=0 order by show_order asc,cate_id asc");
	while($rs = $db->fetch_array($q)){
		$num = @$db->result_first("select count(*) from {$tpf}files where cate_id='{$rs['cate_id']}' or subcate_id='{$rs['cate_id']}' and in_recycle=0 and is_public=1 and cate_id>0 and userid>0 ");
		if($idx){
			$str_c .= "tr.add({$rs['cate_id']},{$rs['pid']},'{$rs['cate_name']}','".urr("public","pid={$rs['pid']}&cate_id={$rs['cate_id']}")."','".$num."','');".LF;
		}else{
			$str_c .= "tr.add({$rs['cate_id']},{$rs['pid']},'{$rs['cate_name']}','".urr("mydisk","item=public&action=index&pid={$rs['pid']}&id={$rs['cate_id']}")."','".$num."','mainframe');".LF;
		}
	}
	$db->free($q);
	unset($rs);
	$cache_dir = PHPDISK_ROOT."system/cache/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	if($idx){
		$cache_file = PHPDISK_ROOT."system/cache/pub_menu_idx.inc.php";
	}else{
		$cache_file = PHPDISK_ROOT."system/cache/pub_menu.inc.php";
	}
	
	if(!file_exists($cache_file) || $update){
		$str = "<?php".LF.LF;
		$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
		$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
		$str .=	"if(!defined('IN_PHPDISK')){".LF;
		$str .= "\texit('[PHPDisk] Access Denied');".LF;
		$str .= "}".LF.LF;
		$str .= "\$public_folder_tree = \"".$str_c."\";".LF;
		$str .= "?>".LF;
		
		write_file($cache_file,$str);
	}
	if(!$update){
		return $str_c;
	}
}
function get_option_public_folder($update=0){
	global $db,$tpf;
	
	$q = $db->query("select * from {$tpf}categories where is_hidden=0 order by show_order asc,cate_id asc");
	while($rs = $db->fetch_array($q)){
		$folders[] = $rs;
	}
	$db->free($q);
	unset($rs);
	
	for($i = 0; $i < count($folders); $i++) {
		if($folders[$i]['pid'] == 0) {
			$str_c .= '<option value=\''.$folders[$i]['cate_id'].'\'>'.$folders[$i]['cate_name'].'</option>'.LF;
			for($j = 0; $j < count($folders); $j++) {
				if($folders[$j]['pid'] == $folders[$i]['cate_id'] && $folders[$j]['pid'] <> 0) {
					$str_c .= '<option value=\''.$folders[$j]['cate_id'].'\'>'.str_repeat('&nbsp;',4).$folders[$j]['cate_name'].'</option>'.LF;
				}
			}
		}
	}
	$cache_dir = PHPDISK_ROOT."system/cache/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	
	$cache_file = PHPDISK_ROOT."system/cache/pub_menu_option.inc.php";
	
	if(!file_exists($cache_file) || $update){
		$str = "<?php".LF.LF;
		$str .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF;
		$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;
		$str .=	"if(!defined('IN_PHPDISK')){".LF;
		$str .= "\texit('[PHPDisk] Access Denied');".LF;
		$str .= "}".LF.LF;
		$str .= "\$pub_menu_option = \"".$str_c."\";".LF;
		$str .= "?>".LF;
		
		write_file($cache_file,$str);
	}
	if(!$update){
		return $str_c;
	}
}
function guest_upload_auto_delete(){
	global $db,$tpf,$timestamp,$settings;
	$expire_time = $settings['guest_file_save_expire'] ? (int)$settings['guest_file_save_expire'] * 86400 : 7*86400;
	$cache_dir = PHPDISK_ROOT."system/cache/";
	if(!is_dir($cache_dir)){
		@mkdir($cache_dir,0777);
		write_file($cache_dir.'index.htm','PHPDisk');
	}
	$cache_file = PHPDISK_ROOT."system/cache/guest_upload_auto_delete.log.php";

	if(file_exists($cache_file)){
		$str = read_file($cache_file);
		if(($timestamp-(int)$str)>86400){
			delete_phpdisk_file("select * from {$tpf}files where userid=0 and $timestamp-file_time>$expire_time");	
			$db->query_unbuffered("delete from {$tpf}files where userid=0 and $timestamp-file_time>$expire_time");
			write_file($cache_file,$timestamp,'w+');
		}
	}else{
		write_file($cache_file,$timestamp,'w+');
	}
}
?>