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

/**
 * Copyright (C) Nico Wenig
 * All rights reserved
 *
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @filesource
 */



/**
 * Get content from element
 *
 * @category   Extension (edit content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	// Execute the PHP code string in content elements
    if(isset($content_elements) && isset($_GET['module-position'])) {
    	if(isset($content_elements[$_GET['module-position']])) {
    		if(preg_match("/<\?php(.+)\?>/s", $content_elements[$_GET['module-position']]['content'], $php_content) > 0) {
    			$html_content = preg_replace("/<\?php(.+)\?>/s", '', $php_content[0]);
    			$php_content[0] = '';
    			connector('edit-content get');
    			eval(preg_replace("/include_module\(.+\);/", '', implode('', $php_content)));
    		}
    	}
    }

/**
 * Save content element
 *
 * @category   Extension (edit content)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-04
 */

	if(isset($_POST['save_content']) || isset($_GET['save']) || isset($_POST['delete_content'])) {
		if(isset($content_elements) && isset($_GET['module-position'])) {
    		if(isset($_POST['save_content']))
    			$content_elements[$_GET['module-position']]['content'] = $module_content_to_save;
			
			if((isset($_POST['delete_content']) || isset($_GET['delete'])) && $_SESSION['user'] == sha1($administrator_name))
				$content_elements[$_GET['module-position']]['content'] = '';
			
			$page_content_to_save = (isset($_GET['move-to']) && $_GET['move-to'] == 0) ? "\n\n" . trim($content_elements[$_GET['module-position']]['content']) : '';
			
			for($i = 1; $i < count($content_elements); $i++) {
				$content_elements[$i]['content'] = isset($content_elements[$i]['content']) ? $content_elements[$i]['content'] : '';
				
				$page_content = '';
				if(isset($_GET['move-to'])) {
					if($i != $_GET['module-position']) $page_content .= "\n\n" . trim($content_elements[$i]['content']);
					if($i == $_GET['move-to']) $page_content .= "\n\n" . trim($content_elements[$_GET['module-position']]['content']);
				} else
					$page_content = "\n\n" . trim($content_elements[$i]['content']);

				$page_content_to_save .= $page_content;
			}
			
			connector('edit-content save');
			
			$page_index_file = $system_data['folder']['pages'] . $_GET['page'] . '/' . $system_data['file']['index page'];
			$open_page_index_file = fopen($page_index_file, 'wb');
			fputs($open_page_index_file, '<?php if(!defined("master_script")) die(); ?>');
			fputs($open_page_index_file, $page_content_to_save);
			fclose($open_page_index_file);
		}
	
		// Reload
		if(isset($_POST['delete_content']) || isset($_GET['delete']) || isset($_GET['close']))
			header('location: index.php?backend=edit-page&page=' . $_GET['page']);
		else
			header('location: index.php?backend=edit-content&page=' . $_GET['page'] . '&module-to-edit=' . $_GET['module-to-edit'] . '&module-position=' . $_GET['module-position']);
	}

?>