/** * Upload a file on the server for use with google Gears multi upload in chunks. * We return a string with the %age uploaded so far. * @return void * @todo make the resume possible * @todo check if the file we try to upload is already there * @todo check if the file we upload is valid */ public function uploadfileAction() { /** * upload.php * * Copyright 2009, Moxiecode Systems AB * Released under GPL License. * * License: http://www.plupload.com/license * Contributing: http://www.plupload.com/contributing */ // Settings //$targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload"; $fullpath = Sydney_Tools_Paths::getAppdataPath() . '/adminfiles/'; $uploadedFileName = $_FILES['file']['name']; // check if appdata and adminfiles exist and if not, create dir if (!is_dir(Sydney_Tools_Paths::getAppdataPath())) { mkdir(Sydney_Tools_Paths::getAppdataPath()); chmod(Sydney_Tools_Paths::getAppdataPath(), 0777); } if (!is_dir($fullpath)) { mkdir($fullpath); chmod($fullpath, 0777); } $ndirn = substr($uploadedFileName, -3); $ndirn = preg_replace('/\\./', '', $ndirn); $nnd = $fullpath . '' . strtoupper($ndirn); $type = strtoupper($ndirn); if (!is_dir($nnd)) { mkdir($nnd); } $targetDir = $nnd; // 5 minutes execution time set_time_limit(5 * 60); // Get parameters $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0; $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0; $fileName = isset($_FILES['file']["name"]) ? $_FILES['file']["name"] : $_REQUEST["name"]; // Clean the fileName for security reasons $fileName = Sydney_Medias_Utils::sanitizeFilename($fileName); // Create target dir if (!file_exists($targetDir)) { mkdir($targetDir); } $contentType = ''; // Look for the content type header if (isset($_SERVER["HTTP_CONTENT_TYPE"])) { $contentType = $_SERVER["HTTP_CONTENT_TYPE"]; } if (isset($_SERVER["CONTENT_TYPE"])) { $contentType = $_SERVER["CONTENT_TYPE"]; } // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5 if (strpos($contentType, "multipart") !== false) { if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { // Open temp file $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen($_FILES['file']['tmp_name'], "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } fclose($in); fclose($out); unlink($_FILES['file']['tmp_name']); } else { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } } else { die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); } } else { // Open temp file $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $in = fopen("php://input", "rb"); if ($in) { while ($buff = fread($in, 4096)) { fwrite($out, $buff); } } else { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } fclose($in); fclose($out); } else { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } } // Return JSON-RPC response $fil = new Filfiles(); $fileId = $fil->registerFileToDb($targetDir, $fileName, filesize($targetDir . '/' . $fileName), $type, $this->usersId, $this->safinstancesId, $this->getRequest()); die('{"jsonrpc" : "2.0", "result" : null, "id" : ' . Zend_Json_Encoder::encode($fileId) . ' }'); }
/** * Move a file from a directory to the final appdata of the webinstance * and register it to the DB * * @param string $filepath Path to the temp files * @param string $comment Comment to place in the meta description (for search purpose, for ex the name of the user) * @param string $folder The name of the folder to push the files in * @return bool|int */ public function fileToFileManager($filepath = '', $comment = '', $folder = 'temp') { if (!file_exists($filepath)) { return false; } try { $fileObj = Sydney_Medias_Filetypesfactory::createfiletype($filepath); $newName = uniqid() . '_' . Sydney_Medias_Utils::sanitizeFilename($fileObj->basename . '.' . $fileObj->extension); $newPath = Sydney_Tools_Paths::getAppdataPath() . '/adminfiles/' . $fileObj->extension . '/'; if (!is_dir($newPath)) { mkdir($newPath, 0777, true); } rename($filepath, $newPath . $newName); $fileName = $newName; $newFileObj = Sydney_Medias_Filetypesfactory::createfiletype($newPath . $newName); $fileInfo = $newFileObj->getFileinfo(); $fileWeight = $fileInfo['general.filesize']; // On récupère la taille du fichier pour pouvoir l'ajouter en DB $type = $newFileObj->extension; $usersId = Sydney_Tools_User::getUserdata('users_id'); // @todo TODO we ll have to change that, for now it uploads the file as Arnaud (user id 1) if nothing is defined. if ($usersId === false) { $usersId = 1; } $safinstancesId = Sydney_Tools_Sydneyglobals::getSafinstancesId(); // save the file to DB $fileFilesId = $this->registerFileToDb($newPath, $fileName, $fileWeight, $type, $usersId, $safinstancesId, array(), $comment); // put them in the right folder $filefoldersDb = new Filfolders(); $filefoldersId = $filefoldersDb->addSystemFolder($folder); if ($fileFilesId) { $fileCorDb = new FilfoldersFilfiles(); $fileCor = $fileCorDb->createRow(); $fileCor->filfolders_id = $filefoldersId; $fileCor->filfiles_id = $fileFilesId; $fileCor->save(); } // returns the files ids return $fileFilesId; } catch (Exception $e) { Zend_Debug::dump($e->getMessage()); return false; } }