quotaplugin.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * This plugin check user quota and deny creating files when they exceeds the quota.
  4. *
  5. * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved.
  6. * @author Sergio Cambra
  7. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  8. */
  9. class OC_Connector_Sabre_QuotaPlugin extends Sabre_DAV_ServerPlugin {
  10. /**
  11. * Reference to main server object
  12. *
  13. * @var Sabre_DAV_Server
  14. */
  15. private $server;
  16. /**
  17. * This initializes the plugin.
  18. *
  19. * This function is called by Sabre_DAV_Server, after
  20. * addPlugin is called.
  21. *
  22. * This method should set up the requires event subscriptions.
  23. *
  24. * @param Sabre_DAV_Server $server
  25. * @return void
  26. */
  27. public function initialize(Sabre_DAV_Server $server) {
  28. $this->server = $server;
  29. $this->server->subscribeEvent('beforeWriteContent', array($this, 'checkQuota'), 10);
  30. $this->server->subscribeEvent('beforeCreateFile', array($this, 'checkQuota'), 10);
  31. }
  32. /**
  33. * This method is called before any HTTP method and forces users to be authenticated
  34. *
  35. * @param string $method
  36. * @throws Sabre_DAV_Exception
  37. * @return bool
  38. */
  39. public function checkQuota($uri, $data = null) {
  40. $expected = $this->server->httpRequest->getHeader('X-Expected-Entity-Length');
  41. $length = $expected ? $expected : $this->server->httpRequest->getHeader('Content-Length');
  42. if ($length) {
  43. if (substr($uri, 0, 1)!=='/') {
  44. $uri='/'.$uri;
  45. }
  46. list($parentUri, $newName) = Sabre_DAV_URLUtil::splitPath($uri);
  47. if ($length > OC_Filesystem::free_space($parentUri)) {
  48. throw new Sabre_DAV_Exception('Quota exceeded. File is too big.');
  49. }
  50. }
  51. return true;
  52. }
  53. }