File indexing completed on 2024-12-22 05:36:24

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Local_Application extends Zend_Application
0024 {
0025     const CACHE_APP_INI = 'cache_app_ini';
0026 
0027     /**
0028      *
0029      * @var Zend_Cache_Core|null
0030      */
0031     protected $_configCache;
0032 
0033     //Override
0034 
0035     /**
0036      * Constructor
0037      *
0038      * Initialize application. Potentially initializes include_paths, PHP
0039      * settings, and bootstrap class.
0040      *
0041      * @param  string                   $environment
0042      * @param  string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
0043      * @throws Zend_Application_Exception
0044      * @throws Zend_Cache_Exception
0045      */
0046     public function __construct($environment, $options = null)
0047     {
0048         $this->_configCache = $this->_initCache();
0049         parent::__construct($environment, $options);
0050     }
0051 
0052     /**
0053      * @return Zend_Cache_Core|Zend_Cache_Frontend
0054      * @throws Zend_Cache_Exception
0055      */
0056     protected function _initCache()
0057     {
0058         $frontendOptions = array(
0059             'lifetime'                => null,
0060             'automatic_serialization' => true
0061         );
0062 
0063         $backendOptions = array(
0064             'cache_dir'              => APPLICATION_CACHE,
0065             'file_locking'           => true,
0066             'read_control'           => true,
0067             'read_control_type'      => 'crc32',
0068             'hashed_directory_level' => 0,
0069             'hashed_directory_perm'  => 0700,
0070             'file_name_prefix'       => 'ocs',
0071             'cache_file_perm'        => 0700
0072         );
0073 
0074         return Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
0075     }
0076 
0077     /**
0078      * @return false|mixed|Zend_Config
0079      * @throws Zend_Cache_Exception
0080      */
0081     public function getApplicationConfig()
0082     {
0083         $cacheName = APPLICATION_ENV . '_' . self::CACHE_APP_INI;
0084         if (false === ($config = $this->_configCache->load($cacheName))) {
0085             $config = new Zend_Config($this->getOptions(), true);
0086             $this->_configCache->save($config, $cacheName, array(), 300);
0087         }
0088 
0089         return $config;
0090     }
0091 
0092     /**
0093      * Load configuration file of options
0094      *
0095      * @param  string $file
0096      *
0097      * @return array
0098      * @throws Zend_Application_Exception When invalid configuration file is provided
0099      * @throws Zend_Cache_Exception
0100      */
0101     protected function _loadConfig($file)
0102     {
0103         $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
0104 
0105         if ($this->_configCache === null OR $suffix == 'php' OR $suffix == 'inc') { //No need for caching those
0106             return parent::_loadConfig($file);
0107         }
0108 
0109         $cacheId = $this->_cacheId($file);
0110 
0111         if (false === ($config = $this->_configCache->load($cacheId))) {
0112             $config = parent::_loadConfig($file);
0113             $this->_configCache->save($config, $cacheId, array(), null);
0114         }
0115 
0116         return $config;
0117     }
0118 
0119     /**
0120      * @param $file
0121      * @return string
0122      */
0123     protected function _cacheId($file)
0124     {
0125         return 'config_' . $this->getEnvironment() . '_' . md5_file($file);
0126     }
0127 
0128     /**
0129      * @param string $file
0130      * @param        $cacheId
0131      *
0132      * @return bool|string
0133      */
0134     protected function _testCache($file, $cacheId)
0135     {
0136         $configMTime = filemtime($file);
0137 
0138         $cacheLastMTime = $this->_configCache->test($cacheId);
0139 
0140         if ($cacheLastMTime !== false AND $configMTime < $cacheLastMTime) { //Valid cache?
0141             return true;
0142         }
0143 
0144         return false;
0145     }
0146 
0147 }