00001 <?php
00010 class utils {
00011
00020 public static function cutArray(array $arr, $nb, $presKey = true) {
00021 return array(
00022 array_slice($arr, 0, $nb, $presKey),
00023 array_slice($arr, $nb, count($arr), $presKey)
00024 );
00025 }
00026
00033 public static function html2Text($html) {
00034 lib::load('html2text');
00035 return html2text($html);
00036 }
00037
00046 public static function htmlTag($tag, array $attributes, $content = null) {
00047 $ret = '<'.$tag.' '.self::htmlAttribute($attributes);
00048 if (!is_null($content))
00049 $ret.= '>'.$content.'</'.$tag.'>';
00050 else
00051 $ret.= ' />';
00052 return $ret;
00053 }
00054
00063 public static function mailTo($email, $name = null, array $attributes = array()) {
00064 $emailObfs = self::htmlObfuscate($email);
00065 if (is_null($name))
00066 $name = $emailObfs;
00067 $emailObfs = self::htmlObfuscate('mailto:').$emailObfs;
00068 return self::htmlTag('a', array_merge(array('href'=>$emailObfs), $attributes), $name);
00069 }
00070
00077 public static function htmlObfuscate($text) {
00078 $ret = null;
00079 for ($i=0; $i<strlen($text); $i++)
00080 $ret.= '&#'.ord($text[$i]).';';
00081 return $ret;
00082 }
00083
00090 public static function htmlAttribute(array $prm) {
00091 $tmp = array();
00092 foreach($prm as $k=>$v)
00093 if (!empty($v) || $v === 0 || $v === '0')
00094 $tmp[] = $k.'="'.$v.'"';
00095 return implode(' ', $tmp);
00096 }
00097
00105 public static function htmlOut($val, $key = false) {
00106 if (is_array($val)) {
00107 if ($key) {
00108 $tmp = $val;
00109 $val = array();
00110 foreach($tmp as $k=>$t)
00111 $val[self::htmlOut($k)] = self::htmlOut($t);
00112 } else
00113 array_walk_recursive($val, create_function('&$v', '$v = utils::htmlOut($v);'));
00114 } else {
00115 $tmp = self::htmlChars();
00116 $val = str_replace(array_keys($tmp), $tmp, $val);
00117 }
00118 return $val;
00119 }
00120
00128 public static function htmlDeOut($val, $key = false) {
00129 if (is_null($val))
00130 return $val;
00131 if (is_array($val)) {
00132 if ($key) {
00133 $tmp = $val;
00134 $val = array();
00135 foreach($tmp as $k=>$t)
00136 $val[self::htmlDeOut($k)] = self::htmlDeOut($t);
00137 } else
00138 array_walk_recursive($val, create_function('&$v', '$v = utils::htmlDeOut($v);'));
00139 } else {
00140 $tmp = self::htmlChars();
00141 $val = str_replace($tmp, array_keys($tmp), $val);
00142 }
00143 return $val;
00144 }
00145
00151 private static $htmlChars = null;
00152
00158 public static function htmlChars() {
00159 if (is_null(self::$htmlChars)) {
00160 $tmp = array();
00161 foreach(get_html_translation_table(HTML_ENTITIES) as $k=>$v) {
00162 $tmp[utf8_encode($k)]= utf8_encode($v);
00163 }
00164 unset($tmp['&']);
00165 self::$htmlChars = array_merge(array(
00166 '&'=>'&',
00167 'Œ'=>'Œ',
00168 'œ'=>'œ',
00169 'Š'=>'Š',
00170 'š'=>'š',
00171 'Ÿ'=>'Ÿ',
00172 '^'=>'ˆ',
00173 '˜'=>'˜',
00174 '–'=>'–',
00175 '—'=>'—',
00176 '‘'=>'‘',
00177 '’'=>'’',
00178 '‚'=>'‚',
00179 '“'=>'“',
00180 '”'=>'”',
00181 '„'=>'„',
00182 '†'=>'†',
00183 '‡'=>'‡',
00184 '‰'=>'‰',
00185 '‹'=>'‹',
00186 '›'=>'›',
00187 '€'=>'€',
00188 ), $tmp);
00189 }
00190 return self::$htmlChars;
00191 }
00192
00199 public static function htmlIn($val) {
00200 return $val;
00201 if (is_array($val))
00202 array_walk_recursive($val, create_function('&$v', '$v = utf8_decode($v);'));
00203 else
00204 $val = $val;
00205 return $val;
00206 }
00207
00214 public static function getModuleName($className) {
00215 $tmp = explode('_', $className);
00216 array_shift($tmp);
00217 array_pop($tmp);
00218 return implode('_', $tmp);
00219 }
00220
00227 public static function initTabNumPair(array &$vars, $finalName = 'final') {
00228 $ret = array();
00229 if (!empty($vars)) {
00230 for($i = 1; $i<count($vars); $i+=2) {
00231 $ret[$vars[$i-1]] = $vars[$i];
00232 }
00233 if ($i == count($vars))
00234 $ret[$finalName] = $vars[$i-1];
00235 }
00236 return $ret;
00237 }
00238
00250 public static function getIcon(array $prm) {
00251 $ret = null;
00252
00253 static $cfg;
00254 if (!$cfg)
00255 $cfg = factory::loadCfg('icons', false);
00256
00257 if (config::initTab($prm, array(
00258 'name'=>null,
00259 'type'=>$cfg['default'],
00260 'imgTag'=>true,
00261 'alt'=>'',
00262 'attr'=>array(),
00263 ))) {
00264 if (array_key_exists($prm['type'], $cfg['icons'])
00265 && is_array($cfg['icons'][$prm['type']])
00266 && in_array($prm['name'], $cfg['icons'][$prm['type']])) {
00267 $ret = request::get('path').$cfg['dir'].'/'.$prm['type'].request::getCfg('sepParam').$prm['name'].$cfg['ext'];
00268 } else if ($prm['type'] != $cfg['default']) {
00269 $ret = self::getIcon(array('name'=>$prm['name'], 'imgTag'=>false));
00270 }
00271
00272 if ($ret && $prm['imgTag']) {
00273 $alt = $prm['alt']? $prm['alt'] : ucFirst($prm['name']);
00274 $ret = self::htmlTag('img', array_merge(array(
00275 'src'=>$ret,
00276 'alt'=>$alt
00277 ), $prm['attr']));
00278 }
00279 }
00280 return $ret;
00281 }
00282
00287 protected static $date;
00288
00298 public static function formatDate($date, $type = 'date', $len = 'short2', $htmlOut = true) {
00299 if (is_null(self::$date)) {
00300 self::$date = factory::getHelper('date');
00301 }
00302 self::$date->getCfg()->setA(array(
00303 'defaultFormat'=>array(
00304 'type'=>$type,
00305 'len'=>$type == 'datetime' && $len == 'short2' ? 'short' : $len
00306 ),
00307 'htmlOut'=>$htmlOut
00308 ));
00309 self::$date->set($date, is_int($date) ? 'timestamp' : 'date');
00310 return self::$date->format();
00311 }
00312
00319 public static function img($prm, $absolute = false) {
00320 if (!is_array($prm))
00321 $prm = array('src'=>$prm, 'size'=>true);
00322 $alt = $prm['src'];
00323 if (isset($prm['size'])) {
00324 unset($prm['size']);
00325 if (!isset($prm['width']) && !isset($prm['height'])) {
00326 $size = getimagesize(WEBROOT.'img/'.$prm['src']);
00327 if (!isset($prm['width']))
00328 $prm['width'] = $size[0];
00329 if (!isset($prm['height']))
00330 $prm['height'] = $size[1];
00331 }
00332 }
00333 $prm['src'] = request::get('path').'img/'.$prm['src'];
00334 if ($absolute)
00335 $prm['src'] = request::get('domain').$prm['src'];
00336 return self::htmlTag('img', array_merge(array(
00337 'alt'=>$alt
00338 ), $prm));
00339 }
00340
00347 public static function render(array $prm) {
00348 return factory::get('tpl', $prm)->render($prm);
00349 }
00350
00358 public static function getValInArray(array $source, array $keys) {
00359 $ret = null;
00360 if (array_key_exists($keys[0], $source)) {
00361 if (is_array($source[$keys[0]]) && count($keys) > 1)
00362 $ret = self::getValInArray($source[$keys[0]], array_slice($keys, 1));
00363 else
00364 $ret = $source[$keys[0]];
00365 }
00366 return $ret;
00367 }
00368
00376 public static function urlify($text, $ignore = null) {
00377 $text = str_replace(
00378 array('ß' , 'æ', 'Æ', 'Œ', 'œ', '¼', '½', '¾', '‰', '™'),
00379 array('ss', 'ae', 'AE', 'OE', 'oe', '1/4', '1/2', '3/4', '0/00', 'TM'),
00380 $text);
00381 $from = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøðÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüŠšÝŸÿÑñÐÞþ()[]~¤$&%*@ç§¶!¡†‡?¿;,.:/\\^¨€¢£¥{}|¦+÷×±<>«»“”„\"‘’' ˜–—…©®¹²³°";
00382 $to = 'AAAAAAaaaaaaOOOOOOoooooooEEEEeeeeCcIIIIiiiiUUUUuuuuSsYYyNnDPp cS -- EcPY __________------CR123-';
00383 if (!is_null($ignore)) {
00384 $len = strlen($ignore);
00385 for($i = 0; $i < $len; $i++) {
00386 $pos = strpos($from, $ignore{$i});
00387 if ($pos !== false) {
00388 $from = substr($from, 0, $pos).substr($from, $pos+1);
00389 $to = substr($to, 0, $pos).substr($to, $pos+1);
00390 }
00391 }
00392 }
00393 return trim(str_replace(
00394 array(' ', '-----', '----', '---', '--'),
00395 URLSEPARATOR,
00396 strtr(utf8_decode($text), utf8_decode($from), utf8_decode($to))), URLSEPARATOR);
00397 }
00398
00405 public static function jsEncode($vars) {
00406 $func = array();
00407 if (is_array($vars)) {
00408 foreach($vars as $k=>$v) {
00409 if (is_string($v) && strpos($v, 'function(') === 0) {
00410 $func['"'.$k.'Func"'] = $v;
00411 $vars[$k] = $k.'Func';
00412 }
00413 }
00414 }
00415
00416 $encoded = json_encode($vars);
00417 if (!empty($func))
00418 $encoded = str_replace(array_keys($func), $func, $encoded);
00419
00420
00421 return $encoded;
00422 }
00423
00431 public static function randomStr($len = 10, $ignore = null) {
00432 $source = 'abcdefghikjlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
00433 if (!is_null($ignore)) {
00434 $tmp = array();
00435 for($i=0;$i<strlen($ignore);$i++)
00436 $tmp[] = $ignore[$i];
00437 $source = str_replace($tmp, '', $source);
00438 }
00439 $len = abs(intval($len));
00440 $n = strlen($source)-1;
00441 $r = '';
00442 for($i = 0; $i < $len; $i++)
00443 $r.= $source{rand(0, $n)};
00444 return $r;
00445 }
00446
00454 public static function isContained(array $url, array $checks) {
00455 foreach($checks as $c) {
00456 $tmp = array_intersect_key($url, $c);
00457 $nbM = 0;
00458 foreach($tmp as $k=>$v)
00459 if (!is_array($v))
00460 $nbM += (preg_match('/'.$c[$k].'/', $v)? 1 : 0);
00461
00462 if ($nbM == count($tmp))
00463 return true;
00464 }
00465 return false;
00466 }
00467
00468 }