File indexing completed on 2024-05-12 05:58:48

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