ldap.class.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * or see http://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/class/ldap.class.php
  23. * \brief File of class to manage LDAP features
  24. */
  25. /**
  26. * Class to manage LDAP features
  27. */
  28. class Ldap
  29. {
  30. var $error;
  31. /**
  32. * Tableau des serveurs (IP addresses ou nom d'hotes)
  33. */
  34. var $server=array();
  35. /**
  36. * Base DN (e.g. "dc=foo,dc=com")
  37. */
  38. var $dn;
  39. /**
  40. * type de serveur, actuellement OpenLdap et Active Directory
  41. */
  42. var $serverType;
  43. /**
  44. * Version du protocole ldap
  45. */
  46. var $domain;
  47. /**
  48. * User administrateur Ldap
  49. * Active Directory ne supporte pas les connexions anonymes
  50. */
  51. var $searchUser;
  52. /**
  53. * Mot de passe de l'administrateur
  54. * Active Directory ne supporte pas les connexions anonymes
  55. */
  56. var $searchPassword;
  57. /**
  58. * DN des utilisateurs
  59. */
  60. var $people;
  61. /**
  62. * DN des groupes
  63. */
  64. var $groups;
  65. /**
  66. * Code erreur retourne par le serveur Ldap
  67. */
  68. var $ldapErrorCode;
  69. /**
  70. * Message texte de l'erreur
  71. */
  72. var $ldapErrorText;
  73. //Fetch user
  74. var $name;
  75. var $firstname;
  76. var $login;
  77. var $phone;
  78. var $skype;
  79. var $fax;
  80. var $mail;
  81. var $mobile;
  82. var $uacf;
  83. var $pwdlastset;
  84. var $ldapcharset='UTF-8'; // LDAP should be UTF-8 encoded
  85. /**
  86. * The internal LDAP connection handle
  87. */
  88. var $connection;
  89. /**
  90. * Result of any connections etc.
  91. */
  92. var $result;
  93. /**
  94. * Constructor
  95. */
  96. function __construct()
  97. {
  98. global $conf;
  99. // Server
  100. if (! empty($conf->global->LDAP_SERVER_HOST)) $this->server[] = $conf->global->LDAP_SERVER_HOST;
  101. if (! empty($conf->global->LDAP_SERVER_HOST_SLAVE)) $this->server[] = $conf->global->LDAP_SERVER_HOST_SLAVE;
  102. $this->serverPort = $conf->global->LDAP_SERVER_PORT;
  103. $this->ldapProtocolVersion = $conf->global->LDAP_SERVER_PROTOCOLVERSION;
  104. $this->dn = $conf->global->LDAP_SERVER_DN;
  105. $this->serverType = $conf->global->LDAP_SERVER_TYPE;
  106. $this->domain = $conf->global->LDAP_SERVER_DN;
  107. $this->searchUser = $conf->global->LDAP_ADMIN_DN;
  108. $this->searchPassword = $conf->global->LDAP_ADMIN_PASS;
  109. $this->people = $conf->global->LDAP_USER_DN;
  110. $this->groups = $conf->global->LDAP_GROUP_DN;
  111. $this->filter = $conf->global->LDAP_FILTER_CONNECTION; // Filter on user
  112. $this->filtermember = $conf->global->LDAP_MEMBER_FILTER; // Filter on member
  113. // Users
  114. $this->attr_login = $conf->global->LDAP_FIELD_LOGIN; //unix
  115. $this->attr_sambalogin = $conf->global->LDAP_FIELD_LOGIN_SAMBA; //samba, activedirectory
  116. $this->attr_name = $conf->global->LDAP_FIELD_NAME;
  117. $this->attr_firstname = $conf->global->LDAP_FIELD_FIRSTNAME;
  118. $this->attr_mail = $conf->global->LDAP_FIELD_MAIL;
  119. $this->attr_phone = $conf->global->LDAP_FIELD_PHONE;
  120. $this->attr_skype = $conf->global->LDAP_FIELD_SKYPE;
  121. $this->attr_fax = $conf->global->LDAP_FIELD_FAX;
  122. $this->attr_mobile = $conf->global->LDAP_FIELD_MOBILE;
  123. }
  124. // Connection handling methods -------------------------------------------
  125. /**
  126. * Connect and bind
  127. * Use this->server, this->serverPort, this->ldapProtocolVersion, this->serverType, this->searchUser, this->searchPassword
  128. * After return, this->connection and $this->bind are defined
  129. *
  130. * @return int <0 if KO, 1 if bind anonymous, 2 if bind auth
  131. */
  132. function connect_bind()
  133. {
  134. global $langs, $conf;
  135. $connected=0;
  136. $this->bind=0;
  137. // Check parameters
  138. if (count($this->server) == 0 || empty($this->server[0]))
  139. {
  140. $this->error='LDAP setup (file conf.php) is not complete';
  141. dol_syslog(get_class($this)."::connect_bind ".$this->error, LOG_WARNING);
  142. return -1;
  143. }
  144. if (! function_exists("ldap_connect"))
  145. {
  146. $this->error='LDAPFunctionsNotAvailableOnPHP';
  147. dol_syslog(get_class($this)."::connect_bind ".$this->error, LOG_WARNING);
  148. $return=-1;
  149. }
  150. if (empty($this->error))
  151. {
  152. // Loop on each ldap server
  153. foreach ($this->server as $key => $host)
  154. {
  155. if ($connected) break;
  156. if (empty($host)) continue;
  157. if (preg_match('/^ldap/',$host))
  158. {
  159. $this->connection = ldap_connect($host);
  160. }
  161. else
  162. {
  163. $this->connection = ldap_connect($host,$this->serverPort);
  164. }
  165. if (is_resource($this->connection))
  166. {
  167. // Begin TLS if requested by the configuration
  168. if (! empty($conf->global->LDAP_SERVER_USE_TLS))
  169. {
  170. if (! ldap_start_tls($this->connection))
  171. {
  172. dol_syslog(get_class($this)."::connect_bind failed to start tls", LOG_WARNING);
  173. $connected = 0;
  174. $this->close();
  175. }
  176. }
  177. // Execute the ldap_set_option here (after connect and before bind)
  178. $this->setVersion();
  179. ldap_set_option($this->connection, LDAP_OPT_SIZELIMIT, 0); // no limit here. should return true.
  180. if ($this->serverType == "activedirectory")
  181. {
  182. $result=$this->setReferrals();
  183. dol_syslog(get_class($this)."::connect_bind try bindauth for activedirectory on ".$host." user=".$this->searchUser." password=".preg_replace('/./','*',$this->searchPassword),LOG_DEBUG);
  184. $this->result=$this->bindauth($this->searchUser,$this->searchPassword);
  185. if ($this->result)
  186. {
  187. $this->bind=$this->result;
  188. $connected=2;
  189. break;
  190. }
  191. else
  192. {
  193. $this->error=ldap_errno($this->connection).' '.ldap_error($this->connection);
  194. }
  195. }
  196. else
  197. {
  198. // Try in auth mode
  199. if ($this->searchUser && $this->searchPassword)
  200. {
  201. dol_syslog(get_class($this)."::connect_bind try bindauth on ".$host." user=".$this->searchUser." password=".preg_replace('/./','*',$this->searchPassword),LOG_DEBUG);
  202. $this->result=$this->bindauth($this->searchUser,$this->searchPassword);
  203. if ($this->result)
  204. {
  205. $this->bind=$this->result;
  206. $connected=2;
  207. break;
  208. }
  209. else
  210. {
  211. $this->error=ldap_errno($this->connection).' '.ldap_error($this->connection);
  212. }
  213. }
  214. // Try in anonymous
  215. if (! $this->bind)
  216. {
  217. dol_syslog(get_class($this)."::connect_bind try bind on ".$host,LOG_DEBUG);
  218. $result=$this->bind();
  219. if ($result)
  220. {
  221. $this->bind=$this->result;
  222. $connected=1;
  223. break;
  224. }
  225. else
  226. {
  227. $this->error=ldap_errno($this->connection).' '.ldap_error($this->connection);
  228. }
  229. }
  230. }
  231. }
  232. if (! $connected) $this->close();
  233. }
  234. }
  235. if ($connected)
  236. {
  237. $return=$connected;
  238. dol_syslog(get_class($this)."::connect_bind return=".$return, LOG_DEBUG);
  239. }
  240. else
  241. {
  242. $this->error='Failed to connect to LDAP'.($this->error?': '.$this->error:'');
  243. $return=-1;
  244. dol_syslog(get_class($this)."::connect_bind return=".$return.' - '.$this->error, LOG_WARNING);
  245. }
  246. return $return;
  247. }
  248. /**
  249. * Simply closes the connection set up earlier.
  250. * Returns true if OK, false if there was an error.
  251. *
  252. * @return boolean true or false
  253. */
  254. function close()
  255. {
  256. if ($this->connection && ! @ldap_close($this->connection))
  257. {
  258. return false;
  259. }
  260. else
  261. {
  262. return true;
  263. }
  264. }
  265. /**
  266. * Anonymously binds to the connection. After this is done,
  267. * queries and searches can be done - but read-only.
  268. *
  269. * @return boolean true or false
  270. */
  271. function bind()
  272. {
  273. if (! $this->result=@ldap_bind($this->connection))
  274. {
  275. $this->ldapErrorCode = ldap_errno($this->connection);
  276. $this->ldapErrorText = ldap_error($this->connection);
  277. $this->error=$this->ldapErrorCode." ".$this->ldapErrorText;
  278. return false;
  279. }
  280. else
  281. {
  282. return true;
  283. }
  284. }
  285. /**
  286. * Binds as an authenticated user, which usually allows for write
  287. * access. The FULL dn must be passed. For a directory manager, this is
  288. * "cn=Directory Manager" under iPlanet. For a user, it will be something
  289. * like "uid=jbloggs,ou=People,dc=foo,dc=com".
  290. *
  291. * @param string $bindDn DN
  292. * @param string $pass Password
  293. * @return boolean true or false
  294. */
  295. function bindauth($bindDn,$pass)
  296. {
  297. if (! $this->result = @ldap_bind($this->connection, $bindDn, $pass))
  298. {
  299. $this->ldapErrorCode = ldap_errno($this->connection);
  300. $this->ldapErrorText = ldap_error($this->connection);
  301. $this->error=$this->ldapErrorCode." ".$this->ldapErrorText;
  302. return false;
  303. }
  304. else
  305. {
  306. return true;
  307. }
  308. }
  309. /**
  310. * Unbind du serveur ldap.
  311. *
  312. * @return boolean true or false
  313. */
  314. function unbind()
  315. {
  316. if (!$this->result=@ldap_unbind($this->connection))
  317. {
  318. return false;
  319. } else {
  320. return true;
  321. }
  322. }
  323. /**
  324. * Verification de la version du serveur ldap.
  325. *
  326. * @return string version
  327. */
  328. function getVersion()
  329. {
  330. $version = 0;
  331. $version = @ldap_get_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $version);
  332. return $version;
  333. }
  334. /**
  335. * Change ldap protocol version to use.
  336. *
  337. * @return boolean version
  338. */
  339. function setVersion() {
  340. // LDAP_OPT_PROTOCOL_VERSION est une constante qui vaut 17
  341. $ldapsetversion = ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $this->ldapProtocolVersion);
  342. return $ldapsetversion;
  343. }
  344. /**
  345. * changement du referrals.
  346. *
  347. * @return boolean referrals
  348. */
  349. function setReferrals() {
  350. // LDAP_OPT_REFERRALS est une constante qui vaut ?
  351. $ldapreferrals = ldap_set_option($this->connection, LDAP_OPT_REFERRALS, 0);
  352. return $ldapreferrals;
  353. }
  354. /**
  355. * Add a LDAP entry
  356. * Ldap object connect and bind must have been done
  357. *
  358. * @param string $dn DN entry key
  359. * @param array $info Attributes array
  360. * @param User $user Objet user that create
  361. * @return int <0 if KO, >0 if OK
  362. */
  363. function add($dn, $info, $user)
  364. {
  365. global $conf;
  366. dol_syslog(get_class($this)."::add dn=".$dn." info=".join(',',$info));
  367. // Check parameters
  368. if (! $this->connection)
  369. {
  370. $this->error="NotConnected";
  371. return -2;
  372. }
  373. if (! $this->bind)
  374. {
  375. $this->error="NotConnected";
  376. return -3;
  377. }
  378. // Encode to LDAP page code
  379. $dn=$this->convFromOutputCharset($dn,$this->ldapcharset);
  380. foreach($info as $key => $val)
  381. {
  382. if (! is_array($val)) $info[$key]=$this->convFromOutputCharset($val,$this->ldapcharset);
  383. }
  384. $this->dump($dn,$info);
  385. //print_r($info);
  386. $result=@ldap_add($this->connection, $dn, $info);
  387. if ($result)
  388. {
  389. dol_syslog(get_class($this)."::add successfull", LOG_DEBUG);
  390. return 1;
  391. }
  392. else
  393. {
  394. $this->ldapErrorCode = @ldap_errno($this->connection);
  395. $this->ldapErrorText = @ldap_error($this->connection);
  396. $this->error=$this->ldapErrorCode." ".$this->ldapErrorText;
  397. dol_syslog(get_class($this)."::add failed: ".$this->error, LOG_ERR);
  398. return -1;
  399. }
  400. }
  401. /**
  402. * Modify a LDAP entry
  403. * Ldap object connect and bind must have been done
  404. *
  405. * @param string $dn DN entry key
  406. * @param array $info Attributes array
  407. * @param string $user Objet user that modify
  408. * @return int <0 if KO, >0 if OK
  409. */
  410. function modify($dn, $info, $user)
  411. {
  412. global $conf;
  413. dol_syslog(get_class($this)."::modify dn=".$dn." info=".join(',',$info));
  414. // Check parameters
  415. if (! $this->connection)
  416. {
  417. $this->error="NotConnected";
  418. return -2;
  419. }
  420. if (! $this->bind)
  421. {
  422. $this->error="NotConnected";
  423. return -3;
  424. }
  425. // Encode to LDAP page code
  426. $dn=$this->convFromOutputCharset($dn,$this->ldapcharset);
  427. foreach($info as $key => $val)
  428. {
  429. if (! is_array($val)) $info[$key]=$this->convFromOutputCharset($val,$this->ldapcharset);
  430. }
  431. $this->dump($dn,$info);
  432. //print_r($info);
  433. $result=@ldap_modify($this->connection, $dn, $info);
  434. if ($result)
  435. {
  436. dol_syslog(get_class($this)."::modify successfull", LOG_DEBUG);
  437. return 1;
  438. }
  439. else
  440. {
  441. $this->error=@ldap_error($this->connection);
  442. dol_syslog(get_class($this)."::modify failed: ".$this->error, LOG_ERR);
  443. return -1;
  444. }
  445. }
  446. /**
  447. * Modify a LDAP entry (to use if dn != olddn)
  448. * Ldap object connect and bind must have been done
  449. *
  450. * @param string $dn DN entry key
  451. * @param array $info Attributes array
  452. * @param User $user Objet user that update
  453. * @param string $olddn Old DN entry key (before update)
  454. * @return int <0 if KO, >0 if OK
  455. */
  456. function update($dn,$info,$user,$olddn)
  457. {
  458. global $conf;
  459. dol_syslog(get_class($this)."::update dn=".$dn." olddn=".$olddn);
  460. // Check parameters
  461. if (! $this->connection)
  462. {
  463. $this->error="NotConnected";
  464. return -2;
  465. }
  466. if (! $this->bind)
  467. {
  468. $this->error="NotConnected";
  469. return -3;
  470. }
  471. if (! $olddn || $olddn != $dn)
  472. {
  473. // If change we make is rename the key of LDAP record, we create new one and if ok, we delete old one.
  474. $result = $this->add($dn, $info, $user);
  475. if ($result > 0 && $olddn && $olddn != $dn) $result = $this->delete($olddn); // If add fails, we do not try to delete old one
  476. }
  477. else
  478. {
  479. //$result = $this->delete($olddn);
  480. $result = $this->add($dn, $info, $user); // If record has been deleted from LDAP, we recreate it. We ignore error if it already exists.
  481. $result = $this->modify($dn, $info, $user); // We use add/modify instead of delete/add when olddn is received
  482. }
  483. if ($result <= 0)
  484. {
  485. $this->error = ldap_errno($this->connection)." ".ldap_error($this->connection)." ".$this->error;
  486. dol_syslog(get_class($this)."::update ".$this->error,LOG_ERR);
  487. //print_r($info);
  488. return -1;
  489. }
  490. else
  491. {
  492. dol_syslog(get_class($this)."::update done successfully");
  493. return 1;
  494. }
  495. }
  496. /**
  497. * Delete a LDAP entry
  498. * Ldap object connect and bind must have been done
  499. *
  500. * @param string $dn DN entry key
  501. * @return int <0 if KO, >0 if OK
  502. */
  503. function delete($dn)
  504. {
  505. global $conf;
  506. dol_syslog(get_class($this)."::delete Delete LDAP entry dn=".$dn);
  507. // Check parameters
  508. if (! $this->connection)
  509. {
  510. $this->error="NotConnected";
  511. return -2;
  512. }
  513. if (! $this->bind)
  514. {
  515. $this->error="NotConnected";
  516. return -3;
  517. }
  518. // Encode to LDAP page code
  519. $dn=$this->convFromOutputCharset($dn,$this->ldapcharset);
  520. $result=@ldap_delete($this->connection, $dn);
  521. if ($result) return 1;
  522. return -1;
  523. }
  524. /**
  525. * Build a LDAP message
  526. *
  527. * @param string $dn DN entry key
  528. * @param array $info Attributes array
  529. * @return string Content of file
  530. */
  531. function dump_content($dn, $info)
  532. {
  533. $content='';
  534. // Create file content
  535. if (preg_match('/^ldap/',$this->server[0]))
  536. {
  537. $target="-H ".join(',',$this->server);
  538. }
  539. else
  540. {
  541. $target="-h ".join(',',$this->server)." -p ".$this->serverPort;
  542. }
  543. $content.="# ldapadd $target -c -v -D ".$this->searchUser." -W -f ldapinput.in\n";
  544. $content.="# ldapmodify $target -c -v -D ".$this->searchUser." -W -f ldapinput.in\n";
  545. $content.="# ldapdelete $target -c -v -D ".$this->searchUser." -W -f ldapinput.in\n";
  546. if (in_array('localhost',$this->server)) $content.="# If commands fails to connect, try without -h and -p\n";
  547. $content.="dn: ".$dn."\n";
  548. foreach($info as $key => $value)
  549. {
  550. if (! is_array($value))
  551. {
  552. $content.="$key: $value\n";
  553. }
  554. else
  555. {
  556. foreach($value as $valuekey => $valuevalue)
  557. {
  558. $content.="$key: $valuevalue\n";
  559. }
  560. }
  561. }
  562. return $content;
  563. }
  564. /**
  565. * Dump a LDAP message to ldapinput.in file
  566. *
  567. * @param string $dn DN entry key
  568. * @param array $info Attributes array
  569. * @return int <0 if KO, >0 if OK
  570. */
  571. function dump($dn, $info)
  572. {
  573. global $conf;
  574. // Create content
  575. $content=$this->dump_content($dn, $info);
  576. //Create file
  577. $result=dol_mkdir($conf->ldap->dir_temp);
  578. $outputfile=$conf->ldap->dir_temp.'/ldapinput.in';
  579. $fp=fopen($outputfile,"w");
  580. if ($fp)
  581. {
  582. fputs($fp, $content);
  583. fclose($fp);
  584. if (! empty($conf->global->MAIN_UMASK))
  585. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  586. return 1;
  587. }
  588. else
  589. {
  590. return -1;
  591. }
  592. }
  593. // Attribute methods -----------------------------------------------------
  594. /**
  595. * Add a LDAP attribute in entry
  596. * Ldap object connect and bind must have been done
  597. *
  598. * @param string $dn DN entry key
  599. * @param array $info Attributes array
  600. * @param User $user Objet user that create
  601. * @return int <0 if KO, >0 if OK
  602. */
  603. function addAttribute($dn, $info, $user)
  604. {
  605. global $conf;
  606. dol_syslog(get_class($this)."::addAttribute dn=".$dn." info=".join(',',$info));
  607. // Check parameters
  608. if (! $this->connection)
  609. {
  610. $this->error="NotConnected";
  611. return -2;
  612. }
  613. if (! $this->bind)
  614. {
  615. $this->error="NotConnected";
  616. return -3;
  617. }
  618. // Encode to LDAP page code
  619. $dn=$this->convFromOutputCharset($dn,$this->ldapcharset);
  620. foreach($info as $key => $val)
  621. {
  622. if (! is_array($val)) $info[$key]=$this->convFromOutputCharset($val,$this->ldapcharset);
  623. }
  624. $this->dump($dn,$info);
  625. //print_r($info);
  626. $result=@ldap_mod_add($this->connection, $dn, $info);
  627. if ($result)
  628. {
  629. dol_syslog(get_class($this)."::add_attribute successfull", LOG_DEBUG);
  630. return 1;
  631. }
  632. else
  633. {
  634. $this->error=@ldap_error($this->connection);
  635. dol_syslog(get_class($this)."::add_attribute failed: ".$this->error, LOG_ERR);
  636. return -1;
  637. }
  638. }
  639. /**
  640. * Update a LDAP attribute in entry
  641. * Ldap object connect and bind must have been done
  642. *
  643. * @param string $dn DN entry key
  644. * @param array $info Attributes array
  645. * @param User $user Objet user that create
  646. * @return int <0 if KO, >0 if OK
  647. */
  648. function updateAttribute($dn, $info, $user)
  649. {
  650. global $conf;
  651. dol_syslog(get_class($this)."::updateAttribute dn=".$dn." info=".join(',',$info));
  652. // Check parameters
  653. if (! $this->connection)
  654. {
  655. $this->error="NotConnected";
  656. return -2;
  657. }
  658. if (! $this->bind)
  659. {
  660. $this->error="NotConnected";
  661. return -3;
  662. }
  663. // Encode to LDAP page code
  664. $dn=$this->convFromOutputCharset($dn,$this->ldapcharset);
  665. foreach($info as $key => $val)
  666. {
  667. if (! is_array($val)) $info[$key]=$this->convFromOutputCharset($val,$this->ldapcharset);
  668. }
  669. $this->dump($dn,$info);
  670. //print_r($info);
  671. $result=@ldap_mod_replace($this->connection, $dn, $info);
  672. if ($result)
  673. {
  674. dol_syslog(get_class($this)."::updateAttribute successfull", LOG_DEBUG);
  675. return 1;
  676. }
  677. else
  678. {
  679. $this->error=@ldap_error($this->connection);
  680. dol_syslog(get_class($this)."::updateAttribute failed: ".$this->error, LOG_ERR);
  681. return -1;
  682. }
  683. }
  684. /**
  685. * Delete a LDAP attribute in entry
  686. * Ldap object connect and bind must have been done
  687. *
  688. * @param string $dn DN entry key
  689. * @param array $info Attributes array
  690. * @param User $user Objet user that create
  691. * @return int <0 if KO, >0 if OK
  692. */
  693. function deleteAttribute($dn, $info, $user)
  694. {
  695. global $conf;
  696. dol_syslog(get_class($this)."::deleteAttribute dn=".$dn." info=".join(',',$info));
  697. // Check parameters
  698. if (! $this->connection)
  699. {
  700. $this->error="NotConnected";
  701. return -2;
  702. }
  703. if (! $this->bind)
  704. {
  705. $this->error="NotConnected";
  706. return -3;
  707. }
  708. // Encode to LDAP page code
  709. $dn=$this->convFromOutputCharset($dn,$this->ldapcharset);
  710. foreach($info as $key => $val)
  711. {
  712. if (! is_array($val)) $info[$key]=$this->convFromOutputCharset($val,$this->ldapcharset);
  713. }
  714. $this->dump($dn,$info);
  715. //print_r($info);
  716. $result=@ldap_mod_del($this->connection, $dn, $info);
  717. if ($result)
  718. {
  719. dol_syslog(get_class($this)."::deleteAttribute successfull", LOG_DEBUG);
  720. return 1;
  721. }
  722. else
  723. {
  724. $this->error=@ldap_error($this->connection);
  725. dol_syslog(get_class($this)."::deleteAttribute failed: ".$this->error, LOG_ERR);
  726. return -1;
  727. }
  728. }
  729. /**
  730. * Returns an array containing attributes and values for first record
  731. *
  732. * @param string $dn DN entry key
  733. * @param string $filter Filter
  734. * @return int|array <0 or false if KO, array if OK
  735. */
  736. function getAttribute($dn,$filter)
  737. {
  738. // Check parameters
  739. if (! $this->connection)
  740. {
  741. $this->error="NotConnected";
  742. return -2;
  743. }
  744. if (! $this->bind)
  745. {
  746. $this->error="NotConnected";
  747. return -3;
  748. }
  749. $search = ldap_search($this->connection,$dn,$filter);
  750. // Only one entry should ever be returned
  751. $entry = ldap_first_entry($this->connection, $search);
  752. if (!$entry)
  753. {
  754. $this->ldapErrorCode = -1;
  755. $this->ldapErrorText = "Couldn't find entry";
  756. return 0; // Couldn't find entry...
  757. }
  758. // Get values
  759. if (! $values = ldap_get_attributes($this->connection, $entry))
  760. {
  761. $this->ldapErrorCode = ldap_errno($this->connection);
  762. $this->ldapErrorText = ldap_error($this->connection);
  763. return 0; // No matching attributes
  764. }
  765. // Return an array containing the attributes.
  766. return $values;
  767. }
  768. /**
  769. * Returns an array containing values for an attribute and for first record matching filterrecord
  770. *
  771. * @param string $filterrecord Record
  772. * @param string $attribute Attributes
  773. * @return void
  774. */
  775. function getAttributeValues($filterrecord,$attribute)
  776. {
  777. $attributes=array();
  778. $attributes[0] = $attribute;
  779. // We need to search for this user in order to get their entry.
  780. $this->result = @ldap_search($this->connection,$this->people,$filterrecord,$attributes);
  781. // Pourquoi cette ligne ?
  782. //$info = ldap_get_entries($this->connection, $this->result);
  783. // Only one entry should ever be returned (no user will have the same uid)
  784. $entry = ldap_first_entry($this->connection, $this->result);
  785. if (!$entry)
  786. {
  787. $this->ldapErrorCode = -1;
  788. $this->ldapErrorText = "Couldn't find user";
  789. return false; // Couldn't find the user...
  790. }
  791. // Get values
  792. if (! $values = @ldap_get_values($this->connection, $entry, $attribute))
  793. {
  794. $this->ldapErrorCode = ldap_errno($this->connection);
  795. $this->ldapErrorText = ldap_error($this->connection);
  796. return false; // No matching attributes
  797. }
  798. // Return an array containing the attributes.
  799. return $values;
  800. }
  801. /**
  802. * Returns an array containing a details or list of LDAP record(s)
  803. * ldapsearch -LLLx -hlocalhost -Dcn=admin,dc=parinux,dc=org -w password -b "ou=adherents,ou=people,dc=parinux,dc=org" userPassword
  804. *
  805. * @param string $search Value of fiel to search, '*' for all. Not used if $activefilter is set.
  806. * @param string $userDn DN (Ex: ou=adherents,ou=people,dc=parinux,dc=org)
  807. * @param string $useridentifier Name of key field (Ex: uid)
  808. * @param array $attributeArray Array of fields required. Note this array must also contains field $useridentifier (Ex: sn,userPassword)
  809. * @param int $activefilter '1' or 'user'=use field this->filter as filter instead of parameter $search, 'member'=use field this->filtermember as filter
  810. * @param array $attributeAsArray Array of fields wanted as an array not a string
  811. * @return array Array of [id_record][ldap_field]=value
  812. */
  813. function getRecords($search, $userDn, $useridentifier, $attributeArray, $activefilter=0, $attributeAsArray=array())
  814. {
  815. $fulllist=array();
  816. dol_syslog(get_class($this)."::getRecords search=".$search." userDn=".$userDn." useridentifier=".$useridentifier." attributeArray=array(".join(',',$attributeArray).") activefilter=".$activefilter);
  817. // if the directory is AD, then bind first with the search user first
  818. if ($this->serverType == "activedirectory")
  819. {
  820. $this->bindauth($this->searchUser, $this->searchPassword);
  821. dol_syslog(get_class($this)."::bindauth serverType=activedirectory searchUser=".$this->searchUser);
  822. }
  823. // Define filter
  824. if (! empty($activefilter))
  825. {
  826. if (((string) $activefilter == '1' || (string) $activefilter == 'user') && $this->filter)
  827. {
  828. $filter = '('.$this->filter.')';
  829. }
  830. elseif (((string) $activefilter == 'member') && $this->filter)
  831. {
  832. $filter = '('.$this->filtermember.')';
  833. }
  834. else // If this->filter is empty, make fiter on * (all)
  835. {
  836. $filter = '('.$useridentifier.'=*)';
  837. }
  838. }
  839. else
  840. {
  841. $filter = '('.$useridentifier.'='.$search.')';
  842. }
  843. if (is_array($attributeArray))
  844. {
  845. // Return list with required fields
  846. $attributeArray=array_values($attributeArray); // This is to force to have index reordered from 0 (not make ldap_search fails)
  847. dol_syslog(get_class($this)."::getRecords connection=".$this->connection." userDn=".$userDn." filter=".$filter. " attributeArray=(".join(',',$attributeArray).")");
  848. //var_dump($attributeArray);
  849. $this->result = @ldap_search($this->connection, $userDn, $filter, $attributeArray);
  850. }
  851. else
  852. {
  853. // Return list with fields selected by default
  854. dol_syslog(get_class($this)."::getRecords connection=".$this->connection." userDn=".$userDn." filter=".$filter);
  855. $this->result = @ldap_search($this->connection, $userDn, $filter);
  856. }
  857. if (!$this->result)
  858. {
  859. $this->error = 'LDAP search failed: '.ldap_errno($this->connection)." ".ldap_error($this->connection);
  860. return -1;
  861. }
  862. $info = @ldap_get_entries($this->connection, $this->result);
  863. // Warning: Dans info, les noms d'attributs sont en minuscule meme si passe
  864. // a ldap_search en majuscule !!!
  865. //print_r($info);
  866. for ($i = 0; $i < $info["count"]; $i++)
  867. {
  868. $recordid=$this->convToOutputCharset($info[$i][$useridentifier][0],$this->ldapcharset);
  869. if ($recordid)
  870. {
  871. //print "Found record with key $useridentifier=".$recordid."<br>\n";
  872. $fulllist[$recordid][$useridentifier]=$recordid;
  873. // Add to the array for each attribute in my list
  874. $num = count($attributeArray);
  875. for ($j = 0; $j < $num; $j++)
  876. {
  877. $keyattributelower=strtolower($attributeArray[$j]);
  878. //print " Param ".$attributeArray[$j]."=".$info[$i][$keyattributelower][0]."<br>\n";
  879. //permet de recuperer le SID avec Active Directory
  880. if ($this->serverType == "activedirectory" && $keyattributelower == "objectsid")
  881. {
  882. $objectsid = $this->getObjectSid($recordid);
  883. $fulllist[$recordid][$attributeArray[$j]] = $objectsid;
  884. }
  885. else
  886. {
  887. if(in_array($attributeArray[$j], $attributeAsArray) && is_array($info[$i][$keyattributelower])) {
  888. $valueTab = array();
  889. foreach($info[$i][$keyattributelower] as $key => $value) {
  890. $valueTab[$key] = $this->convToOutputCharset($value,$this->ldapcharset);
  891. }
  892. $fulllist[$recordid][$attributeArray[$j]] = $valueTab;
  893. } else {
  894. $fulllist[$recordid][$attributeArray[$j]] = $this->convToOutputCharset($info[$i][$keyattributelower][0],$this->ldapcharset);
  895. }
  896. }
  897. }
  898. }
  899. }
  900. asort($fulllist);
  901. return $fulllist;
  902. }
  903. /**
  904. * Converts a little-endian hex-number to one, that 'hexdec' can convert
  905. * Required by Active Directory
  906. *
  907. * @param string $hex Hex value
  908. * @return string Little endian
  909. */
  910. function littleEndian($hex)
  911. {
  912. for ($x=dol_strlen($hex)-2; $x >= 0; $x=$x-2) {
  913. $result .= substr($hex,$x,2);
  914. }
  915. return $result;
  916. }
  917. /**
  918. * Recupere le SID de l'utilisateur
  919. * Required by Active Directory
  920. *
  921. * @param string $ldapUser Login de l'utilisateur
  922. * @return string Sid
  923. */
  924. function getObjectSid($ldapUser)
  925. {
  926. $criteria = '('.$this->getUserIdentifier().'='.$ldapUser.')';
  927. $justthese = array("objectsid");
  928. // if the directory is AD, then bind first with the search user first
  929. if ($this->serverType == "activedirectory")
  930. {
  931. $this->bindauth($this->searchUser, $this->searchPassword);
  932. }
  933. $i = 0;
  934. $searchDN = $this->people;
  935. while ($i <= 2)
  936. {
  937. $ldapSearchResult = @ldap_search($this->connection, $searchDN, $criteria, $justthese);
  938. if (!$ldapSearchResult)
  939. {
  940. $this->error = ldap_errno($this->connection)." ".ldap_error($this->connection);
  941. return -1;
  942. }
  943. $entry = ldap_first_entry($this->connection, $ldapSearchResult);
  944. if (!$entry)
  945. {
  946. // Si pas de resultat on cherche dans le domaine
  947. $searchDN = $this->domain;
  948. $i++;
  949. }
  950. else
  951. {
  952. $i++;
  953. $i++;
  954. }
  955. }
  956. if ($entry)
  957. {
  958. $ldapBinary = ldap_get_values_len($this->connection, $entry, "objectsid");
  959. $SIDText = $this->binSIDtoText($ldapBinary[0]);
  960. return $SIDText;
  961. }
  962. else
  963. {
  964. $this->error = ldap_errno($this->connection)." ".ldap_error($this->connection);
  965. return '?';
  966. }
  967. }
  968. /**
  969. * Returns the textual SID
  970. * Indispensable pour Active Directory
  971. *
  972. * @param string $binsid Binary SID
  973. * @return string Textual SID
  974. */
  975. function binSIDtoText($binsid)
  976. {
  977. $hex_sid=bin2hex($binsid);
  978. $rev = hexdec(substr($hex_sid,0,2)); // Get revision-part of SID
  979. $subcount = hexdec(substr($hex_sid,2,2)); // Get count of sub-auth entries
  980. $auth = hexdec(substr($hex_sid,4,12)); // SECURITY_NT_AUTHORITY
  981. $result = "$rev-$auth";
  982. for ($x=0;$x < $subcount; $x++)
  983. {
  984. $result .= "-".hexdec($this->littleEndian(substr($hex_sid,16+($x*8),8))); // get all SECURITY_NT_AUTHORITY
  985. }
  986. return $result;
  987. }
  988. /**
  989. * Fonction de recherche avec filtre
  990. * this->connection doit etre defini donc la methode bind ou bindauth doit avoir deja ete appelee
  991. * Ne pas utiliser pour recherche d'une liste donnee de proprietes
  992. * car conflit majuscule-minuscule. A n'utiliser que pour les pages
  993. * 'Fiche LDAP' qui affiche champ lisibles par defaut.
  994. *
  995. * @param string $checkDn DN de recherche (Ex: ou=users,cn=my-domain,cn=com)
  996. * @param string $filter Search filter (ex: (sn=nom_personne) )
  997. * @return array|int Array with answers (key lowercased - value)
  998. */
  999. function search($checkDn, $filter)
  1000. {
  1001. dol_syslog(get_class($this)."::search checkDn=".$checkDn." filter=".$filter);
  1002. $checkDn=$this->convFromOutputCharset($checkDn,$this->ldapcharset);
  1003. $filter=$this->convFromOutputCharset($filter,$this->ldapcharset);
  1004. // if the directory is AD, then bind first with the search user first
  1005. if ($this->serverType == "activedirectory") {
  1006. $this->bindauth($this->searchUser, $this->searchPassword);
  1007. }
  1008. $this->result = @ldap_search($this->connection, $checkDn, $filter);
  1009. $result = @ldap_get_entries($this->connection, $this->result);
  1010. if (! $result)
  1011. {
  1012. $this->error = ldap_errno($this->connection)." ".ldap_error($this->connection);
  1013. return -1;
  1014. }
  1015. else
  1016. {
  1017. ldap_free_result($this->result);
  1018. return $result;
  1019. }
  1020. }
  1021. /**
  1022. * Load all attribute of a LDAP user
  1023. *
  1024. * @param User $user User to search for. Not used if a filter is provided.
  1025. * @param string $filter Filter for search. Must start with &.
  1026. * Examples: &(objectClass=inetOrgPerson) &(objectClass=user)(objectCategory=person) &(isMemberOf=cn=Sales,ou=Groups,dc=opencsi,dc=com)
  1027. * @return int >0 if OK, <0 if KO
  1028. */
  1029. function fetch($user,$filter)
  1030. {
  1031. // Perform the search and get the entry handles
  1032. // if the directory is AD, then bind first with the search user first
  1033. if ($this->serverType == "activedirectory") {
  1034. $this->bindauth($this->searchUser, $this->searchPassword);
  1035. }
  1036. $searchDN = $this->people; // TODO Why searching in people then domain ?
  1037. $result = '';
  1038. $i=0;
  1039. while ($i <= 2)
  1040. {
  1041. dol_syslog(get_class($this)."::fetch search with searchDN=".$searchDN." filter=".$filter);
  1042. $this->result = @ldap_search($this->connection, $searchDN, $filter);
  1043. if ($this->result)
  1044. {
  1045. $result = @ldap_get_entries($this->connection, $this->result);
  1046. if ($result['count'] > 0) dol_syslog('Ldap::fetch search found '.$result['count'].' records');
  1047. else dol_syslog('Ldap::fetch search returns but found no records');
  1048. //var_dump($result);exit;
  1049. }
  1050. else
  1051. {
  1052. $this->error = ldap_errno($this->connection)." ".ldap_error($this->connection);
  1053. dol_syslog(get_class($this)."::fetch search fails");
  1054. return -1;
  1055. }
  1056. if (! $result)
  1057. {
  1058. // Si pas de resultat on cherche dans le domaine
  1059. $searchDN = $this->domain;
  1060. $i++;
  1061. }
  1062. else
  1063. {
  1064. break;
  1065. }
  1066. }
  1067. if (! $result)
  1068. {
  1069. $this->error = ldap_errno($this->connection)." ".ldap_error($this->connection);
  1070. return -1;
  1071. }
  1072. else
  1073. {
  1074. $this->name = $this->convToOutputCharset($result[0][$this->attr_name][0],$this->ldapcharset);
  1075. $this->firstname = $this->convToOutputCharset($result[0][$this->attr_firstname][0],$this->ldapcharset);
  1076. $this->login = $this->convToOutputCharset($result[0][$this->attr_login][0],$this->ldapcharset);
  1077. $this->phone = $this->convToOutputCharset($result[0][$this->attr_phone][0],$this->ldapcharset);
  1078. $this->skype = $this->convToOutputCharset($result[0][$this->attr_skype][0],$this->ldapcharset);
  1079. $this->fax = $this->convToOutputCharset($result[0][$this->attr_fax][0],$this->ldapcharset);
  1080. $this->mail = $this->convToOutputCharset($result[0][$this->attr_mail][0],$this->ldapcharset);
  1081. $this->mobile = $this->convToOutputCharset($result[0][$this->attr_mobile][0],$this->ldapcharset);
  1082. $this->uacf = $this->parseUACF($this->convToOutputCharset($result[0]["useraccountcontrol"][0],$this->ldapcharset));
  1083. if (isset($result[0]["pwdlastset"][0])) // If expiration on password exists
  1084. {
  1085. $this->pwdlastset = ($result[0]["pwdlastset"][0] != 0)?$this->convert_time($this->convToOutputCharset($result[0]["pwdlastset"][0],$this->ldapcharset)):0;
  1086. }
  1087. else
  1088. {
  1089. $this->pwdlastset = -1;
  1090. }
  1091. if (!$this->name && !$this->login) $this->pwdlastset = -1;
  1092. $this->badpwdtime = $this->convert_time($this->convToOutputCharset($result[0]["badpasswordtime"][0],$this->ldapcharset));
  1093. // FQDN domain
  1094. $domain = str_replace('dc=','',$this->domain);
  1095. $domain = str_replace(',','.',$domain);
  1096. $this->domainFQDN = $domain;
  1097. // Set ldapUserDn (each user can have a different dn)
  1098. //var_dump($result[0]);exit;
  1099. $this->ldapUserDN=$result[0]['dn'];
  1100. ldap_free_result($this->result);
  1101. return 1;
  1102. }
  1103. }
  1104. // helper methods
  1105. /**
  1106. * Returns the correct user identifier to use, based on the ldap server type
  1107. *
  1108. * @return string Login
  1109. */
  1110. function getUserIdentifier()
  1111. {
  1112. if ($this->serverType == "activedirectory") {
  1113. return $this->attr_sambalogin;
  1114. } else {
  1115. return $this->attr_login;
  1116. }
  1117. }
  1118. /**
  1119. * UserAccountControl Flgs to more human understandable form...
  1120. *
  1121. * @param string $uacf UACF
  1122. * @return void
  1123. */
  1124. function parseUACF($uacf)
  1125. {
  1126. //All flags array
  1127. $flags = array( "TRUSTED_TO_AUTH_FOR_DELEGATION" => 16777216,
  1128. "PASSWORD_EXPIRED" => 8388608,
  1129. "DONT_REQ_PREAUTH" => 4194304,
  1130. "USE_DES_KEY_ONLY" => 2097152,
  1131. "NOT_DELEGATED" => 1048576,
  1132. "TRUSTED_FOR_DELEGATION" => 524288,
  1133. "SMARTCARD_REQUIRED" => 262144,
  1134. "MNS_LOGON_ACCOUNT" => 131072,
  1135. "DONT_EXPIRE_PASSWORD" => 65536,
  1136. "SERVER_TRUST_ACCOUNT" => 8192,
  1137. "WORKSTATION_TRUST_ACCOUNT" => 4096,
  1138. "INTERDOMAIN_TRUST_ACCOUNT" => 2048,
  1139. "NORMAL_ACCOUNT" => 512,
  1140. "TEMP_DUPLICATE_ACCOUNT" => 256,
  1141. "ENCRYPTED_TEXT_PWD_ALLOWED" => 128,
  1142. "PASSWD_CANT_CHANGE" => 64,
  1143. "PASSWD_NOTREQD" => 32,
  1144. "LOCKOUT" => 16,
  1145. "HOMEDIR_REQUIRED" => 8,
  1146. "ACCOUNTDISABLE" => 2,
  1147. "SCRIPT" => 1);
  1148. //Parse flags to text
  1149. $retval = array();
  1150. while (list($flag, $val) = each($flags)) {
  1151. if ($uacf >= $val) {
  1152. $uacf -= $val;
  1153. $retval[$val] = $flag;
  1154. }
  1155. }
  1156. //Return human friendly flags
  1157. return($retval);
  1158. }
  1159. /**
  1160. * SamAccountType value to text
  1161. *
  1162. * @param string $samtype SamType
  1163. * @return string Sam string
  1164. */
  1165. function parseSAT($samtype)
  1166. {
  1167. $stypes = array( 805306368 => "NORMAL_ACCOUNT",
  1168. 805306369 => "WORKSTATION_TRUST",
  1169. 805306370 => "INTERDOMAIN_TRUST",
  1170. 268435456 => "SECURITY_GLOBAL_GROUP",
  1171. 268435457 => "DISTRIBUTION_GROUP",
  1172. 536870912 => "SECURITY_LOCAL_GROUP",
  1173. 536870913 => "DISTRIBUTION_LOCAL_GROUP");
  1174. $retval = "";
  1175. while (list($sat, $val) = each($stypes)) {
  1176. if ($samtype == $sat) {
  1177. $retval = $val;
  1178. break;
  1179. }
  1180. }
  1181. if (empty($retval)) $retval = "UNKNOWN_TYPE_" . $samtype;
  1182. return($retval);
  1183. }
  1184. /**
  1185. * Convertit le temps ActiveDirectory en Unix timestamp
  1186. *
  1187. * @param string $value AD time to convert
  1188. * @return integer Unix timestamp
  1189. */
  1190. function convert_time($value)
  1191. {
  1192. $dateLargeInt=$value; // nano secondes depuis 1601 !!!!
  1193. $secsAfterADEpoch = $dateLargeInt / (10000000); // secondes depuis le 1 jan 1601
  1194. $ADToUnixConvertor=((1970-1601) * 365.242190) * 86400; // UNIX start date - AD start date * jours * secondes
  1195. $unixTimeStamp=intval($secsAfterADEpoch-$ADToUnixConvertor); // Unix time stamp
  1196. return $unixTimeStamp;
  1197. }
  1198. /**
  1199. * Convert a string into output/memory charset
  1200. *
  1201. * @param string $str String to convert
  1202. * @param string $pagecodefrom Page code of src string
  1203. * @return string Converted string
  1204. */
  1205. private function convToOutputCharset($str,$pagecodefrom='UTF-8')
  1206. {
  1207. global $conf;
  1208. if ($pagecodefrom == 'ISO-8859-1' && $conf->file->character_set_client == 'UTF-8') $str=utf8_encode($str);
  1209. if ($pagecodefrom == 'UTF-8' && $conf->file->character_set_client == 'ISO-8859-1') $str=utf8_decode($str);
  1210. return $str;
  1211. }
  1212. /**
  1213. * Convert a string from output/memory charset
  1214. *
  1215. * @param string $str String to convert
  1216. * @param string $pagecodeto Page code for result string
  1217. * @return string Converted string
  1218. */
  1219. function convFromOutputCharset($str,$pagecodeto='UTF-8')
  1220. {
  1221. global $conf;
  1222. if ($pagecodeto == 'ISO-8859-1' && $conf->file->character_set_client == 'UTF-8') $str=utf8_decode($str);
  1223. if ($pagecodeto == 'UTF-8' && $conf->file->character_set_client == 'ISO-8859-1') $str=utf8_encode($str);
  1224. return $str;
  1225. }
  1226. /**
  1227. * Return available value of group GID
  1228. *
  1229. * @return int gid number
  1230. */
  1231. function getNextGroupGid()
  1232. {
  1233. global $conf;
  1234. $search='('.$conf->global->LDAP_KEY_GROUPS.'=*)';
  1235. $result = $this->search($this->groups,$search);
  1236. if($result)
  1237. {
  1238. $c = $result['count'];
  1239. $gids = array();
  1240. for($i=0;$i<$c;$i++)
  1241. {
  1242. $gids[] = $result[$i]['gidnumber'][0];
  1243. }
  1244. rsort($gids);
  1245. return $gids[0]+1;
  1246. }
  1247. return 0;
  1248. }
  1249. }