log = mfLoghandler::singleton(); $this->dirmode = 0777; $this->filemode = 0666; if(defined("MFUPLOAD_DIRMODE")) { $this->dirmode = MFUPLOAD_DIRMODE; } if(defined("MFUPLOAD_FILEMODE")) { $this->filemode = MFUPLOAD_FILEMODE; } if(!$this->upload = new mfUpload_TmpFile($uplName)) { throw new Exception($this->upload->errormessage); } $this->original_filename = $this->upload->filename; if(!$this->filename = $this->upload->getFilename()){ throw new Exception($this->upload->errormessage); } if($randomFileName) { $this->filename = $this->getRandomFilename().'-'.$this->filename; } $this->size = $this->upload->getFileSize(); if($this->size > MFUPLOAD_FILE_MAX_SIZE) { throw new Exception('File is too big. Maximum allowed filesize is '.(MFUPLOAD_FILE_MAX_SIZE).' MB'); } } static public function handleFormUpload($uplName, $filename = false, $subfolder = false, $savepath = MFUPLOAD_FILE_SAVE_PATH) { if(!isset($_FILES) || !array_key_exists($uplName, $_FILES)) { throw new Exception("Uploaded file not found"); } if(array_key_exists("error", $_FILES[$uplName]) && $_FILES[$uplName]['error']) { throw new Exception("Error receiving file"); } $upload_error = false; $savepath = preg_replace('#/+$#', "", $savepath); // remove trailing slash if($subfolder) { $subfolder = preg_replace('#^/+#', "", $subfolder); // remove leading slash $subfolder = preg_replace('#/+$#', "", $subfolder); // remove trailing slash $savepath .= "/$subfolder"; } $upload = new mfUpload($uplName); $upload->setSavepath($savepath); if(!$upload->getSize()) { throw new Exception("Datei darf nicht leer sein"); } if(substr(strtolower($upload->getFilename()), -3, 3) == "pdf" && !$upload->validatePDF()) { throw new Exception("PDF-Validierung fehlgeschlagen"); } try { $upload->save(); } catch (Exception $ex) { throw $ex; } $file_data = []; $file_data['name'] = ($filename) ? $filename : $upload->getOriginalFilename(); $file_data['filename'] = ($filename) ? $filename : $upload->getOriginalFilename(); $file_data['subfolder'] = $subfolder; $file_data['store_filename'] = $upload->getFilename(); $file_data['orig_filename'] = $upload->getOriginalFilename(); $file = FileModel::create($file_data); $file_id = $file->save(); if(!$file_id) { unlink($upload->getSavepath()."/".$upload->getFilename()); throw new Exception("Datei konnte nicht angelegt werden"); } return $file; } public function getSavepath() { return $this->savepath; } public function setSavepath($path) { $this->savepath = $path; return true; } public function getOriginalFilename() { return $this->original_filename; } public function getFilename() { return $this->filename; } public function setFilename($name) { $this->filename = $name; } public function getSize() { return $this->size; } public function getDirmode() { return $this->dirmode; } public function setDirmode($mode) { $this->dirmode = $mode; } public function getFilemode() { return $this->filemode; } public function setFilemode($mode) { $this->filemode = $mode; } public function getMimetype() { return $this->upload->getMimetype(); } public function save() { if(!$this->savepath) { if(!$this->buildSavepath()) { throw new Exception ("Not enough data to build savepath!",605); } } if(!$this->upload->move_upload($this->savepath."/".$this->filename)) { throw new Exception ("Unable to move temp file: ".$this->upload->errormessage,605); } chmod($this->savepath."/".$this->filename,$this->filemode); return true; } private function buildSavepath() { if(!$this->filename) { return false; } $savepath = MFUPLOAD_FILE_SAVE_PATH; if(!MFUPLOAD_FILE_SAVE_PATH) { $savepath = "upload/"; } $this->log->debug("dirmode: ".decoct($this->dirmode)); if(!file_exists($savepath)) { if(!mkdir($savepath, $this->dirmode,true)) { throw new Exception("Error creating directory $savepath."); } chmod($savepath, $this->dirmode); } if(!is_dir($savepath)) { throw new Exception("Upload save path not a directory!"); } $this->savepath = $savepath; return true; } public function getRandomFilename() { $length = 20; $characters = '0123456789abcdef'; $string = ''; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters) -1)]; } $rand = uniqid(md5(rand()), true); list($rand) = explode('.',$rand); $string=$rand.$string; return $string; } public function validatePDF() { if(!$this->upload) { return false; } if(!$this->size) { return false; } $this->log->debug("upload size: ".$this->size); $mime = $this->upload->getMimetype(); $this->log->debug("upload mime: $mime"); if(!stripos($mime, "pdf")) { return false; } $pdftext = $this->upload->pdftotext(); //$this->log->debug("text: ".$pdftext); if($pdftext === false) { $this->log->warn("pdftotext returned error (".$this->filename.")"); return false; } //$this->log->debug("upload text: $pdftext"); return true; } }