PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 408 lines | 187 code | 31 blank | 190 comment | 18 complexity | 8e71f2a30d98a6a7e9bdfa3d473679ed 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-2016 X.commerce, Inc. and affiliates (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($data);
  259. }
  260. /**
  261. * Escape quotes in java script
  262. *
  263. * @param mixed $data
  264. * @param string $quote
  265. * @return mixed
  266. */
  267. public function jsQuoteEscape($data, $quote='\'')
  268. {
  269. if (is_array($data)) {
  270. $result = array();
  271. foreach ($data as $item) {
  272. $result[] = str_replace($quote, '\\'.$quote, $item);
  273. }
  274. return $result;
  275. }
  276. return str_replace($quote, '\\'.$quote, $data);
  277. }
  278. /**
  279. * Escape quotes inside html attributes
  280. * Use $addSlashes = false for escaping js that inside html attribute (onClick, onSubmit etc)
  281. *
  282. * @param string $data
  283. * @param bool $addSlashes
  284. * @return string
  285. */
  286. public function quoteEscape($data, $addSlashes = false)
  287. {
  288. if ($addSlashes === true) {
  289. $data = addslashes($data);
  290. }
  291. return htmlspecialchars($data, ENT_QUOTES, null, false);
  292. }
  293. /**
  294. * Retrieve url
  295. *
  296. * @param string $route
  297. * @param array $params
  298. * @return string
  299. */
  300. protected function _getUrl($route, $params = array())
  301. {
  302. return Mage::getUrl($route, $params);
  303. }
  304. /**
  305. * Declare layout
  306. *
  307. * @param Mage_Core_Model_Layout $layout
  308. * @return Mage_Core_Helper_Abstract
  309. */
  310. public function setLayout($layout)
  311. {
  312. $this->_layout = $layout;
  313. return $this;
  314. }
  315. /**
  316. * Retrieve layout model object
  317. *
  318. * @return Mage_Core_Model_Layout
  319. */
  320. public function getLayout()
  321. {
  322. return $this->_layout;
  323. }
  324. /**
  325. * base64_encode() for URLs encoding
  326. *
  327. * @param string $url
  328. * @return string
  329. */
  330. public function urlEncode($url)
  331. {
  332. return strtr(base64_encode($url), '+/=', '-_,');
  333. }
  334. /**
  335. * base64_dencode() for URLs dencoding
  336. *
  337. * @param string $url
  338. * @return string
  339. */
  340. public function urlDecode($url)
  341. {
  342. $url = base64_decode(strtr($url, '-_,', '+/='));
  343. return Mage::getSingleton('core/url')->sessionUrlVar($url);
  344. }
  345. /**
  346. * base64_decode() and escape quotes in url
  347. *
  348. * @param string $url
  349. * @return string
  350. */
  351. public function urlDecodeAndEscape($url)
  352. {
  353. $url = $this->urlDecode($url);
  354. $quote = array ('\'', '"');
  355. $replace = array('%27', '%22');
  356. $url = str_replace($quote, $replace, $url);
  357. return $url;
  358. }
  359. /**
  360. * Translate array
  361. *
  362. * @param array $arr
  363. * @return array
  364. */
  365. public function translateArray($arr = array())
  366. {
  367. foreach ($arr as $k => $v) {
  368. if (is_array($v)) {
  369. $v = self::translateArray($v);
  370. } elseif ($k === 'label') {
  371. $v = self::__($v);
  372. }
  373. $arr[$k] = $v;
  374. }
  375. return $arr;
  376. }
  377. }