PageRenderTime 30ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/arouestyinicio/lib/upload.php

https://bitbucket.org/gente21apps/arouesty
PHP | 162 lines | 89 code | 32 blank | 41 comment | 25 complexity | da49c74cabb0f82d21c611d17eab0d95 MD5 | raw file
Possible License(s): MIT, Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * upload.php
  4. *
  5. * Copyright 2013, Moxiecode Systems AB
  6. * Released under GPL License.
  7. *
  8. * License: http://www.plupload.com/license
  9. * Contributing: http://www.plupload.com/contributing
  10. */
  11. #!! IMPORTANT:
  12. #!! this file is just an example, it doesn't incorporate any security checks and
  13. #!! is not recommended to be used in production environment as it is. Be sure to
  14. #!! revise it and customize to your needs.
  15. // Make sure file is not cached (as it happens for example on iOS devices)
  16. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  17. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  18. header("Cache-Control: no-store, no-cache, must-revalidate");
  19. header("Cache-Control: post-check=0, pre-check=0", false);
  20. header("Pragma: no-cache");
  21. /*
  22. // Support CORS
  23. header("Access-Control-Allow-Origin: *");
  24. // other CORS headers if any...
  25. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  26. exit; // finish preflight CORS requests here
  27. }
  28. */
  29. // 5 minutes execution time
  30. @set_time_limit(5 * 60);
  31. // Uncomment this one to fake upload time
  32. // usleep(5000);
  33. // Settings
  34. //$targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  35. $targetDir = '../../uploads';
  36. $cleanupTargetDir = true; // Remove old files
  37. $maxFileAge = 5 * 3600; // Temp file age in seconds
  38. // Create target dir
  39. if (!file_exists($targetDir)) {
  40. @mkdir($targetDir);
  41. }
  42. // Get a file name
  43. $fileName = $_POST['userId'].'.jpg';
  44. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  45. // Chunking might be enabled
  46. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  47. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
  48. // Remove old temp files
  49. if ($cleanupTargetDir) {
  50. if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
  51. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  52. }
  53. while (($file = readdir($dir)) !== false) {
  54. $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  55. // If temp file is current file proceed to the next
  56. if ($tmpfilePath == "{$filePath}.part") {
  57. continue;
  58. }
  59. // Remove temp file if it is older than the max age and is not the current file
  60. if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
  61. @unlink($tmpfilePath);
  62. }
  63. }
  64. closedir($dir);
  65. }
  66. //do the GD thing///////////////////////////////////////////////////
  67. //$filePath
  68. function thumb($path, $fileName, $size){
  69. $source_img = @imagecreatefromjpeg($path); //Create a copy of our image for our thumbnails...
  70. if (!$source_img) {
  71. echo "could not create image handle";
  72. exit(0);
  73. }
  74. $new_w = $size;
  75. $new_h = $size;
  76. $orig_w = imagesx($source_img);
  77. $orig_h = imagesy($source_img);
  78. $w_ratio = ($new_w / $orig_w);
  79. $h_ratio = ($new_h / $orig_h);
  80. if ($orig_w > $orig_h ) {//landscape
  81. $crop_w = round($orig_w * $h_ratio);
  82. $crop_h = $new_h;
  83. } elseif ($orig_w < $orig_h ) {//portrait
  84. $crop_h = round($orig_h * $w_ratio);
  85. $crop_w = $new_w;
  86. } else {//square
  87. $crop_w = $new_w;
  88. $crop_h = $new_h;
  89. }
  90. $dest_img = imagecreatetruecolor($new_w,$new_h);
  91. imagecopyresampled($dest_img, $source_img, 0 , 0 , 0, 0, $crop_w, $crop_h, $orig_w, $orig_h);
  92. if(imagejpeg($dest_img, "../../uploads/t600/".$fileName, 95)) {
  93. imagedestroy($dest_img);
  94. imagedestroy($source_img);
  95. } else {
  96. echo "could not make thumbnail image";
  97. exit(0);
  98. }
  99. }
  100. //do the GD thing///////////////////////////////////////////////////
  101. // Open temp file
  102. if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
  103. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  104. }
  105. if (!empty($_FILES)) {
  106. if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
  107. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  108. }
  109. // Read binary input stream and append it to temp file
  110. if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
  111. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  112. }
  113. } else {
  114. if (!$in = @fopen("php://input", "rb")) {
  115. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  116. }
  117. }
  118. while ($buff = fread($in, 4096)) {
  119. fwrite($out, $buff);
  120. }
  121. @fclose($out);
  122. @fclose($in);
  123. // Check if file has been uploaded
  124. if (!$chunks || $chunk == $chunks - 1) {
  125. // Strip the temp .part suffix off
  126. rename("{$filePath}.part", $filePath);
  127. thumb($filePath, $fileName, 600);
  128. }
  129. // Return Success JSON-RPC response
  130. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');