#!/usr/bin/php -q
<? /*
    LibreSSL - CAcert web application
    Copyright (C) 2004-2009  CAcert Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
	include_once("../includes/mysql.php");
 	include_once("../includes/general.php");
 	
  //default mail text
	$lines = "";
	$fp = fopen("oa02-mailingtextPoints.txt", "r");
	while(!feof($fp))
	{
		$line = trim(fgets($fp, 4096));
		$lines .= wordwrap($line, 75, "\n")."\n";
	}
	fclose($fp);
	
  //first variant of mail text
  $lines1 = "";
	$fp = fopen("oa02-mailingtextCats.txt", "r");
	while(!feof($fp))
	{
		$line = trim(fgets($fp, 4096));
		$lines1 .= wordwrap($line, 75, "\n")."\n";
	}
	fclose($fp);
 
  //second variant of mail text
  $lines2 = "";
	$fp = fopen("oa02-mailingtextPointsCats.txt", "r");
	while(!feof($fp))
	{
		$line = trim(fgets($fp, 4096));
		$lines2 .= wordwrap($line, 75, "\n")."\n";
	}
	fclose($fp);


    // comment next line when starting to send mail not only to me 
    //	$query = "select * from `users` where `email` like 'cacerttest%'";

  $query="SELECT `users`.`email`, `users`.`fname`, `users`.`lname`, `orginfo`.`O`, `orginfo`.`id`, `org`.`memid`
    FROM `users`, `orginfo`, `org`
    WHERE `org`.`orgid` = `orginfo`.`id` and `org`.`memid`= `users`.`id` and `users`.`assurer` = 0 and `org`.`deleted`=0
    ORDER BY `users`.`email`, `orginfo`.`O`"
       
	$res = mysql_query($query);
	$xrows = mysql_num_rows($res);

  $report="";
  $report1=""
  $report2=""
  $report3=""
 
	while($row = mysql_fetch_assoc($res))
	{
    //    uncomment next line to send mails ...
    $fullname=$row['fname']." ".$row['lname'];
    $status=get_assurer_status($row['memid']);
    if ($status=3){
      $mail=str_replace("<name>",$fullname,$lines);
		  sendmail($row['email'], "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators", $mail, "support@cacert.org", "", "", "CAcert Organisation Assurer", "returns@cacert.org", 1);
      $report= $row['email'].", ".$fullname.", ".$row['O']."\n";
    }elseif ($status=5){
      $mail=str_replace("<name>",$fullname,$lines1);
		  sendmail($row['email'], "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators", $mail, "support@cacert.org", "", "", "CAcert Organisation Assurer", "returns@cacert.org", 1);
      $report1= $row['email'].", ".$fullname.", ".$row['O']."\n";
    }elseif ($status=7){
      $mail=str_replace("<name>",$fullname,$lines2);
		  sendmail($row['email'], "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators", $mail, "support@cacert.org", "", "", "CAcert Organisation Assurer", "returns@cacert.org", 1);
      $report2= $row['email'].", ".$fullname.", ".$row['O']."\n";
    }else
	    $report3= $row['email'].", ".$fullname.", ".$row['O']."\n";
    }
  }

 // 1x cc to oao.cacert.org
	sendmail("oao@cacert.org", "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators", $lines."\n\n".$lines1."\n\n".$lines2, "oao@cacert.org", "", "", "CAcert OA Support", "returns@cacert.org", 1);
	// 1x mailing report to oao.cacert.org
  sendmail("oao@cacert.org", "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators - Report", "oa-mailing sent to $xrows recipients.", "support@cacert.org", "", "", "CAcert OA Support", "returns@cacert.org", 1);	
 // 1x detailed mailing report to OAO
  $reporttotal="Result of the Mailing \n\n Organisation Administrators with missing points \n".$report."\n\n Organisation Adminstrators with missing points ".$report1."\n\n Organisation Adminstrators with missing points and CATS ".$report2."\n\n Organisation Adminstrators with blocked assurer ".$report3;
  sendmail("oao@cacert.org", "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators - Report", $reporttotal, "support@cacert.org", "", "", "CAcert Organisation Assurer", "returns@cacert.org", 1);	
	
  // 1x mailing report to Arbitrator of case http://wiki.cacert.org/wiki/Arbitrations/a20110608.1
  sendmail("bernhard@cacert.org", "[CAcert.org] Assurer Status Requirement Check on Organisation Adminstrators - Report", "oa-mailing sent to $xrows recipients.", "support@cacert.org", "", "", "CAcert OA Support", "returns@cacert.org", 1);	

	echo "oa-mailing sent to $xrows recipients.\n";

?>
