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

/**
 * Get pages
 *
 * @category   Extension (trashcan)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-04
 */
	
	$folder_trashcan = $system_data['folder']['settings'] . 'extensions/trashcan/';
	$folder_trashcan_pages = $folder_trashcan . 'pages/';
	$trashcan_pages_list = dirlist($folder_trashcan_pages, array(), true);
	
	foreach($trashcan_pages_list as $entry) {
		$entry_info_file = $entry . '/' . $system_data['file']['page info file'];
		$trashcan_pages[$entry] = is_file($entry_info_file) ? simplexml_load_file($entry_info_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><page></page>');
		$trashcan_pages[$entry]->path = $entry;
	}

/**
 * Get files
 *
 * @category   Extension (trashcan)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-06
 */
	
	$folder_trashcan_files = $folder_trashcan . 'files/';
	$trashcan_files_list = filelist($folder_trashcan_files);
	
	foreach($trashcan_files_list as $entry) {
		$basename = basename($entry);
		$trashcan_files[$basename]['basename'] = $basename;
		$trashcan_files[$basename]['timestamp'] = fileatime($entry);
		$trashcan_files[$basename]['path'] = $entry;
		$trashcan_files[$basename]['filetype'] = substr($basename, strrpos($basename, '.') + 1);
	}
	
/**
 * Delete folder or file
 *
 * @category   Extension (trashcan)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-06
 */

	if(isset($_GET['delete']) && file_exists($_GET['delete'])) {
		is_file($_GET['delete']) ? unlink($_GET['delete']) : rec_rmdir($_GET['delete']);
		header('location: index.php?backend=trashcan');
	}

/**
 * Recycle file
 *
 * @category   Extension (trashcan)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-06
 */

	if(isset($_GET['recycle']) && file_exists($_GET['recycle'])) {
		$target_file = str_replace($folder_trashcan_files, '', $_GET['recycle']);
		$target_path = dirname($target_file);
		
		if(!is_dir($target_path)) rmkdir($target_path);
		
		rename($_GET['recycle'], $target_file);
		header('location: index.php?backend=trashcan');
	}

/**
 * Empty trashcan
 *
 * @category   Extension (trashcan)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-06
 */
	
	// Check if trashcan is empty
	$trashcan_is_empty = 1;
	$trashcan_file_list = filelist($folder_trashcan);
	foreach($trashcan_file_list as $entry) $trashcan_is_empty = 0;
	
	// Clean up trashcan if no files found
	if($trashcan_is_empty == 1) rec_rmdir($folder_trashcan);
	
	// Delete trashcan folder
	if(isset($_POST['delete-all']) && $_SESSION['user'] == sha1($administrator_name)) {
		rec_rmdir($folder_trashcan);
		header('location: index.php?backend=trashcan');
	}
	
?>