Re: A patch to keep cache directory out of webroot

Marcel van der Boom <[email protected]> Sat, 02 Oct 2004 19:33:13 +0200
Newsgroups gmane.comp.cms.xaraya.patches
Organization Xaraya
Message-ID <[email protected]>
Jonn,

You should probably take this one. Couple of remarks:

- i think the cache location for the 'output' cache should be a mod var 
for the cache manager module if possible
- the cache location for compiled template can indeed be a config var, 
but if possible i would like to keep it out of  config.system.php, using 
our current value as the default in the absence of  config var.

Other than those, and given some more eyeballs, i dont see much problems.

marcel



Alla Bezroutchko wrote:

> Hello MrB or whoever is reading this,
> 
> Here is a patch that allows a configurable location of cache directory, 
> so it may be kept out of web root. As far as I can see it does not break 
> anything, but I only tested my setup, so more testing is necessary I guess.
> 
> I create a new system config variable and then use it everywhere where 
> the path to cache is needed.
> 
> The patch is made against xaraya-0.9.10-full.
> 
> Alla.
> 
> 
> ------------------------------------------------------------------------
> 
> diff -Nur /home/alla/xaraya-0.9.10/html/includes/caching/page.php html/includes/caching/page.php
> --- /home/alla/xaraya-0.9.10/html/includes/caching/page.php	Thu Sep  2 22:05:09 2004
> +++ html/includes/caching/page.php	Fri Oct  1 17:34:24 2004
> @@ -229,6 +229,8 @@
>   */
>  function xarPage_autoCacheLogStatus($status = 'MISS')
>  {
> +
> +    $varCacheDir =  = xarCore_getSystemVar('CacheDir');
>      if (!empty($_SERVER['REQUEST_METHOD']) &&
>          $_SERVER['REQUEST_METHOD'] == 'GET' &&
>      // the URL is one of the candidates for session-less caching
> @@ -245,20 +247,20 @@
>          global $xarPage_autoCachePeriod;
>  
>          if (!empty($xarPage_autoCachePeriod) &&
> -            filemtime('var/cache/output/autocache.start') < time() - $xarPage_autoCachePeriod) {
> -            @touch('var/cache/output/autocache.start');
> +            filemtime($varCacheDir . '/output/autocache.start') < time() - $xarPage_autoCachePeriod) {
> +            @touch($varCacheDir . '/output/autocache.start');
>  
>              // re-calculate Page.SessionLess based on autocache.log and save in config.caching.php
> -            $cachingConfigFile = 'var/cache/config.caching.php';
> +            $cachingConfigFile = $varCacheDir . '/config.caching.php';
>              if (file_exists($cachingConfigFile) &&
>                  is_writable($cachingConfigFile)) {
>  
>                  include $cachingConfigFile;
>                  if (!empty($cachingConfiguration['AutoCache.MaxPages']) &&
> -                    file_exists('var/cache/output/autocache.log') &&
> -                    filesize('var/cache/output/autocache.log') > 0) {
> +                    file_exists($varCacheDir . '/output/autocache.log') &&
> +                    filesize($varCacheDir . '/output/autocache.log') > 0) {
>  
> -                    $logs = @file('var/cache/output/autocache.log');
> +                    $logs = @file($varCacheDir . '/output/autocache.log');
>                      $autocacheproposed = array();
>                      foreach ($logs as $entry) {
>                          if (empty($entry)) continue;
> @@ -312,9 +314,9 @@
>                  }
>              }
>  
> -            $fp = @fopen('var/cache/output/autocache.log', 'w');
> +            $fp = @fopen($varCacheDir . '/output/autocache.log', 'w');
>          } else {
> -            $fp = @fopen('var/cache/output/autocache.log', 'a');
> +            $fp = @fopen($varCacheDir . '/output/autocache.log', 'a');
>          }
>          if ($fp) {
>              @fwrite($fp, "$time $status $addr $url\n");
> diff -Nur /home/alla/xaraya-0.9.10/html/includes/xarCache.php html/includes/xarCache.php
> --- /home/alla/xaraya-0.9.10/html/includes/xarCache.php	Thu Sep  2 22:05:08 2004
> +++ html/includes/xarCache.php	Fri Oct  1 17:39:11 2004
> @@ -35,7 +35,9 @@
>      global $xarBlock_cacheTime;
>      global $xarPage_autoCachePeriod;
>  
> -    if (!@include_once('var/cache/config.caching.php')) {
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
> +
> +    if (!@include_once($varCacheDir . '/config.caching.php')) {
>          // if the config file is missing, turn caching off
>          @unlink($cacheDir . '/cache.touch');
>          return FALSE;
> @@ -98,7 +100,7 @@
>  
>              xarPage_httpCacheHeaders($cache_file);
>  
> -            if (file_exists('var/cache/output/autocache.start')) {
> +            if (file_exists($varCacheDir . '/output/autocache.start')) {
>                  xarPage_autoCacheLogStatus('HIT');
>              }
>  
> diff -Nur /home/alla/xaraya-0.9.10/html/includes/xarTemplate.php html/includes/xarTemplate.php
> --- /home/alla/xaraya-0.9.10/html/includes/xarTemplate.php	Thu Sep  2 22:05:08 2004
> +++ html/includes/xarTemplate.php	Fri Oct  1 16:56:52 2004
> @@ -53,7 +53,7 @@
>   *
>   */
>  // Let's do this once here, not scattered all over the place
> -define('XAR_TPL_CACHE_DIR',xarCoreGetVarDirPath() . '/cache/templates');
> +define('XAR_TPL_CACHE_DIR', xarCore_getSystemVar('CacheDir') . '/templates');
>  
>  /**
>   * Initializes the BlockLayout Template Engine
> diff -Nur /home/alla/xaraya-0.9.10/html/index.php html/index.php
> --- /home/alla/xaraya-0.9.10/html/index.php	Thu Sep  2 22:05:07 2004
> +++ html/index.php	Fri Oct  1 17:41:46 2004
> @@ -13,22 +13,32 @@
>   */
>  
>  /**
> + * Load the Xaraya core
> + */
> +
> +/* Doing this before xarCache_init to have cache dir
> + * configured via system config
> + * This does not seem to break anything in my install
> +*/
> + 
> +include 'includes/xarCore.php';
> +
> +
> +/**
>   * Set up output caching if enabled
>   */
> -if (file_exists('var/cache/output/cache.touch')) {
> +
> +
> +$varCacheDir = xarCore_getSystemVar('CacheDir');
> +if (file_exists($varCacheDir . '/output/cache.touch')) {
>      include_once('includes/xarCache.php');
>      // Note : we may already exit here if session-less page caching is enabled
> -    if (xarCache_init(array('cacheDir' => 'var/cache/output'))) {
> +    if (xarCache_init(array('cacheDir' => $varCacheDir . '/output'))) {
>          define('XARCACHE_IS_ENABLED',1);
>      }
>  }
>  
>  /**
> - * Load the Xaraya core
> - */
> -include 'includes/xarCore.php';
> -
> -/**
>   * Main Xaraya Entry
>   *
>   * @access public
> Binary files /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/.updateconfig.php.swp and html/modules/xarcachemanager/xaradmin/.updateconfig.php.swp differ
> diff -Nur /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/modifyconfig.php html/modules/xarcachemanager/xaradmin/modifyconfig.php
> --- /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/modifyconfig.php	Thu Sep  2 21:55:41 2004
> +++ html/modules/xarcachemanager/xaradmin/modifyconfig.php	Fri Oct  1 17:12:40 2004
> @@ -17,7 +17,7 @@
>  
>      $data = array();
>  
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>      
>      if (file_exists($varCacheDir . '/output/cache.touch')) {
>          $data['CachingEnabled'] = 1;
> diff -Nur /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/pages.php html/modules/xarcachemanager/xaradmin/pages.php
> --- /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/pages.php	Thu Sep  2 21:55:41 2004
> +++ html/modules/xarcachemanager/xaradmin/pages.php	Fri Oct  1 17:13:06 2004
> @@ -11,7 +11,7 @@
>  
>      $data = array();
>  
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>      if (file_exists($varCacheDir . '/output/cache.pagelevel')) {
>          $iscached = 1;
>      } else {
> diff -Nur /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/updateconfig.php html/modules/xarcachemanager/xaradmin/updateconfig.php
> --- /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradmin/updateconfig.php	Thu Sep  2 21:55:41 2004
> +++ html/modules/xarcachemanager/xaradmin/updateconfig.php	Fri Oct  1 17:46:15 2004
> @@ -23,7 +23,7 @@
>      if (!xarSecurityCheck('AdminXarCache')) return;
>  
>      // set the cache dir
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>      $outputCacheDir = $varCacheDir . '/output';
>  
>      // turn output caching system on or off
> diff -Nur /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradminapi/getmenulinks.php html/modules/xarcachemanager/xaradminapi/getmenulinks.php
> --- /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xaradminapi/getmenulinks.php	Thu Sep  2 21:55:41 2004
> +++ html/modules/xarcachemanager/xaradminapi/getmenulinks.php	Fri Oct  1 17:15:38 2004
> @@ -22,7 +22,7 @@
>                                                    'flushcache'),
>                               'title' => xarML('Flush the output cache of xarCache'),
>                               'label' => xarML('Flush Cache'));
> -        $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +        $varCacheDir = xarCore_getSystemVar('CacheDir');
>          if (file_exists($varCacheDir . '/output/cache.pagelevel')) {
>              $menulinks[] = Array('url'   => xarModURL('xarcachemanager',
>                                                        'admin',
> diff -Nur /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xareventapi.php html/modules/xarcachemanager/xareventapi.php
> --- /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xareventapi.php	Thu Sep  2 21:55:41 2004
> +++ html/modules/xarcachemanager/xareventapi.php	Fri Oct  1 17:35:15 2004
> @@ -11,8 +11,9 @@
>   */
>  
>  // Only define this event handler if auto-caching is enabled
> +$varCacheDir = xarCore_getSystemVar('CacheDir');
>  if (defined('XARCACHE_IS_ENABLED') &&
> -    file_exists('var/cache/output/autocache.start')) {
> +    file_exists($varCacheDir . '/output/autocache.start')) {
>  /**
>   * Log the URL requested by this first-time visitor
>   * @return Boolean
> diff -Nur /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xarinit.php html/modules/xarcachemanager/xarinit.php
> --- /home/alla/xaraya-0.9.10/html/modules/xarcachemanager/xarinit.php	Thu Sep  2 21:55:41 2004
> +++ html/modules/xarcachemanager/xarinit.php	Fri Oct  1 17:18:16 2004
> @@ -18,7 +18,7 @@
>  function xarcachemanager_init()
>  {
>      // set up the output cache directory
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>  
>      if (is_writable($varCacheDir) || is_dir($varCacheDir.'/output')) {
>          if (!is_dir($varCacheDir.'/output')) {
> @@ -240,7 +240,7 @@
>   */
>  function xarcachemanager_upgrade($oldversion)
>  {
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>      $defaultConfigFile = 'modules/xarcachemanager/config.caching.php.dist';
>      $cachingConfigFile = $varCacheDir . '/config.caching.php';
>      
> @@ -504,7 +504,7 @@
>  function xarcachemanager_delete()
>  {
>      //if still there, remove the cache.touch file, this turns everything off
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>      if (file_exists($varCacheDir . '/output') && is_dir($varCacheDir . '/output')) {
>          if (file_exists($varCacheDir . '/output/cache.touch')) {
>              @unlink($varCacheDir . '/output/cache.touch');
> diff -Nur /home/alla/xaraya-0.9.10/html/upgrade.php html/upgrade.php
> --- /home/alla/xaraya-0.9.10/html/upgrade.php	Thu Sep  2 22:05:08 2004
> +++ html/upgrade.php	Fri Oct  1 17:22:13 2004
> @@ -1234,7 +1234,7 @@
>      // If output caching if enabled, check to see if the table xar_cache_blocks exists.
>      // If it does not exist, disable output caching so that xarcachemanager can be upgraded.
>      echo "<h5>Checking xarCache State</h5>";
> -    $varCacheDir = xarCoreGetVarDirPath() . '/cache';
> +    $varCacheDir = xarCore_getSystemVar('CacheDir');
>      if (file_exists($varCacheDir . '/output/cache.touch')) {
>          echo "Output caching enabled, checking for required table...<br/>";
>          $dbconn =& xarDBGetConn();
> diff -Nur /home/alla/xaraya-0.9.10/html/var/config.system.php html/var/config.system.php
> --- /home/alla/xaraya-0.9.10/html/var/config.system.php	Thu Sep  2 22:05:25 2004
> +++ html/var/config.system.php	Fri Oct  1 16:54:15 2004
> @@ -1,7 +1,7 @@
>  <?php
>  // Database Configuration
>  // Database Username: the username that will be used to connect to the database.
> -$systemConfiguration['DB.UserName'] = '';
> +$systemConfiguration['DB.UserName'] = 'root';
>  // Database Password: the password that will be used to connect to the database.
>  $systemConfiguration['DB.Password'] = '';
>  // Database Host: the host name where the database server is located.
> @@ -50,6 +50,16 @@
>  */
>  $systemConfiguration['DB.UseADODBCache'] = false;
>  
> +/*
> + * This value tells the system where the cache directory is located
> + * Thus it is possible to keep the writable cache directory
> + * outside of web root. It is particularly useful on Windows/IIS
> + * where a writable directory means an ability to upload files
> + * It can be set to var/cache to have the cache in its normal place
> +*/
> +
> +$systemConfiguration['CacheDir'] = '/var/www/cache';
> +
>  
>  /*
>   * This value is used to change the default behavior of the