PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Link.php

https://bitbucket.org/enurkov/prestashop
PHP | 568 lines | 334 code | 81 blank | 153 comment | 107 complexity | 45152e410f836a925249eed5db7ae5c8 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 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-2012 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. /**
  36. * Constructor (initialization only)
  37. */
  38. public function __construct($protocol_link = null, $protocol_content = null)
  39. {
  40. $this->allow = (int)Configuration::get('PS_REWRITING_SETTINGS');
  41. $this->url = $_SERVER['SCRIPT_NAME'];
  42. $this->protocol_link = $protocol_link;
  43. $this->protocol_content = $protocol_content;
  44. if (!defined('_PS_BASE_URL_'))
  45. define('_PS_BASE_URL_', Tools::getShopDomain(true));
  46. if (!defined('_PS_BASE_URL_SSL_'))
  47. define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
  48. $this->ssl_enable = Configuration::get('PS_SSL_ENABLED');
  49. }
  50. /**
  51. * Create a link to delete a product
  52. *
  53. * @param mixed $product ID of the product OR a Product object
  54. * @param int $id_picture ID of the picture to delete
  55. * @return string
  56. */
  57. public function getProductDeletePictureLink($product, $id_picture)
  58. {
  59. $url = $this->getProductLink($product);
  60. return $url.((strpos($url, '?')) ? '&' : '?').'&deletePicture='.$id_picture;
  61. }
  62. /**
  63. * Create a link to a product
  64. *
  65. * @param mixed $product Product object (can be an ID product, but deprecated)
  66. * @param string $alias
  67. * @param string $category
  68. * @param string $ean13
  69. * @param int $id_lang
  70. * @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
  71. * @param int $ipa ID product attribute
  72. * @return string
  73. */
  74. public function getProductLink($product, $alias = null, $category = null, $ean13 = null, $id_lang = null, $id_shop = null, $ipa = 0, $force_routes = false)
  75. {
  76. $dispatcher = Dispatcher::getInstance();
  77. if (!$id_lang)
  78. $id_lang = Context::getContext()->language->id;
  79. if (!$id_shop)
  80. $shop = Context::getContext()->shop;
  81. else
  82. $shop = new Shop($id_shop);
  83. $url = 'http://'.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang);
  84. if (!is_object($product))
  85. {
  86. if (is_array($product) && isset($product['id_product']))
  87. $product = new Product($product['id_product'], false, $id_lang);
  88. else if (is_numeric($product) || !$product)
  89. $product = new Product($product, false, $id_lang);
  90. else
  91. throw new PrestaShopException('Invalid product vars');
  92. }
  93. // Set available keywords
  94. $params = array();
  95. $params['id'] = $product->id;
  96. $params['rewrite'] = (!$alias) ? $product->getFieldByLang('link_rewrite') : $alias;
  97. $params['ean13'] = (!$ean13) ? $product->ean13 : $ean13;
  98. $params['meta_keywords'] = Tools::str2url($product->getFieldByLang('meta_keywords'));
  99. $params['meta_title'] = Tools::str2url($product->getFieldByLang('meta_title'));
  100. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'manufacturer'))
  101. $params['manufacturer'] = Tools::str2url($product->isFullyLoaded ? $product->manufacturer_name : Manufacturer::getNameById($product->id_manufacturer));
  102. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'supplier'))
  103. $params['supplier'] = Tools::str2url($product->isFullyLoaded ? $product->supplier_name : Supplier::getNameById($product->id_supplier));
  104. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'price'))
  105. $params['price'] = $product->isFullyLoaded ? $product->price : Product::getPriceStatic($product->id, false, null, 6, null, false, true, 1, false, null, null, null, $product->specificPrice);
  106. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'tags'))
  107. $params['tags'] = Tools::str2url($product->getTags($id_lang));
  108. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'category'))
  109. $params['category'] = Tools::str2url($product->category);
  110. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'reference'))
  111. $params['reference'] = Tools::str2url($product->reference);
  112. if ($dispatcher->hasKeyword('product_rule', $id_lang, 'categories'))
  113. {
  114. $params['category'] = (!$category) ? $product->category : $category;
  115. $cats = array();
  116. foreach ($product->getParentCategories() as $cat)
  117. if (!in_array($cat['id_category'], array(1, 2)))//remove root and home category from the URL
  118. $cats[] = $cat['link_rewrite'];
  119. $params['categories'] = implode('/', $cats);
  120. }
  121. $anchor = $ipa ? $product->getAnchor($ipa) : '';
  122. return $url.$dispatcher->createUrl('product_rule', $id_lang, $params, $force_routes, $anchor);
  123. }
  124. /**
  125. * Create a link to a category
  126. *
  127. * @param mixed $category Category object (can be an ID category, but deprecated)
  128. * @param string $alias
  129. * @param int $id_lang
  130. * @param string $selected_filters Url parameter to autocheck filters of the module blocklayered
  131. * @return string
  132. */
  133. public function getCategoryLink($category, $alias = null, $id_lang = null, $selected_filters = null)
  134. {
  135. if (!$id_lang)
  136. $id_lang = Context::getContext()->language->id;
  137. $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang);
  138. if (!is_object($category))
  139. $category = new Category($category, $id_lang);
  140. // Set available keywords
  141. $params = array();
  142. $params['id'] = $category->id;
  143. $params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
  144. $params['meta_keywords'] = Tools::str2url($category->meta_keywords);
  145. $params['meta_title'] = Tools::str2url($category->meta_title);
  146. // Selected filters is used by the module blocklayered
  147. $selected_filters = is_null($selected_filters) ? '' : $selected_filters;
  148. if (empty($selected_filters))
  149. $rule = 'category_rule';
  150. else
  151. {
  152. $rule = 'layered_rule';
  153. $params['selected_filters'] = $selected_filters;
  154. }
  155. return $url.Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow);
  156. }
  157. /**
  158. * Create a link to a CMS category
  159. *
  160. * @param mixed $category CMSCategory object (can be an ID category, but deprecated)
  161. * @param string $alias
  162. * @param int $id_lang
  163. * @return string
  164. */
  165. public function getCMSCategoryLink($category, $alias = null, $id_lang = null)
  166. {
  167. if (!$id_lang)
  168. $id_lang = Context::getContext()->language->id;
  169. $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang);
  170. if (!is_object($category))
  171. $category = new CMSCategory($category, $id_lang);
  172. // Set available keywords
  173. $params = array();
  174. $params['id'] = $category->id;
  175. $params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
  176. $params['meta_keywords'] = Tools::str2url($category->meta_keywords);
  177. $params['meta_title'] = Tools::str2url($category->meta_title);
  178. return $url.Dispatcher::getInstance()->createUrl('cms_category_rule', $id_lang, $params, $this->allow);
  179. }
  180. /**
  181. * Create a link to a CMS page
  182. *
  183. * @param mixed $cms CMS object (can be an ID CMS, but deprecated)
  184. * @param string $alias
  185. * @param bool $ssl
  186. * @param int $id_lang
  187. * @return string
  188. */
  189. public function getCMSLink($cms, $alias = null, $ssl = false, $id_lang = null)
  190. {
  191. $base = (($ssl && $this->ssl_enable) ? _PS_BASE_URL_SSL_ : _PS_BASE_URL_);
  192. if (!$id_lang)
  193. $id_lang = Context::getContext()->language->id;
  194. $url = $base.__PS_BASE_URI__.$this->getLangLink($id_lang);
  195. if (!is_object($cms))
  196. $cms = new CMS($cms, $id_lang);
  197. // Set available keywords
  198. $params = array();
  199. $params['id'] = $cms->id;
  200. $params['rewrite'] = (!$alias) ? (is_array($cms->link_rewrite) ? $cms->link_rewrite[(int)$id_lang] : $cms->link_rewrite) : $alias;
  201. if (isset($cms->meta_keywords) && !empty($cms->meta_keywords))
  202. $params['meta_keywords'] = is_array($cms->meta_keywords) ? Tools::str2url($cms->meta_keywords[(int)$id_lang]) : Tools::str2url($cms->meta_keywords);
  203. else
  204. $params['meta_keywords'] = '';
  205. if (isset($cms->meta_title) && !empty($cms->meta_title))
  206. $params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int)$id_lang]) : Tools::str2url($cms->meta_title);
  207. else
  208. $params['meta_title'] = '';
  209. return $url.Dispatcher::getInstance()->createUrl('cms_rule', $id_lang, $params, $this->allow);
  210. }
  211. /**
  212. * Create a link to a supplier
  213. *
  214. * @param mixed $supplier Supplier object (can be an ID supplier, but deprecated)
  215. * @param string $alias
  216. * @param int $id_lang
  217. * @return string
  218. */
  219. public function getSupplierLink($supplier, $alias = null, $id_lang = null)
  220. {
  221. if (!$id_lang)
  222. $id_lang = Context::getContext()->language->id;
  223. $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang);
  224. if (!is_object($supplier))
  225. $supplier = new Supplier($supplier, $id_lang);
  226. // Set available keywords
  227. $params = array();
  228. $params['id'] = $supplier->id;
  229. $params['rewrite'] = (!$alias) ? $supplier->link_rewrite : $alias;
  230. $params['meta_keywords'] = Tools::str2url($supplier->meta_keywords);
  231. $params['meta_title'] = Tools::str2url($supplier->meta_title);
  232. return $url.Dispatcher::getInstance()->createUrl('supplier_rule', $id_lang, $params, $this->allow);
  233. }
  234. /**
  235. * Create a link to a manufacturer
  236. *
  237. * @param mixed $manufacturer Manufacturer object (can be an ID supplier, but deprecated)
  238. * @param string $alias
  239. * @param int $id_lang
  240. * @return string
  241. */
  242. public function getManufacturerLink($manufacturer, $alias = null, $id_lang = null)
  243. {
  244. if (!$id_lang)
  245. $id_lang = Context::getContext()->language->id;
  246. $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang);
  247. if (!is_object($manufacturer))
  248. $manufacturer = new Manufacturer($manufacturer, $id_lang);
  249. // Set available keywords
  250. $params = array();
  251. $params['id'] = $manufacturer->id;
  252. $params['rewrite'] = (!$alias) ? $manufacturer->link_rewrite : $alias;
  253. $params['meta_keywords'] = Tools::str2url($manufacturer->meta_keywords);
  254. $params['meta_title'] = Tools::str2url($manufacturer->meta_title);
  255. return $url.Dispatcher::getInstance()->createUrl('manufacturer_rule', $id_lang, $params, $this->allow);
  256. }
  257. /**
  258. * Create a link to a module
  259. *
  260. * @since 1.5.0
  261. * @param string $module Module name
  262. * @param string $process Action name
  263. * @param int $id_lang
  264. * @return string
  265. */
  266. public function getModuleLink($module, $controller = 'default', array $params = array(), $ssl = false, $id_lang = null)
  267. {
  268. $base = (($ssl && $this->ssl_enable) ? _PS_BASE_URL_SSL_ : _PS_BASE_URL_);
  269. if (!$id_lang)
  270. $id_lang = Context::getContext()->language->id;
  271. $url = $base.__PS_BASE_URI__.$this->getLangLink($id_lang);
  272. // Set available keywords
  273. $params['module'] = $module;
  274. $params['controller'] = $controller ? $controller : 'default';
  275. // If the module has its own route ... just use it !
  276. if (Dispatcher::getInstance()->hasRoute('module-'.$module.'-'.$controller, $id_lang))
  277. return $this->getPageLink('module-'.$module.'-'.$controller, $ssl, $id_lang, $params);
  278. else
  279. return $url.Dispatcher::getInstance()->createUrl('module', $id_lang, $params, $this->allow);
  280. }
  281. /**
  282. * Use controller name to create a link
  283. *
  284. * @param string $controller
  285. * @param boolean $with_token include or not the token in the url
  286. * @return controller url
  287. */
  288. public function getAdminLink($controller, $with_token = true)
  289. {
  290. $id_lang = Context::getContext()->language->id;
  291. $params = $with_token ? array('token' => Tools::getAdminTokenLite($controller)) : array();
  292. return Dispatcher::getInstance()->createUrl($controller, $id_lang, $params, false);
  293. }
  294. /**
  295. * Returns a link to a product image for display
  296. * Note: the new image filesystem stores product images in subdirectories of img/p/
  297. *
  298. * @param string $name rewrite link of the image
  299. * @param string $ids id part of the image filename - can be "id_product-id_image" (legacy support, recommended) or "id_image" (new)
  300. * @param string $type
  301. */
  302. public function getImageLink($name, $ids, $type = null)
  303. {
  304. $not_default = false;
  305. // legacy mode or default image
  306. $theme = ((Shop::isFeatureActive() && file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').'-'.(int)Context::getContext()->shop->id_theme.'.jpg')) ? '-'.Context::getContext()->shop->id_theme : '');
  307. if ((Configuration::get('PS_LEGACY_IMAGES')
  308. && (file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg')))
  309. || ($not_default = strpos($ids, 'default') !== false))
  310. {
  311. if ($this->allow == 1 && !$not_default)
  312. $uri_path = __PS_BASE_URI__.$ids.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
  313. else
  314. $uri_path = _THEME_PROD_DIR_.$ids.($type ? '-'.$type : '').$theme.'.jpg';
  315. }
  316. else
  317. {
  318. // if ids if of the form id_product-id_image, we want to extract the id_image part
  319. $split_ids = explode('-', $ids);
  320. $id_image = (isset($split_ids[1]) ? $split_ids[1] : $split_ids[0]);
  321. $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 : '');
  322. if ($this->allow == 1)
  323. $uri_path = __PS_BASE_URI__.$id_image.($type ? '-'.$type : '').$theme.'/'.$name.'.jpg';
  324. else
  325. $uri_path = _THEME_PROD_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').$theme.'.jpg';
  326. }
  327. return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
  328. }
  329. public function getMediaLink($filepath)
  330. {
  331. return Tools::getProtocol().Tools::getMediaServer($filepath).$filepath;
  332. }
  333. /**
  334. * Create a simple link
  335. *
  336. * @param string $controller
  337. * @param bool $ssl
  338. * @param int $id_lang
  339. * @param string|array $request
  340. * @param bool $request_url_encode Use URL encode
  341. *
  342. * @return string Page link
  343. */
  344. public function getPageLink($controller, $ssl = false, $id_lang = null, $request = null, $request_url_encode = false)
  345. {
  346. $controller = Tools::strReplaceFirst('.php', '', $controller);
  347. if (!$id_lang)
  348. $id_lang = (int)Context::getContext()->language->id;
  349. if (!is_array($request))
  350. {
  351. // @FIXME html_entity_decode has been added due to '&amp;' => '%3B' ...
  352. $request = html_entity_decode($request);
  353. if ($request_url_encode)
  354. $request = urlencode($request);
  355. parse_str($request, $request);
  356. }
  357. $uri_path = Dispatcher::getInstance()->createUrl($controller, $id_lang, $request);
  358. $url = ($ssl && $this->ssl_enable) ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true);
  359. $url .= __PS_BASE_URI__.$this->getLangLink($id_lang).ltrim($uri_path, '/');
  360. return $url;
  361. }
  362. public function getCatImageLink($name, $id_category, $type = null)
  363. {
  364. $uri_path = ($this->allow == 1) ? (__PS_BASE_URI__.'c/'.$id_category.($type ? '-'.$type : '').'/'.$name.'.jpg') : (_THEME_CAT_DIR_.$id_category.($type ? '-'.$type : '').'.jpg');
  365. return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
  366. }
  367. /**
  368. * Create link after language change, for the change language block
  369. *
  370. * @param integer $id_lang Language ID
  371. * @return string link
  372. */
  373. public function getLanguageLink($id_lang, Context $context = null)
  374. {
  375. if (!$context)
  376. $context = Context::getContext();
  377. $params = $_GET;
  378. unset($params['isolang'], $params['controller']);
  379. if (!$this->allow)
  380. $params['id_lang'] = $id_lang;
  381. else
  382. unset($params['id_lang']);
  383. $controller = Dispatcher::getInstance()->getController();
  384. if (!empty(Context::getContext()->controller->php_self))
  385. $controller = Context::getContext()->controller->php_self;
  386. if ($controller == 'product' && isset($params['id_product']))
  387. return $this->getProductLink((int)$params['id_product'], null, null, null, (int)$id_lang);
  388. elseif ($controller == 'category' && isset($params['id_category']))
  389. return $this->getCategoryLink((int)$params['id_category'], null, (int)$id_lang);
  390. elseif ($controller == 'supplier' && isset($params['id_supplier']))
  391. return $this->getSupplierLink((int)$params['id_supplier'], null, (int)$id_lang);
  392. elseif ($controller == 'manufacturer' && isset($params['id_manufacturer']))
  393. return $this->getManufacturerLink((int)$params['id_manufacturer'], null, (int)$id_lang);
  394. elseif ($controller == 'cms' && isset($params['id_cms']))
  395. return $this->getCMSLink((int)$params['id_cms'], null, false, (int)$id_lang);
  396. elseif ($controller == 'cms' && isset($params['id_cms_category']))
  397. return $this->getCMSCategoryLink((int)$params['id_cms_category'], null, (int)$id_lang);
  398. return $this->getPageLink($controller, false, $id_lang, $params);
  399. }
  400. public function goPage($url, $p)
  401. {
  402. return $url.($p == 1 ? '' : (!strstr($url, '?') ? '?' : '&amp;').'p='.(int)$p);
  403. }
  404. /**
  405. * Get pagination link
  406. *
  407. * @param string $type Controller name
  408. * @param int $id_object
  409. * @param boolean $nb Show nb element per page attribute
  410. * @param boolean $sort Show sort attribute
  411. * @param boolean $pagination Show page number attribute
  412. * @param boolean $array If false return an url, if true return an array
  413. */
  414. public function getPaginationLink($type, $id_object, $nb = false, $sort = false, $pagination = false, $array = false)
  415. {
  416. // If no parameter $type, try to get it by using the controller name
  417. if (!$type && !$id_object)
  418. {
  419. $method_name = 'get'.Dispatcher::getInstance()->getController().'Link';
  420. if (method_exists($this, $method_name) && isset($_GET['id_'.Dispatcher::getInstance()->getController()]))
  421. {
  422. $type = Dispatcher::getInstance()->getController();
  423. $id_object = $_GET['id_'.$type];
  424. }
  425. }
  426. if ($type && $id_object)
  427. $url = $this->{'get'.$type.'Link'}($id_object, null);
  428. else
  429. {
  430. if (isset(Context::getContext()->controller->php_self))
  431. $name = Context::getContext()->controller->php_self;
  432. else
  433. $name = Dispatcher::getInstance()->getController();
  434. $url = $this->getPageLink($name);
  435. }
  436. $vars = array();
  437. $vars_nb = array('n', 'search_query');
  438. $vars_sort = array('orderby', 'orderway');
  439. $vars_pagination = array('p');
  440. foreach ($_GET as $k => $value)
  441. {
  442. if ($k != 'id_'.$type && $k != 'controller')
  443. {
  444. if (Configuration::get('PS_REWRITING_SETTINGS') && ($k == 'isolang' || $k == 'id_lang'))
  445. continue;
  446. $if_nb = (!$nb || ($nb && !in_array($k, $vars_nb)));
  447. $if_sort = (!$sort || ($sort && !in_array($k, $vars_sort)));
  448. $if_pagination = (!$pagination || ($pagination && !in_array($k, $vars_pagination)));
  449. if ($if_nb && $if_sort && $if_pagination)
  450. {
  451. if (!is_array($value))
  452. $vars[urlencode($k)] = $value;
  453. else
  454. {
  455. foreach (explode('&', http_build_query(array($k => $value), '', '&')) as $key => $val)
  456. {
  457. $data = explode('=', $val);
  458. $vars[urldecode($data[0])] = $data[1];
  459. }
  460. }
  461. }
  462. }
  463. }
  464. if (!$array)
  465. return $url.(($this->allow == 1 || $url == $this->url) ? '?' : '&').http_build_query($vars, '', '&');
  466. $vars['requestUrl'] = $url;
  467. if ($type && $id_object)
  468. $vars['id_'.$type] = (is_object($id_object) ? (int)$id_object->id : (int)$id_object);
  469. if (!$this->allow == 1)
  470. $vars['controller'] = Dispatcher::getInstance()->getController();
  471. return $vars;
  472. }
  473. public function addSortDetails($url, $orderby, $orderway)
  474. {
  475. return $url.(!strstr($url, '?') ? '?' : '&').'orderby='.urlencode($orderby).'&orderway='.urlencode($orderway);
  476. }
  477. protected function getLangLink($id_lang = null, Context $context = null)
  478. {
  479. if (!$context)
  480. $context = Context::getContext();
  481. if (!$this->allow || !Language::isMultiLanguageActivated())
  482. return '';
  483. if (!$id_lang)
  484. $id_lang = $context->language->id;
  485. return Language::getIsoById($id_lang).'/';
  486. }
  487. }