PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Link.php

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