<?php

/**
 * Load backend
 *
 * @category   Backend (start)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-07-11
 */

	if(!defined('master_script')) header('location: ../index.php?backend');

/**
 * Get users from user file, define administrator and logout wrong users
 *
 * @category   Backend (user data)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2011-10-26
 */

	$file_users_array = file_exists($system_data['file']['users']) ? file($system_data['file']['users']) : array();
	$users = array();
    
    for($i = 0; $i < count($file_users_array); $i++) {
    	$parts = explode(':', $file_users_array[$i]);
    	$users[trim($parts[0])] = trim($parts[1]);
    	
    	if($i == 0) $administrator_name = $parts[0];
    	if(isset($_SESSION['user']) && sha1($parts[0]) == $_SESSION['user']) $user_name = $parts[0];
    }
    
    if(isset($_SESSION['user']) && $user_name == '') $_GET['logout'] = true;

/**
 * Logout user
 *
 * @category   Backend (user data)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-04-21
 */

	if(isset($_GET['logout'])) {
		if(isset($_SESSION['user'])) unset($_SESSION['user']);
		session_destroy();
		header('location: index.php?backend');
	}

/**
 * Load plugin actions file
 *
 * @category   System start
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-15
 */

	if(isset($_GET['actions']) && strpos($_GET['actions'], '..') === false && isset($_SESSION['user'])) {
		$actions_file = $system_data['folder']['plugins'] . 'actions/' . $_GET['actions'];
		if(is_file($actions_file)) include($actions_file);
		exit();
	}

/**
 * Set default backend page
 *
 * @category   Backend (start)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	if(isset($_SESSION['user'])) {
		if($_GET['backend'] == '') $_GET['backend'] = 'widgets';
	} else $_GET['backend'] = 'login';

/**
 * Get list of available modules
 *
 * @category   Backend (modules)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

    $folder_modules_open = opendir($system_data['folder']['modules']);
   	
   	while($entry = readdir($folder_modules_open)) {
   		if(is_dir($system_data['folder']['modules'] . $entry) && $entry != "." && $entry != "..") {
    		$module_index = $system_data['folder']['modules'] . $entry . '/backend-module.php';
    		
    		if(file_exists($module_index)) {
    			include($module_index);
    			$modules[$entry]['active'] = is_file($system_data['folder']['settings'] . 'modules/' . $entry . '/DISABLED') ? 0 : 1;
    		}
   		}
   	}
    
    closedir($folder_modules_open);

/**
 * Get module data for page content elements
 *
 * @category   Backend (modules)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-04
 */

	if(isset($_GET['page'])) {
		$page_file_contents = file_exists($page_info->index) ? file($page_info->index) : array();
		
		$content_elements[0]['content'] = '';
    	
    	$i = 0;

    	foreach($page_file_contents as $entry) {
    		if(preg_match("/<!-- MODULE: (.+) -->/", $entry, $content_element) > 0) {
    			$module_name = trim($content_element[1]);
				$module_index = $system_data['folder']['modules'] . $module_name . '/backend-module.php';
				
				if(file_exists($module_index)) {
	   				$i++;
	   				$content_elements[$i]['name'] = $module_name;
	   				$content_elements[$i]['title'] = txt($module_name, 'title: ' . $module_name);
	   				$content_elements[$i]['description'] = txt($module_name, 'description: ' . $module_name);
	   				$content_elements[$i]['position'] = $i;
					$content_elements[$i]['content'] = $entry;
    			}
    		} else {
    			$content_elements[$i]['content'] .= $entry;
    		}
		}
	}

/**
 * Include executeable PHP file for loaded extension
 *
 * @category   Backend (extensions)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-21
 */

	$extension_execute_file = $system_data['folder']['extensions'] . $_GET['backend'] . '/index.php';
	$extension_disabled = is_file($system_data['folder']['settings'] . 'extensions/' . $_GET['backend'] . '/DISABLED') ? true : false;
	
	if(file_exists($extension_execute_file) && $extension_disabled == false) include($extension_execute_file);

/**
 * Include backend template
 *
 * @category   Backend (template)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	include($system_data['folder']['backend'] . 'index.tpl');
	
?>