<?php
/***************************************************************************
 *                                cijoint.php
 *                            -------------------
 *   begin                : May, 10th 2007
 *   copyright            : (C) Beaussier
 *   email                : bidouille@beaussier.com
 *
 *   version 1.0
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   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.
 *
 ***************************************************************************/

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_PROFILE);
init_userprefs($userdata);
$gen_simple_header = TRUE;

$page_title = $lang['CiJoint'];
include($phpbb_root_path . 'includes/page_header.'.$phpEx);

//
// End session management
//

// session id check
if (!empty($HTTP_POST_VARS['sid']) || !empty($HTTP_GET_VARS['sid']))
{
	$sid = (!empty($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : $HTTP_GET_VARS['sid'];
}
else
{
	$sid = '';
}

$template->set_filenames(array(
	'cijoint' => 'cijoint.tpl')
);

//
// Get all config datas
//
$sql = "SELECT *
	FROM " . CIJOINT_TABLE;
if(!$result = $db->sql_query($sql))
{
	message_die(CRITICAL_ERROR, "Could not query config information in admin_cijoint", "", __LINE__, __FILE__, $sql);
}
else
{
	while( $row = $db->sql_fetchrow($result) )
	{
		$filepath = $row['cijoint_filepath']; 
		$filesize = $row['cijoint_filesize'];
		$urlallow = split(',', $row['cijoint_urlallow']);
		$imgallow = split(',', $row['cijoint_imgallow']);
	}
}

$url = "http://".$board_config['server_name'].$board_config['script_path'];
$mode = 0711; // Change mod for subdirectory
$tag = 	"bad"; // Set BbCode default tag

//if ( !$userdata['session_logged_in'] ) 


$template->assign_vars(array(
	'L_TITLE' => $lang['CiJoint'],
	'L_CLOSE_WINDOW' => $lang['Close_window'] )
);

$filesend = isset($HTTP_POST_FILES ['filesend']) ? $HTTP_POST_FILES ['filesend'] : false;

if ( !$filesend ) {
	$template->assign_block_vars('input', array( 
		'S_ACTION' => append_sid("cijoint.$phpEx?mode=send"),
		'FILESIZE' => $filesize*1000,
		'L_SUBMIT' => $lang['Submit'] )
	);
	$template->assign_var('L_EXPLAIN', sprintf($lang['CiJoint_explain'], $filesize));
}
else
{
	// File received
	$explain = $lang['CiJoint_upload_success'];
	if ($filesend['error']) $explain = $lang['CiJoint_file_error'];
	if ($filesend['size'] > $filesize*1000) $explain = $lang['CiJoint_filesize_error'];
	
	$ext = strtolower (substr ($filesend['name'], -3));
	if (in_array($ext, $urlallow)) $tag="url";
	if (in_array($ext, $imgallow)) $tag="img";
	
	if ($tag=="bad") 
		$explain = $lang['CiJoint_filetype_error'];
	else
	{
		// Set subdirectoy
		$dir_y = date("Y"); $dir_m = date("m");
		$file2store = $filepath . $dir_y;
		@mkdir ($file2store); 
		chmod ($file2store, $mode);
		if (!file_exists ($file2store."/index.htm")) 
			copy ($filepath."index.htm",$file2store."/index.htm");
		$file2store .= "/".$dir_m;
		@mkdir ($file2store);
		chmod ($file2store, $mode);
		if (!file_exists ($file2store."/index.htm")) 
			copy ($filepath."index.htm",$file2store."/index.htm");
		$file2store .= "/";
		// Set filename
		$filemove = date("d-his").".".$ext;	
		// Move file and chmod
		move_uploaded_file ($filesend['tmp_name'], $file2store.$filemove);
	    chmod ($file2store.$filemove, 0644);
	    $link_code = "[".$tag."]".$url.$file2store.$filemove."[/".$tag."]";
	}
	
	$template->assign_block_vars('output', array( 
		'S_ACTION' => append_sid("cijoint.$phpEx"),	
		'L_SUBMIT' => $lang['CiJoint_again'],
		'LINK_CODE' => $link_code )
	);

	$template->assign_var('L_EXPLAIN', $explain);
}
	
	
$template->pparse('cijoint');
include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

?>