Exemple #1
0
function add_pdf_to_db($path, $attributes)
{
    global $db;
    $path = canonicalize_path($path);
    if (!file_exists(get_full_path($path))) {
        err_bad_input_data('file_path', $path, "file doesn't exist");
    }
    $attributes['md5'] = get_md5_hash($path);
    if (!$attributes['md5']) {
        err_internal("could not generate hash of file {$path}");
    }
    // TODO: find number of pages
    $pages = 0;
    $sql = "INSERT INTO files VALUES (NULL, '" . addslashes($path) . "'" . ', ' . ($attributes['title'] ? "'" . addslashes($attributes['title']) . "'" : 'NULL') . ', ' . ($attributes['md5'] ? "'" . addslashes($attributes['md5']) . "'" : 'NULL') . ', ' . ($attributes['date'] ? "'" . addslashes($attributes['date']) . "'" : 'NULL') . ", " . $pages . ', ' . ($attributes['origin'] ? "'" . addslashes($attributes['origin']) . "'" : 'NULL') . ', ' . ($attributes['recipient'] ? "'" . addslashes($attributes['recipient']) . "'" : 'NULL') . ")";
    $db->exec($sql);
    return $db->lastInsertRowID();
}
function do_move()
{
    global $uploads_dir, $curr_abspath, $curr_relpath;
    $item_name = @$_POST['item_name'];
    confirm_is_local_file($item_name);
    $src_path = "{$curr_abspath}/{$item_name}";
    $dst_dir_relpath = canonicalize_path(trim(@$_POST['target_dir'], '/'));
    if ($dst_dir_relpath === False || !is_valid_move_destination($dst_dir_relpath)) {
        fatal_error(_("Invalid target folder"));
    }
    if ($dst_dir_relpath == $curr_relpath) {
        fatal_error(_("The source and destination folders are the same."));
    }
    $dst_dir = "{$uploads_dir}/{$dst_dir_relpath}";
    if (!is_dir($dst_dir)) {
        fatal_error(sprintf(_("%s does not exist, or is not a folder"), html_safe($dst_dir_relpath)));
    }
    $dst_path = "{$dst_dir}/{$item_name}";
    // Test for collision in destination
    if (file_exists($dst_path)) {
        fatal_error(_("File already exists in destination folder."));
    }
    if (!@rename($src_path, $dst_path)) {
        fatal_error(sprintf(_('Unable to move file %1$s to destination folder: %2$s.'), html_safe($item_name), html_safe($dst_dir_relpath)));
    }
    show_message('info', sprintf(_('File %1$s has been moved to folder %2$s'), html_safe($item_name), html_safe($dst_dir_relpath)));
    show_return_link();
}
Exemple #3
0
        } else {
            // it has no db record... insert it
            echo "inserting: {$path} \n";
            $attributes = array('md5' => $md5);
            print_r($attributes);
            add_pdf_to_db($path, $attributes);
        }
    }
}
$orphans = array();
// check every existing db record
foreach (find_pdfs_all() as $pdf_id) {
    $pdf = get_pdf_info($pdf_id);
    if ($pdf) {
        $path = $pdf['path'];
        $cpath = canonicalize_path($path);
        if (!file_exists(get_full_path($path))) {
            // the file was deleted. keep the data, but make it an orphan (no path)
            echo "*** file {$cpath} doesn't exist! adding to orphans list\n";
            $orphans[] = $pdf_id;
        } else {
            if ($path != $cpath) {
                // the path is not a cannonical path... fix it
                echo "path not canoncial: {$path} -> {$cpath}\n";
                $other_pdf_id = get_pdf_id($cpath);
                if ($other_pdf_id) {
                    // another record is using this file with an alternative path name... merge the records only
                    // TODO: merge metadata only -- they're using the same file
                    echo "redundant path with {$other_pdf_id}: merge with {$pdf_id} \n";
                    merge_pdf_metadata(array($pdf_id, $other_pdf_id), $cpath);
                } else {