html.formldap.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /* Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
  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 3 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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/html.formldap.class.php
  19. * \ingroup core
  20. * \brief File of class with ldap html predefined components
  21. */
  22. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  23. /**
  24. * Class to manage generation of HTML components for ldap module
  25. */
  26. class FormLdap
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error = '';
  36. /**
  37. * @var string[] Array of error strings
  38. */
  39. public $errors = array();
  40. /**
  41. * Constructor
  42. *
  43. * @param DoliDB $db Database handler
  44. */
  45. public function __construct($db)
  46. {
  47. global $langs, $form;
  48. if (!is_object($form)) {
  49. $form = new Form($this->db);
  50. }
  51. $langs->loadLangs(array("admin", "ldap"));
  52. $this->db = $db;
  53. }
  54. /**
  55. * Return list of types of hash
  56. *
  57. * @param string $selected Preselected type
  58. * @param string $htmlname Name of field in form
  59. * @param int $showempty Add an empty field
  60. * @return string HTML select string
  61. */
  62. public function selectLdapPasswordHashType($selected = 'md5', $htmlname = 'ldaphashtype', $showempty = 0)
  63. {
  64. global $form;
  65. if (empty($selected)) {
  66. $selected = 'md5';
  67. }
  68. if (empty($htmlname)) {
  69. $htmlname = 'ldaphashtype';
  70. }
  71. $arraylist = array(
  72. //"pbkdf2sha256" => "PBKDF2_SHA256",
  73. "ssha512" => "SSHA-512",
  74. "ssha384" => "SSHA-384",
  75. "ssha256" => "SSHA-256",
  76. "ssha" => "SSHA",
  77. "sha512" => "SHA-512",
  78. "sha384" => "SHA-384",
  79. "sha256" => "SHA-256",
  80. "sha" => "SHA",
  81. "md5" => "MD5",
  82. "smd5" => "SMD5",
  83. //"cryptmd5" => "CRYPT-MD5",
  84. //"cryptsha512" => "CRYPT-SHA512",
  85. //"cryptsha384" => "CRYPT-SHA384",
  86. //"cryptsha256" => "CRYPT-SHA256",
  87. "crypt" => "CRYPT",
  88. "clear" => "CLEAR"
  89. );
  90. return $form->selectarray($htmlname, $arraylist, $selected, $showempty);
  91. }
  92. /**
  93. * Return list of type of synchronization
  94. *
  95. * @param int $selected Preselected type
  96. * @param string $htmlname Name of field in form
  97. * @param array $exclude Exclude values from the list
  98. * @param int $scriptonly Add warning if synchro only work with a script (0 = disable, 1 = Dolibarr2ldap, 2 = ldap2dolibarr, 3 = all)
  99. * @param int $showempty Add an empty field
  100. * @return string HTML select string
  101. */
  102. public function selectLdapDnSynchroActive($selected = 0, $htmlname = 'activesynchro', $exclude = array(), $scriptonly = 0, $showempty = 0)
  103. {
  104. global $langs, $form;
  105. if (empty($selected)) {
  106. $selected = Ldap::SYNCHRO_NONE;
  107. }
  108. if (empty($htmlname)) {
  109. $htmlname = 'activesynchro';
  110. }
  111. $dolibarr2ldaplabel = $langs->trans("DolibarrToLDAP") . (($scriptonly == 1 || $scriptonly == 3) ? " (".$langs->trans("SupportedForLDAPExportScriptOnly").")" : "");
  112. $ldap2dolibarrlabel = $langs->trans("LDAPToDolibarr") . (($scriptonly == 2 || $scriptonly == 3) ? " (".$langs->trans("SupportedForLDAPImportScriptOnly").")" : "");
  113. $arraylist = array(
  114. Ldap::SYNCHRO_NONE => $langs->trans("No"),
  115. Ldap::SYNCHRO_DOLIBARR_TO_LDAP => $dolibarr2ldaplabel,
  116. Ldap::SYNCHRO_LDAP_TO_DOLIBARR => $ldap2dolibarrlabel
  117. );
  118. if (is_array($exclude) && !empty($exclude)) {
  119. foreach ($exclude as $value) {
  120. if (array_key_exists($value, $arraylist)) {
  121. unset($arraylist[$value]);
  122. }
  123. }
  124. }
  125. return $form->selectarray($htmlname, $arraylist, $selected, $showempty);
  126. }
  127. /**
  128. * Return list of ldap server types
  129. *
  130. * @param string $selected Preselected type
  131. * @param string $htmlname Name of field in form
  132. * @param int $showempty Add an empty field
  133. * @return string HTML select string
  134. */
  135. public function selectLdapServerType($selected = 'openldap', $htmlname = 'type', $showempty = 0)
  136. {
  137. global $form;
  138. if (empty($selected)) {
  139. $selected = 'openldap';
  140. }
  141. if (empty($htmlname)) {
  142. $htmlname = 'type';
  143. }
  144. $arraylist = array(
  145. 'activedirectory' => 'Active Directory',
  146. 'openldap' => 'OpenLdap',
  147. 'egroupware' => 'Egroupware'
  148. );
  149. return $form->selectarray($htmlname, $arraylist, $selected, $showempty);
  150. }
  151. /**
  152. * Return list of ldap server protocol version
  153. *
  154. * @param string $selected Preselected type
  155. * @param string $htmlname Name of field in form
  156. * @param int $showempty Add an empty field
  157. * @return string HTML select string
  158. */
  159. public function selectLdapServerProtocolVersion($selected = '3', $htmlname = 'ldapprotocolversion', $showempty = 0)
  160. {
  161. global $form;
  162. if (empty($selected)) {
  163. $selected = '3';
  164. }
  165. if (empty($htmlname)) {
  166. $htmlname = 'ldapprotocolversion';
  167. }
  168. $arraylist = array(
  169. '3' => 'Version 3',
  170. '2' => 'Version 2'
  171. );
  172. return $form->selectarray($htmlname, $arraylist, $selected, $showempty);
  173. }
  174. }