コード例 #1
0
ファイル: shop.php プロジェクト: jeprodev/jeproshop
 /**
  * Find the shop from current domain / uri and get an instance of this shop
  * if INSTALL_VERSION is defined, will return an empty shop object
  *
  * @return Shop
  */
 public static function initialize()
 {
     $app = JFactory::getApplication();
     $db = JFactory::getDBO();
     $shop_id = (int) $app->input->get("shop_id");
     //find current shop from url
     if (!$shop_id) {
         $found_uri = '';
         $host = '';
         $request_uri = rawurldecode($_SERVER['REQUEST_URI']);
         $query = "SELECT shop." . $db->quoteName('shop_id') . ", CONCAT(shop_url.";
         $query .= $db->quoteName('physical_uri') . ", shop_url." . $db->quoteName('virtual_uri');
         $query .= ") AS uri, shop_url." . $db->quoteName('domain') . ", shop_url.";
         $query .= $db->quoteName('main') . " FROM " . $db->quoteName('#__jeproshop_shop_url');
         $query .= " AS shop_url LEFT JOIN " . $db->quoteName('#__jeproshop_shop') . " AS shop ON ";
         $query .= "(shop.shop_id = shop_url.shop_id) WHERE (shop_url.domain = " . $db->quote($db->escape($host));
         $query .= " OR shop_url.ssl_domain = " . $db->quote($db->escape($host)) . ") AND shop.published = 1 AND ";
         $query .= "shop.deleted = 0 ORDER BY LENGTH (CONCAT(shop_url.physical_uri, shop_url.virtual_uri)) DESC";
         $db->setQuery($query);
         $results = $db->loadObjectList();
         $throuth = false;
         foreach ($results as $result) {
             if (preg_match('#^' . preg_quote($result->uri, '#') . '#i', $request_uri)) {
                 $throuth = true;
                 $shop_id = $result->shop_id;
                 $found_uri = $result->uri;
                 if ($result->main) {
                     $is_main_uri = true;
                 }
                 break;
             }
         }
         /** If an URL was found and it's not the main URL, redirect to main url  **/
         if ($throuth && $shop_id && !$is_main_uri) {
             foreach ($results as $result) {
                 if ($result->shop_id == $shop_id && $result->main) {
                     $request_uri = substr($request_uri, strlen($found_uri));
                     $url = str_replace('//', '/', $result->domain . $result->uri . $request_uri);
                     $redirect_type = JeproshopSettingModelSetting::getValue('canonical_redirect') == 2 ? '301' : '302';
                     exit;
                 }
             }
         }
     }
     if (!$shop_id || JeproshopTools::isPHPCLI() || in_array(JeproshopTools::getHttpHost(), array(COM_JEPROSHOP_MEDIA_SERVER_1, COM_JEPROSHOP_MEDIA_SERVER_2, COM_JEPROSHOP_MEDIA_SERVER_3))) {
         if (!$shop_id && JeproshopTools::isPHPCLI()) {
             $shop_id = (int) JeproshopSettingModelSetting::getValue('default_shop');
         }
         $shop = new JeproshopShopModelShop($shop_id);
         if (!JeproshopTools::isLoadedObject($shop, 'shop_id')) {
             $shop = new JeproshopShopModelShop((int) JeproshopSettingModelSetting::getValue('default_shop'));
         }
         $shop->physical_uri = preg_replace('#/+#', '/', str_replace('\\', '/', dirname(dirname($_SERVER['SCRIPT_NAME']))) . '/');
         $shop->virtual_uri = '';
         //Define some $_SERVER variables like HTTP_HOST
         if (JeproshopTools::isPHPCLI()) {
             if (!isset($_SERVER['HTTP_HOST']) || empty($_SERVER['HTTP_HOST'])) {
                 $_SERVER['HTTP_HOST'] = $shop->domain;
             }
             if (!isset($_SERVER['SERVER_NAME']) || empty($_SERVER['SERVER_NAME'])) {
                 $_SERVER['SERVER_NAME'] = $shop->domain;
             }
             if (!isset($_SERVER['REMOTE_ADDR']) || empty($_SERVER['REMOTE_ADD'])) {
                 $_SERVER['REMOTE_ADD'] = '127.0.0.1';
             }
         }
     } else {
         $shop = new JeproshopShopModelShop($shop_id);
         if (!JeproshopTools::isLoadedObject($shop, 'shop_id') || !$shop->published) {
             // No shop found too bad let's redirect to default shop
             $default_shop = new JeproshopShopModelShop((int) JeproshopSettingModelSetting::getValue('default_shop'));
             if (!JeproshopTools::isLoadedObject($default_shop, 'shop_id')) {
                 JError::raiseError(500, JText::_('COM_JEPROSHOP_NO_SHOP_FOUND_MESSAGE'));
             }
             $inputs = $app->input;
             $inputs->set('shop_id', NULL);
             $url = $default_shop->domain;
             if (!JeproshopSettingModelSetting::getValue('rewrite_settings')) {
                 $url .= $default_shop->getBaseUrl() . 'index.php?option=com_jeproshop' . JeproshopTools::buildHttpQuery($inputs);
             } else {
                 /** catch sub domain url "www" **/
                 if (strpos($url, 'www.') === 0 && 'www.' . $_SERVER['HTTP_HOST'] === $url || $_SERVER['HTTP_HOST'] === 'www.' . $url) {
                     $url .= $_SERVER['REQUEST_URI'];
                 } else {
                     $url .= $default_shop->getBaseUrl();
                 }
                 if (count($inputs)) {
                     $url .= '?option=com_jeproshop' . JeproshopTools::httpBuildQuery($inputs);
                 }
             }
             $redirect_type = JeproshopSettingModelSetting::getValue('canonical_redirect') == 2 ? '301' : '302';
             exit;
         } elseif (empty($shop->physical_uri)) {
             $default_shop = new JeproshopShopModelShop((int) JeproshopSettingModelSetting::getValue('default_shop'));
             $shop->physical_uri = $default_shop->physical_uri;
             $shop->virtual_uri = $default_shop->virtual_uri;
         }
     }
     self::$context_shop_id = $shop->shop_id;
     self::$context_shop_group_id = $shop->shop_group_id;
     self::$shop_context = self::CONTEXT_SHOP;
     return $shop;
 }