<?php
// Retrieve and set smtp values (save to dsurvey_email mysql file, which is NOT session specific)
// For later implementation: session (or instrument) specific smtp parameters

require("phpVars.php");

$todo=extractRequestvar('TODO','','No TODO in call to doSmtpVals.php');
$sessName=extractRequestvar('SESSNAME','1','');
$aread=extractRequestvar('READ','0','');

// === connect to mysql databases
if ($aread=='1') {
  $amess=connectEmailDatabase($sessName);
  header("Content-Type: text/plain ");
  print $amess ;
  exit;
}
// if here, submitting values
$link=mysql_connect('localhost','dsurveyAdmin','admin');
if (!$link) {
   header("Content-Type: text/plain");
   $amess0=' - Error. Could not connect to mysql (with username=`dsurveyAdmin` and password=`admin`): ' . mysql_error();
   $amess0=rawurlencode($amess0);
   $amess='$message='.$amess0;
   return $amess ;
}
$mdata=$hdrMysql . 'SMTPS' ;
$qq=mysql_select_db($mdata, $link);

//  no SMTPS database
if (!$qq) {                                // no SMTP database -- create it.
   header("Content-Type: text/plain");
   $amess0='No SMTPS mysql database. Try reloading the set dSurveys SMTP parameters page';
   $amess0=rawurlencode($amess0);
   $amess='$message='.$amess0;
   return $amess ;
}

// add SMTP data

//document.forms['aform1'].smtpGateway.value=smtpGateway ;
//document.forms['aform1'].defaultFrom.value=defaultFrom ;
//document.forms['aform1'].smtpName.value=smtpName;
//document.forms['aform1'].smtpPwd.value=smtpPwd ;

$smtpGateway=extractRequestvar('SMTPGATEWAY','','');
$smtpPort=extractRequestvar('SMTPPORT','','');

$username=extractRequestvar('SMTPNAME','','');
$password=extractRequestvar('SMTPPWD','','');
$defaultFrom=extractRequestvar('DEFAULTFROM','','');

$sql="REPLACE INTO basic (sessName,smtpGateway,smtpPort,username,password,defaultFrom)
              VALUES
     ('1' ,'$smtpGateway','$smtpPort','$username','$password','$defaultFrom') ";


 $qq=mysql_query($sql,$link);
 if (!$qq) {
    header("Content-Type: text/plain");
   $amess0='Unable to add info to SMTP basic table in mysql database ' . $mdata . ': '  . mysql_error();
   $amess0=rawurlencode($amess0);
   $amess='$message='.$amess0;
   return $amess ;
 }

 $vudate=date(DATE_RFC822)  ;

  header("Content-Type: text/html");
 $amess='<html><head><title>SMTP database updated</title></head> <body>';
 $amess=$amess. '<a href="/dsurvey/admin.htm">Return to admin page </a><p/>';
 $amess=$amess . " The SMTP database ($mdata) was updated (current time: $vudate) <p/>"  ;

// warnings?
$i1=strpos($smtpGateway,'.') ;
if (!$i1) $amess=$amess. '<b>Warning:</b> No . in smtpGateway: '.$smtpGateway.'<br/>';

if ($smtpPort < 1) $amess=$amess. '<b>Warning:</b> No smtpPort <br/>';


if (trim($defaultFrom)=='') {
  $amess=$amess. '<b>Note:</b> You did not specify a defaultFrom <br/>';
} else {
  $i1=strpos($defaultFrom,'@') ;
  if (!$i1) $amess=$amess. '<b>Warning:</b> No @. in defaultFrom: '.$defaultFrom.'<br/>';
}

if (trim($username)=='') {
  $amess=$amess. '<b>Note:</b> You did not specify a username, so SMTP plain authentication will not be used <br/>';
}
if (trim($password)=='') {
  $amess=$amess. '<b>Note:</b> You did not specify a password, so SMTP plain authentication will not be used <br/>';
}

 $amess=$amess . "</body></html>";
 print $amess ;

exit ;


//===-   mysql
// conect to a dsurvey session mysql database
function connectEmailDatabase($aname) {

global $svydir,$rootdir,$dsvydir,$wwwdir,$instruments_dir,$sessions_dir,$data_dir,$lookup_dir,$myScriptName;
global $pinfo_dir,$debug,$hdrMysql,$mysqlHdr,$currentDatabase,$tmpdir,$backup_dir;


$link=mysql_connect('localhost','dsurveyAdmin','admin');
if (!$link) {
   header("Content-Type: text/plain");
   $amess0=' - Error. Could not connect to mysql (with username=`dsurveyAdmin` and password=`admin`): ' . mysql_error();
   $amess0=rawurlencode($amess0);
   $amess='$message='.$amess0;
   return $amess ;
}

$mdata=$hdrMysql . 'SMTPS' ;
$qq=mysql_select_db($mdata, $link);
//  no such database, then create

if (!$qq) {                                // no SMTP database -- create it.

  if (!mysql_query("CREATE DATABASE " . $mdata,$link))  {
     $amess0="Error creating mysql SMTP parameters database: " . mysql_error();
    $amess0=rawurlencode($amess0);
     $amess='$message='.$amess0;
     return $amess ;
  }

// Create default table
  $qq=mysql_select_db($mdata, $link);
  if (!$qq) {
     $amess0='Error. Unable to select mysql SMTP database ' . $mdata . ': '  . mysql_error();
     $amess0=rawurlencode($amess0);
     $amess='$message='.$amess0;
     return $amess ;
  }

 $sql = "create table basic
 (
    sessName varchar(140) not null,
       PRIMARY KEY (sessName),
    smtpGateway varchar(140) ,
    smtpPort int ,
    username varchar(100) ,
    password varchar(100) ,
    defaultFrom varchar(140)
  )";
 $qq=mysql_query($sql,$link);
 if (!$qq) {
   $amess0='Error. Unable to create BASIC table in mysql SMTP database ' . $mdata . ': '  . mysql_error();
   $amess0=rawurlencode($amess0);
   $amess='$message='.$amess0;
   return $amess ;
 }

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

// add SMTP data
$sql="INSERT INTO basic (sessName,smtpGateway,smtpPort,username,password,defaultFrom)
              VALUES
     ('_created' ,'$vudate','0','nameHere','passwordHere','fromHere') ";

 $qq=mysql_query($sql,$link);
 if (!$qq) {
   $amess0='Error. Unable to add info to SMTP table in mysql database ' . $mdata . ': '  . mysql_error();
   $amess0=rawurlencode($amess0);
   $amess='$message='.$amess0;
   return $amess ;
 }


 $amess0='The SMTP database ('.$mdata.') was created (so no pre-existing values are available)' ;
 $amess0=rawurlencode($amess0);
 $amess='$message='.$amess0;
 return $amess ;

}

/* $amess0='SMTP Database exists '  ;
$amess0=rawurlencode($amess0);
$amess='$message='.$amess0;
return $amess ;
*/

// look for '1' entry (default)
$sql="SELECT * FROM basic where sessName='1'" ;
    $qq=mysql_query($sql,$link);
    if (!$qq) {
       mysql_close($link);
       header('Content-type:text/plain');
       $amess0='Unable to read BASIC table in SMTP database;   ' . $mdata . ': '  . mysql_error();
       $amess0=rawurlencode($amess0);
       $amess='$message='.$amess0;
      return $amess ;
    }

    $rowcount=mysql_num_rows($qq);
    if ($rowcount==0) {
       $amess0='Default SMTP information has not been created (or was removed); so no pre-existing values are available ';
       $amess0=rawurlencode($amess0);
       $amess='$message='.$amess0;
       return $amess ;
    }
//if (aword=='GATEWAY') smtpGateway=strip(vv2[1]);
//if (aword=='PORT') smtpPort=strip(vv2[1]);
//  if (aword=='DEFAULT_FROM') defaultFrom=strip(vv2[1]);
//  if (aword=='USER') smtpName=strip(vv2[1]);
//  if (aword=='PWD') smtpPwd=strip(vv2[1]);
//  if (aword=='FILE') smtpFile=strip(vv2[1]);
//  if (aword=='$MESSAGE') amessage=strip(unescape(vv2[1]));

    $stuff='';
    while($row = mysql_fetch_array($qq)) {
       $a1=($row['smtpGateway']);
       $stuff=$stuff . "GATEWAY=$a1 ";
       $a1=($row['smtpPort']);
       $stuff=$stuff . ", PORT=$a1 ";
       $a1=($row['username']);
       $stuff=$stuff . ", USER=$a1 ";
       $a1=($row['password']);
       $stuff=$stuff . ", PWD=$a1 ";
       $a1=($row['defaultFrom']);
       $stuff=$stuff . ", DEFAULT_FROM=$a1 ";
       $stuff=$stuff . ", FILE=$mdata ";
   }
   mysql_close($link);
   return $stuff ;

}

// for later implementation: check for session specific smtp parameters

?>