<?php
##
# Project: PHPDISK File Storage Solution
# This is NOT a freeware, use is subject to license terms.
#
# Site: http://www.phpdisk.com
#
# $Id: global.func.php 25 2014-01-10 03:13:43Z along $
#
# Copyright (C) 2008-2014 PHPDisk Team. All Rights Reserved.
#
##
if(!defined('IN_PHPDISK')) {
exit('[PHPDisk] Access Denied');
}
function template_echo($tpl,$tpl_dir,$app='',$is_admin_tpl=0){
if($app){
$tpl_cache_dir = PHPDISK_ROOT."system/plugins/$app/";
$tpl_src_dir = PHPDISK_ROOT."plugins/$app/";
}else{
$tpl_cache_dir = $tpl_cache_dir_tmp = PHPDISK_ROOT.'system/'.$tpl_dir;
$tpl_src_dir = PHPDISK_ROOT.$tpl_dir;
$tpl_default_dir = PHPDISK_ROOT.'templates/default/';
$admin_tpl_dir = PHPDISK_ROOT.'templates/admin/';
}
if(strpos($tpl,'/')!==false){
$tpl_cache_dir_tmp = $tpl_cache_dir_tmp.substr($tpl,0,strlen($tpl)-strlen(strrchr($tpl,'/'))).'/';
}
make_dir($tpl_cache_dir_tmp);
make_dir($tpl_cache_dir);
$tpl_cache_file = $tpl_cache_dir.$tpl.'.tpl.php';
$tpl_src_file = $tpl_src_dir.$tpl.'.tpl.php';
if(!$app){
if($is_admin_tpl && !file_exists($tpl_src_file)){
$tpl_src_file = $admin_tpl_dir.$tpl.'.tpl.php';
}elseif(!file_exists($tpl_src_file)){
$tpl_src_file = $tpl_default_dir.$tpl.'.tpl.php';
}
}
if(@filemtime($tpl_cache_file) < @filemtime($tpl_src_file)){
write_file($tpl_cache_file,template_parse($tpl_src_file));
return $tpl_cache_file;
}
if(file_exists($tpl_cache_file)){
return $tpl_cache_file;
}else{
$str = strrchr($tpl_cache_file,'/');
$str = substr($str,1,strlen($str));
die("PHPDisk Template: <b>$tpl_dir$tpl_cache_file</b> not Exists!");
}
}
function template_parse($tpl){
global $user_tpl_dir;
if(!file_exists($tpl)){
exit('Template ['.$tpl.'] not exists!');
}
$str = read_file($tpl);
$str = preg_replace("/\<\!\-\-\#include (.+?)\#\-\-\>/si","<?php require_once template_echo('\\1','$user_tpl_dir'); ?>", $str);
$str = preg_replace("/\<\!\-\-\#(.+?)\#\-\-\>/si","<?php \\1 ?>", $str);
$str = preg_replace("/\{([A-Z_]+)\}/","<?=\\1?>",$str);
$str = preg_replace("/\{(\\\$[a-z0-9_\'\"\[\]]+)\}/si", "<?=\\1?>", $str);
$str = preg_replace("/\{\<\?\=(\\\$[a-z0-9_\'\"\[\]]+)\?\>\}/si","{\\1}",$str);
$str = preg_replace("/\{\#(.+?)\#\}/si","<?=\\1?>", $str);
$str = preg_replace("/\{sql\[(.+)\]\[(.+)\]\}/si","<? foreach(get_sql(\"\\1\") as \\2){ ?>",$str);
$str = str_replace("{/sql}","<? } ?>",$str);
$str = str_replace('@@','{$tpf}',$str); // fix sql tag!
$prefix = "<?php ".LF;
$prefix .= "// This is PHPDISK auto-generated file. Do NOT modify me.".LF.LF;
$prefix .= "// Cache Time:".date('Y-m-d H:i:s').LF.LF;
$prefix .= "!defined('IN_PHPDISK') && exit('[PHPDisk] Access Denied');".LF.LF;
$prefix .= "?>".LF;
return $prefix.$str;
}
function form_auth($p_formhash,$formhash){
if($p_formhash != $formhash){
exit(__('system_error'));
}
}
function convert_str($in,$out,$str){
global $db;
$str = $db->escape($str);
if(function_exists("iconv")){
$str = iconv($in,$out,$str);
}elseif(function_exists("mb_convert_encoding")){
$str = mb_convert_encoding($str,$out,$in);
}
return $db->escape($str);
}
function is_utf8(){
global $charset;
return (strtolower($charset) == 'utf-8') ? true : false;
}
function is_windows(){
return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 1 : 0;
}
function wipe_replace($str) {
return str_replace(array( "\n", "\r", '..'), array('', '', ''), $str);
}
function word_style($i,$type ='font'){
switch($i){
case 500:
$f_px = "25px";
$c_col = '#ff6600';
break;
case 300:
$f_px = "20px";
$c_col = '#ff5500';
break;
case 100:
$f_px = "18px";
$c_col = '#003366';
break;
case 50:
$f_px = "14px";
$c_col = '#003366';
break;
default:
$f_px = "12px";
$c_col = '#666666';
}
return ($type =='font') ? $f_px : $c_col;
}
function page_end_time(){
global $ps_time,$db,$C;
return 'Processed in '.get_runtime('start','end').' second(s), '.$db->querycount.' queries, Gzip '.$C['gz']['status'];
}
function replace_inject_str($str){
$bad_chars = array("\\","'",'"','/','*',',','<','>',"\r","\t","\n",'$','(',')','%','?',';','^','#',':','&');
return str_replace($bad_chars,'',$str);
}
function redirect_top_page(){
global $_SERVER;
$tmp = strrchr($_SERVER['HTTP_REFERER'],'/');
$arr = explode('.',$tmp);
$php_ref = substr($arr[0],1);
if($php_ref =='admincp' || $php_ref =='mydisk'){
return true;
}else{
return false;
}
}
function checklength($str,$min,$max){
if(!$str || strlen($str) > $max || strlen($str) < $min){
return true;
}
}
function ifselected($int1,$int2,$type = 'int'){
if($type == 'int'){
if(intval($int1) == intval($int2)){
return ' selected';
}
}elseif($type == 'str'){
if(strval($int1) == strval($int2)){
return ' selected';
}
}
}
function ifchecked($int1,$int2,$type = 'int'){
if($type == 'int'){
if(intval($int1) == intval($int2)){
return ' checked';
}
}elseif($type == 'str'){
if(strval($int1) == strval($int2)){
return ' checked';
}
}
}
function multi_selected($id,$str){
if(strpos($str,',')){
$a2 = explode(',',$str);
$rtn = in_array($id,$a2) ? ' selected' : '';
}else{
$rtn = $id==$str ? ' selected' : '';
}
return $rtn;
}
function replace_js($str){
return preg_replace("'<script[^>]*?>(.*?)</script>'si","[script]\\1[/script]",$str);
}
function custom_time($format, $time){
global $timestamp;
$s = $timestamp - $time;
if($s < 0){
return __('custom_time_0');
}
if($s < 60){
return $s.__('custom_time_1');
}
$m = $s / 60;
if($m < 60){
return floor($m).__('custom_time_2');
}
$h = $m / 60;
if($h < 24){
return floor($h).__('custom_time_3');
}
$d = $h / 24;
if($d < 2){
return __('custom_time_4').date("H:i", $time);
}
if($d <3){
return __('custom_time_5').date("H:i", $time);
}
if($d <= 30){
return floor($d).__('custom_time_6');
}
return date($format, $time);
}
function get_byte_value($v){
$v = trim($v);
$l = strtolower($v[strlen($v) - 1]);
switch($l){
case 'g':
$v *= 1024;
case 'm':
$v *= 1024;
case 'k':
$v *= 1024;
}
return $v;
}
function redirect($url,$str,$timeout = 2000,$target = ''){
global $user_tpl_dir;
if($timeout ==0){
header("Location:$url");
exit;
}else{
$msg = '';
if(is_array($str)){
for($i=0;$i<count($str);$i++){
$msg .= "<li>¡¤".$str[$i]."</li>".LF;
}
}else{
$msg = $str;
}
$go_url = $url=='back' ? $url = 'javascript:history.back();' : $url;
require_once template_echo('information',$user_tpl_dir);
$rtn = "<script>".LF;
$rtn .= "<!--".LF;
$rtn .= "function redirect() {".LF;
if($target =='top'){
$rtn .= " self.parent.location.href = '$url';".LF;
}else{
$rtn .= " document.location.href = '$go_url';".LF;
}
$rtn .= "}".LF;
$rtn .= "setTimeout('redirect();', $timeout);".LF;
$rtn .= "-->".LF;
$rtn .= "</script>".LF;
echo $rtn;
}
}
function tb_redirect($url,$str,$timeout=2000){
if(is_array($str)){
for($i=0;$i<count($str);$i++){
$msg .= "<li>¡¤".$str[$i]."</li>".LF;
}
}else{
$msg = $str;
}
$go_url = $url=='back' ? $url='javascript:history.back();' : $url;
$rtn = '<div class="tb_box_msg"><img src="images/light.gif" border="0" align="absmiddle"> <ul>'.$msg.'</ul></div>';
$rtn .= "<script>".LF;
$rtn .= "<!--".LF;
$rtn .= "function redirect() {".LF;
$rtn .= $url=='back' ? '' : "self.parent.$,jBox.close(true);".LF;
$rtn .= " self.parent.document.location.href = '$go_url';".LF;
$rtn .= "}".LF;
$rtn .= "setTimeout('redirect();', $timeout);".LF;
$rtn .= "-->".LF;
$rtn .= "</script>".LF;
echo $rtn;
}
function is_bad_chars($str){
$bad_chars = array("\\",' ',"'",'"','/','*',',','<','>',"\r","\t","\n",'$','(',')','%','+','?',';','^','#',':','¡¡','`','=','|','-