<?php
include("config.php");
include("version.php");
include("email.php");
require_once("../configuration.php");


exit();	//ba TODO
// minden hajnal 01:30-kor	- trial letöltőknek automatikus e-mail 20 nappal a letöltés után
$version = new Version();
$kiirni = "";
$log    = "\n" . date('Y-m-d H:i', time()) . ":";

$elteltnap = 20;

$maxtime = time() - ($elteltnap * 24 * 60 * 60);
$mintime = time() - (($elteltnap + 1) * 24 * 60 * 60);

// azok a magyar letöltők akik nem kaptak még e-mailt
$sql  = "SELECT stat_id, stat_email, stat_first, stat_country FROM download_stats ";
$sql .= " WHERE stat_pron='' AND stat_date>{$mintime} AND stat_date<{$maxtime} ";
$sql .= " AND stat_lang='hun' ";
$sql .= " GROUP BY stat_email";

$msh2->query($sql);
$letoltokhun = $msh2->fetchAssoc();
$sql  = "SELECT introtext FROM jml_content WHERE id = '944'";
$msh2->query($sql);
$page = $msh2->fetchNext();
$p = (object)$page;
$pagehun = $p->introtext;
$conf = new JConfig();


foreach ($letoltokhun as $letolto) {
	$l = (object)$letolto;
	// felhasználónak ne küldjön levelet (van-e aktiv kulcsa)
	$nUserID = $version->get_nUserIDByEmail($l->stat_email, 'hun');
	if (is_numeric($nUserID) && $nUserID > 0) {
		$kulcssql = "SELECT hlNum FROM " . CRMADATBAZIS . ".hardlock WHERE hlUser='{$nUserID}' AND hlStat=1";
		$msh2->query($kulcssql);
		$kulcs = $msh2->fetchNext();
		$k = (object)$kulcs;
		if (isset($k->hlNum) && $k->hlNum != "") {
			$kiirni .= "\n\nnincs elkuldve: " . print_r($l, true);
			$log    .= "\nHUN nincs elkuldve (felhasznalo): " . $l->stat_email . " " . $l->stat_first . " " . $l->stat_id . " (hun)";
			continue;
		}
	}

	$leveltext = str_ireplace('#nev#', $l->stat_first, $pagehun);

	$email = new CI_Email();
	$email->initialize();
	$email->setFrom($conf->mailfrom, 'CadLine');
	$email->subject('ARCHLine.XP letoltes');
	$email->message($leveltext);
	$email->send($l->stat_email, 'smtp');
	$email->clear();

	$ujletolto['stat_pron'] = "1";
	$msh2->update('download_stats', $ujletolto, "stat_id=" . $l->stat_id);

	$kiirni .= "\n\nelkuldve: " . print_r($l, true);
	$log    .= "\nHUN elkuldve: " . $l->stat_email . " " . $l->stat_first . " " . $l->stat_id . " (hun)";
}


/***********************************************************************************/

// azok a NEM magyar letöltők akik nem kaptak még e-mailt
$sql  = "SELECT stat_id, stat_email, stat_first, stat_country FROM download_stats ";
$sql .= " WHERE stat_pron='' AND stat_date>{$mintime} AND stat_date<{$maxtime} ";
$sql .= " AND stat_lang!='hun' ";
$sql .= " GROUP BY stat_email";

$msh2->query($sql);
$letoltokeng = $msh2->fetchAssoc();

$sql  = "SELECT introtext FROM jml_content WHERE id = '945'";
$msh2->query($sql);
$page = $msh2->fetchNext();
$p = (object)$page;
$pageeng = $p->introtext;

foreach ($letoltokeng as $letolto) {
	$l = (object)$letolto;
	// felhasználónak ne küldjön levelet (van-e aktiv kulcsa)
	$nUserID = $version->get_nUserIDByEmail($l->stat_email, 'eng');
	if (is_numeric($nUserID) && $nUserID > 0) {
		$kulcssql = "SELECT hlNum FROM " . CRMADATBAZIS . ".hardlock WHERE hlUser='{$nUserID}' AND hlStat=1";
		$msh2->query($kulcssql);
		$kulcs = $msh2->fetchNext();
		$k = (object)$kulcs;
		if (isset($k->hlNum) && $k->hlNum != "") {
			$kiirni .= "\n\nnincs elkuldve: " . print_r($l, true) . "nUserID: " . $nUserID . ", hlnum: " . $k->hlNum;
			$log    .= "\nENG nincs elkuldve (felhasznalo): " . $l->stat_email . " " . $l->stat_first . " " . $l->stat_id . " (eng), hlNum: " . $kulcs->hlNum . " , user: " . $nUserID;
			continue;
		}
	}

	$leveltext = str_ireplace('#nev#', $l->stat_first, $pageeng);

	$email = new CI_Email();
	$email->initialize();
	$email->setFrom('info@archlinexp.com', 'ARCHLine');
	$email->subject('ARCHLine.XP download');
	$email->message($leveltext);
	$email->send($l->stat_email, 'smtp');
	$email->clear();

	$ujletolto['stat_pron'] = "1";
	$msh2->update('download_stats', $ujletolto, "stat_id=" . $l->stat_id);

	$kiirni .= "\n\nENG elkuldve: " . print_r($l, true);
	$log    .= "\nENG elkuldve: " . $l->stat_email . " " . $l->stat_first . " " . $l->stat_id . " (eng)";

	print "<hr>elkuldve: ";
	print_r($l);
}

$record['osztaly'] = "cron";
$record['fv'] = "downloademail";
$record['hiba'] = $kiirni;
$record['datum'] = date('Y-m-d H:i', time());
$msh2->insert('a_hibak', $record);

exec("echo '{$log}' >> ../public/download_email.txt");
