share.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. OCP\JSON::checkAppEnabled('files_sharing');
  3. OCP\JSON::checkLoggedIn();
  4. $userDirectory = '/'.OCP\USER::getUser().'/files';
  5. $sources = explode(';', $_POST['sources']);
  6. $uid_shared_with = $_POST['uid_shared_with'];
  7. $permissions = $_POST['permissions'];
  8. foreach ($sources as $source) {
  9. $file = OC_FileCache::get($source);
  10. $path = ltrim($source, '/');
  11. $source = $userDirectory.$source;
  12. // Check if the file exists or if the file is being reshared
  13. if ($source && $file['encrypted'] == false && (OC_FILESYSTEM::file_exists($path) && OC_FILESYSTEM::is_readable($path) || OC_Share::getSource($source))) {
  14. try {
  15. $shared = new OC_Share($source, $uid_shared_with, $permissions);
  16. // If this is a private link, return the token
  17. if ($uid_shared_with == OC_Share::PUBLICLINK) {
  18. OCP\JSON::success(array('data' => $shared->getToken()));
  19. } else {
  20. OCP\JSON::success();
  21. }
  22. } catch (Exception $exception) {
  23. OCP\Util::writeLog('files_sharing', 'Unexpected Error : '.$exception->getMessage(), OCP\Util::ERROR);
  24. OCP\JSON::error(array('data' => array('message' => $exception->getMessage())));
  25. }
  26. } else {
  27. if ($file['encrypted'] == true) {
  28. OCP\JSON::error(array('data' => array('message' => 'Encrypted files cannot be shared')));
  29. } else {
  30. OCP\Util::writeLog('files_sharing', 'File does not exist or is not readable :'.$source, OCP\Util::ERROR);
  31. OCP\JSON::error(array('data' => array('message' => 'File does not exist or is not readable')));
  32. }
  33. }
  34. }
  35. ?>