configuration.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * ownCloud – LDAP Connection
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 2012, 2013 Arthur Schiwon blizzz@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\user_ldap\lib;
  23. class Configuration {
  24. protected $configPrefix = null;
  25. protected $configRead = false;
  26. //settings
  27. protected $config = array(
  28. 'ldapHost' => null,
  29. 'ldapPort' => null,
  30. 'ldapBackupHost' => null,
  31. 'ldapBackupPort' => null,
  32. 'ldapBase' => null,
  33. 'ldapBaseUsers' => null,
  34. 'ldapBaseGroups' => null,
  35. 'ldapAgentName' => null,
  36. 'ldapAgentPassword' => null,
  37. 'ldapTLS' => null,
  38. 'ldapNoCase' => null,
  39. 'turnOffCertCheck' => null,
  40. 'ldapIgnoreNamingRules' => null,
  41. 'ldapUserDisplayName' => null,
  42. 'ldapUserFilterObjectclass' => null,
  43. 'ldapUserFilterGroups' => null,
  44. 'ldapUserFilter' => null,
  45. 'ldapUserFilterMode' => null,
  46. 'ldapGroupFilter' => null,
  47. 'ldapGroupFilterMode' => null,
  48. 'ldapGroupFilterObjectclass' => null,
  49. 'ldapGroupFilterGroups' => null,
  50. 'ldapGroupDisplayName' => null,
  51. 'ldapGroupMemberAssocAttr' => null,
  52. 'ldapLoginFilter' => null,
  53. 'ldapLoginFilterMode' => null,
  54. 'ldapLoginFilterEmail' => null,
  55. 'ldapLoginFilterUsername' => null,
  56. 'ldapLoginFilterAttributes' => null,
  57. 'ldapQuotaAttribute' => null,
  58. 'ldapQuotaDefault' => null,
  59. 'ldapEmailAttribute' => null,
  60. 'ldapCacheTTL' => null,
  61. 'ldapUuidUserAttribute' => 'auto',
  62. 'ldapUuidGroupAttribute' => 'auto',
  63. 'ldapOverrideMainServer' => false,
  64. 'ldapConfigurationActive' => false,
  65. 'ldapAttributesForUserSearch' => null,
  66. 'ldapAttributesForGroupSearch' => null,
  67. 'homeFolderNamingRule' => null,
  68. 'hasPagedResultSupport' => false,
  69. 'hasMemberOfFilterSupport' => false,
  70. 'ldapExpertUsernameAttr' => null,
  71. 'ldapExpertUUIDUserAttr' => null,
  72. 'ldapExpertUUIDGroupAttr' => null,
  73. 'lastJpegPhotoLookup' => null,
  74. );
  75. public function __construct($configPrefix, $autoread = true) {
  76. $this->configPrefix = $configPrefix;
  77. if($autoread) {
  78. $this->readConfiguration();
  79. }
  80. }
  81. public function __get($name) {
  82. if(isset($this->config[$name])) {
  83. return $this->config[$name];
  84. }
  85. }
  86. public function __set($name, $value) {
  87. $this->setConfiguration(array($name => $value));
  88. }
  89. public function getConfiguration() {
  90. return $this->config;
  91. }
  92. /**
  93. * @brief set LDAP configuration with values delivered by an array, not read
  94. * from configuration. It does not save the configuration! To do so, you
  95. * must call saveConfiguration afterwards.
  96. * @param $config array that holds the config parameters in an associated
  97. * array
  98. * @param &$applied optional; array where the set fields will be given to
  99. * @return null
  100. */
  101. public function setConfiguration($config, &$applied = null) {
  102. if(!is_array($config)) {
  103. return false;
  104. }
  105. $cta = $this->getConfigTranslationArray();
  106. foreach($config as $inputkey => $val) {
  107. if(strpos($inputkey, '_') !== false && isset($cta[$inputkey])) {
  108. $key = $cta[$inputkey];
  109. } elseif(isset($this->config[$inputkey])) {
  110. $key = $inputkey;
  111. } else {
  112. continue;
  113. }
  114. $setMethod = 'setValue';
  115. switch($key) {
  116. case 'homeFolderNamingRule':
  117. if(!empty($val) && strpos($val, 'attr:') === false) {
  118. $val = 'attr:'.$val;
  119. }
  120. break;
  121. case 'ldapBase':
  122. case 'ldapBaseUsers':
  123. case 'ldapBaseGroups':
  124. case 'ldapAttributesForUserSearch':
  125. case 'ldapAttributesForGroupSearch':
  126. case 'ldapUserFilterObjectclass':
  127. case 'ldapUserFilterGroups':
  128. case 'ldapGroupFilterObjectclass':
  129. case 'ldapGroupFilterGroups':
  130. case 'ldapLoginFilterAttributes':
  131. $setMethod = 'setMultiLine';
  132. break;
  133. }
  134. $this->$setMethod($key, $val);
  135. if(is_array($applied)) {
  136. $applied[] = $inputkey;
  137. }
  138. }
  139. }
  140. public function readConfiguration() {
  141. if(!$this->configRead && !is_null($this->configPrefix)) {
  142. $cta = array_flip($this->getConfigTranslationArray());
  143. foreach($this->config as $key => $val) {
  144. if(!isset($cta[$key])) {
  145. //some are determined
  146. continue;
  147. }
  148. $dbkey = $cta[$key];
  149. switch($key) {
  150. case 'ldapBase':
  151. case 'ldapBaseUsers':
  152. case 'ldapBaseGroups':
  153. case 'ldapAttributesForUserSearch':
  154. case 'ldapAttributesForGroupSearch':
  155. case 'ldapUserFilterObjectclass':
  156. case 'ldapUserFilterGroups':
  157. case 'ldapGroupFilterObjectclass':
  158. case 'ldapGroupFilterGroups':
  159. case 'ldapLoginFilterAttributes':
  160. $readMethod = 'getMultiLine';
  161. break;
  162. case 'ldapIgnoreNamingRules':
  163. $readMethod = 'getSystemValue';
  164. $dbkey = $key;
  165. break;
  166. case 'ldapAgentPassword':
  167. $readMethod = 'getPwd';
  168. break;
  169. case 'ldapUserDisplayName':
  170. case 'ldapGroupDisplayName':
  171. $readMethod = 'getLcValue';
  172. break;
  173. default:
  174. $readMethod = 'getValue';
  175. break;
  176. }
  177. $this->config[$key] = $this->$readMethod($dbkey);
  178. }
  179. $this->configRead = true;
  180. }
  181. }
  182. /**
  183. * @brief saves the current Configuration in the database
  184. */
  185. public function saveConfiguration() {
  186. $cta = array_flip($this->getConfigTranslationArray());
  187. foreach($this->config as $key => $value) {
  188. switch ($key) {
  189. case 'ldapAgentPassword':
  190. $value = base64_encode($value);
  191. break;
  192. case 'ldapBase':
  193. case 'ldapBaseUsers':
  194. case 'ldapBaseGroups':
  195. case 'ldapAttributesForUserSearch':
  196. case 'ldapAttributesForGroupSearch':
  197. case 'ldapUserFilterObjectclass':
  198. case 'ldapUserFilterGroups':
  199. case 'ldapGroupFilterObjectclass':
  200. case 'ldapGroupFilterGroups':
  201. case 'ldapLoginFilterAttributes':
  202. if(is_array($value)) {
  203. $value = implode("\n", $value);
  204. }
  205. break;
  206. //following options are not stored but detected, skip them
  207. case 'ldapIgnoreNamingRules':
  208. case 'hasPagedResultSupport':
  209. case 'ldapUuidUserAttribute':
  210. case 'ldapUuidGroupAttribute':
  211. continue 2;
  212. }
  213. if(is_null($value)) {
  214. $value = '';
  215. }
  216. $this->saveValue($cta[$key], $value);
  217. }
  218. }
  219. protected function getMultiLine($varname) {
  220. $value = $this->getValue($varname);
  221. if(empty($value)) {
  222. $value = '';
  223. } else {
  224. $value = preg_split('/\r\n|\r|\n/', $value);
  225. }
  226. return $value;
  227. }
  228. protected function setMultiLine($varname, $value) {
  229. if(empty($value)) {
  230. $value = '';
  231. } else if (!is_array($value)) {
  232. $value = preg_split('/\r\n|\r|\n/', $value);
  233. if($value === false) {
  234. $value = '';
  235. }
  236. }
  237. $this->setValue($varname, $value);
  238. }
  239. protected function getPwd($varname) {
  240. return base64_decode($this->getValue($varname));
  241. }
  242. protected function getLcValue($varname) {
  243. return mb_strtolower($this->getValue($varname), 'UTF-8');
  244. }
  245. protected function getSystemValue($varname) {
  246. //FIXME: if another system value is added, softcode the default value
  247. return \OCP\Config::getSystemValue($varname, false);
  248. }
  249. protected function getValue($varname) {
  250. static $defaults;
  251. if(is_null($defaults)) {
  252. $defaults = $this->getDefaults();
  253. }
  254. return \OCP\Config::getAppValue('user_ldap',
  255. $this->configPrefix.$varname,
  256. $defaults[$varname]);
  257. }
  258. protected function setValue($varname, $value) {
  259. $this->config[$varname] = $value;
  260. }
  261. protected function saveValue($varname, $value) {
  262. return \OCP\Config::setAppValue('user_ldap',
  263. $this->configPrefix.$varname,
  264. $value);
  265. }
  266. /**
  267. * @returns an associative array with the default values. Keys are correspond
  268. * to config-value entries in the database table
  269. */
  270. public function getDefaults() {
  271. return array(
  272. 'ldap_host' => '',
  273. 'ldap_port' => '',
  274. 'ldap_backup_host' => '',
  275. 'ldap_backup_port' => '',
  276. 'ldap_override_main_server' => '',
  277. 'ldap_dn' => '',
  278. 'ldap_agent_password' => '',
  279. 'ldap_base' => '',
  280. 'ldap_base_users' => '',
  281. 'ldap_base_groups' => '',
  282. 'ldap_userlist_filter' => '',
  283. 'ldap_user_filter_mode' => 0,
  284. 'ldap_userfilter_objectclass' => '',
  285. 'ldap_userfilter_groups' => '',
  286. 'ldap_login_filter' => '',
  287. 'ldap_login_filter_mode' => 0,
  288. 'ldap_loginfilter_email' => 0,
  289. 'ldap_loginfilter_username' => 1,
  290. 'ldap_loginfilter_attributes' => '',
  291. 'ldap_group_filter' => '',
  292. 'ldap_group_filter_mode' => 0,
  293. 'ldap_groupfilter_objectclass' => '',
  294. 'ldap_groupfilter_groups' => '',
  295. 'ldap_display_name' => 'displayName',
  296. 'ldap_group_display_name' => 'cn',
  297. 'ldap_tls' => 1,
  298. 'ldap_nocase' => 0,
  299. 'ldap_quota_def' => '',
  300. 'ldap_quota_attr' => '',
  301. 'ldap_email_attr' => '',
  302. 'ldap_group_member_assoc_attribute' => 'uniqueMember',
  303. 'ldap_cache_ttl' => 600,
  304. 'ldap_uuid_user_attribute' => 'auto',
  305. 'ldap_uuid_group_attribute' => 'auto',
  306. 'home_folder_naming_rule' => '',
  307. 'ldap_turn_off_cert_check' => 0,
  308. 'ldap_configuration_active' => 0,
  309. 'ldap_attributes_for_user_search' => '',
  310. 'ldap_attributes_for_group_search' => '',
  311. 'ldap_expert_username_attr' => '',
  312. 'ldap_expert_uuid_user_attr' => '',
  313. 'ldap_expert_uuid_group_attr' => '',
  314. 'has_memberof_filter_support' => 0,
  315. 'last_jpegPhoto_lookup' => 0,
  316. );
  317. }
  318. /**
  319. * @return returns an array that maps internal variable names to database fields
  320. */
  321. public function getConfigTranslationArray() {
  322. //TODO: merge them into one representation
  323. static $array = array(
  324. 'ldap_host' => 'ldapHost',
  325. 'ldap_port' => 'ldapPort',
  326. 'ldap_backup_host' => 'ldapBackupHost',
  327. 'ldap_backup_port' => 'ldapBackupPort',
  328. 'ldap_override_main_server' => 'ldapOverrideMainServer',
  329. 'ldap_dn' => 'ldapAgentName',
  330. 'ldap_agent_password' => 'ldapAgentPassword',
  331. 'ldap_base' => 'ldapBase',
  332. 'ldap_base_users' => 'ldapBaseUsers',
  333. 'ldap_base_groups' => 'ldapBaseGroups',
  334. 'ldap_userfilter_objectclass' => 'ldapUserFilterObjectclass',
  335. 'ldap_userfilter_groups' => 'ldapUserFilterGroups',
  336. 'ldap_userlist_filter' => 'ldapUserFilter',
  337. 'ldap_user_filter_mode' => 'ldapUserFilterMode',
  338. 'ldap_login_filter' => 'ldapLoginFilter',
  339. 'ldap_login_filter_mode' => 'ldapLoginFilterMode',
  340. 'ldap_loginfilter_email' => 'ldapLoginFilterEmail',
  341. 'ldap_loginfilter_username' => 'ldapLoginFilterUsername',
  342. 'ldap_loginfilter_attributes' => 'ldapLoginFilterAttributes',
  343. 'ldap_group_filter' => 'ldapGroupFilter',
  344. 'ldap_group_filter_mode' => 'ldapGroupFilterMode',
  345. 'ldap_groupfilter_objectclass' => 'ldapGroupFilterObjectclass',
  346. 'ldap_groupfilter_groups' => 'ldapGroupFilterGroups',
  347. 'ldap_display_name' => 'ldapUserDisplayName',
  348. 'ldap_group_display_name' => 'ldapGroupDisplayName',
  349. 'ldap_tls' => 'ldapTLS',
  350. 'ldap_nocase' => 'ldapNoCase',
  351. 'ldap_quota_def' => 'ldapQuotaDefault',
  352. 'ldap_quota_attr' => 'ldapQuotaAttribute',
  353. 'ldap_email_attr' => 'ldapEmailAttribute',
  354. 'ldap_group_member_assoc_attribute' => 'ldapGroupMemberAssocAttr',
  355. 'ldap_cache_ttl' => 'ldapCacheTTL',
  356. 'home_folder_naming_rule' => 'homeFolderNamingRule',
  357. 'ldap_turn_off_cert_check' => 'turnOffCertCheck',
  358. 'ldap_configuration_active' => 'ldapConfigurationActive',
  359. 'ldap_attributes_for_user_search' => 'ldapAttributesForUserSearch',
  360. 'ldap_attributes_for_group_search' => 'ldapAttributesForGroupSearch',
  361. 'ldap_expert_username_attr' => 'ldapExpertUsernameAttr',
  362. 'ldap_expert_uuid_user_attr' => 'ldapExpertUUIDUserAttr',
  363. 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr',
  364. 'has_memberof_filter_support' => 'hasMemberOfFilterSupport',
  365. 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup',
  366. );
  367. return $array;
  368. }
  369. }