<?php

	ini_set('max_execution_time', 0);

	chdir(dirname(__FILE__));
	require_once 'init.php';
	require_once 'function.php';

	openLogger(basename(__FILE__));

	$tmpfname = tempnam("/tmp", "FOO");
	$dirfname = dirname($tmpfname);
	$d = dir($dirfname);
	while (false !== ($entry = $d->read())) {
		if (ereg('^tableData.*$', $entry)
		|| ereg('^FOO.*', $entry)
		|| ereg('^tableLog.*', $entry)
		|| ereg('^tableTemp.*', $entry)) {
			$entry = $dirfname.DIRECTORY_SEPARATOR.$entry;
			$logger->info("Delete ".$entry);
			unlink($entry);
		}
	}
	$d->close();

	$sql = ("SELECT * FROM qb_config");
	$param = '';
	if (count($_SERVER['argv'])>1) {
		foreach ($_SERVER['argv'] as $k => $v) {
			if ($k > 0) {
				if ($param) $param .= ",";
				$param .= "'".$v."'";
			}
		}
	}
	if ($param) $sql .= " WHERE tableid IN (".$param.")";
	$rs = $adodb->Execute($sql);
	while (!$rs->EOF) {
		if ($s === date('s')) sleep(1);
		$s = date('s');

		//$adodb->Execute("DELETE FROM ".$rs->fields['tableid']);
		//crcTable($rs->fields);
		$ausgabe = importTable($rs->fields);
		if ( $ausgabe == 1 ) {
        if($rs->fields['history_mode'] == 2)
            updateTable($rs->fields);
        else if($rs->fields['history_mode'] == 1)
            replaceTable($rs->fields);
        else if($rs->fields['history_mode'] == 0)
            diffTable($rs->fields);
    } else {
        $logger->error("Fehler beim Import, Mails werden verschickt...");
        $logger->error($adodb->ErrorMsg());
        
        //include('smtpconnection/smtpconnection.class.php');
        
        ini_set('SMTP', 'mail.group-net.de');
        ini_set('smtp_port', 25);
        $msg = '
<html>
<head>
  <style type="text/css">
    body {
      font-family: Verdana;
      font-size: 0.7em;
    }
  </style>
 <title>Fehlerbenachrichtigung</title>
</head>
<body>
Aufgrund eines Fehlers in der Tabelle <b>' . $param . '</b> wurde der automatische Import <b>abgebrochen</b>!
<br><br>
<table><tr><td style="border: 1px solid black; font-family: Verdana; font-size: 0.7em; padding: 5px;">Folgender Fehler war verantwortlich: <br>
<b>' . $adodb->ErrorMsg() . '</b></td></tr></table><br><br>
<table><tr><td style="border: 1px solid black; font-family: Verdana; font-size: 0.7em; padding: 5px;">Die Ausgabe des Scripts war wie folgt:<br>
<b>' . $ausgabe . '</b></td></tr></table><br><br>
<b>Dies ist eine vom qbuilder automatisch generierte Mail!</b>
</body>
</html>
';

        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

        /* zusätzliche Header */
        $headers .= "To: Matthias Winter <winter@global-cube.de>, Andreas Ankenbrand <ankenbrand@global-cube.de>\r\n";
        //$headers .= "To: Jens Karl <karl@global-cube.de>\r\n";
        $headers .= "From: qbuilder <qbuilder@global-cube.de>\r\n";
        
        
        mail('winter@global-cube.de, ankenbrand@global-cube.de', 'qbuilder - Fehlerbenachrichtigung', $msg, $headers);
        //mail('karl@global-cube.de', 'qbuilder - Fehlerbenachrichtigung', $msg, $headers);
    }

		$rs->MoveNext();
	}
	closeLogger();

?>
