#!/usr/bin/php
<?php

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

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

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

	check_live();

### read configuration options

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

	$mail_fromaddr = $ini['global']['email'];
	$mail_fromname = $ini['global']['name'];
	$mail_subject="E-mail biztonsági emlékeztető";

### Collect weak passwords

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

		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'
			AND (
				CHAR_LENGTH(mail_users.password) < 8
				OR (mail_users.password REGEXP BINARY '^([a-z]*|[A-Z]*|[0-9]*|[[:punct:]]*)$' != 0)
				OR (mail_users.password REGEXP BINARY '.[0-9A-Z[:punct:]].' = 0)
				OR (CHAR_LENGTH(mail_users.name) >2 AND LOWER(mail_users.password) LIKE CONCAT('%',LOWER(mail_users.name),'%'))
				OR (CHAR_LENGTH(domain.name) >2	AND LOWER(mail_users.password) LIKE CONCAT('%',LOWER((SUBSTRING_INDEX(domain.name, '.', 1))),'%'))
				OR (mail_users.password REGEXP BINARY '(123456|jelszo|password)' != 0)
				OR (CHAR_LENGTH(mail_users.password) < 12 AND mail_users.password REGEXP BINARY '(123|321|qwe|ewq|asd|dsa|abc|xyz)' != 0)
			)
		;
	";

	$res=DBquery($sql);

	//send all mails
	while ($row=$res->fetch_assoc()) {

		$mail_subject_suffix=' ('.$row['email'].')';

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

DBclose();

?>
