# HG changeset patch # User Matti Hamalainen # Date 1384711490 -7200 # Node ID 8985d2bdb29b1372d2e46e2422ce1a99ae31222b # Parent e31c42a9b574a1c1c7c58dd59e1d46b3f5a586dd More work on error handling etc. diff -r e31c42a9b574 -r 8985d2bdb29b admajax.php --- a/admajax.php Sun Nov 17 19:26:46 2013 +0200 +++ b/admajax.php Sun Nov 17 20:04:50 2013 +0200 @@ -37,24 +37,17 @@ // XMLHttp responses -$action = "ERROR"; -if (stChkRequestItem("action") && stChkRequestItem("type")) -{ - $action = $_REQUEST["action"]; - $type = $_REQUEST["type"]; -} - - -switch ($action) +$type = stGetRequestItem("type", ""); +switch (stGetRequestItem("action", "")) { case "dump": // // Perform generic data dump // if (($res = stExecSQLCond( - "SELECT * FROM attendees WHERE email NOT NULL AND email != '' ORDER BY regtime DESC", - "Dump OK.")) !== FALSE) + "SELECT * FROM attendees WHERE email NOT NULL AND email != '' ORDER BY regtime DESC")) !== FALSE) { + stSetStatus(200, "Dump OK."); $out1 = array(); $out2 = array(); @@ -211,7 +204,7 @@ // // Perform query if we need to, output results // - if (isset($sql) && ($res = stExecSQLCond($sql, "")) !== FALSE) + if (isset($sql) && ($res = stExecSQLCond($sql)) !== FALSE) { if ($type == "news") { @@ -327,13 +320,13 @@ $prefix = "co"; echo "
\n". - "

#".$id." - ".chentities($item["name"])."

\n". - stGetFormTextInput(40, SET_LEN_COMPO_NAME, "name", $id, $prefix, $item["name"])."\n". - stGetFormCheckBoxInput("visible", $id, $prefix, $item["visible"], "Visible")."\n". - stGetFormCheckBoxInput("showAuthors", $id, $prefix, $item["showAuthors"], "Show authors")."\n". - stGetFormCheckBoxInput("voting", $id, $prefix, $item["voting"], "Enable voting")."
\n". - stGetFormTextArea(5, 60, "description", $id, $prefix, $item["description"])."\n
\n". - stGetFormButtonInput("update", $id, $prefix, " Update ", "updateCompo(".$id.")")."\n". + "

#".$id." - ".chentities($item["name"])."

\n". + " ".stGetFormTextInput(40, SET_LEN_COMPO_NAME, "name", $id, $prefix, $item["name"])."\n". + " ".stGetFormCheckBoxInput("visible", $id, $prefix, $item["visible"], "Visible")."\n". + " ".stGetFormCheckBoxInput("showAuthors", $id, $prefix, $item["showAuthors"], "Show authors")."\n". + " ".stGetFormCheckBoxInput("voting", $id, $prefix, $item["voting"], "Enable voting")."
\n". + " ".stGetFormTextArea(5, 60, "description", $id, $prefix, $item["description"])."\n
\n". + " ".stGetFormButtonInput("update", $id, $prefix, "Update", "updateCompo(".$id.")")."\n". "
\n". "
\n"; } @@ -345,10 +338,8 @@ // // Delete entry // - if (stChkRequestItem("id")) + if (stChkRequestItem("id", $id, array(CHK_TYPE, VT_INT, "Invalid data."))) { - $id = intval(stGetRequestItem("id")); - if ($type == "news") { $sql = stPrepareSQL("DELETE FROM news WHERE id=%d AND persist=0", $id); @@ -375,8 +366,6 @@ stExecSQLCond($sql, "OK, entry ".$id." votes deleted."); } } - else - stSetStatus(901, "No ID specified."); break; case "add": @@ -421,8 +410,6 @@ stExecSQLCond($sql, "OK, entry added."); } - else - stSetStatus(902, "No data."); break; case "update": @@ -483,7 +470,7 @@ $cid = stGetRequestItem("compo_id"); if (stFetchSQLColumn("SELECT id FROM compos WHERE id=".$cid) === FALSE) { - stSetStatus(903, "No such compo id."); + stError("No such compo id."); } else { @@ -509,8 +496,6 @@ stExecSQLCond($sql, "OK, setting updated."); } } - else - stSetStatus(902, "No data."); break; default: @@ -518,4 +503,11 @@ break; } + +if ($statusSet) + + +if ($errorSet) + echo $errorMsg; + ?> \ No newline at end of file diff -r e31c42a9b574 -r 8985d2bdb29b majax.php --- a/majax.php Sun Nov 17 19:26:46 2013 +0200 +++ b/majax.php Sun Nov 17 20:04:50 2013 +0200 @@ -66,6 +66,12 @@ window.location = ""; } else + if (req.status == 902) + { + statusMsg(req.statusText); + alert("Error", req.responseText); + } + else if (req.status == 200) { if (success) diff -r e31c42a9b574 -r 8985d2bdb29b msitegen.inc.php --- a/msitegen.inc.php Sun Nov 17 19:26:46 2013 +0200 +++ b/msitegen.inc.php Sun Nov 17 20:04:50 2013 +0200 @@ -8,6 +8,8 @@ // Globals and definitions $errorSet = FALSE; $errorMsg = ""; +$statusSet = 0; +$statusMsg = ""; // Value types define("VT_STR", 1); @@ -38,6 +40,14 @@ } +function stSetStatus($status, $msg) +{ + global $statusSet, $statusMsg; + $statusMsg = $msg; + $statusSet = $status; +} + + function stCheckHTTPS() { return isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != "" && $_SERVER["HTTPS"] != "off"); @@ -249,7 +259,7 @@ function stChkRequestItem($name, &$sdata) { if (!isset($_REQUEST[$name])) - return FALSE; + return stErrorStrF("Required data item '".$name."' not set.", $name); $data = trim($_REQUEST[$name]); $slen = strlen($data); @@ -482,28 +492,17 @@ } -function stSetStatus($val, $msg) -{ - global $statusSet; - if (!$statusSet) - { - header("Status: ".$val." ".$msg); - } - $statusSet = TRUE; -} - - -function stExecSQLCond($sql, $okmsg) +function stExecSQLCond($sql, $msg = FALSE) { if (($res = stExecSQL($sql)) !== FALSE) { - if ($okmsg != "") - stSetStatus(200, $okmsg); + if ($msg !== FALSE) + stSetStatus(200, $msg); return $res; } else { - stSetStatus(900, "Error in SQL execution."); + stSetStatus(902, "Error in SQL execution."); return FALSE; } }