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': $p[':doctype'] = 'generic'; $p[':pages'] = pdf_get_pagecount($file_tmp); $prefix = 'doc'; break; case 'image/svg+xml': $p[':doctype'] = 'drawing'; $prefix = 'drw'; break; case 'image/png': case 'image/jpeg': $p[':doctype'] = 'picture'; $prefix = 'pic'; break; } $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'); $id = db_exec_insert('document', $p); $file_techname = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $prefix, $id, $file_ext); move_uploaded_file($file_tmp, $file_techname); $action = ACT_VIEW; break; case 'update': $p[':docid'] = $id; $p[':filename'] = gpc_get_string($_POST, 'filename'); $p[':title'] = gpc_get_string($_POST, 'title', 40); $p['remarks'] = gpc_get_string($_POST, 'remarks', 150); db_exec_update('document', $p, 'docid'); $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 $flt = db_get_filter($user->id, 209); $opt_doctype = db_load_enum('document', 'doctype', true, true); /* 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. */ $w = array(); $p = array(); if ($flt->doctype != 'all') { $w[] = 'doctype=:doctype'; $p[':doctype'] = $flt->doctype; } if (strlen($flt->txt) > 1) { $w[] = '(filename LIKE :txt OR title LIKE :txt OR remarks LIKE :txt)'; $p[':txt'] = '%'.$flt->txt.'%'; } $where = join(' AND ', $w); $order = ' ORDER BY docid'; $sql = "SELECT d.docid, d.doctype, d.filename, d.title, COUNT(r.drid) AS refcount " . "FROM document AS d LEFT OUTER JOIN docref AS r USING (docid)"; if ($where) $sql .= ' WHERE ' . $where; $sql .= " GROUP BY d.docid, d.doctype, d.filename, d.title"; if ($order) $sql .= $order; $sth = $pdo->prepare($sql); $sth->execute($p); $res = $sth->fetchAll(); echo "
| ', _('Document type')," | ", $document->doctype, " |
|---|---|
| ', _('Preview'), ' | ';
// echo ' |
| ', _('Drawing'), ' | '; echo '|
| ', _('PDF'), ' | ', $document->title ?? _('Download'), ' (', sprintf(_('%d Pages'), $pages), ") |
| ', _('Mimetype')," | ", $document->mimetype, " |
| ', _('Filename')," | ", $document->filename, " |
| ', _('File size')," | ", format_filesize($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';