<?
	set_time_limit(0);
	
	require_once('user.php');
	require_once('mysql.php');
	require_once('functions.php');
	require_once('thumbnail.php');
	require_once('fb.php');
	
	ob_start();
	
	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
	
	$_GET['name'] = cleanFileName($_GET['name']);
	
	if ($_GET['insert']) {
		
		$type = 'file';
		if (is_image('../../'.$fileDIR.$_GET['name'])) {
			$type = 'image';
			fb('IMAGE DETECTED: '.$_GET['name']);
		}
		
		$query = "SELECT * FROM file WHERE name='".$_GET['name']."'";
		fb('CHECK: '.$query);
		$mysqldb1->query($query);
		$existing_rows = $mysqldb1->affectedRows();
		
		$status = 'enabled';
		
		// INSERT NEW FILE RECORD
		$query = "
		INSERT INTO 
			file 
		SET 
			record_id=".$_GET['record_id'].", 
			type='".$type."', 
			source='".$_GET['source']."', 
			name='".$_GET['name']."', 
			size=".$_GET['size'].", 
			sequence=0, 
			status='".$status."'";
		
		// UPDATE EXISTING FILE RECORD (NOT USED YET)
		if ($existing_rows > 0) {
			$query = "
			UPDATE 
				file 
			SET 
				record_id=".$_GET['record_id'].", 
				source='".$_GET['source']."', 
				size=".$_GET['size']." 
			WHERE
				name='".$_GET['name']."'";
		}
		
	}
	
	fb('FILE TYPE: '.$type);
	fb('INSERT: '.$query);
	if ($mysqldb1->query($query)) {
		fb('FILE RECORD CREATED');
		
		if ($type == 'image') {
			
			make_thumbnail('../../'.$fileDIR.$_GET['name'], substr($_GET['name'], 0, -4), '../../'.$tinyDIR, $tiny_image_size, $_GET['protect']);
			make_thumbnail('../../'.$fileDIR.$_GET['name'], substr($_GET['name'], 0, -4), '../../'.$smallDIR, $small_image_size, $_GET['protect']);
			make_thumbnail('../../'.$fileDIR.$_GET['name'], substr($_GET['name'], 0, -4), '../../'.$mediumDIR, $medium_image_size, $_GET['protect']);	
			make_thumbnail('../../'.$fileDIR.$_GET['name'], substr($_GET['name'], 0, -4), '../../'.$largeDIR, $large_image_size, $_GET['protect']);
			
			fb('MAKE THUMBNAILS OF: '.$_GET['name']);
		}
		
	} else {
		fb('ERROR! FILE RECORD NOT CREATED');
	}
?>
<?
	$mysqldb1->close();
?> 