Implemented temporal directories.

Cache should now be written to pre-defined directories.
This commit is contained in:
Abel Hoogeveen 2016-07-09 11:39:36 +02:00
parent 7fc7d63179
commit ed93cc3442
8 changed files with 43 additions and 132 deletions

10
.gitignore vendored
View File

@ -11,17 +11,9 @@ FuzeWorks.esproj/*
*.swp
*.out
# Framework Files
Core/Cache/
Core/Logs/Error.log
# Build Files
vendor/
build/
Modules/admin/themes/adminlte2.1/plugins/
Modules/admin/themes/adminlte2.1/dist/
Modules/admin/themes/adminlte2.1/bootstrap/
doc
nbproject
Application/Cache
Application/Logs
tests/temp

View File

@ -91,15 +91,7 @@ class FW_DB_Cache {
*/
public function check_path($path = '')
{
if ($path === '')
{
if ($this->db->cachedir === '')
{
return $this->db->cache_off();
}
$path = $this->db->cachedir;
}
$path = ($path === '' ? Core::$tempDir . DS . 'Database' : $path);
// Add a trailing slash to the path if needed
$path = realpath($path)
@ -108,10 +100,13 @@ class FW_DB_Cache {
if ( ! is_dir($path))
{
Logger::logDebug('DB cache path error: '.$path);
if (!mkdir($path, 0777, false))
{
Logger::logDebug('DB cache path error: '.$path);
// If the path is wrong we'll turn off caching
return $this->db->cache_off();
// If the path is wrong we'll turn off caching
return $this->db->cache_off();
}
}
if ( ! Core::isReallyWritable($path))

View File

@ -74,9 +74,7 @@ class FW_Cache_file extends FW_Driver {
// Load the required helpers
$this->factory->helpers->load('file');
$path = $this->factory->config->get('cache')->cache_file_path;
$this->_cache_path = ($path === '') ? Core::$appDir . DS . 'Cache/' : $path;
$this->_cache_path = Core::$tempDir . DS . 'CacheLibrary' . DS;
}
// ------------------------------------------------------------------------
@ -263,6 +261,14 @@ class FW_Cache_file extends FW_Driver {
*/
public function is_supported()
{
if ( ! is_dir($this->_cache_path))
{
if (!mkdir($this->_cache_path, 0777, false))
{
return false;
}
}
return is_really_writable($this->_cache_path);
}

View File

@ -60,9 +60,7 @@ class Configurator
return [
'appDir' => isset($trace[1]['file']) ? dirname($trace[1]['file']) : NULL,
'wwwDir' => isset($last['file']) ? dirname($last['file']) : NULL,
'debugMode' => $debugMode,
'productionMode' => !$debugMode,
'consoleMode' => PHP_SAPI === 'cli',
'debugMode' => $debugMode
];
}
@ -132,7 +130,6 @@ class Configurator
throw new Nette\InvalidArgumentException(sprintf('Value must be either a string, array, or boolean, %s given.', gettype($value)));
}
$this->parameters['debugMode'] = $value;
$this->parameters['productionMode'] = !$this->parameters['debugMode']; // compatibility
return $this;
}
@ -164,6 +161,7 @@ class Configurator
// First set all the directories
Core::$appDir = $this->parameters['appDir'];
Core::$wwwDir = $this->parameters['wwwDir'];
Core::$tempDir = $this->parameters['tempDir'];
// Then set debug mode
if ($this->parameters['debugMode'])

View File

@ -64,6 +64,8 @@ class Core
public static $coreDir;
public static $tempDir;
/**
* Initializes the core.
*

View File

@ -163,7 +163,7 @@ class Layout
}
// Then assign some basic variables for the template
self::$assigned_variables['viewDir'] = Config::get('main')->base_url.preg_replace('#/+#', '/', substr(self::$directory.'/', -strlen(self::$directory.'/')));
self::$assigned_variables['wwwDir'] = Config::get('main')->base_url;
self::$assigned_variables['siteURL'] = Config::get('main')->base_url;
self::$assigned_variables['serverName'] = Config::get('main')->server_name;
self::$assigned_variables['adminMail'] = Config::get('main')->administrator_mail;
@ -535,6 +535,7 @@ class Layout
namespace FuzeWorks\TemplateEngine;
use FuzeWorks\LayoutException;
use FuzeWorks\Core;
use Smarty;
use Latte\Engine as Latte;
@ -694,8 +695,8 @@ class SmartyEngine implements TemplateEngine
$this->smartyInstance = new Smarty();
// Then prepare all variables
$this->smartyInstance->setCompileDir('Core/Cache/Compile');
$this->smartyInstance->setCacheDir('Core/Cache/');
$this->smartyInstance->setCompileDir(Core::$tempDir . DS . 'Smarty' . DS . 'Compile');
$this->smartyInstance->setCacheDir(Core::$tempDir . DS . 'Smarty');
}
}
@ -788,7 +789,7 @@ class LatteEngine implements TemplateEngine
{
// If possible, load Latte\Engine
$this->latte = new Latte;
$this->latte->setTempDirectory(realpath('Application'.DS.'Cache'));
$this->latte->setTempDirectory(realpath(Core::$tempDir . DS . 'Latte'));
}
else
{

View File

@ -532,12 +532,22 @@ class Output {
*/
public function _write_cache($output)
{
$path = $this->config->cache->cache_file_path;
$cache_path = ($path === '') ? 'Application'.DS.'Cache'.DS : $path;
$cache_path = Core::$tempDir . DS . 'Output' . DS;
if ( ! is_dir($cache_path) OR ! Core::isReallyWritable($cache_path))
// First try and see if the directory exists
if ( ! is_dir($cache_path))
{
Logger::logError('Unable to write cache file: '.$cache_path);
// Then try and create the directory
if (!mkdir($cache_path, 0777, false))
{
Logger::logError('Unable to write cache file: \''.$cache_path.'\' Cannot create directory');
return;
}
}
if (! Core::isReallyWritable($cache_path))
{
Logger::logError('Unable to write cache file: \''.$cache_path.'\' Directory not writeable');
return;
}
@ -561,7 +571,7 @@ class Output {
if ( ! $fp = @fopen($cache_path, 'w+b'))
{
Logger::logError('Unable to write cache file: '.$cache_path);
Logger::logError('Unable to write cache file: \''.$cache_path.'\' Directory not writeable');
return;
}
@ -635,7 +645,7 @@ class Output {
*/
public function _display_cache()
{
$cache_path = ($this->config->cache->cache_file_path === '') ? 'Application'.DS.'Cache'.DS : $this->config->cache->cache_file_path;
$cache_path = Core::$tempDir . DS . 'Output' . DS;
// Build the file path. The file name is an MD5 hash of the full URI
$main = $this->config->main;
@ -715,11 +725,7 @@ class Output {
*/
public function delete_cache($uri = '')
{
$cache_path = $this->config->cache->cache_file_path;
if ($cache_path === '')
{
$cache_path = 'Application'.DS.'Cache'.DS;
}
$cache_path = Core::$tempDir . DS . 'Output' . DS;
if ( ! is_dir($cache_path))
{

View File

@ -1,89 +0,0 @@
<?php
/**
* FuzeWorks.
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
*
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
namespace FuzeWorks;
/**
* Class Configurator.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
class Configurator
{
protected $parameters;
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
}
/**
* @return array
*/
protected function getDefaultParameters()
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$last = end($trace);
$debugMode = static::detectDebugMode();
return [
'appDir' => isset($trace[1]['file']) ? dirname($trace[1]['file']) : NULL,
'wwwDir' => isset($last['file']) ? dirname($last['file']) : NULL,
'debugMode' => $debugMode,
'productionMode' => !$debugMode,
'consoleMode' => PHP_SAPI === 'cli',
];
}
/**
* Detects debug mode by IP address.
* @param string|array IP addresses or computer names whitelist detection
* @return bool
*/
public static function detectDebugMode($list = NULL)
{
$addr = isset($_SERVER['REMOTE_ADDR'])
? $_SERVER['REMOTE_ADDR']
: php_uname('n');
$secret = isset($_COOKIE[self::COOKIE_SECRET]) && is_string($_COOKIE[self::COOKIE_SECRET])
? $_COOKIE[self::COOKIE_SECRET]
: NULL;
$list = is_string($list)
? preg_split('#[,\s]+#', $list)
: (array) $list;
if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$list[] = '127.0.0.1';
$list[] = '::1';
}
return in_array($addr, $list, TRUE) || in_array("$secret@$addr", $list, TRUE);
}
}