/extensions/WebStore/store.php

https://github.com/brion/mediawiki-svn · PHP · 77 lines · 54 code · 14 blank · 9 comment · 6 complexity · 1d5244cce26831013955e58b0dde225c MD5 · raw file

  1. <?php
  2. /**
  3. * Store a file to a temporary, private location
  4. *
  5. * TODO: expiration
  6. */
  7. require( dirname( __FILE__ ) . '/WebStoreStart.php' );
  8. class WebStoreStore extends WebStoreCommon {
  9. function execute() {
  10. global $wgRequest;
  11. if ( !$wgRequest->wasPosted() ) {
  12. echo $this->dtd();
  13. echo <<<EOT
  14. <html>
  15. <head><title>store.php Test Interface</title></head>
  16. <body>
  17. <form method="post" action="store.php" enctype="multipart/form-data" >
  18. <p>File: <input type="file" name="file"/></p>
  19. <p><input type="submit" value="OK"/></p>
  20. </form></body></html>
  21. EOT;
  22. return true;
  23. }
  24. $srcFile = $wgRequest->getFileTempname( 'file' );
  25. if ( !$srcFile ) {
  26. $this->error( 400, 'webstore_no_file' );
  27. return false;
  28. }
  29. // Use an hourly timestamped directory for easy cleanup
  30. $now = time();
  31. $this->cleanupTemp( $now );
  32. $timestamp = gmdate( self::$tempDirFormat, $now );
  33. if ( !wfMkdirParents( "{$this->tmpDir}/$timestamp" ) ) {
  34. $this->error( 500, 'webstore_dest_mkdir' );
  35. return false;
  36. }
  37. // Get the extension of the upload, needs to be preserved for type detection
  38. $name = $wgRequest->getFileName( 'file' );
  39. $n = strrpos( $name, '.' );
  40. if ( $n ) {
  41. $extension = '.' . File::normalizeExtension( substr( $name, $n + 1 ) );
  42. } else {
  43. $extension = '';
  44. }
  45. // Pick a random temporary path
  46. $destRel = $timestamp . '/' . md5( mt_rand() . mt_rand() . mt_rand() ) . $extension;
  47. if ( !@move_uploaded_file( $srcFile, "{$this->tmpDir}/$destRel" ) ) {
  48. $this->error( 400, 'webstore_move_uploaded', $srcFile, "{$this->tmpDir}/$destRel" );
  49. return false;
  50. }
  51. // Succeeded, return temporary location
  52. header( 'Content-Type: text/xml' );
  53. echo <<<EOT
  54. <?xml version="1.0" encoding="utf-8"?>
  55. <response>
  56. <location>$destRel</location>
  57. </response>
  58. EOT;
  59. return true;
  60. }
  61. }
  62. $s = new WebStoreStore;
  63. $s->executeCommon();