<?php
// record data to server archive:
//   general, users, participants, results, history, custom (with file name),
//  or to simple history
// fields submitted:
//
// todo=431,
// if    general, users, participants, results, history
//                session=sessname&round=roundNum&round=around&type=atype&contents=EscapedXMLDataRows
//      or, if atype=CUSTOM
//                session=sessname&round=roundNum&round=around&type=atype&cname=filename&contents=EscapedXMLDataRows
//      or, if atype=SIMPLEHISTORY
//                session=sessname&round=roundNum&round=around&type=atype&cname=filename&comment=comment&contents=EscapedXMLDataRows

//  generalData  resultsData, participantsData, usersData,  historyData:
//  round int(4)  NOT NULL ,
//     PRIMARY KEY (round)  ,
//  content text ,
//  servertime int
//
//  Custom:
//  round int(4)  NOT NULL ,
//  cname varchar(60) NOT NULL ,
//     PRIMARY KEY (cname,round)  ,
//  content text ,
//  servertime int
//
// SimpleHistory:
//  arow int(4) AUTO_INCREMENT NOT NULL ,
//     PRIMARY KEY (arow)  ,
//  round int(4),
//  content text ,
//  servertime int,
//  comment varchar(120)

// Note that content is delivered escaped,  but php automatically unescapes.
// For simpleHistory this is a problem, since a "CSV with escaped values" XML field is part of the simpleHistory syntax
// Thus, for simpleHistory the raw data is used via file_get_contents('php://input');


require("phpVars.php");

// extract request fields (typically in  a POST)
  $todo='';
  $cname='' ;
  $sessName='';
  $content='';
  $atype='' ;
  $round='0';
  $comment='';
  foreach ($_REQUEST as $vname=>$oof) {
     $avar=trim(strtoupper($vname)) ;
     if ($avar=='TODO') $todo=trim($oof);
     if ($avar=='ROUND') $round=trim($oof);
     if ($avar=='TYPE') $atype=strtoupper(trim($oof));
     if ($avar=='NAME' ||$avar=='CNAME') $cname=strtoupper(trim($oof));
     if ($avar=='SESSION') $sessName=trim($oof);
     if ($avar=='COMMENT') $comment=trim($oof);
     if ($avar=='CONTENT' || $avar=='CONTENTS') $content=trim($oof);         // content will contain escaped stuff
  }


if ($sessName=='') {
  header('Content-type:text/plain');
  print '- Error. No NAME or SESSION= in call to saveAnyData.php ';
  exit ;
}
if ($todo!=='431') {
  print '- Error. todo=431 not found in call to saveAnyData.php ';
  exit ;
}

if ($atype!='HISTORY'  &&   $atype!=='PARTICIPANTS' &&  $atype!=='GENERAL' && $atype!=='USERS' && $atype!=='RESULTS'  && $atype!=='CUSTOM'  )  {
  header('Content-type:text/plain');
  print '- Error.  TYPE  must be HISTORY, SIMPLEHISTORY, PARTICIPANTS,  GENERAL, USERS ,RESULTS, or CUSTOM in getSavedAnyData ';
  exit ;
}

if ($atype=='CUSTOM' && $cname=='') {
  header('Content-type:text/plain');
  print '- Error. No CNAME in type=custom call to saveAnyData ';
  exit ;
}

if ($atype=='SIMPLEHISTORY') {                 // need to get unescaped content
  $arf = file_get_contents('php://input');
  $glob=explode('&',$arf);
  for ($i=0 ; $i<count($glob);$i++)  {
    $aa=explode('=',$glob[$i],2);
    $v1=strip(strtoupper($aa[0]));
    if ($v1='CONTENT' || $v1=='CONTENTS') $content=$aa[1] ;           // content now has the CSV-escaped stuff
  }
}

$idate =time();
$vudate=date(DATE_RFC822)  ;

// open databsae (exit on error in connectSurveyDatabase)
$link=connectDsurveyDatabase($sessName);
$mdata=$currentDatabase ;


$content=mysql_real_escape_string($content,$link);       // avoid problems with quotes

//vars: round cname content servertime (cname only with custom)

if ($atype=='GENERAL') {
  $sql="INSERT INTO generalData (round,content,servertime)
              VALUES
     ('$round','$content','$idate') ";                         // save escaped content, to avoid quote problems
  $qq=mysql_query($sql,$link);
}
if ($atype=='USERS') {
  $sql="INSERT INTO usersData (round,content,servertime)
              VALUES
     ('$round','$content','$idate') ";                         // save escaped content, to avoid quote problems
  $qq=mysql_query($sql,$link);
}
if ($atype=='RESULTS') {
  $sql="INSERT INTO resultsData (round,content,servertime)
              VALUES
     ('$round','$content','$idate') ";                         // save escaped content, to avoid quote problems
  $qq=mysql_query($sql,$link);
}

if ($atype=='PARTICIPANTS') {
  $sql="INSERT INTO participantsData (round,content,servertime)
              VALUES
     ('$round','$content','$idate') ";                         // save escaped content, to avoid quote problems
  $qq=mysql_query($sql,$link);
}
if ($atype=='HISTORY') {
  $sql="INSERT INTO historyData (round,content,servertime)
              VALUES
     ('$round','$content','$idate') ";                         // save escaped content, to avoid quote problems
  $qq=mysql_query($sql,$link);
}
if ($atype=='CUSTOM') {
  $sql="INSERT INTO customData (cname,round,content,servertime)
              VALUES
     ('$cname','$round','$content','$idate') ";                         // save escaped content, to avoid quote problems
  $qq=mysql_query($sql,$link);
  if (!$qq) {
     mysql_close($link);
     header('Content-type:text/plain');
     print '- Error. Unable to add entry  to '. $atype.' table in mysql database ' . $mdata . ': '  . mysql_error();
     print " Params: cname= $cname , round=$round,  servertime=$idate, content=$content ";
     exit ;
}

}

$comment=mysql_real_escape_string($comment,$link);       // avoid problems with quotes

if ($atype=='SIMPLEHISTORY') {
  $sql="INSERT INTO simpleHistory (content,comment,round,servertime)
              VALUES
     ('$content','$comment','$round','$idate') ";
  $qq=mysql_query($sql,$link);
  if (!$qq) {
     mysql_close($link);
    return '-Error. Unable to add entry  to SIMPLEHISTORY table in mysql database ' . $mdata . ': '  . mysql_error();
  }
}

if (!$qq) {
     mysql_close($link);
     header('Content-type:text/plain');
     print '- Error. Unable to add entry  to '. $atype.' table in mysql database ' . $mdata . ': '  . mysql_error();
     exit ;
}

mysql_close($link);

header('Content-type:text/plain');
$slen=strlen($content);
print "Ok  $atype archive was updated with $slen bytes " ;
exit ;
