<?php 
    require_once(__DIR__ . "/../config/connect.php");
    require_once(__DIR__ . "/../global_functions.php");
    ini_set('memory_limit', '-1');
    set_time_limit(600);
    echo "<pre>";
    //header("Content-Type: application/xml");

    function sendFile($files, $token) {
        $ch = curl_init();
		
        curl_setopt($ch, CURLOPT_URL, FILIALSYSTEM_URL_ZENTRALSYSTEM . "/synchronize/sync_structure.php");
        //curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POST, true);
        $files['kassa'] = FILIALSYSTEM_KASSA_NAME;
        $files['token'] = $token;
        $files['kassa_token'] = FILIALSYSTEM_TOKEN;

        curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //echo "@" . realpath($filename);
        $result = curl_exec($ch);
        curl_close($ch);
        //echo "sendFile: $filename<br>";
		
        echo $result;
        
    }

    $query = mysql_query("SHOW TABLES");
    $type = 'Type';
    $null = 'Null';
    $key = 'Key';
    $default = 'Default';
    $extra = 'Extra';

    if (!file_exists(__DIR__ . "/../synchronize/uploads/")) {
        mkdir(__DIR__ . "/../synchronize/uploads/", 0777, false);
    }

    $rand = substr(md5(microtime()),rand(0,26),5);
    $token = FILIALSYSTEM_KASSA_NAME . "_" . $rand . "_" . date(DATE_ATOM);

    //echo "1<br>";
    $files = array();
    $i = 0;
	
    while ($row = mysql_fetch_assoc($query)) {
		// Das mit der SQL_DB ist nur notwendig, weil auf meinem Lokalen PC die DB anders heissen kann - nicht nur sitpos
		$row_db_name='Tables_in_'. SQL_DB;
        $table_name = $row[$row_db_name];
		//$table_name = $row['Tables_in_sitpos'];
		
		echo $table_name."<br>";	
        $XMLDoc = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><$table_name></$table_name>");
        $query1 = mysql_query("DESCRIBE $table_name");
        //echo "current table: $table_name<br>";
        while ($row1 = mysql_fetch_assoc($query1)) {
            $xmlrow = $XMLDoc->addChild("A" . $row1['Field']);

            $xmlrow->$type = urlencode($row1['Type']);
            $xmlrow->$null = urlencode($row1['Null']);
            $xmlrow->$key = urlencode($row1['Key']);
            $xmlrow->$default = urlencode($row1['Default']);
            $xmlrow->$extra = urlencode($row1['Extra']);
        }
		$filename=__DIR__ . "/../synchronize/uploads/" .FILIALSYSTEM_KASSA_NAME."_". $table_name . '.xml';
        $XMLDoc->asXML($filename);
        $keyTab = "file[$i]";
        $files[$keyTab] = new CurlFile($filename);
        $i++;

	
        if ($i == 20) {
            sendFile($files, $token);
            $files =  array();
            $i = 0;
        }

        //echo sendFile(array('kassa' => FILIALSYSTEM_KASSA_NAME), array($table_name . '.xml', $table_name . '.xml', $XMLDoc->asXML()));
    }

    mysql_query("INSERT INTO sync_info(type, wert) VALUES('upsync', 'structure');");

    if ($i > 0) {
		//echo "<br>send files<br>";
        sendFile($files, $token);
    }

    //return;
    echo "</pre>";
?>