📄 function.base.php
字号:
<?php
/**
* function avaialble to the file manager
* @author Logan Cai (cailongqun [at] yahoo [dot] com [dot] cn)
* @link www.phpletter.com
* @since 22/April/2007
*
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "config.php");
/**
* force to ensure existence of stripos
*/
if (!function_exists("stripos"))
{
function stripos($str,$needle,$offset=0)
{
return @strpos(strtolower($str),strtolower($needle),$offset);
}
}
/**
* get the current Url but not the query string specified in $excls
*
* @param array $excls specify those unwanted query string
* @return string
*/
function getCurrentUrl($excls=array())
{
$output = $_SERVER['PHP_SELF'];
$count = 1;
foreach($_GET as $k=>$v)
{
if(array_search($k, $excls) ===false)
{
$strAppend = "&";
if($count == 1)
{
$strAppend = "?";
$count++;
}
$output .= $strAppend . $k . "=" . $v;
}
}
return $output;
}
/**
* print out an array
*
* @param array $array
*/
function displayArray($array, $comments="")
{
echo "<pre>";
echo $comments;
print_r($array);
echo $comments;
echo "</pre>";
}
/**
* check if a file extension is permitted
*
* @param string $filePath
* @param array $validExts
* @param array $invalidExts
* @return boolean
*/
function isValidExt($filePath, $validExts, $invalidExts=array())
{
$tem = array();
if(sizeof($validExts))
{
foreach($validExts as $k=>$v)
{
$tem[$k] = strtolower(trim($v));
}
}
$validExts = $tem;
$tem = array();
if(sizeof($invalidExts))
{
foreach($invalidExts as $k=>$v)
{
$tem[$k] = strtolower(trim($v));
}
}
$invalidExts = $tem;
if(sizeof($validExts) && sizeof($invalidExts))
{
foreach($validExts as $k=>$ext)
{
if(array_search($ext, $invalidExts) !== false)
{
unset($validExts[$k]);
}
}
}
if(sizeof($validExts))
{
if(array_search(strtolower(getFileExt($filePath)), $validExts) !== false)
{
return true;
}else
{
return false;
}
}elseif(array_search(strtolower(getFileExt($filePath)), $invalidExts) === false)
{
return true;
}else
{
return false;
}
}
/**
* transform file relative path to absolute path
* @param string $value the path to the file
* @return string
*/
function relToAbs($value)
{
return backslashToSlash(preg_replace("/(\\\\)/","\\", getRealPath($value)));
}
function getRelativeFileUrl($value, $relativeTo)
{
$output = '';
$wwwroot = removeTrailingSlash(backslashToSlash(getRootPath()));
$urlprefix = "";
$urlsuffix = "";
$value = backslashToSlash(getRealPath($value));
$pos = strpos($value, $wwwroot);
if ($pos !== false && $pos == 0)
{
$output = $urlprefix . substr($value, strlen($wwwroot)) . $urlsuffix;
}
}
/**
* replace slash with backslash
*
* @param string $value the path to the file
* @return string
*/
function slashToBackslash($value) {
return str_replace("/", DIRECTORY_SEPARATOR, $value);
}
/**
* replace backslash with slash
*
* @param string $value the path to the file
* @return string
*/
function backslashToSlash($value) {
return str_replace(DIRECTORY_SEPARATOR, "/", $value);
}
/**
* removes the trailing slash
*
* @param string $value
* @return string
*/
function removeTrailingSlash($value) {
if(preg_match('@^.+/$@i', $value))
{
$value = substr($value, 0, strlen($value)-1);
}
return $value;
}
/**
* append a trailing slash
*
* @param string $value
* @return string
*/
function addTrailingSlash($value)
{
if(preg_match('@^.*[^/]{1}$@i', $value))
{
$value .= '/';
}
return $value;
}
/**
* transform a file path to user friendly
*
* @param string $value
* @return string
*/
function transformFilePath($value) {
$rootPath = addTrailingSlash(backslashToSlash(getRealPath(CONFIG_SYS_ROOT_PATH)));
$value = addTrailingSlash(backslashToSlash(getRealPath($value)));
if(!empty($rootPath) && ($i = strpos($value, $rootPath)) !== false)
{
$value = ($i == 0?substr($value, strlen($rootPath)):"/");
}
$value = prependSlash($value);
return $value;
}
/**
* prepend slash
*
* @param string $value
* @return string
*/
function prependSlash($value)
{
if (($value && $value[0] != '/') || !$value )
{
$value = "/" . $value;
}
return $value;
}
function writeInfo($data, $die = false)
{
$fp = @fopen(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'data.php', 'w+');
@fwrite($fp, $data);
@fwrite($fp, "\n\n" . date('d/M/Y H:i:s') );
@fclose($fp);
if($die)
{
die();
}
}
/**
* no cachable header
*/
function addNoCacheHeaders() {
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
/**
* add extra query stiring to a url
* @param string $baseUrl
* @param string $extra the query string added to the base url
*/
function appendQueryString($baseUrl, $extra)
{
$output = $baseUrl;
if(!empty($extra))
{
if(strpos($baseUrl, "?") !== false)
{
$output .= "&" . $extra;
}else
{
$output .= "?" . $extra;
}
}
return $output;
}
/**
* make the query strin from $_GET, but excluding those specified by $excluded
*
* @param array $excluded
* @return string
*/
function makeQueryString($excluded=array())
{
$output = '';
$count = 1;
foreach($_GET as $k=>$v)
{
if(array_search($k, $excluded) === false)
{
$output .= ($count>1?'&':'') . ($k . "=" . $v);
$count++;
}
}
return $output;
}
/**
* get parent path from specific path
*
* @param string $value
* @return string
*/
function getParentPath($value)
{
$value = removeTrailingSlash(backslashToSlash($value));
if(false !== ($index = strrpos($value, "/")) )
{
return substr($value, 0, $index);
}
}
/**
* check if the file/folder is sit under the root
*
* @param string $value
* @return boolean
*/
function isUnderRoot($value)
{
$roorPath = strtolower(addTrailingSlash(backslashToSlash(getRealPath(CONFIG_SYS_ROOT_PATH))));
if(file_exists($value) && @strpos(strtolower(addTrailingSlash(backslashToSlash(getRealPath($value)))), $roorPath) === 0 )
{
return true;
}
return false;
}
/**
* check if a file under the session folder
*
* @param string $value
* @return boolean
*/
function isUnderSession($value)
{
global $session;
$sessionPath = strtolower(addTrailingSlash(backslashToSlash(getRealPath($session->getSessionDir()))));
if(file_exists($value) && @strpos(strtolower(addTrailingSlash(backslashToSlash(getRealPath($value)))), $sessionPath) === 0 )
{
return true;
}
return false;
}
/**
* get thumbnail width and height
*
* @param integer $originaleImageWidth
* @param integer $originalImageHeight
* @param integer $thumbnailWidth
* @param integer $thumbnailHeight
* @return array()
*/
function getThumbWidthHeight( $originaleImageWidth, $originalImageHeight, $thumbnailWidth, $thumbnailHeight)
{
$outputs = array( "width"=>0, "height"=>0);
$thumbnailWidth = intval($thumbnailWidth);
$thumbnailHeight = intval($thumbnailHeight);
if(!empty($originaleImageWidth) && !empty($originalImageHeight))
{
//start to get the thumbnail width & height
if(($thumbnailWidth < 1 && $thumbnailHeight < 1) || ($thumbnailWidth > $originaleImageWidth && $thumbnailHeight > $originalImageHeight ))
{
$thumbnailWidth =$originaleImageWidth;
$thumbnailHeight = $originalImageHeight;
}elseif($thumbnailWidth < 1)
{
$thumbnailWidth = floor($thumbnailHeight / $originalImageHeight * $originaleImageWidth);
}elseif($thumbnailHeight < 1)
{
$thumbnailHeight = floor($thumbnailWidth / $originaleImageWidth * $originalImageHeight);
}else
{
$scale = min($thumbnailWidth/$originaleImageWidth, $thumbnailHeight/$originalImageHeight);
$thumbnailWidth = floor($scale*$originaleImageWidth);
$thumbnailHeight = floor($scale*$originalImageHeight);
}
$outputs['width'] = $thumbnailWidth;
$outputs['height'] = $thumbnailHeight;
}
return $outputs;
}
/**
* turn to absolute path from relative path
*
* @param string $value
* @return string
*/
function getAbsPath($value) {
if (substr($value, 0, 1) == "/")
return slashToBackslash(DIR_AJAX_ROOT . $value);
return slashToBackslash(dirname(__FILE__) . "/" . $value);
}
/**
* get file/folder base name
*
* @param string $value
* @return string
*/
function getBaseName($value)
{
$value = removeTrailingSlash(backslashToSlash($value));
if(false !== ($index = strrpos($value, "/")) )
{
return substr($value, $index + 1);
}else
{
return $value;
}
}
function myRealPath($path) {
if(strpos($path, ':/') !== false)
{
return $path;
}
// check if path begins with "/" ie. is absolute
// if it isnt concat with script path
if (strpos($path,"/") !== 0 ) {
$base=dirname($_SERVER['SCRIPT_FILENAME']);
$path=$base."/".$path;
}
// canonicalize
$path=explode('/', $path);
$newpath=array();
for ($i=0; $i<sizeof($path); $i++) {
if ($path[$i]==='' || $path[$i]==='.') continue;
if ($path[$i]==='..') {
array_pop($newpath);
continue;
}
array_push($newpath, $path[$i]);
}
$finalpath="/".implode('/', $newpath);
// check then return valid path or filename
if (file_exists($finalpath)) {
return ($finalpath);
}
else return FALSE;
}
/**
* calcuate realpath for a relative path
*
* @param string $value a relative path
* @return string absolute path of the input
*/
function getRealPath($value)
{
$output = '';
if(($path = realpath($value)) && $path != $value)
{
$output = $path;
}else
{
$output = myRealPath($value);
}
return $output;
}
/**
* get file url
*
* @param string $value
* @return string
*/
function getFileUrl($value)
{
$output = '';
$wwwroot = removeTrailingSlash(backslashToSlash(getRootPath()));
$urlprefix = "";
$urlsuffix = "";
$value = backslashToSlash(getRealPath($value));
$pos = stripos($value, $wwwroot);
if ($pos !== false && $pos == 0)
{
$output = $urlprefix . substr($value, strlen($wwwroot)) . $urlsuffix;
}else
{
$output = $value;
}
return "http://" . addTrailingSlash(backslashToSlash($_SERVER['HTTP_HOST'])) . removeBeginingSlash(backslashToSlash($output));
}
/**
*
* transfer file size number to human friendly string
* @param integer $size.
* @return String
*/
function transformFileSize($size) {
if ($size > 1048576)
{
return round($size / 1048576, 1) . " MB";
}elseif ($size > 1024)
{
return round($size / 1024, 1) . " KB";
}elseif($size == '')
{
return $size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -