Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.11 KB | None | 0 0
  1. <?php
  2. require_once( dirname(__FILE__) . '/../../../../../../../wp-load.php' );
  3. $upload_dir = wp_upload_dir();
  4. /**
  5.  * Handle file uploads via XMLHttpRequest
  6.  */
  7. class qqUploadedFileXhr {
  8.     /**
  9.      * Save the file to the specified path
  10.      * @return boolean TRUE on success
  11.      */
  12.     function save($path) {
  13.         $input = fopen("php://input", "r");
  14.         $temp = tmpfile();
  15.         $realSize = stream_copy_to_stream($input, $temp);
  16.         fclose($input);
  17.  
  18.         if ($realSize != $this->getSize()){
  19.             return false;
  20.         }
  21.  
  22.         $target = fopen($path, "w");
  23.         fseek($temp, 0, SEEK_SET);
  24.         stream_copy_to_stream($temp, $target);
  25.         fclose($target);
  26.  
  27.         return true;
  28.     }
  29.     function getName() {
  30.         return $_GET['qqfile'];
  31.     }
  32.     function getSize() {
  33.         if (isset($_SERVER["CONTENT_LENGTH"])){
  34.             return (int)$_SERVER["CONTENT_LENGTH"];
  35.         } else {
  36.             throw new Exception('Getting content length is not supported.');
  37.         }
  38.     }
  39. }
  40.  
  41. /**
  42.  * Handle file uploads via regular form post (uses the $_FILES array)
  43.  */
  44. class qqUploadedFileForm {
  45.     /**
  46.      * Save the file to the specified path
  47.      * @return boolean TRUE on success
  48.      */
  49.     function save($path) {
  50.         if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
  51.             return false;
  52.         }
  53.         return true;
  54.     }
  55.     function getName() {
  56.         return $_FILES['qqfile']['name'];
  57.     }
  58.     function getSize() {
  59.         return $_FILES['qqfile']['size'];
  60.     }
  61. }
  62.  
  63. class qqFileUploader {
  64.     private $allowedExtensions = array();
  65.     private $sizeLimit = 10485760;
  66.     private $file;
  67.  
  68.     function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
  69.         $allowedExtensions = array_map("strtolower", $allowedExtensions);
  70.  
  71.         $this->allowedExtensions = $allowedExtensions;
  72.         $this->sizeLimit = $sizeLimit;
  73.  
  74.        // $this->checkServerSettings();
  75.  
  76.         if (isset($_GET['qqfile'])) {
  77.             $this->file = new qqUploadedFileXhr();
  78.         } elseif (isset($_FILES['qqfile'])) {
  79.             $this->file = new qqUploadedFileForm();
  80.         } else {
  81.             $this->file = false;
  82.         }
  83.     }
  84.  
  85.     private function checkServerSettings(){
  86.         $postSize = $this->toBytes(ini_get('post_max_size'));
  87.         $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
  88.  
  89.         if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
  90.             $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
  91.             die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
  92.         }
  93.     }
  94.  
  95.     private function toBytes($str){
  96.         $val = trim($str);
  97.         $last = strtolower($str[strlen($str)-1]);
  98.         switch($last) {
  99.             case 'g': $val *= 1024;
  100.             case 'm': $val *= 1024;
  101.             case 'k': $val *= 1024;
  102.         }
  103.         return $val;
  104.     }
  105.  
  106.     /**
  107.      * Returns array('success'=>true) or array('error'=>'error message')
  108.      */
  109.     function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
  110.         if (!is_writable($uploadDirectory)){
  111.             return array('error' => "Server error. Upload directory isn't writable.");
  112.         }
  113.  
  114.         if (!$this->file){
  115.             return array('error' => 'No files were uploaded.');
  116.         }
  117.  
  118.         $size = $this->file->getSize();
  119.  
  120.         if ($size == 0) {
  121.             return array('error' => 'File is empty');
  122.         }
  123.  
  124.         if ($size > $this->sizeLimit) {
  125.             return array('error' => 'File is too large');
  126.         }
  127.  
  128.         $pathinfo = pathinfo($this->file->getName());
  129.         $filename = $pathinfo['filename'];
  130.         //$filename = md5(uniqid());
  131.         $ext = $pathinfo['extension'];
  132.  
  133.         if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
  134.             $these = implode(', ', $this->allowedExtensions);
  135.             return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
  136.         }
  137.  
  138.         if(!$replaceOldFile){
  139.             /// don't overwrite previous files that were uploaded
  140.             while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
  141.                 $filename .= rand(10, 99);
  142.             }
  143.         }
  144.  
  145.         if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
  146.                 global $upload_dir;
  147.             return array('success'=>true , 'url' =>$upload_dir['url'].'/'.$filename . '.' . $ext, 'path' => $upload_dir['path']);
  148.         } else {
  149.             return array('error'=> 'Could not save uploaded file.' .
  150.                 'The upload was cancelled, or server error encountered');
  151.         }
  152.  
  153.     }
  154. }
  155.  
  156. // list of valid extensions, ex. array("jpeg", "xml", "bmp")
  157. $allowedExtensions = array();
  158. // max file size in bytes
  159. $sizeLimit = 10 * 1024 * 1024;
  160.  
  161. $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
  162. $result = $uploader->handleUpload($upload_dir['path']."/");
  163. // to pass data through iframe you will need to encode all html tags
  164. echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement