protected function getMultiShopValues($key) { if (version_compare(_PS_VERSION_, '1.6.0.3', '>=') === true) { return Configuration::getMultiShopValues($key); } else { $shops = Shop::getShops(false, null, true); $id_lang = (int) $this->context->language->id; $results = array(); array_push($results, Configuration::get($key)); foreach ($shops as $id_shop) { array_push($results, Configuration::get($key, $id_lang, null, $id_shop)); } return $results; } }
public static function generateHtaccess($path = null, $rewrite_settings = null, $cache_control = null, $specific = '', $disable_multiviews = null, $medias = false, $disable_modsec = null) { if (defined('PS_INSTALLATION_IN_PROGRESS') && $rewrite_settings === null) { return true; } // Default values for parameters if (is_null($path)) { $path = _PS_ROOT_DIR_ . '/.htaccess'; } if (is_null($cache_control)) { $cache_control = (int) Configuration::get('PS_HTACCESS_CACHE_CONTROL'); } if (is_null($disable_multiviews)) { $disable_multiviews = (int) Configuration::get('PS_HTACCESS_DISABLE_MULTIVIEWS'); } if ($disable_modsec === null) { $disable_modsec = (int) Configuration::get('PS_HTACCESS_DISABLE_MODSEC'); } // Check current content of .htaccess and save all code outside of prestashop comments $specific_before = $specific_after = ''; if (file_exists($path)) { $content = file_get_contents($path); if (preg_match('#^(.*)\\# ~~start~~.*\\# ~~end~~[^\\n]*(.*)$#s', $content, $m)) { $specific_before = $m[1]; $specific_after = $m[2]; } else { // For retrocompatibility if (preg_match('#\\# http://www\\.prestashop\\.com - http://www\\.prestashop\\.com/forums\\s*(.*)<IfModule mod_rewrite\\.c>#si', $content, $m)) { $specific_before = $m[1]; } else { $specific_before = $content; } } } // Write .htaccess data if (!($write_fd = @fopen($path, 'w'))) { return false; } if ($specific_before) { fwrite($write_fd, trim($specific_before) . "\n\n"); } $domains = array(); foreach (ShopUrl::getShopUrls() as $shop_url) { /** @var ShopUrl $shop_url */ if (!isset($domains[$shop_url->domain])) { $domains[$shop_url->domain] = array(); } $domains[$shop_url->domain][] = array('physical' => $shop_url->physical_uri, 'virtual' => $shop_url->virtual_uri, 'id_shop' => $shop_url->id_shop); if ($shop_url->domain == $shop_url->domain_ssl) { continue; } if (!isset($domains[$shop_url->domain_ssl])) { $domains[$shop_url->domain_ssl] = array(); } $domains[$shop_url->domain_ssl][] = array('physical' => $shop_url->physical_uri, 'virtual' => $shop_url->virtual_uri, 'id_shop' => $shop_url->id_shop); } // Write data in .htaccess file fwrite($write_fd, "# ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again\n"); fwrite($write_fd, "# .htaccess automaticaly generated by PrestaShop e-commerce open-source solution\n"); fwrite($write_fd, "# http://www.prestashop.com - http://www.prestashop.com/forums\n\n"); if ($disable_modsec) { fwrite($write_fd, "<IfModule mod_security.c>\nSecFilterEngine Off\nSecFilterScanPOST Off\n</IfModule>\n\n"); } // RewriteEngine fwrite($write_fd, "<IfModule mod_rewrite.c>\n"); // Ensure HTTP_MOD_REWRITE variable is set in environment fwrite($write_fd, "<IfModule mod_env.c>\n"); fwrite($write_fd, "SetEnv HTTP_MOD_REWRITE On\n"); fwrite($write_fd, "</IfModule>\n\n"); // Disable multiviews ? if ($disable_multiviews) { fwrite($write_fd, "\n# Disable Multiviews\nOptions -Multiviews\n\n"); } fwrite($write_fd, "RewriteEngine on\n"); if (!$medias && Configuration::getMultiShopValues('PS_MEDIA_SERVER_1') && Configuration::getMultiShopValues('PS_MEDIA_SERVER_2') && Configuration::getMultiShopValues('PS_MEDIA_SERVER_3')) { $medias = array(Configuration::getMultiShopValues('PS_MEDIA_SERVER_1'), Configuration::getMultiShopValues('PS_MEDIA_SERVER_2'), Configuration::getMultiShopValues('PS_MEDIA_SERVER_3')); } $media_domains = ''; foreach ($medias as $media) { foreach ($media as $media_url) { if ($media_url) { $media_domains .= 'RewriteCond %{HTTP_HOST} ^' . $media_url . '$ [OR]' . "\n"; } } } if (Configuration::get('PS_WEBSERVICE_CGI_HOST')) { fwrite($write_fd, "RewriteCond %{HTTP:Authorization} ^(.*)\nRewriteRule . - [E=HTTP_AUTHORIZATION:%1]\n\n"); } foreach ($domains as $domain => $list_uri) { $physicals = array(); foreach ($list_uri as $uri) { fwrite($write_fd, PHP_EOL . PHP_EOL . '#Domain: ' . $domain . PHP_EOL); if (Shop::isFeatureActive()) { fwrite($write_fd, 'RewriteCond %{HTTP_HOST} ^' . $domain . '$' . "\n"); } fwrite($write_fd, 'RewriteRule . - [E=REWRITEBASE:' . $uri['physical'] . ']' . "\n"); // Webservice fwrite($write_fd, 'RewriteRule ^api$ api/ [L]' . "\n\n"); fwrite($write_fd, 'RewriteRule ^api/(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]' . "\n\n"); if (!$rewrite_settings) { $rewrite_settings = (int) Configuration::get('PS_REWRITING_SETTINGS', null, null, (int) $uri['id_shop']); } $domain_rewrite_cond = 'RewriteCond %{HTTP_HOST} ^' . $domain . '$' . "\n"; // Rewrite virtual multishop uri if ($uri['virtual']) { if (!$rewrite_settings) { fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^' . trim($uri['virtual'], '/') . '/?$ ' . $uri['physical'] . $uri['virtual'] . "index.php [L,R]\n"); } else { fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^' . trim($uri['virtual'], '/') . '$ ' . $uri['physical'] . $uri['virtual'] . " [L,R]\n"); } fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^' . ltrim($uri['virtual'], '/') . '(.*) ' . $uri['physical'] . "\$1 [L]\n\n"); } if ($rewrite_settings) { // Compatibility with the old image filesystem fwrite($write_fd, "# Images\n"); if (Configuration::get('PS_LEGACY_IMAGES')) { fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^([a-z0-9]+)\\-([a-z0-9]+)(\\-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\\.jpg$ %{ENV:REWRITEBASE}img/p/$1-$2$3$4.jpg [L]' . "\n"); fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^([0-9]+)\\-([0-9]+)(-[0-9]+)?/.+\\.jpg$ %{ENV:REWRITEBASE}img/p/$1-$2$3.jpg [L]' . "\n"); } // Rewrite product images < 100 millions for ($i = 1; $i <= 8; $i++) { $img_path = $img_name = ''; for ($j = 1; $j <= $i; $j++) { $img_path .= '$' . $j . '/'; $img_name .= '$' . $j; } $img_name .= '$' . $j; fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^' . str_repeat('([0-9])', $i) . '(\\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\\.jpg$ %{ENV:REWRITEBASE}img/p/' . $img_path . $img_name . '$' . ($j + 1) . ".jpg [L]\n"); } fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^c/([0-9]+)(\\-[\\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]' . "\n"); fwrite($write_fd, $media_domains); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]' . "\n"); } fwrite($write_fd, "# AlphaImageLoader for IE and fancybox\n"); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, 'RewriteRule ^images_ie/?([^/]+)\\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]' . "\n"); } // Redirections to dispatcher if ($rewrite_settings) { fwrite($write_fd, "\n# Dispatcher\n"); fwrite($write_fd, "RewriteCond %{REQUEST_FILENAME} -s [OR]\n"); fwrite($write_fd, "RewriteCond %{REQUEST_FILENAME} -l [OR]\n"); fwrite($write_fd, "RewriteCond %{REQUEST_FILENAME} -d\n"); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, "RewriteRule ^.*\$ - [NC,L]\n"); if (Shop::isFeatureActive()) { fwrite($write_fd, $domain_rewrite_cond); } fwrite($write_fd, "RewriteRule ^.*\$ %{ENV:REWRITEBASE}index.php [NC,L]\n"); } } fwrite($write_fd, "</IfModule>\n\n"); fwrite($write_fd, "AddType application/vnd.ms-fontobject .eot\n"); fwrite($write_fd, "AddType font/ttf .ttf\n"); fwrite($write_fd, "AddType font/otf .otf\n"); fwrite($write_fd, "AddType application/x-font-woff .woff\n"); fwrite($write_fd, "<IfModule mod_headers.c>\n\t<FilesMatch \"\\.(ttf|ttc|otf|eot|woff|svg)\$\">\n\t\tHeader add Access-Control-Allow-Origin \"*\"\n\t</FilesMatch>\n</IfModule>\n\n"); // Cache control if ($cache_control) { $cache_control = "<IfModule mod_expires.c>\n\tExpiresActive On\n\tExpiresByType image/gif \"access plus 1 month\"\n\tExpiresByType image/jpeg \"access plus 1 month\"\n\tExpiresByType image/png \"access plus 1 month\"\n\tExpiresByType text/css \"access plus 1 week\"\n\tExpiresByType text/javascript \"access plus 1 week\"\n\tExpiresByType application/javascript \"access plus 1 week\"\n\tExpiresByType application/x-javascript \"access plus 1 week\"\n\tExpiresByType image/x-icon \"access plus 1 year\"\n\tExpiresByType image/svg+xml \"access plus 1 year\"\n\tExpiresByType image/vnd.microsoft.icon \"access plus 1 year\"\n\tExpiresByType application/font-woff \"access plus 1 year\"\n\tExpiresByType application/x-font-woff \"access plus 1 year\"\n\tExpiresByType application/vnd.ms-fontobject \"access plus 1 year\"\n\tExpiresByType font/opentype \"access plus 1 year\"\n\tExpiresByType font/ttf \"access plus 1 year\"\n\tExpiresByType font/otf \"access plus 1 year\"\n\tExpiresByType application/x-font-ttf \"access plus 1 year\"\n\tExpiresByType application/x-font-otf \"access plus 1 year\"\n</IfModule>\n\n<IfModule mod_headers.c>\n\tHeader unset Etag\n</IfModule>\nFileETag none\n<IfModule mod_deflate.c>\n\t<IfModule mod_filter.c>\n\t\tAddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript application/x-javascript font/ttf application/x-font-ttf font/otf application/x-font-otf font/opentype\n\t</IfModule>\n</IfModule>\n\n"; fwrite($write_fd, $cache_control); } // In case the user hasn't rewrite mod enabled fwrite($write_fd, "#If rewrite mod isn't enabled\n"); // Do not remove ($domains is already iterated upper) reset($domains); $domain = current($domains); fwrite($write_fd, 'ErrorDocument 404 ' . $domain[0]['physical'] . "index.php?controller=404\n\n"); fwrite($write_fd, "# ~~end~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again"); if ($specific_after) { fwrite($write_fd, "\n\n" . trim($specific_after)); } fclose($write_fd); if (!defined('PS_INSTALLATION_IN_PROGRESS')) { Hook::exec('actionHtaccessCreate'); } return true; }
/** * 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() { // Find current shop from URL if (!($id_shop = Tools::getValue('id_shop')) || defined('_PS_ADMIN_DIR_')) { $found_uri = ''; $is_main_uri = false; $host = Tools::getHttpHost(); $request_uri = rawurldecode($_SERVER['REQUEST_URI']); $sql = 'SELECT s.id_shop, CONCAT(su.physical_uri, su.virtual_uri) AS uri, su.domain, su.main FROM ' . _DB_PREFIX_ . 'shop_url su LEFT JOIN ' . _DB_PREFIX_ . 'shop s ON (s.id_shop = su.id_shop) WHERE (su.domain = \'' . pSQL($host) . '\' OR su.domain_ssl = \'' . pSQL($host) . '\') AND s.active = 1 AND s.deleted = 0 ORDER BY LENGTH(CONCAT(su.physical_uri, su.virtual_uri)) DESC'; $result = Db::getInstance()->executeS($sql); $through = false; foreach ($result as $row) { // An URL matching current shop was found if (preg_match('#^' . preg_quote($row['uri'], '#') . '#i', $request_uri)) { $through = true; $id_shop = $row['id_shop']; $found_uri = $row['uri']; if ($row['main']) { $is_main_uri = true; } break; } } // If an URL was found but is not the main URL, redirect to main URL if ($through && $id_shop && !$is_main_uri) { foreach ($result as $row) { if ($row['id_shop'] == $id_shop && $row['main']) { $request_uri = substr($request_uri, strlen($found_uri)); $url = str_replace('//', '/', $row['domain'] . $row['uri'] . $request_uri); $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT'); $redirect_code = $redirect_type == 1 ? '302' : '301'; $redirect_header = $redirect_type == 1 ? 'Found' : 'Moved Permanently'; header('HTTP/1.0 ' . $redirect_code . ' ' . $redirect_header); header('Cache-Control: no-cache'); header('Location: http://' . $url); exit; } } } } $http_host = Tools::getHttpHost(); $all_media = array_merge(Configuration::getMultiShopValues('PS_MEDIA_SERVER_1'), Configuration::getMultiShopValues('PS_MEDIA_SERVER_2'), Configuration::getMultiShopValues('PS_MEDIA_SERVER_3')); if (!$id_shop && defined('_PS_ADMIN_DIR_') || Tools::isPHPCLI() || in_array($http_host, $all_media)) { // If in admin, we can access to the shop without right URL if (!$id_shop && Tools::isPHPCLI() || defined('_PS_ADMIN_DIR_')) { $id_shop = (int) Configuration::get('PS_SHOP_DEFAULT'); } $shop = new Shop((int) $id_shop); if (!Validate::isLoadedObject($shop)) { $shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); } $shop->virtual_uri = ''; // Define some $_SERVER variables like HTTP_HOST if PHP is launched with php-cli if (Tools::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_ADDR'])) { $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; } } } else { $shop = new Shop($id_shop); if (!Validate::isLoadedObject($shop) || !$shop->active) { // No shop found ... too bad, let's redirect to default shop $default_shop = new Shop(Configuration::get('PS_SHOP_DEFAULT')); // Hmm there is something really bad in your Prestashop ! if (!Validate::isLoadedObject($default_shop)) { throw new PrestaShopException('Shop not found'); } $params = $_GET; unset($params['id_shop']); $url = $default_shop->domain; if (!Configuration::get('PS_REWRITING_SETTINGS')) { $url .= $default_shop->getBaseURI() . 'index.php?' . http_build_query($params); } else { // Catch url with subdomain "www" if (strpos($url, 'www.') === 0 && 'www.' . $_SERVER['HTTP_HOST'] === $url || $_SERVER['HTTP_HOST'] === 'www.' . $url) { $url .= $_SERVER['REQUEST_URI']; } else { $url .= $default_shop->getBaseURI(); } if (count($params)) { $url .= '?' . http_build_query($params); } } $redirect_type = Configuration::get('PS_CANONICAL_REDIRECT'); $redirect_code = $redirect_type == 1 ? '302' : '301'; $redirect_header = $redirect_type == 1 ? 'Found' : 'Moved Permanently'; header('HTTP/1.0 ' . $redirect_code . ' ' . $redirect_header); header('Location: http://' . $url); exit; } elseif (defined('_PS_ADMIN_DIR_') && empty($shop->physical_uri)) { $shop_default = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); $shop->physical_uri = $shop_default->physical_uri; $shop->virtual_uri = $shop_default->virtual_uri; } } self::$context_id_shop = $shop->id; self::$context_id_shop_group = $shop->id_shop_group; self::$context = self::CONTEXT_SHOP; return $shop; }