<?php if(!defined('master_script')) die();

/**
 * Functions
 *
 * @category   Extension (updater)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-28
 */

	function updater_addon_warnings($type, $label) {
		global $extensions, $system_data, $updater_xml;
		
		$extensions['updater']['custom ' . $type] = array();
		$extensions['updater']['modified ' . $type] = array();
		$extensions['updater']['installed ' . $type] = dirlist($system_data['folder'][$type], array(), true);
		
		switch($type) {
			case 'widgets' : $extensions['updater']['packaged ' . $type] = explode(',', $updater_xml->packages[0]->widgets[0]); break;
			case 'modules' : $extensions['updater']['packaged ' . $type] = explode(',', $updater_xml->packages[0]->modules[0]); break;
			case 'extensions' : $extensions['updater']['packaged ' . $type] = explode(',', $updater_xml->packages[0]->extensions[0]); break;
			case 'plugins' : $extensions['updater']['packaged ' . $type] = explode(',', $updater_xml->packages[0]->plugins[0]); break;
		}
		
		if($type == 'modules') {
			for($i = 0; $i < count($extensions['updater']['packaged ' . $type]); $i++) {
				$parts = explode('|', $extensions['updater']['packaged ' . $type][$i]);
				$extensions['updater']['packaged ' . $type][$i] = $parts[0];
				$extensions['updater']['packaged ' . $type . ' versions'][$parts[0]] = $parts[1];
			}
		}
		
		foreach($extensions['updater']['installed ' . $type] as $item) {
			$item = str_replace($system_data['folder'][$type], '', $item);
			
			if(!in_array($item, $extensions['updater']['packaged ' . $type])) {
				$name = $item;
				include_txt($item, $system_data['folder'][$type] . $item . '/languages/');

				if(txt($item, 'title: ' . $item) != 'title: ' . $item) $name = txt($item, 'title: ' . $item);
				if(txt($item, 'name: ' . $item) != 'name: ' . $item) $name = txt($item, 'name: ' . $item);
				
				$extensions['updater']['custom ' . $type][] = $name . ' (' . $label . ')';
				$extensions['updater']['warnings'] = 1;
			}

			if($type == 'modules' && is_dir($system_data['folder']['frontend template'] . 'modules/' . $item)) {
				include_txt($item, $system_data['folder']['modules'] . $item . '/languages/');
				
				$module_infofile = $system_data['folder'][$type] . $item . '/backend-module.php';
				if(is_file($module_infofile)) include($module_infofile);
				
				if(isset($modules[$item]['version']) && $modules[$item]['version'] != $extensions['updater']['packaged ' . $type . ' versions'][$item]) {
					$extensions['updater']['modified ' . $type][] = txt($item, 'title: ' . $item);
					$extensions['updater']['warnings'] = 1;
				}
			}
		}
	}

/**
 * Load stylesheet
 *
 * @category   Extension (updater)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-27
 */

	$system_data['backend']['header'] .= '<link rel="stylesheet" type="text/css" href="' . $system_data['folder']['extensions'] . 'updater/index.css">';

/**
 * Download and install package
 *
 * @category   Extension (updater)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-27
 */
	
	if(isset($_POST['submit_updater_go'])) {
		rmkdir($system_data['folder']['temp']);

		$updater_xml = simplexml_load_file($system_data['service']['updater xml file']);
		$package_filename = 'update.zip';
		$install_root = '';
		
		if(fopen($updater_xml->file, 'r')) {
			$package_source = file_get_contents($updater_xml->file);
			$package_download = file_put_contents($system_data['folder']['temp'] . $package_filename, $package_source);
			
			if(filesize($system_data['folder']['temp'] . $package_filename) > 0) {
				include_once($system_data['folder']['plugins'] . 'pclzip/pclzip.lib.php');
				
				$package_installer = new PclZip($system_data['folder']['temp'] . $package_filename);
				$package_installer->extract(PCLZIP_OPT_PATH, $install_root, PCLZIP_OPT_REPLACE_NEWER);
				if(is_file($system_data['folder']['temp'] . $package_filename)) unlink($system_data['folder']['temp'] . $package_filename);
	
				header('location: index.php?backend=updater');
			} else {
				$message = txt('updater', 'Die Anfrage für eine Aktualisierung wurde abgelehnt. Möglicherweise ist Ihre Lizenz abgelaufen oder Ihr Server wurde so konfiguriert, dass automatische Aktualisierungen aus Sicherheitsgründen nicht installiert werden können. Bitte kontaktieren Sie das Service-Team von Pagemap unter getpagemap.com');
			}
		} else {
			$message = txt('updater', 'Die Aktualisierung konnte nicht durchgeführt werden, da ein Verbindungsfehler auftrat. Bitte kontaktieren Sie das Service-Team von Pagemap unter getpagemap.com');
		}
	}

?>