64 if (empty($conf->ldap) || empty($conf->ldap->enabled)) {
67 if (defined(
'DISABLE_LDAP_SYNCHRO')) {
71 if (!function_exists(
'ldap_connect')) {
72 dol_syslog(
"Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
76 require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
77 require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
82 if ($action ==
'USER_CREATE') {
83 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
86 $result = $ldap->connect_bind();
89 $info = $object->_load_ldap_info();
90 $dn = $object->_load_ldap_dn($info);
92 $result = $ldap->add($dn, $info, $user);
96 $this->error =
"ErrorLDAP ".$ldap->error;
99 } elseif ($action ==
'USER_MODIFY') {
100 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
103 $result = $ldap->connect_bind();
106 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
107 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
108 $object->oldcopy = clone $object;
111 $oldinfo = $object->oldcopy->_load_ldap_info();
112 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
115 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
116 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
117 $records = $ldap->search($container, $search);
118 if (count($records) && $records[
'count'] == 0) {
122 $info = $object->_load_ldap_info();
123 $dn = $object->_load_ldap_dn($info);
124 $newrdn = $object->_load_ldap_dn($info, 2);
125 $newparent = $object->_load_ldap_dn($info, 1);
127 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
129 if ($result > 0 && !empty($object->context[
'newgroupid'])) {
132 $usergroup->fetch($object->context[
'newgroupid'],
'',
true);
134 $oldinfo = $usergroup->_load_ldap_info();
135 $olddn = $usergroup->_load_ldap_dn($oldinfo);
138 $container = $usergroup->_load_ldap_dn($oldinfo, 1);
139 $search =
"(".$usergroup->_load_ldap_dn($oldinfo, 2).
")";
140 $records = $ldap->search($container, $search);
141 if (count($records) && $records[
'count'] == 0) {
145 $info = $usergroup->_load_ldap_info();
146 $dn = $usergroup->_load_ldap_dn($info);
148 $result = $ldap->update($dn, $info, $user, $olddn);
151 if ($result > 0 && !empty($object->context[
'oldgroupid'])) {
154 $usergroup->fetch($object->context[
'oldgroupid'],
'',
true);
156 $oldinfo = $usergroup->_load_ldap_info();
157 $olddn = $usergroup->_load_ldap_dn($oldinfo);
160 $container = $usergroup->_load_ldap_dn($oldinfo, 1);
161 $search =
"(".$usergroup->_load_ldap_dn($oldinfo, 2).
")";
162 $records = $ldap->search($container, $search);
163 if (count($records) && $records[
'count'] == 0) {
167 $info = $usergroup->_load_ldap_info();
168 $dn = $usergroup->_load_ldap_dn($info);
170 $result = $ldap->update($dn, $info, $user, $olddn);
175 $this->error =
"ErrorLDAP ".$ldap->error;
178 } elseif ($action ==
'USER_NEW_PASSWORD') {
179 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
182 $result = $ldap->connect_bind();
185 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
186 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
187 $object->oldcopy = clone $object;
190 $oldinfo = $object->oldcopy->_load_ldap_info();
191 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
194 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
195 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
196 $records = $ldap->search($container, $search);
197 if (count($records) && $records[
'count'] == 0) {
201 $info = $object->_load_ldap_info();
202 $dn = $object->_load_ldap_dn($info);
204 $result = $ldap->update($dn, $info, $user, $olddn);
208 $this->error =
"ErrorLDAP ".$ldap->error;
211 } elseif ($action ==
'USER_ENABLEDISABLE') {
212 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
213 } elseif ($action ==
'USER_DELETE') {
214 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
217 $result = $ldap->connect_bind();
220 $info = $object->_load_ldap_info();
221 $dn = $object->_load_ldap_dn($info);
223 $result = $ldap->delete($dn);
227 $this->error =
"ErrorLDAP ".$ldap->error;
298 } elseif ($action ==
'USERGROUP_CREATE') {
300 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
303 $result = $ldap->connect_bind();
306 $info = $object->_load_ldap_info();
307 $dn = $object->_load_ldap_dn($info);
310 if (empty($info[$conf->global->LDAP_GROUP_FIELD_GROUPID]) && in_array(
'posixGroup', $info[
'objectclass'])) {
311 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_GROUPS');
314 $result = $ldap->add($dn, $info, $user);
318 $this->error =
"ErrorLDAP ".$ldap->error;
321 } elseif ($action ==
'USERGROUP_MODIFY') {
322 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
325 $result = $ldap->connect_bind();
328 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
329 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
330 $object->oldcopy = clone $object;
333 $oldinfo = $object->oldcopy->_load_ldap_info();
334 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
337 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
338 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
339 $records = $ldap->search($container, $search);
340 if (count($records) && $records[
'count'] == 0) {
344 $info = $object->_load_ldap_info();
345 $dn = $object->_load_ldap_dn($info);
347 $result = $ldap->update($dn, $info, $user, $olddn);
351 $this->error =
"ErrorLDAP ".$ldap->error;
354 } elseif ($action ==
'USERGROUP_DELETE') {
355 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
358 $result = $ldap->connect_bind();
361 $info = $object->_load_ldap_info();
362 $dn = $object->_load_ldap_dn($info);
364 $result = $ldap->delete($dn);
368 $this->error =
"ErrorLDAP ".$ldap->error;
371 } elseif ($action ==
'CONTACT_CREATE') {
373 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
374 if (!empty($conf->global->LDAP_CONTACT_ACTIVE)) {
376 $result = $ldap->connect_bind();
379 $info = $object->_load_ldap_info();
380 $dn = $object->_load_ldap_dn($info);
382 $result = $ldap->add($dn, $info, $user);
386 $this->error =
"ErrorLDAP ".$ldap->error;
389 } elseif ($action ==
'CONTACT_MODIFY') {
390 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
391 if (!empty($conf->global->LDAP_CONTACT_ACTIVE)) {
393 $result = $ldap->connect_bind();
396 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
397 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
398 $object->oldcopy = clone $object;
401 $oldinfo = $object->oldcopy->_load_ldap_info();
402 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
405 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
406 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
407 $records = $ldap->search($container, $search);
408 if (count($records) && $records[
'count'] == 0) {
412 $info = $object->_load_ldap_info();
413 $dn = $object->_load_ldap_dn($info);
415 $result = $ldap->update($dn, $info, $user, $olddn);
419 $this->error =
"ErrorLDAP ".$ldap->error;
422 } elseif ($action ==
'CONTACT_DELETE') {
423 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
424 if (!empty($conf->global->LDAP_CONTACT_ACTIVE)) {
426 $result = $ldap->connect_bind();
429 $info = $object->_load_ldap_info();
430 $dn = $object->_load_ldap_dn($info);
432 $result = $ldap->delete($dn);
436 $this->error =
"ErrorLDAP ".$ldap->error;
439 } elseif ($action ==
'MEMBER_CREATE') {
441 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
444 $result = $ldap->connect_bind();
447 $info = $object->_load_ldap_info();
448 $dn = $object->_load_ldap_dn($info);
450 $result = $ldap->add($dn, $info, $user);
454 if ($object->typeid > 0) {
455 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
457 $membertype->fetch($object->typeid);
458 $membertype->listMembersForMemberType(
'', 1);
460 $oldinfo = $membertype->_load_ldap_info();
461 $olddn = $membertype->_load_ldap_dn($oldinfo);
464 $container = $membertype->_load_ldap_dn($oldinfo, 1);
465 $search =
"(".$membertype->_load_ldap_dn($oldinfo, 2).
")";
466 $records = $ldap->search($container, $search);
467 if (count($records) && $records[
'count'] == 0) {
471 $info = $membertype->_load_ldap_info();
472 $dn = $membertype->_load_ldap_dn($info);
474 $result = $ldap->update($dn, $info, $user, $olddn);
480 $this->error =
"ErrorLDAP ".$ldap->error;
483 } elseif ($action ==
'MEMBER_VALIDATE') {
484 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
487 if (!empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) {
489 $result = $ldap->connect_bind();
492 $info = $object->_load_ldap_info();
493 $dn = $object->_load_ldap_dn($info);
496 $result = $ldap->update($dn, $info, $user, $olddn);
500 $this->error =
"ErrorLDAP ".$ldap->error;
504 } elseif ($action ==
'MEMBER_SUBSCRIPTION') {
505 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
508 if (!empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE)
509 || !empty($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT)
510 || !empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE)
511 || !empty($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT)
512 || !empty($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)) {
514 $result = $ldap->connect_bind();
517 $info = $object->_load_ldap_info();
518 $dn = $object->_load_ldap_dn($info);
521 $result = $ldap->update($dn, $info, $user, $olddn);
525 $this->error =
"ErrorLDAP ".$ldap->error;
529 } elseif ($action ==
'MEMBER_MODIFY') {
530 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
533 $result = $ldap->connect_bind();
536 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
537 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
538 $object->oldcopy = clone $object;
541 $oldinfo = $object->oldcopy->_load_ldap_info();
542 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
545 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
546 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
547 $records = $ldap->search($container, $search);
548 if (count($records) && $records[
'count'] == 0) {
552 $info = $object->_load_ldap_info();
553 $dn = $object->_load_ldap_dn($info);
554 $newrdn = $object->_load_ldap_dn($info, 2);
555 $newparent = $object->_load_ldap_dn($info, 1);
557 $result = $ldap->update($dn, $info, $user, $olddn, $newrdn, $newparent);
561 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
567 $newmembertype->fetch($object->typeid);
568 $newmembertype->listMembersForMemberType(
'', 1);
570 $oldinfo = $newmembertype->_load_ldap_info();
571 $olddn = $newmembertype->_load_ldap_dn($oldinfo);
574 $container = $newmembertype->_load_ldap_dn($oldinfo, 1);
575 $search =
"(".$newmembertype->_load_ldap_dn($oldinfo, 2).
")";
576 $records = $ldap->search($container, $search);
577 if (count($records) && $records[
'count'] == 0) {
581 $info = $newmembertype->_load_ldap_info();
582 $dn = $newmembertype->_load_ldap_dn($info);
584 $result = $ldap->update($dn, $info, $user, $olddn);
586 if ($object->oldcopy->typeid != $object->typeid) {
591 $oldmembertype->fetch($object->oldcopy->typeid);
592 $oldmembertype->listMembersForMemberType(
'', 1);
594 $oldinfo = $oldmembertype->_load_ldap_info();
595 $olddn = $oldmembertype->_load_ldap_dn($oldinfo);
598 $container = $oldmembertype->_load_ldap_dn($oldinfo, 1);
599 $search =
"(".$oldmembertype->_load_ldap_dn($oldinfo, 2).
")";
600 $records = $ldap->search($container, $search);
601 if (count($records) && $records[
'count'] == 0) {
605 $info = $oldmembertype->_load_ldap_info();
606 $dn = $oldmembertype->_load_ldap_dn($info);
608 $result = $ldap->update($dn, $info, $user, $olddn);
614 $this->errors[] =
"ErrorLDAP ".$ldap->error;
617 } elseif ($action ==
'MEMBER_NEW_PASSWORD') {
618 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
621 if (!empty($conf->global->LDAP_FIELD_PASSWORD) || !empty($conf->global->LDAP_FIELD_PASSWORD_CRYPTED)) {
623 $result = $ldap->connect_bind();
626 $info = $object->_load_ldap_info();
627 $dn = $object->_load_ldap_dn($info);
630 $result = $ldap->update($dn, $info, $user, $olddn);
634 $this->errors[] =
"ErrorLDAP ".$ldap->error;
638 } elseif ($action ==
'MEMBER_RESILIATE') {
639 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
642 if (!empty($conf->global->LDAP_FIELD_MEMBER_STATUS)) {
644 $result = $ldap->connect_bind();
647 $info = $object->_load_ldap_info();
648 $dn = $object->_load_ldap_dn($info);
651 $result = $ldap->update($dn, $info, $user, $olddn);
655 $this->errors[] =
"ErrorLDAP ".$ldap->error;
659 } elseif ($action ==
'MEMBER_DELETE') {
660 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
663 $result = $ldap->connect_bind();
666 $info = $object->_load_ldap_info();
667 $dn = $object->_load_ldap_dn($info);
669 $result = $ldap->delete($dn);
673 if ($object->typeid > 0) {
674 require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
680 $membertype->fetch($object->typeid);
681 $membertype->listMembersForMemberType(
'a.rowid != '.$object->id, 1);
683 $oldinfo = $membertype->_load_ldap_info();
684 $olddn = $membertype->_load_ldap_dn($oldinfo);
687 $container = $membertype->_load_ldap_dn($oldinfo, 1);
688 $search =
"(".$membertype->_load_ldap_dn($oldinfo, 2).
")";
689 $records = $ldap->search($container, $search);
690 if (count($records) && $records[
'count'] == 0) {
694 $info = $membertype->_load_ldap_info();
695 $dn = $membertype->_load_ldap_dn($info);
697 $result = $ldap->update($dn, $info, $user, $olddn);
703 $this->errors[] =
"ErrorLDAP ".$ldap->error;
706 } elseif ($action ==
'MEMBER_TYPE_CREATE') {
708 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
711 $result = $ldap->connect_bind();
714 $info = $object->_load_ldap_info();
715 $dn = $object->_load_ldap_dn($info);
718 if (in_array(
'posixGroup', $info[
'objectclass'])) {
719 $info[
'gidNumber'] = $ldap->getNextGroupGid(
'LDAP_KEY_MEMBERS_TYPE');
722 $result = $ldap->add($dn, $info, $user);
726 $this->errors[] =
"ErrorLDAP ".$ldap->error;
729 } elseif ($action ==
'MEMBER_TYPE_MODIFY') {
730 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
733 $result = $ldap->connect_bind();
736 if (empty($object->oldcopy) || !is_object($object->oldcopy)) {
737 dol_syslog(
"Trigger ".$action.
" was called by a function that did not set previously the property ->oldcopy onto object", LOG_WARNING);
738 $object->oldcopy = clone $object;
741 $object->oldcopy->listMembersForMemberType(
'', 1);
743 $oldinfo = $object->oldcopy->_load_ldap_info();
744 $olddn = $object->oldcopy->_load_ldap_dn($oldinfo);
747 $container = $object->oldcopy->_load_ldap_dn($oldinfo, 1);
748 $search =
"(".$object->oldcopy->_load_ldap_dn($oldinfo, 2).
")";
749 $records = $ldap->search($container, $search);
750 if (count($records) && $records[
'count'] == 0) {
754 $object->listMembersForMemberType(
'', 1);
756 $info = $object->_load_ldap_info();
757 $dn = $object->_load_ldap_dn($info);
759 $result = $ldap->update($dn, $info, $user, $olddn);
763 $this->errors[] =
"ErrorLDAP ".$ldap->error;
766 } elseif ($action ==
'MEMBER_TYPE_DELETE') {
767 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
770 $result = $ldap->connect_bind();
773 $info = $object->_load_ldap_info();
774 $dn = $object->_load_ldap_dn($info);
776 $result = $ldap->delete($dn);
780 $this->errors[] =
"ErrorLDAP ".$ldap->error;