| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 | <?phprequire_once(dirname(__FILE__).'/../models/Logfile.php');require_once(dirname(__FILE__).'/../models/MailConfig.php');require_once(dirname(__FILE__).'/../vendor/PHPMailer/class.phpmailer.php');require_once(dirname(__FILE__).'/../vendor/PHPMailer/class.smtp.php');// require_once(dirname(__FILE__).'/TemplateController.php');class MailController{	private $config;	/** @var MailConfig */    private $smtpConfDefault;	/** @var MailConfig */	private $smtpConfXml;	/** @var string */	private $mailError;    function __construct($config = array())    {		$this->config = $config;		$this->smtpConfDefault = new MailConfig();        $this->smtpConfXml = (isset($config['SMTP_HOST']) && $config['SMTP_HOST'] != "")        		? $this->getSmtpConfFromIni($config)        		: $this->smtpConfDefault;    }    public function checkDirAndSendMail($logDir, $customer)    {        $logs = $this->checkProtocolDirectory($logDir);        if (count($logs) > 0) {            $this->sendErrorMail($logs, $customer);        }    }        function checkProtocolDirectory($path)    {        $result = array();        $logDir = dir($path);        while ($filename = $logDir->read()) {            if (!preg_match('/\.log$/i', $filename))                continue;            $log = new Logfile("{$path}\\{$filename}");            if ($log->ErrorLevel < 3 || $log->LastChangedDays >= 4) {                $result[] = $log;            }        }        return $result;    }	function checkProtocolFile ($protocolFile)	{		$log = new Logfile($protocolFile);		if ($log->ErrorLevel < 3) {			$result = array("");			foreach ($log->Errors as $error) {				$result[] = "{$error->Number}: ({$error->Level}) {$error->Message}";			}			return implode("\r\n", $result);		}		return "";	}    public function checkStarterLogsAndSendStatusMail($logDir, $mailTo)    {        $logs = $this->checkStarterLogs($logDir);        if (count($logs) > 0) {            $this->sendStatusMail($logs, $mailTo);        }    }        function checkStarterLogs($path)    {        $today = date("d.m.Y");        $result = array();            $protDir = dir($path);        while ($filename = $protDir->read()) {            if (!preg_match('/\.csv\.log$/i', $filename))                continue;                foreach (file("{$path}\\{$filename}") as $line) {                if (strpos($line, $today)) {                    $result[] = json_decode($line);                }            }        }        return $result;    }        function getSmtpConfFromIni ($config) {        $conf = new MailConfig();		$conf->Host = $config['SMTP_HOST'];        $conf->Port = $config['SMTP_PORT'];        $conf->MailFrom = $config['SMTP_FROM'];        $conf->Username = $config['SMTP_USER'];        $conf->Password = $config['SMTP_PW'];        $conf->Secure = ($config['SMTP_SSL'] == "J");        $conf->Auth = ($conf->Username != "");        return $conf;    }	/**	 * @param PHPMailer $mail	 * @param MailConfig $conf	 * @param string $mailFrom	 * @return bool	 */	function setSmtpConf($mail, $conf) {        $mail->IsSMTP();        $mail->Host = $conf->Host;        $mail->SMTPAuth = $conf->Auth;		$mail->SMTPSecure = ($conf->Secure) ? "ssl" : false;        $mail->Port = $conf->Port;        $mail->Username = $conf->Username;        $mail->Password = $conf->Password;        $mail->Sender = '';        $mail->SetFrom($conf->MailFrom);        //$mail->SMTPDebug = true;        return true;    }    function sendErrorMail($logs, $mailFrom)    {		$body = $this->getMailBody($logs, $mailFrom, "fehlerbericht");        $today = date("d.m.Y");        $total = count($logs);		$subject = "{$mailFrom}: Fehlerbericht vom {$today}, {$total} Fehler";		$mailTo = "fehlerbericht@global-cube.de";		$attachments = array();		foreach ($logs as $logfile) {			if ($logfile->Type == "Modell") {				$attachments[] = $logfile->Filename;			}		}		return $this->sendMail($mailTo, $subject, $body, $mailFrom, $attachments);    }        function sendStatusMail($logs, $mailTo)    {		$body = $this->getMailBody($logs, $mailTo, "statusbericht");		$mailFrom = "Global Cube Business Solutions";		$subject = "Statusbericht GAPS";		return $this->sendMail($mailTo, $subject, $body, $mailFrom);    }	/**	 * @param array $logs	 * @param string $customer	 * @param string $template	 * @return string	 */	public function getMailBody($logs, $customer, $template)	{		$tpl = new RainTPL;		$tpl->assign("logs", $logs);		$tpl->assign("customer", $customer);		$today = date("d.m.Y");		$tpl->assign("today", $today);		$tpl->assign("list", array("neu" => "Neue Konten", "akt" => "Aktualisierte Konten", "entf" => "Gelöschte Konten"));		$body = $tpl->draw($template, true);		return $body;	}	public function sendFiles ($csvfile)	{		$subject = basename($csvfile);		$file = file($csvfile);		array_shift($file); // Headline		foreach ($file as $row) {			$col = explode(";", $row);			if (count($col) < 2 || $col[1] == "\r\n") continue;			$attachment = realpath($this->config['PORTAL'] . "\\" . $col[0]);			$body = basename($attachment);			$this->sendMail($col[1], $subject, $body, "Global Cube", array($attachment));		}	}	/**	 * @param string $mailTo	 * @param string $subject	 * @param string $body	 * @param string $mailFrom	 * @param array $attachments	 * @return bool	 */	public function sendMail($mailTo, $subject, $body, $mailFrom, $attachments = array())	{		$mail = new PHPMailer();		$mail->ClearAllRecipients();		$mail->AddAddress($mailTo, $mailTo);		$mail->Subject = $subject;		$mail->MsgHTML($body);		foreach ($attachments as $file) {			if (is_array($file)) {							$mail->AddAttachment($file[0], $file[1]);			} else {				$mail->AddAttachment($file);			}		}		$this->mailError = "  Mail gesendet!";		$this->setSmtpConf($mail, $this->smtpConfXml);		$res = $mail->Send();		$mail->SmtpClose();		echo $mail->ErrorInfo;		if ($res) {			return true;		}		if (false) {			sleep(10);			$this->setSmtpConf($mail, $this->smtpConfDefault, $mailFrom);			$res = $mail->Send();			$mail->SmtpClose();			if ($res) {				return true;			}			sleep(10);			$mail->Port = 25;			$res = $mail->Send();			$mail->SmtpClose();			if ($res) {				return true;			}		}		$this->mailError = "  Fehler im Mailversand: " . $mail->ErrorInfo;		return false;	}}?>
 |