PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/override/classes/_Tools.php

https://bitbucket.org/enurkov/prestashop
PHP | 321 lines | 186 code | 27 blank | 108 comment | 35 complexity | bd206a0903c543769ab5550df4caebdc 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. //
  27. // IMPORTANT : don't forget to delete the underscore _ in the file name if you want to use it !
  28. //
  29. // if FB class is already loaded, just enable it. else, enable it only if fb.php exists and is loaded
  30. if (!defined('PS_USE_FIREPHP') AND class_exists('FB'))
  31. define('PS_USE_FIREPHP',true);
  32. elseif (file_exists(_PS_TOOL_DIR_.'FirePHP/fb.php'))
  33. {
  34. if (!defined('PS_USE_FIREPHP'))
  35. {
  36. require_once(_PS_TOOL_DIR_.'FirePHP/fb.php');
  37. define('PS_USE_FIREPHP',true);
  38. }
  39. else
  40. define('PS_USE_FIREPHP',false);
  41. }
  42. else
  43. define('PS_USE_FIREPHP',class_exists('FB'));
  44. class Tools extends ToolsCore
  45. {
  46. /**
  47. * Redirect user to another page after 5 sec
  48. *
  49. * @param string $url Desired URL
  50. * @param string $baseUri Base URI (optional)
  51. */
  52. public static function redirect($url, $baseUri = __PS_BASE_URI__, Link $link = null)
  53. {
  54. if (!$link)
  55. $link = Context::getContext()->link;
  56. if (strpos($url, 'http://') === FALSE && strpos($url, 'https://') === FALSE)
  57. {
  58. if (strpos($url, $baseUri) !== FALSE && strpos($url, $baseUri) == 0)
  59. $url = substr($url, strlen($baseUri));
  60. $explode = explode('?', $url);
  61. $url = $link->getPageLink($explode[0], true);
  62. if (isset($explode[1]))
  63. $url .= '?'.$explode[1];
  64. $baseUri = '';
  65. }
  66. if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER']))
  67. header('Refresh: 5; url='.$_SERVER['HTTP_REFERER']);
  68. else
  69. header('Refresh: 5; url='.$baseUri.$url);
  70. echo '<h1>Redirection automatique dans 5 secondes</h1><a href='.$url.'>'.$url.'</a>';
  71. exit;
  72. }
  73. /**
  74. * Redirect url wich allready PS_BASE_URI after 5 sec
  75. *
  76. * @param string $url Desired URL
  77. */
  78. public static function redirectLink($url)
  79. {
  80. if (!preg_match('@^https?://@i', $url))
  81. {
  82. if (strpos($url, __PS_BASE_URI__) !== FALSE && strpos($url, __PS_BASE_URI__) == 0)
  83. $url = substr($url, strlen(__PS_BASE_URI__));
  84. $explode = explode('?', $url);
  85. $url = Context::getContext()->link->getPageLink($explode[0]);
  86. if (isset($explode[1]))
  87. $url .= '?'.$explode[1];
  88. }
  89. header('Refresh: 5; url='.$url);
  90. echo '<h1>Redirection automatique dans 5 secondes</h1><a href='.$url.'>'.$url.'</a>';
  91. exit;
  92. }
  93. /**
  94. * Redirect user to another admin page after 5 sec
  95. *
  96. * @param string $url Desired URL
  97. */
  98. public static function redirectAdmin($url)
  99. {
  100. header('Refresh: 5; url='.$url);
  101. echo '<h1>Redirection automatique dans 5 secondes</h1><a href='.$url.'>'.$url.'</a>';
  102. exit;
  103. }
  104. /**
  105. * Display an error with detailed object
  106. * (display in firefox console if Firephp is enabled)
  107. *
  108. * @param mixed $object
  109. * @param boolean $kill
  110. * @return $object if $kill = false;
  111. */
  112. public static function dieObject($object, $kill = true)
  113. {
  114. if(PS_USE_FIREPHP)
  115. FB::error($object);
  116. else
  117. return parent::dieObject($object,$kill);
  118. if ($kill)
  119. die('END');
  120. return $object;
  121. }
  122. /**
  123. * ALIAS OF dieObject() - Display an error with detailed object
  124. * (display in firefox console if Firephp is enabled)
  125. *
  126. * @param object $object Object to display
  127. */
  128. public static function d($obj, $kill = true)
  129. {
  130. if(PS_USE_FIREPHP)
  131. FB::error($obj);
  132. else
  133. parent::d($obj,$kill);
  134. if ($kill)
  135. die('END');
  136. return $object;
  137. }
  138. /**
  139. * ALIAS OF dieObject() - Display an error with detailed object but don't stop the execution
  140. * (display in firefox console if Firephp is enabled)
  141. *
  142. * @param object $object Object to display
  143. */
  144. public static function p($object)
  145. {
  146. if(PS_USE_FIREPHP)
  147. FB::info($object);
  148. else
  149. return parent::p($object);
  150. return $object;
  151. }
  152. /**
  153. * Display a warning message indicating that the method is deprecated
  154. * (display in firefox console if Firephp is enabled)
  155. */
  156. public static function displayAsDeprecated($message = null)
  157. {
  158. if (_PS_DISPLAY_COMPATIBILITY_WARNING_)
  159. {
  160. $backtrace = debug_backtrace();
  161. $callee = next($backtrace);
  162. if (PS_USE_FIREPHP)
  163. FB::warn('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', 'Deprecated method');
  164. else
  165. trigger_error('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', E_USER_WARNING);
  166. $message = sprintf(
  167. Tools::displayError('The function %1$s (Line %2$s) is deprecated and will be removed in the next major version.'),
  168. $callee['function'],
  169. $callee['line']
  170. );
  171. Logger::addLog($message, 3, $callee['class']);
  172. }
  173. }
  174. /**
  175. * Display a warning message indicating that the parameter is deprecated
  176. * (display in firefox console if Firephp is enabled)
  177. */
  178. public static function displayParameterAsDeprecated($parameter)
  179. {
  180. if (_PS_DISPLAY_COMPATIBILITY_WARNING_)
  181. {
  182. $backtrace = debug_backtrace();
  183. $callee = next($backtrace);
  184. trigger_error('Parameter <strong>'.$parameter.'</strong> in function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['Line'].'</strong><br />', E_USER_WARNING);
  185. if(PS_USE_FIREPHP)
  186. FB::trace('Parameter <strong>'.$parameter.'</strong> in function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['Line'].'</strong><br />', 'deprecated parameter');
  187. else
  188. $message = sprintf(
  189. Tools::displayError('The parameter %1$s in function %2$s (Line %3$s) is deprecated and will be removed in the next major version.'),
  190. $parameter,
  191. $callee['function'],
  192. $callee['Line']
  193. );
  194. Logger::addLog($message, 3, $callee['class']);
  195. }
  196. }
  197. /**
  198. * use of FirePHP::error() if allowed
  199. *
  200. * @param mixed $obj
  201. * @param string $label
  202. * @return void
  203. */
  204. public static function error($obj, $label = '')
  205. {
  206. if(PS_USE_FIREPHP)
  207. FB::error($obj, $label);
  208. }
  209. /**
  210. * use of FirePHP::warn() if allowed
  211. *
  212. * @param mixed $obj
  213. * @param string $label
  214. * @return void
  215. */
  216. public static function warn($obj, $label = '')
  217. {
  218. if(PS_USE_FIREPHP)
  219. FB::warn($obj, $label);
  220. }
  221. /**
  222. * use of FirePHP::info() if allowed
  223. *
  224. * @param mixed $obj
  225. * @param string $label
  226. * @return void
  227. */
  228. public static function info($obj, $label = '')
  229. {
  230. if(PS_USE_FIREPHP)
  231. FB::info($obj, $label);
  232. }
  233. /**
  234. * use of FirePHP::log() if allowed
  235. *
  236. * @param mixed $obj
  237. * @param string $label
  238. * @return void
  239. */
  240. public static function log($obj, $label = '')
  241. {
  242. if(PS_USE_FIREPHP)
  243. FB::log($obj,$label);
  244. }
  245. /**
  246. * display debug_backtrace()
  247. * (display in firefox console if Firephp is enabled)
  248. *
  249. * @param mixed $obj
  250. * @return void
  251. */
  252. public static function trace($obj = NULL, $label = '')
  253. {
  254. if(PS_USE_FIREPHP)
  255. FB::trace($obj, $label);
  256. else{
  257. Tools::p($obj);
  258. echo'<pre><h1>'.$label.'</h1><br/>';
  259. debug_print_backtrace();
  260. echo '</pre>';
  261. }
  262. }
  263. }
  264. // Add some convenient shortcut
  265. if (!function_exists('error'))
  266. {
  267. function error($obj, $label = ''){
  268. return Tools::error($obj, $label);
  269. }
  270. }
  271. if (!function_exists('warn'))
  272. {
  273. function warn($obj, $label = ''){
  274. return Tools::warn($obj,$label);
  275. }
  276. }
  277. if (!function_exists('info'))
  278. {
  279. function info($obj, $label = ''){
  280. return Tools::info($obj, $label);
  281. }
  282. }
  283. if (!function_exists('log'))
  284. {
  285. function log($obj, $label = ''){
  286. return Tools::log($obj, $label);
  287. }
  288. }
  289. if (!function_exists('trace'))
  290. {
  291. function trace($obj, $label = ''){
  292. return Tools::trace($obj, $label);
  293. }
  294. }