Network Object



 
The Network object provides network functionality and information.


Members Table

Remarks
    This object is available through the main object SiteKiosk. Use the Network object to create and send a HTTP request or to receive information about the network and downloads.
    In combination with the ScriptDevice object the Network object provides the possibility to control the SiteCash account from another computer through HTTP.

    Note that the path of a file using SiteKiosk objects must be allowed in the
    SiteKiosk configuration (Security -> Access -> URL's With Script Permission)
    if it is not a browser skin file.
Examples
    The following example represents a complete payment solution that uses the ScriptDevice object and the Network object to read and set values of an account that is saved on a server.


    This is the login dialog. The soure code of the script device dialog which is defined in the SkinDef file of the browser skin should be replaced with this one.
    The payment simulation dialog box of the SiteKiosk Payment Module must be enabled in the SiteKiosk configuration.

    <html>
    <head>
    <title>Script & Test Device</title>
    <style type='text/css'>
    <!--
    body
    {
       margin:0px;
       font-family:Arial;
       font-size:9pt;
    }
    td
    {
       font-family:Arial;
       font-size:9pt;
    }
    //-->
    </style>
    <script type='text/jscript'>
    // We still need some global objects in order to emulate some
    // kind of "session". First, our currency formatter (used with
    // all the numbers)
    var CurFormatter;
    
    // Then, the Post Object. Needed in order to communicate with our
    // webserver
    var PostObj = new Object();
    
    // The User Object holds all user-relevant information
    var UserObj = new Object();
    
    // And the number of open posts started by the "OnSiteCashDebit"
    // -function
    var openposts = 0;
    
    // Here all our posts will land
    var ServerPage = "http://vision.sitekiosk.net/nocoinsys/"
    + "userpost.php";
    
    // ---- Function ----
    // InitDialog
    // ---- Parameters --
    // none
    // ------------------
    // this function is called while initializing the page
    function InitDialog()
    {
       // we need to set up our SiteKiosk-interface
       window.external.InitScriptInterface();
    
       // Init the ScriptDevice
       // The account needs to be enabled in order to debit/credit it
       ScriptDevice.AccountEnabled = true;
    
       // Volatile means the accountmoney can change spontaneously
       ScriptDevice.Volatile = true;
    
       // Retransfer enables the Device to handle the money itself,
       // when user wants to log out
       ScriptDevice.RetransferEnabled = true;
    
       // Eventhandler for the log out
       ScriptDevice.OnCheckRetransfer = OnCheckRetransfer;
    
       // Eventhandler for the log out
       ScriptDevice.OnRetransfer = OnRetransfer;
    
       // Eventhandler for "Volatile" mode
       ScriptDevice.OnSiteCashDebit = OnSiteCashDebit;
    
       // Create our currency formatter
       var SCash = SiteKiosk.Plugins("SiteCash");
       CurFormatter = SCash.CreateCurrencyFormatter();
    			
       // Create the post object
       PostObj = SiteKiosk.Network.CreateHTTPPost();
       // bind the eventhandler used when the post returns
       PostObj.OnPostComplete = OnPostComplete;
    
       // Debug-notifications
       SiteKiosk.Logfile.Notification("ScriptDevice Initialized!");
       SiteKiosk.Logfile.Notification("ScriptDevice's remaining "
       + "money (last session): " + ScriptDevice.Balance);
    }
    
    // ---- Function ----
    // FormSubmit
    // ---- Parameters --
    // none
    // ------------------
    // User gave her login-infos and clicked on "OK" - create
    // HTTP-Post Object and send to the server
    function FormSubmit()
    {
       // Debug notification
       SiteKiosk.Logfile.Notification("ScriptDevice Logging in!");
    
       // Prearrange the Post Object parameters
       PostObj.OnPostComplete = OnPostComplete;
       PostObj.AddParameter("login", "SiteKiosk");
       PostObj.AddParameter("action", "login");
       PostObj.AddParameter("login_name", edtLoginName.value);
       PostObj.AddParameter("login_password", edtLoginPassword.value);
    			
       UserObj.name = edtLoginName.value;
       UserObj.password = edtLoginPassword.value;
    				
       // And posting...
       PostObj.Submit(ServerPage);
    }
    		
    // ---- Event --------
    // OnPostComplete
    // ---- Parameters --
    // success - >= 0 means "OK", post was successful
    // returnvalue - the page that was actually thrown by the server
    // ------------------
    // User wanted to log in, server sent an answer where extended
    // info to the user is given
    function OnPostComplete(success, returnvalue)
    {
       // Debug notification
       SiteKiosk.Logfile.Notification("Got answer from Server: "
       + returnvalue);
    			
       // Parse our return value and put it into local variables
       ParseReturnValue(returnvalue, false);
    
       // To be sure, create a new post object
       PostObj = SiteKiosk.Network.CreateHTTPPost();
       PostObj.OnPostComplete = OnPostComplete;
    }
    		
    // ---- Event -------
    // OnCheckRetransfer
    // ---- Parameters --
    // amount - the amount that's retransferred (after logout)
    // ------------------
    // SiteKiosk calls this function before it really retransfers the
    // money returnvalue "true" means that everything can be
    // transferred, "false" means "stop event chain here"
    function OnCheckRetransfer(amount)
    {
       return true;
    }
    
    // ---- Event -------
    // OnRetransfer
    // ---- Parameters --
    // amount - the amount that's retransferred (after logout)
    // ------------------
    // SiteKiosk got the OK to transfer the money back to the user 
    // (or, in this case - the Database)
    function OnRetransfer(amount)
    {
       // We want a new event handler for our post object (can't be
       // handled the same way as the other ones)
       PostObj.OnPostComplete = OnREPostComplete;
    
       // Debug notification
       SiteKiosk.Logfile.Notification("ScriptDevice Logout  Amount: "
       + amount);
    		
       // Parameters for the server
       // for details, have a look at the other part of the tutorial
       // (Server-part)
       PostObj.AddParameter("login", "SiteKiosk");
       PostObj.AddParameter("action", "setmoney");
       PostObj.AddParameter("MoneyBack", ScriptDevice.Balance
       + amount);
       PostObj.AddParameter("login_name", UserObj.name);
       PostObj.AddParameter("login_password", UserObj.password);
    
       // Now, send the request to the server!
       PostObj.Submit(ServerPage);
    		
       // We need to debit the account manually in order to reset our
       // SiteCash account
       ScriptDevice.Debit(ScriptDevice.Balance, false);
    
       return true;
    }
    
    // ---- Event -------
    // OnREPostComplete
    // ---- Parameters --
    // success - >= 0 means "OK", post was successful
    // returnvalue - the page that was actually thrown by the server
    // ------------------
    // User logged out, server needed to be notified and the result of
    //this post lands here
    function OnREPostComplete(success, returnvalue)
    {
       // Debug notification
       SiteKiosk.Logfile.Notification("Got answer from Server (RE): "
       + returnvalue);
    			
       // Parse our returned value and don't put it into local
       // variables
       ParseReturnValue(returnvalue, true);
    			
       // To be sure that there are no corpses, create a new post
       // object
       PostObj = SiteKiosk.Network.CreateHTTPPost();
       PostObj.OnPostComplete = OnPostComplete;
    			
       // User logged out, so destroy the old user infos
       edtLoginName.value = "";
       edtLoginPassword.value = "";
       LogInfo.innerHTML = "";
       UserObj = new Object();
    
       // Display the login box again
       trName.style.display = "block";
       trPW.style.display = "block";
       trOK.style.display = "block";
    }
    		
    // ---- Function ----
    // ParseReturnValue
    // ---- Parameters --
    // value - the actual return value from the server
    // is_retransfer - when this is true, the user already logged out,
    // so don't do anything
    // ------------------
    // Here the extended info, given by the server, is parsed and put
    // into local (JS-Global) variables
    function ParseReturnValue(value, is_retransfer)
    {
       // The delimiter for each value is "|" (for details, look at
       // part 2 of the tutorial (Server part))
       var RetArr = value.split("|");
    				
       // Is this a retransfer (log out)?
       if (is_retransfer == false)
       {
          // No, so just put everything into local variables
    					
          // Error prophylaxis (answer could be some kind of screwed)
          if (RetArr[2] != null)
          {
             UserObj.RetVal = RetArr[2];
             UserObj.RetName = RetArr[3];
             UserObj.RetLastName = RetArr[4];
             UserObj.RetStartPage = RetArr[5];
          } else UserObj.RetVal = 0;
       } else UserObj.RetVal = 0;
    
       // Put some atributes to our user object
       UserObj.ReturnValue = value;
       UserObj.RetCode = parseInt(RetArr[0]);
       UserObj.RetMsg = RetArr[1];
    				
       // Debug notification
       SiteKiosk.Logfile.Notification("ScriptDevice's UserName "
       + "(ParseReturnValue): " + UserObj.name);
    			
       // RetCode < 0 means we got an error (for details have a look
       // at the Server part of the tutorial)
       if (UserObj.RetCode < 0)
       alert(UserObj.RetMsg);
       else if (UserObj.RetVal > 0 && is_retransfer == false)
       {
          // Everything went fine, so we're logged in and ready to go
          LogInfo.innerHTML = "Successfully logged in as:<br><b>"
          + UserObj.RetName + " " + UserObj.RetLastName + "</b>";
    
          // Hide the input boxes
          trName.style.display = "none";
          trPW.style.display = "none";
          trOK.style.display = "none";
    
          // and Credit our account with the money stored on the server
          CreditDebit(UserObj.RetVal, true);
    
          // User got a start page? So why don't navigate there...
          if (UserObj.RetStartPage != "")
          var SW = SiteKiosk.WindowList.MainWindow.SiteKioskWindow;
          SW.SiteKioskWebBrowser.Navigate(UserObj.RetStartPage, true);
       }
    }
    
    // ---- Function ----
    // CreditDebit
    // ---- Parameters --
    // fValue - the string that holds the money to credit/debit
    // bCredit - Credit account?
    // ------------------
    // Just credit is used... 
    function CreditDebit(fValue, bCredit)
    {
       fValue = parseFloat(fValue);
       if (bCredit) ScriptDevice.Credit(fValue, false);
       else ScriptDevice.Debit(fValue, false);
    }
    
    // ---- Function ----
    // OnSCDPostComplete (On SiteCashDebit PostComplete)
    // ---- Parameters --
    // success - >= 0 means "OK", post was successful
    // returnvalue - the page that was actually thrown by the server
    // ------------------
    // One of our minutely posted queries got back, decrement counter
    function OnSCDPostComplete(success, returnvalue)
    {
       // Debug notification
       SiteKiosk.Logfile.Notification("Got answer from Server (SCD): "
       + returnvalue);
       // Decrement openposts, since this is the PostComplete Event
       openposts--;
    }
    
    // ---- Function ----
    // OnSiteCashDebit
    // ---- Parameters --
    // payedamount - the amount the user payed for this span of time
    // newbalance - the new balance of the account
    // ------------------
    // When the "Volatile"-Flag is used with the Script Device,
    // SiteCash wants to debit every minute (so the server needs
    // to be updated)
    function OnSiteCashDebit(payedamount, newbalance)
    {
       // Debug notification
       SiteKiosk.Logfile.Notification("ScriptDevice Debiting ("
       + payedamount + ", " + newbalance + ")!");
    			
       // Three retries (about three minutes)
       if (openposts < 3)
       {
          // ok...
          // Do we have a user?
          if (UserObj.name != null)
          {
             // ok...
             // Prepare post object to receive the updates from
             // the server
             PostObj.OnPostComplete = OnSCDPostComplete;
    					
             PostObj.AddParameter("login", "SiteKiosk");
             PostObj.AddParameter("action", "setmoney");
             PostObj.AddParameter("MoneyBack", newbalance);
             PostObj.AddParameter("login_name", UserObj.name);
             PostObj.AddParameter("login_password", UserObj.password);
    					
             // Debug notification
             SiteKiosk.Logfile.Notification("ScriptDevice Debiting "
             + "for User: " + UserObj.name);
    						
             // And away...
             PostObj.Submit(ServerPage);
    
             // increment openposts - we just posted a new one
             ++openposts;
            }
            else
            {
               // no, we don't have a user, don't do anything
               return false;
            }
          }
          else
          {
             // openposts >= 3
             // Three retries, log out - maybe server dead?
             OnREPostComplete(true, "");
             ScriptDevice.Debit(ScriptDevice.Balance, false);
    
             // Notify the user and the logfiles
             SiteKiosk.Logfile.Notification("ScriptDevice got a"
             + " server timeout! User was logged out!");
             alert("Server timed out, please call support personell");
             openposts = 0;
          }
    }
    </script>
    </head>
    <body onload="InitDialog()"
    style='background:url(../Images/hintergrund.jpg);'>
    <table style='width:100%;'>
    <tr>
    <td align=''>
    <table>
    <tr id='trName'>
    <td style='color:white;'>Name:
    <td><input type='text' name='edtLoginName'>
    <tr id='trPW'>
    <td style='color:white;'>Passwort:
    <td><input type='password' name='edtLoginPassword'>
    <tr id='trOK'>
    <td colspan='2'>
    <input type='button' value='OK' onClick='FormSubmit();'>
    <tr>
    <td style='color:white;' id='LogInfo' colspan='2'>
    </table>
    </td>
    </tr>
    </table>
    </body>
    </html>
    



    This is the php script which is used by the login dialog to handle the database containing the accounts on the server. The server must support php version 4.1.0 or higher and SQL. phpMyAdmin can be used to create or modify the database manually.

    <?php
    $sqlserver        = "localhost";
    $sqldb            = "NoCoinSys";
    $sqluser          = "SQLUSER";
    $sqlpasswd        = "SQLPASSWORD";
    	
    // Establish connection to SQL-Server
    $link = mysql_connect($sqlserver, $sqluser, $sqlpasswd);
    mysql_select_db($sqldb);
    	
    // Right Posted Form to login (no bots, etc)?
    if ($_POST["login"] == "SiteKiosk")
    {
       // Normal Name/PW Login
       $sql = "SELECT * FROM ncs_user WHERE login_id='"
       . $_POST["login_name"] . "' AND password='"
       . $_POST["login_password"] . "'";
       $accounttable = "ncs_account";
    		
       // Succesful user posted
       $res = mysql_query($sql);
       if ($res)
       {
          // Query Successful
          if (mysql_num_rows($res) == 1)
          {
             // Found User
             // Now select the money
             $userobj = mysql_fetch_object($res);
    				
             if ($_POST["action"] == "login")
             {
                $sql = "SELECT * FROM $accounttable WHERE pk='"
                . $userobj->pk . "'";
                $res = mysql_query($sql);
                if ($res)
                {
                   // Seems as though the user has still money...
                   $accountobj = mysql_fetch_object($res);
                   print "1|Authentication successful|"
                   . $accountobj->value . "|" . $userobj->name . "|"
                   . $userobj->last_name . "|". $userobj->startpage;
                }
                else print "-3|Error: No account found";
             }
             else if ($_POST["action"] == "setmoney")
             {
                // Set our new money / logout
                $sql = "UPDATE $accounttable SET value='"
                . ereg_replace(",", ".", $_POST["MoneyBack"])
                . "' WHERE pk='" . $userobj->pk . "'";
                $res = mysql_query($sql);
                if ($res) print "1|Account successfully set|"
                . ereg_replace(",", ".", $_POST["MoneyBack"]) . "";
                else
                print "-1|Error: Internal Error";
             }
          }
          else print "-2|Error: Authentication unsuccessful";
       }
    }
    mysql_close($link);
    ?>
    

Applies to
    SiteKiosk v5.0 (and later versions).

Back to top