PageRenderTime 76ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/core/classes/sabredav/ajaxplorer/class.AJXP_Sabre_NodeLeaf.php

https://gitlab.com/KasaiDot/pydio-core
PHP | 174 lines | 67 code | 14 blank | 93 comment | 8 complexity | 33a23327da4d4e6002d050a38e2302b6 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
  4. * This file is part of Pydio.
  5. *
  6. * Pydio is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Pydio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Pydio. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * The latest code can be found at <http://pyd.io/>.
  20. */
  21. defined('AJXP_EXEC') or die( 'Access not allowed');
  22. /**
  23. * @package AjaXplorer
  24. * @subpackage SabreDav
  25. */
  26. class AJXP_Sabre_NodeLeaf extends AJXP_Sabre_Node implements Sabre\DAV\IFile
  27. {
  28. /**
  29. * Updates the data
  30. *
  31. * The data argument is a readable stream resource.
  32. *
  33. * After a succesful put operation, you may choose to return an ETag. The
  34. * etag must always be surrounded by double-quotes. These quotes must
  35. * appear in the actual string you're returning.
  36. *
  37. * Clients may use the ETag from a PUT request to later on make sure that
  38. * when they update the file, the contents haven't changed in the mean
  39. * time.
  40. *
  41. * If you don't plan to store the file byte-by-byte, and you return a
  42. * different object on a subsequent GET you are strongly recommended to not
  43. * return an ETag, and just return null.
  44. *
  45. * @param resource $data
  46. * @return string|null
  47. */
  48. public function put($data)
  49. {
  50. // Warning, passed by ref
  51. $p = $this->path;
  52. if (!AuthService::getLoggedUser()->canWrite($this->repository->getId())) {
  53. throw new \Sabre\DAV\Exception\Forbidden() ;
  54. }
  55. $this->getAccessDriver()->nodeWillChange($p, intval($_SERVER["CONTENT_LENGTH"]));
  56. $stream = fopen($this->getUrl(), "w");
  57. stream_copy_to_stream($data, $stream);
  58. fclose($stream);
  59. $toto = null;
  60. $this->getAccessDriver()->nodeChanged($toto, $p);
  61. return $this->getETag();
  62. }
  63. /**
  64. * Returns the data
  65. *
  66. * This method may either return a string or a readable stream resource
  67. *
  68. * @return mixed
  69. */
  70. public function get()
  71. {
  72. $ajxpNode = new AJXP_Node($this->getUrl());
  73. AJXP_Controller::applyHook("node.read", array(&$ajxpNode));
  74. return fopen($this->getUrl(), "r");
  75. }
  76. /**
  77. * Returns the mime-type for a file
  78. *
  79. * If null is returned, we'll assume application/octet-stream
  80. *
  81. * @return void|string
  82. */
  83. public function getContentType()
  84. {
  85. //Get mimetype with fileinfo PECL extension
  86. $fp = fopen($this->getUrl(), "r");
  87. $fileMime = null;
  88. if (class_exists("finfo")) {
  89. $finfo = new finfo(FILEINFO_MIME);
  90. $fileMime = $finfo->buffer(fread($fp, 100));
  91. } elseif (function_exists("mime_content_type")) {
  92. $fileMime = @mime_content_type($fp);
  93. } else {
  94. $fileExt = substr(strrchr(basename($this->getUrl()), '.'), 1);
  95. if(empty($fileExt))
  96. $fileMime = "application/octet-stream";
  97. else {
  98. $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileExt\s)/i";
  99. $lines = file( AJXP_CONF_PATH ."/mime.types");
  100. foreach ($lines as $line) {
  101. if(substr($line, 0, 1) == '#')
  102. continue; // skip comments
  103. $line = rtrim($line) . " ";
  104. if(!preg_match($regex, $line, $matches))
  105. continue; // no match to the extension
  106. $fileMime = $matches[1];
  107. }
  108. }
  109. }
  110. fclose($fp);
  111. return $fileMime;
  112. /*
  113. if ( $this->options->useMimeExts && ezcBaseFeatures::hasExtensionSupport( 'fileinfo' ) ) {
  114. $fInfo = new fInfo( FILEINFO_MIME );
  115. $mimeType = $fInfo->file( $this->getUrl() );
  116. // The documentation tells to do this, but it does not work with a
  117. // current version of pecl/fileinfo
  118. // $fInfo->close();
  119. return $mimeType;
  120. }
  121. // Check if extension ext/mime-magic is usable.
  122. if ( $this->options->useMimeExts &&
  123. ezcBaseFeatures::hasExtensionSupport( 'mime_magic' ) &&
  124. ( $mimeType = mime_content_type( $this->getUrl() ) ) !== false )
  125. {
  126. return $mimeType;
  127. }
  128. */
  129. }
  130. /**
  131. * Returns the ETag for a file
  132. *
  133. * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
  134. *
  135. * Return null if the ETag can not effectively be determined
  136. *
  137. * @return String|null
  138. */
  139. public function getETag()
  140. {
  141. clearstatcache();
  142. return '"'.md5(
  143. $this->path
  144. . $this->getSize()
  145. . date( 'c', $this->getLastModified() )
  146. ).'"';
  147. }
  148. /**
  149. * Returns the size of the node, in bytes
  150. *
  151. * @return int
  152. */
  153. public function getSize()
  154. {
  155. return filesize($this->getUrl());
  156. }
  157. }