Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Util.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Utilities functions for freedom
9  *
10  * @author Anakeen 2004
11  * @version $Id: Lib.Util.php,v 1.23 2009/01/07 18:05:08 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
20 {
21  include_once ("VAULT/Class.VaultFile.php");
22  return new VaultFile($dbaccess, "FREEDOM");
23 }
24 function getGen($dbaccess)
25 {
26  $freedomctx = getenv("freedom_context");
27  if ($freedomctx != "") {
28  return "GEN/" . $freedomctx;
29  }
30  return "GEN/default";
31 }
32 /**
33  * convert French date to iso8601
34  * @param string $fdate DD/MM/YYYY HH:MM:SS (CET)
35  * @param string $wtz with timezone add time zone in the end if true
36  * @return string date YYYY-MM-DD HH:MM:SS
37  * @deprecated use stringDateToIso() instead
38  */
39 function toIso8601($fdate, $wtz = false)
40 {
42  $isoDate = "";
43  if (preg_match("/^(\d\d)\/(\d\d)\/(\d\d\d\d)\s(\d\d)?:?(\d\d)?:?(\d\d)?\s+?(\w+)?$/", $fdate, $reg)) {
44  $isoDate = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $reg[3], $reg[2], $reg[1], $reg[4], $reg[5], $reg[6]);
45  if ($reg[8] != "") $tz = $reg[7];
46  }
47  // ISO 8601
48  if ($wtz && $tz) $isoDate.= " " . $tz;
49 
50  return $isoDate;
51 }
52 
53 function stringDateToJD($sdate)
54 {
55  $jd = FrenchDateToJD($sdate);
56  if ($jd === false) $jd = Iso8601ToJD($sdate);
57  return $jd;
58 }
59 /**
60  * convert French date to Julian day
61  * the seconds are ignored
62  * @param string $fdate DD/MM/YYYY HH:MM
63  * @return float julian day (return false if incorrect date)
64  */
65 function frenchDateToJD($fdate)
66 {
67  if (preg_match("/^(\d\d)\/(\d\d)\/(\d\d\d\d)\s?(\d\d)?:?(\d\d)?:?(\d\d)?(\.\d)*/", $fdate, $reg)) {
68  return cal2jd("CE", $reg[3], $reg[2], $reg[1], $reg[4], $reg[5], 0);
69  }
70  return false;
71 }
72 /**
73  * convert French date to unix timestamp
74  * date must be > 01/01/1970 and < 2038
75  * @param string $fdate DD/MM/YYYY HH:MM
76  * @return float number of second since epoch (return -1 if incorrect date)
77  */
78 function frenchDateToUnixTs($fdate, $utc = false)
79 {
80  if (preg_match("/^(\d\d)\/(\d\d)\/(\d\d\d\d)\s?(\d\d)?:?(\d\d)?:?(\d\d)?/", $fdate, $r)) {
81  if ($utc) $dt = gmmktime($r[4], $r[5], $r[6], $r[2], $r[1], $r[3]);
82  else $dt = mktime($r[4], $r[5], $r[6], $r[2], $r[1], $r[3]);
83  } else {
84  $dt = - 1;
85  }
86  return $dt;
87 }
88 /**
89  *
90  * @param string $fdate
91  * @return string
92  */
93 function stringDateToLocaleDate($fdate, $format = '')
94 {
95  if (preg_match("/^(\d\d\d\d)-(\d\d)-(\d\d)[\s|T]?(\d\d)?:?(\d\d)?:?(\d\d)?/", $fdate, $r)) {
96  // convert to french
97  $yy = $r[1];
98  $mo = $r[2];
99  $dd = $r[3];
100  $hh = $r[4];
101  $mm = $r[5];
102  $ss = $r[6];
103  if (!$hh && !$mm && !$ss) {
104  $fdate = sprintf("%s/%s/%s", $dd, $mo, $yy);
105  } elseif (!$ss) {
106 
107  $fdate = sprintf("%s/%s/%s %s:%s", $dd, $mo, $yy, $hh, $mm);
108  } else {
109 
110  $fdate = sprintf("%s/%s/%s %s:%s:%s", $dd, $mo, $yy, $hh, $mm, $ss);
111  }
112  }
113  return FrenchDateToLocaleDate($fdate, $format);
114 }
115 /**
116  *
117  * @param string $fdate
118  * @return string
119  */
120 function frenchDateToLocaleDate($fdate, $format = '')
121 {
122  if (empty($fdate)) {
123  return "";
124  }
125  if (empty($format)) {
126  $localeconfig = getLocaleConfig();
127  if ($localeconfig !== false) {
128  if (strlen($fdate) >= 16) {
129  $format = $localeconfig['dateTimeFormat'];
130  } else {
131  $format = $localeconfig['dateFormat'];
132  }
133  } else {
134  return $fdate;
135  }
136  }
137  $ldate = $format;
138  $d = substr($fdate, 0, 2);
139  $m = substr($fdate, 3, 2);
140  $y = substr($fdate, 6, 4);
141  if (!ctype_digit($d)) return $fdate;
142  if (!ctype_digit($m)) return $fdate;
143  if (!ctype_digit($y)) return $fdate;
144  if (strlen($fdate) >= 16) {
145  $h = substr($fdate, 11, 2);
146  $i = substr($fdate, 14, 2);
147  if (!ctype_digit($h)) return $fdate;
148  if (!ctype_digit($i)) return $fdate;
149  if (strlen($fdate) == 19) {
150  $s = substr($fdate, 17, 2);
151  if (!ctype_digit($s)) return $fdate;
152  }
153  }
154  $ldate = str_replace('%d', $d, $ldate);
155  $ldate = str_replace('%m', $m, $ldate);
156  $ldate = str_replace('%Y', $y, $ldate);
157  if (isset($h)) {
158  $ldate = str_replace('%H', $h, $ldate);
159  }
160  if (isset($i)) {
161  $ldate = str_replace('%M', $i, $ldate);
162  }
163  if (isset($s)) {
164  $ldate = str_replace('%S', $s, $ldate);
165  }
166  return $ldate;
167 }
168 /**
169  * convert French date DD/MM/YYYY to iso
170  * date must be > 01/01/1970 and < 2038
171  * @param string $fdate DD/MM/YYYY HH:MM
172  * @param boolean $withT return YYYY-MM-DDTHH:MM:SS else YYYY-MM-DD HH:MM:SS
173  * @return string YYYY-MM-DD HH:MM:SS
174  */
175 function frenchDateToIso($fdate, $withT = true)
176 {
177  if (!$fdate) return '';
178  if (preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d)\s?(\d\d)?:?(\d\d)?:?(\d\d)?/', $fdate, $r)) {
179 
180  if ($r[4] == "") $dt = sprintf("%04d-%02d-%02d", $r[3], $r[2], $r[1]);
181  else $dt = sprintf("%04d-%02d-%02d%s%02d:%02d:%02d", $r[3], $r[2], $r[1], ($withT) ? 'T' : ' ', $r[4], $r[5], $r[6]);
182  } else {
183  $dt = "";
184  }
185  return $dt;
186 }
187 /**
188  * convert iso date to unix timestamp
189  * date must be > 1970-01-01 and < 2038
190  * @param string $isodate YYYY-MM-DD HH:MM
191  * @return float number of second since epoch (return -1 if incorrect date)
192  */
193 function iso8601DateToUnixTs($isodate, $utc = false)
194 {
195  if (preg_match("/^(\d\d\d\d)-(\d\d)-(\d\d)[\s|T]?(\d\d)?:?(\d\d)?:?(\d\d)?/", $isodate, $r)) {
196  if ($utc) $dt = gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
197  else $dt = mktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
198  } else {
199  $dt = - 1;
200  }
201  return $dt;
202 }
203 /**
204  * convert date to unix timestamp
205  * date must be > 1970-01-01 and < 2038
206  * @param string $isodate YYYY-MM-DD HH:MM
207  * @return float number of second since epoch (return -1 if incorrect date)
208  */
209 function stringDateToUnixTs($isodate, $utc = false)
210 {
211  $dt = FrenchDateToUnixTs($isodate, $utc);
212  if ($dt < 0) $dt = iso8601DateToUnixTs($isodate, $utc);
213  return $dt;
214 }
215 /**
216  * convert string date to iso
217  *
218  * @param string $date DD/MM/YYYY HH:MM*
219  * @param string $format to indicate locale
220  * @param bool $withT to add a T : YYYY-MM-DDTHH:MM
221  * @return string YYYY-MM-DD HH:MM
222  */
223 function stringDateToIso($date, $format = false, $withT = false)
224 {
225  if ($format === false) {
226  if (preg_match("/^(\d\d\d\d)-(\d\d)-(\d\d)[\s|T]?(\d\d)?:?(\d\d)?:?(\d\d)?/", $date, $r)) {
227  if ($withT) {
228  if (strlen($date) > 11) {
229  $date[10] = 'T';
230  }
231  }
232  return $date;
233  } else {
234  $dt = FrenchDateToIso($date, $withT);
235  if (!$dt) return $date;
236  return $dt;
237  }
238  }
239  if (empty($format)) {
240  $localeconfig = getLocaleConfig();
241  if ($localeconfig !== false) {
242  $format = $localeconfig['dateTimeFormat'];
243  if (strlen($date) < strlen($format)) {
244  $format = $localeconfig['dateFormat'];
245  }
246  }
247  }
248  if (!empty($format)) {
249  $format = str_replace('%Y', '%YYY', $format);
250  if (strlen($date) < strlen($format)) {
251  return $date;
252  }
253  // date
254  $d = strpos($format, '%d');
255  $m = strpos($format, '%m');
256  $y = strpos($format, '%YYY');
257  if ($d !== false && $m !== false && $y !== false) {
258  $tmp = substr($date, $y, 4);
259  if (!ctype_digit($tmp)) return $date;
260  $dt = $tmp . '-';
261  $tmp = substr($date, $m, 2);
262  if (!ctype_digit($tmp)) return $date;
263  $dt.= $tmp . '-';
264  $tmp = substr($date, $d, 2);
265  if (!ctype_digit($tmp)) return $date;
266  $dt.= $tmp;
267  } else {
268  return $date;
269  }
270  // time
271  $h = strpos($format, '%H');
272  $m = strpos($format, '%M');
273  $s = strpos($format, '%S');
274  if ($h !== false && $m !== false) {
275  $dt.= ($withT ? 'T' : ' ') . substr($date, $h, 2) . ':' . substr($date, $m, 2);
276  if ($s !== false) {
277  $dt.= ':' . substr($date, $s, 2);
278  }
279  }
280  return $dt;
281  } else {
282  $dt = FrenchDateToIso($date, $withT);
283  if (!$dt) return $date;
284  return $dt;
285  }
286 }
287 /**
288  * convert iso8601 date to Julian day
289  * the seconds are ignored
290  * @param string $isodate YYYY-MM-DD HH:MM
291  * @return float julian day (return false if incorrect date)
292  */
293 function iso8601ToJD($isodate)
294 {
295  if (preg_match("/^(\d\d\d\d)-(\d\d)-(\d\d)\s?(\d\d)?:?(\d\d)?:?(\d\d)?/", $isodate, $reg)) {
296  return cal2jd("CE", $reg[1], $reg[2], $reg[3], $reg[4], $reg[5], 0);
297  }
298  return false;
299 }
300 
301 function cal2jd($era, $y, $m, $d, $h, $mn, $s)
302 {
303  if (($y > 1969) && ($y < 2038)) {
304  $nd = unixtojd(mktime($h, $mn, $s, $m, $d, $y));
305  $nm = (($h * 60 + $mn) - 720) / 1440;
306  $nd+= round($nm, 5);
307  return $nd;
308  } else {
309 
310  if ($y == 0) {
311  AddWarningMsg("There is no year 0 in the Julian system!");
312  return "invalid";
313  }
314  if ($y == 1582 && $m == 10 && $d > 4 && $d < 15 && $era != "BCE") {
315  AddWarningMsg("The dates 5 through 14 October, 1582, do not exist in the Gregorian system!");
316  return "invalid";
317  }
318 
319  if ($era == "BCE") $y = - $y + 1;
320  if ($m > 2) {
321  $jy = $y;
322  $jm = $m + 1;
323  } else {
324  $jy = $y - 1;
325  $jm = $m + 13;
326  }
327 
328  $intgr = floor(floor(365.25 * $jy) + floor(30.6001 * $jm) + $d + 1720995);
329  //check for switch to Gregorian calendar
330  $gregcal = 15 + 31 * (10 + 12 * 1582);
331  if ($d + 31 * ($m + 12 * $y) >= $gregcal) {
332  $ja = floor(0.01 * $jy);
333  $intgr+= 2 - $ja + floor(0.25 * $ja);
334  }
335  //correct for half-day offset
336  $dayfrac = $h / 24.0 - 0.5;
337  if ($dayfrac < 0.0) {
338  $dayfrac+= 1.0;
339  $intgr--;
340  }
341  //now set the fraction of a day
342  $frac = $dayfrac + ($mn + $s / 60.0) / 60.0 / 24.0;
343  //round to nearest second
344  $jd0 = ($intgr + $frac) * 100000;
345  $jd = floor($jd0);
346  if ($jd0 - $jd > 0.5) $jd++;
347  return $jd / 100000;
348  }
349  return "Date Error";
350 }
351 /**
352  * return the day of the week (1 id Monday, 7 is Sunday)
353  * @param float $jd julian date
354  * @return int
355  */
356 function jdWeekDay($jd)
357 {
358  //weekday
359  $t = doubleval($jd) + 0.5;
360  $wd = floor(($t / 7 - floor($t / 7)) * 7 + 0.000000000317); //add 0.01 sec for truncation error correction
361  return $wd + 1;
362 }
363 /**
364  * return the number of the week in year
365  * @param float $jd julian date
366  * @return int between 1 and 53
367  */
368 function jdWeekNumber($jd)
369 {
370  $j = doubleval($jd) + 0.5;
371  $d4 = ((($j + 31741 - ($j % 7)) % 146097) % 36524) % 1461;
372  $l = floor($d4 / 1460);
373  $d1 = (($d4 - $l) % 365) + $l;
374  $wn = floor($d1 / 7) + 1;
375  return ($wn);
376 }
377 /**
378  * return date in string format
379  * @param float $jd julian date
380  * @param string $dformat the format (default iso8601)
381  * @return string the formatted date
382  */
383 function jd2cal($jd, $dformat = '')
384 {
385  //
386  // get the date from the Julian day number
387  //
388  $intgr = floor($jd);
389  $frac = $jd - $intgr;
390  $gregjd = 2299160.5;
391  if ($jd >= $gregjd) { //Gregorian calendar correction
392  $tmp = floor((($intgr - 1867216.0) - 0.25) / 36524.25);
393  $j1 = $intgr + 1 + $tmp - floor(0.25 * $tmp);
394  } else $j1 = $intgr;
395  //correction for half day offset
396  $df = $frac + 0.5;
397  if ($df >= 1.0) {
398  $df-= 1.0;
399  $j1++;
400  }
401 
402  $j2 = $j1 + 1524.0;
403  $j3 = floor(6680.0 + (($j2 - 2439870.0) - 122.1) / 365.25);
404  $j4 = floor($j3 * 365.25);
405  $j5 = floor(($j2 - $j4) / 30.6001);
406 
407  $d = floor($j2 - $j4 - floor($j5 * 30.6001));
408  $m = floor($j5 - 1.0);
409  if ($m > 12) $m-= 12;
410  $y = floor($j3 - 4715.0);
411  if ($m > 2) $y--;
412  if ($y <= 0) $y--;
413  //
414  // get time of day from day fraction
415  //
416  $hr = floor($df * 24.0);
417  $mn = floor(($df * 24.0 - $hr) * 60.0);
418  $f = (($df * 24.0 - $hr) * 60.0 - $mn) * 60.0;
419  $sc = floor($f);
420  $f-= $sc;
421  if ($f > 0.5) $sc++;
422  if ($sc == 60) {
423  $sc = 0;
424  $mn++;
425  }
426  if ($mn == 60) {
427  $mn = 0;
428  $hr++;
429  }
430  if ($hr == 24) {
431  $hr = 0;
432  $d++; //this could cause a bug, but probably will never happen in practice
433 
434  }
435 
436  if ($y < 0) {
437  $y = - $y;
438  $ce = ' BCE';
439  // form.era[1].checked = true;
440 
441  } else {
442  $ce = '';
443  // form.era[0].checked = true;
444 
445  }
446  switch ($dformat) {
447  case 'M':
448  $retiso8601 = $m;
449  break;
450 
451  case 'Y':
452  $retiso8601 = $y;
453  break;
454 
455  case 'd':
456  $retiso8601 = $d;
457  break;
458 
459  case 'French':
460  $retiso8601 = sprintf("%02d/%02d/%04s", $d, $m, $y);
461  break;
462 
463  case 'FrenchLong':
464  $retiso8601 = sprintf("%02d/%02d/%04s %02d:%02d %s", $d, $m, $y, $hr, $mn, $ce);
465  break;
466 
467  default:
468  $retiso8601 = sprintf("%04d-%02d-%02s %02d:%02d%s", $y, $m, $d, $hr, $mn, $ce);
469  }
470  return $retiso8601;
471 }
472 /**
473  * backslash quote and replace double-quote by html entity
474  */
475 function addJsSlashes($s)
476 {
477  return str_replace(array(
478  "'",
479  "\""
480  ) , array(
481  "\\'",
482  "&#34;"
483  ) , $s);
484 }
485 /**
486  * Remove Character Accents
487  * Replaces accented characters in a string with their unaccented versions, for instance, converts "ÉéÜüÄäÖ" into "EeUuAaO". The function will handle any accented character for which there exists an HTML entity in PHP's translation table (i.e. pretty much any and all characters). Credits go to jennings at trad dot uji dot es for the original version of this incredibly useful little function. I used this function to good effect in OpenSEF.
488  */
489 function unaccent_($text)
490 {
491  static $search, $replace;
492  if (!$search) {
493  $search = $replace = array();
494  // Get the HTML entities table into an array
495  $trans = get_html_translation_table(HTML_ENTITIES);
496  // Go through the entity mappings one-by-one
497  foreach ($trans as $literal => $entity) {
498  // Make sure we don't process any other characters
499  // such as fractions, quotes etc:
500  if (ord($literal) >= 192) {
501  // Get the accented form of the letter
502  $search[] = $literal;
503  // Get e.g. 'E' from the string '&Eacute'
504  $replace[] = $entity[1];
505  }
506  }
507  }
508  return str_replace($search, $replace, $text);
509 }
510 function unaccent_iso8859_1($string)
511 {
512  $string = strtr($string, "\xA1\xAA\xBA\xBF\xC0\xC1\xC2\xC3\xC5\xC7
513  \xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1
514  \xD2\xD3\xD4\xD5\xD8\xD9\xDA\xDB\xDD\xE0
515  \xE1\xE2\xE3\xE5\xE7\xE8\xE9\xEA\xEB\xEC
516  \xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF8
517  \xF9\xFA\xFB\xFD\xFF", "!ao?AAAAAC
518  EEEEIIIIDN
519  OOOOOUUUYa
520  aaaaceeeei
521  iiidnooooo
522  uuuyy");
523  $string = strtr($string, array(
524  "\xC4" => "Ae",
525  "\xC6" => "AE",
526  "\xD6" => "Oe",
527  "\xDC" => "Ue",
528  "\xDE" => "TH",
529  "\xDF" => "ss",
530  "\xE4" => "ae",
531  "\xE6" => "ae",
532  "\xF6" => "oe",
533  "\xFC" => "ue",
534  "\xFE" => "th"
535  ));
536  return ($string);
537 }
538 
539 function unaccent_utf8($string)
540 {
541  return iconv("UTF-8", "ASCII//TRANSLIT", $string);
542 }
543 
544 function unaccent($s)
545 {
546  return unaccent_utf8($s);
547 }
548 /**
549  * replace a string separate by $sep
550  */
551 function sep_replace($ak, $idx, $by = "-", $sep = "\n")
552 {
553  $endoff = - 1;
554  do {
555  $offset = $endoff + 1;
556  $endoff = strpos($ak, "\n", $offset);
557  $idx--;
558  } while (($idx >= 0) && ($endoff !== false));
559 
560  if ($idx >= 0) return $ak . str_repeat("\n-", $idx) . "\n$by";
561  else if ($endoff == false) {
562  if ($offset == 0) return "$by";
563  else return substr($ak, 0, $offset - 1) . "\n$by";
564  } else return substr($ak, 0, $offset) . "$by\n" . substr($ak, $endoff + 1);
565 }
566 /**
567  * change & < and > character to respetiv entity
568  * @param string $s string to encode
569  * @return string encoded string
570  */
572 {
573  return str_replace(array(
574  "<",
575  ">",
576  '&'
577  ) , array(
578  "&lt;",
579  "&gt;",
580  "&amp;"
581  ) , $s);
582 }
583 ?>
← centre documentaire © anakeen - published under CC License - Dynacase