helper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Brice Maron <brice@bmaron.net>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\user_ldap\lib;
  28. class Helper {
  29. /**
  30. * returns prefixes for each saved LDAP/AD server configuration.
  31. * @param bool $activeConfigurations optional, whether only active configuration shall be
  32. * retrieved, defaults to false
  33. * @return array with a list of the available prefixes
  34. *
  35. * Configuration prefixes are used to set up configurations for n LDAP or
  36. * AD servers. Since configuration is stored in the database, table
  37. * appconfig under appid user_ldap, the common identifiers in column
  38. * 'configkey' have a prefix. The prefix for the very first server
  39. * configuration is empty.
  40. * Configkey Examples:
  41. * Server 1: ldap_login_filter
  42. * Server 2: s1_ldap_login_filter
  43. * Server 3: s2_ldap_login_filter
  44. *
  45. * The prefix needs to be passed to the constructor of Connection class,
  46. * except the default (first) server shall be connected to.
  47. *
  48. */
  49. public function getServerConfigurationPrefixes($activeConfigurations = false) {
  50. $referenceConfigkey = 'ldap_configuration_active';
  51. $sql = '
  52. SELECT DISTINCT `configkey`
  53. FROM `*PREFIX*appconfig`
  54. WHERE `appid` = \'user_ldap\'
  55. AND `configkey` LIKE ?
  56. ';
  57. if($activeConfigurations) {
  58. if (\OC_Config::getValue( 'dbtype', 'sqlite' ) === 'oci') {
  59. //FIXME oracle hack: need to explicitly cast CLOB to CHAR for comparison
  60. $sql .= ' AND to_char(`configvalue`)=\'1\'';
  61. } else {
  62. $sql .= ' AND `configvalue` = \'1\'';
  63. }
  64. }
  65. $stmt = \OCP\DB::prepare($sql);
  66. $serverConfigs = $stmt->execute(array('%'.$referenceConfigkey))->fetchAll();
  67. $prefixes = array();
  68. foreach($serverConfigs as $serverConfig) {
  69. $len = strlen($serverConfig['configkey']) - strlen($referenceConfigkey);
  70. $prefixes[] = substr($serverConfig['configkey'], 0, $len);
  71. }
  72. return $prefixes;
  73. }
  74. /**
  75. *
  76. * determines the host for every configured connection
  77. * @return array an array with configprefix as keys
  78. *
  79. */
  80. public function getServerConfigurationHosts() {
  81. $referenceConfigkey = 'ldap_host';
  82. $query = '
  83. SELECT DISTINCT `configkey`, `configvalue`
  84. FROM `*PREFIX*appconfig`
  85. WHERE `appid` = \'user_ldap\'
  86. AND `configkey` LIKE ?
  87. ';
  88. $query = \OCP\DB::prepare($query);
  89. $configHosts = $query->execute(array('%'.$referenceConfigkey))->fetchAll();
  90. $result = array();
  91. foreach($configHosts as $configHost) {
  92. $len = strlen($configHost['configkey']) - strlen($referenceConfigkey);
  93. $prefix = substr($configHost['configkey'], 0, $len);
  94. $result[$prefix] = $configHost['configvalue'];
  95. }
  96. return $result;
  97. }
  98. /**
  99. * deletes a given saved LDAP/AD server configuration.
  100. * @param string $prefix the configuration prefix of the config to delete
  101. * @return bool true on success, false otherwise
  102. */
  103. public function deleteServerConfiguration($prefix) {
  104. if(!in_array($prefix, self::getServerConfigurationPrefixes())) {
  105. return false;
  106. }
  107. $saveOtherConfigurations = '';
  108. if(empty($prefix)) {
  109. $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\'';
  110. }
  111. $query = \OCP\DB::prepare('
  112. DELETE
  113. FROM `*PREFIX*appconfig`
  114. WHERE `configkey` LIKE ?
  115. '.$saveOtherConfigurations.'
  116. AND `appid` = \'user_ldap\'
  117. AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\')
  118. ');
  119. $delRows = $query->execute(array($prefix.'%'));
  120. if(\OCP\DB::isError($delRows)) {
  121. return false;
  122. }
  123. if($delRows === 0) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. /**
  129. * checks whether there is one or more disabled LDAP configurations
  130. * @throws \Exception
  131. * @return bool
  132. */
  133. public function haveDisabledConfigurations() {
  134. $all = $this->getServerConfigurationPrefixes(false);
  135. $active = $this->getServerConfigurationPrefixes(true);
  136. if(!is_array($all) || !is_array($active)) {
  137. throw new \Exception('Unexpected Return Value');
  138. }
  139. return count($all) !== count($active) || count($all) === 0;
  140. }
  141. /**
  142. * extracts the domain from a given URL
  143. * @param string $url the URL
  144. * @return string|false domain as string on success, false otherwise
  145. */
  146. public function getDomainFromURL($url) {
  147. $uinfo = parse_url($url);
  148. if(!is_array($uinfo)) {
  149. return false;
  150. }
  151. $domain = false;
  152. if(isset($uinfo['host'])) {
  153. $domain = $uinfo['host'];
  154. } else if(isset($uinfo['path'])) {
  155. $domain = $uinfo['path'];
  156. }
  157. return $domain;
  158. }
  159. }