update.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. $installedVersion = OCP\Config::getAppValue('files_sharing', 'installed_version');
  3. if (version_compare($installedVersion, '0.3', '<')) {
  4. $update_error = false;
  5. $query = OCP\DB::prepare('SELECT * FROM `*PREFIX*sharing`');
  6. $result = $query->execute();
  7. $groupShares = array();
  8. //we need to set up user backends, otherwise creating the shares will fail with "because user does not exist"
  9. OC_User::useBackend(new OC_User_Database());
  10. OC_Group::useBackend(new OC_Group_Database());
  11. OC_App::loadApps(array('authentication'));
  12. while ($row = $result->fetchRow()) {
  13. $itemSource = OC_FileCache::getId($row['source'], '');
  14. if ($itemSource != -1) {
  15. $file = OC_FileCache::get($row['source'], '');
  16. if ($file['mimetype'] == 'httpd/unix-directory') {
  17. $itemType = 'folder';
  18. } else {
  19. $itemType = 'file';
  20. }
  21. if ($row['permissions'] == 0) {
  22. $permissions = OCP\PERMISSION_READ | OCP\PERMISSION_SHARE;
  23. } else {
  24. $permissions = OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_SHARE;
  25. if ($itemType == 'folder') {
  26. $permissions |= OCP\PERMISSION_CREATE;
  27. }
  28. }
  29. $pos = strrpos($row['uid_shared_with'], '@');
  30. if ($pos !== false && OC_Group::groupExists(substr($row['uid_shared_with'], $pos + 1))) {
  31. $shareType = OCP\Share::SHARE_TYPE_GROUP;
  32. $shareWith = substr($row['uid_shared_with'], 0, $pos);
  33. if (isset($groupShares[$shareWith][$itemSource])) {
  34. continue;
  35. } else {
  36. $groupShares[$shareWith][$itemSource] = true;
  37. }
  38. } else if ($row['uid_shared_with'] == 'public') {
  39. $shareType = OCP\Share::SHARE_TYPE_LINK;
  40. $shareWith = null;
  41. } else {
  42. $shareType = OCP\Share::SHARE_TYPE_USER;
  43. $shareWith = $row['uid_shared_with'];
  44. }
  45. OC_User::setUserId($row['uid_owner']);
  46. //we need to setup the filesystem for the user, otherwise OC_FileSystem::getRoot will fail and break
  47. OC_Util::setupFS($row['uid_owner']);
  48. try {
  49. OCP\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions);
  50. }
  51. catch (Exception $e) {
  52. $update_error = true;
  53. OCP\Util::writeLog('files_sharing', 'Upgrade Routine: Skipping sharing "'.$row['source'].'" to "'.$shareWith.'" (error is "'.$e->getMessage().'")', OCP\Util::WARN);
  54. }
  55. OC_Util::tearDownFS();
  56. }
  57. }
  58. OC_User::setUserId(null);
  59. if ($update_error) {
  60. OCP\Util::writeLog('files_sharing', 'There were some problems upgrading the sharing of files', OCP\Util::ERROR);
  61. }
  62. // NOTE: Let's drop the table after more testing
  63. // $query = OCP\DB::prepare('DROP TABLE `*PREFIX*sharing`');
  64. // $query->execute();
  65. }
  66. if (version_compare($installedVersion, '0.3.3', '<')) {
  67. OC_User::useBackend(new OC_User_Database());
  68. OC_App::loadApps(array('authentication'));
  69. $users = OC_User::getUsers();
  70. foreach ($users as $user) {
  71. OC_FileCache::delete('Shared', '/'.$user.'/files/');
  72. }
  73. }