全部博文(39)
分类: LINUX
2011-09-14 13:52:01
需要修改files.php文件(路径: \opengoo\environment\functions\files.php)
代码如下:
function folder_is_writable($path) {
if(!is_dir($path)) {
return false;
} // if
do {
$test_file = with_slash($path)
. sha1(uniqid(rand(), true));
} while(is_file($test_file));
$put = @file_put_contents($test_file,
'test');
if($put === false) {
return false;
} // if
@unlink($test_file);
return true;
} // folder_is_writable
function file_is_writable($path) {
if(!is_file($path)) {
return false;
} // if
$open = @fopen($path, 'a+');
if($open === false) {
return false;
} // if
@fclose($open);
return true;
} // file_is_writable
function get_file_line($file, $line, $default = null) {
if(is_file($file)) {
$lines = file($file);
return isset($file[$line]) ?
$file[$line] : $default;
} else {
return $default;
} // if
} // get_file_line
function get_files($dir, $extension = null, $base_name_only =
false) {
// Check dir...
if(!is_dir($dir)) return false;
// Prepare input data...
$dir = with_slash($dir);
if(!is_null($extension)) {
if(is_array($extension))
{
foreach($extension
as $k => $v) $extension[$k] = strtolower($v);
} else {
$extension =
strtolower($extension);
} // if
} // if
// We have a dir...
if(!is_dir($dir)) return null;
// Open dir and prepare result
$d = dir($dir);
if (!is_object($d)) throw new Error($dir);
$files = array();
// Loop dir entries
while(false !== ($entry =
$d->read())) {
// Valid entry?
if(($entry
<> '.')
&& ($entry
<> '..')) {
// Get file
path...
$path = $dir
. $entry;
// If we
have valid file that do the checks
if(is_file($path))
{
if(is_null($extension))
{
$files[]
= $base_name_only ? basename($path) : $path;
}
else {
//
Match multiple extensions?
if(is_array($extension))
{
//
If in array add...
if(in_array(
strtolower(get_file_extension($path)), $extension )) {
$files[]
= $base_name_only ? basename($path) : $path;
}
// if
//
Match single extension
}
else {
//
If extensions match add...
if(strtolower(get_file_extension($path))
== $extension) {
$files[]
= $base_name_only ? basename($path) : $path;
}
// if
} // if
} // if
} // if
} // if
} // while
// Done... close dir...
$d->close();
// And return...
return count($files) > 0 ? $files
: null;
} // get_files
function get_file_extension($path, $leading_dot = false) {
$filename = basename($path);
$dot_offset = (boolean) $leading_dot ? 0 :
1;
if( ($pos = strrpos($filename, '.')) !== false )
{
return substr($filename, $pos +
$dot_offset, strlen($filename));
} // if
return '';
} // get_file_extension
function dir_size($dir) {
$totalsize = 0;
if ($dirstream = @opendir($dir)) {
while (false !== ($filename =
readdir($dirstream))) {
if
(($filename != ".") && ($filename
!= "..")) {
$path
= with_slash($dir) . $filename;
if
(is_file($path)) $totalsize += filesize($path);
if
(is_dir($path)) $totalsize += dir_size($path);
} // if
} // while
} // if
closedir($dirstream);
return $totalsize;
} // end func dir_size
function file_is_zip($mime_type, $extension = '') {
return ($mime_type == 'application/zip' ||
$mime_type == 'application/x-zip-compressed' ||
($mime_type
== 'application/x-compressed' &&
$extension == 'zip'));
}
function delete_dir($dir) {
$dh = opendir($dir);
while($file = readdir($dh)) {
if(($file != ".")
&& ($file != "..")) {
$fullpath =
$dir . "/" . $file;
if(!is_dir($fullpath))
{
unlink($fullpath);
} else
{
delete_dir($fullpath);
} // if
} // if
} // while
closedir($dh);
return rmdir($dir) ? true : false;
} // end func delete_dir
function force_mkdir($path, $chmod = null) {
return mkdir($path, $chmod, true);
} // force_mkdir
function force_mkdir_from_base($base, $path, $chmod = null)
{
if(is_dir(with_slash($base).$path)) return
true;
$real_path = str_replace('\\', '/', $path);
$parts = explode('/', $real_path);
$forced_path = '';
foreach($parts as $part) {
if($part !='')
{
// Skip first
on windows
if($forced_path
== '') {
$forced_path
= with_slash($base) . $part;
} else
{
$forced_path
.= '/' . $part;
} // if
if(!is_dir($forced_path))
{
if(!is_null($chmod))
{
if(!mkdir($forced_path))
return false;
}
else {
if(!mkdir($forced_path,
$chmod)) return false;
}
// if
} // if
} // if
} // foreach
return true;
} // force_mkdir
function is_dir_empty($dir_path) {
$d = dir($dir_path);
if($d) {
while(false !== ($entry =
$d->read())) {
if(($entry ==
'.') || ($entry == '..')) continue;
return
false;
} // while
} // if
return true;
} // is_dir_empty
function get_unique_filename($in, $desired_filename) {
if(!is_dir($in)) false;
$file_path = $in . '/' .
$desired_filename;
$counter = 0;
while(is_file($file_path)) {
$counter++;
$file_path =
insert_before_file_extension($file_path, '(' . $counter .
')');
} // if
return $file_path;
} // get_unique_filename
function insert_before_file_extension($filename, $insert) {
if (strpos($filename,'.') >
0)
return str_replace_first('.', $insert . '.',
$filename);
else
return $filename . $insert;
} // insert_before_file_extension
function download_file($path, $type = 'application/octet-stream',
$name = '', $force_download = false) {
if (!function_exists('readfile')) {
$contents =
file_get_contents($path);
return
download_contents($contents, $type, $name, $force_download);
}
if (!is_readable($path)) return false;
$name = trim($name) == '' ? basename($path) :
trim($name);
$size = filesize($path);
if(connection_status() != 0) return false; //
check connection
if($force_download) {
header("Cache-Control:
public");
} else {
header("Cache-Control:
no-store, no-cache, must-ridate");
header("Cache-Control:
post-check=0, pre-check=0", false);
header("Pragma:
no-cache");
} // if
header("Expires: " . gmdate("D, d M Y H:i:s",
mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"),
date("Y"))) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y
H:i:s") . " GMT");
header("Content-Type: $type");
header("Content-Length: " . (string) $size);
// Prepare disposition
$disposition = $force_download ? 'attachment' :
'inline';
$name1 =
iconv("UTF-8","GBK",$name); //wangzhaoliang
header("Content-Disposition: $disposition;
filename=\"" . $name1 . "\"");
//wangzhaoliang
header("Content-Transfer-Encoding:
binary");
readfile($path);
return((connection_status() == 0)
&& !connection_aborted());
} // download_file
function download_contents($content, $type, $name, $size,
$force_download = false) {
if(connection_status() != 0) return false; //
check connection
if($force_download) {
header("Cache-Control:
public");
} else {
header("Cache-Control:
no-store, no-cache, must-ridate");
header("Cache-Control:
post-check=0, pre-check=0", false);
header("Pragma:
no-cache");
} // if
header("Expires: " . gmdate("D, d M Y H:i:s",
mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"),
date("Y"))) . " GMT");
header("Last-Modified: " . gmdate("D, d M Y
H:i:s") . " GMT");
header("Content-Type: $type");
header("Content-Length: " . (string) $size);
// Prepare disposition
$disposition = $force_download ? 'attachment' :
'inline';
$name1 =
iconv("UTF-8","GBK",$name); //wangzhaoliang
header("Content-Disposition: $disposition;
filename=\"" . $name1 . "\""); //wangzhaoliang
header("Content-Transfer-Encoding:
binary");
print $content;
return((connection_status() == 0)
&& !connection_aborted());
} // download_contents
function sort_files($files, $extractor, $sort_with = 'array_ksort',
$sort_method = null) {
// Prepare...
$extractor = trim($extractor);
$sort_with = trim($sort_with);
// Check the input data...
if(!is_array($files)) return false;
if(!function_exists($extractor)) return
false;
if(!function_exists($sort_with)) return
false;
// Prepare the tmp array...
$tmp = array();
// OK, now get the files...
foreach($files as $file) {
// Pass this one?
if(!is_file($file))
continue;
// Get data...
$data =
call_user_func($extractor, $file);
// Prepare array...
if(!isset($tmp[$data]))
$tmp[$data] = array();
// Add filename to the
extracted param...
$tmp[$data][] = $file;
} // foreach
// OK, now sort subarrays
foreach($tmp as &$subarray)
{
if(count($subarray)
> 0) sort($subarray);
} // if
// OK, do the sort thing...
if(is_null($sort_method)) {
$sorted =
call_user_func($sort_with, $tmp);
} else {
$sorted =
call_user_func_array($sort_with, array($tmp, $sort_method));
} // if
// Check sorted array
if(!is_array($sorted)) return false;
// OK, flatten...
$result = array();
foreach($sorted as &$subarray)
{
$result = array_merge($result,
$subarray);
} // foreach
// And done...
return $result;
} // sort_files
//
================================================================
// SORT FUNC REPLACEMENTS
//
// These function RETURN sorted array, don't use
side effect. They
// are used by the sort_files() function in
the
// environment/functions/files.php
//
================================================================
function array_sort($array, $flag = SORT_REGULAR) {
sort($array, $flag);
return $array;
} // end func
function array_rsort($array, $flag = SORT_REGULAR) {
rsort($array, $flag);
return $array;
} // end func array_rsort
function array_ksort($array, $flag = SORT_REGULAR) {
ksort($array, $flag);
return $array;
} // end func array_ksort
function array_krsort($array, $flag = SORT_REGULAR) {
krsort($array, $flag);
return $array;
} // end func array_krsort
?>