helper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * ownCloud – LDAP Helper
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 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 Helper {
  24. /**
  25. * returns prefixes for each saved LDAP/AD server configuration.
  26. * @param bool $activeConfigurations optional, whether only active configuration shall be
  27. * retrieved, defaults to false
  28. * @return array with a list of the available prefixes
  29. *
  30. * Configuration prefixes are used to set up configurations for n LDAP or
  31. * AD servers. Since configuration is stored in the database, table
  32. * appconfig under appid user_ldap, the common identifiers in column
  33. * 'configkey' have a prefix. The prefix for the very first server
  34. * configuration is empty.
  35. * Configkey Examples:
  36. * Server 1: ldap_login_filter
  37. * Server 2: s1_ldap_login_filter
  38. * Server 3: s2_ldap_login_filter
  39. *
  40. * The prefix needs to be passed to the constructor of Connection class,
  41. * except the default (first) server shall be connected to.
  42. *
  43. */
  44. public function getServerConfigurationPrefixes($activeConfigurations = false) {
  45. $referenceConfigkey = 'ldap_configuration_active';
  46. $sql = '
  47. SELECT DISTINCT `configkey`
  48. FROM `*PREFIX*appconfig`
  49. WHERE `appid` = \'user_ldap\'
  50. AND `configkey` LIKE ?
  51. ';
  52. if($activeConfigurations) {
  53. if (\OC_Config::getValue( 'dbtype', 'sqlite' ) === 'oci') {
  54. //FIXME oracle hack: need to explicitly cast CLOB to CHAR for comparison
  55. $sql .= ' AND to_char(`configvalue`)=\'1\'';
  56. } else {
  57. $sql .= ' AND `configvalue` = \'1\'';
  58. }
  59. }
  60. $stmt = \OCP\DB::prepare($sql);
  61. $serverConfigs = $stmt->execute(array('%'.$referenceConfigkey))->fetchAll();
  62. $prefixes = array();
  63. foreach($serverConfigs as $serverConfig) {
  64. $len = strlen($serverConfig['configkey']) - strlen($referenceConfigkey);
  65. $prefixes[] = substr($serverConfig['configkey'], 0, $len);
  66. }
  67. return $prefixes;
  68. }
  69. /**
  70. *
  71. * determines the host for every configured connection
  72. * @return array an array with configprefix as keys
  73. *
  74. */
  75. public function getServerConfigurationHosts() {
  76. $referenceConfigkey = 'ldap_host';
  77. $query = '
  78. SELECT DISTINCT `configkey`, `configvalue`
  79. FROM `*PREFIX*appconfig`
  80. WHERE `appid` = \'user_ldap\'
  81. AND `configkey` LIKE ?
  82. ';
  83. $query = \OCP\DB::prepare($query);
  84. $configHosts = $query->execute(array('%'.$referenceConfigkey))->fetchAll();
  85. $result = array();
  86. foreach($configHosts as $configHost) {
  87. $len = strlen($configHost['configkey']) - strlen($referenceConfigkey);
  88. $prefix = substr($configHost['configkey'], 0, $len);
  89. $result[$prefix] = $configHost['configvalue'];
  90. }
  91. return $result;
  92. }
  93. /**
  94. * deletes a given saved LDAP/AD server configuration.
  95. * @param string $prefix the configuration prefix of the config to delete
  96. * @return bool true on success, false otherwise
  97. */
  98. public function deleteServerConfiguration($prefix) {
  99. if(!in_array($prefix, self::getServerConfigurationPrefixes())) {
  100. return false;
  101. }
  102. $saveOtherConfigurations = '';
  103. if(empty($prefix)) {
  104. $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\'';
  105. }
  106. $query = \OCP\DB::prepare('
  107. DELETE
  108. FROM `*PREFIX*appconfig`
  109. WHERE `configkey` LIKE ?
  110. '.$saveOtherConfigurations.'
  111. AND `appid` = \'user_ldap\'
  112. AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\')
  113. ');
  114. $delRows = $query->execute(array($prefix.'%'));
  115. if(\OCP\DB::isError($delRows)) {
  116. return false;
  117. }
  118. if($delRows === 0) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * checks whether there is one or more disabled LDAP configurations
  125. * @throws \Exception
  126. * @return bool
  127. */
  128. public function haveDisabledConfigurations() {
  129. $all = $this->getServerConfigurationPrefixes(false);
  130. $active = $this->getServerConfigurationPrefixes(true);
  131. if(!is_array($all) || !is_array($active)) {
  132. throw new \Exception('Unexpected Return Value');
  133. }
  134. return count($all) !== count($active) || count($all) === 0;
  135. }
  136. /**
  137. * extracts the domain from a given URL
  138. * @param string $url the URL
  139. * @return string|false domain as string on success, false otherwise
  140. */
  141. public function getDomainFromURL($url) {
  142. $uinfo = parse_url($url);
  143. if(!is_array($uinfo)) {
  144. return false;
  145. }
  146. $domain = false;
  147. if(isset($uinfo['host'])) {
  148. $domain = $uinfo['host'];
  149. } else if(isset($uinfo['path'])) {
  150. $domain = $uinfo['path'];
  151. }
  152. return $domain;
  153. }
  154. }