<?php
//    read a server status variable (todo =122).
//       session=sessname&varname=aVarnames&delim=adelim&default=defValue
//   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
//       delim: optional. Only used if several variables. Adelim will be used by server to seperate values
//       default: optional. If no varname exits, return defValue
//       spy: optional. Suppress recording of data
//  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=''   ;
  $adelim=' ' ;
  $adefault='';
  $anid='';
  $amessage='';
  $atime='';
  $spying='0';
  $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=='DELIM') $adelim=trim($oof) ;
     if ($avar=='DEFAULT') $adefault=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=='SPY') $spying=trim($oof) ;
  }

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

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

if ($varname=='') {
  print '- ERROR: No VARNAME=  in call to   readServerVar.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 && $bvars[0]!='*') {     // just one variable: so perhaps save request info to clientstatus

 $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!='' && $spying!='1') {
// use REPLACE (since clientid is a primary key)
    $cval4=preg_replace("/\'/",'`',$cval3);
    $sql="REPLACE INTO clientstatus (clientid,servertime,clienttime,state,message,varname,varvalue)
              VALUES
     ('$anid','$idate','$atime','$astate','$amessage','$cvar','$cval4') ";
    $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 ;

}              // one variable

// else, multiple variables (return, but no status recording

$stuff='';
$i1=0;

// special case: varname=*
if ($bvars[0]=='*') {
  $sql="SELECT * FROM servervars " ;
  $qq=mysql_query($sql,$link);
  if (!$qq) {
     print '- Error. Unable to read server variables in STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
     mysql_close($link);
     return 0;
  }
  while($row = mysql_fetch_array($qq)) {
       $cvar=$row['varname']; $cval=$row['varvalue'];
       if ($cvar=='_created') continue ;
       if ($i1==0) {
            $stuff=$cvar . '=' . $cval;
            $i1++ ;
       } else {
          $stuff=$stuff . $adelim . $cvar . '=' . $cval;
       }
  }
  mysql_close($link);
  print $stuff ;
  exit;

}              // = *


for ($ith=0; $ith<count($bvars); $ith++) {

  $cvar=$bvars[$ith];
  $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) {
     $cval=$adefault;
  } else {
      $cval=$row['varvalue'] ;
  }
  if ($i1==0) {
    $stuff=$cval;
    $i1++ ;
  } else {
    $stuff=$stuff . $adelim . $cval ;
  }
}

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


?>

