update.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. //from version 0.1 to 0.2
  3. //ATTENTION
  4. //Upgrade from ownCloud 3 (LDAP backend 0.1) to ownCloud 4.5 (LDAP backend 0.3) is not supported!!
  5. //You must do upgrade to ownCloud 4.0 first!
  6. //The upgrade stuff in the section from 0.1 to 0.2 is just to minimize the bad efffects.
  7. //settings
  8. $pw = OCP\Config::getAppValue('user_ldap', 'ldap_password');
  9. if(!is_null($pw)) {
  10. $pwEnc = base64_encode($pw);
  11. OCP\Config::setAppValue('user_ldap', 'ldap_agent_password', $pwEnc);
  12. OC_Appconfig::deleteKey('user_ldap', 'ldap_password');
  13. }
  14. //detect if we can switch on naming guidelines. We won't do it on conflicts.
  15. //it's a bit spaghetti, but hey.
  16. $state = OCP\Config::getSystemValue('ldapIgnoreNamingRules', 'unset');
  17. if($state == 'unset') {
  18. OCP\Config::setSystemValue('ldapIgnoreNamingRules', false);
  19. }
  20. // ### SUPPORTED upgrade path starts here ###
  21. //from version 0.2 to 0.3 (0.2.0.x dev version)
  22. $objects = array('user', 'group');
  23. $connector = new \OCA\user_ldap\lib\Connection('user_ldap');
  24. $userBE = new \OCA\user_ldap\USER_LDAP();
  25. $userBE->setConnector($connector);
  26. $groupBE = new \OCA\user_ldap\GROUP_LDAP();
  27. $groupBE->setConnector($connector);
  28. foreach($objects as $object) {
  29. $fetchDNSql = '
  30. SELECT `ldap_dn`, `owncloud_name`, `directory_uuid`
  31. FROM `*PREFIX*ldap_'.$object.'_mapping`';
  32. $updateSql = '
  33. UPDATE `*PREFIX*ldap_'.$object.'_mapping`
  34. SET `ldap_DN` = ?, `directory_uuid` = ?
  35. WHERE `ldap_dn` = ?';
  36. $query = OCP\DB::prepare($fetchDNSql);
  37. $res = $query->execute();
  38. $DNs = $res->fetchAll();
  39. $updateQuery = OCP\DB::prepare($updateSql);
  40. foreach($DNs as $dn) {
  41. $newDN = escapeDN(mb_strtolower($dn['ldap_dn'], 'UTF-8'));
  42. if(!empty($dn['directory_uuid'])) {
  43. $uuid = $dn['directory_uuid'];
  44. } elseif($object == 'user') {
  45. $uuid = $userBE->getUUID($newDN);
  46. //fix home folder to avoid new ones depending on the configuration
  47. $userBE->getHome($dn['owncloud_name']);
  48. } else {
  49. $uuid = $groupBE->getUUID($newDN);
  50. }
  51. try {
  52. $updateQuery->execute(array($newDN, $uuid, $dn['ldap_dn']));
  53. } catch(Exception $e) {
  54. \OCP\Util::writeLog('user_ldap', 'Could not update '.$object.' '.$dn['ldap_dn'].' in the mappings table. ', \OCP\Util::WARN);
  55. }
  56. }
  57. }
  58. function escapeDN($dn) {
  59. $aDN = ldap_explode_dn($dn, false);
  60. unset($aDN['count']);
  61. foreach($aDN as $key => $part) {
  62. $value = substr($part, strpos($part, '=')+1);
  63. $escapedValue = strtr($value, Array(','=>'\2c', '='=>'\3d', '+'=>'\2b',
  64. '<'=>'\3c', '>'=>'\3e', ';'=>'\3b', '\\'=>'\5c',
  65. '"'=>'\22', '#'=>'\23'));
  66. $part = str_replace($part, $value, $escapedValue);
  67. }
  68. $dn = implode(',', $aDN);
  69. return $dn;
  70. }