PageRenderTime 88ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Core/Helper/Abstract.php

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 484 lines | 237 code | 37 blank | 210 comment | 28 complexity | 517490cce084aed4198cc9aa78ba4d97 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  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@magento.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 Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Abstract helper
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. abstract class Mage_Core_Helper_Abstract
  32. {
  33. /**
  34. * Helper module name
  35. *
  36. * @var string
  37. */
  38. protected $_moduleName;
  39. /**
  40. * Request object
  41. *
  42. * @var Zend_Controller_Request_Http
  43. */
  44. protected $_request;
  45. /**
  46. * Layout model object
  47. *
  48. * @var Mage_Core_Model_Layout
  49. */
  50. protected $_layout;
  51. /**
  52. * Retrieve request object
  53. *
  54. * @return Zend_Controller_Request_Http
  55. */
  56. protected function _getRequest()
  57. {
  58. if (!$this->_request) {
  59. $this->_request = Mage::app()->getRequest();
  60. }
  61. return $this->_request;
  62. }
  63. /**
  64. * Loading cache data
  65. *
  66. * @param string $id
  67. * @return mixed
  68. */
  69. protected function _loadCache($id)
  70. {
  71. return Mage::app()->loadCache($id);
  72. }
  73. /**
  74. * Saving cache
  75. *
  76. * @param mixed $data
  77. * @param string $id
  78. * @param array $tags
  79. * @return Mage_Core_Helper_Abstract
  80. */
  81. protected function _saveCache($data, $id, $tags=array(), $lifeTime=false)
  82. {
  83. Mage::app()->saveCache($data, $id, $tags, $lifeTime);
  84. return $this;
  85. }
  86. /**
  87. * Removing cache
  88. *
  89. * @param string $id
  90. * @return Mage_Core_Helper_Abstract
  91. */
  92. protected function _removeCache($id)
  93. {
  94. Mage::app()->removeCache($id);
  95. return $this;
  96. }
  97. /**
  98. * Cleaning cache
  99. *
  100. * @param array $tags
  101. * @return Mage_Core_Helper_Abstract
  102. */
  103. protected function _cleanCache($tags=array())
  104. {
  105. Mage::app()->cleanCache($tags);
  106. return $this;
  107. }
  108. /**
  109. * Retrieve helper module name
  110. *
  111. * @return string
  112. */
  113. protected function _getModuleName()
  114. {
  115. if (!$this->_moduleName) {
  116. $class = get_class($this);
  117. $this->_moduleName = substr($class, 0, strpos($class, '_Helper'));
  118. }
  119. return $this->_moduleName;
  120. }
  121. /**
  122. * Check whether or not the module output is enabled in Configuration
  123. *
  124. * @param string $moduleName Full module name
  125. * @return boolean
  126. */
  127. public function isModuleOutputEnabled($moduleName = null)
  128. {
  129. if ($moduleName === null) {
  130. $moduleName = $this->_getModuleName();
  131. }
  132. if (!$this->isModuleEnabled($moduleName)) {
  133. return false;
  134. }
  135. if (Mage::getStoreConfigFlag('advanced/modules_disable_output/' . $moduleName)) {
  136. return false;
  137. }
  138. return true;
  139. }
  140. /**
  141. * Check is module exists and enabled in global config.
  142. *
  143. * @param string $moduleName the full module name, example Mage_Core
  144. * @return boolean
  145. */
  146. public function isModuleEnabled($moduleName = null)
  147. {
  148. if ($moduleName === null) {
  149. $moduleName = $this->_getModuleName();
  150. }
  151. if (!Mage::getConfig()->getNode('modules/' . $moduleName)) {
  152. return false;
  153. }
  154. $isActive = Mage::getConfig()->getNode('modules/' . $moduleName . '/active');
  155. if (!$isActive || !in_array((string)$isActive, array('true', '1'))) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * Translate
  162. *
  163. * @return string
  164. */
  165. public function __()
  166. {
  167. $args = func_get_args();
  168. $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->_getModuleName());
  169. array_unshift($args, $expr);
  170. return Mage::app()->getTranslator()->translate($args);
  171. }
  172. /**
  173. * @deprecated after 1.4.0.0-rc1
  174. * @see self::escapeHtml()
  175. */
  176. public function htmlEscape($data, $allowedTags = null)
  177. {
  178. return $this->escapeHtml($data, $allowedTags);
  179. }
  180. /**
  181. * Escape html entities
  182. *
  183. * @param mixed $data
  184. * @param array $allowedTags
  185. * @return mixed
  186. */
  187. public function escapeHtml($data, $allowedTags = null)
  188. {
  189. if (is_array($data)) {
  190. $result = array();
  191. foreach ($data as $item) {
  192. $result[] = $this->escapeHtml($item);
  193. }
  194. } else {
  195. // process single item
  196. if (strlen($data)) {
  197. if (is_array($allowedTags) and !empty($allowedTags)) {
  198. $allowed = implode('|', $allowedTags);
  199. $result = preg_replace('/<([\/\s\r\n]*)(' . $allowed . ')([\/\s\r\n]*)>/si', '##$1$2$3##', $data);
  200. $result = htmlspecialchars($result, ENT_COMPAT, 'UTF-8', false);
  201. $result = preg_replace('/##([\/\s\r\n]*)(' . $allowed . ')([\/\s\r\n]*)##/si', '<$1$2$3>', $result);
  202. } else {
  203. $result = htmlspecialchars($data, ENT_COMPAT, 'UTF-8', false);
  204. }
  205. } else {
  206. $result = $data;
  207. }
  208. }
  209. return $result;
  210. }
  211. /**
  212. * Remove html tags, but leave "<" and ">" signs
  213. *
  214. * @param string $html
  215. * @return string
  216. */
  217. public function removeTags($html)
  218. {
  219. $html = preg_replace_callback(
  220. "# <(?![/a-z]) | (?<=\s)>(?![a-z]) #xi",
  221. function ($matches) {
  222. return htmlentities($matches[0]);
  223. },
  224. $html
  225. );
  226. $html = strip_tags($html);
  227. return htmlspecialchars_decode($html);
  228. }
  229. /**
  230. * Wrapper for standart strip_tags() function with extra functionality for html entities
  231. *
  232. * @param string $data
  233. * @param string $allowableTags
  234. * @param bool $escape
  235. * @return string
  236. */
  237. public function stripTags($data, $allowableTags = null, $escape = false)
  238. {
  239. $result = strip_tags($data, $allowableTags);
  240. return $escape ? $this->escapeHtml($result, $allowableTags) : $result;
  241. }
  242. /**
  243. * @deprecated after 1.4.0.0-rc1
  244. * @see self::escapeHtml()
  245. */
  246. public function urlEscape($data)
  247. {
  248. return $this->escapeUrl($data);
  249. }
  250. /**
  251. * Escape html entities in url
  252. *
  253. * @param string $data
  254. * @return string
  255. */
  256. public function escapeUrl($data)
  257. {
  258. return htmlspecialchars(
  259. $this->escapeScriptIdentifiers((string) $data),
  260. ENT_COMPAT | ENT_HTML5 | ENT_HTML401,
  261. 'UTF-8'
  262. );
  263. }
  264. /**
  265. * Remove `\t`,`\n`,`\r`,`\0`,`\x0B:` symbols from the string.
  266. *
  267. * @param string $data
  268. * @return string
  269. */
  270. public function escapeSpecial($data)
  271. {
  272. $specialSymbolsFiltrationPattern = '/[\t\n\r\0\x0B]+/';
  273. return (string) preg_replace($specialSymbolsFiltrationPattern, '', $data);
  274. }
  275. /**
  276. * Remove `javascript:`, `vbscript:`, `data:` words from the string.
  277. *
  278. * @param string $data
  279. * @return string
  280. */
  281. public function escapeScriptIdentifiers($data)
  282. {
  283. $scripIdentifiersFiltrationPattern = '/((javascript(\\\\x3a|:|%3A))|(data(\\\\x3a|:|%3A))|(vbscript:))|'
  284. . '((\\\\x6A\\\\x61\\\\x76\\\\x61\\\\x73\\\\x63\\\\x72\\\\x69\\\\x70\\\\x74(\\\\x3a|:|%3A))|'
  285. . '(\\\\x64\\\\x61\\\\x74\\\\x61(\\\\x3a|:|%3A)))/i';
  286. $preFilteredData = $this->escapeSpecial($data);
  287. $filteredData = preg_replace($scripIdentifiersFiltrationPattern, ':', $preFilteredData) ?: '';
  288. if (preg_match($scripIdentifiersFiltrationPattern, $filteredData)) {
  289. $filteredData = $this->escapeScriptIdentifiers($filteredData);
  290. }
  291. return $filteredData;
  292. }
  293. /**
  294. * Escape quotes in java script
  295. *
  296. * @param mixed $data
  297. * @param string $quote
  298. * @return mixed
  299. */
  300. public function jsQuoteEscape($data, $quote='\'')
  301. {
  302. if (is_array($data)) {
  303. $result = array();
  304. foreach ($data as $item) {
  305. $result[] = str_replace($quote, '\\'.$quote, $item);
  306. }
  307. return $result;
  308. }
  309. return str_replace($quote, '\\'.$quote, $data);
  310. }
  311. /**
  312. * Escape quotes inside html attributes
  313. * Use $addSlashes = false for escaping js that inside html attribute (onClick, onSubmit etc)
  314. *
  315. * @param string $data
  316. * @param bool $addSlashes
  317. * @return string
  318. */
  319. public function quoteEscape($data, $addSlashes = false)
  320. {
  321. if ($addSlashes === true) {
  322. $data = addslashes($data);
  323. }
  324. return htmlspecialchars($data, ENT_QUOTES, null, false);
  325. }
  326. /**
  327. * Retrieve url
  328. *
  329. * @param string $route
  330. * @param array $params
  331. * @return string
  332. */
  333. protected function _getUrl($route, $params = array())
  334. {
  335. return Mage::getUrl($route, $params);
  336. }
  337. /**
  338. * Declare layout
  339. *
  340. * @param Mage_Core_Model_Layout $layout
  341. * @return Mage_Core_Helper_Abstract
  342. */
  343. public function setLayout($layout)
  344. {
  345. $this->_layout = $layout;
  346. return $this;
  347. }
  348. /**
  349. * Retrieve layout model object
  350. *
  351. * @return Mage_Core_Model_Layout
  352. */
  353. public function getLayout()
  354. {
  355. return $this->_layout;
  356. }
  357. /**
  358. * base64_encode() for URLs encoding
  359. *
  360. * @param string $url
  361. * @return string
  362. */
  363. public function urlEncode($url)
  364. {
  365. return strtr(base64_encode($url), '+/=', '-_,');
  366. }
  367. /**
  368. * base64_dencode() for URLs dencoding
  369. *
  370. * @param string $url
  371. * @return string
  372. */
  373. public function urlDecode($url)
  374. {
  375. $url = base64_decode(strtr($url, '-_,', '+/='));
  376. return Mage::getSingleton('core/url')->sessionUrlVar($url);
  377. }
  378. /**
  379. * base64_decode() and escape quotes in url
  380. *
  381. * @param string $url
  382. * @return string
  383. */
  384. public function urlDecodeAndEscape($url)
  385. {
  386. $url = $this->urlDecode($url);
  387. $quote = array ('\'', '"');
  388. $replace = array('%27', '%22');
  389. $url = str_replace($quote, $replace, $url);
  390. return $url;
  391. }
  392. /**
  393. * Translate array
  394. *
  395. * @param array $arr
  396. * @return array
  397. */
  398. public function translateArray($arr = array())
  399. {
  400. foreach ($arr as $k => $v) {
  401. if (is_array($v)) {
  402. $v = self::translateArray($v);
  403. } elseif ($k === 'label') {
  404. $v = self::__($v);
  405. }
  406. $arr[$k] = $v;
  407. }
  408. return $arr;
  409. }
  410. /**
  411. * Check for tags in multidimensional arrays
  412. *
  413. * @param string|array $data
  414. * @param array $arrayKeys keys of the array being checked that are excluded and included in the check
  415. * @param bool $skipTags skip transferred array keys, if false then check only them
  416. * @return bool
  417. */
  418. public function hasTags($data, array $arrayKeys = array(), $skipTags = true)
  419. {
  420. if (is_array($data)) {
  421. foreach ($data as $key => $item) {
  422. if ($skipTags && in_array($key, $arrayKeys)) {
  423. continue;
  424. }
  425. if (is_array($item)) {
  426. if ($this->hasTags($item, $arrayKeys, $skipTags)) {
  427. return true;
  428. }
  429. } elseif (
  430. (bool)strcmp($item, $this->removeTags($item))
  431. || (bool)strcmp($key, $this->removeTags($key))
  432. ) {
  433. if (!$skipTags && !in_array($key, $arrayKeys)) {
  434. continue;
  435. }
  436. return true;
  437. }
  438. }
  439. return false;
  440. } elseif (is_string($data)) {
  441. if ((bool)strcmp($data, $this->removeTags($data))) {
  442. return true;
  443. }
  444. }
  445. return false;
  446. }
  447. }