ImageCacheActionsModuleStreamWrapper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * Drupal module stream wrapper class (module://{module}/{resource}).
  5. *
  6. * Provides support for accessing files in module directories.
  7. */
  8. class ImageCacheActionsModuleStreamWrapper extends DrupalLocalStreamWrapper {
  9. /**
  10. * Parses an uri into its separate components.
  11. *
  12. * Example:
  13. * module://my_module/images/mask.png will return
  14. * array('module', 'my_module', 'images/mask.png').
  15. *
  16. * @return array
  17. * An array of strings containing the separate parts of the uri:
  18. * - scheme (module)
  19. * - module name
  20. * - resource name
  21. */
  22. protected function parseUri() {
  23. $scheme = '';
  24. $module = '';
  25. $component = '';
  26. if (!empty($this->uri)) {
  27. list($scheme, $target) = explode('://', $this->uri, 2);
  28. if (!empty($target)) {
  29. $target = trim($target, '\/');
  30. list($module, $resource) = explode('/', $target, 2);
  31. }
  32. }
  33. return array($scheme, $module, $resource);
  34. }
  35. /**
  36. * Implements abstract public function getDirectoryPath()
  37. */
  38. public function getDirectoryPath() {
  39. list($scheme, $module, $component) = $this->parseUri();
  40. return drupal_get_path('module', $module);
  41. }
  42. /**
  43. * Returns the local writable target of the resource within the stream.
  44. *
  45. * @see DrupalLocalStreamWrapper::getTarget()
  46. */
  47. protected function getTarget($uri = NULL) {
  48. if (!empty($uri)) {
  49. $this->setUri($uri);
  50. }
  51. list($scheme, $module, $component) = $this->parseUri();
  52. return $component;
  53. }
  54. /**
  55. * Returns the canonical absolute path of the URI, if possible.
  56. *
  57. * @see DrupalLocalStreamWrapper::getLocalPath()
  58. */
  59. protected function getLocalPath($uri = NULL) {
  60. if (!empty($uri)) {
  61. $this->setUri($uri);
  62. }
  63. return parent::getLocalPath();
  64. }
  65. /**
  66. * Returns the local writable target of the resource within the stream.
  67. *
  68. * Overrides getExternalUrl().
  69. *
  70. * Return the HTML URI of a public file.
  71. */
  72. function getExternalUrl() {
  73. $path = str_replace('\\', '/', $this->getTarget());
  74. return $GLOBALS['base_url'] . '/' . $this->getDirectoryPath() . '/' . drupal_encode_path($path);
  75. }
  76. /*
  77. * Unsupported methods, modes and operations because we are read-only.
  78. */
  79. function chmod($mode) {
  80. return $this->unsupportedError('chmod', STREAM_REPORT_ERRORS);
  81. }
  82. public function stream_open($uri, $mode, $options, &$opened_path) {
  83. if (!in_array($mode, array('r'))) {
  84. return $this->unsupportedError("stream_open(mode = '$mode')", $options);
  85. }
  86. return parent::stream_open($uri, $mode, $options, $opened_path);
  87. }
  88. public function stream_lock($operation) {
  89. if (in_array($operation, array(LOCK_EX))) {
  90. return $this->unsupportedError('stream_lock(LOCK_EX)', $options);
  91. }
  92. return parent::stream_lock($operation);
  93. }
  94. public function stream_write($data) {
  95. return $this->unsupportedError('stream_write', STREAM_REPORT_ERRORS);
  96. }
  97. public function stream_flush() {
  98. return $this->unsupportedError('stream_flush', STREAM_REPORT_ERRORS);
  99. }
  100. public function unlink($uri) {
  101. return $this->unsupportedError('unlink', STREAM_REPORT_ERRORS);
  102. }
  103. public function rename($from_uri, $to_uri) {
  104. return $this->unsupportedError('rename', STREAM_REPORT_ERRORS);
  105. }
  106. public function mkdir($uri, $mode, $options) {
  107. return $this->unsupportedError('mkdir', $options);
  108. }
  109. public function rmdir($uri, $options) {
  110. return $this->unsupportedError('rmdir', $options);
  111. }
  112. public function unsupportedError($method, $options) {
  113. if ($options & STREAM_REPORT_ERRORS) {
  114. drupal_set_message("ImageCacheActionsModuleStreamWrapper is read-only and does not support $method", 'error');
  115. watchdog(WATCHDOG_ERROR, "ImageCacheActionsModuleStreamWrapper is read-only and does not support $method");
  116. }
  117. return FALSE;
  118. }
  119. }