share.php 1.5 KB

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