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 "

$pagetitle

\n"; $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) " . "GROUP BY d.docid, d.doctype, d.filename, d.title"; $sth = $pdo->query($sql); $res = $sth->fetchAll(); echo '', "\n"; echo "\n"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; echo "\n"; foreach ($res as $row) { echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; // Action buttons echo "\n"; echo "\n"; } // Table summary echo "\n"; echo '', "\n"; echo "\n"; echo "
" . _('ID') . "" . _('Type') . "" . _('Filename') . "" . _('Title') . "" . _('Links') . " 
". $row['docid'] ."". $row['doctype'] ."". $row['filename'] ."". $row['title'] ."". $row['refcount'] .""; echo '', "\n"; echo '', "\n"; echo "
', sprintf(_('%d records'), count($res)), '
\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"; if ($document->doctype == 'picture') { $techname = 'pic-' . $document->hash . '.' . $document->extension; echo '\n"; } elseif (($document->doctype == 'drawing')) { echo ''; echo '', "\n"; } elseif (($document->doctype == 'generic')) { $techname = 'doc-' . $document->hash . '.' . $document->extension; if ($document->mimetype == 'application/pdf') { // TODO show preview of previewpage, ctrate thumbnail if needed // 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'), "
', _('Mimetype'),"", $document->mimetype, "
', _('Filename'),"", $document->filename, "
', _('File size'),"", $document->docsize, "
', _('Upload date'),"", $document->doctime, "
', _('Title'),"", $document->title, "
', _('Remarks'),"", $document->remarks, "
\n"; 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 "
\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';