<?php

/**
 * CONFIG
 */
$subject_prefix = "DIE Newsletter";
$sender = "DIE <info@executionroom.com>";
$replyto = $sender;
$footer = "

Stay Brutal!
// DIE
http://www.executionroom.com
info@executionroom.com
";

class Email {
	public $timestamp;
	public $email;

	public function Email($email, $timestamp)
	{
		$this->email = $email;
		$this->timestamp = $timestamp;
	}

	function send($subject, $message) {
		global $subject_prefix;
		global $sender;
		global $replyto;
		global $footer;

		$message .= $footer;
		$message .= "\nTo stop receiving this newsletter, click the following link: http://www.executionroom.com/?page=news&action=unsubscribe&email=". $this->email . "\n";
		$headers = "From: " . $sender . "\r\n";
		$headers .= "Reply-To: " . $replyto . "\r\n";
		$headers .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
		$headers .= "X-Mailer: PHP/" . phpversion();
		$subject = "[".$subject_prefix."] " . $subject;
		
		$ret = mail($this->email, $subject, $message, $headers);
		if(!$ret) echo "Fail(".$this->email.")";
 
		//usleep(100000);
	}
}

class Mailinglist {
	private $file;
	private $mailinglist = array();

	public function add($email) {
		$key = $email->email;
		if(array_key_exists($key, $this->mailinglist)) return false;
		$this->mailinglist[$key] = $email;
		return true;
	}
	
	public function remove($email) {
		if(array_key_exists($email, $this->mailinglist)) {
			unset($this->mailinglist[$email]);
			return true;
		}
		return false;
	}

	public function write()
	{
		$fp = fopen($this->file, "w");
		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<mailinglist>\n");
		foreach($this->mailinglist as $email) {
			fwrite($fp, "  <email email=\"" .
						 htmlspecialchars($email->email, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "         timestamp=\"" . $email->timestamp . "\">\n");
			fwrite($fp, "  </email>\n");
		}
		fwrite($fp, "</mailinglist>\n");

		fclose($fp);
	}
	
	private function read()
	{
		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);
		$params = $dom->getElementsByTagName('email');

		foreach ($params as $param) {
			$email = new Email($param->getAttribute('email'),
												 $param->getAttribute('timestamp'));
			$this->add($email);
		}
	}

	public function subscribe($emailaddr)
	{
		$str = "";
		if($emailaddr == "your@email.here") return "";
		$email = new EMail($emailaddr, time());
		if($this->add($email)) {
			$this->write();
			$str .= "<div class=\"newsletter_message\">The email: " . $emailaddr . " has now been subscribed.</div>";
		} else {
			$str .= "<div class=\"newsletter_message\">The email: " . $emailaddr . " is already subscribed.</div>";
		}
		return $str;
	}

	public function unsubscribe($emailaddr)
	{
		$str = "";
		if($this->remove($emailaddr)) {
			$this->write();
			$str .= "<div class=\"newsletter_message\">The email: " . $emailaddr . " has now been unsubscribed.</div>";
		} else {
			$str .= "<div class=\"newsletter_message\">The email: " . $emailaddr . " is not subscribed.</div>";
		}
		return $str;
	}

	public function post($subject, $message)
	{
		$sz = sizeof($this->mailinglist);

		echo "<div style=\"text-align: center; padding-top: 120px; padding-bottom: 100px; position: absolute; top: 25%; left: 0px; width: 99.4%; height: 150px; border: solid #0000ff 3px; background: #fff; color: #000;\">Sending ". $sz ." mails <br/>\n&lt;";
		ob_flush();
		flush();

		$num = 0;
		$lvl = 0;
		$steps = floor($sz / 10) + 1;

		foreach($this->mailinglist as $email) {
			$email->send($subject, $message);
			$pct = $num / $sz * 100;
			if($pct >= $lvl) {
				printf("<font style=\"font-size: 9px;\">%.0f%%</font>", $lvl);
				$lvl += 100/$steps;
			} else {
				echo ".";
			}
			ob_flush();
			flush();
			$num++;
		}
		echo "<font style=\"font-size: 9px;\">[100%]</font>&gt;\n<br/>done<br/>\n";
		echo "<a style=\"font-size: 20px; font-weight: bold;\" href=\"?page=admin&amp;module=newsletter\">[CLOSE]</a>";
		echo "</div>\n";
		ob_flush();
		flush();
	}

	public function Mailinglist($file)
	{
		$this->file =  $file;
		if(file_exists($file)) $this->read();
	}
}

class Newsletter {

  private $file;
  private $mailinglist;

  // Admin config
  public $admin_title = "NewsLetter";
  public $admin_submodules = array("Send newsletter" => "create");

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "create":
			//			$this->admin_add($action, $vars);
      break;
    }
  }

  public function run($params)
  {
		global $page;
		global $newsletter_action;
		global $_POST, $GLOBALS;
		$email = $_POST["newsletter_email"]; // get from lineedit.
		if($email == "") $email = $GLOBALS["newsletter_email"]; // get from link.

		$str = "";

		$str .= "<div class=\"newsletter\">\n";
		$str .= "  <form class=\"newsletter_form\" " .
			"action=\"?page=".$page."&amp;newsletter_action=subscribe\" " .
			"method=\"post\">\n";
		$str .= "    <div>\n";
		$str .= "      <input name=\"newsletter_email\" value=\"your@email.here\"/>\n";
		$str .= "      <button type=\"submit\">Subscribe</button>\n";
		$str .= "    </div>\n";
		$str .= "  </form>\n";
		$str .= "</div>\n";

		if($newsletter_action == "subscribe") {
			$str .= $this->mailinglist->subscribe($email);
		}

		if($newsletter_action == "unsubscribe") {
			$str .= $this->mailinglist->unsubscribe($email);
		}

		return $str;
  }

  private function read()
  {
		$this->mailinglist = new Mailinglist($this->file);
  }

  public function Newsletter($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }
}


function newsletter_init()
{
  global $DATA_DIR;
  return new Newsletter($DATA_DIR . "/newsletter.xml");
}

?>