PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Link.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 676 lines | 423 code | 97 blank | 156 comment | 147 complexity | bcd6e0706cf9e2ecbf4198845eb9ecaa MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class LinkCore
  27. {
  28. /** @var boolean Rewriting activation */
  29. protected $allow;
  30. protected $url;
  31. public static $cache = array('page' => array());
  32. public $protocol_link;
  33. public $protocol_content;
  34. protected $ssl_enable;
  35. protected static $category_disable_rewrite = null;
  36. /**
  37. * Constructor (initialization only)
  38. */
  39. public function __construct($protocol_link = null, $protocol_content = null)
  40. {
  41. $this->allow = (int)Configuration::get('PS_REWRITING_SETTINGS');
  42. $this->url = $_SERVER['SCRIPT_NAME'];
  43. $this->protocol_link = $protocol_link;
  44. $this->protocol_content = $protocol_content;
  45. if (!defined('_PS_BASE_URL_'))
  46. define('_PS_BASE_URL_', Tools::getShopDomain(true));
  47. if (!defined('_PS_BASE_URL_SSL_'))
  48. define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
  49. if (Link::$category_disable_rewrite === null)
  50. Link::$category_disable_rewrite = array(Configuration::get('PS_HOME_CATEGORY'), Configuration::get('PS_ROOT_CATEGORY'));
  51. $this->ssl_enable = Configuration::get('PS_SSL_ENABLED');
  52. }
  53. /**
  54. * Create a link to delete a product
  55. *
  56. * @param mixed $product ID of the product OR a Product object
  57. * @param int $id_picture ID of the picture to delete
  58. * @return string
  59. */
  60. public function getProductDeletePictureLink($product, $id_picture)
  61. {
  62. $url = $this->getProductLink($product);
  63. return $url.((strpos($url, '?')) ? '&' : '?').'&deletePicture='.$id_picture;
  64. }
  65. /**
  66. * Create a link to a product
  67. *
  68. * @param mixed $product Product object (can be an ID product, but deprecated)
  69. * @param string $alias
  70. * @param string $category
  71. * @param string $ean13
  72. * @param int $id_lang
  73. * @param int $id_shop (since 1.5.0) ID shop need to be used when we generate a product link for a product in a cart
  74. * @param int $ipa ID product attribute
  75. * @return string
  76. */
  77. public function getProductLink($product, $alias = null, $category = null, $ean13 = null, $id_lang = null, $id_shop = null, $ipa = 0, $force_routes = false, $relative_protocol = false)
  78. {
  79. $dispatcher = Dispatcher::getInstance();
  80. if (!$id_lang)
  81. $id_lang = Context::getContext()->language->id;
  82. $url = $this->getBaseLink($id_shop, null, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
  83. if (!is_object($product))
  84. {
  85. if (is_array($product) && isset($product['id_product']))
  86. $product = new Product($product['id_product'], false, $id_lang, $id_shop);
  87. elseif ((int)$product)
  88. $product = new Product((int)$product, false, $id_lang, $id_shop);
  89. else
  90. throw new PrestaShopException('Invalid product vars');
  91. }
  92. // Set available keywords
  93. $params = array();
  94. $params['id'] = $product->id;
  95. $params['rewrite'] = (!$alias) ? $product->getFieldByLang('link_rewrite') : $alias;
  96. $params['ean13'] = (!$ean13) ? $product->ean13 : $ean13;
  97. $params['meta_keywords'] = Tools::str2url($product->getFieldByLang('meta_keywords'));
  98. $params['meta_title'] = Tools::str2url($product->getFieldByLang('meta_title'));
  99. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'manufacturer', $id_shop))
  100. $params['manufacturer'] = Tools::str2url($product->isFullyLoaded ? $product->manufacturer_name : Manufacturer::getNameById($product->id_manufacturer));
  101. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'supplier', $id_shop))
  102. $params['supplier'] = Tools::str2url($product->isFullyLoaded ? $product->supplier_name : Supplier::getNameById($product->id_supplier));
  103. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'price', $id_shop))
  104. $params['price'] = $product->isFullyLoaded ? $product->price : Product::getPriceStatic($product->id, false, null, 6, null, false, true, 1, false, null, null, null, $product->specificPrice);
  105. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'tags', $id_shop))
  106. $params['tags'] = Tools::str2url($product->getTags($id_lang));
  107. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'category', $id_shop))
  108. $params['category'] = (!is_null($product->category) && !empty($product->category)) ? Tools::str2url($product->category) : Tools::str2url($category);
  109. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'reference', $id_shop))
  110. $params['reference'] = Tools::str2url($product->reference);
  111. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'categories', $id_shop))
  112. {
  113. $params['category'] = (!$category) ? $product->category : $category;
  114. $cats = array();
  115. foreach ($product->getParentCategories() as $cat)
  116. if (!in_array($cat['id_category'], Link::$category_disable_rewrite))//remove root and home category from the URL
  117. $cats[] = $cat['link_rewrite'];
  118. $params['categories'] = implode('/', $cats);
  119. }
  120. $anchor = $ipa ? $product->getAnchor($ipa) : '';
  121. return $url.$dispatcher->createUrl('product_rule', $id_lang, $params, $force_routes, $anchor, $id_shop);
  122. }
  123. /**
  124. * Create a link to a category
  125. *
  126. * @param mixed $category Category object (can be an ID category, but deprecated)
  127. * @param string $alias
  128. * @param int $id_lang
  129. * @param string $selected_filters Url parameter to autocheck filters of the module blocklayered
  130. * @return string
  131. */
  132. public function getCategoryLink($category, $alias = null, $id_lang = null, $selected_filters = null, $id_shop = null, $relative_protocol = false)
  133. {
  134. if (!$id_lang)
  135. $id_lang = Context::getContext()->language->id;
  136. $url = $this->getBaseLink($id_shop, null, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
  137. if (!is_object($category))
  138. $category = new Category($category, $id_lang);
  139. // Set available keywords
  140. $params = array();
  141. $params['id'] = $category->id;
  142. $params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
  143. $params['meta_keywords'] = Tools::str2url($category->getFieldByLang('meta_keywords'));
  144. $params['meta_title'] = Tools::str2url($category->getFieldByLang('meta_title'));
  145. // Selected filters is used by the module blocklayered
  146. $selected_filters = is_null($selected_filters) ? '' : $selected_filters;
  147. if (empty($selected_filters))
  148. $rule = 'category_rule';
  149. else
  150. {
  151. $rule = 'layered_rule';
  152. $params['selected_filters'] = $selected_filters;
  153. }
  154. return $url.Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow, '', $id_shop);
  155. }
  156. /**
  157. * Create a link to a CMS category
  158. *
  159. * @param mixed $category CMSCategory object (can be an ID category, but deprecated)
  160. * @param string $alias
  161. * @param int $id_lang
  162. * @return string
  163. */
  164. public function getCMSCategoryLink($cms_category, $alias = null, $id_lang = null, $id_shop = null, $relative_protocol = false)
  165. {
  166. if (!$id_lang)
  167. $id_lang = Context::getContext()->language->id;
  168. $url = $this->getBaseLink($id_shop, null, $relative_protocol = false).$this->getLangLink($id_lang, null, $id_shop);
  169. $dispatcher = Dispatcher::getInstance();
  170. if (!is_object($cms_category))
  171. {
  172. if ($alias !== null && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_title', $id_shop))
  173. return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, array('id' => (int)$cms_category, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  174. $cms_category = new CMSCategory($cms_category, $id_lang);
  175. }
  176. if (is_array($cms_category->link_rewrite) && isset($cms_category->link_rewrite[(int)$id_lang]))
  177. $cms_category->link_rewrite = $cms_category->link_rewrite[(int)$id_lang];
  178. if (is_array($cms_category->meta_keywords) && isset($cms_category->meta_keywords[(int)$id_lang]))
  179. $cms_category->meta_keywords = $cms_category->meta_keywords[(int)$id_lang];
  180. if (is_array($cms_category->meta_title) && isset($cms_category->meta_title[(int)$id_lang]))
  181. $cms_category->meta_title = $cms_category->meta_title[(int)$id_lang];
  182. // Set available keywords
  183. $params = array();
  184. $params['id'] = $cms_category->id;
  185. $params['rewrite'] = (!$alias) ? $cms_category->link_rewrite : $alias;
  186. $params['meta_keywords'] = Tools::str2url($cms_category->meta_keywords);
  187. $params['meta_title'] = Tools::str2url($cms_category->meta_title);
  188. return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, $params, $this->allow, '', $id_shop);
  189. }
  190. /**
  191. * Create a link to a CMS page
  192. *
  193. * @param mixed $cms CMS object (can be an ID CMS, but deprecated)
  194. * @param string $alias
  195. * @param bool $ssl
  196. * @param int $id_lang
  197. * @return string
  198. */
  199. public function getCMSLink($cms, $alias = null, $ssl = null, $id_lang = null, $id_shop = null, $relative_protocol = false)
  200. {
  201. if (!$id_lang)
  202. $id_lang = Context::getContext()->language->id;
  203. $url = $this->getBaseLink($id_shop, $ssl, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
  204. $dispatcher = Dispatcher::getInstance();
  205. if (!is_object($cms))
  206. {
  207. if ($alias !== null && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_title', $id_shop))
  208. return $url.$dispatcher->createUrl('cms_rule', $id_lang, array('id' => (int)$cms, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  209. $cms = new CMS($cms, $id_lang);
  210. }
  211. // Set available keywords
  212. $params = array();
  213. $params['id'] = $cms->id;
  214. $params['rewrite'] = (!$alias) ? (is_array($cms->link_rewrite) ? $cms->link_rewrite[(int)$id_lang] : $cms->link_rewrite) : $alias;
  215. $params['meta_keywords'] = '';
  216. if (isset($cms->meta_keywords) && !empty($cms->meta_keywords))
  217. $params['meta_keywords'] = is_array($cms->meta_keywords) ? Tools::str2url($cms->meta_keywords[(int)$id_lang]) : Tools::str2url($cms->meta_keywords);
  218. $params['meta_title'] = '';
  219. if (isset($cms->meta_title) && !empty($cms->meta_title))
  220. $params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int)$id_lang]) : Tools::str2url($cms->meta_title);
  221. return $url.$dispatcher->createUrl('cms_rule', $id_lang, $params, $this->allow, '', $id_shop);
  222. }
  223. /**
  224. * Create a link to a supplier
  225. *
  226. * @param mixed $supplier Supplier object (can be an ID supplier, but deprecated)
  227. * @param string $alias
  228. * @param int $id_lang
  229. * @return string
  230. */
  231. public function getSupplierLink($supplier, $alias = null, $id_lang = null, $id_shop = null, $relative_protocol = false)
  232. {
  233. if (!$id_lang)
  234. $id_lang = Context::getContext()->language->id;
  235. $url = $this->getBaseLink($id_shop, null, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
  236. $dispatcher = Dispatcher::getInstance();
  237. if (!is_object($supplier))
  238. {
  239. if ($alias !== null && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_title', $id_shop))
  240. return $url.$dispatcher->createUrl('supplier_rule', $id_lang, array('id' => (int)$supplier, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  241. $supplier = new Supplier($supplier, $id_lang);
  242. }
  243. // Set available keywords
  244. $params = array();
  245. $params['id'] = $supplier->id;
  246. $params['rewrite'] = (!$alias) ? $supplier->link_rewrite : $alias;
  247. $params['meta_keywords'] = Tools::str2url($supplier->meta_keywords);
  248. $params['meta_title'] = Tools::str2url($supplier->meta_title);
  249. return $url.$dispatcher->createUrl('supplier_rule', $id_lang, $params, $this->allow, '', $id_shop);
  250. }
  251. /**
  252. * Create a link to a manufacturer
  253. *
  254. * @param mixed $manufacturer Manufacturer object (can be an ID supplier, but deprecated)
  255. * @param string $alias
  256. * @param int $id_lang
  257. * @return string
  258. */
  259. public function getManufacturerLink($manufacturer, $alias = null, $id_lang = null, $id_shop = null, $relative_protocol = false)
  260. {
  261. if (!$id_lang)
  262. $id_lang = Context::getContext()->language->id;
  263. $url = $this->getBaseLink($id_shop, null, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
  264. $dispatcher = Dispatcher::getInstance();
  265. if (!is_object($manufacturer))
  266. {
  267. if ($alias !== null && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_title', $id_shop))
  268. return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, array('id' => (int)$manufacturer, 'rewrite' => (string)$alias), $this->allow, '', $id_shop);
  269. $manufacturer = new Manufacturer($manufacturer, $id_lang);
  270. }
  271. // Set available keywords
  272. $params = array();
  273. $params['id'] = $manufacturer->id;
  274. $params['rewrite'] = (!$alias) ? $manufacturer->link_rewrite : $alias;
  275. $params['meta_keywords'] = Tools::str2url($manufacturer->meta_keywords);
  276. $params['meta_title'] = Tools::str2url($manufacturer->meta_title);
  277. return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, $params, $this->allow, '', $id_shop);
  278. }
  279. /**
  280. * Create a link to a module
  281. *
  282. * @since 1.5.0
  283. * @param string $module Module name
  284. * @param string $process Action name
  285. * @param int $id_lang
  286. * @return string
  287. */
  288. public function getModuleLink($module, $controller = 'default', array $params = array(), $ssl = null, $id_lang = null, $id_shop = null, $relative_protocol = false)
  289. {
  290. if (!$id_lang)
  291. $id_lang = Context::getContext()->language->id;
  292. $url = $this->getBaseLink($id_shop, $ssl, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
  293. // Set available keywords
  294. $params['module'] = $module;
  295. $params['controller'] = $controller ? $controller : 'default';
  296. // If the module has its own route ... just use it !
  297. if (Dispatcher::getInstance()->hasRoute('module-'.$module.'-'.$controller, $id_lang, $id_shop))
  298. return $this->getPageLink('module-'.$module.'-'.$controller, $ssl, $id_lang, $params);
  299. else
  300. return $url.Dispatcher::getInstance()->createUrl('module', $id_lang, $params, $this->allow, '', $id_shop);
  301. }
  302. /**
  303. * Use controller name to create a link
  304. *
  305. * @param string $controller
  306. * @param boolean $with_token include or not the token in the url
  307. * @return string url
  308. */
  309. public function getAdminLink($controller, $with_token = true)
  310. {
  311. $id_lang = Context::getContext()->language->id;
  312. $params = $with_token ? array('token' => Tools::getAdminTokenLite($controller)) : array();
  313. return Dispatcher::getInstance()->createUrl($controller, $id_lang, $params, false);
  314. }
  315. /**
  316. * Returns a link to a product image for display
  317. * Note: the new image filesystem stores product images in subdirectories of img/p/
  318. *
  319. * @param string $name rewrite link of the image
  320. * @param string $ids id part of the image filename - can be "id_product-id_image" (legacy support, recommended) or "id_image" (new)
  321. * @param string $type
  322. */
  323. public function getImageLink($name, $ids, $type = null)
  324. {
  325. $not_default = false;
  326. // Check if module is installed, enabled, customer is logged in and watermark logged option is on
  327. if (Configuration::get('WATERMARK_LOGGED') && (Module::isInstalled('watermark') && Module::isEnabled('watermark')) && isset(Context::getContext()->customer->id))
  328. $type .= '-'.Configuration::get('WATERMARK_HASH');
  329. // legacy mode or default image
  330. $theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
  331. if ((Configuration::get('PS_LEGACY_IMAGES')
  332. && (file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg')))
  333. || ($not_default = strpos($ids, 'default') !== false))
  334. {
  335. if ($this->allow == 1 && !$not_default)
  336. $uri_path = __PS_BASE_URI__.$ids.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
  337. else
  338. $uri_path = _THEME_PROD_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg';
  339. }
  340. else
  341. {
  342. // if ids if of the form id_product-id_image, we want to extract the id_image part
  343. $split_ids = explode('-', $ids);
  344. $id_image = (isset($split_ids[1]) ? $split_ids[1] : $split_ids[0]);
  345. $theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
  346. if ($this->allow == 1)
  347. $uri_path = __PS_BASE_URI__.$id_image.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
  348. else
  349. $uri_path = _THEME_PROD_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').$theme.'.jpg';
  350. }
  351. return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
  352. }
  353. public function getMediaLink($filepath)
  354. {
  355. return $this->protocol_content.Tools::getMediaServer($filepath).$filepath;
  356. }
  357. /**
  358. * Create a simple link
  359. *
  360. * @param string $controller
  361. * @param bool $ssl
  362. * @param int $id_lang
  363. * @param string|array $request
  364. * @param bool $request_url_encode Use URL encode
  365. *
  366. * @return string Page link
  367. */
  368. public function getPageLink($controller, $ssl = null, $id_lang = null, $request = null, $request_url_encode = false, $id_shop = null, $relative_protocol = false)
  369. {
  370. //If $controller contains '&' char, it means that $controller contains request data and must be parsed first
  371. $p = strpos($controller, '&');
  372. if ($p !== false)
  373. {
  374. $request = substr($controller, $p + 1);
  375. $request_url_encode = false;
  376. $controller = substr($controller, 0, $p);
  377. }
  378. $controller = Tools::strReplaceFirst('.php', '', $controller);
  379. if (!$id_lang)
  380. $id_lang = (int)Context::getContext()->language->id;
  381. //need to be unset because getModuleLink need those params when rewrite is enable
  382. if (is_array($request))
  383. {
  384. if (isset($request['module']))
  385. unset($request['module']);
  386. if (isset($request['controller']))
  387. unset($request['controller']);
  388. }
  389. else
  390. {
  391. // @FIXME html_entity_decode has been added due to '&amp;' => '%3B' ...
  392. $request = html_entity_decode($request);
  393. if ($request_url_encode)
  394. $request = urlencode($request);
  395. parse_str($request, $request);
  396. }
  397. $uri_path = Dispatcher::getInstance()->createUrl($controller, $id_lang, $request, false, '', $id_shop);
  398. return $this->getBaseLink($id_shop, $ssl, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop).ltrim($uri_path, '/');
  399. }
  400. public function getCatImageLink($name, $id_category, $type = null)
  401. {
  402. if ($this->allow == 1 && $type)
  403. $uri_path = __PS_BASE_URI__.'c/'.$id_category.'-'.$type.'/'.$name.'.jpg';
  404. else
  405. $uri_path = _THEME_CAT_DIR_.$id_category.($type ? '-'.$type : '').'.jpg';
  406. return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
  407. }
  408. /**
  409. * Create link after language change, for the change language block
  410. *
  411. * @param integer $id_lang Language ID
  412. * @return string link
  413. */
  414. public function getLanguageLink($id_lang, Context $context = null)
  415. {
  416. if (!$context)
  417. $context = Context::getContext();
  418. $params = $_GET;
  419. unset($params['isolang'], $params['controller']);
  420. if (!$this->allow)
  421. $params['id_lang'] = $id_lang;
  422. else
  423. unset($params['id_lang']);
  424. $controller = Dispatcher::getInstance()->getController();
  425. if (!empty(Context::getContext()->controller->php_self))
  426. $controller = Context::getContext()->controller->php_self;
  427. if ($controller == 'product' && isset($params['id_product']))
  428. return $this->getProductLink((int)$params['id_product'], null, null, null, (int)$id_lang);
  429. elseif ($controller == 'category' && isset($params['id_category']))
  430. return $this->getCategoryLink((int)$params['id_category'], null, (int)$id_lang);
  431. elseif ($controller == 'supplier' && isset($params['id_supplier']))
  432. return $this->getSupplierLink((int)$params['id_supplier'], null, (int)$id_lang);
  433. elseif ($controller == 'manufacturer' && isset($params['id_manufacturer']))
  434. return $this->getManufacturerLink((int)$params['id_manufacturer'], null, (int)$id_lang);
  435. elseif ($controller == 'cms' && isset($params['id_cms']))
  436. return $this->getCMSLink((int)$params['id_cms'], null, false, (int)$id_lang);
  437. elseif ($controller == 'cms' && isset($params['id_cms_category']))
  438. return $this->getCMSCategoryLink((int)$params['id_cms_category'], null, (int)$id_lang);
  439. elseif (isset($params['fc']) && $params['fc'] == 'module')
  440. {
  441. $module = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
  442. if (!empty($module))
  443. {
  444. unset($params['fc'], $params['module']);
  445. return $this->getModuleLink($module, $controller, $params, null, (int)$id_lang);
  446. }
  447. }
  448. return $this->getPageLink($controller, null, $id_lang, $params);
  449. }
  450. public function goPage($url, $p)
  451. {
  452. $url = rtrim(str_replace('?&', '?', $url), '?');
  453. return $url.($p == 1 ? '' : (!strstr($url, '?') ? '?' : '&').'p='.(int)$p);
  454. }
  455. /**
  456. * Get pagination link
  457. *
  458. * @param string $type Controller name
  459. * @param int $id_object
  460. * @param boolean $nb Show nb element per page attribute
  461. * @param boolean $sort Show sort attribute
  462. * @param boolean $pagination Show page number attribute
  463. * @param boolean $array If false return an url, if true return an array
  464. */
  465. public function getPaginationLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
  466. {
  467. // If no parameter $type, try to get it by using the controller name
  468. if (!$type && !$id_object)
  469. {
  470. $method_name = 'get'.Dispatcher::getInstance()->getController().'Link';
  471. if (method_exists($this, $method_name) && isset($_GET['id_'.Dispatcher::getInstance()->getController()]))
  472. {
  473. $type = Dispatcher::getInstance()->getController();
  474. $id_object = $_GET['id_'.$type];
  475. }
  476. }
  477. if ($type && $id_object)
  478. $url = $this->{'get'.$type.'Link'}($id_object, null);
  479. else
  480. {
  481. if (isset(Context::getContext()->controller->php_self))
  482. $name = Context::getContext()->controller->php_self;
  483. else
  484. $name = Dispatcher::getInstance()->getController();
  485. $url = $this->getPageLink($name);
  486. }
  487. $vars = array();
  488. $vars_nb = array('n', 'search_query');
  489. $vars_sort = array('orderby', 'orderway');
  490. $vars_pagination = array('p');
  491. foreach ($_GET as $k => $value)
  492. {
  493. if ($k != 'id_'.$type && $k != 'controller')
  494. {
  495. if (Configuration::get('PS_REWRITING_SETTINGS') && ($k == 'isolang' || $k == 'id_lang'))
  496. continue;
  497. $if_nb = (!$nb || ($nb && !in_array($k, $vars_nb)));
  498. $if_sort = (!$sort || ($sort && !in_array($k, $vars_sort)));
  499. $if_pagination = (!$pagination || ($pagination && !in_array($k, $vars_pagination)));
  500. if ($if_nb && $if_sort && $if_pagination)
  501. {
  502. if (!is_array($value))
  503. $vars[urlencode($k)] = $value;
  504. else
  505. {
  506. foreach (explode('&', http_build_query(array($k => $value), '', '&')) as $key => $val)
  507. {
  508. $data = explode('=', $val);
  509. $vars[urldecode($data[0])] = $data[1];
  510. }
  511. }
  512. }
  513. }
  514. }
  515. if (!$array)
  516. if (count($vars))
  517. return $url.(($this->allow == 1 || $url == $this->url) ? '?' : '&').http_build_query($vars, '', '&');
  518. else
  519. return $url;
  520. $vars['requestUrl'] = $url;
  521. if ($type && $id_object)
  522. $vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
  523. if (!$this->allow == 1)
  524. $vars['controller'] = Dispatcher::getInstance()->getController();
  525. return $vars;
  526. }
  527. public function addSortDetails($url, $orderby, $orderway)
  528. {
  529. return $url.(!strstr($url, '?') ? '?' : '&').'orderby='.urlencode($orderby).'&orderway='.urlencode($orderway);
  530. }
  531. protected function getLangLink($id_lang = null, Context $context = null, $id_shop = null)
  532. {
  533. if (!$context)
  534. $context = Context::getContext();
  535. if ((!$this->allow && in_array($id_shop, array($context->shop->id, null))) || !Language::isMultiLanguageActivated($id_shop) || !(int)Configuration::get('PS_REWRITING_SETTINGS', null, null, $id_shop))
  536. return '';
  537. if (!$id_lang)
  538. $id_lang = $context->language->id;
  539. return Language::getIsoById($id_lang).'/';
  540. }
  541. protected function getBaseLink($id_shop = null, $ssl = null, $relative_protocol = false)
  542. {
  543. static $force_ssl = null;
  544. if ($ssl === null)
  545. {
  546. if ($force_ssl === null)
  547. $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
  548. $ssl = $force_ssl;
  549. }
  550. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && $id_shop !== null)
  551. $shop = new Shop($id_shop);
  552. else
  553. $shop = Context::getContext()->shop;
  554. if ($relative_protocol)
  555. $base = '//'.($ssl && $this->ssl_enable ? $shop->domain_ssl : $shop->domain);
  556. else
  557. $base = (($ssl && $this->ssl_enable) ? 'https://'.$shop->domain_ssl : 'http://'.$shop->domain);
  558. return $base.$shop->getBaseURI();
  559. }
  560. public static function getQuickLink($url)
  561. {
  562. $parsedUrl = parse_url($url);
  563. $output = array();
  564. if (is_array($parsedUrl) && isset($parsedUrl['query']))
  565. {
  566. parse_str($parsedUrl['query'], $output);
  567. unset($output['token'], $output['conf'], $output['id_quick_access']);
  568. }
  569. return http_build_query($output);
  570. }
  571. public function matchQuickLink($url)
  572. {
  573. $quicklink = $this->getQuickLink($url);
  574. if (isset($quicklink) && $quicklink === ($this->getQuickLink($_SERVER['REQUEST_URI'])))
  575. return true;
  576. else
  577. return false;
  578. }
  579. }