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

/**
 * Actions
 *
 * @category   Plugin (actions)
 * @author     Nico Wenig
 * @version	   2010-01-07
 */

	$_GET['path'] = urldecode($_GET['path']);
	
	if(file_exists($_GET['path'])) {
		// Pathinfo
		if($_GET['action'] == 'pathinfo') {
			$filesize = is_dir($_GET['path']) ? dirsize($_GET['path']) : filesize($_GET['path']);
			echo txt('global', 'Größe') . ': ' . format_filesize($filesize);
		}
		
		// Upload
		if($_GET['action'] == 'upload') {
			$message = 0;
			
			if($_POST['dirpath'] == $system_data['folder']['pages']) $stop_uploading = 1;
			
			if(!isset($stop_uploading) && file_exists($_POST['dirpath']) && is_writeable($_POST['dirpath'])) {
				$new_file_name = transcribe($_FILES['file']['name']);
				$new_file_extension = strtolower(substr($new_file_name, strrpos($new_file_name, '.') + 1));
				$new_file_path = $_POST['dirpath'] . $new_file_name;
				
				// Check if user is admin
				$file_extension = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
				$invalid_file_extensions = $system_data['backend']['not allowed file extensions'];
				
				// Send status to extension
				if(!in_array($file_extension, $invalid_file_extensions))
					$message = move_uploaded_file($_FILES['file']['tmp_name'], $new_file_path) ? 1 : 0;
				else
					$message = txt('global', 'Dieser Dateityp ist nicht erlaubt.');
				
				// Unzip
				if(isset($_POST['unzip']) && $new_file_extension == 'zip') {
					$page_cache_path = $_POST['dirpath'] . 'cache/';
					$page_temp_path = $page_cache_path . random_string() . '/';
					
					// Create cache and temp dirs
					if(!file_exists($page_cache_path)) mkdir($page_cache_path);
					if(!file_exists($page_temp_path)) mkdir($page_temp_path);
					
					// Extract ZIP to temp
					include_once($system_data['folder']['plugins'] . 'pclzip/pclzip.lib.php');
					
					$zip = new PclZip($new_file_path);
					$zip->extract(PCLZIP_OPT_PATH, $page_temp_path, PCLZIP_OPT_REMOVE_ALL_PATH);
					if(file_exists($new_file_path)) unlink($new_file_path);
					
					// Move valid files to page path
					$open_temp_path = opendir($page_temp_path);
					
					while($entry = readdir($open_temp_path)) {
						if(is_file($page_temp_path . $entry)) {
							$file_extension = substr($entry, strrpos($entry, '.') + 1);
							
							if(!in_array($file_extension, $invalid_file_extensions))
								copy($page_temp_path . $entry, $_POST['dirpath'] . transcribe($entry));
							else
								$message = txt('global', 'Dieser Dateityp ist nicht erlaubt.');
						}
					}
					
					closedir($open_temp_path);
					rec_rmdir($page_temp_path);
				}
			} else
				$message = txt('global', 'Das Verzeichnis existiert nicht oder ist schreibgeschützt.');
			
			if($_POST['dirpath'] == $system_data['folder']['pages']) $message = txt('global', 'Please choose a subdirectory.');
			if(transcribe($_FILES['file']['name']) == '') $message = txt('global', 'Bitte wählen Sie eine gültige Datei.');

			echo '
				<div id="message">' . $message . '</div>
				<script type="text/javascript">window.top.filetreeUpload()</script>
			';
		}

		// New dir
		if($_GET['action'] == 'newdir') {
			if(!is_dir($_GET['path'] . $_GET['dirname'])) {
				if(!mkdir($_GET['path'] . $_GET['dirname']))
					echo txt('global', 'Das Verzeichnis konnte nicht erstellt werden.');
			}
		}

		// Rename
		if($_GET['action'] == 'rename') {
			$old_filename = $_GET['path'] . $_GET['oldname'];
			$new_filename = $_GET['path'] . $_GET['newname'];
			$old_xml_filename = $_GET['path'] . 'storage/' . $_GET['oldname'] . '.xml';
			$new_xml_filename = $_GET['path'] . 'storage/' . $_GET['newname'] . '.xml';
			
			if($new_filename == $old_filename || !file_exists($new_filename)) {
				if(rename($old_filename, $new_filename)) {
					if(is_file($old_xml_filename))
						rename($old_xml_filename, $new_xml_filename);
				} else {
					echo txt('global', 'Die Datei konnte nicht umbenannt werden.');
				}
			} else
				echo txt('global', 'Der Dateiname wird bereits verwendet.');
		}
		
		// Delete
		if($_GET['action'] == 'delete' && $_SESSION['user'] == sha1($administrator_name)) {
			if(file_exists($_GET['path'] . $_GET['name'])) {
				$pages_trashcan = $system_data['folder']['settings'] . 'extensions/trashcan/files/';
				
				if(is_file($_GET['path'] . 'storage/' . $_GET['name'] . '.xml')) unlink($_GET['path'] . 'storage/' . $_GET['name'] . '.xml');
				if(!is_dir($pages_trashcan . $_GET['path'])) rmkdir($pages_trashcan . $_GET['path']);
				
				if(is_dir($_GET['path'] . $_GET['name']) && is_dir($pages_trashcan . $_GET['path'] . $_GET['name']))
					rec_rmdir($pages_trashcan . $_GET['path'] . $_GET['name']);
				
				rename($_GET['path'] . $_GET['name'], $pages_trashcan . $_GET['path'] . $_GET['name']);
			}
		}
	} else {
		// General error
		echo txt('global', 'Die Datei konnte nicht gefunden werden.');
	}
	
?>