SimpleFile.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * SimpleFile
  4. *
  5. * The 'SimpleFile' class is used to easily add read-only immutable files to
  6. * the directory structure. One usecase would be to add a 'readme.txt' to a
  7. * root of a webserver with some standard content.
  8. *
  9. * @package Sabre
  10. * @subpackage DAV
  11. * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
  12. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  13. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  14. */
  15. class Sabre_DAV_SimpleFile extends Sabre_DAV_File {
  16. /**
  17. * File contents
  18. *
  19. * @var string
  20. */
  21. protected $contents = array();
  22. /**
  23. * Name of this resource
  24. *
  25. * @var string
  26. */
  27. protected $name;
  28. /**
  29. * A mimetype, such as 'text/plain' or 'text/html'
  30. *
  31. * @var string
  32. */
  33. protected $mimeType;
  34. /**
  35. * Creates this node
  36. *
  37. * The name of the node must be passed, as well as the contents of the
  38. * file.
  39. *
  40. * @param string $name
  41. * @param string $contents
  42. * @param string|null $mimeType
  43. */
  44. public function __construct($name, $contents, $mimeType = null) {
  45. $this->name = $name;
  46. $this->contents = $contents;
  47. $this->mimeType = $mimeType;
  48. }
  49. /**
  50. * Returns the node name for this file.
  51. *
  52. * This name is used to construct the url.
  53. *
  54. * @return string
  55. */
  56. public function getName() {
  57. return $this->name;
  58. }
  59. /**
  60. * Returns the data
  61. *
  62. * This method may either return a string or a readable stream resource
  63. *
  64. * @return mixed
  65. */
  66. public function get() {
  67. return $this->contents;
  68. }
  69. /**
  70. * Returns the size of the file, in bytes.
  71. *
  72. * @return int
  73. */
  74. public function getSize() {
  75. return strlen($this->contents);
  76. }
  77. /**
  78. * Returns the ETag for a file
  79. *
  80. * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
  81. * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
  82. *
  83. * Return null if the ETag can not effectively be determined
  84. * @return string
  85. */
  86. public function getETag() {
  87. return '"' . md5($this->contents) . '"';
  88. }
  89. /**
  90. * Returns the mime-type for a file
  91. *
  92. * If null is returned, we'll assume application/octet-stream
  93. * @return string
  94. */
  95. public function getContentType() {
  96. return $this->mimeType;
  97. }
  98. }