<?php
//    read a server status variable (todo =122). CLIENT side version
//       session=sessname&varname=aVarnames
//   where
//        sessName: the survey session
//        varname: variable to be retrieved. Or, a space delimited (escaped) list of variables.
//                 If varname='*', return a delim delimited list of all the server variables defined for this session, using a
//                        varname=value delim varname=value ...
//                 syntax
//  Optional: id and message variables
//          id=participantId
//          message or summary=escaped_message
//          state= status of participant
//          time=aTime
//   This participant specific (based on participantID) message will be saved, for use in status display.
//   In addition, the current time  (highly recommended: use getTime()) will be stored.

require("phpVars.php");

  $sessName='';
  $varname=''   ;
  $anid='';
  $amessage='';
  $atime='';
  $adefault='';
  $astate='';
  foreach ($_REQUEST as $vname=>$oof) {
     $avar=strtoupper(trim($vname)) ;
     if ($avar=='SESSION' || $avar=='NAME') $sessName=trim($oof) ;
     if ($avar=='VARNAME' || $avar=='VAR') $varname=trim($oof) ;
     if ($avar=='ID') $anid=trim($oof) ;
     if ($avar=='MESSAGE' || $avar=='SUMMARY') $amessage=trim($oof) ;
     if ($avar=='STATE') $astate=trim($oof) ;
     if ($avar=='TIME') $atime=trim($oof) ;
     if ($avar=='DEFAULT') $adefault=trim($oof) ;

     if ($avar=='DELIM' || $avar=='SPY') {
         print 'Error in client-side readServerVar: unsupported option= ' . $avar;
         exit ;
     }

  }

header("Content-Type: text/plain");

if ($sessName=='') {
  print '- ERROR: No SESSION=  in call to   readServerVarC.php ';
  exit ;
}

if ($varname=='') {
  print '- ERROR: No VARNAME=  in call to   readServerVarC.php ';
  exit ;
}


// add variables to STATUSVARS table

$idate =time();

 // === connect to mysql databases
$link=connectDsurveyDatabase($sessName);
$mdata=$currentDatabase ;

$bvars=getWords($varname);
//$avars=trim($varname);
//$avars = preg_replace("/\s+/"," ", $avars) ;             // collapse spaces
//$bvars=explode(' ',$avars) ;

if (count($bvars)!=1) {
    print '- ERROR: No multiple variables in call to   readServerVarC.php:  ' . $varname ;
    exit ;
}

$cvar=$bvars[0];
$sql="SELECT * FROM servervars WHERE varname='$cvar'" ;

 $qq=mysql_query($sql,$link);
 if (!$qq) {
     print '- Error. Unable to read server variable ' .$cvar . ' in STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
      mysql_close($link);
     return 0;
 }
 $row = mysql_fetch_array($qq);        // just use first row (should only be one row)
 if (!$row) {
     $cval3= $adefault;
 } else {
     $cval3=$row['varvalue'] ;
 }

// update client status?

 if ($anid!='' ) {
// use REPLACE (since clientid is a primary key)
//    $cval4=preg_replace("/\'/",'`',$cval3);
    $cval4e=mysql_real_escape_string($cval3,$link);

    $sql="REPLACE INTO clientstatus (clientid,servertime,clienttime,state,message,varname,varvalue)
              VALUES
     ('$anid','$idate','$atime','$astate','$amessage','$cvar','$cval4e') ";
    $qq=mysql_query($sql,$link);
    if (!$qq) {
       print 'Error. Unable to add info to CLIENTSTATUS  table in mysql database ' . $mdata . ': '  . mysql_error();
      return 0;
    }
 }                   // record client status

// variables in clientstatus
//  clientid varchar(40) NOT NULL ,
//        PRIMARY KEY(clientid),
//  servertime int,
//  clienttime varchar(40) ,
//  state varchar(40),
//  message tinytext,
//  varname varchar(40),
//  varvalue tinytext


 mysql_close($link);
 print $cval3 ;
 exit ;



?>

