public function init()
 {
     $this->_WeTransfer_Upload = new WeTransfer_Upload();
     if (IS_MOBILE) {
         $this->view->setScriptPath(VIEWS_DIR . '/mobile/');
     }
     // START:	chunk handling
     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
     header('Cache-Control: no-store, no-cache, must-revalidate');
     header('Cache-Control: post-check=0, pre-check=0', false);
     header('Pragma: no-cache');
     $uuid = trim($_POST['uuid']);
     $cleanupTargetDir = true;
     $targetDir = BASEDIR . '/data/temp/' . $uuid;
     // Temp file age in seconds
     $maxFileAge = 5 * 3600;
     $chunk = isset($_REQUEST['chunk']) ? intval($_REQUEST['chunk']) : 0;
     $chunks = isset($_REQUEST['chunks']) ? intval($_REQUEST['chunks']) : 0;
     // create target directory
     if (!file_exists($targetDir)) {
         @mkdir($targetDir);
     }
     // check for filename
     if (isset($_REQUEST['name'])) {
         $fileName = $_REQUEST['name'];
     } elseif (!empty($_FILES)) {
         $fileName = $_FILES['file']['name'];
     } else {
         $fileName = uniqid('file_');
     }
     $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
     // Remove old temp files
     if ($cleanupTargetDir) {
         if (!is_dir($targetDir) || !($dir = opendir($targetDir))) {
             die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
         }
         while (($file = readdir($dir)) !== false) {
             $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
             // if temp file is current file proceed to the next
             if ($tmpfilePath == "{$filePath}.part") {
                 continue;
             }
             // remove temp file if it is older than the max age and is not the current file
             if (preg_match('/\\.part$/', $file) && filemtime($tmpfilePath) < time() - $maxFileAge) {
                 @unlink($tmpfilePath);
             }
         }
         closedir($dir);
     }
     // open temp file
     if (!($out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb"))) {
         die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
     }
     if (!empty($_FILES)) {
         if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
             die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
         }
         // read binary input stream and append it to temp file
         if (!($in = @fopen($_FILES["file"]["tmp_name"], "rb"))) {
             die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
         }
     } else {
         if (!($in = @fopen("php://input", "rb"))) {
             die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
         }
     }
     while ($buff = fread($in, 4096)) {
         fwrite($out, $buff);
     }
     @fclose($out);
     @fclose($in);
     // check if file has been uploaded
     if (!$chunks || $chunk == $chunks - 1) {
         // strip the temp .part suffix off
         rename("{$filePath}.part", $filePath);
     } else {
         // return Success JSON-RPC response
         exit(json_encode(array('chunk' => $_REQUEST['chunk'], 'totalChunks' => $_POST['chunks'])));
     }
     // END:		chunk handling
     if (!empty($_POST) and !empty($_FILES)) {
         if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
             $fileData = array();
             $fileData['userId'] = (int) $_SESSION['user']['id'];
             $fileData['tmp_name'] = $_FILES['file']['tmp_name'];
             $fileData['uuid'] = $_POST['uuid'];
             $fileData['filename'] = fetchFilename($filePath);
             $fileData['fileExt'] = fetchFileExt($filePath);
             $fileData['uploader_ip'] = $_SERVER['REMOTE_ADDR'];
             if (isAllowedFileType($fileData['fileExt'])) {
                 header('Content-type: application/json');
                 $json = array();
                 $json['status'] = 'OK';
                 exit(json_encode($json));
                 //exit( $this->_WeTransfer_Upload->handleTempFileUpload( $filePath, $fileData ) );
                 //exit( $this->_WeTransfer_Upload->handleTempFileUpload( $fileData['tmp_name'], $fileData ) );
             } else {
                 $json = array();
                 $json['status'] = 'ERROR';
                 $json['error'] = 'FILE_TYPE_NOT_PERMITTED';
                 header('Content-type: application/json');
                 exit(json_encode($json));
             }
         }
     } elseif (!empty($_POST['formParams']['email']) && !empty($_POST['formParams']['recipients'])) {
         header('Content-type: application/json');
         exit($this->_WeTransfer_Upload->completeFileUpload($_POST['formParams']));
     } else {
         $json = array();
         $json['status'] = 'ERROR';
         $json['error'] = 'FILE_UPLOAD_ERROR';
         header('Content-type: application/json');
         exit(json_encode($json));
     }
 }
Пример #2
0
 public function uploadOwnAvatar()
 {
     if (!empty($_FILES)) {
         if ($_FILES['myNewAvatar']['error'] == UPLOAD_ERR_OK) {
             if (!is_image($_FILES['myNewAvatar']['tmp_name'])) {
                 return array('status' => 'error', 'error' => 'NOT_IMAGE');
             }
             $fileExt = strtolower(fetchFileExt($_FILES['myNewAvatar']['name']));
             $wideImage = 'avatar-wideImageTemp.' . $fileExt;
             $filename = 'avatar-temp.' . $fileExt;
             $destinationTemp = BASEDIR . '/' . SITE_UPLOAD_DIR_USERS . '/' . $_SESSION['user']['id'] . '/' . $wideImage;
             $destination = BASEDIR . '/' . SITE_UPLOAD_DIR_USERS . '/' . $_SESSION['user']['id'] . '/' . $filename;
             $moved = move_uploaded_file($_FILES['myNewAvatar']['tmp_name'], $destinationTemp);
             if ($moved) {
                 if ($fileExt != 'gif') {
                     require_once 'WideImage/WideImage.php';
                     WideImage::load($destinationTemp)->resize(SITE_AVATAR_WIDTH, SITE_AVATAR_HEIGHT, 'inside')->saveToFile($destination);
                 } else {
                     rename($destinationTemp, $destination);
                 }
                 unlink($destinationTemp);
                 return array('status' => 'OK', 'url' => BASEURL . '/' . SITE_UPLOAD_DIR_USERS . '/' . $_SESSION['user']['id'] . '/' . $filename);
             } else {
                 return array('status' => 'error', 'error' => 'UPLOAD', 'error_code' => 'UPLOAD_FAILED');
             }
         }
     } else {
         return array('status' => 'error', 'error' => 'NO_FILE_UPLOADED', 'error_code' => $_FILES['myNewAvatar']['error']);
     }
 }