nyroFwk  0.2
utils.class.php
Go to the documentation of this file.
1 <?php
10 class utils {
11 
20  public static function cutArray(array $arr, $nb, $presKey = true) {
21  return array(
22  array_slice($arr, 0, $nb, $presKey),
23  array_slice($arr, $nb, count($arr), $presKey)
24  );
25  }
26 
33  public static function html2Text($html) {
34  lib::load('html2text');
35  return html2text($html);
36  }
37 
46  public static function htmlTag($tag, array $attributes, $content = null) {
47  $ret = '<'.$tag.' '.self::htmlAttribute($attributes);
48  if (!is_null($content))
49  $ret.= '>'.$content.'</'.$tag.'>';
50  else
51  $ret.= ' />';
52  return $ret;
53  }
54 
63  public static function mailTo($email, $name = null, array $attributes = array()) {
64  $emailObfs = self::htmlObfuscate($email);
65  if (is_null($name))
66  $name = $emailObfs;
67  $emailObfs = self::htmlObfuscate('mailto:').$emailObfs;
68  return self::htmlTag('a', array_merge(array('href'=>$emailObfs), $attributes), $name);
69  }
70 
77  public static function htmlObfuscate($text) {
78  $ret = null;
79  for ($i=0; $i<strlen($text); $i++)
80  $ret.= '&#'.ord($text[$i]).';';
81  return $ret;
82  }
83 
90  public static function htmlAttribute(array $prm) {
91  $tmp = array();
92  foreach($prm as $k=>$v)
93  if (!empty($v) || $v === 0 || $v === '0')
94  $tmp[] = $k.'="'.$v.'"';
95  return implode(' ', $tmp);
96  }
97 
105  public static function htmlOut($val, $key = false) {
106  if (is_array($val)) {
107  if ($key) {
108  $tmp = $val;
109  $val = array();
110  foreach($tmp as $k=>$t)
111  $val[self::htmlOut($k)] = self::htmlOut($t);
112  } else
113  array_walk_recursive($val, create_function('&$v', '$v = utils::htmlOut($v);'));
114  } else {
115  $tmp = self::htmlChars();
116  $val = str_replace(array_keys($tmp), $tmp, $val);
117  }
118  return $val;
119  }
120 
128  public static function htmlDeOut($val, $key = false) {
129  if (is_null($val))
130  return $val;
131  if (is_array($val)) {
132  if ($key) {
133  $tmp = $val;
134  $val = array();
135  foreach($tmp as $k=>$t)
136  $val[self::htmlDeOut($k)] = self::htmlDeOut($t);
137  } else
138  array_walk_recursive($val, create_function('&$v', '$v = utils::htmlDeOut($v);'));
139  } else {
140  $tmp = self::htmlChars();
141  $val = str_replace($tmp, array_keys($tmp), $val);
142  }
143  return $val;
144  }
145 
151  private static $htmlChars = null;
152 
158  public static function htmlChars() {
159  if (is_null(self::$htmlChars)) {
160  $tmp = array();
161  foreach(get_html_translation_table(HTML_ENTITIES) as $k=>$v) {
162  $tmp[utf8_encode($k)]= utf8_encode($v);
163  }
164  unset($tmp['&']); // Unset here to place it at the very top of the array
165  self::$htmlChars = array_merge(array(
166  '&'=>'&amp;',
167  'Œ'=>'&OElig;',
168  'œ'=>'&oelig;',
169  'Š'=>'&Scaron;',
170  'š'=>'&scaron;',
171  'Ÿ'=>'&Yuml;',
172  '^'=>'&circ;',
173  '˜'=>'&tilde;',
174  '–'=>'&ndash;',
175  '—'=>'&mdash;',
176  '‘'=>'&lsquo;',
177  '’'=>'&rsquo;',
178  '‚'=>'&sbquo;',
179  '“'=>'&ldquo;',
180  '”'=>'&rdquo;',
181  '„'=>'&bdquo;',
182  '†'=>'&dagger;',
183  '‡'=>'&Dagger;',
184  '‰'=>'&permil;',
185  '‹'=>'&lsaquo;',
186  '›'=>'&rsaquo;',
187  '€'=>'&euro;',
188  ), $tmp);
189  }
190  return self::$htmlChars;
191  }
192 
199  public static function htmlIn($val) {
200  return $val;
201  if (is_array($val))
202  array_walk_recursive($val, create_function('&$v', '$v = utf8_decode($v);'));
203  else
204  $val = $val;
205  return $val;
206  }
207 
214  public static function getModuleName($className) {
215  $tmp = explode('_', $className);
216  array_shift($tmp);
217  array_pop($tmp);
218  return implode('_', $tmp);
219  }
220 
227  public static function initTabNumPair(array &$vars, $finalName = 'final') {
228  $ret = array();
229  if (!empty($vars)) {
230  for($i = 1; $i<count($vars); $i+=2) {
231  $ret[$vars[$i-1]] = $vars[$i];
232  }
233  if ($i == count($vars))
234  $ret[$finalName] = $vars[$i-1];
235  }
236  return $ret;
237  }
238 
250  public static function getIcon(array $prm) {
251  $ret = null;
252 
253  static $cfg;
254  if (!$cfg)
255  $cfg = factory::loadCfg('icons', false);
256 
257  if (config::initTab($prm, array(
258  'name'=>null,
259  'type'=>$cfg['default'],
260  'imgTag'=>true,
261  'alt'=>'',
262  'attr'=>array(),
263  ))) {
264  if (array_key_exists($prm['type'], $cfg['icons'])
265  && is_array($cfg['icons'][$prm['type']])
266  && in_array($prm['name'], $cfg['icons'][$prm['type']])) {
267  $ret = request::get('path').$cfg['dir'].'/'.$prm['type'].request::getCfg('sepParam').$prm['name'].$cfg['ext'];
268  } else if ($prm['type'] != $cfg['default']) {
269  $ret = self::getIcon(array('name'=>$prm['name'], 'imgTag'=>false));
270  }
271 
272  if ($ret && $prm['imgTag']) {
273  $alt = $prm['alt']? $prm['alt'] : ucFirst($prm['name']);
274  $ret = self::htmlTag('img', array_merge(array(
275  'src'=>$ret,
276  'alt'=>$alt
277  ), $prm['attr']));
278  }
279  }
280  return $ret;
281  }
282 
287  protected static $date;
288 
298  public static function formatDate($date, $type = 'date', $len = 'short2', $htmlOut = true) {
299  if (is_null(self::$date)) {
300  self::$date = factory::getHelper('date');
301  }
302  self::$date->set($date, is_int($date) ? 'timestamp' : 'date');
303  if (is_bool($type)) {
304  self::$date->getCfg()->setA(array(
305  'htmlOut'=>$htmlOut
306  ));
307  return self::$date->formatDirect($len);
308  } else {
309  self::$date->getCfg()->setA(array(
310  'defaultFormat'=>array(
311  'type'=>$type,
312  'len'=>$type == 'datetime' && $len == 'short2' ? 'short' : $len
313  ),
314  'htmlOut'=>$htmlOut
315  ));
316  return self::$date->format();
317  }
318  }
319 
326  public static function img($prm, $absolute = false) {
327  if (!is_array($prm))
328  $prm = array('src'=>$prm, 'size'=>true);
329  $alt = $prm['src'];
330  if (isset($prm['size'])) {
331  unset($prm['size']);
332  if (!isset($prm['width']) && !isset($prm['height'])) {
333  $size = getimagesize(WEBROOT.'img/'.$prm['src']);
334  if (!isset($prm['width']))
335  $prm['width'] = $size[0];
336  if (!isset($prm['height']))
337  $prm['height'] = $size[1];
338  }
339  }
340  $prm['src'] = request::get('path').'img/'.$prm['src'];
341  if ($absolute)
342  $prm['src'] = request::get('domain').$prm['src'];
343  return self::htmlTag('img', array_merge(array(
344  'alt'=>$alt
345  ), $prm));
346  }
347 
354  public static function render(array $prm) {
355  return factory::get('tpl', $prm)->render($prm);
356  }
357 
365  public static function getValInArray(array $source, array $keys) {
366  $ret = null;
367  if (array_key_exists($keys[0], $source)) {
368  if (is_array($source[$keys[0]]) && count($keys) > 1)
369  $ret = self::getValInArray($source[$keys[0]], array_slice($keys, 1));
370  else
371  $ret = $source[$keys[0]];
372  }
373  return $ret;
374  }
375 
383  public static function urlify($text, $ignore = null) {
384  $text = str_replace(
385  array('ß' , 'æ', 'Æ', 'Œ', 'œ', '¼', '½', '¾', '‰', '™'),
386  array('ss', 'ae', 'AE', 'OE', 'oe', '1/4', '1/2', '3/4', '0/00', 'TM'),
387  $text);
388  $from = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøðÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüŠšÝŸÿÑñÐÞþ()[]~¤$&%*@秶!¡†‡?¿;,.#:/\\^¨€¢£¥{}|¦+÷×±<>«»“”„\"‘’' ˜–—…©®¹²³°";
389  $to = 'AAAAAAaaaaaaOOOOOOoooooooEEEEeeeeCcIIIIiiiiUUUUuuuuSsYYyNnDPp cS --- EcPY __________------CR123-';
390  if (!is_null($ignore)) {
391  $len = strlen($ignore);
392  for($i = 0; $i < $len; $i++) {
393  $pos = strpos($from, $ignore{$i});
394  if ($pos !== false) {
395  $from = substr($from, 0, $pos).substr($from, $pos+1);
396  $to = substr($to, 0, $pos).substr($to, $pos+1);
397  }
398  }
399  }
400  $ret = trim(str_replace(
401  array(' ', '-----', '----', '---', '--'),
402  URLSEPARATOR,
403  strtr(utf8_decode($text), utf8_decode($from), utf8_decode($to))), URLSEPARATOR);
404 
405  return URLLOWER ? mb_strtolower($ret) : $ret;
406  }
407 
414  public static function jsEncode($vars) {
415  $func = array();
416  if (is_array($vars))
417  $func = self::jsEncodeSearchFunc($vars);
418 
419  $encoded = json_encode($vars);
420  if (!empty($func))
421  $encoded = str_replace(array_keys($func), $func, $encoded);
422 
423  return $encoded;
424  }
425 
433  protected static function jsEncodeSearchFunc(array &$vars, &$startFunc = 1) {
434  $func = array();
435  foreach($vars as $k=>$v) {
436  if (is_string($v) && strpos($v, 'function(') === 0) {
437  $func['"'.$startFunc.'Func"'] = $v;
438  $vars[$k] = $startFunc.'Func';
439  $startFunc++;
440  } else if (is_array($v)) {
441  $func = array_merge($func, self::jsEncodeSearchFunc($v, $startFunc));
442  $vars[$k] = $v;
443  }
444  }
445  return $func;
446  }
447 
455  public static function randomStr($len = 10, $ignore = null) {
456  $source = 'abcdefghikjlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
457  if (!is_null($ignore)) {
458  $tmp = array();
459  for($i=0;$i<strlen($ignore);$i++)
460  $tmp[] = $ignore[$i];
461  $source = str_replace($tmp, '', $source);
462  }
463  $len = abs(intval($len));
464  $n = strlen($source)-1;
465  $r = '';
466  for($i = 0; $i < $len; $i++)
467  $r.= $source{rand(0, $n)};
468  return $r;
469  }
470 
478  public static function isContained(array $url, array $checks) {
479  foreach($checks as $c) {
480  $tmp = array_intersect_key($url, $c);
481  $nbM = 0;
482  foreach($tmp as $k=>$v)
483  if (!is_array($v))
484  $nbM += (preg_match('/'.$c[$k].'/', $v)? 1 : 0);
485 
486  if ($nbM == count($tmp))
487  return true;
488  }
489  return false;
490  }
491 
492 }
static $date
static get($get=null)
static htmlOut($val, $key=false)
static htmlDeOut($val, $key=false)
static img($prm, $absolute=false)
static initTabNumPair(array &$vars, $finalName='final')
static urlify($text, $ignore=null)
static htmlChars()
static htmlTag($tag, array $attributes, $content=null)
Definition: utils.class.php:46
static formatDate($date, $type='date', $len='short2', $htmlOut=true)
static randomStr($len=10, $ignore=null)
static jsEncode($vars)
static htmlIn($val)
static getHelper($className, array $cfg=array())
static getIcon(array $prm)
static jsEncodeSearchFunc(array &$vars, &$startFunc=1)
static initTab(array &$vars, array $init)
static html2Text($html)
Definition: utils.class.php:33
static htmlAttribute(array $prm)
Definition: utils.class.php:90
static mailTo($email, $name=null, array $attributes=array())
Definition: utils.class.php:63
static cutArray(array $arr, $nb, $presKey=true)
Definition: utils.class.php:20
static isContained(array $url, array $checks)
static $htmlChars
static getModuleName($className)
static loadCfg($className, $searchParent=true)
static render(array $prm)
static load($name)
Definition: lib.class.php:51
static getValInArray(array $source, array $keys)
static htmlObfuscate($text)
Definition: utils.class.php:77
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13