Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Common.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  * Common util functions
9  *
10  * @author Anakeen 2002
11  * @version $Id: Lib.Common.php,v 1.50 2008/09/11 14:50:04 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 include_once ("Lib.Prefix.php");
19 
20 function N_($s)
21 {
22  return ($s);
23 } // to tag gettext without change text immediatly
24 // library of utilies functions
25 function print_r2($z, $ret = false)
26 {
27  print "<PRE>";
28  print_r($z, $ret);
29  print "</PRE>";
30  flush();
31 }
32 /**
33  * send a message to system log
34  * @param string $msg message to log
35  * @param int $cut size limit
36  */
37 function AddLogMsg($msg, $cut = 80)
38 {
39  global $action;
40  if (isset($action->parent)) $action->parent->AddLogMsg($msg, $cut);
41 }
42 /**
43  * send a message to system log
44  * @param string $functioName
45  */
46 function deprecatedFunction($msg = '')
47 {
48  global $action;
49  if (isset($action->parent)) $action->parent->log->deprecated("Deprecated : " . $msg);
50 }
51 function AddWarningMsg($msg)
52 {
53  global $action;
54  if (isset($action->parent)) $action->parent->AddWarningMsg($msg);
55 }
56 /**
57  * get mail addr of a user
58  * @param int $userid system user identificator
59  * @param bool $full if true email is like : "John Doe" <John.doe@blackhole.net> else only system email address : john.doe@blackhole.net
60  * @return string mail address, false if user not exists
61  */
62 function getMailAddr($userid, $full = false)
63 {
64  $user = new User("", $userid);
65 
66  if ($user->isAffected()) {
67  $pren = $postn = "";
68  if ($full) {
69  $pren = '"' . trim(str_replace('"', '-', ucwords(strtolower($user->getDisplayName($user->id))))) . '" <';
70  $postn = '>';
71  }
72  return $pren . $user->getMail() . $postn;
73  }
74  return false;
75 }
76 
77 function getTmpDir($def = '/tmp')
78 {
79  static $tmp;
80  if (isset($tmp) && !empty($tmp)) {
81  return $tmp;
82  }
83  $tmp = getParam('CORE_TMPDIR', $def);
84  if (empty($tmp)) {
85  return $def;
86  }
87  return $tmp;
88 }
89 /**
90  * return value of parameters
91  *
92  * @brief must be in core or global type
93  * @param string $name param name
94  * @param string $def default value if value is empty
95  *
96  * @return string
97  */
98 function getParam($name, $def = "")
99 {
100  global $action;
101  if ($action) return $action->getParam($name, $def);
102  // if context not yet initialized
103  return getCoreParam($name, $def);
104 }
105 /**
106  * return value of a parameter
107  *
108  * @brief must be in core or global type
109  * @param string $name param name
110  * @param string $def default value if value is empty
111  *
112  * @return string
113  */
114 function getCoreParam($name, $def = "")
115 {
116  static $params = null;
117 
118  if (!$params) {
119  $tparams = array();
120  $err = simpleQuery("", "select name, val from paramv where (type = 'G') or (type='A' and appid = (select id from application where name ='CORE'));", $tparams);
121  if ($err == "") {
122  foreach ($tparams as $p) {
123  $params[$p['name']] = $p['val'];
124  }
125  }
126  }
127  if (array_key_exists($name, $params) == false) {
128  error_log(sprintf("parameter %s not found use %s instead", $name, $def));
129  }
130  return $params[$name] ? $params[$name] : $def;
131 }
132 /**
133  *
134  * @param string $name the variable
135  * @param string $def default value if variable is not defined
136  * @return unknown_type
137  */
138 function getSessionValue($name, $def = "")
139 {
140  global $action;
141  if ($action) return $action->read($name, $def);
142  return null;
143 }
144 
145 function getLayoutFile($app, $layfile)
146 {
147  if (strstr($layfile, '..')) {
148  return "";
149  }
150  if (!strstr($layfile, '.')) $layfile.= ".xml";
151  $socStyle = Getparam("CORE_SOCSTYLE");
152  $style = Getparam("STYLE");
153  $root = Getparam("CORE_PUBDIR");
154  if ($socStyle != "") {
155  $file = $root . "/STYLE/$socStyle/Layout/$layfile";
156  if (file_exists($file)) {
157  return ($file);
158  }
159 
160  $file = $root . "/STYLE/$socStyle/Layout/" . strtolower($layfile);
161  if (file_exists($file)) {
162  return ($file);
163  }
164  } elseif ($style != "") {
165  $file = $root . "/STYLE/$style/Layout/$layfile";
166  if (file_exists($file)) {
167  return ($file);
168  }
169 
170  $file = $root . "/STYLE/$style/Layout/" . strtolower($layfile);
171  if (file_exists($file)) {
172  return ($file);
173  }
174  }
175 
176  $file = $root . "/$app/Layout/$layfile";
177  if (file_exists($file)) {
178  return ($file);
179  }
180 
181  $file = $root . "/$app/Layout/" . strtolower($layfile);
182  if (file_exists($file)) {
183  return ($file);
184  }
185 
186  return "";
187 }
188 /**
189  * like ucfirst for utf-8
190  * @param $s
191  * @return string
192  */
193 function mb_ucfirst($s)
194 {
195  if ($s) {
196  $s = mb_strtoupper(mb_substr($s, 0, 1, 'UTF-8') , 'UTF-8') . mb_substr($s, 1, mb_strlen($s) , 'UTF-8');
197  }
198  return $s;
199 }
200 function microtime_diff($a, $b)
201 {
202  list($a_micro, $a_int) = explode(' ', $a);
203  list($b_micro, $b_int) = explode(' ', $b);
204  if ($a_int > $b_int) {
205  return ($a_int - $b_int) + ($a_micro - $b_micro);
206  } elseif ($a_int == $b_int) {
207  if ($a_micro > $b_micro) {
208  return ($a_int - $b_int) + ($a_micro - $b_micro);
209  } elseif ($a_micro < $b_micro) {
210  return ($b_int - $a_int) + ($b_micro - $a_micro);
211  } else {
212  return 0;
213  }
214  } else { // $a_int<$b_int
215  return ($b_int - $a_int) + ($b_micro - $a_micro);
216  }
217 }
218 function getDebugStack($slice = 1)
219 {
220  $td = @debug_backtrace(false);
221  if (!is_array($td)) return;
222  $t = array_slice($td, $slice);
223  foreach ($t as $k => $s) {
224  unset($t[$k]["args"]); // no set arg
225 
226  }
227  return $t;
228 }
230 {
231  global $CORE_DBID;
232  if (!$dbaccess) $dbaccess = getDbAccess();
233  if (!isset($CORE_DBID) || !($CORE_DBID[$dbaccess])) {
234  $CORE_DBID[$dbaccess] = pg_connect($dbaccess);
235  if (!$CORE_DBID[$dbaccess]) {
236  // fatal error
237  error_log("Fail to DB connect to : $dbaccess");
238  header('HTTP/1.0 503 DB connection unavalaible');
239  exit;
240  }
241  }
242  return $CORE_DBID[$dbaccess];
243 }
244 
245 function getDbAccess()
246 {
247  return getDbAccessCore();
248 }
249 
250 function getDbAccessCore()
251 {
252  return "service='" . getServiceCore() . "'";
253 }
254 
256 {
257  return "service='" . getServiceFreedom() . "'";
258 }
259 
260 function getDbEnv()
261 {
262  error_log("Deprecated call to getDbEnv() : use getFreedomContext()");
263  return getFreedomContext();
264 }
265 
267 {
268  $freedomctx = getenv("freedom_context");
269  if ($freedomctx == false || $freedomctx == "") {
270  return "default";
271  }
272  return $freedomctx;
273 }
274 
275 function getServiceCore()
276 {
277  static $pg_service = null;
278  global $pubdir;
279 
280  if ($pg_service) return $pg_service;
281 
282  $freedomctx = getFreedomContext();
283  if ($freedomctx != "") {
284  $filename = "$pubdir/context/$freedomctx/dbaccess.php";
285  if (file_exists($filename)) {
286  include ($filename);
287  }
288  }
289  if ($pgservice_core == "") {
290  include ("dbaccess.php");
291  }
292  if ($pgservice_core == "") {
293  error_log("Undefined pgservice_core in dbaccess.php");
294  exit(1);
295  }
296  $pg_service = $pgservice_core;
297  return $pg_service;
298 }
299 
301 {
302  static $pg_service = null;
303  global $pubdir;
304 
305  if ($pg_service) return $pg_service;
306 
307  $freedomctx = getFreedomContext();
308  if ($freedomctx != "") {
309  $filename = "$pubdir/context/$freedomctx/dbaccess.php";
310  if (file_exists($filename)) {
311  include ($filename);
312  }
313  }
314  if ($pgservice_freedom == "") {
315  include ("dbaccess.php");
316  }
317  if ($pgservice_freedom == "") {
318  error_log("Undefined pgservice_freedom in dbaccess.php");
319  exit(1);
320  }
321  $pg_service = $pgservice_freedom;
322  return $pg_service;
323 }
324 
326 {
327  error_log("Deprecated call to getDbName(dbaccess) : use getServiceName(dbaccess)");
328  return getServiceName($dbaccess);
329 }
330 
332 {
333  if (preg_match("/service='?([a-zA-Z0-9_.-]+)/", $dbaccess, $reg)) {
334  return $reg[1];
335  }
336 }
337 /**
338  * send simple query to database
339  * @param string $dbaccessaccess database coordonates
340  * @param string $query sql query
341  * @param string/array &$result query result
342  * @param boolean $singlecolumn set to true if only onz field is return
343  * @param boolean $singleresult set to true is only one row is expected (return the first row). If is combined with singlecolumn return the value not an array
344  * @return string error message. Empty message if no errors.
345  */
346 function simpleQuery($dbaccess, $query, &$result = array() , $singlecolumn = false, $singleresult = false)
347 {
348  global $SQLDEBUG;
350  if ($dbid) {
351  $result = array();
352  if ($SQLDEBUG) $sqlt1 = microtime();
353  $r = pg_query($dbid, $query);
354  if ($r) {
355  if (pg_numrows($r) > 0) {
356  if ($singlecolumn) $result = pg_fetch_all_columns($r, 0);
357  else $result = pg_fetch_all($r);
358  if ($singleresult) $result = $result[0];
359  } else {
360  if ($singleresult) $result = false;
361  }
362  if ($SQLDEBUG) {
363  global $TSQLDELAY;
364  $SQLDELAY+= microtime_diff(microtime() , $sqlt1); // to test delay of request
365  $TSQLDELAY[] = array(
366  "t" => sprintf("%.04f", microtime_diff(microtime() , $sqlt1)) ,
367  "s" => str_replace(array(
368  "from",
369  'where'
370  ) , array(
371  "\nfrom",
372  "\nwhere"
373  ) , $query) ,
374  "st" => getDebugStack(1)
375  );
376  }
377  } else {
378  $err = pg_last_error($dbid);
379  }
380  } else $err = sprintf(_("cannot connect to %s") , $dbaccess);
381  return $err;
382 }
383 function getAuthType($freedomctx = "")
384 {
385  global $pubdir;
386 
387  if (array_key_exists('authtype', $_GET)) {
388  return $_GET['authtype'];
389  }
390 
391  if ($freedomctx == "") {
392  $freedomctx = getFreedomContext();
393  }
394  if ($freedomctx != "") {
395  $filename = "$pubdir/context/$freedomctx/dbaccess.php";
396  if (file_exists($filename)) {
397  include ($filename);
398  }
399  }
400 
401  if ($freedom_authtype == "") {
402  $freedom_authtype = "apache";
403  }
404 
405  return trim($freedom_authtype);
406 }
407 
408 function getAuthProvider($freedomctx = "")
409 {
410  global $pubdir;
411 
412  if ($freedomctx == "") {
413  $freedomctx = getFreedomContext();
414  }
415  if ($freedomctx != "") {
416  $filename = "$pubdir/context/$freedomctx/dbaccess.php";
417  if (file_exists($filename)) {
418  include ($filename);
419  }
420  }
421 
422  if ($freedom_authprovider == "") {
423  $freedom_authprovider = "apache";
424  }
425 
426  return trim($freedom_authprovider);
427 }
428 
429 function getAuthProviderList($freedomctx = "")
430 {
431  global $pubdir;
432 
433  return preg_split("/\s*,\s*/", getAuthProvider($freedomctx));
434 }
435 
436 function getAuthTypeParams($freedomctx = "")
437 {
438  global $pubdir;
439 
440  if ($freedomctx == "") {
441  $freedomctx = getFreedomContext();
442  }
443  if ($freedomctx != "") {
444  $filename = "$pubdir/context/$freedomctx/dbaccess.php";
445  if (file_exists($filename)) {
446  include ($filename);
447  }
448  }
449  if (!is_array($freedom_authtypeparams)) {
450  printf(_("filename %s does not contain freedom_authtypeparams variable. May be old syntax for configuration file") , $filename);
451  exit;
452  }
453 
454  if (!array_key_exists(getAuthType() , $freedom_authtypeparams)) {
455  error_log(__FUNCTION__ . ":" . __LINE__ . "> authtype " . getAuthType() . " does not exists in freedom_authtypeparams");
456  return array();
457  }
458 
459  return $freedom_authtypeparams[getAuthType() ];
460 }
461 
462 function getAuthParam($freedomctx = "", $provider = "")
463 {
464  global $pubdir;
465 
466  if ($provider == "") return array();
467 
468  if ($freedomctx == "") {
469  $freedomctx = getFreedomContext();
470  }
471  if ($freedomctx != "") {
472  $filename = "$pubdir/context/$freedomctx/dbaccess.php";
473  if (file_exists($filename)) {
474  include ($filename);
475  }
476  }
477 
478  if (!is_array($freedom_providers)) {
479  return array();
480  }
481 
482  if (!array_key_exists($provider, $freedom_providers)) {
483  error_log(__FUNCTION__ . ":" . __LINE__ . "provider " . $provider . " does not exists in freedom_providers");
484  return array();
485  }
486 
487  return $freedom_providers[$provider];
488 }
489 /**
490  * return shell commande for wsh
491  * depending of database (in case of several instances)
492  * @param bool $nice set to true if want nice mode
493  * @param int $userid the user identificator to send command (if 0 send like admin without specific user parameter)
494  * @param bool $sudo set to true if want to be send with sudo (need /etc/sudoers correctly configured)
495  * @return string the command
496  */
497 function getWshCmd($nice = false, $userid = 0, $sudo = false)
498 {
499  $freedomctx = getFreedomContext(); // choose when several databases
500  $wsh = "export freedom_context=\"$freedomctx\";";
501  if ($nice) $wsh.= "nice -n +10 ";
502  if ($sudo) $wsh.= "sudo ";
503  $wsh.= escapeshellarg(GetParam("CORE_PUBDIR")) . "/wsh.php ";
504  $userid = intval($userid);
505  if ($userid > 0) $wsh.= "--userid=$userid ";
506  return $wsh;
507 }
508 /**
509  * get the system user id
510  * @return int
511  */
512 function getUserId()
513 {
514  global $action;
515  if ($action) return $action->user->id;
516 
517  return 0;
518 }
519 /**
520  * exec list of unix command in background
521  * @param array $tcmd unix command strings
522  */
523 function bgexec($tcmd, &$result, &$err)
524 {
525  $foutname = uniqid(getTmpDir() . "/bgexec");
526  $fout = fopen($foutname, "w+");
527  fwrite($fout, "#!/bin/bash\n");
528  foreach ($tcmd as $v) {
529  fwrite($fout, "$v\n");
530  }
531  fclose($fout);
532  chmod($foutname, 0700);
533  // if (session_id()) session_write_close(); // necessary to close if not background cmd
534  exec("exec nohup $foutname > /dev/null 2>&1 &", $result, $err);
535  //if (session_id()) @session_start();
536 
537 }
538 
539 function wbartext($text)
540 {
541  wbar('-', '-', $text);
542 }
543 
544 function wbar($reste, $total, $text = "", $fbar = false)
545 {
546  static $preste, $ptotal;
547  if (!$fbar) $fbar = GetHttpVars("bar"); // for progress bar
548  if ($fbar) {
549  if ($reste === '-') $reste = $preste;
550  else $preste = $reste;
551  if ($total === '-') $total = $ptotal;
552  else $ptotal = $total;
553  if (file_exists("$fbar.lck")) {
554  $wmode = "w";
555  unlink("$fbar.lck");
556  } else {
557  $wmode = "a";
558  }
559  $ffbar = fopen($fbar, $wmode);
560  fputs($ffbar, "$reste/$total/$text\n");
561  fclose($ffbar);
562  }
563 }
564 
565 function getJsVersion()
566 {
567  include_once ("Class.QueryDb.php");
568  $q = new QueryDb("", "param");
569  $q->AddQuery("name='VERSION'");
570  $l = $q->Query(0, 0, "TABLE");
571  $nv = 0;
572  foreach ($l as $k => $v) {
573  $nv+= intval(str_replace('.', '', $v["val"]));
574  }
575 
576  return $nv;
577 }
578 /**
579  * produce an anchor mailto '<a ...>'
580  * @param string to a valid mail address or list separated by comma -supported by client-
581  * @param string anchor content <a...>anchor content</a>
582  * @param string subject
583  * @param string cc
584  * @param string bcc
585  * @param array treated as html anchor attribute : key is attribute name and value.. value
586  * @param string force link to be produced according the value
587  * @return string like user admin dbname anakeen
588  */
589 function setMailtoAnchor($to, $acontent = "", $subject = "", $cc = "", $bcc = "", $from = "", $anchorattr = array() , $forcelink = "")
590 {
591 
592  global $action;
593 
594  if ($to == "") return '';
595 
596  if ($forcelink == "mailto" || $forcelink == "squirrel") {
597  $target = $forcelink;
598  } else {
599  $target = strtolower(GetParam("CORE_MAIL_LINK", "optimal"));
600  if ($target == "optimal") {
601  $target = "mailto";
602  if ($action->user->iddomain > 9) {
603  $query = new QueryDb($action->dbaccess, "Application");
604  $query->basic_elem->sup_where = array(
605  "name='MAIL'",
606  "available='Y'",
607  "displayable='Y'"
608  );
609  $list = $query->Query(0, 0, "TABLE");
610  if ($query->nb > 0) {
611  $queryact = new QueryDb($action->dbaccess, "Action");
612  $queryact->AddQuery("id_application=" . $list[0]["id"]);
613  $queryact->AddQuery("root='Y'");
614  $listact = $queryact->Query(0, 0, "TABLE");
615  $root_acl_name = $listact[0]["acl"];
616  if ($action->HasPermission($root_acl_name, $list[0]["id"])) {
617  $target = "squirrel";
618  }
619  }
620  }
621  }
622  }
623  $prot = ($_SERVER["HTTPS"] == "on" ? "https" : "http");
624  $host = $_SERVER["SERVER_NAME"];
625  $port = $_SERVER["SERVER_PORT"];
626 
627  $attrcode = "";
628  if (is_array($anchorattr)) {
629  foreach ($anchorattr as $k => $v) $attrcode.= ' ' . $k . '="' . $v . '"';
630  }
631 
632  $subject = str_replace(" ", "%20", $subject);
633 
634  switch ($target) {
635  case "squirrel":
636  $link = ' <a ';
637  $link.= 'href="' . $prot . "://" . $host . ":" . $port . "/" . GetParam("CORE_MAIL_SQUIRRELBASE", "squirrel") . "/src/compose.php?";
638  $link.= "&send_to=" . $to;
639  $link.= ($subject != "" ? '&subject=' . $subject : '');
640  $link.= ($cc != "" ? '&cc=' . $cc : '');
641  $link.= ($bcc != "" ? '&bcc=' . $bcc : '');
642  $link.= '"';
643  $link.= $attrcode;
644  $link.= '>';
645  $link.= $acontent;
646  $link.= '</a>';
647  break;
648 
649  case "mailto":
650  $link = '<a ';
651  $link.= 'href="mailto:' . $to . '"';
652  $link.= ($subject != "" ? '&Subject=' . $subject : '');
653  $link.= ($cc != "" ? '&cc=' . $cc : '');
654  $link.= ($bcc != "" ? '&bcc=' . $bcc : '');
655  $link.= '"';
656  $link.= $attrcode;
657  $link.= '>';
658  $link.= $acontent;
659  $link.= '</a>';
660  break;
661 
662  default:
663  $link = '<span ' . $classcode . '>' . $acontent . '</span>';
664  }
665  return $link;
666 }
667 /**
668  * Returns <kbd>true</kbd> if the string or array of string is encoded in UTF8.
669  *
670  * Example of use. If you want to know if a file is saved in UTF8 format :
671  * <code> $array = file('one file.txt');
672  * $isUTF8 = isUTF8($array);
673  * if (!$isUTF8) --> we need to apply utf8_encode() to be in UTF8
674  * else --> we are in UTF8 :)
675  * </code>
676  * @param mixed A string, or an array from a file() function.
677  * @return boolean
678  */
679 function isUTF8($string)
680 {
681  if (is_array($string)) return seems_utf8(implode('', $string));
682  else return seems_utf8($string);
683 }
684 /**
685  * Returns <kbd>true</kbd> if the string is encoded in UTF8.
686  *
687  * @param mixed $Str string
688  * @return boolean
689  */
690 function seems_utf8($Str)
691 {
692  for ($i = 0; $i < strlen($Str); $i++) {
693  if (ord($Str[$i]) < 0x80) $n = 0; # 0bbbbbbb
694  elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n = 1; # 110bbbbb
695  elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n = 2; # 1110bbbb
696  elseif ((ord($Str[$i]) & 0xF0) == 0xF0) $n = 3; # 1111bbbb
697  else return false; # Does not match any model
698  for ($j = 0; $j < $n; $j++) { # n octets that match 10bbbbbb follow ?
699  if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) return false;
700  }
701  }
702  return true;
703 }
704 /**
705  * return true if it is possible to manage USER by FREEDOM
706  *
707  */
708 function usefreedomuser()
709 {
710  if (@include_once ('FDL/Lib.Usercard.php')) {
711  $usefreedom = (GetParam("USE_FREEDOM_USER") != "no");
712  return $usefreedom;
713  }
714  return false;
715 }
716 /**
717  * Initialise WHAT : set global $action whithout an authorized user
718  *
719  */
721 {
722  global $action;
723  include_once ('Class.User.php');
724  include_once ('Class.Session.php');
725 
726  $CoreNull = "";
727  $core = new Application();
728  $core->Set("CORE", $CoreNull);
729  $core->session = new Session();
730  $action = new Action();
731  $action->Set("", $core);
732  // i18n
733  $lang = $action->Getparam("CORE_LANG");
735 }
736 
737 function setSystemLogin($login)
738 {
739  global $action;
740  include_once ('Class.User.php');
741  include_once ('Class.Session.php');
742 
743  if ($login != "") {
744  $action->user = new User(); //create user
745  $action->user->setLoginName($login);
746  }
747 }
748 /**
749  * Returns a random password of specified length composed
750  * with chars from the given charspace string or pattern
751  */
752 
753 function mkpasswd($length = 8, $charspace = "")
754 {
755  if ($charspace == "") {
756  $charspace = "[:alnum:]";
757  }
758  // Repeat a pattern e.g. [:a:3] -> [:a:][:a:][:a:]
759  $charspace = preg_replace("/(\[:[a-z]+:)(\d+)(\])/e", "str_repeat('\\1\\3',\\2)", $charspace);
760  // Expand [:patterns:]
761  $charspace = preg_replace(array(
762  "/\[:alnum:\]/",
763  "/\[:extrastrong:\]/",
764  "/\[:hex:\]/",
765  "/\[:lower:\]/",
766  "/\[:upper:\]/",
767  "/\[:digit:\]/",
768  "/\[:extra:\]/",
769  ) , array(
770  "[:lower:][:upper:][:digit:]",
771  "[:extra:],;:=+*/(){}[]&@#!?\"'<>",
772  "[:digit:]abcdef",
773  "abcdefghijklmnopqrstuvwxyz",
774  "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
775  "0123456789",
776  "-_.",
777  ) , $charspace);
778 
779  $passwd = "";
780  for ($i = 0; $i < $length; $i++) {
781  $passwd.= substr($charspace, rand(0, strlen($charspace) - 1) , 1);
782  }
783 
784  return $passwd;
785 }
786 /**
787  * return lcdate use in database : iso or dmy
788  * @return string
789  */
790 function getLcdate()
791 {
792  return substr(getParam("CORE_LCDATE") , 0, 3);
793 }
794 /**
795  *
796  * @param string $core_lang
797  */
798 function getLocaleConfig($core_lang = '')
799 {
800 
801  if (empty($core_lang)) {
802  $core_lang = getParam("CORE_LANG", "fr_FR");
803  }
804  $lng = substr($core_lang, 0, 2);
805  if (preg_match('#^[a-z0-9_\.-]+$#i', $core_lang) && file_exists("locale/" . $lng . "/lang.php")) {
806  include ("locale/" . $lng . "/lang.php");
807  } else {
808  include ("locale/fr/lang.php");
809  }
810  if (!isset($lang) || !isset($lang[$core_lang]) || !is_array($lang[$core_lang])) {
811  return false;
812  }
813  return $lang[$core_lang];
814 }
815 /**
816  *
817  * @param string $lang
818  */
820 {
821  global $pubdir;
822  // print "<h1>setLanguage:$lang</H1>";
823  $lang.= ".UTF-8";
824  setlocale(LC_MESSAGES, $lang);
825  setlocale(LC_CTYPE, $lang);
826  setlocale(LC_MONETARY, $lang);
827  setlocale(LC_TIME, $lang);
828  //print $action->Getparam("CORE_LANG");
829  $number = 0;
830  if (is_file("$pubdir/locale/.gettextnumber")) {
831  $number = trim(@file_get_contents("$pubdir/locale/.gettextnumber"));
832  if ($number == "") {
833  $number = 0;
834  }
835  }
836 
837  $td = "freedom-catalog$number";
838 
839  putenv("LANG=" . $lang); // needed for old Linux kernel < 2.4
840  bindtextdomain($td, "$pubdir/locale");
841  bind_textdomain_codeset($td, 'utf-8');
842  textdomain($td);
843  mb_internal_encoding('UTF-8');
844 }
845 ?>
← centre documentaire © anakeen - published under CC License - Dynacase