<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary 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)
############################################################################
# javaUpload.php: last changed 05/10/2006    S@S version 2.4.10                                 
#                      by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: javaUpload.php,v 1.2 2006/10/15 19:08:54 fstuurman Exp $
# this file is part of the allbum module
# Multiple file upload applet postlet used is from http://sourceforge.net/projects/postlet/
############################################################################
#                        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.
##############################################################################
//this file is called by the java postlet applet, so it runs on its own and is called
//every time an uploaded file is processed.

define('IN_SAS', true);
// include configuration file
require ("../../configuration/database.inc.php");
mysql_connect("$db_server", "$db_username", "$db_password");
mysql_select_db("$db_name");

require ("../../core/session.php"); //build a session
require ("../../core/common.inc.php");

# following code gets the session id from the URL . I need to do this because
# Postlet applet uses his own session id.
# the session id is used to get the S@S session code from the session table to see if we have a session.

if (IsSet ($_GET['PHPsessid']))
{
	$PHPsessid = $_GET['PHPsessid'];
}
$PHPsessid = strip_tags($PHPsessid); //remove any tags

if (ctype_alnum($PHPsessid)) //be sure to have a alphanum as sess id
{
	$session_table = $dbprefix . "sessions";
	$query = "SELECT ses_value FROM $session_table WHERE ses_id = '$PHPsessid'";
	$result = mysql_query($query);
	$ses_value = mysql_result($result, 0); //get the session value from the sn_session table

	//get postion of the sessioncode name
	$pos = strpos($ses_value, "sessioncode|s:");
	$ses_value = substr($ses_value, $pos +14); //add 14 to get to the first posistion of the length
	$pos = strpos($ses_value, ":"); //this is the position of the ":" after the length
	$length = substr($ses_value, 0, $pos); //length of the session code
	$ses_value = substr($ses_value, $pos +2, $length); //this is the contents of the session value 

	$query = "SELECT config_value FROM $table_configuration WHERE config_key='sessioncode'";
	$result = mysql_query($query);
	$check_sessioncode = mysql_result($result, 0); //get the session code from the configuration table

	if ($check_sessioncode != $ses_value) //compare the two values
	{
		print "Go away!"; //if we don't have a session , exit!
		exit;
	}
	else
	{
		unset ($sas_config); //clear the config array first
		$query = "SELECT config_key,config_value FROM $table_configuration WHERE config_key NOT LIKE 'agenda_t%'";
		//read all the configuration items except agenda options.

		$result = mysql_query($query);
		while ($config_list = mysql_fetch_array($result))
		{
			$sas_config[$config_list[0]] = $config_list[1]; //put them in the config array
		}

		// include shared functions:
		include ("shared.inc.php");
		// include private functions:
		include ("admin.inc.php");

		//path is different as in the allbum module because we run from this script 
		//instead of from the starnet/index.php
		$path = "../../" . $_GET['store_path'] . "/";
		$parent_ID = $_GET['ID'];

		$write_path = $_GET['store_path'];
		$file_name = $_FILES['userfile']['name'];
		$temp_file = $_FILES['userfile']['tmp_name']; //this is temporary uploaded file.	

		if (!file_exists($path . $file_name)) //only upload non existing files
		{
			sas_move_uploaded_file($write_path, $file_name, $temp_file);
			//move_uploaded_file($_FILES['userfile']['tmp_name'], $path.$file_name);

			// Get path to main parent:
			$parent = db_GetPath($parent_ID);
			$title = $file_name;
			if (strpos($title, ".")) //object title has extention?
			{
				$tmp = explode(".", $title);
				array_pop($tmp);
				$title = implode(".", $tmp);
			}
			$path = $_GET['store_path'] . "/";
			$descr = "";

			$filename = $_FILES['userfile']['name'];
			$tmp = explode(".", $filename);
			$file_ext = array_pop($tmp);

			if (strtoupper($file_ext) == "JPG")
			{
				$type = "picture";
				$dest = "../../" . $parent . "/" . $file_name;
				$thumb = ListThumb($dest); //make the thumb file
				$path = MakeThumb($thumb, $dest);
				$path = $_GET['store_path'] . "/"; //set path back
				$thumb = $file_name;
			}
			elseif ((strtoupper($file_ext) == "GIF") OR (strtoupper($file_ext) == "PNG"))
			{
				$type = "picture";
				$thumb = $file_name;
			}
			elseif (strtoupper($file_ext) == "SWF")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/swf.gif";
			}
			elseif (strtoupper($file_ext) == "MP3")
			{
				$type = "sound";
				$thumb = "modules/sn_allbum/sound.gif";
			}
			elseif (strtoupper($file_ext) == "WMA")
			{
				$type = "sound";
				$thumb = "modules/sn_allbum/winmedia.gif";
			}
			elseif (strtoupper($file_ext) == "WAV")
			{
				$type = "sound";
				$thumb = "modules/sn_allbum/sound.gif";
			}
			elseif (strtoupper($file_ext) == "MID")
			{
				$type = "sound";
				$thumb = "modules/sn_allbum/sound.gif";
			}
			elseif (strtoupper($file_ext) == "RA")
			{
				$type = "sound";
				$thumb = "modules/sn_allbum/sound.gif";
			}
			elseif (strtoupper($file_ext) == "AVI")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/movie.gif";
			}
			elseif (strtoupper($file_ext) == "RAM")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/movie.gif";
			}
			elseif (strtoupper($file_ext) == "MOV")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/mov.gif";
			}
			elseif (strtoupper($file_ext) == "MPG")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/movie.gif";
			}
			elseif (strtoupper($file_ext) == "MPEG")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/movie.gif";
			}
			elseif (strtoupper($file_ext) == "DOC")
			{
				$type = "doc";
				$thumb = "modules/sn_allbum/document.gif";
			}
			elseif (strtoupper($file_ext) == "PDF")
			{
				$type = "document";
				$thumb = "modules/sn_allbum/pdf.gif";
			}
			elseif (strtoupper($file_ext) == "WMV")
			{
				$type = "movie";
				$thumb = "modules/sn_allbum/movie.gif";
			}
			else
			{
				$type = "unknown";
				$thumb = "modules/sn_allbum/unknown.gif";
			}

			$allbum_table = $dbprefix . "m_allbum";
			$is_public = db_GetPublic($parent_ID); //get public bit of parent
			$thumb = remove_special_characters($thumb);
			db_PutCreateObject($type, $title, $descr, addslashes($path), $thumb, $parent_ID, '', '0', '0', $is_public);
		}
	}
}
?>
