示例#1
0
function record_unknown($dbHandle, $title, $string, $file, $userID)
{
    $query = "INSERT INTO library (file, title, title_ascii, addition_date, rating, added_by, bibtex)\r\n             VALUES ((SELECT IFNULL((SELECT SUBSTR('0000' || CAST(MAX(id)+1 AS TEXT) || '.pdf',-9,9) FROM library),'00001.pdf')),\r\n             :title, :title_ascii, :addition_date, :rating, :added_by,\r\n             'unknown-0000-ID' || (SELECT IFNULL((SELECT MAX(id)+1 FROM library), 1)))";
    $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);
    }
    $file_extension = pathinfo($title, PATHINFO_EXTENSION);
    $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;
    $id = $dbHandle->lastInsertId();
    $new_file = str_pad($id, 5, "0", STR_PAD_LEFT) . '.pdf';
    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})");
    }
    if (isset($_GET['project']) && !empty($_GET['projectID'])) {
        $dbHandle->exec("INSERT OR IGNORE INTO projectsfiles (projectID,fileID) VALUES (" . intval($_GET['projectID']) . "," . intval($id) . ")");
    }
    ####### 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();
            $category_ids[] = $dbHandle->lastInsertId();
        }
    }
    $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, IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $new_file);
    $hash = md5_file(IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $new_file);
    //record office file into supplement
    if (in_array($file_extension, array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp'))) {
        //record original file into supplement
        $supplement_filename = sprintf("%05d", intval($new_file)) . $title;
        copy(IL_TEMP_PATH . DIRECTORY_SEPARATOR . $title, IL_SUPPLEMENT_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $supplement_filename);
        unlink(IL_TEMP_PATH . DIRECTORY_SEPARATOR . $title);
    }
    //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(IL_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;
    }
    $unpack_dir = IL_TEMP_PATH . DIRECTORY_SEPARATOR . $new_file;
    mkdir($unpack_dir);
    exec(select_pdfdetach() . ' -saveall -o "' . $unpack_dir . '" "' . IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $new_file . '"');
    $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, IL_SUPPLEMENT_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . sprintf("%05d", intval($new_file)) . $unpacked_file);
        }
    }
    rmdir($unpack_dir);
}
示例#2
0
             $supplement_filename = preg_replace('/[^a-zA-Z0-9\\-\\_\\.]/', '_', $supplement_filename);
             $supplement_filename = sprintf("%05d", $new_file) . $supplement_filename;
             $move = move_uploaded_file($_FILES['form_supplementary_file']['tmp_name'][$i], IL_SUPPLEMENT_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $supplement_filename);
             if ($move == false) {
                 $error[] = "Error! The supplementary file " . $_FILES['form_supplementary_file']['name'][$i] . " has not been recorded.";
             }
             if ($move == true) {
                 $message[] = "The supplementary file file " . $_FILES['form_supplementary_file']['name'][$i] . " has been recorded.";
             }
         }
     }
 }
 if (file_exists(IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $new_file)) {
     $unpack_dir = IL_TEMP_PATH . DIRECTORY_SEPARATOR . $new_file;
     mkdir($unpack_dir);
     exec(select_pdfdetach() . ' -saveall -o "' . $unpack_dir . '" "' . IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . DIRECTORY_SEPARATOR . $new_file . '"');
     $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, IL_SUPPLEMENT_PATH . DIRECTORY_SEPARATOR . get_subfolder($new_file) . 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;
    exec(select_pdfinfo() . ' test.pdf', $output);
    if (!empty($output)) {
        die('OK');
    } else {
        die;
    }
} elseif ($_GET['binary'] == 'pdftohtml') {
    exec(select_pdftohtml() . ' -q -noframes -enc UTF-8 -nomerge -c -xml test.pdf "' . IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test"');
    if (file_exists(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.xml')) {
        unlink(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.xml');
        die('OK');
    } else {
        die;
    }
} elseif ($_GET['binary'] == 'pdfdetach') {
    exec(select_pdfdetach() . ' -saveall -o "' . IL_TEMP_PATH . '" test.pdf');
    if (is_readable(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.odt') && filesize(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.odt') > 0) {
        unlink(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.odt');
        die('OK');
    } else {
        die;
    }
} elseif ($_GET['binary'] == 'ghostscript') {
    exec(select_ghostscript() . ' -sDEVICE=png16m -r15 -dTextAlphaBits=1 -dGraphicsAlphaBits=1 -dFirstPage=1 -dLastPage=1 -o "' . IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.png" test.pdf');
    if (file_exists(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.png')) {
        unlink(IL_TEMP_PATH . DIRECTORY_SEPARATOR . 'test.png');
        die('OK');
    } else {
        die;
    }
} elseif ($_GET['binary'] == 'tesseract') {