rework initial sanitycheck to use JSON

This commit is contained in:
Andrew Dolgov 2011-03-18 17:39:23 +03:00
parent 81f6deea47
commit ebb948c24e
7 changed files with 22 additions and 127 deletions

View File

@ -69,18 +69,6 @@
authenticate_user($link, "admin", null); authenticate_user($link, "admin", null);
} }
/* if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds"
&& $op != "rss" && $op != "getUnread" && $op != "publish" && $op != "getProfiles") {
if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
print_error_xml(6); exit;
} else {
header("Location: tt-rss.php?return=" .
urlencode($_SERVER['REQUEST_URI']));
}
exit;
} */
if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" && if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
$op != "rss" && $op != "getUnread" && $op != "getProfiles") { $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
@ -144,7 +132,12 @@
require_once "modules/pref-labels.php"; require_once "modules/pref-labels.php";
require_once "modules/pref-users.php"; require_once "modules/pref-users.php";
if (!sanity_check($link)) { return; } $error = sanity_check($link);
if ($error['code'] != 0) {
print json_encode(array("error" => $error));
return;
}
switch($op) { // Select action according to $op value. switch($op) { // Select action according to $op value.
case "rpc": case "rpc":

View File

@ -1,7 +1,7 @@
<?php <?php
require_once "functions.php"; require_once "functions.php";
$ERRORS[0] = __("Unknown error"); $ERRORS[0] = "";
$ERRORS[1] = __("This program requires XmlHttpRequest " . $ERRORS[1] = __("This program requires XmlHttpRequest " .
"to function properly. Your browser doesn't seem to support it."); "to function properly. Your browser doesn't seem to support it.");

View File

@ -1055,36 +1055,26 @@ function backend_sanity_check_callback(transport) {
return; return;
} }
if (!transport.responseXML) { var reply = JSON.parse(transport.responseText);
if (!store) {
fatalError(3, "Sanity check: Received reply is not XML",
transport.responseText);
return;
}
}
var reply = transport.responseXML.getElementsByTagName("error")[0];
if (!reply) { if (!reply) {
fatalError(3, "Sanity check: invalid RPC reply", transport.responseText); fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
return; return;
} }
var error_code = reply.getAttribute("error-code"); var error_code = reply['error']['code'];
if (error_code && error_code != 0) { if (error_code && error_code != 0) {
return fatalError(error_code, reply.getAttribute("error-msg")); return fatalError(error_code, reply['error']['message']);
} }
console.log("sanity check ok"); console.log("sanity check ok");
var params = transport.responseXML.getElementsByTagName("init-params")[0]; var params = reply['init-params'];
if (params) { if (params) {
console.log('reading init-params...'); console.log('reading init-params...');
params = JSON.parse(params.firstChild.nodeValue);
if (params) { if (params) {
for (k in params) { for (k in params) {
var v = params[k]; var v = params[k];

View File

@ -2227,6 +2227,8 @@
function sanity_check($link) { function sanity_check($link) {
global $ERRORS;
$error_code = 0; $error_code = 0;
$schema_version = get_schema_version($link); $schema_version = get_schema_version($link);
@ -2245,12 +2247,7 @@
$error_code = 12; $error_code = 12;
} }
if ($error_code != 0) { return array("code" => $error_code, "message" => $ERRORS[$error_code]);
print_error_xml($error_code);
return false;
} else {
return true;
}
} }
function file_is_locked($filename) { function file_is_locked($filename) {
@ -2851,20 +2848,6 @@
return $version[1]; return $version[1];
} }
function print_error_xml($code, $add_msg = "") {
global $ERRORS;
$error_msg = $ERRORS[$code];
if ($add_msg) {
$error_msg = "$error_msg; $add_msg";
}
print "<rpc-reply>";
print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
print "</rpc-reply>";
}
/** /**
* Subscribes the user to the given feed * Subscribes the user to the given feed
* *

View File

@ -307,23 +307,21 @@
return; return;
} }
// XML method
if ($subop == "sanityCheck") { if ($subop == "sanityCheck") {
header("Content-Type: text/plain");
$_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true"; $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
print "<rpc-reply>"; $reply = array();
if (sanity_check($link)) {
print "<error error-code=\"0\"/>";
print "<init-params><![CDATA["; $reply['error'] = sanity_check($link);
print json_encode(make_init_params($link));
print "]]></init-params>";
print_runtime_info($link); if ($reply['error']['code'] == 0) {
$reply['init-params'] = make_init_params($link);
$reply['runtime-info'] = make_runtime_info($link);
} }
print "</rpc-reply>";
print json_encode($reply);
return; return;
} }

View File

@ -1539,23 +1539,6 @@ function clearFeedAccessKeys() {
return false; return false;
} }
function handle_rpc_reply(transport, scheduled_call) {
try {
if (transport.responseXML) {
if (!transport_error_check(transport)) return false;
} else {
notify_error("Error communicating with server.");
}
} catch (e) {
exception_error("handle_rpc_reply", e, transport);
}
return true;
}
function resetFeedOrder() { function resetFeedOrder() {
try { try {
notify_progress("Loading, please wait..."); notify_progress("Loading, please wait...");

View File

@ -1022,58 +1022,6 @@ function showFeedsWithErrors() {
displayDlg('feedUpdateErrors'); displayDlg('feedUpdateErrors');
} }
function handle_rpc_reply(transport, scheduled_call) {
try {
if (transport.responseXML) {
if (!transport_error_check(transport)) return false;
var seq = transport.responseXML.getElementsByTagName("seq")[0];
if (seq) {
seq = seq.firstChild.nodeValue;
if (get_seq() != seq) {
//console.log("[handle_rpc_reply] sequence mismatch: " + seq);
return true;
}
}
var message = transport.responseXML.getElementsByTagName("message")[0];
if (message) {
message = message.firstChild.nodeValue;
if (message == "UPDATE_COUNTERS") {
console.log("need to refresh counters...");
setInitParam("last_article_id", -1);
_force_scheduled_update = true;
}
}
var counters = transport.responseXML.getElementsByTagName("counters")[0];
if (counters)
parse_counters(JSON.parse(counters.firstChild.nodeValue), scheduled_call);
var runtime_info = transport.responseXML.getElementsByTagName("runtime-info")[0];
if (runtime_info)
parse_runtime_info(JSON.parse(runtime_info.firstChild.nodeValue));
hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
} else {
notify_error("Error communicating with server.");
}
} catch (e) {
exception_error("handle_rpc_reply", e, transport);
}
return true;
}
function scheduleFeedUpdate(id, is_cat) { function scheduleFeedUpdate(id, is_cat) {
try { try {
if (!id) { if (!id) {