ret = 'failed';
// Remeber path
// Check if path exists
if (isDirectory(getConfig('CACHE_PATH'))) {
// Is there a .htaccess file?
if (isFileReadable(getConfig('CACHE_PATH') . '.htaccess')) {
// All done!
$this->ret = $this->statusDone;
} else {
// Stop! Set a .htaccess file first
$this->ret = 'htaccess';
}
} // END - if
}
// Checks validity of cache file and if content is given
function loadCacheFile ($cacheName) {
// Remember cache file
$this->name = $cacheName;
// Construct FQFN (full qualified file name)
$this->fqfn = getConfig('CACHE_PATH') . $cacheName . $this->extension;
// Check if file exists and if version matches
if (!isset($this->status[$cacheName])) {
// Pre-fetch cache here if found
if ($this->isCacheReadable()) $this->getArrayFromCache();
//* DEBUG: */ print($cacheName.'='.intval($this->isCacheReadable()).'/'.intval(is_writeable($this->fqfn)).'/'.intval($this->extensionVersionMatches('cache')).'
');
$this->status[$cacheName] = ($this->isCacheReadable() && (is_writeable($this->fqfn)) && ($this->extensionVersionMatches('cache')));
} // END - if
//* DEBUG: */ print($cacheName.'='.intval($this->status[$cacheName]).'
');
// Return status
return $this->status[$cacheName];
}
// Initializes the cache file
function init () {
// This will destory an existing cache file!
if ($this->ret == $this->statusDone) {
// Reset read status
$this->resetCacheReadStatus();
// Create file
if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
$this->pointer = fopen($this->fqfn, 'w') or app_die(__METHOD__, __LINE__, "Cannot write to cache ".$this->fqfn." !");
// Add open PHP tag
fwrite($this->pointer, "' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED');
}
}
// Reset the read status
function resetCacheReadStatus () {
unset($this->readable[$this->name]);
unset($GLOBALS['file_readable'][$this->fqfn]);
unset($this->status[$this->name]);
}
function addRow ($data) {
// Is the pointe rvalid?
if (is_resource($this->pointer)) {
// Write every array element to cache file
foreach ($data as $k => $v) {
// Write global cache array for immediate access
if ((substr($k, 0, 4) == 'ext_') && (isset($data['ext_name'])) && (isset($data['ext_id']))) {
if ($k == 'ext_name') {
$GLOBALS['cache_array']['extension'][$k][$data['ext_id']] = $v;
} else {
$GLOBALS['cache_array']['extension'][$k][$data['ext_name']] = $v;
}
if (($k == 'ext_keep') && ($v == 'Y')) {
$GLOBALS['cache_array']['always_active'][$data['ext_name']] = $v;
} // END - if
} elseif ($this->name == 'config') {
// Configuration
$GLOBALS['cache_array']['config'][$data['config']][$k] = $v;
} elseif ($this->name == 'filter') {
// Filter
$GLOBALS['cache_array']['filter']['chains'][$data['filter_name']][$data['filter_function']] = $data['filter_active'];
$GLOBALS['cache_array']['filter']['counter'][$data['filter_name']][$data['filter_function']] = $data['filter_counter'];
$GLOBALS['cache_array']['filter']['loaded'][$data['filter_name']][$data['filter_function']] = true;
} elseif ($this->name == 'modules') {
// Modules
$GLOBALS['cache_array']['modules'][$k][$data['module']] = $v;
} elseif ($this->name == 'admin') {
// Modules
if ($k == 'login') {
$GLOBALS['cache_array']['admin'][$k][$data['admin_id']] = $v;
} else {
$GLOBALS['cache_array']['admin'][$k][$data['login']] = $v;
}
} elseif ($this->name == 'refdepths') {
// Referal levels
$GLOBALS['cache_array']['refdepths'][$k][$data['id']] = $v;
} elseif ($this->name == 'refsystem') {
// Referal system
$GLOBALS['cache_array']['refsystem'][$k][$data['id']] = $v;
} elseif ($this->name == 'revision') {
// Revision data
$GLOBALS['cache_array']['revision'][$k][0] = $v;
} elseif ($this->name == 'themes') {
// Themes
if ($k == 'theme_path') {
$GLOBALS['cache_array']['themes'][$k][$data['id']] = $v;
} else {
$GLOBALS['cache_array']['themes'][$k][$data['theme_path']] = $v;
}
} elseif ($this->name == 'imprint') {
// Imprint
$GLOBALS['cache_array']['imprint'][$k][$data['imprint_id']] = $v;
} elseif (is_array($v)) {
// Serialize and BASE64-encode the array
$v = base64_encode(serialize($v));
} else {
// Finialize the cache and close it
$this->finalize();
// Remove cache
$this->removeCacheFile(true);
// Unsupported cache found!
debug_report_bug('Unsupported cache ' . $this->name . ' detected.');
}
// Write cache line to file
fwrite($this->pointer, $this->rewriteEntry($k, $v));
} // END - foreach
} else {
// Cannot create file
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED');
}
}
function finalize () {
// Quit function when no pointer is set
if (is_resource($this->pointer)) {
// Add default depency
$this->storeExtensionVersion('cache');
// Write footer
fwrite($this->pointer, "?>\n");
// Close file add destroy handler
fclose($this->pointer);
// Reset readable status
$this->resetCacheReadStatus();
// Set rights
if ($this->isCacheReadable()) changeMode($this->fqfn, 0666);
// Remove pointer and status
unset($this->status[$this->name]);
$this->pointer = false;
//* DEBUG: */ outputHtml(__METHOD__ . '(' . __LINE__."): {$this->name} - FINALIZED!
");
} // END - if
}
function getArrayFromCache () {
// Is the cache already loaded?
if (isset($this->data[$this->name])) {
// Return it's content!
return $this->data[$this->name];
} // END - if
// Is the cache file there?
if ($this->isCacheReadable()) {
// Load cache file
include($this->fqfn);
// Is there an array?
if (isset($this->data[$this->name])) {
// Cache version found?
if (isset($this->version[$this->name])) {
// Check it here if cache matches ext-cache version
if (!$this->extensionVersionMatches('cache')) {
// Invalid
logDebugMessage(__METHOD__, __LINE__, 'Invalid cache data ' . $this->name . ' detected.');
$this->removeCacheFile();
} // END - if
} // END - if
// Return cache if detected
if (isset($this->data[$this->name])) {
return $this->data[$this->name];
} else {
// Damaged!
logDebugMessage(__METHOD__, __LINE__, 'Possible damaged cache ' . $this->name . ' detected.');
$this->removeCacheFile();
}
} // END - if
} else {
// Cache file not found or not readable
debug_report_bug($this->name);
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): ' . getMaskedMessage('CACHE_CANNOT_LOAD', $this->fqfn));
// Try to remove it
$this->removeCacheFile();
}
// Always return an empty array if we have trouble or no data
return array();
}
// Destroy an existing cache file
function removeCacheFile ($force = false) {
// Reset read status
$this->resetCacheReadStatus();
// Debug message
/* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("%s should be removed.", $this->name));
// Is the cache file not yet rebuilt?
if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
// Only run in regular output mode
if ((getOutputMode() != 0) && ($force === false)) {
// Debug message if allowed
if (isDebugModeEnabled()) {
// Debug message
logDebugMessage(__METHOD__, __LINE__, 'Not removing cache ' . $this->name . ' in output_mode=' . getOutputMode());
} // END - if
// Abort here
return;
} // END - if
// Close cache
$this->finalize();
// Debug-mode enabled?
if (isDebugModeEnabled()) {
// Log removal of cache
logDebugMessage(__METHOD__, __LINE__, 'removing cache: ' . $this->name);
} // END - if
// Remove cache file from system
removeFile($this->fqfn);
// Reset read status
$this->resetCacheReadStatus();
// Is the file there?
if (!$this->isCacheReadable()) {
// The cache does no longer exist so kill the content
unset($this->data[$this->name]);
unset($this->version[$this->name]);
$this->rebuilt[$this->name] = true;
} else {
// Not removed!
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): ' . getMaskedMessage('CACHE_CANNOT_UNLINK', $this->fqfn));
}
} // END - if
}
// Unused method:
function removeEntry ($search, $data, $array) {
if ($this->status[$this->name] == $this->statusDone) {
// Load cache into dummy array
$dummy = $this->getArrayFromCache();
// Search for key in array
$key = array_search($data, $dummy[$search]);
if (!empty($key)) {
// Key (hopefully) found?
foreach ($array as $a) {
// So we can remove all elements as requested
unset($dummy[$a][$key]);
} // END - foreach
// Flush array to cache file
$this->init();
// Write array out
$this->writeArray($dummy);
// Close cache file
$this->finalize();
}
} else {
// Cannot write to cache!
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED');
}
}
function writeArray ($array) {
if (is_resource($this->pointer)) {
foreach ($array as $k => $v) {
if (is_array($v)) {
// Multi line(s) found
$LINE = '';
foreach($v as $k2 => $v2) {
// Put every array element in a row...
$LINE .= $this->rewriteEntry($k, $v2);
}
} else {
// Single line found
$LINE = $this->rewriteEntry($k, $v);
}
// Write line(s)
fwrite($this->pointer, $LINE);
} // END - foreach
} else {
// Cannot write array!
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED');
}
}
// Unused method
function replaceEntry ($search, $replace, $search_key, $array) {
if ($this->status[$this->name] == $this->statusDone) {
// Load cache into dummy array
$dummy = $this->getArrayFromCache();
// Check if $dummy is valid (prevents some errors)
if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
// Search for key in array
$key_found = array_key_exists($search_key, $dummy[$search]);
if ($key_found == true) {
$key = $search_key;
// Key (hopefully) found?
foreach ($dummy as $a => $v) {
// So we can update all entries
if ($a == $search) {
// Update now...
$dummy[$a][$search_key] = $replace;
} // END - if
} // END - foreach
// Flush array to cache file
$this->init();
// Write array out
$this->writeArray($dummy);
// Close cache file
$this->finalize();
} // END - if
} // END - if
} else {
// Cannot write to cache!
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED');
}
}
// Writes the version of given extension to the cache file
function storeExtensionVersion ($ext_name) {
// Valid cache pointer?
if (is_resource($this->pointer)) {
// Write only if extension is installed
if (isExtensionInstalled($ext_name)) {
// Get extension version
$ext_ver = getExtensionVersion($ext_name);
// Add the extension version to object (DO NOT REMOVE IT! Endless loop...)
$this->version[$this->name][$ext_name] = $ext_ver;
// Write cache line to file
fwrite($this->pointer, $this->rewriteEntry($ext_name, $ext_ver, 'version', true));
} // END - if
//* DEBUG: */ outputHtml(__METHOD__ . '(' . __LINE__."): {$this->name} - {$ext_name}={$ext_ver}
");
} else {
// Cannot create file
addFatalMessage(__METHOD__, __LINE__, '(' . __LINE__ . '): {--CACHE_PROBLEMS_DETECTED');
}
}
// Checks wether versions from cache and extension matches
function extensionVersionMatches ($ext_name) {
// Check cache
if (!isset($GLOBALS[__METHOD__][$ext_name])) {
// Does never match by default
$GLOBALS[__METHOD__][$ext_name] = false;
// Compare only if installed
if (isExtensionInstalled($ext_name)) {
// Get extension version
$ext_ver = getExtensionVersion($ext_name);
// Debug messages
if (isset($this->version[$this->name][$ext_name])) {
// Does it match?
$GLOBALS[__METHOD__][$ext_name] = ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
} elseif ($this->isCacheReadable()) {
// No cache version found!
logDebugMessage(__METHOD__, __LINE__, "Cache {$this->name} has missing version entry for extension {$ext_name}! Purging cache...");
// Remove the cache file
$this->removeCacheFile(true);
}
} else {
// Not installed, does always match
$GLOBALS[__METHOD__][$ext_name] = true;
}
} else {
// Cache entry found, log debug message
//* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, 'ext_name=' . $ext_name . ', matches=' . intval($GLOBALS[__METHOD__][$ext_name]));
}
// Compare both
return $GLOBALS[__METHOD__][$ext_name];
}
// Rewrit the entry so it can be stored in cache file
// @TODO Add support for more types which break in last else-block
function rewriteEntry ($key, $value, $prefix = 'data', $single = false) {
// Default is not single entry
$extender = '[]';
// Add only for single array entry?
if ($single === true) $extender = '';
// Init line
$line = '';
// String or non-string? ;-)
if (is_string($value)) {
// String...
$line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = '" . escapeQuotes($value) . "';\n";
} elseif (is_null($value)) {
// Null
$line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = null;\n";
} elseif (is_bool($value)) {
// Boolean value
if ($value === true) {
// True
$line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = true;\n";
} else {
// False
$line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . " = false;\n";
}
} elseif (isset($value[0])) {
// These lines needs fixing
debug_report_bug('Invalid entry with [0] found. key=' . $key);
} else {
// Non-string
$line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = ' . $value . ";\n";
}
// Return line
return $line;
}
// Getter for cache status
function getStatus () {
return $this->ret;
}
// Checks wether the current cache file is readable
function isCacheReadable () {
// Array entry found?
if (!isset($this->readable[$this->name])) {
// Not found, so create it
$this->readable[$this->name] = isFileReadable($this->fqfn);
} // END - if
// Return result
return $this->readable[$this->name];
}
} // END - class
// [EOF]
?>