You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filesplugin.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Müller
  6. * @copyright 2013 Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL3
  9. */
  10. class OC_Connector_Sabre_FilesPlugin extends Sabre_DAV_ServerPlugin
  11. {
  12. // namespace
  13. const NS_OWNCLOUD = 'http://owncloud.org/ns';
  14. /**
  15. * Reference to main server object
  16. *
  17. * @var Sabre_DAV_Server
  18. */
  19. private $server;
  20. /**
  21. * This initializes the plugin.
  22. *
  23. * This function is called by Sabre_DAV_Server, after
  24. * addPlugin is called.
  25. *
  26. * This method should set up the required event subscriptions.
  27. *
  28. * @param Sabre_DAV_Server $server
  29. * @return void
  30. */
  31. public function initialize(Sabre_DAV_Server $server) {
  32. $server->xmlNamespaces[self::NS_OWNCLOUD] = 'oc';
  33. $server->protectedProperties[] = '{' . self::NS_OWNCLOUD . '}id';
  34. $this->server = $server;
  35. $this->server->subscribeEvent('beforeGetProperties', array($this, 'beforeGetProperties'));
  36. $this->server->subscribeEvent('afterCreateFile', array($this, 'sendFileIdHeader'));
  37. $this->server->subscribeEvent('afterWriteContent', array($this, 'sendFileIdHeader'));
  38. }
  39. /**
  40. * Adds all ownCloud-specific properties
  41. *
  42. * @param string $path
  43. * @param Sabre_DAV_INode $node
  44. * @param array $requestedProperties
  45. * @param array $returnedProperties
  46. * @return void
  47. */
  48. public function beforeGetProperties($path, Sabre_DAV_INode $node, array &$requestedProperties, array &$returnedProperties) {
  49. if ($node instanceof OC_Connector_Sabre_Node) {
  50. $fileid_propertyname = '{' . self::NS_OWNCLOUD . '}id';
  51. if (array_search($fileid_propertyname, $requestedProperties)) {
  52. unset($requestedProperties[array_search($fileid_propertyname, $requestedProperties)]);
  53. }
  54. /** @var $node OC_Connector_Sabre_Node */
  55. $fileId = $node->getFileId();
  56. if (!is_null($fileId)) {
  57. $returnedProperties[200][$fileid_propertyname] = $fileId;
  58. }
  59. }
  60. }
  61. /**
  62. * @param $filePath
  63. * @param Sabre_DAV_INode $node
  64. * @throws Sabre_DAV_Exception_BadRequest
  65. */
  66. public function sendFileIdHeader($filePath, Sabre_DAV_INode $node = null) {
  67. // chunked upload handling
  68. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  69. list($path, $name) = \Sabre_DAV_URLUtil::splitPath($filePath);
  70. $info = OC_FileChunking::decodeName($name);
  71. if (!empty($info)) {
  72. $filePath = $path . '/' . $info['name'];
  73. }
  74. }
  75. // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
  76. if (!$this->server->tree->nodeExists($filePath)) {
  77. return;
  78. }
  79. $node = $this->server->tree->getNodeForPath($filePath);
  80. if ($node instanceof OC_Connector_Sabre_Node) {
  81. $fileId = $node->getFileId();
  82. if (!is_null($fileId)) {
  83. $this->server->httpResponse->setHeader('OC-FileId', $fileId);
  84. }
  85. }
  86. }
  87. }