<?php

#############################################################################
#                              Site@School
# Site@School is a website management system designed for schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1 - 2.4): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# browser.php: last changed 04/10/2006  , version: 2.4.10
#            by Fred Stuurman  (fc.stuurman@hccnet.nl)
#$Id: browser.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
# this file is part of the FCK filemanager created for S@S
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2006 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
define('IN_SAS', true);

if (IsSet ($_GET['option']))
{
	$option = $_GET['option'];
}
else
{
	$option = "";
}

include ("../../../../../configuration/database.inc.php"); // connecting to database
mysql_connect("$db_server", "$db_username", "$db_password");
mysql_select_db("$db_name");

include ("../../../../../core/common.inc.php");
include ("../../../../../core/session.php");

if (IsSet ($_GET['type'])) //type can be image, flash or link
{
	$_SESSION['type'] = $_GET['type'];
}
else
{
	$_SESSION['type'] = "image";
}

$query = "SELECT config_value FROM $table_configuration WHERE config_key='sessioncode'";
if ($result = mysql_query($query))
{
	$check_sessioncode = mysql_result($result, 0);
	unset ($query);
	unset ($result);
}
if ($_SESSION['sessioncode'] != $check_sessioncode)
{
	//if we don't have a session present the login screen
	Header("Location: ../../../../../index.php");
	exit;
}

$query = "SELECT config_value FROM $table_configuration WHERE config_key='serverpath'";
$result = mysql_query($query) or die_script(mysql_error());
$sas_basedir = mysql_result($result, 0);
unset ($query);
unset ($result);

$query = "SELECT config_value FROM $table_configuration WHERE config_key='user_path'";
$result = mysql_query($query) or die_script(mysql_error());
$user_path = mysql_result($result, 0);
if (substr($user_path, 0, 1) == "/")
{
	$user_path = substr($user_path, 1); //strip first slash
}
unset ($query);
unset ($result);

$query = "SELECT config_value FROM $table_configuration WHERE config_key='file_permissions'";
$result = mysql_query($query) or die_script(mysql_error());
$perm = mysql_result($result, 0);
unset ($query);
unset ($result);

$query = "SELECT config_value FROM $table_configuration WHERE config_key='language'";
$result = mysql_query($query) or die_script(mysql_error());
$language = mysql_result($result, 0);
include ("../../../../../languages/$language/$language.php");

if (($option == "new_directory") AND (trim($_POST['new_directory']) != "")) //check for empty field
{
	$str_from = "/ \'@!,/\\\t\*?`\"";
	$str_to = str_repeat("_", strlen($str_from));
	$new_directory = strtr($_POST['new_directory'], $str_from, $str_to); //replace special characters with underscore

	$new_directory = $sas_basedir . $user_path . "/" . $_SESSION['user_media_path'] . $_SESSION['opendir'] . "/" . $new_directory;

	mkdir($new_directory, 0755);
	chmod($new_directory, octdec($perm)); //chmod it with permissions stored in configuration table.
	$opendir = $_SESSION['opendir']; //for returning to the directory were we came from
}

if ($option == "upload")
{
	if (IsSet ($_FILES["new_file"]["name"]))
	{
		$file_name = $_FILES["new_file"]["name"];
	}
	if (IsSet ($_SESSION['opendir']))
	{
		$write_path = $_SESSION['user_media_path'] . "/" . $_SESSION['opendir'];
		// moveupload the file to $write_path, function is in core/common.inc.php
		$temp_file = $_FILES["new_file"]["tmp_name"]; //this is temporary uploaded file.	
		sas_move_uploaded_file($write_path, $file_name, $temp_file);
	}
	$opendir = $_SESSION['opendir']; //for returning to the directory were we came from	
}
?>

<html>
	<head>
		<title><?php echo $sas_lang['filemanager'] ?></title>
		<link href="fck_dialog.css" type="text/css" rel="stylesheet">

<style type="text/css">

<!--
.title { 
color: #000; font-weight: bold; font-size: 120%; padding: 2px 10px; 
letter-spacing: 2px;
}
-->
</style>
</head>
<body>
<table width="100%" cellspacing="0" cellpadding="0" style="border: 1px solid threedface;" bordercolor="#111111">
	<tr>
		<td align="center" bgcolor="#e3e3c7"><div class="PopupTitle"><?php echo $sas_lang['filemanager'] ?></div></td>
	</tr>
	<tr>
		<td>
		<iframe name="images" style="width:100%;height:355" src="images.php?opendir=<?php echo $opendir ?>" scrolling="auto" frameborder="NO" leftmargin="0" topmargin="0" rightmargin="0"></iframe>
		</td>
	</tr>
	<tr>
		<td>
			&nbsp;
		</td>
	</tr>	
	<tr>
		<td bgcolor="#e3e3c7">
		<form id="frmUpload" action="browser.php?option=upload&type=<?php echo $_SESSION['type'] ?>" method="post" enctype="multipart/form-data">
			<table height="100%" cellSpacing="2" cellPadding="0" width="100%">
				<tr>
					<td nowrap><?php echo $sas_lang['uploadfile'] ?><br>
						<table cellSpacing="0" cellPadding="0" width="100%" border="0">
							<tr>
								<td width="50%">
									<input id="new_file" name="new_file" size="80" style="width: 100%; style="background : #e3e3c7; color : white" type="file">
								</td>
								<td nowrap>&nbsp;<input type="submit" value="<?php echo $sas_lang['uploadfile'] ?>"></td>
								<td width="40%">&nbsp;</td>
							</tr>
							<tr>
							<td colspan="3">&nbsp;</td>
							</tr>
							<tr>
							<td colspan="3"><?php echo $sas_lang['createdir'] ?></td>
							</tr>
		</form>
		<form id="frmUpload" action="browser.php?option=new_directory" method="post" enctype="multipart/form-data">
							<tr>
								<td width="50%">
									<input id="new_directory" name="new_directory" size="80" style="style="background : #e3e3c7; color : white" type="text">
									
								</td>
								<td colspan="2">&nbsp;<input type="submit" value="<?php echo $sas_lang['createdir'] ?>"></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</form>
	</td>
	</tr>
</table>
</body>
</html>