#!/usr/bin/php
<?php

/// init
	global $ini;
	global $node;

	require(dirname(__FILE__)."/../include/functions.php");
	init();

	if (get_ini_opt('cron','mail-notls') === '0') { exit(0); }

	check_live();

### read configuration options

	$mail_tpl = $ini['global']['template_dir']."/mail-notls.txt";
	$msgbody=file_get_contents($mail_tpl);

	$mail_fromaddr = $ini['global']['email'];
	$mail_fromname = $ini['global']['name'];
	$mail_subject="Nem biztonságos e-mail beállítás";

### Get emails from log
	$notlsarr=array();
	exec('grep dovecot /var/log/mail.log[-0-9]* |grep "\(pop3\|imap\)-login: Login:" |grep -v TLS |grep -v secured | sed -e "s/^.*user=<\([^>]*\)>,.*/\1/" |sort |uniq', $notlsarr);
	if (count($notlsarr))
	{

### Collect e-mail addresses

		$sql="SELECT
			CONCAT(
				mail_users.name,
				'@',
				IF((dom_host.host IS NULL), '', CONCAT(dom_host.host,'.')),
				domain.name
			) AS email

			FROM mail_users
				LEFT JOIN domain ON mail_users.domainid=domain.id
				LEFT JOIN dom_host ON dom_host.id=mail_users.hostid
				LEFT JOIN client ON domain.clientid=client.id

			WHERE mail_users.active='1'
				AND domain.active='1'
				AND domain.mx IS NULL
				AND domain.transport IS NULL
				AND client.active='1'
			;
		";

		$res=DBquery($sql);

		//send all mails
		while ($row=$res->fetch_assoc())
		{
			if (in_array($row['email'], $notlsarr))
			{
				$email=$row['email'];

				$mail_subject_suffix=' ('.$email.')';

				//echo "$row[email] - $mail_subject$mail_subject_suffix\n";
				//echo "$msgbody\n";
				mailsend($mail_fromname, $mail_fromaddr, $email, $mail_subject.$mail_subject_suffix, $msgbody);
				sleep(1);
			}
		}
	}

DBclose();
