PageRenderTime 110ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 0ms

/application/protected/extensions/actions/PluploadAction.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 145 lines | 86 code | 16 blank | 43 comment | 15 complexity | f9c3c9bea1b72f016f3c0ece33031940 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * PluploadAction class file.
  4. *
  5. * @author Jon Doe <jonny@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * PluploadAction is ...
  12. *
  13. *
  14. * @author Jon Doe <jonny@gmail.com>
  15. * @version
  16. * @package
  17. * @since 1.0
  18. */
  19. class PluploadAction extends CAction
  20. {
  21. /**
  22. * Target upload directory
  23. * @var string
  24. */
  25. public $targetDir = null;
  26. /**
  27. * Whether to remove old files or not
  28. * @var boolean
  29. */
  30. public $cleanup = false;
  31. /**
  32. * Temp file age in seconds
  33. * @var int
  34. */
  35. public $fileAge = 3600; // Temp file age in seconds
  36. public function run()
  37. {
  38. header('Content-type: text/plain; charset=UTF-8');
  39. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  40. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  41. header("Cache-Control: no-store, no-cache, must-revalidate");
  42. header("Cache-Control: post-check=0, pre-check=0", false);
  43. header("Pragma: no-cache");
  44. if (is_null($this->targetDir)) $this->targetDir = isset($_GET["targetDir"]) ? urldecode($_GET["targetDir"]) : sys_get_temp_dir();
  45. Yii::log("Upload directory: " . $this->targetDir, "trace");
  46. // 5 minutes execution time
  47. @set_time_limit(5 * 60);
  48. // usleep(5000);
  49. // Get parameters
  50. $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
  51. $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
  52. $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
  53. // Clean the fileName for security reasons
  54. $fileName = preg_replace('/[^\w\._\s]+/', '', $fileName);
  55. if (!file_exists($this->targetDir))
  56. @mkdir($this->targetDir);
  57. // Remove old temp files
  58. if (is_dir($this->targetDir) && ($dir = opendir($this->targetDir))) {
  59. while (($file = readdir($dir)) !== false) {
  60. $filePath = $this->targetDir . DIRECTORY_SEPARATOR . $file;
  61. // Remove temp files if they are older than the max age
  62. if (preg_match('/\\.tmp$/', $file) && (filemtime($filePath) < time() - $this->fileAge))
  63. @unlink($filePath);
  64. }
  65. closedir($dir);
  66. } else
  67. throw new CHttpException (500, Yii::t('app', "Can't open temporary directory :dir", array(':dir' => $this->targetDir)));
  68. // Look for the content type header
  69. if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
  70. $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
  71. if (isset($_SERVER["CONTENT_TYPE"]))
  72. $contentType = $_SERVER["CONTENT_TYPE"];
  73. if (strpos($contentType, "multipart") !== false) {
  74. if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
  75. // Open temp file
  76. $out = fopen($this->targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  77. if ($out) {
  78. // Read binary input stream and append it to temp file
  79. $in = fopen($_FILES['file']['tmp_name'], "rb");
  80. if ($in) {
  81. while ($buff = fread($in, 4096))
  82. fwrite($out, $buff);
  83. } else
  84. throw new CHttpException (500, Yii::t('app', "Can't open input stream."));
  85. fclose($out);
  86. @unlink($_FILES['file']['tmp_name']);
  87. } else
  88. throw new CHttpException (500, Yii::t('app', "Can't open output stream."));
  89. } else
  90. throw new CHttpException (500, Yii::t('app', "Can't move uploaded file."));
  91. } else {
  92. // Open temp file
  93. $out = fopen($this->targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  94. if ($out) {
  95. // Read binary input stream and append it to temp file
  96. $in = fopen("php://input", "rb");
  97. if ($in) {
  98. while ($buff = fread($in, 4096))
  99. fwrite($out, $buff);
  100. } else
  101. throw new CHttpException (500, Yii::t('app', "Can't open input stream."));
  102. fclose($out);
  103. } else
  104. throw new CHttpException (500, Yii::t('app', "Can't open output stream."));
  105. }
  106. // After last chunk is received, process the file
  107. $ret = array('result' => '1');
  108. if (intval($chunk) + 1 >= intval($chunks)) {
  109. $originalname = $fileName;
  110. if (isset($_SERVER['HTTP_CONTENT_DISPOSITION'])) {
  111. $arr = array();
  112. preg_match('@^attachment; filename="([^"]+)"@',$_SERVER['HTTP_CONTENT_DISPOSITION'],$arr);
  113. if (isset($arr[1]))
  114. $originalname = $arr[1];
  115. }
  116. // **********************************************************************************************
  117. // Do whatever you need with the uploaded file, which has $originalname as the original file name
  118. // and is located at $this->targetDir . DIRECTORY_SEPARATOR . $fileName
  119. // **********************************************************************************************
  120. }
  121. // Return response
  122. die(json_encode($ret));
  123. }
  124. }