You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

share_v2.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. PHP script to handle file uploads and downloads for Prosody's mod_http_upload_external
  4. Tested with Apache 2.2+ and PHP 5.3+
  5. ** Why this script?
  6. This script only allows uploads that have been authorized by mod_http_upload_external. It
  7. attempts to make the upload/download as safe as possible, considering that there are *many*
  8. security concerns involved with allowing arbitrary file upload/download on a web server.
  9. With that said, I do not consider myself a PHP developer, and at the time of writing, this
  10. code has had no external review. Use it at your own risk. I make no claims that this code
  11. is secure.
  12. ** How to use?
  13. Drop this file somewhere it will be served by your web server. Edit the config options below.
  14. In Prosody set:
  15. http_upload_external_base_url = "https://your.example.com/path/to/share.php/"
  16. http_upload_external_secret = "this is your secret string"
  17. http_upload_external_protocol = "v2";
  18. ** License
  19. (C) 2016 Matthew Wild <mwild1@gmail.com>
  20. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  21. and associated documentation files (the "Software"), to deal in the Software without restriction,
  22. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  24. subject to the following conditions:
  25. The above copyright notice and this permission notice shall be included in all copies or substantial
  26. portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  28. BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  30. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  34. /* CONFIGURATION OPTIONS */
  35. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  36. /* Change this to a directory that is writable by your web server, but is outside your web root */
  37. $CONFIG_STORE_DIR = '/tmp';
  38. /* This must be the same as 'http_upload_external_secret' that you set in Prosody's config file */
  39. $CONFIG_SECRET = 'this is your secret string';
  40. /* For people who need options to tweak that they don't understand... here you are */
  41. $CONFIG_CHUNK_SIZE = 4096;
  42. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  43. /* END OF CONFIGURATION */
  44. /*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  45. /* Do not edit below this line unless you know what you are doing (spoiler: nobody does) */
  46. //$upload_file_name = substr($_SERVER['PHP_SELF'], strlen($_SERVER['SCRIPT_NAME'])+1);
  47. $upload_file_name = $_SERVER['UPLOADED_FILENAME'];
  48. $store_file_name = $CONFIG_STORE_DIR . '/store-' . hash('sha256', $upload_file_name);
  49. $request_method = $_SERVER['REQUEST_METHOD'];
  50. /* Set CORS headers */
  51. header('Access-Control-Allow-Methods: GET, PUT, OPTIONS');
  52. header('Access-Control-Allow-Headers: Content-Type');
  53. header('Access-Control-Max-Age: 7200');
  54. header('Access-Control-Allow-Origin: *');
  55. if(array_key_exists('v2', $_GET) === TRUE && $request_method === 'PUT') {
  56. // error_log(var_export($_SERVER, TRUE));
  57. $upload_file_size = $_SERVER['CONTENT_LENGTH'];
  58. $upload_token = $_GET['v2'];
  59. if(array_key_exists('CONTENT_TYPE', $_SERVER) === TRUE) {
  60. $upload_file_type = $_SERVER['CONTENT_TYPE'];
  61. } else {
  62. $upload_file_type = 'application/octet-stream';
  63. }
  64. // Imagine being able to store the file data in the content-type!
  65. if(strlen($upload_file_type) > 255) {
  66. header('HTTP/1.0 400 Bad Request');
  67. exit;
  68. }
  69. $calculated_token = hash_hmac('sha256', "$upload_file_name\0$upload_file_size\0$upload_file_type", $CONFIG_SECRET);
  70. if(function_exists('hash_equals')) {
  71. if(hash_equals($calculated_token, $upload_token) !== TRUE) {
  72. error_log("Token mismatch: calculated $calculated_token got $upload_token");
  73. header('HTTP/1.0 403 Forbidden');
  74. exit;
  75. }
  76. }
  77. else {
  78. if($upload_token !== $calculated_token) {
  79. error_log("Token mismatch: calculated $calculated_token got $upload_token");
  80. header('HTTP/1.0 403 Forbidden');
  81. exit;
  82. }
  83. }
  84. /* Open a file for writing */
  85. $store_file = fopen($store_file_name, 'x');
  86. if($store_file === FALSE) {
  87. header('HTTP/1.0 409 Conflict');
  88. exit;
  89. }
  90. /* PUT data comes in on the stdin stream */
  91. $incoming_data = fopen('php://input', 'r');
  92. /* Read the data a chunk at a time and write to the file */
  93. while ($data = fread($incoming_data, $CONFIG_CHUNK_SIZE)) {
  94. fwrite($store_file, $data);
  95. }
  96. /* Close the streams */
  97. fclose($incoming_data);
  98. fclose($store_file);
  99. file_put_contents($store_file_name.'-type', $upload_file_type);
  100. } else if($request_method === 'GET' || $request_method === 'HEAD') {
  101. // Send file (using X-Sendfile would be nice here...)
  102. if(file_exists($store_file_name)) {
  103. $mime_type = file_get_contents($store_file_name.'-type');
  104. if($mime_type === FALSE) {
  105. $mime_type = 'application/octet-stream';
  106. header('Content-Disposition: attachment');
  107. }
  108. header('Content-Type: '.$mime_type);
  109. header('Content-Length: '.filesize($store_file_name));
  110. header("Content-Security-Policy: \"default-src 'none'\"");
  111. header("X-Content-Security-Policy: \"default-src 'none'\"");
  112. header("X-WebKit-CSP: \"default-src 'none'\"");
  113. if($request_method !== 'HEAD') {
  114. readfile($store_file_name);
  115. }
  116. } else {
  117. header('HTTP/1.0 404 Not Found');
  118. }
  119. } else if($request_method === 'OPTIONS') {
  120. } else {
  121. header('HTTP/1.0 400 Bad Request');
  122. }
  123. exit;