function update_paper($pj, $opj, $qreq, $action, $diffs)
{
    global $Conf, $Me, $Opt, $OK, $Error, $prow;
    // XXX lock tables
    $ps = new PaperStatus($Me);
    $saved = $ps->save_paper_json($pj);
    if (!$saved && !$prow && count($qreq->_FILES)) {
        $ps->set_error_html("paper", "<strong>Your uploaded files were ignored.</strong>");
    }
    if (!get($pj, "collaborators") && $Conf->setting("sub_collab")) {
        $field = $Conf->setting("sub_pcconf") ? "Other conflicts" : "Potential conflicts";
        $ps->set_warning_html("collaborators", "Please enter the authors’ potential conflicts in the {$field} field. If none of the authors have potential conflicts, just enter “None”.");
    }
    $Error = $ps->error_fields();
    if (!$saved) {
        $emsg = $ps->error_html();
        Conf::msg_error("There were errors in saving your paper. Please fix them and try again." . (count($emsg) ? "<ul><li>" . join("</li><li>", $emsg) . "</li></ul>" : ""));
        return false;
    }
    // note differences in contacts
    $contacts = $ocontacts = [];
    foreach (get($pj, "contacts", []) as $v) {
        $contacts[] = strtolower(is_string($v) ? $v : $v->email);
    }
    if ($opj && get($opj, "contacts")) {
        foreach ($opj->contacts as $v) {
            $ocontacts[] = strtolower($v->email);
        }
    }
    sort($contacts);
    sort($ocontacts);
    if (json_encode($contacts) !== json_encode($ocontacts)) {
        $diffs["contacts"] = true;
    }
    // submit paper if no error so far
    $_REQUEST["paperId"] = $_GET["paperId"] = $qreq->paperId = $pj->pid;
    loadRows();
    if ($action === "final") {
        $submitkey = "timeFinalSubmitted";
        $storekey = "finalPaperStorageId";
    } else {
        $submitkey = "timeSubmitted";
        $storekey = "paperStorageId";
    }
    $wasSubmitted = $opj && get($opj, "submitted");
    if (get($pj, "submitted") || $Conf->can_pc_see_all_submissions()) {
        $Conf->update_papersub_setting(true);
    }
    if ($wasSubmitted != get($pj, "submitted")) {
        $diffs["submission"] = 1;
    }
    // confirmation message
    if ($action == "final") {
        $actiontext = "Updated final version of";
        $template = "@submitfinalpaper";
    } else {
        if (get($pj, "submitted") && !$wasSubmitted) {
            $actiontext = "Submitted";
            $template = "@submitpaper";
        } else {
            if (!$opj) {
                $actiontext = "Registered new";
                $template = "@registerpaper";
            } else {
                $actiontext = "Updated";
                $template = "@updatepaper";
            }
        }
    }
    // additional information
    $notes = array();
    if ($action == "final") {
        if ($prow->{$submitkey} === null || $prow->{$submitkey} <= 0) {
            $notes[] = "The final version has not yet been submitted.";
        }
        $deadline = $Conf->printableTimeSetting("final_soft", "span");
        if ($deadline != "N/A" && $Conf->deadlinesAfter("final_soft")) {
            $notes[] = "<strong>The deadline for submitting final versions was {$deadline}.</strong>";
        } else {
            if ($deadline != "N/A") {
                $notes[] = "You have until {$deadline} to make further changes.";
            }
        }
    } else {
        if (get($pj, "submitted")) {
            $notes[] = "You will receive email when reviews are available.";
        } else {
            if ($prow->size == 0 && !opt("noPapers")) {
                $notes[] = "The submission has not yet been uploaded.";
            } else {
                if ($Conf->setting("sub_freeze") > 0) {
                    $notes[] = "The submission has not yet been completed.";
                } else {
                    $notes[] = "The submission is marked as not ready for review.";
                }
            }
        }
        $deadline = $Conf->printableTimeSetting("sub_update", "span");
        if ($deadline != "N/A" && ($prow->timeSubmitted <= 0 || $Conf->setting("sub_freeze") <= 0)) {
            $notes[] = "Further updates are allowed until {$deadline}.";
        }
        $deadline = $Conf->printableTimeSetting("sub_sub", "span");
        if ($deadline != "N/A" && $prow->timeSubmitted <= 0) {
            $notes[] = "<strong>If the submission " . ($Conf->setting("sub_freeze") > 0 ? "is not completed" : "is not ready for review") . " by {$deadline}, it will not be considered.</strong>";
        }
    }
    $notes = join(" ", $notes);
    $webnotes = "";
    if (count($ps->error_html())) {
        $webnotes .= " <ul><li>" . join("</li><li>", $ps->error_html()) . "</li></ul>";
    }
    if (!count($diffs)) {
        $Conf->warnMsg("There were no changes to submission #{$prow->paperId}. " . $notes . $webnotes);
        return true;
    }
    // HTML confirmation
    if ($prow->{$submitkey} > 0) {
        $Conf->confirmMsg($actiontext . " submission #{$prow->paperId}. " . $notes . $webnotes);
    } else {
        $Conf->warnMsg($actiontext . " submission #{$prow->paperId}. " . $notes . $webnotes);
    }
    // mail confirmation to all contact authors
    if (!$Me->privChair || $qreq->doemail > 0) {
        $options = array("infoNames" => 1);
        if ($Me->privChair && $prow->conflictType < CONFLICT_AUTHOR) {
            $options["adminupdate"] = true;
        }
        if ($Me->privChair && isset($qreq->emailNote)) {
            $options["reason"] = $qreq->emailNote;
        }
        if ($notes !== "") {
            $options["notes"] = preg_replace(",</?(?:span.*?|strong)>,", "", $notes) . "\n\n";
        }
        HotCRPMailer::send_contacts($template, $prow, $options);
    }
    // other mail confirmations
    if ($action == "final" && $OK && !count($Error)) {
        $prow->notify(WATCHTYPE_FINAL_SUBMIT, "final_submit_watch_callback", $Me);
    }
    $Me->log_activity($actiontext, $prow->paperId);
    return true;
}
$Admin = Contact::create(array("email" => "chair@_.com", "name" => "Jane Chair", "password" => "testchair"));
$Admin->save_roles(Contact::ROLE_ADMIN | Contact::ROLE_CHAIR | Contact::ROLE_PC, $Admin);
// Load data.
$json = json_decode(file_get_contents("{$ConfSitePATH}/test/db.json"));
if (!$json) {
    die_hard("* test/testdb.json error: " . json_last_error_msg() . "\n");
}
foreach ($json->contacts as $c) {
    $us = new UserStatus();
    if (!$us->save($c)) {
        die_hard("* failed to create user {$c->email}\n");
    }
}
foreach ($json->papers as $p) {
    $ps = new PaperStatus(null);
    if (!$ps->save_paper_json($p)) {
        die_hard("* failed to create paper {$p->title}:\n" . htmlspecialchars_decode(join("\n", $ps->error_html())) . "\n");
    }
}
$assignset = new AssignmentSet($Admin, true);
$assignset->parse($json->assignments_1, null, null);
$assignset->execute();
class Xassert
{
    public static $n = 0;
    public static $nsuccess = 0;
    public static $nerror = 0;
    public static $emap = array(E_ERROR => "PHP Fatal Error", E_WARNING => "PHP Warning", E_NOTICE => "PHP Notice", E_USER_ERROR => "PHP Error", E_USER_WARNING => "PHP Warning", E_USER_NOTICE => "PHP Notice");
}
function xassert_error_handler($errno, $emsg, $file, $line)
{
foreach ($jp as $j) {
    ++$index;
    if (isset($j->pid) && is_int($j->pid) && $j->pid > 0) {
        $prefix = "#{$j->pid}: ";
    } else {
        if (!isset($j->pid) && isset($j->id) && is_int($j->id) && $j->id > 0) {
            $prefix = "#{$j->id}: ";
        } else {
            if (!isset($j->pid) && !isset($j->id)) {
                $prefix = "new paper #{$index}: ";
            } else {
                fwrite(STDERR, "paper #{$index}: bad pid\n");
                exit(1);
            }
        }
    }
    if (!$quiet) {
        fwrite(STDERR, $prefix);
    }
    $ps = new PaperStatus(null, ["no_email" => true, "disable_users" => $disable_users, "allow_error" => ["topics", "options"]]);
    $res = $ps->save_paper_json($j);
    if (!$quiet) {
        fwrite(STDERR, $res ? "saved\n" : "failed\n");
    }
    foreach ($ps->error_html() as $msg) {
        fwrite(STDERR, $prefix . htmlspecialchars_decode($msg) . "\n");
    }
    if (!$res) {
        exit(1);
    }
}
<?php

// test05.php -- HotCRP paper submission tests
// HotCRP is Copyright (c) 2006-2016 Eddie Kohler and Regents of the UC
// Distributed under an MIT-like license; see LICENSE
global $ConfSitePATH;
$ConfSitePATH = preg_replace(",/[^/]+/[^/]+\$,", "", __FILE__);
require_once "{$ConfSitePATH}/test/setup.php";
$Conf->save_setting("sub_open", 1);
$Conf->save_setting("sub_update", $Now + 100);
$Conf->save_setting("sub_sub", $Now + 100);
// load users
$user_estrin = Contact::find_by_email("*****@*****.**");
// pc
$user_nobody = new Contact();
$ps = new PaperStatus($user_estrin);
$paper1a = $ps->paper_json(1);
xassert_eqq($paper1a->title, "Scalable Timers for Soft State Protocols");
$ps->save_paper_json((object) ["id" => 1, "title" => "Scalable Timers? for Soft State Protocols"]);
xassert(!$ps->nerrors);
$paper1b = $ps->paper_json(1);
xassert_eqq($paper1b->title, "Scalable Timers? for Soft State Protocols");
$paper1b->title = $paper1a->title;
$paper1b->submitted_at = $paper1a->submitted_at;
xassert_eqq(json_encode($paper1b), json_encode($paper1a));
$doc = Filer::file_upload_json(["error" => UPLOAD_ERR_OK, "name" => "amazing-sample.pdf", "tmp_name" => "{$ConfSitePATH}/src/sample.pdf", "tmp_name_safe" => true, "type" => "application/pdf"]);
$ps->save_paper_json((object) ["id" => 1, "submission" => $doc]);
xassert(!$ps->nerrors);
$paper1c = $ps->paper_json(1);
xassert_eqq($paper1c->submission->sha1, "2f1bccbf1e0e98004c01ef5b26eb9619f363e38e");
xassert_exit();
xassert_eqq($te2_cdb->affiliation, "String");
// borrow from cdb
$acct = $us->save((object) ["email" => "te@_.com"]);
xassert(!!$acct);
$te = user("te@_.com");
xassert_eqq($te->email, "te@_.com");
xassert_eqq($te->firstName, "Te");
xassert_eqq($te->lastName, "Thamrongrattanarit");
xassert_eqq($te->affiliation, "Brandeis University");
xassert_eqq($te->collaborators, "Computational Linguistics Magazine");
// create a user in cdb: create, then delete from local db
$anna = "*****@*****.**";
xassert(!user($anna));
$acct = $us->save((object) ["email" => $anna, "first" => "Anna", "last" => "Akhmatova"]);
xassert(!!$acct);
Dbl::qe("delete from ContactInfo where email=?", $anna);
save_password($anna, "aquablouse", true);
xassert(!user($anna));
$user_estrin = user("*****@*****.**");
$user_floyd = user("*****@*****.**");
$user_van = user("*****@*****.**");
$ps = new PaperStatus(null);
$ps->save_paper_json((object) ["id" => 1, "authors" => ["*****@*****.**", $user_estrin->email, $user_floyd->email, $user_van->email, $anna]]);
$paper1 = $Conf->paperRow(1, $user_chair);
$user_anna = user($anna);
xassert(!!$user_anna);
xassert($user_anna->act_author_view($paper1));
xassert($user_estrin->act_author_view($paper1));
xassert($user_floyd->act_author_view($paper1));
xassert($user_van->act_author_view($paper1));
xassert_exit();