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 "

$pagetitle

\n"; // Filter $opt_special = array( 'all' => _('― all ―'), ); ?>
Filter
', "\n"; echo "\n"; echo ""; echo "", _('ID'), ""; echo "", _('Type'), ""; echo "", _('Filename'), ""; echo "", _('Title'), ""; echo "", _('Links'), ""; echo " "; echo "\n"; echo "\n"; foreach ($res as $row) { echo "\n"; echo "", $row['docid'], "\n"; echo "", $opt_doctype[$row['doctype']], "\n"; echo "", $row['filename'], "\n"; echo "", $row['title'], "\n"; echo "", $row['refcount'], "\n"; // Action buttons echo ""; echo '', "\n"; echo '', "\n"; echo "\n"; echo "\n"; } // Table summary echo "\n"; echo '', sprintf(_('%d records'), count($res)), '', "\n"; echo "\n"; echo "\n"; form_add_button($g_scriptname); elseif ($action == ACT_ADD): // ========== VARIANT: add record ============================================= echo '

', _('Add Document'), "

\n"; ?>
prepare($sql); $sth->execute([$id]); $document = $sth->fetch(PDO::FETCH_OBJ); echo '

', _('View Document'), "

\n"; echo '
', "\n"; echo '
', "\n"; echo '', "\n"; echo '\n"; if ($document->doctype == 'picture') { $techname = 'pic-' . $document->hash . '.' . $document->extension; echo '\n"; } elseif (($document->doctype == 'drawing')) { echo ''; echo '', "\n"; } elseif (($document->doctype == 'generic')) { $techname = sprintf('doc-%06d.%s', $document->docid, $document->extension); if ($document->mimetype == 'application/pdf') { // TODO show preview of previewpage, create thumbnail if needed $pages = pdf_get_pagecount("$g_doc_basepath/$techname"); // Download link echo '\n"; } } echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo "
', _('Document type'),"", $document->doctype, "
', _('Preview'), ''; // echo '', $document->title, ''; echo ''; echo '', $document->title, ''; echo ''; echo "
', _('Drawing'), '', $document->title, '
', _('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, "
\n"; echo "
\n", '
', "\n"; // column break if ($document->mimetype == 'application/pdf') { echo 'PDF-Preview', "\n"; } echo "
\n
\n"; // end of columns form_view_buttons($g_scriptname, $id); echo '

', _('Document references'), "

\n"; $sql = "SELECT drid, reftype, ename AS name, eid " . "FROM docref INNER JOIN equipment ON (docref.reftype='equipment' AND docref.refid=equipment.eid) " . "WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); echo '', "\n"; echo "\n"; echo ""; echo '"; echo ""; echo ""; echo "\n"; echo "\n"; $eids = array(); foreach ($sth->fetchAll() as $row) { $eids[] = $row['eid']; echo ""; echo ""; echo ""; echo ""; echo "\n"; } echo "
' . _('Type') . "" . _('Used by') . "
", $row['reftype'], "", $row['name'], ""; echo ''; echo ''; echo "
\n"; // Equipment options // Do not include equipment that has already been referenced in the list! $opt_equipment = array(); $sql = "SELECT eid, ename FROM equipment WHERE vid=? ORDER BY ename"; $sth = $pdo->prepare($sql); $sth->execute([$user->vid]); foreach ($sth->fetchAll() as $row) { if (!in_array($row['eid'], $eids)) { $opt_equipment[$row['eid']] = $row['ename']; } } ?>
', _('Edit Document'), "\n"; $sql = "SELECT docid, doctype, filename, extension, hash, title, remarks " . "FROM document " . "WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $document = $sth->fetch(PDO::FETCH_OBJ); if ($document->doctype == 'picture') { echo ''; } ?>
', _('Delete Document'), "\n"; else: // ========== ERROR UNKNOWN VARIANT =========================================== echo '

', _('Unknown function call: Please report to system development!'), "

\n"; endif; // $action == ... // ========== END OF VARIANTS ================================================= include 'footer.php';