<?php


namespace IPS\chatbox\modules\front\chatbox;

/* 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;
}

/**
 * chatbox
 */
class _chatbox extends \IPS\Dispatcher\Controller
{
	/**
	 * Execute
	 *
	 * @return	void
	 */
	public function execute()
	{
		parent::execute();
	}

	/**
	 * ...
	 *
	 * @return	void
	 */
	protected function manage()
	{
		\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack( 'chatbox_home' );
		\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'room', 'chatbox' )->home();
	}
	
	/*-------------------------------------------------------------------------*/
	// Update total online users in menu
	/*-------------------------------------------------------------------------*/	
	protected function tabCount()
	{
		if ( \IPS\Settings::i()->chatbox_tabCount == 1 )
		{
			\IPS\Output::i()->json( array( 'count' => \IPS\chatbox\Rooms\Room::onlineInRooms() ) );
		}
	}
	
	/*-------------------------------------------------------------------------*/
	// View Images
	/*-------------------------------------------------------------------------*/	
	protected function zoomImage()
	{
		\IPS\Session::i()->csrfCheck();
		
		
		try
		{
			$thumb = \IPS\File::get( 'chatbox_File', \IPS\Request::i()->thumb );
		}
		catch( \Exception $ex ) {}	
		
		if ( $thumb && $thumb->isImage() )
		{
			$location = (string) $thumb;
			
			try
			{
				$row = \IPS\Db::i()->select( '*', 'chatbox_files', array( 'file_from=? AND file_msg_id=? AND (file_thumb=? OR file_location=?)', \IPS\Request::i()->from, \IPS\Request::i()->chatID, $location, $location ) )->first();
				
				try
				{
					$file = \IPS\File::get( 'chatbox_File', $row['file_location'] );
					\IPS\Output::i()->json( array( 'image' => (string) $file->url ) );
				}
				catch( \Exception $ex ) { }				
			}
			catch ( \UnderflowException $e ){}	
		}
		
		\IPS\Output::i()->json( array( 'image' => \IPS\Request::i()->thumb ) );
	}
	
	/*-------------------------------------------------------------------------*/
	// Get Emoji
	/*-------------------------------------------------------------------------*/
	protected function emoji()
	{
		$setsToAdd = array();
		
		foreach ( \IPS\Db::i()->select( '*', 'core_emoticons', NULL, 'emo_set_position, emo_position' ) as $row )
		{
			$key = ( \IPS\Settings::i()->emoji_shortcodes and preg_match( '/^:.*:$/', $row['typed'] ) );

			try
			{
				$category = \IPS\Member::loggedIn()->language()->get( 'core_emoticon_group_' . $row['emo_set'] );
			}
			catch( \UnderflowException $e )
			{
				$category = '_default';
			}
			
			$setsToAdd[ $row['emo_set'] ][] = array(
				'code'			=> 'custom-' . $row['emo_set'] . '-' . $row['id'],
				'name'			=> $row['typed'],
				'shortNames'	=> $key ? array( trim( $row['typed'], ':' ) ) : array(),
				'ascii'			=> $key ? array() : array( $row['typed'] ),
				'categoryName'	=> $category,
				'image'			=> \IPS\File::get( 'core_Emoticons', $row['image'] )->url,
				'image2x'		=> $row['image_2x'] ? \IPS\File::get( 'core_Emoticons', $row['image_2x'] )->url : FALSE,
				'width'			=> $row['width'],
				'height'		=> $row['height'],
			);
		}
		foreach ( $setsToAdd as $set => $e )
		{
			$emoji[] = array( 'category' => $set, 'emoji' => $e );
		}		
		
		\IPS\Output::i()->json( $emoji );
	}		
}