19 include_once (
'Class.DbObj.php');
20 include_once (
'Class.QueryDb.php');
21 include_once (
'Class.Log.php');
22 include_once (
'Class.Application.php');
23 include_once (
'Class.Group.php');
25 require_once
'PEAR.php';
26 require_once
'Crypt/CHAP.php';
29 define(
"ANONYMOUS_ID", 3);
30 define(
"GADMIN_ID", 4);
66 create table users ( id int not null,
67 iddomain int not null,
72 password varchar(30) not null,
81 create index users_idx2 on users(lastname);
82 create index users_idx3 on users(login);
83 CREATE UNIQUE INDEX uni_users on users (login,iddomain);
84 create sequence seq_id_users start 10";
92 include_once (
"Class.Domain.php");
93 $loginDomain = trim(mb_strtolower($loginDomain));
95 $query->AddQuery(
"login='" . pg_escape_string($loginDomain) .
"'");
96 $query->order_by =
'iddomain';
101 if (preg_match(
"/(.*)@(.*)/", $loginDomain, $reg)) {
103 $queryd =
new QueryDb($this->dbaccess,
"Domain");
104 $queryd->AddQuery(
"name='" . $reg[2] .
"'");
105 $list = $queryd->Query();
107 if ($queryd->nb == 1) {
108 $domainId =
$list[0]->iddomain;
109 $query->AddQuery(
"iddomain='$domainId'");
110 $query->AddQuery(
"login='" . pg_escape_string($reg[1]) .
"'");
129 $login = mb_strtolower($login);
130 $domain = mb_strtolower($domain);
133 $query->basic_elem->sup_where = array(
134 "login='" . pg_escape_string($login) .
"'",
157 $query->AddQuery(sprintf(
"fid = %d", $fid));
169 if ($this->Setlogin($this->
login, $this->iddomain))
return "this login exists";
170 if ($this->
login ==
"")
return _(
"login must not be empty");
171 if ($this->
id ==
"") {
172 $res = pg_exec($this->dbid,
"select nextval ('seq_id_users')");
173 $arr = pg_fetch_array(
$res, 0);
174 $this->
id = $arr[
"nextval"];
177 if (isset($this->isgroup) && ($this->isgroup ==
"Y")) {
181 $this->isgroup =
"N";
188 if ($this->
id == 1) {
199 $group =
new group($this->dbaccess);
202 if ($this->iddomain > 1) {
203 $qu =
new QueryDb($this->dbaccess,
"User");
204 $qu->AddQuery(
"login='all'");
205 $qu->AddQuery(
"iddomain=" . $this->iddomain);
206 $qu->AddQuery(
"id !=" . $this->
id);
207 $lu = $qu->Query(0, 0,
"TABLE");
213 $group->idgroup = $gid;
229 if (function_exists(
"mhash")) {
230 $this->cryptEngine =
new Crypt_CHAP_MSv1;
231 $this->ntpasswordhash = strtoupper(bin2hex($this->cryptEngine->ntPasswordHash($this->password_new)));
232 $this->lmpasswordhash = strtoupper(bin2hex($this->cryptEngine->lmPasswordHash($this->password_new)));
235 if ($this->
id == 1) {
246 include_once (
"WHAT/Class.Session.php");
248 $group =
new Group($this->dbaccess, $this->
id);
249 $ugroups = $group->groups;
250 $err = $group->Delete();
258 $action->session->CloseUsers($this->
id);
267 $query->basic_elem->sup_where = array(
268 "login='" . pg_escape_string($login) .
"'",
286 static $tdn = array();
290 if (isset($tdn[
$uid]))
return $tdn[
$uid];
292 $res = pg_exec(
$dbid,
"select firstname, lastname from users where id=$uid");
293 if (pg_num_rows(
$res) > 0) {
294 $arr = pg_fetch_array(
$res, 0);
295 if ($arr[
"firstname"]) $tdn[
$uid] = $arr[
"firstname"] .
' ' . $arr[
"lastname"];
296 else $tdn[
$uid] = $arr[
"lastname"];
308 function SetUsers($fid, $lname, $fname, $expires, $passdelay, $login, $status, $pwd1, $pwd2, $iddomain, $extmail)
311 $this->lastname = $lname;
312 $this->firstname = $fname;
313 $this->status = $status;
314 if ($this->
login ==
"") $this->
login = $login;
316 if ($pwd1 == $pwd2 and $pwd1 <>
"") {
319 if (($iddomain > 1) && ($this->iddomain != $iddomain) && ($this->iddomain < 2)) $needmail =
true;
320 else $needmail =
false;
322 if ($iddomain == 1) $iddomain = 0;
323 if ($iddomain == 0) {
324 if ($extmail !=
"") {
325 $this->mail = trim($extmail);
327 $this->iddomain =
"0";
329 if ($iddomain == 1) {
331 $this->iddomain = $iddomain;
332 } elseif ($this->iddomain != $iddomain) {
333 If ($this->iddomain > 1) {
335 include_once (
"Class.MailAccount.php");
336 $uacc =
new MailAccount(GetParam(
"MAILDB") , $this->
id);
337 if ($uacc->isAffected()) {
338 $uacc->iddomain = $iddomain;
342 $this->iddomain = $iddomain;
343 $this->mail = $this->
getMail();
347 if ($expires > 0) $this->
expires = $expires;
348 if ($passdelay > 0) $this->passdelay = $passdelay;
349 elseif ($passdelay == - 1) {
351 $this->passdelay = 0;
363 include_once (
"Class.MailAccount.php");
364 $this->iddomain = $iddomain;
367 if ($mailapp->Exists(
"MAILADMIN")) {
368 $mailapp->Set(
"MAILADMIN",
$action->parent);
369 $uacc =
new MailAccount($mailapp->GetParam(
"MAILDB"));
370 $uacc->iddomain = $this->iddomain;
372 $uacc->login = $this->login;
373 $err = $uacc->Add(
true);
375 $this->mail = $this->
getMail();
392 if ($gname !=
"") $this->lastname = $gname;
393 if (($this->
login ==
"") && ($login !=
"")) $this->
login = $login;
395 $this->iddomain = $iddomain;
396 if ($this->iddomain == 0) {
400 $this->mail = $this->
getMail();
403 $this->isgroup =
"Y";
415 if (intval($this->passdelay) == 0) {
417 $this->passdelay =
"0";
419 else if (intval($this->
expires) == 0) {
420 $this->
expires = time() + $this->passdelay;
428 if (
$dbaccess ==
"")
return _(
"no freedom DB access");
429 if ($this->fid <>
"") {
432 $err = $iuser->RefreshDocUser();
435 include_once (
"FDL/Lib.Dir.php");
437 elseif ($this->isgroup ==
"Y") $fam =
"IGROUP";
438 else $fam =
"IUSER";;
440 "us_whatid = '" . $this->
id .
"'"
443 if (count(
$tdoc) == 0) {
446 $iuser->SetValue(
"US_WHATID", $this->
id);
448 $this->fid = $iuser->id;
449 $this->
modify(
true, array(
452 $err = $iuser->RefreshDocUser();
454 $this->fid =
$tdoc[0]->id;
455 $this->
modify(
true, array(
467 $salt_space =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
468 srand((
double)microtime() * 1000000);
469 $salt = $salt_space[rand(0, strlen($salt_space) - 1) ];
470 $salt.= $salt_space[rand(0, strlen($salt_space) - 1) ];
471 $passk = crypt($pass, $salt);
476 if ($this->isgroup ==
'Y')
return false;
477 return ($this->
checkpass($pass, $this->password));
482 $salt = substr($passk, 0, 2);
483 $passres = crypt($pass, $salt);
484 return ($passres == $passk);
498 $group =
new group($this->dbaccess);
502 $this->lastname =
"Master";
504 if ($freedomctx ==
"") $this->firstname =
"Dynacase Platform";
505 else $this->firstname = ucfirst(
"$freedomctx");
507 $this->
login =
"admin";
513 $this->lastname =
"Utilisateurs";
514 $this->firstname =
"";
515 $this->
login =
"all";
516 $this->isgroup =
"Y";
523 $this->lastname =
"anonymous";
524 $this->firstname =
"guest";
525 $this->
login =
"anonymous";
526 $this->isgroup =
"N";
531 $this->lastname =
"Administrateurs";
532 $this->firstname =
"";
533 $this->
login =
"gadmin";
534 $this->isgroup =
"Y";
546 $query->order_by =
"lastname";
547 $query->AddQuery(
"(isgroup != 'Y') OR (isgroup isnull)");
548 if ($filteruser)
$query->AddQuery(
"(login ~* '" . pg_escape_string($filteruser) .
"')" .
" or " .
"(lastname ~* '" . pg_escape_string($filteruser) .
"')");
555 $query->order_by =
"lastname";
556 $query->AddQuery(
"isgroup = 'Y'");
557 return (
$query->Query(0, 0, $qtype));
563 $query->order_by =
"isgroup desc, lastname";
564 return (
$query->Query(0, 0, $qtype));
572 $query->AddQuery(
"iduser='{$this->id}'");
578 while (list($k, $v) = each(
$list)) {
579 $groupsid[$v[
"idgroup"]] = $v[
"idgroup"];
593 $list =
$query->Query(0, 0,
"TABLE",
"select users.* from users, groups where " .
"groups.iduser=users.id and " .
"idgroup=$id ;");
598 foreach (
$list as $k => $v) {
600 if ($v[
"isgroup"] ==
"Y") {
601 if (!in_array($v[
"id"],
$r)) {
602 array_push(
$r, $v[
"id"]);
620 if (
$onlygroup) $optgroup =
" and users.isgroup='Y' ";
622 $list =
$query->Query(0, 0,
"TABLE",
"select users.* from users, groups where " .
"groups.iduser=users.id and " .
"idgroup=$gid $optgroup;");
626 foreach (
$list as $k => $v) {
641 $g =
new Group($this->dbaccess);
642 $lg = $g->getChildsGroupId($this->
id);
644 $cond = getSqlCond($lg,
"idgroup",
true);
645 if (!$cond) $cond =
"true";
650 $sql = sprintf(
"SELECT distinct on (%s, users.id) users.id, users.login, users.firstname , users.lastname, users.mail,users.fid from users, groups where %s and (groups.iduser=users.id) %s and isgroup != 'Y' order by %s", $sort, $cond, $condname, $sort);
664 $g =
new Group($this->dbaccess);
665 $lg = $g->getChildsGroupId($this->
id);
667 $cond = getSqlCond($lg,
"idgroup",
true);
668 if (!$cond) $cond =
"true";
670 $sql = sprintf(
"select users.id from users, groups where %s and (groups.iduser=users.id) and users.id=%d and isgroup != 'Y'", $cond,
$uid);
674 return ($result !=
'');
681 $query->order_by =
"isgroup desc, lastname";
682 $selgroup =
"and (isgroup != 'Y' or isgroup is null)";
683 if ($withgroup) $selgroup =
"";
684 return (
$query->Query(0, 0, $qtype,
"select users.* from users, groups where " .
"groups.iduser=users.id and " .
"idgroup={$this->id} {$selgroup};"));
693 if ($expire ===
false) {
694 $expire = 3600 * 24 * 365 * 20;
698 }
else $scontext =
'';
701 include_once (
'WHAT/Class.UserToken.php');
702 include_once (
'WHAT/Class.QueryDb.php');
705 $q =
new QueryDb($this->dbaccess,
"UserToken");
706 $q->addQuery(
"userid=" . $this->
id);
707 if ($scontext)
$q->addQuery(
"context='" . pg_escape_string($scontext) .
"'");
708 $tu =
$q->Query(0, 0,
"TABLE");
709 $create = (
$q->nb == 0);
717 $uk->deleteExpired();
719 $uk->token = $uk->genToken();
720 $uk->expire = $uk->setExpiration($expire);
721 $uk->expendable = $oneshot;
722 $uk->context = $scontext;
726 $token = $tu[0][
"token"];
736 include_once (
'WHAT/Lib.Prefix.php');
740 if( $this->
id != 1 ) {
741 $err = sprintf(
"Method %s can only be used on the admin user.", __FUNCTION__);
745 $adminDir = $pubdir . DIRECTORY_SEPARATOR .
'admin';
746 $tmpFile = @tempnam($adminDir,
'.htpasswd');
747 if ($tmpFile ===
false) {
748 $err = sprintf(
"Error creating temporary file in '%s'.", $adminDir);
751 if (chmod($tmpFile, 0600) ===
false) {
752 $err = sprintf(
"Error setting mode 0600 on temporary file '%s'.", $tmpFile);
756 $passwdLine = sprintf(
"%s:{SHA}%s",
'admin', base64_encode(sha1(
$admin_passwd,
true)));
757 if (file_put_contents($tmpFile, $passwdLine) ===
false) {
758 $err = sprintf(
"Error writing to temporary file '%s'.", $tmpFile);
762 $htpasswdFile = $adminDir . DIRECTORY_SEPARATOR .
'.htpasswd';
763 if (rename($tmpFile, $htpasswdFile) ===
false) {
764 $err = sprintf(
"Error renaming temporary file '%s' to '%s'.", $tmpFile, $htpasswdFile);