<?php
/**
 * @brief		File Storage Extension: Nofifile
 * @author		<a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
 * @copyright	(c) Invision Power Services, Inc.
 * @license		https://www.invisioncommunity.com/legal/standards/
 * @package		Invision Community
 * @subpackage	Forums
 * @since		06 Feb 2014
 */

namespace IPS\chatbox\extensions\core\FileStorage;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * File Storage Extension: Nofifile
 */
class _Nofifile
{
	/**
	 * Count stored files
	 *
	 * @return	int
	 */
	public function count()
	{
		$count = \IPS\Db::i()->select( 'COUNT(*)', 'chatbox_rooms', 'room_notifile IS NOT NULL' )->first();
		
		if( \IPS\Settings::i()->chatbox_con_notifile )
		{
			$count += 1;
		}
		
		if( \IPS\Settings::i()->room_clubs_notifile )
		{
			$count += 1;
		}	
		
		return $count;
	}
	
	/**
	 * Move stored files
	 *
	 * @param	int			$offset					This will be sent starting with 0, increasing to get all files stored by this extension
	 * @param	int			$storageConfiguration	New storage configuration ID
	 * @param	int|NULL	$oldConfiguration		Old storage configuration ID
	 * @throws	\Underflowexception				When file record doesn't exist. Indicating there are no more files to move
	 * @return	void
	 */
	public function move( $offset, $storageConfiguration, $oldConfiguration=NULL )
	{
		$room = \IPS\chatbox\Rooms\Room::constructFromData( \IPS\Db::i()->select( '*', 'chatbox_rooms', 'room_notifile IS NOT NULL', 'id', array( $offset, 1 ) )->first() );
		
		try
		{
			$room->notifile = \IPS\File::get( $oldConfiguration ?: 'chatbox_Nofifile', $room->notifile )->move( $storageConfiguration );
			$room->save();
		}
		catch( \Exception $e )
		{
			/* Any issues are logged */
		}
		
		if( \IPS\Settings::i()->chatbox_con_notifile )
		{
			\IPS\File::get( $oldConfiguration ?: 'chatbox_Nofifile', \IPS\Settings::i()->chatbox_con_notifile )->move( $storageConfiguration );
		}
		
		if( \IPS\Settings::i()->room_clubs_notifile )
		{
			\IPS\File::get( $oldConfiguration ?: 'chatbox_Nofifile', \IPS\Settings::i()->room_clubs_notifile )->move( $storageConfiguration );
		}		
	}
	
	/**
	 * Fix all URLs
	 *
	 * @param	int			$offset					This will be sent starting with 0, increasing to get all files stored by this extension
	 * @return void
	 */
	public function fixUrls( $offset )
	{
		$room = \IPS\chatbox\Rooms\Room::constructFromData( \IPS\Db::i()->select( '*', 'chatbox_rooms', 'room_notifile IS NOT NULL', 'room_id', array( $offset, 1 ) )->first() );
		
		if ( $new = \IPS\File::repairUrl( $room->notifile ) )
		{
			$room->notifile = $new;
			$room->save();
		}
		
		$updated = array();
		
		if( \IPS\Settings::i()->chatbox_con_notifile )
		{
			$updated['chatbox_con_notifile'] = \IPS\File::repairUrl( \IPS\Settings::i()->chatbox_con_notifile );
		}
		
		if( \IPS\Settings::i()->room_clubs_notifile )
		{
			$updated['room_clubs_notifile'] = \IPS\File::repairUrl( \IPS\Settings::i()->room_clubs_notifile );
		}	
		
		if( $updated )
		{
			\IPS\Settings::i()->changeValues( $updated );
		}		
	}
	
	/**
	 * Check if a file is valid
	 *
	 * @param	string	$file		The file path to check
	 * @return	bool
	 */
	public function isValidFile( $file )
	{
		if( \IPS\Settings::i()->chatbox_con_notifile AND $file == \IPS\Settings::i()->chatbox_con_notifile )
		{
			return TRUE;
		}
		
		if( \IPS\Settings::i()->room_clubs_notifile AND $file == \IPS\Settings::i()->room_clubs_notifile )
		{
			return TRUE;
		}
		
		try
		{
			\IPS\Db::i()->select( 'room_id', 'chatbox_rooms', array( 'room_notifile=?', $file ) )->first();
			return TRUE;
		}
		catch ( \UnderflowException $e )
		{
			return FALSE;
		}
	}

	/**
	 * Delete all stored files
	 *
	 * @return	void
	 */
	public function delete()
	{
		if( \IPS\Settings::i()->chatbox_con_notifile )
		{
			try
			{
				\IPS\File::get( 'chatbox_Nofifile', \IPS\Settings::i()->chatbox_con_notifile )->delete();
			}
			catch( \OutOfRangeException $e ){}
		}
		
		if( \IPS\Settings::i()->room_clubs_notifile )
		{
			try
			{
				\IPS\File::get( 'chatbox_Nofifile', \IPS\Settings::i()->room_clubs_notifile )->delete();
			}
			catch( \OutOfRangeException $e ){}
		}
		
		foreach( \IPS\Db::i()->select( '*', 'chatbox_rooms', "room_notifile IS NOT NULL" ) as $room )
		{
			try
			{
				\IPS\File::get( 'chatbox_Nofifile', $room['room_notifile'] )->delete();
			}
			catch( \Exception $e ){}
		}
	}
}