MediaReadOnlyStreamWrapper.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * A base class for Resource Stream Wrappers.
  4. *
  5. * This class provides a complete stream wrapper implementation. It passes
  6. * incoming URL's through an interpolation method then recursively calls
  7. * the invoking PHP filesystem function.
  8. *
  9. * MediaReadOnlyStreamWrapper implementations need to override at least the
  10. * interpolateUrl method to rewrite the URL before is it passed back into the
  11. * calling function.
  12. */
  13. abstract class MediaReadOnlyStreamWrapper implements DrupalStreamWrapperInterface {
  14. protected $parameters = array();
  15. protected $base_url = NULL;
  16. private $_DEBUG_MODE = NULL;
  17. public function get_parameters() {
  18. return $this->parameters;
  19. }
  20. // As part of the inode protection mode returned by stat(), identifies the
  21. // file as a regular file, as opposed to a directory, symbolic link, or other
  22. // type of "file".
  23. // @see http://linux.die.net/man/2/stat
  24. const S_IFREG = 0100000;
  25. /**
  26. * "Template" for stat calls. All elements must be initialized.
  27. * @var array
  28. */
  29. protected $_stat = array(
  30. 0 => 0, // device number
  31. 'dev' => 0,
  32. 1 => 0, // inode number
  33. 'ino' => 0,
  34. // inode protection mode. file_unmanaged_delete() requires is_file() to
  35. // return TRUE.
  36. 2 => self::S_IFREG,
  37. 'mode' => self::S_IFREG,
  38. 3 => 0, // number of links
  39. 'nlink' => 0,
  40. 4 => 0, // userid of owner
  41. 'uid' => 0,
  42. 5 => 0, // groupid of owner
  43. 'gid' => 0,
  44. 6 => -1, // device type, if inode device *
  45. 'rdev' => -1,
  46. 7 => 0, // size in bytes
  47. 'size' => 0,
  48. 8 => 0, // time of last access (Unix timestamp)
  49. 'atime' => 0,
  50. 9 => 0, // time of last modification (Unix timestamp)
  51. 'mtime' => 0,
  52. 10 => 0, // time of last inode change (Unix timestamp)
  53. 'ctime' => 0,
  54. 11 => -1, // blocksize of filesystem IO
  55. 'blksize' => -1,
  56. 12 => -1, // number of blocks allocated
  57. 'blocks' => -1,
  58. );
  59. function interpolateUrl() {
  60. if ($parameters = $this->get_parameters()) {
  61. return $this->base_url . '?' . http_build_query($parameters);
  62. }
  63. }
  64. /**
  65. * Returns a web accessible URL for the resource.
  66. *
  67. * This function should return a URL that can be embedded in a web page
  68. * and accessed from a browser. For example, the external URL of
  69. * "youtube://xIpLd0WQKCY" might be
  70. * "http://www.youtube.com/watch?v=xIpLd0WQKCY".
  71. *
  72. * @return
  73. * Returns a string containing a web accessible URL for the resource.
  74. */
  75. public function getExternalUrl() {
  76. return $this->interpolateUrl();
  77. }
  78. /**
  79. * Base implementation of getMimeType().
  80. */
  81. static function getMimeType($uri, $mapping = NULL) {
  82. return 'application/octet-stream';
  83. }
  84. /**
  85. * Base implementation of realpath().
  86. */
  87. function realpath() {
  88. return $this->getExternalUrl();
  89. }
  90. /**
  91. * Stream context resource.
  92. *
  93. * @var Resource
  94. */
  95. public $context;
  96. /**
  97. * A generic resource handle.
  98. *
  99. * @var Resource
  100. */
  101. public $handle = NULL;
  102. /**
  103. * Instance URI (stream).
  104. *
  105. * A stream is referenced as "scheme://target".
  106. *
  107. * @var String
  108. */
  109. protected $uri;
  110. /**
  111. * Base implementation of setUri().
  112. */
  113. function setUri($uri) {
  114. $this->uri = $uri;
  115. $this->parameters = $this->_parse_url($uri);
  116. }
  117. /**
  118. * Base implementation of getUri().
  119. */
  120. function getUri() {
  121. return $this->uri;
  122. }
  123. /**
  124. * Report an error.
  125. * @param $message
  126. * The untranslated string to report.
  127. * @param $options
  128. * An optional array of options to send to t().
  129. * @param $display
  130. * If TRUE, then we display the error to the user.
  131. * @return
  132. * We return FALSE, since we sometimes pass that back from the reporting
  133. * function.
  134. */
  135. private function _report_error($message, $options = array(), $display = FALSE) {
  136. watchdog('resource', $message, $options, WATCHDOG_ERROR);
  137. if ($display) {
  138. drupal_set_message(t($message, $options), 'error');
  139. }
  140. return FALSE;
  141. }
  142. private function _debug($message, $type = 'status') {
  143. if ($this->_DEBUG_MODE) {
  144. drupal_set_message($message, $type);
  145. }
  146. }
  147. /**
  148. * Returns an array of any parameters stored in the URL's path.
  149. * @param $url
  150. * The URL to parse, such as youtube://v/[video-code]/t/[tags+more-tags].
  151. * @return
  152. * An associative array of all the parameters in the path,
  153. * or FALSE if the $url is ill-formed.
  154. */
  155. protected function _parse_url($url) {
  156. $path = explode('://', $url);
  157. $parts = explode('/', $path[1]);
  158. $params = array();
  159. $count = 0;
  160. $total = count($parts);
  161. if (!$total || ($total % 2)) {
  162. // If we have no parts, or an odd number of parts, it's malformed.
  163. return FALSE;
  164. }
  165. while ($count < $total) {
  166. // We iterate count for each step of the assignment to keep us honest.
  167. $params[$parts[$count++]] = $parts[$count++];
  168. }
  169. return $params;
  170. }
  171. /**
  172. * Support for fopen(), file_get_contents(), file_put_contents() etc.
  173. *
  174. * @param $path
  175. * A string containing the path to the file to open.
  176. * @param $mode
  177. * The file mode ("r", "wb" etc.).
  178. * @param $options
  179. * A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
  180. * @param &$opened_path
  181. * A string containing the path actually opened.
  182. * @return
  183. * TRUE if file was opened successfully.
  184. */
  185. public function stream_open($url, $mode, $options, &$opened_url) {
  186. $this->_debug(t('Stream open: %url', array('%url' => $url)));
  187. // We only handle Read-Only mode by default.
  188. if ($mode != 'r' && $mode != 'rb') {
  189. return $this->_report_error('Attempted to open %url as mode: %mode.', array('%url' => $url, '%mode' => $mode), ($options & STREAM_REPORT_ERRORS));
  190. }
  191. // We parse a URL as youtube://v/dsyiufo34/t/cats+dogs to store
  192. // the relevant code(s) in our private array of parameters.
  193. $this->parameters = $this->_parse_url($url);
  194. if ($this->parameters === FALSE) {
  195. return $this->_report_error('Attempted to parse an ill-formed url: %url.', array('%url' => $url), ($options & STREAM_REPORT_ERRORS));
  196. }
  197. if ((bool)$this->parameters && ($options & STREAM_USE_PATH)) {
  198. $opened_url = $url;
  199. }
  200. $this->_debug(t('Stream opened: %parameters', array('%parameters' => print_r($this->parameters, TRUE))));
  201. return (bool)$this->parameters;
  202. }
  203. // Undocumented PHP stream wrapper method.
  204. function stream_lock($operation) {
  205. return FALSE;
  206. }
  207. /**
  208. * Support for fread(), file_get_contents() etc.
  209. *
  210. * @param $count
  211. * Maximum number of bytes to be read.
  212. * @return
  213. * The string that was read, or FALSE in case of an error.
  214. */
  215. public function stream_read($count) {
  216. return FALSE;
  217. }
  218. /**
  219. * Support for fwrite(), file_put_contents() etc.
  220. *
  221. * @param $data
  222. * The string to be written.
  223. * @return
  224. * The number of bytes written.
  225. */
  226. public function stream_write($data) {
  227. return FALSE;
  228. }
  229. /**
  230. * Support for feof().
  231. *
  232. * @return
  233. * TRUE if end-of-file has been reached.
  234. */
  235. public function stream_eof() {
  236. return FALSE;
  237. }
  238. /**
  239. * Support for fseek().
  240. *
  241. * @param $offset
  242. * The byte offset to got to.
  243. * @param $whence
  244. * SEEK_SET, SEEK_CUR, or SEEK_END.
  245. * @return
  246. * TRUE on success
  247. */
  248. public function stream_seek($offset, $whence) {
  249. return FALSE;
  250. }
  251. /**
  252. * Support for fflush().
  253. *
  254. * @return
  255. * TRUE if data was successfully stored (or there was no data to store).
  256. */
  257. public function stream_flush() {
  258. return FALSE;
  259. }
  260. /**
  261. * Support for ftell().
  262. *
  263. * @return
  264. * The current offset in bytes from the beginning of file.
  265. */
  266. public function stream_tell() {
  267. return FALSE;
  268. }
  269. /**
  270. * Support for fstat().
  271. *
  272. * @return
  273. * An array with file status, or FALSE in case of an error - see fstat()
  274. * for a description of this array.
  275. */
  276. public function stream_stat() {
  277. return $this->_stat;
  278. }
  279. /**
  280. * Support for fclose().
  281. *
  282. * @return
  283. * TRUE if stream was successfully closed.
  284. */
  285. public function stream_close() {
  286. return TRUE;
  287. }
  288. /**
  289. * Support for unlink().
  290. *
  291. * @param $uri
  292. * A string containing the uri to the resource to delete.
  293. * @return
  294. * TRUE if resource was successfully deleted.
  295. * @see http://php.net/manual/en/streamwrapper.unlink.php
  296. */
  297. // public function unlink($uri) {
  298. // $this->uri = $uri;
  299. // return unlink($this->getLocalPath());
  300. // }
  301. /**
  302. * Support for rename().
  303. *
  304. * @param $from_uri,
  305. * The uri to the file to rename.
  306. * @param $to_uri
  307. * The new uri for file.
  308. * @return
  309. * TRUE if file was successfully renamed.
  310. * @see http://php.net/manual/en/streamwrapper.rename.php
  311. */
  312. // public function rename($from_uri, $to_uri) {
  313. // return rename($this->getLocalPath($from_uri), $this->getLocalPath($to_uri));
  314. // }
  315. /**
  316. * Support for mkdir().
  317. *
  318. * @param $uri
  319. * A string containing the URI to the directory to create.
  320. * @param $mode
  321. * Permission flags - see mkdir().
  322. * @param $options
  323. * A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
  324. * @return
  325. * TRUE if directory was successfully created.
  326. * @see http://php.net/manual/en/streamwrapper.mkdir.php
  327. */
  328. // public function mkdir($uri, $mode, $options) {
  329. // $this->uri = $uri;
  330. // $recursive = (bool)($options & STREAM_MKDIR_RECURSIVE);
  331. // if ($recursive) {
  332. // // $this->getLocalPath() fails if $uri has multiple levels of directories
  333. // // that do not yet exist.
  334. // $localpath = $this->getDirectoryPath() . '/' . file_uri_target($uri);
  335. // }
  336. // else {
  337. // $localpath = $this->getLocalPath($uri);
  338. // }
  339. // if ($options & STREAM_REPORT_ERRORS) {
  340. // return mkdir($localpath, $mode, $recursive);
  341. // }
  342. // else {
  343. // return @mkdir($localpath, $mode, $recursive);
  344. // }
  345. // }
  346. /**
  347. * Support for rmdir().
  348. *
  349. * @param $uri
  350. * A string containing the URI to the directory to delete.
  351. * @param $options
  352. * A bit mask of STREAM_REPORT_ERRORS.
  353. * @return
  354. * TRUE if directory was successfully removed.
  355. * @see http://php.net/manual/en/streamwrapper.rmdir.php
  356. */
  357. // public function rmdir($uri, $options) {
  358. // $this->uri = $uri;
  359. // if ($options & STREAM_REPORT_ERRORS) {
  360. // return rmdir($this->getLocalPath());
  361. // }
  362. // else {
  363. // return @rmdir($this->getLocalPath());
  364. // }
  365. // }
  366. /**
  367. * Support for stat().
  368. *
  369. * @param $url
  370. * A string containing the url to get information about.
  371. * @param $flags
  372. * A bit mask of STREAM_URL_STAT_LINK and STREAM_URL_STAT_QUIET.
  373. * @return
  374. * An array with file status, or FALSE in case of an error - see fstat()
  375. * for a description of this array.
  376. */
  377. public function url_stat($url, $flags) {
  378. return $this->stream_stat();
  379. }
  380. /**
  381. * Support for opendir().
  382. *
  383. * @param $url
  384. * A string containing the url to the directory to open.
  385. * @param $options
  386. * Unknown (parameter is not documented in PHP Manual).
  387. * @return
  388. * TRUE on success.
  389. */
  390. public function dir_opendir($url, $options) {
  391. return FALSE;
  392. }
  393. /**
  394. * Support for readdir().
  395. *
  396. * @return
  397. * The next filename, or FALSE if there are no more files in the directory.
  398. */
  399. public function dir_readdir() {
  400. return FALSE;
  401. }
  402. /**
  403. * Support for rewinddir().
  404. *
  405. * @return
  406. * TRUE on success.
  407. */
  408. public function dir_rewinddir() {
  409. return FALSE;
  410. }
  411. /**
  412. * Support for closedir().
  413. *
  414. * @return
  415. * TRUE on success.
  416. */
  417. public function dir_closedir() {
  418. return FALSE;
  419. }
  420. public function getDirectoryPath() {
  421. return '';
  422. }
  423. /**
  424. * DrupalStreamWrapperInterface requires that these methods be implemented,
  425. * but none of them apply to a read-only stream wrapper. On failure they
  426. * are expected to return FALSE.
  427. */
  428. public function unlink($uri) {
  429. // Although the remote file itself can't be deleted, return TRUE so that
  430. // file_delete() can remove the file record from the database.
  431. return TRUE;
  432. }
  433. public function rename($from_uri, $to_uri) {
  434. return FALSE;
  435. }
  436. public function mkdir($uri, $mode, $options) {
  437. return FALSE;
  438. }
  439. public function rmdir($uri, $options) {
  440. return FALSE;
  441. }
  442. public function chmod($mode) {
  443. return FALSE;
  444. }
  445. public function dirname($uri = NULL) {
  446. return FALSE;
  447. }
  448. }