File indexing completed on 2024-12-15 05:21:37

0001 <?php
0002 
0003 /**
0004  *
0005  *   ocs-apiserver
0006  *
0007  *   Copyright 2016 by pling GmbH.
0008  *
0009  *    This file is part of ocs-apiserver.
0010  *
0011  *    This program is free software: you can redistribute it and/or modify
0012  *    it under the terms of the GNU Affero General Public License as
0013  *    published by the Free Software Foundation, either version 3 of the
0014  *    License, or (at your option) any later version.
0015  *
0016  *    This program is distributed in the hope that it will be useful,
0017  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0018  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019  *    GNU Affero General Public License for more details.
0020  *
0021  *    You should have received a copy of the GNU Affero General Public License
0022  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0023  *
0024  */
0025 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
0026 {
0027     /**
0028      * @var Zend_Controller_Router_Rewrite
0029      */
0030     protected $_router = false;
0031 
0032     /**
0033      * @throws Zend_Exception
0034      * @throws Zend_Session_Exception
0035      */
0036     protected function _initSessionManagement()
0037     {
0038 //        $session = $this->bootstrap('session');
0039 //        $domain = Local_Tools_ParseDomain::get_domain();
0040 //        Zend_Session::setOptions(array('cookie_domain'   => $domain));
0041 //        Zend_Session::start();
0042         Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_NonPersistent());
0043     }
0044 
0045     /**
0046      * @return mixed
0047      */
0048     protected function _initConfig()
0049     {
0050         /** $config Zend_Config */
0051         $config = $this->getApplication()->getApplicationConfig();
0052         Zend_Registry::set('config', $config);
0053 
0054         return $config;
0055     }
0056 
0057     /**
0058      * @throws Zend_Application_Bootstrap_Exception
0059      */
0060     protected function _initLogger()
0061     {
0062         /** @var Zend_Log $logger */
0063         $logger = $this->getPluginResource('log')->getLog();
0064         $logger->registerErrorHandler();
0065         Zend_Registry::set('logger', $logger);
0066     }
0067 
0068     /**
0069      * @return mixed|null|Zend_Cache_Core|Zend_Cache_Frontend
0070      * @throws Zend_Cache_Exception
0071      * @throws Zend_Exception
0072      */
0073     protected function _initCache()
0074     {
0075         if (Zend_Registry::isRegistered('cache')) {
0076             return Zend_Registry::get('cache');
0077         }
0078 
0079         $cache = null;
0080         $options = $this->getOption('settings');
0081 
0082         if (true == $options['cache']['enabled']) {
0083             $cache = Zend_Cache::factory(
0084                 $options['cache']['frontend']['type'],
0085                 $options['cache']['backend']['type'],
0086                 $options['cache']['frontend']['options'],
0087                 $options['cache']['backend']['options']
0088             );
0089         } else {
0090             // Fallback settings for some (maybe development) environments which have no cache management installed.
0091 
0092             if (false === is_writeable(APPLICATION_CACHE)) {
0093                 error_log('directory for cache files does not exists or not writable: ' . APPLICATION_CACHE);
0094                 exit('directory for cache files does not exists or not writable: ' . APPLICATION_CACHE);
0095             }
0096 
0097             $frontendOptions = array(
0098                 'lifetime'                => 600,
0099                 'automatic_serialization' => true,
0100                 'cache_id_prefix'         => 'front_cache',
0101                 'cache'                   => true
0102             );
0103 
0104             $backendOptions = array(
0105                 'cache_dir'              => APPLICATION_CACHE,
0106                 'file_locking'           => true,
0107                 'read_control'           => true,
0108                 'read_control_type'      => 'crc32',
0109                 'hashed_directory_level' => 1,
0110                 'hashed_directory_perm'  => 0700,
0111                 'file_name_prefix'       => 'ocs',
0112                 'cache_file_perm'        => 0700
0113             );
0114 
0115             $cache = Zend_Cache::factory(
0116                 'Core',
0117                 'File',
0118                 $frontendOptions,
0119                 $backendOptions
0120             );
0121         }
0122 
0123         Zend_Registry::set('cache', $cache);
0124 
0125         Zend_Locale::setCache($cache);
0126         Zend_Locale_Data::setCache($cache);
0127         Zend_Currency::setCache($cache);
0128         Zend_Translate::setCache($cache);
0129         Zend_Translate_Adapter::setCache($cache);
0130         Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
0131         Zend_Paginator::setCache($cache);
0132 
0133         return $cache;
0134     }
0135 
0136     /**
0137      * @throws Zend_Application_Bootstrap_Exception
0138      * @throws Zend_Exception
0139      */
0140     protected function _initDbAdapter()
0141     {
0142         $db = $this->bootstrap('db')->getResource('db');
0143 
0144         if ((APPLICATION_ENV == 'development')) {
0145             $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
0146             $profiler->setEnabled(true);
0147 
0148             // Attach the profiler to your db adapter
0149             $db->setProfiler($profiler);
0150         }
0151 
0152         Zend_Registry::set('db', $db);
0153         Zend_Db_Table::setDefaultAdapter($db);
0154         Zend_Db_Table_Abstract::setDefaultAdapter($db);
0155         
0156         
0157         $config = Zend_Registry::get('config');
0158         //$db2 = $this->bootstrap('db')->getResource('db2');
0159         try {
0160             $db2 = Zend_Db::factory($config->settings->db2->adapter, array(
0161                 'host'     => $config->settings->db2->params->host,
0162                 'username' => $config->settings->db2->params->username,
0163                 'password' => $config->settings->db2->params->password,
0164                 'dbname'   => $config->settings->db2->params->dbname,
0165                 'charset'  => $config->settings->db2->params->charset,
0166                 'type'  => $config->settings->db2->params->type,
0167                 'persistent'  => $config->settings->db2->params->persistent,
0168                 'isDefaultTableAdapter' => FALSE
0169             ));
0170 
0171             Zend_Registry::set('db2', $db2);
0172             
0173             $db2->getConnection();
0174             
0175         } catch (Zend_Db_Adapter_Exception $e) {
0176             Zend_Registry::get('logger')->err('Error Init DB2: '. $e->getTraceAsString());
0177             //$e->getMessage();
0178         } catch (Zend_Exception $e) {
0179             Zend_Registry::get('logger')->err('Error Init DB2: '. $e->getTraceAsString());
0180            //$e->getMessage();
0181         }
0182         
0183     }
0184 
0185     protected function _initRouter()
0186     {
0187         /** @var Zend_Cache_Core $cache */
0188         $cache = Zend_Registry::get('cache');
0189 
0190         $this->_router = $cache->load('ocs_api_router');
0191 
0192         $bootstrap = $this;
0193         $bootstrap->bootstrap('FrontController');
0194         if (false === $this->_router) {
0195             $this->_router = $bootstrap->getContainer()->frontcontroller->getRouter();
0196 
0197             $options = $this->getOptions()['resources']['router'];
0198             if (!isset($options['routes'])) {
0199                 $options['routes'] = array();
0200             }
0201 
0202             if (isset($options['chainNameSeparator'])) {
0203                 $this->_router->setChainNameSeparator($options['chainNameSeparator']);
0204             }
0205 
0206             if (isset($options['useRequestParametersAsGlobal'])) {
0207                 $this->_router->useRequestParametersAsGlobal($options['useRequestParametersAsGlobal']);
0208             }
0209 
0210             $this->_router->addConfig(new Zend_Config($options['routes']));
0211             $cache->save($this->_router, 'ocs_api_router', array(), 7200);
0212         }
0213 
0214         $this->getContainer()->frontcontroller->setRouter($this->_router);
0215 
0216         return $this->_router;
0217     }
0218 
0219     protected function _initGlobalAppConst()
0220     {
0221         $appConfig = $this->getResource('config');
0222 
0223         $imageConfig = $appConfig->images;
0224         defined('IMAGES_UPLOAD_PATH') || define('IMAGES_UPLOAD_PATH', $imageConfig->upload->path);
0225         defined('IMAGES_MEDIA_SERVER') || define('IMAGES_MEDIA_SERVER', $imageConfig->media->server);
0226 
0227         // ppload
0228         $configFileserver = $appConfig->settings->server->files;
0229         defined('PPLOAD_API_URI') || define('PPLOAD_API_URI', $configFileserver->api->uri);
0230         defined('PPLOAD_CLIENT_ID') || define('PPLOAD_CLIENT_ID', $configFileserver->api->client_id);
0231         defined('PPLOAD_SECRET') || define('PPLOAD_SECRET', $configFileserver->api->client_secret);
0232         defined('PPLOAD_HOST') || define('PPLOAD_HOST', $configFileserver->host);
0233         defined('PPLOAD_DOWNLOAD_SECRET') || define('PPLOAD_DOWNLOAD_SECRET', $configFileserver->download_secret);
0234     }
0235 
0236     protected function _initGlobalApplicationVars()
0237     {
0238         $modelDomainConfig = new Application_Model_DbTable_ConfigStore();
0239         Zend_Registry::set('application_store_category_list', $modelDomainConfig->fetchAllStoresAndCategories());
0240         Zend_Registry::set('application_store_config_list', $modelDomainConfig->fetchAllStoresConfigArray());
0241         Zend_Registry::set('application_store_config_id_list', $modelDomainConfig->fetchAllStoresConfigByIdArray());
0242     }
0243 
0244     protected function _initStoreDependentVars()
0245     {
0246         /** @var $front Zend_Controller_Front */
0247         $front = $this->bootstrap('frontController')->getResource('frontController');
0248         $front->registerPlugin(new Application_Plugin_InitGlobalStoreVars());
0249     }
0250 
0251 }
0252