Add(print_r($_FILES, true)); if (empty($_FILES['files']['tmp_name']) ) { $g_warning->Add(_('No file for upload submitted. Try again by selecting a file first.')); $action = ACT_ADD; break; } $mimetypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf']; $nerr = 0; $file_name = $_FILES['files']['name']; $file_tmp = $_FILES['files']['tmp_name']; $file_type = $_FILES['files']['type']; $file_size = $_FILES['files']['size']; $file_ext = strtolower(end(explode('.', $_FILES['files']['name']))); $file_mimetype = mime_content_type($file_tmp); if (!in_array($file_mimetype, $mimetypes)) { $g_error->Add(sprintf(_("Mimetype '%s' not allowed for upload."), $file_mimetype)); $action = ACT_ADD; break; }; if ($file_type != $file_mimetype) { $g_warning->Add(sprintf(_("Mimetype mismatch: '%s'."), $file_type)); } // ok let's go $file_hash = md5_file($file_tmp); $file_timestamp = date('Y-m-d H:i:s', filemtime($file_tmp)); // check if the file already exists, in which case issue // a notice and display the existing document $sth = $pdo->prepare("SELECT docid FROM document WHERE hash=?"); $sth->execute([$file_hash]); $id = $sth->fetchColumn(); if ($id) { $g_warning->Add(_('Document already exists!')); $action = ACT_VIEW; break; } switch ($file_mimetype) { case 'application/pdf': $file_techname = $g_doc_basepath . '/doc-'.$file_hash.'.'.$file_ext; $doctype = 'generic'; break; case 'image/svg+xml': $file_techname = $g_doc_basepath . '/drw-'.$file_hash.'.'.$file_ext; $doctype = 'drawing'; break; case 'image/png': case 'image/jpeg': $file_techname = $g_doc_basepath . '/pic-'.$file_hash.'.'.$file_ext; $doctype = 'picture'; break; } $p[':doctype'] = $doctype; $p[':mimetype'] = $file_mimetype; $p[':filename'] = $file_name; $p[':extension'] = $file_ext; $p[':hash'] = $file_hash; $p[':doctime'] = $file_timestamp; $p[':docsize'] = $file_size; $p[':title'] = gpc_get_string($_POST, 'title'); $p[':remarks'] = gpc_get_string($_POST, 'remarks'); /* $sql = "INSERT INTO document" . " (doctype, filename, extension, hash, doctime, docsize, mimetype) " . "VALUES" . " (?, ?, ?, ?, ?, ?, ?)"; $sth = $pdo->prepare($sql); try { $sth->execute([$doctype, $file_name, $file_ext, $file_hash, $file_timestamp, $file_size, $file_mimetype]); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } $id = $pdo->lastInsertId(); */ $id = db_exec_insert('document', $p); move_uploaded_file($file_tmp, $file_techname); $action = ACT_VIEW; break; case 'update': $filename = gpc_get_string($_POST, 'filename'); $title = gpc_get_string($_POST, 'title', 40); $remarks = gpc_get_string($_POST, 'remarks', 150); $sql = "UPDATE document " . "SET filename=:filename," . " title=:title," . " remarks=:remarks " . "WHERE docid=:docid"; $sth = $pdo->prepare($sql); $sth->bindValue(':docid', $id, PDO::PARAM_INT); $sth->bindValue(':filename', $filename, PDO::PARAM_STR); $sth->bindValue(':title', $title, PDO::PARAM_STR); $sth->bindValue(':remarks', $remarks, PDO::PARAM_STR); try { $sth->execute(); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } $action = ACT_VIEW; break; case 'delete': // Security token needed! if (gpc_get_string($_POST, 'token', 16) != $_SESSION['token']) { $g_error->Add(_('Delete prohibited, invalid security token!')); $action = ACT_VIEW; break; } $action = ACT_DEFAULT; break; case 'addref': $eid = gpc_get_int($_POST, 'eid'); $sql = "INSERT INTO docref " . " (docid, refid, reftype) " . "VALUES " . " (:docid, :refid, 'equipment')"; $sth = $pdo->prepare($sql); $sth->bindValue(':docid', $id, PDO::PARAM_INT); $sth->bindValue(':refid', $eid, PDO::PARAM_INT); $sth->execute(); $action = ACT_VIEW; break; case 'delref': // remove only link, not critical so get without token is ok $drid = gpc_get_int($_GET, 'drid'); $sql = "DELETE FROM docref WHERE drid=?"; $sth = $pdo->prepare($sql); $sth->execute([$drid]); $g_message->Add(_('Removed document reference')); $action = ACT_VIEW; break; default: $g_error->Add(sprintf(_('Unknown function!'), $submit)); $valid = FALSE; } // ========== ACTIONS END ===================================================== require 'header.php'; // ========== PAGE CONTENT ==================================================== if ($action == ACT_DEFAULT): // ========== VARIANT: default behavior ======================================= // All documents /* TODO show all documents which - are completely unbound: no docref available - assigned to the current boat: docref with doctype vessel - are assigned to an equipment or project of current vesseƶ There can be multiple docref entries for a document, behavior still needs to be clarified. */ echo "
| ', _('Document type')," | ", $document->doctype, " |
|---|---|
| ', _('Preview'), ' | ';
// echo ' |
| ', _('Drawing'), ' | '; echo '|
| ', _('PDF'), ' | ', $document->title ?? _('Download'), " |
| ', _('Mimetype')," | ", $document->mimetype, " |
| ', _('Filename')," | ", $document->filename, " |
| ', _('File size')," | ", $document->docsize, " |
| ', _('Upload date')," | ", $document->doctime, " |
| ', _('Title')," | ", $document->title, " |
| ', _('Remarks')," | ", $document->remarks, " |
', _('Unknown function call: Please report to system development!'), "
\n"; endif; // $action == ... // ========== END OF VARIANTS ================================================= include 'footer.php';