<?php
// Join a session


require("phpVars.php");

//header("Content-Type: text/plain");
$idate =time();

  $sessName='';
  $username='';
  $content='';
  foreach ($_REQUEST as $vname=>$oof) {
     $avar=trim(strtoupper($vname)) ;
     $toof=trim($oof);
     if ($avar=='NAME' || $avar=='SESSION')  {
        $sessName=$toof;
     }
     if ($avar=='USERNAME' || $avar=='CLIENTNAME') {
         $username=$toof;
     }
     if ($avar=='CONTENT') {
         $content=$oof;
     }
  }      // foreach

if ($sessName=='') {
   print '- Error. No session= in call to joinSession.php ';
   exit ;
}
if ($username=='') {
   print '- Error. No username= in call to joinSession.php ';
   exit ;
}

// does this session exist?
$dafile=$sessions_dir . $sessName . '.XML' ;
$isfile=file_exists($dafile);
if (!$isfile) {
  print '- Error. No such session ' . $sessName . ' (joinSession.php) ';
  exit ;
}

$content2=preg_replace("/\'/",'`',$content);       // avoid problems below

// break username (name:id=refid) into its components
$a1=explode(':',$username);

if (count($a1)<2) {
   print '- Error. bad structure of user info (should be name:id=refid, is ' . $username . ')' ;
   exit;
}
$refId='';
$clientName=$a1[0];
$a2=explode('=',$a1[1]);
if (count($a2)<2) {
  $clientId=trim($a1[1]);
} else {
  $clientId=$a2[0];
  $refId=$a2[1];
}

// === save to mysql databases
$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();
   mysql_close($link);
   return 0;
}

// add this entry to clientid table

$sql="INSERT INTO clientids (servertime,clientName,clientId,refId,content)
              VALUES
     ('$idate','$clientName','$clientId','$refId','$content2') ";

$qq=mysql_query($sql,$link);
if (!$qq) {
   print 'Error. Unable to add entry to CLIENTIDS  table in mysql database ' . $mdata . ': '  . mysql_error();
   return 0;
}


// append this user to list of users

 $sql="SELECT * FROM statusvars WHERE varname='users'" ;

 $qq=mysql_query($sql,$link);
 if (!$qq) {
     print '- Error. Unable to read users variable from STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
     mysql_close($link);
     return 0;
 }

 // read current value of users
 $row = mysql_fetch_array($qq);        // just use first row (should only be one row)
 if (!$row) {
     print '- Error. Missing users variable in STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
     mysql_close($link);
     return 0;
 } else {
   $cval=$row['varvalue'] ;
 }

 $cval2=$cval . ' ' .  $username . ' ' ;

// append to users row (by replacing prior contents with updated contents)
 $sql="REPLACE INTO statusvars (varname,varvalue,modified)
              VALUES
     ('users' ,'$cval2','$idate') ";
 $qq=mysql_query($sql,$link);
 if (!$qq) {
   print '-Error. Unable to add user to  users variable in STATUSVARS  table in mysql database ' . $mdata . ': '  . mysql_error();
   return 0;
 }


print "ok  $username " ;
exit ;


/*===
  print count($otherVars) . " \n ";
$stuff="Session= $sessName \n Namecheck=$nameCheck \n clientid = $clientid \n Others  " . count($otherVars);
for ($ith=0;$ith<count($otherVars);$ith++) {
  $goose=$otherVars[$ith];
  $aline=" $goose[0]  = $goose[1]  \n " ;
  $stuff .= $aline ;
}
===*/

?>