1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31:
32: if (get_magic_quotes_gpc()) {
33: function mageUndoMagicQuotes($array, $topLevel=true) {
34: $newArray = array();
35: foreach($array as $key => $value) {
36: if (!$topLevel) {
37: $newKey = stripslashes($key);
38: if ($newKey!==$key) {
39: unset($array[$key]);
40: }
41: $key = $newKey;
42: }
43: $newArray[$key] = is_array($value) ? mageUndoMagicQuotes($value, false) : stripslashes($value);
44: }
45: return $newArray;
46: }
47: $_GET = mageUndoMagicQuotes($_GET);
48: $_POST = mageUndoMagicQuotes($_POST);
49: $_COOKIE = mageUndoMagicQuotes($_COOKIE);
50: $_REQUEST = mageUndoMagicQuotes($_REQUEST);
51: }
52:
53: 54: 55: 56: 57: 58: 59:
60: function __autoload($class)
61: {
62: if (defined('COMPILER_INCLUDE_PATH')) {
63: $classFile = $class.'.php';
64: } else {
65: $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
66: }
67:
68: include($classFile);
69: }
70:
71: 72: 73: 74: 75:
76: function destruct($object)
77: {
78: if (is_array($object)) {
79: foreach ($object as $obj) {
80: destruct($obj);
81: }
82: }
83: unset($object);
84: }
85:
86: 87: 88: 89: 90: 91: 92:
93: function __()
94: {
95: return Mage::app()->getTranslator()->translate(func_get_args());
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105: 106: 107:
108: function uc_words($str, $destSep='_', $srcSep='_')
109: {
110: return str_replace(' ', $destSep, ucwords(str_replace($srcSep, ' ', $str)));
111: }
112:
113: 114: 115: 116: 117: 118:
119: function now($dayOnly=false)
120: {
121: return date($dayOnly ? 'Y-m-d' : 'Y-m-d H:i:s');
122: }
123:
124: 125: 126: 127: 128: 129:
130: function is_empty_date($date)
131: {
132: return preg_replace('#[ 0:-]#', '', $date)==='';
133: }
134:
135: function mageFindClassFile($class)
136: {
137: if (defined('COMPILER_INCLUDE_PATH')) {
138: $classFile = $class.'.php';
139: } else {
140: $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
141: }
142: $found = false;
143: foreach (explode(PS, get_include_path()) as $path) {
144: $fileName = $path.DS.$classFile;
145: if (file_exists($fileName)) {
146: $found = $fileName;
147: break;
148: }
149: }
150: return $found;
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160:
161: function mageCoreErrorHandler($errno, $errstr, $errfile, $errline){
162: if (strpos($errstr, 'DateTimeZone::__construct')!==false) {
163:
164: return false;
165: }
166:
167: $errno = $errno & error_reporting();
168: if ($errno == 0) {
169: return false;
170: }
171: if (!defined('E_STRICT')) {
172: define('E_STRICT', 2048);
173: }
174: if (!defined('E_RECOVERABLE_ERROR')) {
175: define('E_RECOVERABLE_ERROR', 4096);
176: }
177: if (!defined('E_DEPRECATED')) {
178: define('E_DEPRECATED', 8192);
179: }
180:
181:
182: if (stripos($errfile.$errstr, 'pear') !== false) {
183:
184: if (($errno == E_STRICT) || ($errno == E_DEPRECATED)) {
185: return true;
186: }
187:
188: if ($errno == E_WARNING && stripos($errstr, 'open_basedir') !== false) {
189: return true;
190: }
191: }
192:
193: $errorMessage = '';
194:
195: switch($errno){
196: case E_ERROR:
197: $errorMessage .= "Error";
198: break;
199: case E_WARNING:
200: $errorMessage .= "Warning";
201: break;
202: case E_PARSE:
203: $errorMessage .= "Parse Error";
204: break;
205: case E_NOTICE:
206: $errorMessage .= "Notice";
207: break;
208: case E_CORE_ERROR:
209: $errorMessage .= "Core Error";
210: break;
211: case E_CORE_WARNING:
212: $errorMessage .= "Core Warning";
213: break;
214: case E_COMPILE_ERROR:
215: $errorMessage .= "Compile Error";
216: break;
217: case E_COMPILE_WARNING:
218: $errorMessage .= "Compile Warning";
219: break;
220: case E_USER_ERROR:
221: $errorMessage .= "User Error";
222: break;
223: case E_USER_WARNING:
224: $errorMessage .= "User Warning";
225: break;
226: case E_USER_NOTICE:
227: $errorMessage .= "User Notice";
228: break;
229: case E_STRICT:
230: $errorMessage .= "Strict Notice";
231: break;
232: case E_RECOVERABLE_ERROR:
233: $errorMessage .= "Recoverable Error";
234: break;
235: case E_DEPRECATED:
236: $errorMessage .= "Deprecated functionality";
237: break;
238: default:
239: $errorMessage .= "Unknown error ($errno)";
240: break;
241: }
242:
243: $errorMessage .= ": {$errstr} in {$errfile} on line {$errline}";
244: if (Mage::getIsDeveloperMode()) {
245: throw new Exception($errorMessage);
246: } else {
247: Mage::log($errorMessage, Zend_Log::ERR);
248: }
249: }
250:
251: function mageDebugBacktrace($return=false, $html=true, $showFirst=false)
252: {
253: $d = debug_backtrace();
254: $out = '';
255: if ($html) $out .= "<pre>";
256: foreach ($d as $i=>$r) {
257: if (!$showFirst && $i==0) {
258: continue;
259: }
260:
261: @$out .= "[$i] {$r['file']}:{$r['line']}\n";
262: }
263: if ($html) $out .= "</pre>";
264: if ($return) {
265: return $out;
266: } else {
267: echo $out;
268: }
269: }
270:
271: function ()
272: {
273: return;
274: if (!isset($_SERVER['SCRIPT_NAME'])) {
275: return;
276: }
277: $action = Mage::app()->getRequest()->getBasePath()."bugreport.php";
278: echo '<form id="error_report" method="post" style="display:none" action="'.$action.'"><textarea name="error">';
279: }
280:
281: function ()
282: {
283: return;
284: if (!isset($_SERVER['SCRIPT_NAME'])) {
285: return;
286: }
287: echo '</textarea></form><script type="text/javascript">document.getElementById("error_report").submit()</script>';
288: exit;
289: }
290:
291: function mageDelTree($path) {
292: if (is_dir($path)) {
293: $entries = scandir($path);
294: foreach ($entries as $entry) {
295: if ($entry != '.' && $entry != '..') {
296: mageDelTree($path.DS.$entry);
297: }
298: }
299: @rmdir($path);
300: } else {
301: @unlink($path);
302: }
303: }
304:
305: function mageParseCsv($string, $delimiter=",", $enclosure='"', $escape='\\')
306: {
307: $elements = explode($delimiter, $string);
308: for ($i = 0; $i < count($elements); $i++) {
309: $nquotes = substr_count($elements[$i], $enclosure);
310: if ($nquotes %2 == 1) {
311: for ($j = $i+1; $j < count($elements); $j++) {
312: if (substr_count($elements[$j], $enclosure) > 0) {
313:
314: array_splice($elements, $i, $j-$i+1,
315: implode($delimiter, array_slice($elements, $i, $j-$i+1)));
316: break;
317: }
318: }
319: }
320: if ($nquotes > 0) {
321:
322: $qstr =& $elements[$i];
323: $qstr = substr_replace($qstr, '', strpos($qstr, $enclosure), 1);
324: $qstr = substr_replace($qstr, '', strrpos($qstr, $enclosure), 1);
325: $qstr = str_replace($enclosure.$enclosure, $enclosure, $qstr);
326: }
327: }
328: return $elements;
329: }
330:
331: function is_dir_writeable($dir)
332: {
333: if (is_dir($dir) && is_writable($dir)) {
334: if (stripos(PHP_OS, 'win') === 0) {
335: $dir = ltrim($dir, DIRECTORY_SEPARATOR);
336: $file = $dir . DIRECTORY_SEPARATOR . uniqid(mt_rand()).'.tmp';
337: $exist = file_exists($file);
338: $fp = @fopen($file, 'a');
339: if ($fp === false) {
340: return false;
341: }
342: fclose($fp);
343: if (!$exist) {
344: unlink($file);
345: }
346: }
347: return true;
348: }
349: return false;
350: }
351:
352: if ( !function_exists('sys_get_temp_dir') ) {
353:
354:
355: function sys_get_temp_dir()
356: {
357:
358: if ( !empty($_ENV['TMP']) ) {
359: return realpath( $_ENV['TMP'] );
360: } else if ( !empty($_ENV['TMPDIR']) ) {
361: return realpath( $_ENV['TMPDIR'] );
362: } else if ( !empty($_ENV['TEMP']) ) {
363: return realpath( $_ENV['TEMP'] );
364: } else {
365:
366:
367: $temp_file = tempnam( md5(uniqid(rand(), TRUE)), '' );
368: if ( $temp_file ) {
369: $temp_dir = realpath( dirname($temp_file) );
370: unlink( $temp_file );
371: return $temp_dir;
372: } else {
373: return FALSE;
374: }
375: }
376: }
377: }
378: