<?php
exit(0);  // Obsolete, 2022-12-03 by Chiachi
/*
  This version was based on original srilm.tgz shell script.

  Download licensed software
             or
  Sign up SRILM user mailing list

  Translated from shell script by Chiachi Hung, November 2014
*/

// GLOBAL DEFINITION
$product_name = "srilm.tgz";
$product_location = "/var/release";
$webrootdir = "/var/www/websites/speech";
$logfile = "$webrootdir/license/$product_name.log";
$maillist_announce = "$webrootdir/license/$product_name.emails";
$maillist_users = "$webrootdir/license/$product_name.users.emails";
$datadir = "/var/www/websites/srilm_release";

function strip_html_in_array($arr) {
  $new_array = array();
  foreach ($arr as $key => $value) {
    $new_array["$key"] = strip_tags($value); 
  }

  return $new_array;
}

function reclog($logfile) {
  global $newpost;

  date_default_timezone_set('America/Los_Angeles');
  $fecha = date(DATE_RFC2822);
  $remote_addr = $_SERVER['REMOTE_ADDR'];
// REMOTE_HOST is undefined index in current Apache2 server
//  $remote_host = $_SERVER['REMOTE_HOST'] ?: gethostbyaddr($remote_addr);
  $remote_host = gethostbyaddr($remote_addr);

  $fh = fopen("$logfile", 'a+');
  fwrite($fh, "$fecha\n");
  fwrite($fh, "From_Addr=$remote_addr\n");
  fwrite($fh, "From_Host=$remote_host\n");
  fwrite($fh, "Name=" . $newpost['WWW_name'] . "\n");
  fwrite($fh, "Org=" . $newpost['WWW_org'] . "\n");
  fwrite($fh, "Address=" . $newpost['WWW_address'] . "\n");
  fwrite($fh, "Email=" . $newpost['WWW_email'] . "\n");
  fwrite($fh, "URL=" . $newpost['WWW_url'] . "\n");
  fwrite($fh, "File=" . $newpost['WWW_file'] . "\n");
  if (!isset($newpost['WWW_list'])) $newpost['WWW_list'] = "";
  fwrite($fh, "List=" . $newpost['WWW_list'] . "\n\n");
  fclose($fh);
}

function recemail($maillist) {
  global $newpost;

  $email = preg_replace('/\s+/', ' ', $newpost['WWW_email']);
  $fh = fopen("$maillist", 'a+');
  fwrite($fh, $newpost['WWW_name'] . " <$email>\n");
  fclose($fh);
}

function download($file) {
  if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/gzip');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
  } else {
    header("Content-type: text/plain\n");
    header("Status: 404 Not Found\n");
print "$file not found!\n";
  }
}

/**** MAIN ****/
// clean input values
$newpost = strip_html_in_array($_POST);

// check for proper form entry
if (empty($newpost['WWW_name']) || empty($newpost['WWW_email'])) {
  if (!empty($newpost['WWW_signup'])) { // for sign-up
     print "Your Name or Email are missing.<p>";
     print "Please go back and complete the form.<p>";
     exit(0);
  } else if (empty($newpost['WWW_address'])) {  // for download
     print "Your Name, Address or Email are missing.<p>";
     print "Please go back and complete the form.<p>";
     exit(0);
  }
}

/*  DEBUGGING
print "Send result:<p>";
print "<pre>";
print_r($_POST);
print_r($newpost);
print "</pre>";
exit (0);
*/

if (!isset($newpost['WWW_list'])) {
   recemail($maillist_announce);
} else if (isset($newpost['WWW_signup'])) {
   recemail($maillist_users);
}

if (isset($newpost['WWW_signup'])) {  
    header('Content-Description: Display signup successfully done');
    header('Content-Type: text/html');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    print "<html><header>";
    print "<META http-equiv=\"refresh\" content=\"0;URL=/projects/srilm/\">";
    print "</header>";
    print "<script>";
    print "alert(\"You have sign up to SRILM user mailing list successfully!\");";
    print "</script>";
    print "</html>";
    exit(0);
} else {
   // not signup so it's download
   reclog($logfile);
   download("$datadir/" . $newpost['WWW_file']);
}
?>
