00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 if (get_magic_quotes_gpc()) {
00033 function mageUndoMagicQuotes($array, $topLevel=true) {
00034 $newArray = array();
00035 foreach($array as $key => $value) {
00036 if (!$topLevel) {
00037 $newKey = stripslashes($key);
00038 if ($newKey!==$key) {
00039 unset($array[$key]);
00040 }
00041 $key = $newKey;
00042 }
00043 $newArray[$key] = is_array($value) ? mageUndoMagicQuotes($value, false) : stripslashes($value);
00044 }
00045 return $newArray;
00046 }
00047 $_GET = mageUndoMagicQuotes($_GET);
00048 $_POST = mageUndoMagicQuotes($_POST);
00049 $_COOKIE = mageUndoMagicQuotes($_COOKIE);
00050 $_REQUEST = mageUndoMagicQuotes($_REQUEST);
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060 function __autoload($class)
00061 {
00062 if (defined('COMPILER_INCLUDE_PATH')) {
00063 $classFile = $class.'.php';
00064 } else {
00065 $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
00066 }
00067
00068 include($classFile);
00069 }
00070
00071
00072
00073
00074
00075
00076 function destruct($object)
00077 {
00078 if (is_array($object)) {
00079 foreach ($object as $obj) {
00080 destruct($obj);
00081 }
00082 }
00083 unset($object);
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093 function __()
00094 {
00095 return Mage::app()->getTranslator()->translate(func_get_args());
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 function uc_words($str, $destSep='_', $srcSep='_')
00109 {
00110 return str_replace(' ', $destSep, ucwords(str_replace($srcSep, ' ', $str)));
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 function now($dayOnly=false)
00120 {
00121 return date($dayOnly ? 'Y-m-d' : 'Y-m-d H:i:s');
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 function is_empty_date($date)
00131 {
00132 return preg_replace('#[ 0:-]#', '', $date)==='';
00133 }
00134
00135 function mageFindClassFile($class)
00136 {
00137 if (defined('COMPILER_INCLUDE_PATH')) {
00138 $classFile = $class.'.php';
00139 } else {
00140 $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
00141 }
00142 $found = false;
00143 foreach (explode(PS, get_include_path()) as $path) {
00144 $fileName = $path.DS.$classFile;
00145 if (file_exists($fileName)) {
00146 $found = $fileName;
00147 break;
00148 }
00149 }
00150 return $found;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 function mageCoreErrorHandler($errno, $errstr, $errfile, $errline){
00162 if (strpos($errstr, 'DateTimeZone::__construct')!==false) {
00163
00164 return false;
00165 }
00166
00167 $errno = $errno & error_reporting();
00168 if ($errno == 0) {
00169 return false;
00170 }
00171 if (!defined('E_STRICT')) {
00172 define('E_STRICT', 2048);
00173 }
00174 if (!defined('E_RECOVERABLE_ERROR')) {
00175 define('E_RECOVERABLE_ERROR', 4096);
00176 }
00177
00178
00179 if (stripos($errfile.$errstr, 'pear') !== false) {
00180
00181 if ($errno == E_STRICT) {
00182 return false;
00183 }
00184
00185 if ($errno == E_WARNING && stripos($errstr, 'open_basedir') !== false) {
00186 return false;
00187 }
00188 }
00189
00190 $errorMessage = '';
00191
00192 switch($errno){
00193 case E_ERROR:
00194 $errorMessage .= "Error";
00195 break;
00196 case E_WARNING:
00197 $errorMessage .= "Warning";
00198 break;
00199 case E_PARSE:
00200 $errorMessage .= "Parse Error";
00201 break;
00202 case E_NOTICE:
00203 $errorMessage .= "Notice";
00204 break;
00205 case E_CORE_ERROR:
00206 $errorMessage .= "Core Error";
00207 break;
00208 case E_CORE_WARNING:
00209 $errorMessage .= "Core Warning";
00210 break;
00211 case E_COMPILE_ERROR:
00212 $errorMessage .= "Compile Error";
00213 break;
00214 case E_COMPILE_WARNING:
00215 $errorMessage .= "Compile Warning";
00216 break;
00217 case E_USER_ERROR:
00218 $errorMessage .= "User Error";
00219 break;
00220 case E_USER_WARNING:
00221 $errorMessage .= "User Warning";
00222 break;
00223 case E_USER_NOTICE:
00224 $errorMessage .= "User Notice";
00225 break;
00226 case E_STRICT:
00227 $errorMessage .= "Strict Notice";
00228 break;
00229 case E_RECOVERABLE_ERROR:
00230 $errorMessage .= "Recoverable Error";
00231 break;
00232 default:
00233 $errorMessage .= "Unknown error ($errno)";
00234 break;
00235 }
00236
00237 $errorMessage .= ": {$errstr} in {$errfile} on line {$errline}";
00238
00239 throw new Exception($errorMessage);
00240 }
00241
00242 function mageDebugBacktrace($return=false, $html=true, $showFirst=false)
00243 {
00244 $d = debug_backtrace();
00245 $out = '';
00246 if ($html) $out .= "<pre>";
00247 foreach ($d as $i=>$r) {
00248 if (!$showFirst && $i==0) {
00249 continue;
00250 }
00251
00252 @$out .= "[$i] {$r['file']}:{$r['line']}\n";
00253 }
00254 if ($html) $out .= "</pre>";
00255 if ($return) {
00256 return $out;
00257 } else {
00258 echo $out;
00259 }
00260 }
00261
00262 function mageSendErrorHeader()
00263 {
00264 return;
00265 if (!isset($_SERVER['SCRIPT_NAME'])) {
00266 return;
00267 }
00268 $action = Mage::app()->getRequest()->getBasePath()."bugreport.php";
00269 echo '<form id="error_report" method="post" style="display:none" action="'.$action.'"><textarea name="error">';
00270 }
00271
00272 function mageSendErrorFooter()
00273 {
00274 return;
00275 if (!isset($_SERVER['SCRIPT_NAME'])) {
00276 return;
00277 }
00278 echo '</textarea></form><script type="text/javascript">document.getElementById("error_report").submit()</script>';
00279 exit;
00280 }
00281
00282 function mageDelTree($path) {
00283 if (is_dir($path)) {
00284 $entries = scandir($path);
00285 foreach ($entries as $entry) {
00286 if ($entry != '.' && $entry != '..') {
00287 mageDelTree($path.DS.$entry);
00288 }
00289 }
00290 @rmdir($path);
00291 } else {
00292 @unlink($path);
00293 }
00294 }
00295
00296 function mageParseCsv($string, $delimiter=",", $enclosure='"', $escape='\\')
00297 {
00298 $elements = explode($delimiter, $string);
00299 for ($i = 0; $i < count($elements); $i++) {
00300 $nquotes = substr_count($elements[$i], $enclosure);
00301 if ($nquotes %2 == 1) {
00302 for ($j = $i+1; $j < count($elements); $j++) {
00303 if (substr_count($elements[$j], $enclosure) > 0) {
00304
00305 array_splice($elements, $i, $j-$i+1,
00306 implode($delimiter, array_slice($elements, $i, $j-$i+1)));
00307 break;
00308 }
00309 }
00310 }
00311 if ($nquotes > 0) {
00312
00313 $qstr =& $elements[$i];
00314 $qstr = substr_replace($qstr, '', strpos($qstr, $enclosure), 1);
00315 $qstr = substr_replace($qstr, '', strrpos($qstr, $enclosure), 1);
00316 $qstr = str_replace($enclosure.$enclosure, $enclosure, $qstr);
00317 }
00318 }
00319 return $elements;
00320 }
00321
00322 function is_dir_writeable($dir)
00323 {
00324 if (is_dir($dir) && is_writable($dir)) {
00325 if (stripos(PHP_OS, 'win') === 0) {
00326 $dir = ltrim($dir, DIRECTORY_SEPARATOR);
00327 $file = $dir . DIRECTORY_SEPARATOR . uniqid(mt_rand()).'.tmp';
00328 $exist = file_exists($file);
00329 $fp = @fopen($file, 'a');
00330 if ($fp === false) {
00331 return false;
00332 }
00333 fclose($fp);
00334 if (!$exist) {
00335 unlink($file);
00336 }
00337 }
00338 return true;
00339 }
00340 return false;
00341 }
00342
00343 if ( !function_exists('sys_get_temp_dir') ) {
00344
00345
00346 function sys_get_temp_dir()
00347 {
00348
00349 if ( !empty($_ENV['TMP']) ) {
00350 return realpath( $_ENV['TMP'] );
00351 } else if ( !empty($_ENV['TMPDIR']) ) {
00352 return realpath( $_ENV['TMPDIR'] );
00353 } else if ( !empty($_ENV['TEMP']) ) {
00354 return realpath( $_ENV['TEMP'] );
00355 } else {
00356
00357
00358 $temp_file = tempnam( md5(uniqid(rand(), TRUE)), '' );
00359 if ( $temp_file ) {
00360 $temp_dir = realpath( dirname($temp_file) );
00361 unlink( $temp_file );
00362 return $temp_dir;
00363 } else {
00364 return FALSE;
00365 }
00366 }
00367 }
00368 }