<?php

/*******************************************************************************
 * eredeti url: http://www.archline.hu/version/updatexml
 ******************************************************************************/
if (!class_exists('crm_database')) require_once("common_cadline_libraries/crm_database.class.php");

@header("Content-Type: text/html; charset=UTF-8");

$fajl = "../public/downloads/eng/BuildLog.xml";

$xml = simplexml_load_file($fajl);
$tomb = array();

$tomb['deleted'] = "0";

$tomb['full']      = preg_replace(array("/Full/", "/Update/"), array("1", "0"), $xml->archline_version);
$tomb['prod_ver']  = substr($xml->product_version, 0);
$tomb['d_build']   = substr($xml->date_of_build, 0);
$tomb['datum']     = substr($xml->date_of_cadlineftp_upload, 0);
$tomb['d_itc_ftp'] = substr($xml->date_of_itcftp_upload, 0);
$tomb['pkgid']     = substr($xml->build, 0);
$tomb['setup_type']   = substr($xml->setup_type, 0);
$tomb['path_newroda'] = substr($xml->path_on_newroda, 0);
$tomb['fajl']      = preg_replace("/http:\/\/www\.cadline\.hu\/public\/downloads\//", "", $xml->path_on_cadline_ftp);
$tomb['path_itc']  = substr($xml->path_on_itc_ftp, 0);
$tomb['ev']        = substr($xml->year, 0);
$x64               = substr($xml->x64, 0);
if (substr($xml->serials, 0))
	$tomb['usertype']  = substr($xml->serials, 0);
if (substr($xml->hash, 0))
	$tomb['hash']  = substr($xml->hash, 0);
if (isset($xml->delete) && (substr($xml->delete, 0) == "1" || substr($xml->delete, 0) == "0"))
	$tomb['deleted'] = substr($xml->delete, 0);


$parancs = " SELECT * FROM `" . JMLADATBAZIS . "`.`version` WHERE ev = " . $tomb['ev'] . " AND pkgid = " . $tomb['pkgid'] . " AND deleted = 0 AND full = " . $tomb['full'] . " AND fajl='" . $tomb['fajl'] . "'";
print "parancs: " . $parancs . "<br><br>";
crm_database::getInstance()->query($parancs);
$sql = crm_database::getInstance()->fetchAssoc();

print "tom: ";
print_r($tomb);
print "\n\nsql: ";
print_r($sql);

if (count($sql) == 1) {
	crm_database::getInstance()->update(JMLADATBAZIS . '.version', $tomb, "ev = " . $tomb['ev'] . " AND pkgid = " . $tomb['pkgid'] . " AND full = " . $tomb['full'] . " AND fajl='" . $tomb['fajl'] . "' AND deleted = 0");
	$record->osztaly = "version";
	$record->fv = "updatexml";
	$record->hiba = "Egy, frissíteni kéne .. rekord találat, build: " . $tomb['pkgid'] . "\nsql: " . $parancs . "\ndb: " . count($sql);
	$record->datum = date('Y-m-d H:i:s', time());
	//		$this->db->insert('a_hibak',$record);
} elseif (count($sql) == 0 && $tomb['deleted'] != "1") {
	//	$tomb['ev'] = "2013";
	//	$tomb['projectid'] = "ARCHlineXP2012";	// ne legyen online frissítés
	// csak akkor legyen online frissítés, ha a fájlnévben van "_Update_"
	if (strstr(basename($tomb['fajl']), "_Update_")) {
		$tomb['projectid'] = "ArchlineXP" . $tomb['ev'];
		if ($x64 == 1)
			$tomb['projectid'] .= "x64";
	} else
		$tomb['projectid'] = "";

	if ($tomb['ev'] == "2012")
		$tomb['azonosito'] = "{8D322C6F-87A6-4487-B47D-15BF449DAEF3}";
	else {
		//			$tomb['azonosito'] = "{93B045D0-84A3-447E-BFA5-FE77755A26D7}";
		if ($x64 == 1)
			$tomb['azonosito'] = "{2CCF8C6E-FEAE-4167-AC48-BC1DF423AE85}";
		else
			$tomb['azonosito'] = "{93B045D0-84A3-447E-BFA5-FE77755A26D7}";
	}

	$tomb['usertype'] = ";360012;";
	crm_database::getInstance()->insert(JMLADATBAZIS . '.version', $tomb);

	$record->osztaly = "version";
	$record->fv = "updatexml";
	$record->hiba = "0, beszúrok .., build: " . $tomb['pkgid'] . "\nsql: " . $parancs . "\ndb: " . count($sql);
	$record->datum = date('Y-m-d H:i:s', time());
	//	$this->db->insert('a_hibak',$record);
} else {
	$record->osztaly = "version";
	$record->fv = "updatexml";
	$record->hiba = "Több rekord találat (vagy torolt beszuras), build: " . $tomb['pkgid'] . "\nsql: " . $parancs . "\ndb: " . count($sql) . "\ndeleted: " . $tomb['deleted'];
	$record->datum = date('Y-m-d H:i:s', time());
	if ($tomb['deleted'] != "1")
		crm_database::getInstance()->insert(JMLADATBAZIS . '.a_hibak', $record);
}

print "<pre>";
print_r($tomb);
print $tomb['projectid'];
