PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/triggers/interface_modLdap_Ldapsynchro.class.php

https://github.com/asterix14/dolibarr
PHP | 536 lines | 401 code | 63 blank | 72 comment | 123 complexity | 006a0302d7370e195a3a7e0a1d69021a MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005-2007 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/triggers/interface_modLdap_Ldapsynchro.class.php
  19. * \ingroup core
  20. * \brief Fichier de gestion des triggers LDAP
  21. */
  22. require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
  23. /**
  24. * \class InterfaceLdapsynchro
  25. * \brief Class of triggers for ldap module
  26. */
  27. class InterfaceLdapsynchro
  28. {
  29. var $db;
  30. var $error;
  31. /**
  32. * Constructor.
  33. * @param DB Database handler
  34. */
  35. function InterfaceLdapsynchro($DB)
  36. {
  37. $this->db = $DB ;
  38. $this->name = preg_replace('/^Interface/i','',get_class($this));
  39. $this->family = "ldap";
  40. $this->description = "Triggers of this module allows to synchronize Dolibarr toward a LDAP database.";
  41. $this->version = 'dolibarr'; // 'experimental' or 'dolibarr' or version
  42. $this->picto = 'technic';
  43. }
  44. /**
  45. * Return name of trigger file
  46. * @return string Name of trigger file
  47. */
  48. function getName()
  49. {
  50. return $this->name;
  51. }
  52. /**
  53. * Return description of trigger file
  54. * @return string Description of trigger file
  55. */
  56. function getDesc()
  57. {
  58. return $this->description;
  59. }
  60. /**
  61. * Return version of trigger file
  62. * @return string Version of trigger file
  63. */
  64. function getVersion()
  65. {
  66. global $langs;
  67. $langs->load("admin");
  68. if ($this->version == 'experimental') return $langs->trans("Experimental");
  69. elseif ($this->version == 'dolibarr') return DOL_VERSION;
  70. elseif ($this->version) return $this->version;
  71. else return $langs->trans("Unknown");
  72. }
  73. /**
  74. * Function called when a Dolibarrr business event is done.
  75. * All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers
  76. * @param action Event code (COMPANY_CREATE, PROPAL_VALIDATE, ...)
  77. * @param object Object action is done on
  78. * @param user Object user
  79. * @param langs Object langs
  80. * @param conf Object conf
  81. * @return int <0 if KO, 0 if no action are done, >0 if OK
  82. */
  83. function run_trigger($action,$object,$user,$langs,$conf)
  84. {
  85. if (empty($conf->ldap->enabled)) return 0; // Module not active, we do nothing
  86. if (! function_exists('ldap_connect'))
  87. {
  88. dol_syslog("Warning, module LDAP is enabled but LDAP functions not available in this PHP", LOG_WARNING);
  89. return 0;
  90. }
  91. // Users
  92. if ($action == 'USER_CREATE')
  93. {
  94. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  95. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  96. {
  97. $ldap=new Ldap();
  98. $ldap->connect_bind();
  99. $info=$object->_load_ldap_info();
  100. $dn=$object->_load_ldap_dn($info);
  101. $result=$ldap->add($dn,$info,$user);
  102. if ($result < 0)
  103. {
  104. $this->error="ErrorLDAP"." ".$ldap->error;
  105. }
  106. return $result;
  107. }
  108. }
  109. elseif ($action == 'USER_MODIFY')
  110. {
  111. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  112. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  113. {
  114. $ldap=new Ldap();
  115. $ldap->connect_bind();
  116. $oldinfo=$object->oldcopy->_load_ldap_info();
  117. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  118. // Verify if entry exist
  119. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  120. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  121. $records=$ldap->search($container,$search);
  122. if (count($records) && $records['count'] == 0)
  123. {
  124. $olddn = '';
  125. }
  126. $info=$object->_load_ldap_info();
  127. $dn=$object->_load_ldap_dn($info);
  128. $result=$ldap->update($dn,$info,$user,$olddn);
  129. if ($result < 0)
  130. {
  131. $this->error="ErrorLDAP"." ".$ldap->error;
  132. }
  133. return $result;
  134. }
  135. }
  136. elseif ($action == 'USER_NEW_PASSWORD')
  137. {
  138. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  139. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  140. {
  141. $ldap=new Ldap();
  142. $ldap->connect_bind();
  143. $oldinfo=$object->oldcopy->_load_ldap_info();
  144. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  145. // Verify if entry exist
  146. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  147. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  148. $records=$ldap->search($container,$search);
  149. if (count($records) && $records['count'] == 0)
  150. {
  151. $olddn = '';
  152. }
  153. $info=$object->_load_ldap_info();
  154. $dn=$object->_load_ldap_dn($info);
  155. $result=$ldap->update($dn,$info,$user,$olddn);
  156. if ($result < 0)
  157. {
  158. $this->error="ErrorLDAP"." ".$ldap->error;
  159. }
  160. return $result;
  161. }
  162. }
  163. elseif ($action == 'USER_ENABLEDISABLE')
  164. {
  165. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  166. }
  167. elseif ($action == 'USER_DELETE')
  168. {
  169. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  170. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  171. {
  172. $ldap=new Ldap();
  173. $ldap->connect_bind();
  174. $info=$object->_load_ldap_info();
  175. $dn=$object->_load_ldap_dn($info);
  176. $result=$ldap->delete($dn,$info,$user);
  177. if ($result < 0)
  178. {
  179. $this->error="ErrorLDAP"." ".$ldap->error;
  180. }
  181. return $result;
  182. }
  183. }
  184. // Groupes
  185. elseif ($action == 'GROUP_CREATE')
  186. {
  187. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  188. {
  189. $ldap=new Ldap();
  190. $ldap->connect_bind();
  191. $info=$object->_load_ldap_info();
  192. $dn=$object->_load_ldap_dn($info);
  193. // Get a gid number for objectclass PosixGroup
  194. if(in_array('posixGroup',$info['objectclass']))
  195. $info['gidNumber'] = $ldap->getNextGroupGid();
  196. $result=$ldap->add($dn,$info,$user);
  197. if ($result < 0)
  198. {
  199. $this->error="ErrorLDAP"." ".$ldap->error;
  200. }
  201. return $result;
  202. }
  203. }
  204. elseif ($action == 'GROUP_MODIFY')
  205. {
  206. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  207. {
  208. $ldap=new Ldap();
  209. $ldap->connect_bind();
  210. $oldinfo=$object->oldcopy->_load_ldap_info();
  211. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  212. // Verify if entry exist
  213. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  214. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  215. $records=$ldap->search($container,$search);
  216. if (count($records) && $records['count'] == 0)
  217. {
  218. $olddn = '';
  219. }
  220. $info=$object->_load_ldap_info();
  221. $dn=$object->_load_ldap_dn($info);
  222. $result=$ldap->update($dn,$info,$user,$olddn);
  223. if ($result < 0)
  224. {
  225. $this->error="ErrorLDAP"." ".$ldap->error;
  226. }
  227. return $result;
  228. }
  229. }
  230. elseif ($action == 'GROUP_DELETE')
  231. {
  232. if ($conf->ldap->enabled && $conf->global->LDAP_SYNCHRO_ACTIVE == 'dolibarr2ldap')
  233. {
  234. $ldap=new Ldap();
  235. $ldap->connect_bind();
  236. $info=$object->_load_ldap_info();
  237. $dn=$object->_load_ldap_dn($info);
  238. $result=$ldap->delete($dn,$info,$user);
  239. if ($result < 0)
  240. {
  241. $this->error="ErrorLDAP"." ".$ldap->error;
  242. }
  243. return $result;
  244. }
  245. }
  246. // Contacts
  247. elseif ($action == 'CONTACT_CREATE')
  248. {
  249. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  250. if ($conf->ldap->enabled && $conf->global->LDAP_CONTACT_ACTIVE)
  251. {
  252. $ldap=new Ldap();
  253. $ldap->connect_bind();
  254. $info=$object->_load_ldap_info();
  255. $dn=$object->_load_ldap_dn($info);
  256. $result=$ldap->add($dn,$info,$user);
  257. if ($result < 0)
  258. {
  259. $this->error="ErrorLDAP"." ".$ldap->error;
  260. }
  261. return $result;
  262. }
  263. }
  264. elseif ($action == 'CONTACT_MODIFY')
  265. {
  266. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  267. if ($conf->ldap->enabled && $conf->global->LDAP_CONTACT_ACTIVE)
  268. {
  269. $ldap=new Ldap();
  270. $ldap->connect_bind();
  271. $oldinfo=$object->oldcopy->_load_ldap_info();
  272. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  273. // Verify if entry exist
  274. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  275. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  276. $records=$ldap->search($container,$search);
  277. if (count($records) && $records['count'] == 0)
  278. {
  279. $olddn = '';
  280. }
  281. $info=$object->_load_ldap_info();
  282. $dn=$object->_load_ldap_dn($info);
  283. $result=$ldap->update($dn,$info,$user,$olddn);
  284. if ($result < 0)
  285. {
  286. $this->error="ErrorLDAP"." ".$ldap->error;
  287. }
  288. return $result;
  289. }
  290. }
  291. elseif ($action == 'CONTACT_DELETE')
  292. {
  293. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  294. if ($conf->ldap->enabled && $conf->global->LDAP_CONTACT_ACTIVE)
  295. {
  296. $ldap=new Ldap();
  297. $ldap->connect_bind();
  298. $info=$object->_load_ldap_info();
  299. $dn=$object->_load_ldap_dn($info);
  300. $result=$ldap->delete($dn,$info,$user);
  301. if ($result < 0)
  302. {
  303. $this->error="ErrorLDAP"." ".$ldap->error;
  304. }
  305. return $result;
  306. }
  307. }
  308. // Members
  309. elseif ($action == 'MEMBER_CREATE')
  310. {
  311. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  312. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  313. {
  314. $ldap=new Ldap();
  315. $ldap->connect_bind();
  316. $info=$object->_load_ldap_info();
  317. $dn=$object->_load_ldap_dn($info);
  318. $result=$ldap->add($dn,$info,$user);
  319. if ($result < 0)
  320. {
  321. $this->error="ErrorLDAP"." ".$ldap->error;
  322. }
  323. return $result;
  324. }
  325. }
  326. elseif ($action == 'MEMBER_VALIDATE')
  327. {
  328. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  329. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  330. {
  331. // If status field is setup to be synchronized
  332. if ($conf->global->LDAP_FIELD_MEMBER_STATUS)
  333. {
  334. $ldap=new Ldap();
  335. $ldap->connect_bind();
  336. $info=$object->_load_ldap_info();
  337. $dn=$object->_load_ldap_dn($info);
  338. $olddn=$dn; // We know olddn=dn as we change only status
  339. $result=$ldap->update($dn,$info,$user,$olddn);
  340. if ($result < 0)
  341. {
  342. $this->error="ErrorLDAP"." ".$ldap->error;
  343. }
  344. return $result;
  345. }
  346. }
  347. }
  348. elseif ($action == 'MEMBER_SUBSCRIPTION')
  349. {
  350. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  351. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  352. {
  353. // If subscriptions fields are setup to be synchronized
  354. if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE
  355. || $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT
  356. || $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE
  357. || $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
  358. || $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)
  359. {
  360. $ldap=new Ldap();
  361. $ldap->connect_bind();
  362. $info=$object->_load_ldap_info();
  363. $dn=$object->_load_ldap_dn($info);
  364. $olddn=$dn; // We know olddn=dn as we change only subscriptions
  365. $result=$ldap->update($dn,$info,$user,$olddn);
  366. if ($result < 0)
  367. {
  368. $this->error="ErrorLDAP"." ".$ldap->error;
  369. }
  370. return $result;
  371. }
  372. }
  373. }
  374. elseif ($action == 'MEMBER_MODIFY')
  375. {
  376. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  377. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  378. {
  379. $ldap=new Ldap();
  380. $ldap->connect_bind();
  381. $oldinfo=$object->oldcopy->_load_ldap_info();
  382. $olddn=$object->oldcopy->_load_ldap_dn($oldinfo);
  383. // Verify if entry exist
  384. $container=$object->oldcopy->_load_ldap_dn($oldinfo,1);
  385. $search = "(".$object->oldcopy->_load_ldap_dn($oldinfo,2).")";
  386. $records=$ldap->search($container,$search);
  387. if (count($records) && $records['count'] == 0)
  388. {
  389. $olddn = '';
  390. }
  391. $info=$object->_load_ldap_info();
  392. $dn=$object->_load_ldap_dn($info);
  393. $result=$ldap->update($dn,$info,$user,$olddn);
  394. if ($result < 0)
  395. {
  396. $this->error="ErrorLDAP"." ".$ldap->error;
  397. }
  398. return $result;
  399. }
  400. }
  401. elseif ($action == 'MEMBER_NEW_PASSWORD')
  402. {
  403. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  404. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  405. {
  406. // If password field is setup to be synchronized
  407. if ($conf->global->LDAP_FIELD_PASSWORD || $conf->global->LDAP_FIELD_PASSWORD_CRYPTED)
  408. {
  409. $ldap=new Ldap();
  410. $ldap->connect_bind();
  411. $info=$object->_load_ldap_info();
  412. $dn=$object->_load_ldap_dn($info);
  413. $olddn=$dn; // We know olddn=dn as we change only password
  414. $result=$ldap->update($dn,$info,$user,$olddn);
  415. if ($result < 0)
  416. {
  417. $this->error="ErrorLDAP"." ".$ldap->error;
  418. }
  419. return $result;
  420. }
  421. }
  422. }
  423. elseif ($action == 'MEMBER_RESILIATE')
  424. {
  425. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  426. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  427. {
  428. # If status field is setup to be synchronized
  429. if ($conf->global->LDAP_FIELD_MEMBER_STATUS)
  430. {
  431. $ldap=new Ldap();
  432. $ldap->connect_bind();
  433. $info=$object->_load_ldap_info();
  434. $dn=$object->_load_ldap_dn($info);
  435. $olddn=$dn; // We know olddn=dn as we change only status
  436. $result=$ldap->update($dn,$info,$user,$olddn);
  437. if ($result < 0)
  438. {
  439. $this->error="ErrorLDAP"." ".$ldap->error;
  440. }
  441. return $result;
  442. }
  443. }
  444. }
  445. elseif ($action == 'MEMBER_DELETE')
  446. {
  447. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  448. if ($conf->ldap->enabled && $conf->global->LDAP_MEMBER_ACTIVE)
  449. {
  450. $ldap=new Ldap();
  451. $ldap->connect_bind();
  452. $info=$object->_load_ldap_info();
  453. $dn=$object->_load_ldap_dn($info);
  454. $result=$ldap->delete($dn,$info,$user);
  455. if ($result < 0)
  456. {
  457. $this->error="ErrorLDAP"." ".$ldap->error;
  458. }
  459. return $result;
  460. }
  461. }
  462. // If not found
  463. /*
  464. else
  465. {
  466. dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
  467. return -1;
  468. }
  469. */
  470. return 0;
  471. }
  472. }
  473. ?>