<?php
function GetRndFileName($sExt)
{
	return strtoupper(base_convert(getSysTime(), 10, 36)) . qbase::random(4) . "." . $sExt;
}

function GetRootPath()
{
	$sRealPath = realpath('./') ;
	$sSelfPath = $_SERVER['PHP_SELF'] ;
	$sSelfPath = substr($sSelfPath, 0, strrpos($sSelfPath, '/')) ;
	return substr($sRealPath, 0, strlen($sRealPath) - strlen($sSelfPath)) ;
}

function getExt($s_File)
{
	$Ext = substr(strrchr($s_File, "."), 1);
	return $Ext;
}

function getSmallImageFile($s_File, $e = '_s')
{
	$exts = explode(".", $s_File);
	$ext = array_pop($exts);
	return implode('.',$exts) . $e .'.'. $ext;
}

function isPic( $ext)
{
	$ext = strtolower($ext);
	$sAllowExt = "jpg|jpeg|png|gif";
	if ( in_array( $ext, explode('|', $sAllowExt))) return true;
	else return false;
}

function DoCreateNewDir()
{
	global $channel,$channelid;
	switch ( $channel[$channelid]['upload_date_path'])
	{
		Case 1:
			$sCreateDir = qbase::formatTime("Y");
		break;
		Case 2:
			$sCreateDir = qbase::formatTime("Ym");
		break;
		Case 3:
			$sCreateDir = qbase::formatTime("Ymd");
		break;
		case 4:
			$sCreateDir = qbase::formatTime("Y/m");
		break;
		case 5:
			$sCreateDir = qbase::formatTime("Y/m/d");
		break;
		default:
			$sCreateDir = qbase::formatTime("Y/m");
		break;
	}
	$GLOBALS["sSaveDir"] = $GLOBALS["sSaveDir"].$sCreateDir."/";
	qbase::createdir(R_P .'/'. $GLOBALS["sSaveDir"]);
}

function  InitUpload()
{
	global $_SET,$channelid,$channel,$channelid,$sSaveDir;
	if( $channelid>0){
		$sSaveDir   =  'attachment/'.$channel[$channelid]['channeldir']."/";
	}else{
		$sSaveDir   =  "attachment/support/";
	}
}

function exitupload($key){
	$map = array(
		1 => 'not-found',
		2 => 'invalid-upload',
		3 => 'size-too-big',
		4 => 'type-not-match',
		5 => 'move-error',
		6 => 'not-allowed',
		7 => 'It is not a picture',
	);
	$arr = array('status'=>404, "msg"=>$map[$key]);
	echo qbase::jsonencode($arr);
	exit();
}
?>
