<?php

	/**
	 * Dieses Script soll das Changelog parsen  
	 */
 
	$changelog_file = dirname(__FILE__).'/../../wpshopgermany_svn/changelog';
	$wp_home = '/var/home/htdocs/wpshopgermany.de/www/';

	if (sizeof($_SERVER["argv"]) > 1)
	{

		// TEST Modus
		$version_info = '/var/home/htdocs/dl.maennchen1.de/www/wpsg/wpsg_info_dev';
		$version_file = '/var/home/htdocs/dl.maennchen1.de/www/wpsg/version_dev';
		
	}
	else
	{

		// Live Modus
		$version_info = '/var/home/htdocs/dl.maennchen1.de/www/wpsg/wpsg_info';
		$version_file = '/var/home/htdocs/dl.maennchen1.de/www/wpsg/version';

	}
	
	function parseChangelog($changelog_file)
	{
	
		$arZeilen = file($changelog_file);	
		$arChangelog = array();
		$arBlock = array();
		
		foreach ($arZeilen as $zeile)
		{
				
			$zeile = preg_replace('/(\r)|(\n)/', '', $zeile);
					
			if (preg_match('/^#(.*)/', $zeile))
			{
				
				if (sizeof($arBlock) > 0) $arChangelog[] = $arBlock;
				$arBlock = array();
				
				$version = preg_replace('/(#)|(\040*\/\040\d*)/', '', $zeile);
				$rev = preg_replace('/#\d*\.\d*\.\d*\040*\/\040*/', '', $zeile);
				
				$arBlock['version'] = $version;
				$arBlock['rev'] = $rev;
				$arBlock['changes'] = array();
							
			}
			else if (preg_match('/^\040*\-\040*(.*)/', $zeile))
			{
				
				$arBlock['changes'][] = preg_replace('/^\040*\-\040*/', '', $zeile);
				
			}
			
		}
	
		if (sizeof($arBlock) > 0) $arChangelog[] = $arBlock;
		
		return $arChangelog;
		
	} // function parseChangelog($changelog_file)

	$arChangelog_shop = array_reverse(parseChangelog($changelog_file));

	//http://wpshopgermany.maennchen1.de/websvn/comp.php?repname=wpShopGermany3&compare[]=%2F@1594&compare[]=%2F@1628
	$template_link = 'http://wpshopgermany.maennchen1.de/websvn/comp.php?repname=wpShopGermany3&compare[]=%2F@'.trim($arChangelog_shop[1]['rev']).'&compare[]=%2F@'.trim($arChangelog_shop[0]['rev']);

	if (!isset($_SERVER["argv"][1]))
	{	
		
		// Live Modus

		// Klasse für XML-RPC Schnittstelle einbinden und aufrufen
		require($wp_home.'/wp-includes/class-IXR.php');
		$rpc = new IXR_Client('http://wpshopgermany.maennchen1.de/xmlrpc.php');
	 
		$post['title'] = 'wpShopGermany #'.$arChangelog_shop[0]['version'];
		$post['categories'] = array('wpShopGermany');
		$post['mt_keywords'] = 'wpShopGermany Wordpress Shop Plugin Changelog '.$arChangelog_shop[0]['version'];
		$post['description'] = '<strong>Changelog wpShopGermany Version '.$arChangelog_shop[0]['version'].'</strong><ul><li>'.implode('</li><li>', $arChangelog_shop[0]['changes']).'</li><li><a title="veränderte Templates" href="'.$template_link.'" target="_blank">veränderte Templates</a></li></ul>';
 
		$status = $rpc->query(
		    'metaWeblog.newPost',
		    1,				
		    'daniel',  
		    'Qbd8uio',
		    $post,		
		    false // Veröffentlicht Ja/Nein			
		);
 
		$post['description'] = preg_replace('/"/', '\\"', $post['description']);
	
	}
	else
	{

		$post['description'] = '<strong>Changelog wpShopGermany Version '.$arChangelog_shop[0]['version'].'</strong><ul><li>'.implode('</li><li>', $arChangelog_shop[0]['changes']).'</li><li><a title="veränderte Templates" href="'.$template_link.'" target="_blank">veränderte Templates</a></li><li><a href="https://wpshopgermany.maennchen1.de/category/changelog/">Gesamtes Changelog</a></li></ul>';
		$post['description'] = preg_replace('/"/', '\\"', $post['description']);

	}

	$mysqli_wpsg = @new mysqli("localhost", "wpshopgermany", "gEi94Qml1Q!a", "wpshopgermany");
	$result = $mysqli_wpsg->query("SELECT COUNT(DISTINCT `url`) AS `reg` FROM `wp_wpshopgermany_register_url`");
	$row = $result->fetch_assoc(); $active_installs = $row['reg'];
	$mysqli_wpsg->close();
	
	$downloaded = $active_installs * 2 + rand(1, 3000);
	
	file_put_contents($version_info, '
		{
			"active_installs":"'.$active_installs.'",
			"downloaded":"'.$downloaded.'",
			"last_updated":"'.date('d.m.Y', time()).'",
			"rating":"",
			"num_ratings":"",
			"tested":"4.9.0",
			"requires":"3.0",
			"version":"'.$arChangelog_shop[0]['version'].'",
			"sections": {
				"changelog":"<div style=\"color:red; font-size:2rem;\">Denken Sie daran, Ihre Installation vor einem Update zu sichern und beachten Sie auch unsere <a href=\"https://wpshopgermany.maennchen1.de/2010/11/17/update-anleitung/\">Update Hinweise</a>.</div><h4>#'.$arChangelog_shop[0]['version'].'</h4>'.$post['description'].'"
			}
		}
	');

	file_put_contents($version_file, $arChangelog_shop[0]['version']);
	 
?>