diff --git a/admin-dev/tabs/AdminEmails.php b/admin-dev/tabs/AdminEmails.php index 62ee65620..0f2480dd0 100644 --- a/admin-dev/tabs/AdminEmails.php +++ b/admin-dev/tabs/AdminEmails.php @@ -34,23 +34,24 @@ class AdminEmails extends AdminPreferences $this->className = 'Configuration'; $this->table = 'configuration'; + parent::__construct(); + foreach (Contact::getContacts($this->context->language->id) AS $contact) $arr[] = array('email_message' => $contact['id_contact'], 'name' => $contact['name']); $this->_fieldsEmail = array( - 'PS_MAIL_EMAIL_MESSAGE' => array('title' => $this->l('Send e-mail to:'), 'desc' => $this->l('When customers send message from order page'), 'validation' => 'isUnsignedId', 'type' => 'select', 'cast' => 'intval', 'identifier' => 'email_message', 'list' => $arr), - 'PS_MAIL_METHOD' => array('title' => '', 'validation' => 'isGenericName', 'required' => true, 'type' => 'radio', 'choices' => array(1 => $this->l('Use PHP mail() function. Recommended; works in most cases'), 2 => $this->l('Set my own SMTP parameters. For advanced users ONLY')), 'js' => array(1 => 'onclick="$(\'#SMTP_CONTAINER\').slideUp();"', 2 => 'onclick="$(\'#SMTP_CONTAINER\').slideDown();"'), 'visibility' => Shop::CONTEXT_ALL), - 'PS_MAIL_TYPE' => array('title' => '', 'validation' => 'isGenericName', 'required' => true, 'type' => 'radio', 'choices' => array(1 => $this->l('Send e-mail as HTML'), 2 => $this->l('Send e-mail as Text'), 3 => $this->l('Both'))), - 'SMTP_CONTAINER' => array('title' => '', 'type' => 'container'), - 'PS_MAIL_DOMAIN' => array('title' => $this->l('Mail domain:'), 'desc' => $this->l('Fully qualified domain name (keep it empty if you do not know)'), 'validation' => 'isUrl', 'size' => 30, 'type' => 'text', 'visibility' => Shop::CONTEXT_ALL), - 'PS_MAIL_SERVER' => array('title' => $this->l('SMTP server:'), 'desc' => $this->l('IP or server name (e.g., smtp.mydomain.com)'), 'validation' => 'isGenericName', 'size' => 30, 'type' => 'text', 'visibility' => Shop::CONTEXT_ALL), - 'PS_MAIL_USER' => array('title' => $this->l('SMTP user:'), 'desc' => $this->l('Leave blank if not applicable'), 'validation' => 'isGenericName', 'size' => 30, 'type' => 'text', 'visibility' => Shop::CONTEXT_ALL), - 'PS_MAIL_PASSWD' => array('title' => $this->l('SMTP password:'), 'desc' => $this->l('Leave blank if not applicable'), 'validation' => 'isAnything', 'size' => 30, 'type' => 'password', 'visibility' => Shop::CONTEXT_ALL), - 'PS_MAIL_SMTP_ENCRYPTION' => array('title' => $this->l('Encryption:'), 'desc' => $this->l('Use an encrypt protocol'), 'type' => 'select', 'cast' => 'strval', 'identifier' => 'mode', 'list' => array(array('mode' => 'off', 'name' => $this->l('None')), array('mode' => 'tls', 'name' => $this->l('TLS')), array('mode' => 'ssl', 'name' => $this->l('SSL'))), 'visibility' => Shop::CONTEXT_ALL), - 'PS_MAIL_SMTP_PORT' => array('title' => $this->l('Port:'), 'desc' => $this->l('Number of port to use'), 'validation' => 'isInt', 'size' => 5, 'type' => 'text', 'cast' => 'intval', 'visibility' => Shop::CONTEXT_ALL), - 'SMTP_CONTAINER_END' => array('title' => '', 'type' => 'container_end', 'content' => '')); - - parent::__construct(); + 'PS_MAIL_EMAIL_MESSAGE' => array('title' => $this->l('Send e-mail to:'), 'desc' => $this->l('When customers send message from order page'), 'validation' => 'isUnsignedId', 'type' => 'select', 'cast' => 'intval', 'identifier' => 'email_message', 'list' => $arr), + 'PS_MAIL_METHOD' => array('title' => '', 'validation' => 'isGenericName', 'required' => true, 'type' => 'radio', 'choices' => array(1 => $this->l('Use PHP mail() function. Recommended; works in most cases'), 2 => $this->l('Set my own SMTP parameters. For advanced users ONLY')), 'js' => array(1 => 'onclick="$(\'#SMTP_CONTAINER\').slideUp();"', 2 => 'onclick="$(\'#SMTP_CONTAINER\').slideDown();"'), 'visibility' => Shop::CONTEXT_ALL), + 'PS_MAIL_TYPE' => array('title' => '', 'validation' => 'isGenericName', 'required' => true, 'type' => 'radio', 'choices' => array(1 => $this->l('Send e-mail as HTML'), 2 => $this->l('Send e-mail as Text'), 3 => $this->l('Both'))), + 'SMTP_CONTAINER' => array('title' => '', 'type' => 'container'), + 'PS_MAIL_DOMAIN' => array('title' => $this->l('Mail domain:'), 'desc' => $this->l('Fully qualified domain name (keep it empty if you do not know)'), 'validation' => 'isUrl', 'size' => 30, 'type' => 'text', 'visibility' => Shop::CONTEXT_ALL), + 'PS_MAIL_SERVER' => array('title' => $this->l('SMTP server:'), 'desc' => $this->l('IP or server name (e.g., smtp.mydomain.com)'), 'validation' => 'isGenericName', 'size' => 30, 'type' => 'text', 'visibility' => Shop::CONTEXT_ALL), + 'PS_MAIL_USER' => array('title' => $this->l('SMTP user:'), 'desc' => $this->l('Leave blank if not applicable'), 'validation' => 'isGenericName', 'size' => 30, 'type' => 'text', 'visibility' => Shop::CONTEXT_ALL), + 'PS_MAIL_PASSWD' => array('title' => $this->l('SMTP password:'), 'desc' => $this->l('Leave blank if not applicable'), 'validation' => 'isAnything', 'size' => 30, 'type' => 'password', 'visibility' => Shop::CONTEXT_ALL), + 'PS_MAIL_SMTP_ENCRYPTION' => array('title' => $this->l('Encryption:'), 'desc' => $this->l('Use an encrypt protocol'), 'type' => 'select', 'cast' => 'strval', 'identifier' => 'mode', 'list' => array(array('mode' => 'off', 'name' => $this->l('None')), array('mode' => 'tls', 'name' => $this->l('TLS')), array('mode' => 'ssl', 'name' => $this->l('SSL'))), 'visibility' => Shop::CONTEXT_ALL), + 'PS_MAIL_SMTP_PORT' => array('title' => $this->l('Port:'), 'desc' => $this->l('Number of port to use'), 'validation' => 'isInt', 'size' => 5, 'type' => 'text', 'cast' => 'intval', 'visibility' => Shop::CONTEXT_ALL), + 'SMTP_CONTAINER_END' => array('title' => '', 'type' => 'container_end', 'content' => ''), + ); } public function postProcess() diff --git a/admin-dev/tabs/AdminMeta.php b/admin-dev/tabs/AdminMeta.php index 53fe292a4..6b1f3fb0a 100644 --- a/admin-dev/tabs/AdminMeta.php +++ b/admin-dev/tabs/AdminMeta.php @@ -50,8 +50,20 @@ class AdminMeta extends AdminTab 'PS_REWRITING_SETTINGS' => array('title' => $this->l('Friendly URL'), 'desc' => $this->l('Enable only if your server allows URL rewriting (recommended)').'

'.$this->l('If you turn on this feature, you must').' '.$this->l('generate a .htaccess file').'

', 'validation' => 'isBool', 'cast' => 'intval', 'type' => 'bool'), 'PS_CANONICAL_REDIRECT' => array('title' => $this->l('Automatically redirect to Canonical url'), 'desc' => $this->l('Recommended but your theme must be compliant'), 'validation' => 'isBool', 'cast' => 'intval', 'type' => 'bool'), ); - if (!Tools::getValue('__PS_BASE_URI__')) - $_POST['__PS_BASE_URI__'] = __PS_BASE_URI__; + + // Display route options only if friendly URL is activated + if (Configuration::get('PS_REWRITING_SETTINGS')) + { + $this->_fieldsOptions += array( + 'PS_ROUTE_product_rule' => array('title' => $this->l('Route to products'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['product_rule']['rule']), + 'PS_ROUTE_product_rule2' => array('title' => $this->l('Route to products with category'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['product_rule2']['rule']), + 'PS_ROUTE_category_rule' => array('title' => $this->l('Route to category'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['category_rule']['rule']), + 'PS_ROUTE_supplier_rule' => array('title' => $this->l('Route to supplier'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['supplier_rule']['rule']), + 'PS_ROUTE_manufacturer_rule' => array('title' => $this->l('Route to manufacturer'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['manufacturer_rule']['rule']), + 'PS_ROUTE_cms_rule' => array('title' => $this->l('Route to CMS page'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['cms_rule']['rule']), + 'PS_ROUTE_cms_category_rule' => array('title' => $this->l('Route to CMS category'), 'desc' => $this->l('Select URL pattern if friendly url is activated'), 'validation' => 'isString', 'type' => 'text', 'size' => 50, 'defaultValue' => Dispatcher::getInstance()->defaultRoutes['cms_category_rule']['rule']), + ); + } } public function display() @@ -189,6 +201,64 @@ class AdminMeta extends AdminTab { parent::getList($id_lang, $orderBy, $orderWay, $start, $limit, Context::getContext()->shop->getID()); } + + /** + * Validate route syntax and save it in configuration + * + * @param string $routeID + */ + public function checkAndUpdateRoute($routeID) + { + $defaultRoutes = Dispatcher::getInstance()->defaultRoutes; + if (!isset($defaultRoutes[$routeID])) + return ; + + $rule = Tools::getValue('PS_ROUTE_'.$routeID); + if (!$rule || $rule == $defaultRoutes[$routeID]['rule']) + return ; + + $errors = array(); + if (!Dispatcher::getInstance()->validateRoute($routeID, $rule, $errors)) + { + foreach ($errors as $error) + $this->_errors[] = sprintf('Keyword "%1$s" required for route "%2$s" (rule: "%3$s")', $error, $routeID, htmlspecialchars($rule)); + } + else + Configuration::updateValue('PS_ROUTE_'.$routeID, $rule); + } + + public function updateOptionPsDispatcherRouteProductRule() + { + $this->checkAndUpdateRoute('product_rule'); + } + + public function updateOptionPsDispatcherRouteProductRule2() + { + $this->checkAndUpdateRoute('product_rule2'); + } + + public function updateOptionPsDispatcherRouteCategoryRule() + { + $this->checkAndUpdateRoute('category_rule'); + } + + public function updateOptionPsDispatcherRouteSupplierRule() + { + $this->checkAndUpdateRoute('supplier_rule'); + } + + public function updateOptionPsDispatcherRouteManufacturerRule() + { + $this->checkAndUpdateRoute('manufacturer_rule'); + } + + public function updateOptionPsDispatcherRouteCmsRule() + { + $this->checkAndUpdateRoute('cms_rule'); + } + + public function updateOptionPsDispatcherRouteCmsCategoryRule() + { + $this->checkAndUpdateRoute('cms_category_rule'); + } } - - diff --git a/classes/AdminTab.php b/classes/AdminTab.php index 4690709f5..097318984 100644 --- a/classes/AdminTab.php +++ b/classes/AdminTab.php @@ -1693,6 +1693,9 @@ abstract class AdminTabCore if ($field['type'] != 'textLang') if (!Validate::isCleanHtml($val)) $val = Configuration::get($key); + + if (isset($field['defaultValue']) && !$val) + $val = $field['defaultValue']; $isDisabled = (Shop::isMultiShopActivated() && isset($field['visibility']) && $field['visibility'] > $this->context->shop->getContextType()) ? true : false; @@ -2162,7 +2165,7 @@ EOF; $method_name = 'updateOption'.Tools::toCamelCase($key, true); if (method_exists($this, $method_name)) $this->$method_name(Tools::getValue($key)); - if (isset($options['type']) && in_array($options['type'], array('textLang', 'textareaLang'))) + else if (isset($options['type']) && in_array($options['type'], array('textLang', 'textareaLang'))) { $list = array(); foreach ($languages as $language) diff --git a/classes/Dispatcher.php b/classes/Dispatcher.php index 3cce9afe5..c8c212e4f 100644 --- a/classes/Dispatcher.php +++ b/classes/Dispatcher.php @@ -40,7 +40,7 @@ class DispatcherCore * * @var array */ - protected $defaultRoutes = array( + public $defaultRoutes = array( 'product_rule' => array( 'controller' => 'product', 'rule' => '{number:id_product}-{text}.html', @@ -175,9 +175,9 @@ class DispatcherCore foreach ($this->defaultRoutes as $id => $route) $this->addRoute($id, $route['rule'], $route['controller']); - // Load routes from meta table if ($this->useRoutes) { + // Load routes from meta table $sql = 'SELECT m.page, ml.url_rewrite FROM `'._DB_PREFIX_.'meta` m LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta'.$context->shop->sqlLang('ml').') @@ -194,6 +194,11 @@ class DispatcherCore 'controller' => $row['page'], ); } + + // Load custom routes + foreach ($this->defaultRoutes as $routeID => $routeData) + if ($customRoute = Configuration::get('PS_ROUTE_'.$routeID)) + $this->addRoute($routeID, $customRoute, $routeData['controller']); } } @@ -225,6 +230,24 @@ class DispatcherCore 'required' => $required, ); } + + public function validateRoute($routeID, $rule, &$errors = array()) + { + $errors = array(); + if (!isset($this->defaultRoutes[$routeID])) + return false; + + $required = array(); + preg_match_all('#\{('.implode('|', array_keys($this->keywords)).')[0-9]*:([a-z0-9_]+)\}#', $this->defaultRoutes[$routeID]['rule'], $m); + for ($i = 0, $total = count($m[0]); $i < $total; $i++) + $required[] = $m[2][$i]; + + foreach ($required as $requiredKey) + if (!preg_match('#\{('.implode('|', array_keys($this->keywords)).')[0-9]*:'.$requiredKey.'\}#', $rule)) + $errors[] = $requiredKey; + + return (count($errors)) ? false : true; + } /** * Create an url from diff --git a/classes/FrontController.php b/classes/FrontController.php index 96901de18..77e1e3d0a 100755 --- a/classes/FrontController.php +++ b/classes/FrontController.php @@ -393,7 +393,7 @@ class FrontControllerCore { // $_SERVER['HTTP_HOST'] must be replaced by the real canonical domain - $canonicalURL = $link->getPageLink($this->php_self, $this->ssl, $this->context->language->id); + $canonicalURL = $this->context->link->getPageLink($this->php_self, $this->ssl, $this->context->language->id); if (!Tools::getValue('ajax') && !preg_match('/^'.Tools::pRegexp($canonicalURL, '/').'([&?].*)?$/', (($this->ssl AND Configuration::get('PS_SSL_ENABLED')) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])) { header('HTTP/1.0 301 Moved');