<?php
//    save a server status variable (todo =131). See saveServerVar for a more complex version of this task
// Fields in the request are:
//        session: the survey session
//        varname: variable to be saved
//        value : value of the variable
require("phpVars.php");

  $sessName='';
  $varname=''   ;
  $aValue='' ;

  foreach ($_REQUEST as $vname=>$oof) {
     $avar=strtoupper(trim($vname)) ;
     if ($avar=='SESSION') $sessName=trim($oof) ;
     if ($avar=='VARNAME') $varname=trim($oof) ;
     if ($avar=='VALUE') $aValue=trim($oof) ;
  }

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

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

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

$cvar=strtoupper(trim($varname));
$cval=trim($aValue);

// add variables to STATUSVARS table

$idate =time();

$link=mysql_connect('localhost','dsurveyAdmin','admin');
if (!$link) {
   print ' - Error. Could not connect to mysql (with username=`dsurveyAdmin` and password=`admin`): ' . mysql_error();
   exit ;
}

$mdata=$hdrMysql . $sessName ;
$qq=mysql_select_db($mdata, $link);
if (!$qq) {
   print '- Error. Unable to select mysql database ' . $mdata . ': '  . mysql_error();
   return 0;
}

$iadded=0;

// use REPLACE (since varname is a primary key)
// $cval2=preg_replace("/\'/",'`',$cval); (hack replaced by below)
 $cvalE=mysql_real_escape_string($cval);
 $sql="REPLACE INTO statusvars (varname,varvalue,modified)
              VALUES
     ('$cvar' ,'$cvalE','$idate') ";

 $qq=mysql_query($sql,$link);
 if (!$qq) {
     print '- Error. Unable to update/add variable ' .$cvar . ' to STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
     return 0;
 }
 $iadded ++ ;


mysql_close($link);
print 'Ok. Variables added or updated= ' . $iadded ;
exit ;


?>

