first
This commit is contained in:
@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Filesystem;
|
||||
|
||||
use Nextend\Framework\Url\Url;
|
||||
|
||||
if (!defined('NEXTEND_RELATIVE_CACHE_WEB')) {
|
||||
define('NEXTEND_RELATIVE_CACHE_WEB', '/cache/nextend/web');
|
||||
define('NEXTEND_CUSTOM_CACHE', 0);
|
||||
} else {
|
||||
define('NEXTEND_CUSTOM_CACHE', 1);
|
||||
}
|
||||
if (!defined('NEXTEND_RELATIVE_CACHE_NOTWEB')) {
|
||||
define('NEXTEND_RELATIVE_CACHE_NOTWEB', '/cache/nextend/notweb');
|
||||
}
|
||||
|
||||
abstract class AbstractPlatformFilesystem {
|
||||
|
||||
public $paths = array();
|
||||
|
||||
/**
|
||||
* @var string Absolute path which match to the baseuri. It must not end with /
|
||||
* @example /asd/xyz/wordpress
|
||||
*/
|
||||
protected $_basepath;
|
||||
|
||||
protected $dirPermission = 0777;
|
||||
|
||||
protected $filePermission = 0666;
|
||||
|
||||
|
||||
protected $translate = array();
|
||||
|
||||
public function init() {
|
||||
|
||||
}
|
||||
|
||||
public function getPaths() {
|
||||
|
||||
return $this->paths;
|
||||
}
|
||||
|
||||
public function check($base, $folder) {
|
||||
static $checked = array();
|
||||
if (!isset($checked[$base . '/' . $folder])) {
|
||||
$cacheFolder = $base . '/' . $folder;
|
||||
if (!$this->existsFolder($cacheFolder)) {
|
||||
if ($this->is_writable($base)) {
|
||||
$this->createFolder($cacheFolder);
|
||||
} else {
|
||||
die('<div style="position:fixed;background:#fff;width:100%;height:100%;top:0;left:0;z-index:100000;">' . sprintf('<h2><b>%s</b> is not writable.</h2>', esc_html($base)) . '</div>');
|
||||
}
|
||||
} else if (!$this->is_writable($cacheFolder)) {
|
||||
die('<div style="position:fixed;background:#fff;width:100%;height:100%;top:0;left:0;z-index:100000;">' . sprintf('<h2><b>%s</b> is not writable.</h2>', esc_html($cacheFolder)) . '</div>');
|
||||
}
|
||||
$checked[$base . '/' . $folder] = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function measurePermission($testDir) {
|
||||
while ('.' != $testDir && !is_dir($testDir)) {
|
||||
$testDir = dirname($testDir);
|
||||
}
|
||||
|
||||
if ($stat = @stat($testDir)) {
|
||||
$this->dirPermission = $stat['mode'] & 0007777;
|
||||
$this->filePermission = $this->dirPermission & 0000666;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function toLinux($path) {
|
||||
return str_replace(DIRECTORY_SEPARATOR, '/', $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBasePath() {
|
||||
|
||||
return $this->_basepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*/
|
||||
public function setBasePath($path) {
|
||||
|
||||
$this->_basepath = $path;
|
||||
}
|
||||
|
||||
public function getWebCachePath() {
|
||||
|
||||
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_WEB;
|
||||
}
|
||||
|
||||
public function getNotWebCachePath() {
|
||||
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_NOTWEB;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function pathToAbsoluteURL($path) {
|
||||
return Url::pathToUri($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function pathToRelativePath($path) {
|
||||
|
||||
return preg_replace('/^' . preg_quote($this->_basepath, '/') . '/', '', str_replace('/', DIRECTORY_SEPARATOR, $path));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function pathToAbsolutePath($path) {
|
||||
|
||||
return $this->_basepath . str_replace('/', DIRECTORY_SEPARATOR, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function absoluteURLToPath($url) {
|
||||
|
||||
$fullUri = Url::getFullUri();
|
||||
if (substr($url, 0, strlen($fullUri)) == $fullUri) {
|
||||
|
||||
return str_replace($fullUri, $this->_basepath, $url);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function fileexists($file) {
|
||||
return is_file($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function safefileexists($file) {
|
||||
return realpath($file) && is_file($file);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $dir
|
||||
*
|
||||
* @return array Folder names without trailing slash
|
||||
*/
|
||||
public function folders($dir) {
|
||||
if (!is_dir($dir)) {
|
||||
return array();
|
||||
}
|
||||
$folders = array();
|
||||
foreach (scandir($dir) as $file) {
|
||||
if ($file == '.' || $file == '..') continue;
|
||||
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $folders[] = $file;
|
||||
}
|
||||
|
||||
return $folders;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_writable($path) {
|
||||
return is_writable($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createFolder($path) {
|
||||
|
||||
return mkdir($path, $this->dirPermission, true);
|
||||
}
|
||||
|
||||
public function deleteFolder($dir) {
|
||||
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
|
||||
foreach (scandir($dir) as $file) {
|
||||
if ($file == '.' || $file == '..') continue;
|
||||
if (!$this->deleteFolder($dir . DIRECTORY_SEPARATOR . $file)) {
|
||||
chmod($dir . DIRECTORY_SEPARATOR . $file, $this->dirPermission);
|
||||
if (!$this->deleteFolder($dir . DIRECTORY_SEPARATOR . $file)) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return rmdir($dir);
|
||||
}
|
||||
|
||||
public function existsFolder($path) {
|
||||
return is_dir($path);
|
||||
}
|
||||
|
||||
public function files($path) {
|
||||
$files = array();
|
||||
if (is_dir($path)) {
|
||||
if ($dh = opendir($path)) {
|
||||
while (($file = readdir($dh)) !== false) {
|
||||
if ($file[0] != ".") {
|
||||
$files[] = $file;
|
||||
}
|
||||
}
|
||||
closedir($dh);
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function existsFile($path) {
|
||||
|
||||
return file_exists($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function createFile($path, $buffer) {
|
||||
return file_put_contents($path, $buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function readFile($path) {
|
||||
return file_get_contents($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert dir alias to normal format
|
||||
*
|
||||
* @param $pathName
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function dirFormat($pathName) {
|
||||
return str_replace(".", DIRECTORY_SEPARATOR, $pathName);
|
||||
}
|
||||
|
||||
public function getImagesFolder() {
|
||||
return '';
|
||||
}
|
||||
|
||||
public function realpath($path) {
|
||||
return rtrim(realpath($path), '/\\');
|
||||
}
|
||||
|
||||
public function registerTranslate($from, $to) {
|
||||
$this->translate[$from] = $to;
|
||||
}
|
||||
|
||||
protected function trailingslashit($string) {
|
||||
return $this->untrailingslashit($string) . '/';
|
||||
}
|
||||
|
||||
protected function untrailingslashit($string) {
|
||||
return rtrim($string, '/\\');
|
||||
}
|
||||
|
||||
public function convertToRealDirectorySeparator($path) {
|
||||
return str_replace(DIRECTORY_SEPARATOR == '/' ? '\\' : '/', DIRECTORY_SEPARATOR, $path);
|
||||
}
|
||||
|
||||
public function get_temp_dir() {
|
||||
static $temp = '';
|
||||
if (defined('SS_TEMP_DIR')) return $this->trailingslashit(SS_TEMP_DIR);
|
||||
|
||||
if ($temp) return $this->trailingslashit($temp);
|
||||
|
||||
if (function_exists('sys_get_temp_dir')) {
|
||||
$temp = sys_get_temp_dir();
|
||||
if (@is_dir($temp) && $this->is_writable($temp)) return $this->trailingslashit($temp);
|
||||
}
|
||||
|
||||
$temp = ini_get('upload_tmp_dir');
|
||||
if (@is_dir($temp) && $this->is_writable($temp)) return $this->trailingslashit($temp);
|
||||
|
||||
$temp = $this->getNotWebCachePath() . '/';
|
||||
if (is_dir($temp) && $this->is_writable($temp)) return $temp;
|
||||
|
||||
return '/tmp/';
|
||||
}
|
||||
|
||||
public function tempnam($filename = '', $dir = '') {
|
||||
if (empty($dir)) {
|
||||
$dir = $this->get_temp_dir();
|
||||
}
|
||||
|
||||
if (empty($filename) || '.' == $filename || '/' == $filename || '\\' == $filename) {
|
||||
$filename = time();
|
||||
}
|
||||
|
||||
// Use the basename of the given file without the extension as the name for the temporary directory
|
||||
$temp_filename = basename($filename);
|
||||
$temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
|
||||
|
||||
// If the folder is falsey, use its parent directory name instead.
|
||||
if (!$temp_filename) {
|
||||
return $this->tempnam(dirname($filename), $dir);
|
||||
}
|
||||
|
||||
// Suffix some random data to avoid filename conflicts
|
||||
$temp_filename .= '-' . md5(uniqid(rand() . time()));
|
||||
$temp_filename .= '.tmp';
|
||||
$temp_filename = $dir . $temp_filename;
|
||||
|
||||
$fp = @fopen($temp_filename, 'x');
|
||||
if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
|
||||
return $this->tempnam($filename, $dir);
|
||||
}
|
||||
if ($fp) {
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
return $temp_filename;
|
||||
}
|
||||
}
|
@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Filesystem;
|
||||
|
||||
use Nextend\Framework\Filesystem\Joomla\JoomlaFilesystem;
|
||||
use Nextend\Framework\Filesystem\WordPress\WordPressFilesystem;
|
||||
|
||||
class Filesystem {
|
||||
|
||||
/**
|
||||
* @var AbstractPlatformFilesystem
|
||||
*/
|
||||
private static $platformFilesystem;
|
||||
|
||||
public function __construct() {
|
||||
self::$platformFilesystem = new WordPressFilesystem();
|
||||
|
||||
self::$platformFilesystem->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractPlatformFilesystem
|
||||
*/
|
||||
public static function get() {
|
||||
|
||||
return self::$platformFilesystem;
|
||||
}
|
||||
|
||||
public static function getPaths() {
|
||||
|
||||
return self::$platformFilesystem->getPaths();
|
||||
}
|
||||
|
||||
public static function check($base, $folder) {
|
||||
|
||||
self::$platformFilesystem->check($base, $folder);
|
||||
}
|
||||
|
||||
public static function measurePermission($testDir) {
|
||||
|
||||
self::$platformFilesystem->measurePermission($testDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function toLinux($path) {
|
||||
|
||||
return self::$platformFilesystem->toLinux($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function getBasePath() {
|
||||
|
||||
return self::$platformFilesystem->getBasePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*/
|
||||
public static function setBasePath($path) {
|
||||
|
||||
self::$platformFilesystem->setBasePath($path);
|
||||
}
|
||||
|
||||
public static function getWebCachePath() {
|
||||
|
||||
return self::$platformFilesystem->getWebCachePath();
|
||||
}
|
||||
|
||||
public static function getNotWebCachePath() {
|
||||
|
||||
return self::$platformFilesystem->getNotWebCachePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function pathToAbsoluteURL($path) {
|
||||
|
||||
return self::$platformFilesystem->pathToAbsoluteURL($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function pathToRelativePath($path) {
|
||||
|
||||
return self::$platformFilesystem->pathToRelativePath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function pathToAbsolutePath($path) {
|
||||
|
||||
return self::$platformFilesystem->pathToAbsolutePath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function absoluteURLToPath($url) {
|
||||
|
||||
return self::$platformFilesystem->absoluteURLToPath($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function fileexists($file) {
|
||||
|
||||
return self::$platformFilesystem->fileexists($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function safefileexists($file) {
|
||||
|
||||
return self::$platformFilesystem->safefileexists($file);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $dir
|
||||
*
|
||||
* @return array Folder names without trailing slash
|
||||
*/
|
||||
public static function folders($dir) {
|
||||
|
||||
return self::$platformFilesystem->folders($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_writable($path) {
|
||||
|
||||
return self::$platformFilesystem->is_writable($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function createFolder($path) {
|
||||
|
||||
return self::$platformFilesystem->createFolder($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $dir
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function deleteFolder($dir) {
|
||||
|
||||
return self::$platformFilesystem->deleteFolder($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function existsFolder($path) {
|
||||
|
||||
return self::$platformFilesystem->existsFolder($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function files($path) {
|
||||
|
||||
return self::$platformFilesystem->files($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function existsFile($path) {
|
||||
|
||||
return self::$platformFilesystem->existsFile($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function createFile($path, $buffer) {
|
||||
|
||||
return self::$platformFilesystem->createFile($path, $buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function readFile($path) {
|
||||
|
||||
return self::$platformFilesystem->readFile($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert dir alias to normal format
|
||||
*
|
||||
* @param $pathName
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function dirFormat($pathName) {
|
||||
|
||||
return self::$platformFilesystem->dirFormat($pathName);
|
||||
}
|
||||
|
||||
public static function getImagesFolder() {
|
||||
|
||||
return self::$platformFilesystem->getImagesFolder();
|
||||
}
|
||||
|
||||
public static function realpath($path) {
|
||||
|
||||
return self::$platformFilesystem->realpath($path);
|
||||
}
|
||||
|
||||
public static function registerTranslate($from, $to) {
|
||||
|
||||
self::$platformFilesystem->registerTranslate($from, $to);
|
||||
}
|
||||
|
||||
public static function convertToRealDirectorySeparator($path) {
|
||||
|
||||
return self::$platformFilesystem->convertToRealDirectorySeparator($path);
|
||||
}
|
||||
|
||||
public static function get_temp_dir() {
|
||||
|
||||
return self::$platformFilesystem->get_temp_dir();
|
||||
}
|
||||
|
||||
public static function tempnam($filename = '', $dir = '') {
|
||||
|
||||
return self::$platformFilesystem->tempnam($filename = '', $dir = '');
|
||||
}
|
||||
}
|
||||
|
||||
new Filesystem();
|
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Filesystem\WordPress;
|
||||
|
||||
use Nextend\Framework\Filesystem\AbstractPlatformFilesystem;
|
||||
use Nextend\Framework\Platform\Platform;
|
||||
use Nextend\Framework\Url\Url;
|
||||
use function get_current_blog_id;
|
||||
|
||||
class WordPressFilesystem extends AbstractPlatformFilesystem {
|
||||
|
||||
public function init() {
|
||||
|
||||
$this->paths[] = realpath(ABSPATH);
|
||||
|
||||
$this->_basepath = realpath(WP_CONTENT_DIR);
|
||||
|
||||
$this->paths[] = $this->_basepath;
|
||||
|
||||
$this->paths[] = realpath(WP_PLUGIN_DIR);
|
||||
|
||||
$wp_upload_dir = wp_upload_dir();
|
||||
|
||||
/**
|
||||
* Amazon S3 storage has s3://my-bucket/uploads upload path. If we found a scheme in the path we will
|
||||
* skip the realpath check so it won't fail in the future.
|
||||
* @url https://github.com/humanmade/S3-Uploads
|
||||
*/
|
||||
if (!stream_is_local($wp_upload_dir['basedir'])) {
|
||||
$uploadPath = $wp_upload_dir['basedir'];
|
||||
} else {
|
||||
$uploadPath = rtrim(realpath($wp_upload_dir['basedir']), "/\\");
|
||||
if (empty($uploadPath)) {
|
||||
echo 'Error: Your upload path is not valid or does not exist: ' . esc_html($wp_upload_dir['basedir']);
|
||||
$uploadPath = rtrim($wp_upload_dir['basedir'], "/\\");
|
||||
} else {
|
||||
$this->measurePermission($uploadPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos($this->_basepath, $uploadPath) !== 0) {
|
||||
$this->paths[] = $uploadPath;
|
||||
}
|
||||
}
|
||||
|
||||
public function getImagesFolder() {
|
||||
return Platform::getPublicDirectory();
|
||||
}
|
||||
|
||||
public function getWebCachePath() {
|
||||
if (is_multisite()) {
|
||||
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_WEB . get_current_blog_id();
|
||||
}
|
||||
|
||||
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_WEB;
|
||||
}
|
||||
|
||||
public function getNotWebCachePath() {
|
||||
if (is_multisite()) {
|
||||
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_NOTWEB . get_current_blog_id();
|
||||
}
|
||||
|
||||
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_NOTWEB;
|
||||
}
|
||||
|
||||
public function absoluteURLToPath($url) {
|
||||
$uris = Url::getUris();
|
||||
|
||||
for ($i = count($uris) - 1; $i >= 0; $i--) {
|
||||
$uri = $uris[$i];
|
||||
if (substr($url, 0, strlen($uri)) == $uri) {
|
||||
|
||||
return str_replace($uri, $this->paths[$i], $url);
|
||||
}
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function tempnam($filename = '', $dir = '') {
|
||||
return wp_tempnam($filename, $dir);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user