PageRenderTime 142ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Link.php

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