PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/functions.php

https://gitlab.com/inglobe/mgt-clemente-css
PHP | 412 lines | 287 code | 28 blank | 97 comment | 60 complexity | e720e89444d851040333f1cd71fc72f5 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. * Disable magic quotes in runtime if needed
  28. *
  29. * @link http://us3.php.net/manual/en/security.magicquotes.disabling.php
  30. */
  31. if (get_magic_quotes_gpc()) {
  32. function mageUndoMagicQuotes($array, $topLevel=true) {
  33. $newArray = array();
  34. foreach($array as $key => $value) {
  35. if (!$topLevel) {
  36. $newKey = stripslashes($key);
  37. if ($newKey!==$key) {
  38. unset($array[$key]);
  39. }
  40. $key = $newKey;
  41. }
  42. $newArray[$key] = is_array($value) ? mageUndoMagicQuotes($value, false) : stripslashes($value);
  43. }
  44. return $newArray;
  45. }
  46. $_GET = mageUndoMagicQuotes($_GET);
  47. $_POST = mageUndoMagicQuotes($_POST);
  48. $_COOKIE = mageUndoMagicQuotes($_COOKIE);
  49. $_REQUEST = mageUndoMagicQuotes($_REQUEST);
  50. }
  51. /**
  52. * Class autoload
  53. *
  54. * @todo change to spl_autoload_register
  55. * @deprecated
  56. * @param string $class
  57. */
  58. function __autoload($class)
  59. {
  60. if (defined('COMPILER_INCLUDE_PATH')) {
  61. $classFile = $class.'.php';
  62. } else {
  63. $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
  64. }
  65. include($classFile);
  66. }
  67. /**
  68. * Object destructor
  69. *
  70. * @param mixed $object
  71. */
  72. function destruct($object)
  73. {
  74. if (is_array($object)) {
  75. foreach ($object as $obj) {
  76. destruct($obj);
  77. }
  78. }
  79. unset($object);
  80. }
  81. /**
  82. * Translator function
  83. *
  84. * @deprecated 1.3
  85. * @param string $text the text to translate
  86. * @param mixed optional parameters to use in sprintf
  87. */
  88. function __()
  89. {
  90. return Mage::app()->getTranslator()->translate(func_get_args());
  91. }
  92. /**
  93. * Tiny function to enhance functionality of ucwords
  94. *
  95. * Will capitalize first letters and convert separators if needed
  96. *
  97. * @param string $str
  98. * @param string $destSep
  99. * @param string $srcSep
  100. * @return string
  101. */
  102. function uc_words($str, $destSep='_', $srcSep='_')
  103. {
  104. return str_replace(' ', $destSep, ucwords(str_replace($srcSep, ' ', $str)));
  105. }
  106. /**
  107. * Simple sql format date
  108. *
  109. * @param string $format
  110. * @return string
  111. */
  112. function now($dayOnly=false)
  113. {
  114. return date($dayOnly ? 'Y-m-d' : 'Y-m-d H:i:s');
  115. }
  116. /**
  117. * Check whether sql date is empty
  118. *
  119. * @param string $date
  120. * @return boolean
  121. */
  122. function is_empty_date($date)
  123. {
  124. return preg_replace('#[ 0:-]#', '', $date)==='';
  125. }
  126. function mageFindClassFile($class)
  127. {
  128. if (defined('COMPILER_INCLUDE_PATH')) {
  129. $classFile = $class.'.php';
  130. } else {
  131. $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
  132. }
  133. $found = false;
  134. foreach (explode(PS, get_include_path()) as $path) {
  135. $fileName = $path.DS.$classFile;
  136. if (file_exists($fileName)) {
  137. $found = $fileName;
  138. break;
  139. }
  140. }
  141. return $found;
  142. }
  143. /**
  144. * Custom error handler
  145. *
  146. * @param integer $errno
  147. * @param string $errstr
  148. * @param string $errfile
  149. * @param integer $errline
  150. */
  151. function mageCoreErrorHandler($errno, $errstr, $errfile, $errline){
  152. if (strpos($errstr, 'DateTimeZone::__construct')!==false) {
  153. // there's no way to distinguish between caught system exceptions and warnings
  154. return false;
  155. }
  156. $errno = $errno & error_reporting();
  157. if ($errno == 0) {
  158. return false;
  159. }
  160. if (!defined('E_STRICT')) {
  161. define('E_STRICT', 2048);
  162. }
  163. if (!defined('E_RECOVERABLE_ERROR')) {
  164. define('E_RECOVERABLE_ERROR', 4096);
  165. }
  166. if (!defined('E_DEPRECATED')) {
  167. define('E_DEPRECATED', 8192);
  168. }
  169. // PEAR specific message handling
  170. if (stripos($errfile.$errstr, 'pear') !== false) {
  171. // ignore strict and deprecated notices
  172. if (($errno == E_STRICT) || ($errno == E_DEPRECATED)) {
  173. return true;
  174. }
  175. // ignore attempts to read system files when open_basedir is set
  176. if ($errno == E_WARNING && stripos($errstr, 'open_basedir') !== false) {
  177. return true;
  178. }
  179. }
  180. $errorMessage = '';
  181. switch($errno){
  182. case E_ERROR:
  183. $errorMessage .= "Error";
  184. break;
  185. case E_WARNING:
  186. $errorMessage .= "Warning";
  187. break;
  188. case E_PARSE:
  189. $errorMessage .= "Parse Error";
  190. break;
  191. case E_NOTICE:
  192. $errorMessage .= "Notice";
  193. break;
  194. case E_CORE_ERROR:
  195. $errorMessage .= "Core Error";
  196. break;
  197. case E_CORE_WARNING:
  198. $errorMessage .= "Core Warning";
  199. break;
  200. case E_COMPILE_ERROR:
  201. $errorMessage .= "Compile Error";
  202. break;
  203. case E_COMPILE_WARNING:
  204. $errorMessage .= "Compile Warning";
  205. break;
  206. case E_USER_ERROR:
  207. $errorMessage .= "User Error";
  208. break;
  209. case E_USER_WARNING:
  210. $errorMessage .= "User Warning";
  211. break;
  212. case E_USER_NOTICE:
  213. $errorMessage .= "User Notice";
  214. break;
  215. case E_STRICT:
  216. $errorMessage .= "Strict Notice";
  217. break;
  218. case E_RECOVERABLE_ERROR:
  219. $errorMessage .= "Recoverable Error";
  220. break;
  221. case E_DEPRECATED:
  222. $errorMessage .= "Deprecated functionality";
  223. break;
  224. default:
  225. $errorMessage .= "Unknown error ($errno)";
  226. break;
  227. }
  228. $errorMessage .= ": {$errstr} in {$errfile} on line {$errline}";
  229. if (Mage::getIsDeveloperMode()) {
  230. throw new Exception($errorMessage);
  231. } else {
  232. Mage::log($errorMessage, Zend_Log::ERR);
  233. }
  234. }
  235. function mageDebugBacktrace($return=false, $html=true, $showFirst=false)
  236. {
  237. $d = debug_backtrace();
  238. $out = '';
  239. if ($html) $out .= "<pre>";
  240. foreach ($d as $i=>$r) {
  241. if (!$showFirst && $i==0) {
  242. continue;
  243. }
  244. // sometimes there is undefined index 'file'
  245. @$out .= "[$i] {$r['file']}:{$r['line']}\n";
  246. }
  247. if ($html) $out .= "</pre>";
  248. if ($return) {
  249. return $out;
  250. } else {
  251. echo $out;
  252. }
  253. }
  254. function mageSendErrorHeader()
  255. {
  256. return;
  257. if (!isset($_SERVER['SCRIPT_NAME'])) {
  258. return;
  259. }
  260. $action = Mage::app()->getRequest()->getBasePath()."bugreport.php";
  261. echo '<form id="error_report" method="post" style="display:none" action="'.$action.'"><textarea name="error">';
  262. }
  263. function mageSendErrorFooter()
  264. {
  265. return;
  266. if (!isset($_SERVER['SCRIPT_NAME'])) {
  267. return;
  268. }
  269. echo '</textarea></form><script type="text/javascript">document.getElementById("error_report").submit()</script>';
  270. exit;
  271. }
  272. function mageDelTree($path) {
  273. if (is_dir($path)) {
  274. $entries = scandir($path);
  275. foreach ($entries as $entry) {
  276. if ($entry != '.' && $entry != '..') {
  277. mageDelTree($path.DS.$entry);
  278. }
  279. }
  280. @rmdir($path);
  281. } else {
  282. @unlink($path);
  283. }
  284. }
  285. function mageParseCsv($string, $delimiter=",", $enclosure='"', $escape='\\')
  286. {
  287. $elements = explode($delimiter, $string);
  288. for ($i = 0; $i < count($elements); $i++) {
  289. $nquotes = substr_count($elements[$i], $enclosure);
  290. if ($nquotes %2 == 1) {
  291. for ($j = $i+1; $j < count($elements); $j++) {
  292. if (substr_count($elements[$j], $enclosure) > 0) {
  293. // Put the quoted string's pieces back together again
  294. array_splice($elements, $i, $j-$i+1,
  295. implode($delimiter, array_slice($elements, $i, $j-$i+1)));
  296. break;
  297. }
  298. }
  299. }
  300. if ($nquotes > 0) {
  301. // Remove first and last quotes, then merge pairs of quotes
  302. $qstr =& $elements[$i];
  303. $qstr = substr_replace($qstr, '', strpos($qstr, $enclosure), 1);
  304. $qstr = substr_replace($qstr, '', strrpos($qstr, $enclosure), 1);
  305. $qstr = str_replace($enclosure.$enclosure, $enclosure, $qstr);
  306. }
  307. }
  308. return $elements;
  309. }
  310. function is_dir_writeable($dir)
  311. {
  312. if (is_dir($dir) && is_writable($dir)) {
  313. if (stripos(PHP_OS, 'win') === 0) {
  314. $dir = ltrim($dir, DIRECTORY_SEPARATOR);
  315. $file = $dir . DIRECTORY_SEPARATOR . uniqid(mt_rand()).'.tmp';
  316. $exist = file_exists($file);
  317. $fp = @fopen($file, 'a');
  318. if ($fp === false) {
  319. return false;
  320. }
  321. fclose($fp);
  322. if (!$exist) {
  323. unlink($file);
  324. }
  325. }
  326. return true;
  327. }
  328. return false;
  329. }
  330. if ( !function_exists('sys_get_temp_dir') ) {
  331. // Based on http://www.phpit.net/
  332. // article/creating-zip-tar-archives-dynamically-php/2/
  333. function sys_get_temp_dir()
  334. {
  335. // Try to get from environment variable
  336. if ( !empty($_ENV['TMP']) ) {
  337. return realpath( $_ENV['TMP'] );
  338. } else if ( !empty($_ENV['TMPDIR']) ) {
  339. return realpath( $_ENV['TMPDIR'] );
  340. } else if ( !empty($_ENV['TEMP']) ) {
  341. return realpath( $_ENV['TEMP'] );
  342. } else {
  343. // Try to use system's temporary directory
  344. // as random name shouldn't exist
  345. $temp_file = tempnam( md5(uniqid(rand(), TRUE)), '' );
  346. if ( $temp_file ) {
  347. $temp_dir = realpath( dirname($temp_file) );
  348. unlink( $temp_file );
  349. return $temp_dir;
  350. } else {
  351. return FALSE;
  352. }
  353. }
  354. }
  355. }
  356. if (!function_exists('hash_equals')) {
  357. /**
  358. * Compares two strings using the same time whether they're equal or not.
  359. * A difference in length will leak
  360. *
  361. * @param string $known_string
  362. * @param string $user_string
  363. * @return boolean Returns true when the two strings are equal, false otherwise.
  364. */
  365. function hash_equals($known_string, $user_string)
  366. {
  367. $result = 0;
  368. if (!is_string($known_string)) {
  369. trigger_error("hash_equals(): Expected known_string to be a string", E_USER_WARNING);
  370. return false;
  371. }
  372. if (!is_string($user_string)) {
  373. trigger_error("hash_equals(): Expected user_string to be a string", E_USER_WARNING);
  374. return false;
  375. }
  376. if (strlen($known_string) != strlen($user_string)) {
  377. return false;
  378. }
  379. for ($i = 0; $i < strlen($known_string); $i++) {
  380. $result |= (ord($known_string[$i]) ^ ord($user_string[$i]));
  381. }
  382. return 0 === $result;
  383. }
  384. }