1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 20:58:30 +01:00

Move file-realted methods to file_class.php.

This commit is contained in:
lonalore 2017-08-07 09:47:00 +02:00
parent deb8c906af
commit d6daa6b5bf
2 changed files with 127 additions and 13 deletions

View File

@ -33,16 +33,6 @@ class e107
const LOCALHOST_IP = '0000:0000:0000:0000:0000:ffff:7f00:0001';
const LOCALHOST_IP2 = '0000:0000:0000:0000:0000:0000:0000:0001';
/**
* Flag used by prepareDirectory() method -- create directory if not present.
*/
const FILE_CREATE_DIRECTORY = 1;
/**
* Flag used by prepareDirectory() method -- file permissions may be changed.
*/
const FILE_MODIFY_PERMISSIONS = 2;
public $server_path;
public $e107_dirs = array();
@ -426,6 +416,8 @@ class e107
mkdir(e_SYSTEM, 0755);
}
// Prepare essential directories.
$this->prepareDirs();
}
@ -487,6 +479,16 @@ class e107
{
$this->e107_dirs['CACHE_DIRECTORY'] = $this->e107_dirs['SYSTEM_DIRECTORY']."cache/"; // multisite support.
}
return $this;
}
/**
* Prepares essential directories.
*/
public function prepareDirs()
{
$file = e107::getFile();
// Essential directories which should be created and writable.
$essential_directories = array(
@ -512,10 +514,8 @@ class e107
continue;
}
$this->prepareDirectory($this->e107_dirs[$directory], self::FILE_CREATE_DIRECTORY);
$file->prepareDirectory($this->e107_dirs[$directory], FILE_CREATE_DIRECTORY);
}
return $this;
}
/**

View File

@ -62,6 +62,16 @@ Note:
*/
/**
* Flag used by prepareDirectory() method -- create directory if not present.
*/
define('FILE_CREATE_DIRECTORY', 1);
/**
* Flag used by prepareDirectory() method -- file permissions may be changed.
*/
define('FILE_MODIFY_PERMISSIONS', 2);
class e_file
{
@ -1650,4 +1660,108 @@ class e_file
}
/**
* Checks that the directory exists and is writable.
*
* @param string $directory
* A string containing the name of a directory path. A trailing slash will be trimmed from a path.
* @param int $options
* A bitmask to indicate if the directory should be created if it does not exist (FILE_CREATE_DIRECTORY) or
* made writable if it is read-only (FILE_MODIFY_PERMISSIONS).
*
* @return bool
* TRUE if the directory exists (or was created) and is writable. FALSE otherwise.
*/
public function prepareDirectory($directory, $options = FILE_MODIFY_PERMISSIONS)
{
$directory = rtrim($directory, '/\\');
// Check if directory exists.
if(!is_dir($directory))
{
// Let mkdir() recursively create directories and use the default directory permissions.
if(($options & FILE_CREATE_DIRECTORY) && @$this->mkDir($directory, null, true))
{
return $this->_chMod($directory);
}
return false;
}
// The directory exists, so check to see if it is writable.
$writable = is_writable($directory);
if(!$writable && ($options & FILE_MODIFY_PERMISSIONS))
{
return $this->_chMod($directory);
}
return $writable;
}
/**
* (Non-Recursive) Sets the permissions on a file or directory.
*
* @param string $path
* A string containing a file, or directory path.
* @param int $mode
* Integer value for the permissions. Consult PHP chmod() documentation for more information.
*
* @return bool
* TRUE for success, FALSE in the event of an error.
*/
public function _chMod($path, $mode = null)
{
if(!isset($mode))
{
if(is_dir($path))
{
$mode = 0775;
}
else
{
$mode = 0664;
}
}
if(@chmod($path, $mode))
{
return true;
}
return false;
}
/**
* Creates a directory.
*
* @param string $path
* A string containing a file path.
* @param int $mode
* Mode is used.
* @param bool $recursive
* Default to FALSE.
* @param null $context
* Refer to http://php.net/manual/ref.stream.php
*
* @return bool
* Boolean TRUE on success, or FALSE on failure.
*/
public function mkDir($path, $mode = null, $recursive = false, $context = null)
{
if(!isset($mode))
{
$mode = 0775;
}
if(!isset($context))
{
return mkdir($path, $mode, $recursive);
}
else
{
return mkdir($path, $mode, $recursive, $context);
}
}
}