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 class Mage_Core_Helper_Data extends Mage_Core_Helper_Abstract
00033 {
00034
00035
00036
00037 protected $_encryptor = null;
00038
00039
00040
00041
00042 public function getEncryptor()
00043 {
00044 if ($this->_encryptor === null) {
00045 $encryptionModel = (string)Mage::getConfig()->getNode('global/helpers/core/encryption_model');
00046 if ($encryptionModel) {
00047 $this->_encryptor = new $encryptionModel;
00048 } else {
00049 $this->_encryptor = Mage::getModel('core/encryption');
00050 }
00051
00052 $this->_encryptor->setHelper($this);
00053 }
00054 return $this->_encryptor;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 public static function currency($value, $format=true, $includeContainer = true)
00066 {
00067 try {
00068 $value = Mage::app()->getStore()->convertPrice($value, $format, $includeContainer);
00069 }
00070 catch (Exception $e){
00071 $value = $e->getMessage();
00072 }
00073 return $value;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083 public function formatCurrency($value, $includeContainer = true)
00084 {
00085 return $this->currency($value, true, $includeContainer);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095 public function formatPrice($price, $includeContainer = true)
00096 {
00097 return Mage::app()->getStore()->formatPrice($price, $includeContainer);
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 public function formatDate($date=null, $format='short', $showTime=false)
00109 {
00110 if (Mage_Core_Model_Locale::FORMAT_TYPE_FULL !==$format &&
00111 Mage_Core_Model_Locale::FORMAT_TYPE_LONG !==$format &&
00112 Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM !==$format &&
00113 Mage_Core_Model_Locale::FORMAT_TYPE_SHORT !==$format) {
00114 return $date;
00115 }
00116 if (!($date instanceof Zend_Date) && $date && !strtotime($date)) {
00117 return '';
00118 }
00119 if (is_null($date)) {
00120 $date = Mage::app()->getLocale()->date(Mage::getSingleton('core/date')->gmtTimestamp(), null, null);
00121 }
00122 elseif (!$date instanceof Zend_Date) {
00123 $date = Mage::app()->getLocale()->date(strtotime($date), null, null, $showTime);
00124 }
00125
00126 if ($showTime) {
00127 $format = Mage::app()->getLocale()->getDateTimeFormat($format);
00128 }
00129 else {
00130 $format = Mage::app()->getLocale()->getDateFormat($format);
00131 }
00132
00133 return $date->toString($format);
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 public function formatTime($time=null, $format='short', $showDate=false)
00145 {
00146 if (Mage_Core_Model_Locale::FORMAT_TYPE_FULL !==$format &&
00147 Mage_Core_Model_Locale::FORMAT_TYPE_LONG !==$format &&
00148 Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM !==$format &&
00149 Mage_Core_Model_Locale::FORMAT_TYPE_SHORT !==$format) {
00150 return $time;
00151 }
00152
00153 if (is_null($time)) {
00154 $date = Mage::app()->getLocale()->date(time());
00155 }
00156 elseif ($time instanceof Zend_Date) {
00157 $date = $time;
00158 }
00159 else {
00160 $date = Mage::app()->getLocale()->date(strtotime($time));
00161 }
00162
00163 if ($showDate) {
00164 $format = Mage::app()->getLocale()->getDateTimeFormat($format);
00165 }
00166 else {
00167 $format = Mage::app()->getLocale()->getTimeFormat($format);
00168 }
00169
00170 return $date->toString($format);
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 public function encrypt($data)
00180 {
00181 if (!Mage::isInstalled()) {
00182 return $data;
00183 }
00184 return $this->getEncryptor()->encrypt($data);
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 public function decrypt($data)
00194 {
00195 if (!Mage::isInstalled()) {
00196 return $data;
00197 }
00198 return $this->getEncryptor()->decrypt($data);
00199 }
00200
00201 public function validateKey($key)
00202 {
00203 return $this->getEncryptor()->validateKey($key);
00204 }
00205
00206 public function getRandomString($len, $chars=null)
00207 {
00208 if (is_null($chars)) {
00209 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00210 }
00211 mt_srand(10000000*(double)microtime());
00212 for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
00213 $str .= $chars[mt_rand(0, $lc)];
00214 }
00215 return $str;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 public function getHash($password, $salt = false)
00225 {
00226 return $this->getEncryptor()->getHash($password, $salt);
00227 }
00228
00229 public function validateHash($password, $hash)
00230 {
00231 return $this->getEncryptor()->validateHash($password, $hash);
00232 }
00233
00234
00235
00236
00237
00238
00239
00240 public function getStoreId($store=null)
00241 {
00242 return Mage::app()->getStore($store)->getId();
00243 }
00244
00245 public function removeAccents($string, $german=false)
00246 {
00247 static $replacements;
00248
00249 if (empty($replacements[$german])) {
00250 $subst = array(
00251
00252 192=>'A', 193=>'A', 194=>'A', 195=>'A', 196=>'A', 197=>'A', 199=>'C', 208=>'D', 200=>'E', 201=>'E', 202=>'E', 203=>'E', 204=>'I', 205=>'I', 206=>'I', 207=>'I', 209=>'N', 210=>'O', 211=>'O', 212=>'O', 213=>'O', 214=>'O', 216=>'O', 138=>'S', 217=>'U', 218=>'U', 219=>'U', 220=>'U', 221=>'Y', 142=>'Z', 224=>'a', 225=>'a', 226=>'a', 227=>'a', 228=>'a', 229=>'a', 231=>'c', 232=>'e', 233=>'e', 234=>'e', 235=>'e', 236=>'i', 237=>'i', 238=>'i', 239=>'i', 241=>'n', 240=>'o', 242=>'o', 243=>'o', 244=>'o', 245=>'o', 246=>'o', 248=>'o', 154=>'s', 249=>'u', 250=>'u', 251=>'u', 252=>'u', 253=>'y', 255=>'y', 158=>'z',
00253
00254 258=>'A', 260=>'A', 262=>'C', 268=>'C', 270=>'D', 272=>'D', 280=>'E', 282=>'E', 286=>'G', 304=>'I', 313=>'L', 317=>'L', 321=>'L', 323=>'N', 327=>'N', 336=>'O', 340=>'R', 344=>'R', 346=>'S', 350=>'S', 354=>'T', 356=>'T', 366=>'U', 368=>'U', 377=>'Z', 379=>'Z', 259=>'a', 261=>'a', 263=>'c', 269=>'c', 271=>'d', 273=>'d', 281=>'e', 283=>'e', 287=>'g', 305=>'i', 322=>'l', 314=>'l', 318=>'l', 324=>'n', 328=>'n', 337=>'o', 341=>'r', 345=>'r', 347=>'s', 351=>'s', 357=>'t', 355=>'t', 367=>'u', 369=>'u', 378=>'z', 380=>'z',
00255
00256 198=>'Ae', 230=>'ae', 140=>'Oe', 156=>'oe', 223=>'ss',
00257 );
00258
00259 if ($german) {
00260
00261 $subst = array_merge($subst, array(196=>'Ae', 228=>'ae', 214=>'Oe', 246=>'oe', 220=>'Ue', 252=>'ue'));
00262 }
00263
00264 $replacements[$german] = array();
00265 foreach ($subst as $k=>$v) {
00266 $replacements[$german][$k<256 ? chr($k) : '&#'.$k.';'] = $v;
00267 }
00268 }
00269
00270
00271
00272 if ($s = @iconv('UTF-8', 'ISO-8859-1', $string)) {
00273 $string = $s;
00274 }
00275
00276
00277 $string = strtr($string, $replacements[$german]);
00278
00279 return $string;
00280 }
00281
00282 public function isDevAllowed($storeId=null)
00283 {
00284 $allow = true;
00285
00286 $allowedIps = Mage::getStoreConfig('dev/restrict/allow_ips', $storeId);
00287 if (!empty($allowedIps) && isset($_SERVER['REMOTE_ADDR'])) {
00288 $allowedIps = preg_split('#\s*,\s*#', $allowedIps, null, PREG_SPLIT_NO_EMPTY);
00289 if (array_search($_SERVER['REMOTE_ADDR'], $allowedIps)===false
00290 && array_search($_SERVER['HTTP_HOST'], $allowedIps)===false) {
00291 $allow = false;
00292 }
00293 }
00294
00295 return $allow;
00296 }
00297
00298
00299
00300
00301
00302
00303 public function getCacheTypes()
00304 {
00305 $types = array();
00306 $config = Mage::getConfig()->getNode('global/cache/types');
00307 foreach ($config->children() as $type=>$node) {
00308 $types[$type] = (string)$node->label;
00309 }
00310 return $types;
00311 }
00312
00313
00314
00315
00316
00317
00318 public function getCacheBetaTypes()
00319 {
00320 $types = array();
00321 $config = Mage::getConfig()->getNode('global/cache/betatypes');
00322 foreach ($config->children() as $type=>$node) {
00323 $types[$type] = (string)$node->label;
00324 }
00325 return $types;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 public function copyFieldset($fieldset, $aspect, $source, $target, $root='global')
00343 {
00344 if (!(is_array($source) || $source instanceof Varien_Object)
00345 || !(is_array($target) || $target instanceof Varien_Object)) {
00346
00347 return false;
00348 }
00349 $fields = Mage::getConfig()->getFieldset($fieldset, $root);
00350 if (!$fields) {
00351 return false;
00352 }
00353
00354 $sourceIsArray = is_array($source);
00355 $targetIsArray = is_array($target);
00356
00357 $result = false;
00358 foreach ($fields as $code=>$node) {
00359 if (empty($node->$aspect)) {
00360 continue;
00361 }
00362
00363 if ($sourceIsArray) {
00364 $value = isset($source[$code]) ? $source[$code] : null;
00365 } else {
00366 $value = $source->getDataUsingMethod($code);
00367 }
00368
00369 $targetCode = (string)$node->$aspect;
00370 $targetCode = $targetCode == '*' ? $code : $targetCode;
00371
00372 if ($targetIsArray) {
00373 $target[$targetCode] = $value;
00374 } else {
00375 $target->setDataUsingMethod($targetCode, $value);
00376 }
00377
00378 $result = true;
00379 }
00380
00381 return $result;
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 public function decorateArray($array, $prefix = 'decorated_', $forceSetAll = false)
00405 {
00406
00407 if (!(is_array($array) || is_object($array))) {
00408 return $array;
00409 }
00410
00411 $keyIsFirst = "{$prefix}is_first";
00412 $keyIsOdd = "{$prefix}is_odd";
00413 $keyIsEven = "{$prefix}is_even";
00414 $keyIsLast = "{$prefix}is_last";
00415
00416 $count = count($array);
00417 $i = 0;
00418 $isEven = false;
00419 foreach ($array as $key => $element) {
00420 if (is_object($element)) {
00421 $this->_decorateArrayObject($element, $keyIsFirst, (0 === $i), $forceSetAll || (0 === $i));
00422 $this->_decorateArrayObject($element, $keyIsOdd, !$isEven, $forceSetAll || !$isEven);
00423 $this->_decorateArrayObject($element, $keyIsEven, $isEven, $forceSetAll || $isEven);
00424 $isEven = !$isEven;
00425 $i++;
00426 $this->_decorateArrayObject($element, $keyIsLast, ($i === $count), $forceSetAll || ($i === $count));
00427 }
00428 elseif (is_array($element)) {
00429 if ($forceSetAll || (0 === $i)) {
00430 $array[$key][$keyIsFirst] = (0 === $i);
00431 }
00432 if ($forceSetAll || !$isEven) {
00433 $array[$key][$keyIsOdd] = !$isEven;
00434 }
00435 if ($forceSetAll || $isEven) {
00436 $array[$key][$keyIsEven] = $isEven;
00437 }
00438 $isEven = !$isEven;
00439 $i++;
00440 if ($forceSetAll || ($i === $count)) {
00441 $array[$key][$keyIsLast] = ($i === $count);
00442 }
00443 }
00444 }
00445
00446 return $array;
00447 }
00448
00449 private function _decorateArrayObject($element, $key, $value, $dontSkip) {
00450 if ($dontSkip) {
00451 if ($element instanceof Varien_Object) {
00452 $element->setData($key, $value);
00453 }
00454 else {
00455 $element->$key = $value;
00456 }
00457 }
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 public function assocToXml(array $array, $rootName = '_')
00470 {
00471 if (empty($rootName) || is_numeric($rootName)) {
00472 throw new Exception('Root element must not be empty or numeric');
00473 }
00474
00475 $xmlstr = <<<XML
00476 <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
00477 <$rootName></$rootName>
00478 XML;
00479 $xml = new SimpleXMLElement($xmlstr);
00480 foreach ($array as $key => $value) {
00481 if (is_numeric($key)) {
00482 throw new Exception('Array root keys must not be numeric.');
00483 }
00484 }
00485 return self::_assocToXml($array, $rootName, $xml);
00486 }
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 private function _assocToXml(array $array, $rootName, SimpleXMLElement &$xml)
00498 {
00499 $hasNumericKey = false;
00500 $hasStringKey = false;
00501 foreach ($array as $key => $value) {
00502 if (!is_array($value)) {
00503 if (is_string($key)) {
00504 if ($key === $rootName) {
00505 throw new Exception('Associative key must not be the same as its parent associative key.');
00506 }
00507 $hasStringKey = true;
00508 $xml->$key = $value;
00509 }
00510 elseif (is_int($key)) {
00511 $hasNumericKey = true;
00512 $xml->{$rootName}[$key] = $value;
00513 }
00514 }
00515 else {
00516 self::_assocToXml($value, $key, $xml->$key);
00517 }
00518 }
00519 if ($hasNumericKey && $hasStringKey) {
00520 throw new Exception('Associative and numeric keys must not be mixed at one level.');
00521 }
00522 return $xml;
00523 }
00524
00525
00526
00527
00528
00529
00530
00531
00532 public function xmlToAssoc(SimpleXMLElement $xml)
00533 {
00534 $array = array();
00535 foreach ($xml as $key => $value) {
00536 if (isset($value->$key)) {
00537 $i = 0;
00538 foreach ($value->$key as $v) {
00539 $array[$key][$i++] = (string)$v;
00540 }
00541 }
00542 else {
00543
00544 $array[$key] = trim((string)$value);
00545 if (empty($array[$key]) && !empty($value)) {
00546 $array[$key] = self::xmlToAssoc($value);
00547 }
00548
00549 else {
00550 $array[$key] = (string)$value;
00551 }
00552 }
00553 }
00554 return $array;
00555 }
00556 }