Esempio n. 1
0
 /**
  * Returns array('success'=>true) or array('error'=>'error message')
  */
 function handleUpload($uploadDirectory, $random_dir, $p_api_id, $replaceOldFile = FALSE)
 {
     $uploadDirectory .= '/' . $random_dir . '/';
     if (!is_writable($uploadDirectory)) {
         return array('error' => "Server error. Upload directory isn't writable.");
     }
     if (!$this->file) {
         return array('error' => 'No files were uploaded.');
     }
     $size = $this->file->getSize();
     if ($size == 0) {
         return array('error' => 'File is empty');
     }
     if ($size > $this->sizeLimit) {
         return array('error' => 'File is too large');
     }
     $pathinfo = pathinfo($this->file->getName());
     $filename = $pathinfo['filename'];
     //$filename = md5(uniqid());
     $ext = @$pathinfo['extension'];
     // hide notices if extension is empty
     if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
         $these = implode(', ', $this->allowedExtensions);
         return array('error' => 'File has an invalid extension, it should be one of ' . $these . '.');
     }
     $ext = $ext == '' ? $ext : '.' . $ext;
     if (!$replaceOldFile) {
         /// don't overwrite previous files that were uploaded
         while (file_exists($uploadDirectory . $filename . $ext)) {
             $filename .= rand(10, 99);
         }
     }
     $this->uploadName = $filename . $ext;
     if ($this->file->save($uploadDirectory . $filename . $ext)) {
         // If file pushed to temp directory, push it to package server
         $filename = $filename . $ext;
         $upload_tmp_dir = sys_get_temp_dir();
         $files = array();
         $file = $upload_tmp_dir . '/' . $random_dir . '/' . $filename;
         // Read and put content of $file to $filebinary
         $filebinary = fread(fopen($file, "r"), filesize($file));
         $files[] = array("filename" => $filename, "filebinary" => base64_encode($filebinary));
         $push_package_result = pushPackage($p_api_id, $random_dir, $files);
         // Delete package from PHP /tmp dir
         delete_directory($upload_tmp_dir . '/' . $random_dir);
         if (!isXMLRPCError() and $push_package_result) {
             return array('success' => true);
         } else {
             return array('error' => 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
         }
     } else {
         return array('error' => 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
     }
 }
Esempio n. 2
0
 /**
  * Returns array('success'=>true) or array('error'=>'error message')
  */
 function handleUpload($uploadDirectory, $random_dir, $p_api_id, $replaceOldFile = FALSE)
 {
     $uploadDirectory .= '/' . $random_dir . '/';
     if (!is_writable($uploadDirectory)) {
         return array('error' => "Server error. Upload directory isn't writable.");
     }
     if (!$this->file) {
         return array('error' => 'No files were uploaded.');
     }
     $size = $this->file->getSize();
     if ($size == 0) {
         return array('error' => 'File is empty');
     }
     if ($size > $this->sizeLimit) {
         return array('error' => 'File is too large');
     }
     $pathinfo = pathinfo($this->file->getName());
     $filename = $pathinfo['filename'];
     //$filename = md5(uniqid());
     $ext = @$pathinfo['extension'];
     // hide notices if extension is empty
     if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
         $these = implode(', ', $this->allowedExtensions);
         return array('error' => 'File has an invalid extension, it should be one of ' . $these . '.');
     }
     $ext = $ext == '' ? $ext : '.' . $ext;
     if (!$replaceOldFile) {
         /// don't overwrite previous files that were uploaded
         while (file_exists($uploadDirectory . $filename . $ext)) {
             $filename .= rand(10, 99);
         }
     }
     $this->uploadName = $filename . $ext;
     if ($this->file->save($uploadDirectory . $filename . $ext)) {
         // If file pushed to temp directory, push it to MMC agent
         $filename = $filename . $ext;
         $upload_tmp_dir = sys_get_temp_dir();
         $files = array();
         // If mmc-agent is not on the same machine than apache server
         // send binary files with XMLRPC (base64 encoded)
         // else mmc-agent will directly get it from tmp directory
         $mmc_ip = xmlrpc_getMMCIP();
         $local_mmc = in_array($mmc_ip, array('127.0.0.1', 'localhost', $_SERVER['SERVER_ADDR'])) ? True : False;
         $filebinary = False;
         if (!$local_mmc) {
             $file = $upload_tmp_dir . '/' . $random_dir . '/' . $filename;
             // Read and put content of $file to $filebinary
             $filebinary = fread(fopen($file, "r"), filesize($file));
         }
         $files[] = array("filename" => $filename, "filebinary" => $local_mmc ? False : base64_encode($filebinary), "tmp_dir" => $local_mmc ? $upload_tmp_dir : False);
         $push_package_result = pushPackage($p_api_id, $random_dir, $files, $local_mmc);
         // Delete package from PHP /tmp dir
         delete_directory($upload_tmp_dir . '/' . $random_dir);
         if (!isXMLRPCError() and $push_package_result) {
             return array('success' => true);
         } else {
             return array('error' => 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
         }
     } else {
         return array('error' => 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
     }
 }
Esempio n. 3
0
                new NotifyWidgetSuccess(_T("Package successfully edited", "pkgs"));
                $package = $ret[3];
            }
            if (isset($_POST["bassoc"])) {
                // If no error with sending package infos, push package previously uploaded
                $package_id = $ret[3]['id'];
                $upload_tmp_dir = sys_get_temp_dir();
                $file_list = get_directory_list($upload_tmp_dir . '/' . $random_dir);
                $files = array();
                foreach ($file_list as $filename) {
                    $file = $upload_tmp_dir . '/' . $random_dir . '/' . $filename;
                    // Read and put content of $file to $filebinary
                    $filebinary = fread(fopen($file, "r"), filesize($file));
                    $files[] = array("filename" => $filename, "filebinary" => base64_encode($filebinary));
                }
                $push_package_result = pushPackage($p_api_id, $random_dir, $files);
                // Delete package from PHP /tmp dir
                delete_directory($upload_tmp_dir . '/' . $random_dir);
                if (!isXMLRPCError() and $push_package_result) {
                    header("Location: " . urlStrRedirect("pkgs/pkgs/associate_files", array('p_api' => base64_encode($p_api_id), 'random_dir' => base64_encode($random_dir), 'pid' => base64_encode($ret[3]['id']), 'plabel' => base64_encode($ret[3]['label']), 'pversion' => base64_encode($ret[3]['version']), 'mode' => $_POST['mode'])));
                    exit;
                }
            }
        } else {
            new NotifyWidgetFailure($ret[1]);
        }
    } else {
        new NotifyWidgetFailure(_T("Package failed to save", "pkgs"));
    }
}
$p_api_id = base64_decode($_GET['p_api']);