#include <lt2001.style>

<page title="Infomail" subtitle=Infomail>

<font face="Helvetica,Arial">
<?

  function get_email ($project)
  {
    global $dbh;

    $query = sprintf ("SELECT email FROM person WHERE project = '%s' AND ( list = 1 OR infomail = 1 ) AND email <> ''", $project);
    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

    if (pg_NumRows ($sth) > 0) {
      for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
	$row = pg_fetch_array ($sth, $nr);
	if (strlen ($addr) > 0) {
	  $addr .= "," . $row['email'];
	} else {
	  $addr = $row['email'];
	}
      }
    }
    return $addr;
  }

  function php_unscrew ($text)
  {
    # Der Himmel ist blau, das Wasser ist klar, die Vögel zwitschern
    # draußen, doch PHP weigert sich beharrlich, vernünftig zu
    # arbeiten.
    #
    # Problem: Im Form kommt " an, PHP macht daraus freundlicherweise \"
    #          und schon sind unsere Daten in der SQL-DB kaputt.  Danke
    # $mytext = ereg_replace ("\\\\", "\\\\", $text);
    $mytext = ereg_replace ("\\\\", "", $text);
    return ($mytext);
  }

  if ((strlen ($subject) > 0) && (strlen ($body) > 0) && (strlen ($number) > 0)) {
    $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
		 or die("Unable to connect to SQL server");
    $body = php_unscrew ($body);

    # pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");

    $query = "SELECT name FROM project ORDER by name";
    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

    if (pg_NumRows ($sth) > 0) {
      for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
	 $row = pg_fetch_array ($sth, $nr);

	 $msubject = sprintf ("[LT Infomail#%d] %s - %s", $number, $subject, $row['name']);
	 $header = "From: Martin Schulze <joey@linuxtag.org>";
	 $header .= "\nBcc: lt2k-email@linuxtag.de,joey@infodrom.north.de";
	 $header .= "\nContent-Type: text/plain; charset=iso-8859-1";
	 $header .= "\nContent-Disposition: inline";
	 $header .= "\nContent-Transfer-Encoding: 8bit";

	 $email = get_email ($row['name']);

	 $url = sprintf ("http://www.infodrom.ffis.de/Debian/events/LinuxTag2001/infomail-%d.html", $number);
	 # mail ("joey", $msubject, $email . "\n\n" . $url . "\n\n" . $body, $header);
	 # mail ($email, $msubject, $url . "\n\n" . $body, $header);
      }

      # Copy for mailing list
      $msubject = sprintf ("[LT Infomail#%d] %s", $number, $subject);
      $header = "From: Martin Schulze <joey@linuxtag.org>";
      $header .= "\nBcc: joey@infodrom.north.de";
      $header .= "\nContent-Type: text/plain; charset=iso-8859-1";
      $header .= "\nContent-Disposition: inline";
      $header .= "\nContent-Transfer-Encoding: 8bit";

      # mail ("linuxtag-projects@infodrom.ffis.de", $msubject, $url . "\n\n" . $body, $header);
      # mail ("joey@infodrom.ffis.de", $msubject, $url . "\n\n" . $body, $header);
      # echo ("<p><b>Mails sent.</b><p>");
      echo ("<p><b>Mails not sent, because LinuxTag 2001 is over.</b><p>");
    }
  } else {
    echo ("<p><b>Empty fields, bugger.</b><p>");
  }
?>

</font>
</page>

# Local variables:
# mode: text
# mode: auto-fill
# end:
