for ($i = 1; $i <= 5; $i++) { if (isset($_FILES['form_supplementary_file' . $i]) && is_uploaded_file($_FILES['form_supplementary_file' . $i]['tmp_name']) && preg_match('/\\.(php|php4|php3|htm|html)$/i', $_FILES['form_supplementary_file' . $i]['name']) == 0) { $supplement_filename = sprintf("%05d", intval($new_file)) . $_FILES['form_supplementary_file' . $i]['name']; $move = move_uploaded_file($_FILES['form_supplementary_file' . $i]['tmp_name'], "{$library_path}/supplement/{$supplement_filename}"); if ($move == false) { $error[] = "Error! The supplementary file " . $_FILES['form_supplementary_file' . $i]['name'] . " has not been recorded."; } if ($move == true) { $message[] = "The supplementary file file " . $_FILES['form_supplementary_file' . $i]['name'] . " has been recorded."; } } } if (file_exists($library_path . DIRECTORY_SEPARATOR . $new_file)) { $unpack_dir = $temp_dir . DIRECTORY_SEPARATOR . $new_file; @mkdir($unpack_dir); exec(select_pdftk() . '"' . $library_path . DIRECTORY_SEPARATOR . $new_file . '" unpack_files output "' . $unpack_dir . '"'); $unpacked_files = scandir($unpack_dir); foreach ($unpacked_files as $unpacked_file) { if (is_file($unpack_dir . DIRECTORY_SEPARATOR . $unpacked_file)) { @rename($unpack_dir . DIRECTORY_SEPARATOR . $unpacked_file, $library_path . DIRECTORY_SEPARATOR . supplement . DIRECTORY_SEPARATOR . sprintf("%05d", intval($new_file)) . $unpacked_file); } } @rmdir($unpack_dir); } ########## record graphical abstract ########## if (isset($_FILES['form_graphical_abstract']) && is_uploaded_file($_FILES['form_graphical_abstract']['tmp_name'])) { $extension = pathinfo($_FILES['form_graphical_abstract']['name'], PATHINFO_EXTENSION); if (empty($extension)) { $extension = 'jpg'; } $new_name = sprintf("%05d", intval($new_file)) . 'graphical_abstract.' . $extension;
} if (isset($_GET['renderthumbs'])) { if (file_exists($pdf_path . DIRECTORY_SEPARATOR . $file)) { if (!file_exists($png_path . DIRECTORY_SEPARATOR . $file . ".t1.png") || filemtime($png_path . DIRECTORY_SEPARATOR . $file . '.t1.png') < filemtime($pdf_path . DIRECTORY_SEPARATOR . $file)) { exec(select_ghostscript() . " -dSAFER -sDEVICE=png256 -r20 -dTextAlphaBits=1 -dGraphicsAlphaBits=1 -o \"" . $png_path . DIRECTORY_SEPARATOR . $file . ".t%d.png\" \"" . $pdf_path . DIRECTORY_SEPARATOR . $file . "\""); } } die; } if (isset($_GET['renderbookmarks'])) { if (file_exists($pdf_path . DIRECTORY_SEPARATOR . $file)) { $safe_file_name = preg_replace('/[^\\d\\.pdf]/', '', $_GET['file']); $file_name = $pdf_path . DIRECTORY_SEPARATOR . $safe_file_name; $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $safe_file_name . '-bookmarks.txt'; if (!file_exists($temp_file) || filemtime($temp_file) < filemtime($file_name)) { system(select_pdftk() . '"' . $file_name . '" dump_data output "' . $temp_file . '"', $ret); } if (file_exists($temp_file)) { $i = 0; $bookmark = array(); $pdftk_array = file($temp_file, FILE_IGNORE_NEW_LINES); foreach ($pdftk_array as $pdftk_line) { if (stripos($pdftk_line, 'BookmarkTitle') === 0) { $bookmark[$i]['title'] = trim(stristr($pdftk_line, ' ')); $j = $i; } if (stripos($pdftk_line, 'BookmarkLevel') === 0) { $bookmark[$j]['level'] = trim(stristr($pdftk_line, ' ')); } if (stripos($pdftk_line, 'BookmarkPageNumber') === 0) { $bookmark[$j]['page'] = trim(stristr($pdftk_line, ' '));
file_put_contents($temp_dir . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . 'richnotes.html', $notetxt); $supfile_arr[] = $temp_dir . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . 'richnotes.html'; } } //ATTACH SUPPLEMENTARY FILES if (in_array('supp', $_GET['attachments'])) { $supfiles = array(); $integer = sprintf("%05d", intval($_GET['file'])); $supfiles = glob($path . DIRECTORY_SEPARATOR . 'supplement' . DIRECTORY_SEPARATOR . $integer . '*'); $supfile_arr = array_merge((array) $supfiles, $supfile_arr); } $supfile_str = join('" "', $supfile_arr); $supfile_str = trim($supfile_str); if (!empty($supfile_str)) { $temp_file = $temp_dir . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . $file . '-attachments.pdf'; system(select_pdftk() . '"' . $file_name . '" attach_files "' . $supfile_str . '" output "' . $temp_file . '"', $ret); $file_name = $temp_file; } } // CUSTOM NAME // database_connect($database_path, 'library'); // $qfile = $dbHandle->quote($_GET['file']); // $result = $dbHandle->query("SELECT title FROM library where file=" . $qfile); // $data = $result->fetch(PDO::FETCH_NAMED); // $file = str_replace(' ', '_', substr($data['title'],0,35)) . '.pdf'; //RENDER FINISHED PDF header("Content-type: application/pdf"); if (!isset($_GET['mode'])) { header("Content-Disposition: inline; filename=\"{$file}\""); } if (isset($_GET['mode']) && $_GET['mode'] == 'download') {
function record_unknown($dbHandle, $title, $string, $file, $userID) { global $temp_dir, $database_path; $query = "INSERT INTO library (file, title, title_ascii, addition_date, rating, added_by)\n VALUES ((SELECT IFNULL((SELECT SUBSTR('0000' || CAST(MAX(file)+1 AS TEXT) || '.pdf',-9,9) FROM library),'00001.pdf')), :title, :title_ascii, :addition_date, :rating, :added_by)"; $stmt = $dbHandle->prepare($query); $stmt->bindParam(':title', $title, PDO::PARAM_STR); $stmt->bindParam(':title_ascii', $title_ascii, PDO::PARAM_STR); $stmt->bindParam(':addition_date', $addition_date, PDO::PARAM_STR); $stmt->bindParam(':rating', $rating, PDO::PARAM_INT); $stmt->bindParam(':added_by', $added_by, PDO::PARAM_INT); if (empty($title)) { $title = basename($file); } $title_ascii = utf8_deaccent($title); $addition_date = date('Y-m-d'); $rating = 2; $added_by = intval($userID); $dbHandle->exec("BEGIN IMMEDIATE TRANSACTION"); $stmt->execute(); $stmt = null; $last_insert = $dbHandle->query("SELECT last_insert_rowid(),max(file) FROM library"); $last_row = $last_insert->fetch(PDO::FETCH_ASSOC); $last_insert = null; $id = $last_row['last_insert_rowid()']; $new_file = $last_row['max(file)']; if (isset($_GET['shelf']) && !empty($userID)) { $user_query = $dbHandle->quote($userID); $file_query = $dbHandle->quote($id); $dbHandle->exec("INSERT OR IGNORE INTO shelves (userID,fileID) VALUES ({$user_query},{$file_query})"); @unlink($temp_dir . DIRECTORY_SEPARATOR . 'lib_' . session_id() . DIRECTORY_SEPARATOR . 'shelf_files'); } if (isset($_GET['project']) && !empty($_GET['projectID'])) { $dbHandle->exec("INSERT OR IGNORE INTO projectsfiles (projectID,fileID) VALUES (" . intval($_GET['projectID']) . "," . intval($id) . ")"); $clean_files = glob($temp_dir . DIRECTORY_SEPARATOR . 'lib_*' . DIRECTORY_SEPARATOR . 'desk_files', GLOB_NOSORT); if (is_array($clean_files)) { foreach ($clean_files as $clean_file) { if (is_file($clean_file) && is_writable($clean_file)) { @unlink($clean_file); } } } } ####### record new category into categories, if not exists ######### if (isset($_GET['category2'])) { $category2 = $_GET['category2']; } $category2[] = '!unknown'; $category_ids = array(); $category2 = preg_replace('/\\s{2,}/', '', $category2); $category2 = preg_replace('/^\\s$/', '', $category2); $category2 = array_filter($category2); $query = "INSERT INTO categories (category) VALUES (:category)"; $stmt = $dbHandle->prepare($query); $stmt->bindParam(':category', $new_category, PDO::PARAM_STR); while (list($key, $new_category) = each($category2)) { $new_category_quoted = $dbHandle->quote($new_category); $result = $dbHandle->query("SELECT categoryID FROM categories WHERE category={$new_category_quoted}"); $exists = $result->fetchColumn(); $category_ids[] = $exists; $result = null; if (empty($exists)) { $stmt->execute(); $last_id = $dbHandle->query("SELECT last_insert_rowid() FROM categories"); $category_ids[] = $last_id->fetchColumn(); $last_id = null; } } $stmt = null; ####### record new relations into filescategories ######### $categories = array(); $category_array = array(); if (isset($_GET['category'])) { $category_array = $_GET['category']; } if (!empty($category_array) || !empty($category_ids)) { $categories = array_merge((array) $category_array, (array) $category_ids); $categories = array_filter(array_unique($categories)); } $query = "INSERT OR IGNORE INTO filescategories (fileID,categoryID) VALUES (:fileid,:categoryid)"; $stmt = $dbHandle->prepare($query); $stmt->bindParam(':fileid', $id); $stmt->bindParam(':categoryid', $category_id); while (list($key, $category_id) = each($categories)) { if (!empty($id)) { $stmt->execute(); } } $stmt = null; $dbHandle->exec("COMMIT"); copy($file, dirname(__FILE__) . DIRECTORY_SEPARATOR . "library" . DIRECTORY_SEPARATOR . $new_file); $hash = md5_file(dirname(__FILE__) . DIRECTORY_SEPARATOR . "library" . DIRECTORY_SEPARATOR . $new_file); //RECORD FILE HASH FOR DUPLICATE DETECTION if (!empty($hash)) { $hash = $dbHandle->quote($hash); $dbHandle->exec('UPDATE library SET filehash=' . $hash . ' WHERE id=' . $id); } $dbHandle = null; if (!empty($string)) { $dbHandle2 = database_connect($database_path, 'fulltext'); $file_query = $dbHandle2->quote($id); $fulltext_query = $dbHandle2->quote($string); $dbHandle2->query("DELETE FROM full_text WHERE fileID={$file_query}"); $insert = $dbHandle2->exec("INSERT INTO full_text (fileID,full_text) VALUES ({$file_query},{$fulltext_query})"); $dbHandle2 = null; } $pdftk = select_pdftk(); $unpack_dir = $temp_dir . DIRECTORY_SEPARATOR . $new_file; @mkdir($unpack_dir); exec($pdftk . '"' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . $new_file . '" unpack_files output "' . $unpack_dir . '"'); $unpacked_files = array(); $unpacked_files = scandir($unpack_dir); foreach ($unpacked_files as $unpacked_file) { if (is_file($unpack_dir . DIRECTORY_SEPARATOR . $unpacked_file)) { @rename($unpack_dir . DIRECTORY_SEPARATOR . $unpacked_file, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'supplement' . DIRECTORY_SEPARATOR . sprintf("%05d", intval($new_file)) . $unpacked_file); } } @rmdir($unpack_dir); }
if (file_exists($temp_dir . DIRECTORY_SEPARATOR . 'test.xml')) { unlink($temp_dir . DIRECTORY_SEPARATOR . 'test.xml'); die('OK'); } else { die; } } elseif ($_GET['binary'] == 'ghostscript') { exec(select_ghostscript() . ' -sDEVICE=png16m -r15 -dTextAlphaBits=1 -dGraphicsAlphaBits=1 -dFirstPage=1 -dLastPage=1 -o "' . $temp_dir . DIRECTORY_SEPARATOR . 'test.png" test.pdf'); if (file_exists($temp_dir . DIRECTORY_SEPARATOR . 'test.png')) { unlink($temp_dir . DIRECTORY_SEPARATOR . 'test.png'); die('OK'); } else { die; } } elseif ($_GET['binary'] == 'pdftk') { exec(select_pdftk() . 'test.pdf dump_data output "' . $temp_dir . DIRECTORY_SEPARATOR . 'test-pdftk.txt"'); if (is_readable($temp_dir . DIRECTORY_SEPARATOR . 'test-pdftk.txt') && filesize($temp_dir . DIRECTORY_SEPARATOR . 'test-pdftk.txt') > 0) { unlink($temp_dir . DIRECTORY_SEPARATOR . 'test-pdftk.txt'); die('OK'); } else { die; } } elseif ($_GET['binary'] == 'tesseract') { exec('tesseract ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'test.bmp "' . $temp_dir . DIRECTORY_SEPARATOR . 'test-tesseract"'); if (is_readable($temp_dir . DIRECTORY_SEPARATOR . 'test-tesseract.txt') && filesize($temp_dir . DIRECTORY_SEPARATOR . 'test-tesseract.txt') > 0) { unlink($temp_dir . DIRECTORY_SEPARATOR . 'test-tesseract.txt'); die('OK'); } else { die; } }
include_once '../functions.php'; session_write_close(); $file = preg_replace('/[^\\d\\.pdf]/', '', $_GET['file']); $file_name = $library_path . $file; if (is_readable($file_name)) { //ADD WATERMARKS if ($_SESSION['watermarks'] == 'nocopy') { $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $file . '-nocopy.pdf'; if (!file_exists($temp_file) || filemtime($temp_file) < filemtime($file_name)) { system(select_pdftk() . '"' . $file_name . '" multistamp "../nocopy.pdf" output "' . $temp_file . '"', $ret); } $file_name = $temp_file; } elseif ($_SESSION['watermarks'] == 'confidential') { $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $file . '-confidential.pdf'; if (!file_exists($temp_file) || filemtime($temp_file) < filemtime($file_name)) { system(select_pdftk() . '"' . $file_name . '" multistamp "../confidential.pdf" output "' . $temp_file . '"', $ret); } $file_name = $temp_file; } //RENDER FINISHED PDF header("Content-type: application/pdf"); if (!isset($_GET['mode'])) { header("Content-Disposition: inline; filename={$file}"); } if (isset($_GET['mode']) && $_GET['mode'] == 'download') { header("Content-Disposition: attachment; filename={$file}"); } header("Pragma: no-cache"); header("Expires: 0"); header('Content-Length: ' . filesize($file_name)); ob_clean();