-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.php
More file actions
496 lines (447 loc) · 15.6 KB
/
tools.php
File metadata and controls
496 lines (447 loc) · 15.6 KB
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
namespace Slrfw;
/** @todo faire la présentation du code */
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of tools
*
* @author stephane
*/
class Tools {
static function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '', $type = "text/plain") {
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: ' . $type . '; charset="UTF-8"' . "\r\n";
mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=', $message, $header_ . $header);
}
static function cut($text, $nbCharsMax)
{
return mb_strlen($text, 'UTF-8') > $nbCharsMax ? mb_substr($text, 0, $nbCharsMax, 'UTF-8') . '...' : $text;
}
/**
*
*
* @param type $valeur
*
* @return type
*/
static function format_taille($valeur) {
$strTmp = "";
if (preg_match("#^[0-9]{1,}$#", $valeur)) {
if ($valeur >= 1000000) {
// Taille supérieur à 1 MegaOctet
$strTmp = sprintf("%01.2f", $valeur / 1000000);
// Suppression des "0" en fin de chaine
$strTmp = preg_replace("#[\.]{1}[0]{1,}$#", "", $strTmp) . " Mo";
} else if ($valeur >= 1000) {
// Taille inférieur à 1 MegaOctet
$strTmp = sprintf("%01.2f", $valeur / 1000);
// Suppression des "0" en fin de chaine
$strTmp = preg_replace("#[\.]{1}[0]{1,}$#", "", $strTmp) . " Ko";
} else if ($valeur >= 0) {
// Taille inférieur à 1 KiloOctet
$strTmp = $valeur . " octect";
if ($valeur > 0)
$strTmp .= "s";
}
else {
$strTmp = $valeur;
}
} else {
$strTmp = $valeur;
}
return $strTmp;
}
/**
* @deprecated ???
*/
static function RelativeTimeFromDate($date) {
if (substr($date, 0, 10) == "0000-00-00")
return "";
$timestamp = strtotime($date);
return self::RelativeTime($timestamp);
}
/**
* @deprecated ???
*/
static function RelativeTime($timestamp) {
$difference = time() - $timestamp;
$periods = array("seconde", "minute", "heure", "jour", "semaine",
"mois", "année", "décennie");
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
if ($difference > 0) { // this was in the past
$ending = "il y a";
} else { // this was in the future
$difference = -$difference;
$ending = "dans";
}
for ($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if ($difference != 1 && $periods[$j] != "mois")
$periods[$j].= "s";
$text = "$ending $difference $periods[$j]";
return $text;
}
/**
* @deprecated ???
*/
static function random($car) {
$string = "";
$chaine = "abcdefghijklmnpqrstuvwxy0123456789";
srand((double) microtime() * 1000000);
for ($i = 0; $i < $car; $i++) {
$string .= $chaine[rand() % strlen($chaine)];
}
return $string;
}
/**
* @deprecated ???
*/
static function configAssign($arrayConf, $columnName, $columnValue, $columnLang = null, $lang = null) {
$tableConfig = array();
foreach ($arrayConf as $key => $value) {
//SI PAS DE GESTION DE LANGUE
if ($columnLang == null) {
$tableConfig[$value[$columnName]] = $value[$columnValue];
}
//SI GESTION DE LANGUE
else {
// ON RECUPERE LA CONFIG DE LA LANG PASSER EN PARAM
if ($lang != null && ($value[$columnLang] == $lang || $value[$columnLang] == 0))
$tableConfig[$value[$columnName]] = $value[$columnValue];
// ON RECUPERE LA CONFIG POUR TOUTES LES LANGS
if ($lang == null)
$tableConfig[$value[$columnLang]][$value[$columnName]] = $value[$columnValue];
}
}
return $tableConfig;
}
/**
* Recherche dans un array multidim
*
* @param array $parents
* @param array $searched
* @return boolean
*/
static function multidimensional_search($parents, $searched) {
if (empty($searched) || empty($parents)) {
return false;
}
foreach ($parents as $key => $value) {
$exists = true;
foreach ($searched as $skey => $svalue) {
$exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
}
if ($exists) {
return $key;
}
}
return false;
}
static function might_serialize($val) {
if (is_object($val)) {
$obj_keys = array_keys(get_object_vars($val));
foreach ($obj_keys as $k) {
$val->$k = self::might_serialize($val->$k);
}
$val = serialize($val);
}
if (is_array($val)) {
foreach ($val as &$v) {
$v = self::might_serialize($v);
}
$val = serialize($val);
}
return $val;
}
static function might_unserialized($val) {
//$pattern = "/.*\{(.*)\}/";
if (self::is_serialized($val)) {
$ret = unserialize($val);
foreach ($ret as &$r) {
$r = self::might_unserialized($r);
}
return $ret;
} else {
return $val;
}
}
static function is_serialized($val) {
if (!is_string($val)) {
return false;
}
if (trim($val) == "") {
return false;
}
//if (preg_match("/^(i|s|a|o|d)(.*);/si",$val)) { return true; }
if (preg_match("/^(i|s|a|o|d):(.*);/si", $val) !== false) {
return true;
}
return false;
}
/**
* Construit une url propre à partir d'une chaine de caractere,
* @param string $string la chaine de base à transformer.
* @return string La chaine transfomée.
*
* @deprecated ???
*/
static function friendlyURL($string) {
$string = preg_replace("`\[.*\]`U", "", $string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i", "\\1", $string);
$string = preg_replace(array("`[^a-z0-9]`i", "`[-]+`"), "-", $string);
return strtolower(trim($string, '-'));
}
/**
* @deprecated ???
*/
static function trimUltime($chaine) {
$chaine = trim($chaine);
$chaine = str_replace("\t", " ", $chaine);
$chaine = str_replace("\r", " ", $chaine);
$chaine = str_replace("\n", " ", $chaine);
$chaine = preg_replace("( +)", " ", $chaine);
return $chaine;
}
/**
* Advanced browse of a directory<br/>
* Retrieves the names of files and directories
* @param string $path <p>
* Path to list.
* </p>
* @param bool $dir [optional] <p>
* return directories's name.
* </p>
* @param bool $file [optional] <p>
* return files's name.
* </p>
* @param bool $recursive [optional] <p>
* recursive browse
* </p>
* @param bool $hierarch [optional] <p>
* return multi dimension array, keeping the tree
* </p>
* @param bool $fullPath [optional] <p>
* return fullpath of each files and directories.
* </p>
* @param bool $extension [optional] <p>
* return files with their extension.
* </p>
* @return array an array with each name of directories/files
* @deprecated ???
*/
static function listDir($path, $dir = true, $file = true, $recursive = true, $hierarch = false, $fullPath = true, $extension = true) {
// $path2 = $path . (substr($path, - 1) != "/" ? "/" : "" ) . "*";
$path2 = $path;
$myVideos = array();
foreach (iglob($path2) as $value) {
$value3 = substr($value, strrpos($value, "/") + 1);
$value2 = $fullPath ? $value : $value3;
if (is_dir($value)) {
if ($dir)
if ($hierarch)
$myVideos[$value3] = $value2 . '/';
else
$myVideos[] = $value2 . '/';
if ($recursive)
if ($hierarch)
$myVideos[$value3] = Tools::listDir($value, $dir, $file, $recursive, $hierarch, $fullPath);
else
$myVideos = array_merge(Tools::listDir($value . "/*", $dir, $file, $recursive, $hierarch, $fullPath), $myVideos);
}
else
if ($file)
if ($extension)
$myVideos[] = $value2;
else
$myVideos[] = self::removeExtension($value2);
}
return $myVideos;
}
/**
* @deprecated ???
*/
static function removeExtension($fileName) {
// cherche la postion du '.'
$position = strpos($fileName, ".");
// enleve l'extention, tout ce qui se trouve apres le '.'
$fileNameWithoutExtension = substr($fileName, 0, $position);
return $fileNameWithoutExtension;
}
/**
* @deprecated ???
*/
static function get_file_extension($file_name) {
return substr(strrchr($file_name, '.'), 1);
}
/**
*
*
* @param string $date
* @param bool $moiscomplet
*
* @return string
*
* @deprecated ??? use Slrfw\Format\DateTime::toText($date) instead
* @see Format\DateTime
*/
static function formate_date_texte($date, $moiscomplet = FALSE) {
if ($moiscomplet)
$lesmois = array("", "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre");
else
$lesmois = array("", "janv.", "fév.", "mars", "avril", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc.");
$date = explode("-", $date);
$d = strpos($date[2], " ");
if ($d) {
$heure = substr($date[2], $d + 1, strrpos($date[2], ":") - ($d + 1));
$ladate = (int) $date[2] . " " . $lesmois[(int) $date[1]] . " " . $date[0] . " à " . $heure;
}
else
$ladate = (int) $date[2] . " " . $lesmois[(int) $date[1]] . " " . $date[0];
return ($ladate);
}
/**
* transforme une date au format US (format DATE de mysql) en date au format FR et inverse.
*
* @param string $date date sous la forme YYYY-mm-dd resp. dd-mm-YYYY
* @param string $glueBefore séparateur de la $dateUS
* @param string $glueAfter séparateur de la date à retourner
*
* @return string sous la forme dd-mm-YYYY resp. YYYY-mm-dd
*
* @deprecated ??? use Slrfw\Format\DateTime::sql($dateSql) instead
* @see Format\DateTime
*/
static function formate_date_nombre($dateUS, $glueBefore = '-', $glueAfter = '-') {
$heureUs = substr($dateUS, 10);
$dateUS = substr($dateUS, 0, 10);
return implode($glueAfter, array_reverse(explode($glueBefore, $dateUS))) . $heureUs;
}
/**
*
*
* @param type $chaine
*
* @return type
*
* @deprecated ???
*/
static function regexAccents($chaine) {
mb_internal_encoding("UTF-8");
mb_regex_encoding('UTF-8');
$accent = array('a', 'à', 'á', 'â', 'ã', 'ä', 'å', 'c', 'ç', 'e', 'è', 'é', 'ê', 'ë', 'i', 'ì', 'í', 'î', 'ï', 'o', 'ð', 'ò', 'ó', 'ô', 'õ', 'ö', 'u', 'ù', 'ú', 'û', 'ü', 'y', 'ý', 'ý', 'ÿ');
$inter = array('%01', '%02', '%03', '%04', '%05', '%06', '%07', '%08', '%09', '%10', '%11', '%12', '%13', '%14', '%15', '%16', '%17', '%18',
'%19', '%20', '%21', '%22', '%23', '%24', '%25', '%26', '%27', '%28', '%29', '%30', '%31', '%32', '%33', '%34', '%35');
$regex = array('[aàáâãäå]', '[aàáâãäå]', '[aàáâãäå]', '[aàáâãäå]', '[aàáâãäå]', '[aàáâãäå]', '[aàáâãäå]',
'[cç]', '[cç]',
'[eèéêë]', '[eèéêë]', '[eèéêë]', '[eèéêë]', '[eèéêë]',
'[iìíîï]', '[iìíîï]', '[iìíîï]', '[iìíîï]', '[iìíîï]',
'[oðòóôõö]', '[oðòóôõö]', '[oðòóôõö]', '[oðòóôõö]', '[oðòóôõö]', '[oðòóôõö]', '[oðòóôõö]',
'[uùúûü]', '[uùúûü]', '[uùúûü]', '[uùúûü]',
'[yýýÿ]', '[yýýÿ]', '[yýýÿ]', '[yýýÿ]');
$chaine = str_ireplace($accent, $inter, $chaine);
$chaine = str_replace($inter, $regex, $chaine);
return $chaine;
}
/**
*
* @param type $chaine
* @param string $keywords
*
* @return type
*/
static function highlightedSearch($chaine, $keywords)
{
mb_internal_encoding("UTF-8");
mb_regex_encoding('UTF-8');
for ($Z = 0; $Z < count($keywords); $Z++) {
if (str_replace(' ', '', $keywords) != "") {
$keywords[$Z] = '#(' . self::regexAccents(str_replace(array('<¤>', '</¤>'), '', $keywords[$Z])) . ')#iu';
} else {
array_splice($keywords, $Z, 1);
$Z--;
}
}
if (is_array($keywords) && count($keywords) > 0) {
$chaine = preg_replace($keywords, '<¤>$1</¤>', $chaine);
$chaine = str_replace(array('<¤>', '</¤>'), array('<strong>', '</strong>'), $chaine);
}
return $chaine;
}
}
/**
*
*
* @param type $pattern
* @param type $flags
*
* @return type
*
* @deprecated ???
*/
function iglob($pattern, $flags = null) {
$path = preg_split(
'#(?<=\A|[\\\\/])((?>[^\\\\/*?]*)[*?](?>[^\\\\/]*))(?=\Z|[\\\\/])#', $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
foreach ($path as &$n)
if (preg_match('/[*?]/', $n)) {
$re = '';
for ($i = 0, $l = strlen($n); $i < $l; $i++)
switch ($n{$i}) {
case '*': $re .= '.*';
break;
case '?': $re .= '.';
break;
default: $re .= sprintf('\x%02x', ord($n{$i}));
}
$n = array(0, "/^$re$/i");
}
else
$n = array(1, $n);
$res = array();
iglob_DFS($path, $flags, '', 0, $res);
if (!($flags & GLOB_NOSORT))
sort($res);
return $res;
}
/**
*
* @param type $path
* @param type $flags
* @param type $parent
* @param type $lvl
* @param type $res
*
* @return void
*
* @deprecated ???
*/
function iglob_DFS($path, $flags, $parent, $lvl, &$res) {
$depth = count($path) - 1;
if (($lvl < $depth) && $path[$lvl][0])
$parent .= $path[$lvl++][1];
$files = array();
if ($path[$lvl][0])
$files[] = $path[$lvl][1];
else
if ($d = @opendir(($parent == '') ? '.' : $parent)) {
while (($n = readdir($d)) !== false)
if ($n != '.' && $n != '..')
$files[] = $n;
closedir($d);
}
foreach ($files as $f)
if ($path[$lvl][0] || preg_match($path[$lvl][1], $f)) {
$fullpath = $parent . $f;
if ($lvl == $depth) {
if (!($flags & GLOB_ONLYDIR) || is_dir($fullpath))
$res[] = $fullpath;
}
else
iglob_DFS($path, $flags, $fullpath, $lvl + 1, $res);
}
}