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

/**
 * Load stylesheet
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-07-11
 */

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

/**
 * Global data settings
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	$_GET['area'] = isset($_GET['area']) ? trim($_GET['area']) : 'system';
	
/**
 * Get data for system settings
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   201-12-02
 */
	
	if($_GET['area'] == 'system' && $_SESSION['user'] == sha1($administrator_name)) {
		
	}

/**
 * Get data for template settings
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-11-19
 */

	if($_GET['area'] == 'template' && $_SESSION['user'] == sha1($administrator_name)) {
		// Get available frontend templates
		$templates = array();
		
		$open_template_dir = opendir($system_data['folder']['templates']);
		
		while($dir_entry = readdir($open_template_dir)) {
			if(is_dir($system_data['folder']['templates'] . $dir_entry) && $dir_entry != '.' && $dir_entry != '..' && file_exists($system_data['folder']['templates'] . $dir_entry . '/info.php')) {
				include($system_data['folder']['templates'] . $dir_entry . '/info.php');
				$templates[$dir_entry]['path'] = $dir_entry;
				$templates[$dir_entry]['title'] = $template_data['title'];
			}
		}
		
		closedir($open_template_dir);
		
		// Get data for current frontend template (because it was overwritten by previous method)
		if(is_file($system_data['folder']['templates'] . $user_data['frontend template'] . '/info.php'))
			include($system_data['folder']['templates'] . $user_data['frontend template'] . '/info.php');
		
		// Get template HTML header and footer
		$template_user_header = is_file($system_data['file']['frontend html header file']) ? file_get_contents($system_data['file']['frontend html header file']) : '';
		$template_user_footer = is_file($system_data['file']['frontend html footer file']) ? file_get_contents($system_data['file']['frontend html footer file']) : '';
	}

/**
 * Save system settings
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-05-19
 */
	
	if($_GET['area'] == 'system' && isset($_POST['save_settings']) && $_SESSION['user'] == sha1($administrator_name)) {		
		// Update user settings
		$user_data['author'] = clean_string($_POST['site_author'], true);
    	$user_data['email'] = clean_string($_POST['email'], true);
    	$user_data['frontend languages'] = clean_string($_POST['site_languages'], true) == '' ? 'en' : clean_string($_POST['site_languages'], true);
    	$user_data['quick help'] = isset($_POST['quick_help']) ? 1 : 0;
    	$user_data['error reporting'] = isset($_POST['error_reporting']) ? 0 : 1;
    	$user_data['site title'] = clean_string($_POST['site_title']);
    	$user_data['base url'] = clean_string($_POST['base_url'], true);
    	$user_data['url rewrite'] = isset($_POST['url_rewrite']) ? 1 : 0;
    	$user_data['backend language'] = $_POST['backend_language'];
    	$user_data['rss title'] = clean_string($_POST['feed_title'], true);
    	$user_data['rss description'] = clean_string($_POST['feed_description'], true);
    	$user_data['rss url'] = clean_string($_POST['feed_url'], true);
    	$user_data['rss only news'] = isset($_POST['feed_only_news']) ? 1 : 0;
    	$user_data['rss items'] = intval($_POST['feed_items']);
    	$user_data['content preview'] = $_POST['content_preview'];
    	$user_data['latitude'] = clean_string($_POST['latitude'], true);
    	$user_data['longitude'] = clean_string($_POST['longitude'], true);
    	$user_data['region'] = clean_string($_POST['region'], true);
    	$user_data['placename'] = clean_string($_POST['placename'], true);
    	$user_data['meta language'] = isset($_POST['meta_language']) ? 1 : 0;
    	$user_data['meta page-topic'] = isset($_POST['meta_page_topic']) ? 1 : 0;
    	$user_data['meta noodp'] = isset($_POST['meta_noodp']) ? 1 : 0;
    	$user_data['meta last-modified'] = isset($_POST['meta_last_modified']) ? 1 : 0;
    	$user_data['meta canonical'] = isset($_POST['meta_canonical']) ? 1 : 0;
		
    	// Validate frontend language codes
    	$user_data['frontend languages'] = explode(',', $user_data['frontend languages']);
    	foreach($user_data['frontend languages'] as $code) $valid_language_codes[] = trim($code);
    	$user_data['frontend languages'] = implode(',', $valid_language_codes);
    	
    	// Validate RSS items
    	if($user_data['rss items'] > $system_data['backend']['maximum latest pages'])
    		$user_data['rss items'] = $system_data['backend']['maximum latest pages'];
		
		connector('settings save user data');
		
		// Save to user settings file
		save_user_data();
			
		// Create .htaccess file if not exists
    	$htaccess_filename = '.htaccess';
    	
    	if($user_data['url rewrite'] == 1 && !file_exists($htaccess_filename)) {
    		$htaccess[] = '<IfModule mod_rewrite.c>';
    		$htaccess[] = '	RewriteEngine on';
    		$htaccess[] = '	RewriteBase ' . dirname($_SERVER['PHP_SELF']);
    		$htaccess[] = '	RewriteCond %{REQUEST_FILENAME} !-f';
    		$htaccess[] = '	RewriteCond %{REQUEST_FILENAME} !-d';
    		$htaccess[] = '	RewriteRule .* index.php [L]';
    		$htaccess[] = '</IfModule>';

    		$htaccess[] = '<IfModule mod_deflate.c>';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE text/plain';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE text/html';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE text/xml';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE text/css';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE application/xml';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE application/xhtml+xml';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE application/rss+xml';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE application/javascript';
    		$htaccess[] = '	AddOutputFilterByType DEFLATE application/x-javascript';
    		$htaccess[] = '</IfModule>';
    		
			connector('settings save htaccess');

    		$htaccess_file = fopen($htaccess_filename, 'wb');
    		fputs($htaccess_file, implode($htaccess, "\n"));
    		fclose($htaccess_file);
    	}
    	
    	header('location: index.php?backend=settings&area=' . $_GET['area']);
	}

/**
 * Save template settings
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-11-19
 */

	if($_GET['area'] == 'template' && isset($_POST['save_settings']) && $_SESSION['user'] == sha1($administrator_name)) {		
		// Update user settings
    	$user_data['frontend template'] = $_POST['template'];
    	$user_data['frontend template style'] = $_POST['template_style'];
    	$user_data['date format print'] = clean_string($_POST['date_print'], true) == '' ? $user_data['date format print'] : clean_string($_POST['date_print'], true);
    	$user_data['date format sort'] = clean_string($_POST['date_sort_format'], true) == '' ? $user_data['date format sort'] : clean_string($_POST['date_sort_format'], true);
    	$user_data['sort by'] = $_POST['sort_by'];
    	$user_data['sort ascending'] = isset($_POST['sort_ascending']) ? 1 : 0;
    	$user_data['show sort headlines'] = isset($_POST['show_sort_headlines']) ? 1 : 0;
    	$user_data['show info pages'] = isset($_POST['show_info_pages']) ? 1 : 0;
    	
    	// Delete logo
    	if(isset($_POST['delete_old_logo'])) {
    		if(is_file($user_data['logo'])) unlink($user_data['logo']);
    		$user_data['logo'] = '';
    	}
    	
    	// Upload new logo
    	if($_FILES['logo_upload']['name'] != '') {
    		$new_logo_name = transcribe($_FILES['logo_upload']['name']);
    		
    		rmkdir($system_data['folder']['user files']);
			
			if(is_image($new_logo_name)) {
				move_uploaded_file($_FILES['logo_upload']['tmp_name'], $system_data['folder']['user files'] . $new_logo_name);
				
				if(is_file($system_data['folder']['user files'] . $new_logo_name))
					$user_data['logo'] = $system_data['folder']['user files'] . $new_logo_name;
			}
    	}
    	
		// Save to user settings file
		save_user_data();
		
		// Save HTML header and footer
		rmkdir($system_data['folder']['template files']);
		file_put_contents($system_data['file']['frontend html footer file'], strip_custom_tags(stripslashes($_POST['template_html_header']), true, false, false, false, false));
		file_put_contents($system_data['file']['frontend html footer file'], strip_custom_tags(stripslashes($_POST['template_html_footer']), true, false, false, false, false));
		
		// Reload
    	header('location: index.php?backend=settings&area=' . $_GET['area']);
	}

/**
 * Upload and extract WMEP ZIP file
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */
	
	if(isset($_POST['upload-wmep-file']) && isset($_GET['area']) && $_SESSION['user'] == sha1($administrator_name)) {
		$new_wmep_file_name = transcribe($_FILES['wmep-file']['name']);
		$new_wmep_name = substr($new_wmep_file_name, 0, strrpos($new_wmep_file_name, '.'));
		$new_wmep_file_extension = strtolower(substr($new_wmep_file_name, strrpos($new_wmep_file_name, '.') + 1));
		$new_wmep_path = $system_data['folder'][$_GET['area']];
		$new_wmep_dirname = $new_wmep_path . $new_wmep_name;

		if($new_wmep_file_extension == 'zip') {
			if(file_exists($new_wmep_dirname) && is_writeable($new_wmep_dirname)) rec_rmdir($new_wmep_dirname);
			
			move_uploaded_file($_FILES['wmep-file']['tmp_name'], $new_wmep_path . $new_wmep_file_name);

			include_once($system_data['folder']['plugins'] . 'pclzip/pclzip.lib.php');
			
			$zip = new PclZip($new_wmep_path . $new_wmep_file_name);
			$zip->extract(PCLZIP_OPT_PATH, $new_wmep_path);
			if(file_exists($new_wmep_path . $new_wmep_file_name)) unlink($new_wmep_path . $new_wmep_file_name);

			header('location: index.php?backend=settings&area=' . $_GET['area']);
		}
	}

/**
 * Delete WMEP package
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-31
 */
	
	if(isset($_GET['delete-wmep']) && isset($_GET['area']) && $_SESSION['user'] == sha1($administrator_name)) {
		$wmep_path = $system_data['folder'][$_GET['area']] . $_GET['delete-wmep'];
		
		if(file_exists($wmep_path) && is_dir($wmep_path)) {
			$delete_package = 1;
			
			if($_GET['area'] == 'modules' && isset($extensions[$_GET['delete-wmep']]['protected']) && $extensions[$_GET['delete-wmep']]['protected'] == 1) $delete_package = 0;
			if($_GET['area'] == 'extensions' && isset($extensions[$_GET['delete-wmep']]['protected']) && $extensions[$_GET['delete-wmep']]['protected'] == 1) $delete_package = 0;
			
			if($delete_package == 1 && ($_GET['area'] == 'widgets' || $_GET['area'] == 'modules' || $_GET['area'] == 'extensions')) rec_rmdir($wmep_path);
		}

		header('location: index.php?backend=settings&area=' . $_GET['area']);
	}

/**
 * Get data for Template
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-11-19
 */
	
	// Frontend template style
	$frontend_template_styles = explode(',', $template_data['styles']);
	if(!is_array($frontend_template_styles)) $frontend_template_styles = array($frontend_template_styles);

/**
 * Get data for Widgets
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

	if($_GET['area'] == 'widgets' && $_SESSION['user'] == sha1($administrator_name)) {
		$widgets = array();
		$open_widgets_folder = opendir($system_data['folder']['widgets']);
		
		while($entry = readdir($open_widgets_folder)) {
			if(is_dir($system_data['folder']['widgets'] . $entry) && $entry != "." && $entry != "..") {
				$info_file = $system_data['folder']['widgets'] . $entry . '/info.php';
				if(file_exists($info_file)) include($info_file);
					
				include_txt($entry, $system_data['folder']['widgets'] . $entry . '/languages/');
    			
    			$widgets[$entry]['name'] = $entry;
    			if(!isset($widgets[$entry]['version'])) $widgets[$entry]['version'] = '';
    			if(!isset($widgets[$entry]['author'])) $widgets[$entry]['author'] = 'n/a';
    			if(!isset($widgets[$entry]['support'])) $widgets[$entry]['support'] = '';
    			$widgets[$entry]['support type'] = substr($widgets[$entry]['support'], 0, 6) == 'mailto' ? 'Email' : 'Website';
    			$widgets[$entry]['active'] = is_file($system_data['folder']['settings'] . 'widgets/' . $entry . '/DISABLED') ? 0 : 1;
    			$widgets[$entry]['writeable'] = is_writeable($system_data['folder']['widgets'] . $entry) ? 1 : 0;
    			$widgets[$entry]['size'] = file_exists($system_data['folder']['widgets'] . $entry) ? dirsize($system_data['folder']['widgets'] . $entry) : 0;
			}
		}
		
		closedir($open_widgets_folder);
		$package_count = count($widgets);
	}

/**
 * Get data for Modules
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

	if($_GET['area'] == 'modules' && $_SESSION['user'] == sha1($administrator_name)) {
		foreach($modules as $module) {
			$modules[$module['key']]['support type'] = substr($module['support'], 0, 6) == 'mailto' ? 'Email' : 'Website';
			$modules[$module['key']]['writeable'] = is_writeable($module['path']) ? 1 : 0;
			$modules[$module['key']]['size'] = file_exists($module['path']) ? dirsize($module['path']) : 0;
			$modules[$module['key']]['version conflict'] = str_replace('.', '', $module['pagemap version']) > str_replace('.', '', $system_data['system']['version']) ? 1 : 0;
		}
		
		$package_count = count($modules);
	}

/**
 * Get data for Extensions
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

	if($_GET['area'] == 'extensions' && $_SESSION['user'] == sha1($administrator_name)) {
		foreach($extensions as $extension) {
			$extensions[$extension['key']]['support type'] = substr($extension['support'], 0, 6) == 'mailto' ? 'Email' : 'Website';
			$extensions[$extension['key']]['writeable'] = is_writeable($system_data['folder']['extensions'] . $extension['key']) ? 1 : 0;
			$extensions[$extension['key']]['size'] = file_exists($system_data['folder']['extensions'] . $extension['key']) ? dirsize($system_data['folder']['extensions'] . $extension['key']) : 0;
			$extensions[$extension['key']]['version conflict'] = str_replace('.', '', $extension['pagemap version']) > str_replace('.', '', $system_data['system']['version']) ? 1 : 0;
		}
		
		$package_count = count($extensions);
	}

/**
 * Get data for Plugins
 *
 * @category   Extension (settings)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	if($_GET['area'] == 'plugins' && $_SESSION['user'] == sha1($administrator_name)) {
		$plugins = array();
		$open_plugins_folder = opendir($system_data['folder']['plugins']);
		
		while($entry = readdir($open_plugins_folder)) {
			if(is_dir($system_data['folder']['plugins'] . $entry) && $entry != "." && $entry != "..") {
				$plugins[$entry]['name'] = $entry;

				$info_file = $system_data['folder']['plugins'] . $entry . '/info.php';	
				if(file_exists($info_file)) include($info_file);

				if(!isset($plugins[$entry]['pagemap version'])) $plugins[$entry]['pagemap version'] = 0;
				if(!isset($plugins[$entry]['version'])) $plugins[$entry]['version'] = '';
				if(!isset($plugins[$entry]['author'])) $plugins[$entry]['author'] = 'n/a';
				if(!isset($plugins[$entry]['support'])) $plugins[$entry]['support'] = '';
				if(!isset($plugins[$entry]['description'])) $plugins[$entry]['description'] = '';
				$plugins[$entry]['version conflict'] = str_replace('.', '', $plugins[$entry]['pagemap version']) > str_replace('.', '', $system_data['system']['version']) ? 1 : 0;
				$plugins[$entry]['support type'] = substr($plugins[$entry]['support'], 0, 6) == 'mailto' ? 'Email' : 'Website';
				$plugins[$entry]['size'] = file_exists($system_data['folder']['plugins'] . $entry) ? dirsize($system_data['folder']['plugins'] . $entry) : 0;
			}
		}
		
		closedir($open_plugins_folder);
		$package_count = count($plugins);
	}
	
?>