<?php
/***************************************************************************
 *                          admin_cijoint_config.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', 1);

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['CiJoint']['CiJoint_manage'] = "$file?mode=manage";
	$module['CiJoint']['CiJoint_config'] = "$file?mode=config";
	return;
}

//
// Let's set the root dir for phpBB
//
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);

//
// 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 = $row['cijoint_urlallow'];
		$imgallow = $row['cijoint_imgallow'];
	}
}

if( isset($HTTP_GET_VARS['mode']) || isset($HTTP_POST_VARS['mode']) )
{
	$mode = ($HTTP_GET_VARS['mode']) ? $HTTP_GET_VARS['mode'] : $HTTP_POST_VARS['mode'];
}
else 
{
		$mode = "";
}


if( $mode != "" )
{
	if( $mode == "config" )
	{
		//
		// They want to config, show the form.
		//
		
		$template->set_filenames(array(
			"body" => "admin/cijoint_config_body.tpl")
		);

		$template->assign_vars(array(	
			"S_CONFIG_ACTION" => append_sid("admin_cijoint.$phpEx?mode=save"),		
			
			"L_CONFIG_TITLE" => $lang['CiJoint_title'].' : '.$lang['CiJoint_config'],
			"L_CONFIG_EXPLAIN" => $lang['CiJoint_config_explain'],
			"L_CONFIG_SETTINGS" => $lang['CiJoint_settings'],
			"L_FILEPATH" => $lang['CiJoint_filepath'],
			"L_FILEPATH_EXPLAIN" => $lang['CiJoint_filepath_explain'],
			"L_FILESIZE" => $lang['CiJoint_filesize'],
			"L_FILESIZE_EXPLAIN" => $lang['CiJoint_filesize_explain'],
			"L_EXT_ALLOW_URL" => $lang['CiJoint_url_allow'],
			"L_EXT_ALLOW_IMG" => $lang['CiJoint_img_allow'],
			"L_EXT_ALLOW_EXPLAIN" => $lang['CiJoint_allow_explain'],
			
			"FILEPATH" => $filepath,
			"FILESIZE" => $filesize,
			"EXT_ALLOW_URL" => $urlallow,
			"EXT_ALLOW_IMG" => $imgallow,
			
			"L_SUBMIT" => $lang['Submit'],
			"L_RESET" => $lang['Reset'] )
		);
		
	}
	else if( $mode == "save" )
	{
		//
		// Ok, they sent us some paramaters, let's update it.
		//
		
		$filesize = isset($HTTP_POST_VARS['filesize']) ? intval($HTTP_POST_VARS['filesize']) : 0;
		$filepath = isset($HTTP_POST_VARS['filepath']) ? trim($HTTP_POST_VARS['filepath']) : "";
		$urlallow = isset($HTTP_POST_VARS['urlallow']) ? trim($HTTP_POST_VARS['urlallow']) : "";
		$imgallow = isset($HTTP_POST_VARS['imgallow']) ? trim($HTTP_POST_VARS['imgallow']) : "";

		if( !@opendir($phpbb_root_path.$filepath) )
		{
			message_die(GENERAL_MESSAGE, sprintf($lang['CiJoint_path_unexist'],$filepath) );
		}
		else
		{
			$sql = "UPDATE " . CIJOINT_TABLE . " SET " .
				"cijoint_filepath = '" . str_replace("\'", "''", $filepath) . "', ".
				"cijoint_urlallow = '" . str_replace("\'", "''", $urlallow) . "', ".
				"cijoint_imgallow = '" . str_replace("\'", "''", $imgallow) . "', ".
				"cijoint_filesize = ". $filesize;
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "Failed to update general configuration for $config_name", "", __LINE__, __FILE__, $sql);
			}

		}
		$message = $lang['CiJoint_updated'];
		$message .= "<br /><br />" . sprintf($lang['CiJoint_return_config'], "<a href=\"" . append_sid("admin_cijoint.$phpEx?mode=config") . "\">", "</a>") . "<br /><br />" . sprintf($lang['Click_return_admin_index'], "<a href=\"" . append_sid("index.$phpEx?pane=right") . "\">", "</a>");

		message_die(GENERAL_MESSAGE, $message);

	}
	else if( $mode == "manage" )
	{
		$filepath = $phpbb_root_path.$filepath; // Relative path to admin folder
		
		//
		// A file must be deleted
		//		
		if ( isset($HTTP_POST_VARS['delete']) )
		{
			$filename = $HTTP_POST_VARS['delete'];
			$year = $HTTP_POST_VARS['year'];
			$month = $HTTP_POST_VARS['month'];
			$filename = $filepath . $year.'/'.$month.'/'.$filename;
			if (!@unlink ( $filename )) message_die(GENERAL_MESSAGE, $lang['CiJoint_unable_to_delete']);
		}
		//
		// Ok, they want to manage their files
		//		
		
		$year = isset($HTTP_POST_VARS['year']) ? $HTTP_POST_VARS['year'] : date("Y");
		$month = isset($HTTP_POST_VARS['month']) ? $HTTP_POST_VARS['month'] : date("m");
		
		$year_select = '<select name="year">';
		if ( !$hd = @opendir($filepath) ) message_die(GENERAL_MESSAGE, $lang['CiJoint_no_folderfound']);
		while ($year_dir = readdir($hd)) {
		    if ($year_dir != '.' && $year_dir != '..' && $year_dir != 'index.htm') {
			    $select = ( $year_dir == $year ) ? 'selected' : '';
		        $year_select .= '<option value="'. $year_dir .'" '. $select .'>'. $year_dir .'</option>';
		    }
		}
		closedir($hd);	
		$year_select .= '</select>';	
		
		$month_select = '<select name="month">';
		for ( $month_dir = 1; $month_dir < 13 ; $month_dir++ ) 
		{
			$month_dir = sprintf ("%02d", $month_dir);
			$select = ( $month_dir == $month ) ? 'selected' : '';
			$month_select .= '<option value="'. $month_dir .'" '. $select .'>'. $month_dir .'</option>';
		}
		$month_select .= '</select>';
				
		$template->set_filenames(array(
			"body" => "admin/cijoint_manage_body.tpl")
		);

		$template->assign_vars(array(	
			"S_MANAGE_ACTION" => append_sid("admin_cijoint.$phpEx?mode=manage"),		
			
			"L_MANAGE_TITLE" => $lang['CiJoint_title'].' : '.$lang['CiJoint_manage'],
			"L_MANAGE_EXPLAIN" => $lang['CiJoint_manage_explain'],
			"L_MANAGE_SETTINGS" => $lang['CiJoint_settings'],
			"L_YEAR" => $lang['CiJoint_year'],
			"L_MONTH" => $lang['CiJoint_month'],
			"L_FILE_SETTINGS" => $lang['CiJoint_filelist'],
			
			"YEAR_SELECT" => $year_select,
			"MONTH_SELECT" => $month_select,
						
			"L_SUBMIT" => $lang['Search'] )
		);
		
		//
		// Ok, they submit year and month
		//			
		$folder = $filepath . $year.'/'.$month.'/';
		$count_files = 0;

		if ( $hd = @opendir($folder) ) 
		{
			while ($filename = readdir($hd)) {
			    if ($filename != '.' && $filename != '..' && $filename != 'index.htm') {
				    // Test if this file exists in the _posts_text table
				    $sql = "SELECT post_id FROM " . POSTS_TEXT_TABLE . " " .
				    		"WHERE post_text LIKE '%".$filename."%' ";
					if (!$result = $db->sql_query($sql) )
					{
						message_die(CRITICAL_ERROR, "Could not query config information in admin_cijoint", "", __LINE__, __FILE__, $sql);
					}
					else
					{
						$row = $db->sql_fetchrow($result);
						if ( !$row['post_id'] )
						{
							// Only for image
	    					$filetype = getimagesize($folder.$filename);
	    					$filelink = $filetype[3] ? "<a href=\"$folder$filename\" target=\"_new\" />".$filename."</a>" : $filename;
	  	    					
	    					$template->assign_block_vars('filerow', array( 
	    						"S_MANAGE_ACTION" => append_sid("admin_cijoint.$phpEx?mode=manage"),
	    						"NAME" => $filename,
	    						"LINK" => $filelink,
	    						"YEAR" => $year,
	    						"MONTH" => $month,
	    						
	    						"L_SUBMIT" => $lang['Delete'] )
	    					);
	    					$count_files++;
						}
					}
	
			    }
			}
		closedir($hd);	
		}
		$template->assign_vars(array( 
			"L_COUNT_FILES" => $lang['CiJoint_count_files'].$count_files ));
	}

}
else
{
	//
	// Show default page
	//
	
	message_die(GENERAL_MESSAGE, $lang['Not_Authorised']);

}

$template->pparse("body");

include('./page_footer_admin.'.$phpEx);

?>
