update.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $rootView = new \OC\Files\View('');
  13. while ($row = $result->fetchRow()) {
  14. $meta = $rootView->getFileInfo($$row['source']);
  15. $itemSource = $meta['fileid'];
  16. if ($itemSource != -1) {
  17. $file = $meta;
  18. if ($file['mimetype'] == 'httpd/unix-directory') {
  19. $itemType = 'folder';
  20. } else {
  21. $itemType = 'file';
  22. }
  23. if ($row['permissions'] == 0) {
  24. $permissions = OCP\PERMISSION_READ | OCP\PERMISSION_SHARE;
  25. } else {
  26. $permissions = OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE | OCP\PERMISSION_SHARE;
  27. if ($itemType == 'folder') {
  28. $permissions |= OCP\PERMISSION_CREATE;
  29. }
  30. }
  31. $pos = strrpos($row['uid_shared_with'], '@');
  32. if ($pos !== false && OC_Group::groupExists(substr($row['uid_shared_with'], $pos + 1))) {
  33. $shareType = OCP\Share::SHARE_TYPE_GROUP;
  34. $shareWith = substr($row['uid_shared_with'], 0, $pos);
  35. if (isset($groupShares[$shareWith][$itemSource])) {
  36. continue;
  37. } else {
  38. $groupShares[$shareWith][$itemSource] = true;
  39. }
  40. } else if ($row['uid_shared_with'] == 'public') {
  41. $shareType = OCP\Share::SHARE_TYPE_LINK;
  42. $shareWith = null;
  43. } else {
  44. $shareType = OCP\Share::SHARE_TYPE_USER;
  45. $shareWith = $row['uid_shared_with'];
  46. }
  47. OC_User::setUserId($row['uid_owner']);
  48. //we need to setup the filesystem for the user, otherwise OC_FileSystem::getRoot will fail and break
  49. OC_Util::setupFS($row['uid_owner']);
  50. try {
  51. OCP\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions);
  52. }
  53. catch (Exception $e) {
  54. $update_error = true;
  55. OCP\Util::writeLog('files_sharing',
  56. 'Upgrade Routine: Skipping sharing "'.$row['source'].'" to "'.$shareWith
  57. .'" (error is "'.$e->getMessage().'")',
  58. OCP\Util::WARN);
  59. }
  60. OC_Util::tearDownFS();
  61. }
  62. }
  63. OC_User::setUserId(null);
  64. if ($update_error) {
  65. OCP\Util::writeLog('files_sharing', 'There were some problems upgrading the sharing of files', OCP\Util::ERROR);
  66. }
  67. // NOTE: Let's drop the table after more testing
  68. // $query = OCP\DB::prepare('DROP TABLE `*PREFIX*sharing`');
  69. // $query->execute();
  70. }
  71. // clean up oc_share table from files which are no longer exists
  72. if (version_compare($installedVersion, '0.3.5', '<')) {
  73. // get all shares where the original file no longer exists
  74. $findShares = \OC_DB::prepare('SELECT `file_source` FROM `*PREFIX*share` LEFT JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` WHERE `*PREFIX*filecache`.`fileid` IS NULL AND `*PREFIX*share`.`item_type` IN (\'file\', \'folder\')');
  75. $sharesFound = $findShares->execute(array())->fetchAll();
  76. // delete those shares from the oc_share table
  77. if (is_array($sharesFound) && !empty($sharesFound)) {
  78. $delArray = array();
  79. foreach ($sharesFound as $share) {
  80. $delArray[] = $share['file_source'];
  81. }
  82. $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source` IN (?)');
  83. $result = $removeShares->execute(array(implode(',', $delArray)));
  84. }
  85. }