File indexing completed on 2024-04-14 05:47:03

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 Application_Plugin_InitGlobalStoreVars extends Zend_Controller_Plugin_Abstract
0026 {
0027     private static $exceptionThrown = false;
0028 
0029     /**
0030      * @param Zend_Controller_Request_Abstract $request
0031      *
0032      * @throws Zend_Exception
0033      */
0034     public function preDispatch(Zend_Controller_Request_Abstract $request)
0035     {
0036         /** @var Zend_Controller_Request_Http $request */
0037         parent::preDispatch($request);
0038 
0039         $storeHost = $this->getStoreHost($request);
0040         Zend_Registry::set('store_host', $storeHost);
0041 
0042         $storeConfigName = $this->getStoreConfigName($storeHost);
0043         Zend_Registry::set('store_config_name', $storeConfigName);
0044 
0045         $config_store = $this->getConfigStore($storeHost);
0046         Zend_Registry::set('store_config', $config_store);
0047         Zend_Registry::set('config_store_tags', $this->getConfigStoreTags($config_store->store_id));
0048         Zend_Registry::set('store_category_list', $this->getStoreCategories($storeHost));
0049     }
0050 
0051     /**
0052      * @param Zend_Controller_Request_Http $request
0053      *
0054      * @return mixed
0055      * @throws Zend_Exception
0056      */
0057     private function getStoreHost($request)
0058     {
0059         $storeHost = '';
0060         $storeConfigArray = Zend_Registry::get('application_store_config_list');
0061 
0062         // search for store id param
0063         $requestStoreConfigName = null;
0064         if ($request->getParam('domain_store_id')) {
0065             $requestStoreConfigName =
0066                 $request->getParam('domain_store_id') ? preg_replace('/[^-a-zA-Z0-9_\.]/', '', $request->getParam('domain_store_id'))
0067                     : null;
0068 
0069             $result = $this->searchForConfig($storeConfigArray, 'name', $requestStoreConfigName);
0070 
0071             if (isset($result['host'])) {
0072                 $storeHost = $result['host'];
0073 
0074                 return $storeHost;
0075             }
0076         }
0077 
0078         // search for host
0079         $httpHost = strtolower($request->getHttpHost());
0080         if (isset($storeConfigArray[$httpHost])) {
0081             return $storeConfigArray[$httpHost]['host'];
0082         }
0083 
0084         // search for default
0085         $result = $this->searchForConfig($storeConfigArray, 'default', 1);
0086         $storeHost = $result['host'];
0087 
0088         return $storeHost;
0089     }
0090 
0091     /**
0092      * alternative version which replace arraySearchConfig for PHP < 5.5.0
0093      *
0094      * @param $haystack
0095      * @param $key
0096      * @param $value
0097      *
0098      * @return array
0099      */
0100     private function searchForConfig($haystack, $key, $value)
0101     {
0102         if (false === is_array($haystack)) {
0103             return array();
0104         }
0105         foreach ($haystack as $element) {
0106             if (isset($element[$key]) and (strtolower($element[$key]) == strtolower($value))) {
0107                 return $element;
0108             }
0109         }
0110 
0111         return array();
0112     }
0113 
0114     /**
0115      * @param string $storeHostName
0116      *
0117      * @return string
0118      * @throws Zend_Exception
0119      */
0120     private function getStoreConfigName($storeHostName)
0121     {
0122         $storeIdName = Zend_Registry::get('config')->settings->client->default->name; //set to default
0123 
0124         $store_config_list = Zend_Registry::get('application_store_config_list');
0125 
0126         // search for host
0127         $httpHost = strtolower($storeHostName);
0128         if (isset($store_config_list[$httpHost])) {
0129             return $store_config_list[$httpHost]['config_id_name'];
0130         } else {
0131             Zend_Registry::get('logger')->warn(__METHOD__ . '(' . __LINE__ . ') - $httpHost = ' . $httpHost
0132                 . ' :: no config id name configured')
0133             ;
0134         }
0135 
0136         // search for default
0137         $result = $this->searchForConfig($store_config_list, 'default', 1);
0138 
0139         if (isset($result['config_id_name'])) {
0140             $storeIdName = $result['config_id_name'];
0141         } else {
0142             Zend_Registry::get('logger')->warn(__METHOD__ . '(' . __LINE__ . ') - no default store config name configured');
0143         }
0144 
0145         return $storeIdName;
0146     }
0147  
0148 
0149     /**
0150      * @param $message
0151      */
0152     private function raiseException($message)
0153     {
0154         if (self::$exceptionThrown) {
0155             return;
0156         }
0157 
0158         $request = $this->getRequest();
0159         // Repoint the request to the default error handler
0160         $request->setModuleName('default');
0161         $request->setControllerName('error');
0162         $request->setActionName('error');
0163         //$request->setDispatched(true);
0164 
0165         // Set up the error handler
0166         $error = new Zend_Controller_Plugin_ErrorHandler();
0167         $error->type = Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
0168         $error->request = clone($request);
0169         $error->exception = new Zend_Exception($message);
0170         $request->setParam('error_handler', $error);
0171         //$this->setRequest($request);
0172 
0173         self::$exceptionThrown = true;
0174     }
0175 
0176     /**
0177      * @param string $storeHostName
0178      *
0179      * @return Application_Model_ConfigStore
0180      */
0181     private function getConfigStore($storeHostName)
0182     {
0183         $storeConfig = new Application_Model_ConfigStore($storeHostName);
0184 
0185         return $storeConfig;
0186     }
0187 
0188     private function getConfigStoreTags($store_id)
0189     {
0190         $modelConfigStoreTags = new Application_Model_ConfigStoreTags();
0191 
0192         $result = $modelConfigStoreTags->getTagsAsIdForStore($store_id);
0193 
0194         return $result;
0195     }
0196 
0197     /**
0198      * @param string $storeHostName
0199      *
0200      * @return array
0201      * @throws Zend_Exception
0202      */
0203     private function getStoreCategories($storeHostName)
0204     {
0205         $storeCategoryArray = Zend_Registry::get('application_store_category_list');
0206 
0207         //check store_category_list to see if some categories are defined here
0208         if (isset($storeCategoryArray[$storeHostName])) {
0209             $storeCategories = $storeCategoryArray[$storeHostName];
0210             if (is_string($storeCategories)) {
0211                 return array($storeCategories);
0212             }
0213             return $storeCategories;
0214         }
0215 
0216         Zend_Registry::get('logger')->warn(__METHOD__ . '(' . __LINE__ . ') - ' . $storeHostName
0217             . ' :: no categories for domain context configured. Try to use categories from default store instead')
0218         ;
0219 
0220         // next step: check store_category_list to see if some categories are defined for the default store
0221         $storeConfigArray = Zend_Registry::get('application_store_config_list');
0222         $defaultStore = $this->arraySearchConfig($storeConfigArray, 'default', '1');
0223         if (isset($storeCategoryArray[$defaultStore['host']])) {
0224             $storeCategories = $storeCategoryArray[$defaultStore['host']];
0225             if (is_string($storeCategories)) {
0226                 return array($storeCategories);
0227             }
0228             return $storeCategories;
0229         }
0230 
0231         Zend_Registry::get('logger')->warn(__METHOD__ . '(' . __LINE__ . ') - ' . $storeHostName
0232             . ' :: no categories for default store found. Try to use main categories instead')
0233         ;
0234 
0235         // last chance: take the main categories from the tree
0236         $modelCategories = new Application_Model_DbTable_ProjectCategory();
0237         $root = $modelCategories->fetchRoot();
0238         $storeCategories = $modelCategories->fetchImmediateChildrenIds($root['project_category_id'], $modelCategories::ORDERED_TITLE);
0239 
0240         return $storeCategories;
0241     }
0242 
0243     /**
0244      * needs PHP >= 5.5.0
0245      *
0246      * @param $haystack
0247      * @param $key
0248      * @param $needle
0249      *
0250      * @return array
0251      */
0252     private function arraySearchConfig($haystack, $column, $needle)
0253     {
0254         if (PHP_VERSION_ID <= 50500) {
0255             return $this->searchForConfig($haystack, $column, $needle);
0256         }
0257         if (false === is_array($haystack)) {
0258             return array();
0259         }
0260         $key = array_search($needle, array_column($haystack, $column, 'host'));
0261         if ($key) {
0262             return $haystack[$key];
0263         }
0264 
0265         return array();
0266     }
0267 
0268 }