changeset 208:8985d2bdb29b

More work on error handling etc.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 17 Nov 2013 20:04:50 +0200
parents e31c42a9b574
children 3870601c17c3
files admajax.php majax.php msitegen.inc.php
diffstat 3 files changed, 42 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- 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
             "<div id=\"compo".$id."\">\n".
-            "<h2>#".$id." - ".chentities($item["name"])."</h2>\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")."<br />\n".
-            stGetFormTextArea(5, 60, "description", $id, $prefix, $item["description"])."\n<br />\n".
-            stGetFormButtonInput("update", $id, $prefix, " Update ", "updateCompo(".$id.")")."\n".
+            "  <h2>#".$id." - ".chentities($item["name"])."</h2>\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")."<br />\n".
+            "  ".stGetFormTextArea(5, 60, "description", $id, $prefix, $item["description"])."\n<br />\n".
+            "  ".stGetFormButtonInput("update", $id, $prefix, "Update", "updateCompo(".$id.")")."\n".
             "</div>\n".
             "<hr />\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
--- 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 = "<? echo $failover ?>";
       }
       else
+      if (req.status == 902)
+      {
+        statusMsg(req.statusText);
+        alert("Error", req.responseText);
+      }
+      else
       if (req.status == 200)
       {
         if (success)
--- 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;
   }
 }