Example #1
0
 /**
  * Load a configuration file. In addition to the parameters described below two global variables can also affect loading:
  *
  *		$g_ui_locale - if it contains the current locale code, this code will be used when computing the MD5 signature of the current configuration for caching purposes. By setting this to the current locale simultaneous caching of configurations for various locales (eg. config files with gettext-translated strings in them) is enabled.
  *		$g_configuration_cache_suffix - any text it contains is used along with the configuration path and $g_ui_locale to compute the MD5 signature of the current configuration for caching purposes. By setting this to some value you can support simultaneous caching of configurations for several different modes. This is mainly used to support caching of theme-specific configurations. Since the theme can change based upon user agent, we need to potentially keep several computed configurations cached at the same time, one for each theme used.
  *
  * @param string $ps_file_path Absolute path to configuration file to parse
  * @param bool $pb_die_on_error If true, request processing will halt with call to die() on error in parsing config file
  * @param bool $pb_dont_cache If true, file will be parsed even if it's already cached
  *
  *
  */
 public function __construct($ps_file_path = __CA_APP_CONFIG__, $pb_die_on_error = false, $pb_dont_cache = false)
 {
     global $g_ui_locale, $g_configuration_cache_suffix;
     $this->ops_config_file_path = $ps_file_path ? $ps_file_path : __CA_APP_CONFIG__;
     # path to configuration file
     // cache key for on-disk caching
     $vs_path_as_md5 = md5($_SERVER['HTTP_HOST'] . $this->ops_config_file_path . '/' . $g_ui_locale . (isset($g_configuration_cache_suffix) ? '/' . $g_configuration_cache_suffix : ''));
     #
     # Is configuration file already cached?
     #
     $va_config_path_components = explode("/", $this->ops_config_file_path);
     $vs_config_filename = array_pop($va_config_path_components);
     $vs_local_conf_file_path = null;
     if (defined('__CA_LOCAL_CONFIG_DIRECTORY__') && file_exists(__CA_LOCAL_CONFIG_DIRECTORY__ . '/' . $vs_config_filename)) {
         $vs_local_conf_file_path = __CA_LOCAL_CONFIG_DIRECTORY__ . '/' . $vs_config_filename;
     } elseif (defined('__CA_DEFAULT_THEME_CONFIG_DIRECTORY__') && file_exists(__CA_DEFAULT_THEME_CONFIG_DIRECTORY__ . '/' . $vs_config_filename)) {
         $vs_local_conf_file_path = __CA_DEFAULT_THEME_CONFIG_DIRECTORY__ . '/' . $vs_config_filename;
     }
     // try to figure out if we can get it from cache
     if ((!defined('__CA_DISABLE_CONFIG_CACHING__') || !__CA_DISABLE_CONFIG_CACHING__) && !$pb_dont_cache) {
         self::loadConfigCacheInMemory();
         if ($vb_setup_has_changed = caSetupPhpHasChanged()) {
             self::clearCache();
         }
         if (!$vb_setup_has_changed && isset(self::$s_config_cache[$vs_path_as_md5])) {
             $vb_cache_is_invalid = false;
             $vs_config_mtime = caGetFileMTime($this->ops_config_file_path);
             if ($vs_config_mtime != self::$s_config_cache['mtime_' . $vs_path_as_md5]) {
                 // config file has changed
                 self::$s_config_cache['mtime_' . $vs_path_as_md5] = $vs_config_mtime;
                 $vb_cache_is_invalid = true;
             }
             if ($vs_local_conf_file_path) {
                 $vs_local_config_mtime = caGetFileMTime($vs_local_conf_file_path);
                 if ($vs_local_config_mtime != self::$s_config_cache['local_mtime_' . $vs_path_as_md5]) {
                     // local config file has changed
                     self::$s_config_cache['local_mtime_' . $vs_path_as_md5] = $vs_local_config_mtime;
                     $vb_cache_is_invalid = true;
                 }
             }
             if (!$vb_cache_is_invalid) {
                 // cache is ok
                 $this->ops_config_settings = self::$s_config_cache[$vs_path_as_md5];
                 $this->ops_md5_path = md5($this->ops_config_file_path);
                 return;
             }
         }
     }
     # load hash
     $this->ops_config_settings = array();
     # try loading global.conf file
     $vs_global_path = join("/", $va_config_path_components) . '/global.conf';
     if (file_exists($vs_global_path)) {
         $this->loadFile($vs_global_path, false);
     }
     //
     // Insert current user locale as constant into configuration.
     //
     $this->ops_config_settings['scalars']['LOCALE'] = $g_ui_locale;
     #
     # load specified config file
     #
     if (file_exists($this->ops_config_file_path) && $this->loadFile($this->ops_config_file_path, false)) {
         $this->ops_config_settings["ops_config_file_path"] = $this->ops_config_file_path;
     }
     #
     # try to load optional "local" config file (extra, optional, config file that can override values in the specified config file with "local" values)
     #
     if ($vs_local_conf_file_path) {
         $this->loadFile($vs_local_conf_file_path, false, false);
     }
     if ($vs_path_as_md5 && !$pb_dont_cache) {
         self::$s_config_cache[$vs_path_as_md5] = $this->ops_config_settings;
         // we loaded this cfg from file, so we have to write the
         // config cache to disk at least once on this request
         self::$s_have_to_write_config_cache = true;
     }
 }
Example #2
0
/**
 * Check if setup.php has changed since we last cached the mtime.
 * If it has, cache the new mtime
 * @return bool
 */
function caSetupPhpHasChanged()
{
    $vn_setup_mtime = caGetFileMTime(__CA_BASE_DIR__ . '/setup.php');
    if (!CompositeCache::contains('setup_php_mtime') || $vn_setup_mtime != CompositeCache::fetch('setup_php_mtime')) {
        CompositeCache::save('setup_php_mtime', $vn_setup_mtime, 'default', 0);
        return true;
    }
    return false;
}
 public static function lockAcquire()
 {
     // make absolutely sure the lock is released, even if a PHP error occurrs during script execution
     register_shutdown_function('ca_search_indexing_queue::lockRelease');
     if (function_exists('sem_get') && caGetOSFamily() == OS_POSIX) {
         if (!self::$s_lock_resource) {
             self::$s_lock_resource = sem_get(ftok(__FILE__, 'C'));
         }
         return sem_acquire(self::$s_lock_resource);
     } else {
         $vs_temp_file = caGetTempDirPath() . DIRECTORY_SEPARATOR . 'search_indexing_queue.lock';
         $vb_got_lock = (bool) (self::$s_lock_resource = @fopen($vs_temp_file, 'x'));
         // if we couldn't get the lock, check if the lock file is old (i.e. older than 5 minutes)
         // if that's the case, it's likely something went wrong and the lock hangs.
         // so we just kill it and try to re-acquire
         if (!$vb_got_lock && file_exists($vs_temp_file)) {
             if (time() - caGetFileMTime($vs_temp_file) > 5 * 60) {
                 self::lockRelease();
                 return self::lockAcquire();
             }
         }
         return $vb_got_lock;
     }
 }