<?php
//    Kill a session
//       name=sessname


require("phpVars.php");

  $sessName='';
  $fake=0; $isfake='' ;
  $atstep='0';
  foreach ($_REQUEST as $vname=>$oof) {
     $avar=strtoupper(trim($vname)) ;
     if ($avar=='SESSION' || $avar=='NAME' ) $sessName=trim($oof) ;
     if ($avar=='FAKE' ) $isfake=trim($oof) ;
     if ($avar=='STEP' ) $atstep=trim($oof) ;
  }
if ($isfake=='1') $fake=1;

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

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

$idate =time();
header('Content-type: text/plain ');

if ($atstep=='1') {                    // step 1: delete session descriptor file

  $newfile=$sessions_dir . $sessName . '.xml' ;
  $aexists=file_exists($newfile);
  if (!$aexists) {
      print "Warning: session descriptor file, $newfile, does not exist.";
      exit ;
  }

  $astat=stat($newfile);
  $adate=date('r',$astat['mtime']);
  if ($fake==1 ) {
      $stuff='OK : session descriptor file would be deleted: <ul><li> size of '. $astat['size']. ', creation data of '. $adate .'</li></ul>'   ;
  } else {
      $stuff='OK : session descriptor file will be deleted: <ul><li> size of '. $astat['size']. ', creation data of '.$adate .'</li></ul>' ;
      $astat=unlink($newfile);
      if (!$astat) {
           print "Sorry. Unable to delete session descriptor file: $newfile ";
          exit ;
      }
   }
   print $stuff ;
   exit ;
}

if ($atstep=='2') {                    // step 2: kill files in session directory
  $newdir=$sessions_dir . $sessName . '/'  ;
  if (!file_exists($newdir)) {
      print "Sorry. No such session directory: $newdir  ";
      exit;
   }
   $stuff= killDir($newdir,$fake) ;
   print 'OK  : ' . $stuff ;
   exit ;
}


if ($atstep=='3') {                    // step 2: kill files in data directory

  $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 ;
  if (!mysql_query("DROP DATABASE " . $mdata,$link))  {
       print "Error: unable to delete mySql database: " . mysql_error();
      exit ;
   }
   mysql_close($link);
   $stuff='mySQL database dropped=' . $mdata . ' ; <br/> ' ;

// mysql database deleted, no delete (probably empty) subdirectory under data
  $dnewdir=$data_dir . $sessName . '/';
  if (!file_exists($dnewdir)) {
      print "Sorry. No such data directory: $dnewdir  ";
      exit;
   }
   $stuff= $stuff . ' ' . killDir($dnewdir,$fake) ;

   print  'OK  : ' . $stuff ;
   exit ;
}

if ($atstep=='4') {                    // step 2: kill files in pinfo directory
  $pnewdir=$pinfo_dir . $sessName . '/';
  if (!file_exists($pnewdir)) {
      print "Sorry. No such pinfo directory: $pnewdir  ";
      exit ;
  }
   $stuff= killDir($pnewdir,$fake) ;
   print  'OK  : ' . $stuff ;
  exit ;
}

print 'No such step = ' . $atstep ;
exit ;

 exit;


//==========================================
// (this is adapated from http://psoug.org/snippet/Delete_all_files_in_directory_34.htm
// kill a directory (first delete files, and subdirectories
// note: will NOT delete subdirectoreis of subdirecotires

FUNCTION killDir($path,$afake) {

  $debugStr = '';                 // init the debug string
   if ($afake==1) {
       $debugStr .= "  File/directories that WOULD BE deleted from $path = ";
   } else {
        $debugStr .= "  File/directories deleted from $path = ";
   }

  $handle = OPENDIR($path) ;
  if (!$handle) {
     $amess='Problem: ' . $debugStr . ' <br /> unable to  open ' . $path    ;
     return $amess ;
  }

// parse through all files/dirs ....
  WHILE (FALSE !== ($file = READDIR($handle))) {
     IF ($file == "." || $file == "..") continue ;              //  skip?

     IF(IS_FILE($path."/".$file)) {
             if ($afake==1) {
                 $debugStr .= $file . ' ' ;
             } else {
                 IF(UNLINK($path."/".$file)) {
                      $debugStr .= $file . ' ' ;
                 }  else {
                   $amess='Problem: ' . $debugStr . ' <br /> unable to delete ' . $file    ;
                   return $amess ;
                 }
             }

     } ELSE {                    // It's a directory...crawl through the directory and delete the contents

         $handle2 = OPENDIR($path."/".$file) ;
         if (!$handle2) {
                $amess="Problem:  $debugStr  <br /> Unable to  open  $path  \/  $file "   ;
                return $amess ;
         }
         WHILE (FALSE !== ($file2 = READDIR($handle2))) {
              IF ($file2 == "." || $file2 == "..") continue ;
              if ($afake==1) {
                               $debugStr .=  $file/$file2 . ' ' ;
               } else {
                       IF(UNLINK($path."/".$file."/".$file2)) {
                              $debugStr .=  $file/$file2 . ' ' ;
                       }  else {
                              return 'Problem: ' . $debugStr . '  <br /> Could not delete '. $path.'/'.$file.'/'.$file2 ;
                      }
               }               // afake unlink
         }                             // all $file2 in $handle2
         closedir($handle2) ;

         if ($afake==1) {
                 $debugStr .= ' [dir : '.$file. ' ]';
         } else {
                 IF(RMDIR($path."/".$file)) {
                    $debugStr .= ' [dir : ' .$file. ' ]' ;
                 }  else {
                    return 'Problem: ' . $debugStr . '  <br />Could not rmDir '. $path.'/'.$file.'/' ;
                }
         }             // remove subdirectory
      }                          // a subdir of $path

  }                       // $file in $handle

 closedir($handle) ;

 if ($afake==1) {
          $debugStr .= " :: NOT ACTUALLY removed  ".$path  ;
 } else {
       IF(RMDIR($path)) {                       // and remove the actual directory
             $debugStr .= " :: <br /> Sucesss: removed  ".$path  ;
      }  else {
          return $debugStr . ' :: <br /> Problem: could not remove session directory: ' . $path ;
      }
 }
 RETURN $debugStr;

}




?>

