// Straightaway clear the Bible action for this chapter. // This atomic operation enables new edits from the user in this chapter to be recorded straightaway, // even during the time that this chapter is still being sent. // In the face of a slow network at times, this does occur in practise. // Examples have been seen. $database_bibleactions->delete($bible, $book, $chapter); // If old USFM and new USFM differ, and the new USFM is not empty, send it to the server. if ($newusfm != $oldusfm && $newusfm != "") { $checksum = Checksum_Logic::get($oldusfm . $newusfm); // It used to send the old and new USFM in compressed form. // But the server failed to decompress it. // That means that compression is not a good option. // Generate a POST request. $post = array("u" => bin2hex($user), "p" => $hash, "bi" => $bible, "bo" => $book, "ch" => $chapter, "o" => $oldusfm, "n" => $newusfm, "s" => $checksum); $url = "{$address}/sync/bible.php"; $response = Sync_Logic::post($post, $url); if ($response === false) { // Communication error. $communication_errors = true; $database_logs->log("Failure sending Bibles", Filter_Roles::TRANSLATOR_LEVEL); // Restore the Bible action for this chapter. $database_bibleactions->delete($bible, $book, $chapter); $database_bibleactions->record($bible, $book, $chapter, $oldusfm); } else { if ($response != "") { // The first line of the response can contain a message as a string. // This is what the server says to the client in case of an error. // Normally the first line of the response contains a numerical value. // This value is the checksum of the data that the server returns in the subsequent lines. // This data would be the updated USFM for the chapter. $response = explode("\n", $response);
// Get checksum for the chapter on client and on server. // If both are the same, it means the USFM in both is the same, and we're done. $client_checksum = Checksum_Logic::getChapter($bible, $book, $chapter); $post = array("b" => $bible, "bk" => $book, "c" => $chapter, "a" => "chapter"); $server_checksum = Sync_Logic::post($post, $url); if ($server_checksum === false) { $database_logs->log(Locale_Translate::_("Failure getting checksum:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); continue; } if ($client_checksum == $server_checksum) { continue; } // Different checksums: Get the USFM for the chapter as it is on the server. $database_logs->log(Locale_Translate::_("Getting chapter:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); $post = array("b" => $bible, "bk" => $book, "c" => $chapter, "a" => "get"); $server_usfm = Sync_Logic::post($post, $url); if ($server_usfm === false) { $database_logs->log(Locale_Translate::_("Failure getting chapter:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); continue; } // Verify the checksum of the chapter on the server, to be sure there's no corruption during transmission. $server_usfm = explode("\n", $server_usfm); $checksum = array_shift($server_usfm); $server_usfm = implode("\n", $server_usfm); if (!Checksum_Logic::get($server_usfm) == $checksum) { $database_logs->log(Locale_Translate::_("Checksum error while receiving chapter from server:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); continue; } // Check whether the user on the client has made changes in this chapter since the edits were sent to the server. // If there are none, then the client stores the chapter as it gets it from the server, and is done. $old_usfm = $database_bibleactions->getUsfm($bible, $book, $chapter);