Add(print_r($_FILES, true)); // check for possible errors if (!isset($_FILES['files']['error']) || is_array($_FILES['files']['error'])) { $g_error->Add(_("Invalid file upload parameters.")); $action = ACT_ADD; break; } if (($_FILES['files']['error'] == UPLOAD_ERR_INI_SIZE) || ($_FILES['files']['error'] == UPLOAD_ERR_FORM_SIZE)) { $g_error->Add(_("Exceeded filesize limit.")); $action = ACT_ADD; break; } 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; } $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, $g_doc_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; } if ($file_size > $g_doc_maxsize) { $g_error->Add(sprintf(_('File to big: %s.%s'), $file_name, $file_type)); 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[':doctype'] = gpc_get_string($_POST, 'doctype'); $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; } unset($_SESSION['token']); */ $sql = "SELECT doctype, extension FROM document WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $doc = $sth->fetch(PDO::FETCH_OBJ); // clean thumbs $thumbdir = $g_doc_basepath . '/thumbs/'; $pattern = sprintf('%s-%06d*', $g_doc_typemap[$doctype], $docid); foreach (glob($thumbdir . $pattern) as $f) { // unlink($f); $g_success->Add($f); } // remove document $g_success->Add(_('Deleted document %s'), $docfilename); // sprintf('%s/%s-%06d.%s', $g_doc_basepath, $dtmap[$row['doctype']], $docid, $row['extension']); /* $sth = $pdo->prepare("DELETE FROM document WHERE docid=?"); try { $sth->execute([$id]); } catch (PDOexception $e) { $g_error->Add(sprintf(_('SQL-Error: %s', $e->getMessage()))); }*/ //$action = ACT_DEFAULT; $action = ACT_DELETE; break; case 'addref': $opt_reftype = db_load_enum('docref', 'reftype'); $p[':docid'] = $id; $p[':refid'] = gpc_get_int($_POST, 'refid'); $p[':reftype'] = gpc_get_enum($_POST, 'reftype', $opt_reftype, 'equipment'); /* $sql = "INSERT INTO docref " . " (docid, refid, reftype) " . "VALUES " . " (:docid, :refid, :reftype)"; $sth = $pdo->prepare($sql); $sth->bindValue(':docid', $id, PDO::PARAM_INT); $sth->bindValue(':refid', $refid, PDO::PARAM_INT); $sth->bindValue(':reftype', $reftype, PDO::PARAM_STR); $sth->execute(); */ db_exec_insert('docref', $p); $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 ======================================= page_caption_search($pagetitle); $flt = db_get_filter($user->id, 209); // $w = array('(vid=:vid OR vid IS NULL)'); // $p = array(':vid' => $user->vid); $w = array(); $p = array(); if (isset($flt->doctype) and $flt->doctype != 'all') { $w[] = 'doctype=:doctype'; $p[':doctype'] = $flt->doctype; } if (isset($flt->reftype) and $flt->reftype != 'all') { if ($flt->reftype == 'none') { $w[] = 'reftype IS NULL'; } else { $w[] = 'reftype=:reftype'; $p[':reftype'] = $flt->reftype; } } 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'; /* WIP Attention: Do not count duplicate, so first select docid from different sources an as laststep get a distinct list of docids to count. Documents with no reference at all SELECT docid FROM document LEFT OUTER JOIN docref USING (docid) WHERE docref.drid IS NULL; Documents for current vessel SELECT docid FROM document LEFT OUTER JOIN docref USING (docid) WHERE docref.reftype='vessel' AND docref.refid=:vid Documents for equipment in current vessel SELECT DISTINCT d.docid AS docid FROM document AS d LEFT OUTER JOIN docref AS r USING (docid) LEFT OUTER JOIN equipment AS e ON (r.refid=e.eid) WHERE r.reftype='equipment' AND e.vid=:vid */ // get total recond count for pagination and limit $sql = "SELECT COUNT(DISTINCT docid) FROM document LEFT OUTER JOIN docref USING (docid)"; if ($where) $sql .= ' WHERE ' . $where; $sth = $pdo->prepare($sql); try { $sth->execute($p); } catch(PDOException $e) { $g_error->Add($e->getMessage()); $g_error->Add($sql); $g_error->Add(print_r($p, true)); $g_error->PrintOut(); } $numrows = $sth->fetchColumn(); $page = gpc_get_int($_REQUEST, 'p', 1); $rows_pp = gpc_get_int($_REQUEST, 'n', $g_rows_pp); $lastpage = ceil($numrows/$rows_pp); $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; // if pagination: $sql .= ' LIMIT ' . ($page - 1) * $g_rows_pp . ',' . $g_rows_pp; $sth = $pdo->prepare($sql); $sth->execute($p); $res = $sth->fetchAll(); // Filter $opt_special = array( 'all' => _('― all ―'), ); ?>
Filter
', "\n"; echo "\n"; echo ""; echo '#'; echo "", _('Type'), ""; echo "", _('Filename'), ""; echo "", _('Title'), ""; echo "", _('Links'), ""; echo " "; echo "\n"; echo "\n"; $i = ($page - 1) * $rows_pp; $i0 = $i + 1; foreach ($res as $row) { $i++; // record number echo "\n"; echo "", $i; echo '', "\n"; echo "\n"; echo "", $opt_doctype[$row['doctype']], "\n"; echo "", $row['filename'], "\n"; echo "", $row['title'], "\n"; echo "", $row['refcount'], "\n"; // Edit current record button echo ""; echo '', "\n"; echo "\n"; echo "\n"; } // Table summary echo "\n"; echo '', sprintf(_('Records %d to %d of %d'), $i0, $i, $numrows), '', "\n"; echo "\n"; echo "\n"; echo $pagination; 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 = sprintf('pic-%06d.%s', $document->docid, $document->extension); echo '\n"; } elseif (($document->doctype == 'drawing')) { $techname = sprintf('drw-%06d.%s', $document->docid, $document->extension); echo ''; echo '', "\n"; } elseif (in_array($document->doctype, ['generic', 'manual', 'invoice'])) { $techname = sprintf('doc-%06d.%s', $document->docid, $document->extension); if ($document->mimetype == 'application/pdf') { // read page count from pdf file: // $pdfpages = pdf_get_pagecount("$g_doc_basepath/$techname"); echo '\n"; } } if ($document->docsize == 0) { // update get current size in db $document->docsize = file_fix_filesize($document->docid, "$g_doc_basepath/$techname"); } echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo "
', _('Document type'),"", $document->doctype, "
', _('Preview'), ''; // echo ''; echo ''; // gallery ?> '; echo "
', _('Drawing'), '', $document->title, '
', _('PDF'), ''; // Download link echo '', $document->title ?? _('Download'), ' (', sprintf(_('%d Pages'), $document->pages), ')'; echo "
', _('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"; if ($document->pages > 1) { $pagination = get_pagination($g_scriptname, 'f=view&id='.$id, $page, $document->pages, true); echo $pagination; // $pagination; } } echo "
\n
\n"; // end of columns form_view_buttons($g_scriptname, $id); echo '

', _('Document references'), "

\n"; $references = array(); // Vessel $vids = array(); $sql = "SELECT drid, reftype, refid, vesselname AS target " . "FROM docref INNER JOIN vessel ON (docref.reftype='vessel' AND docref.refid=vessel.vid) " . "WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); foreach ($sth->fetchAll() as $row) { $references[] = $row; $vids[] = $row['refid']; } // Equipment $eids = array(); $sql = "SELECT drid, reftype, refid, ename AS target " . "FROM docref INNER JOIN equipment ON (docref.reftype='equipment' AND docref.refid=equipment.eid) " . "WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); foreach ($sth->fetchAll() as $row) { $references[] = $row; $eids[] = $row['refid']; } // Companies $compids = array(); $sql = "SELECT drid, reftype, refid, compname AS target " . "FROM docref INNER JOIN company ON (docref.reftype='company' AND docref.refid=company.compid) " . "WHERE docid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); foreach ($sth->fetchAll() as $row) { $references[] = $row; $compids[] = $row['refid']; } echo '', "\n"; echo "\n"; echo ""; echo '"; echo ""; echo ""; echo "\n"; echo "\n"; foreach ($references as $ref) { echo ""; echo ""; echo ""; echo ""; echo "\n"; } echo "
' . _('Type') . "" . _('Used by') . "
", $ref['reftype'], "", $ref['target'], ""; if ($ref['reftype'] == 'equipment') { echo ''; } elseif ($ref['reftype'] == 'company') { echo ''; } echo ''; echo "
\n"; // New references echo '

', _('Add new reference'), "

\n"; // enum('vessel','box','equipment','project','task','user','company') // // 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']; } } ?>
prepare($sql); $sth->execute(); foreach ($sth->fetchAll() as $row) { if (!in_array($row['compid'], $compids)) { $opt_company[$row['compid']] = $row['compname']; } } ?>
query($sql); foreach ($sth->fetchAll() as $row) { if (!in_array($row['vid'], $vids)) { $opt_vessel[$row['vid']] = $row['vesselname']; } } ?>
', _('Edit Document'), "\n"; $sql = "SELECT docid, doctype, mimetype, 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 ''; } ?>
mimetype == 'application/pdf') { echo '
', "\n"; $opt_doctype = array( 'generic' => _('Generic'), 'manual' => _('Manual'), 'invoice' => _('Invoice') ) ; form_create_select('doctype', _('Document type'), $opt_doctype, $document->doctype); echo "
"; } else { echo '', "\n"; } ?>
prepare($sql); $sth->execute([$id]); $document = $sth->fetch(PDO::FETCH_OBJ); echo '

', _('Delete Document'), "

\n"; echo '

', sprintf(_('Record no. %d'), $id), "

\n"; echo '

Dokument: ', $document->filename, "

"; echo '

Title: ', $document->title, "

"; echo '

', _('Deleting a document item is final. There is no way back. Only delete if you are absolute sure.'), "

\n"; $_SESSION['token'] = bin2hex(random_bytes(8)); form_delete_buttons($g_scriptname, $id, $_SESSION['token']); else: // ========== ERROR UNKNOWN VARIANT =========================================== echo '

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

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