<?php
/**
 * @brief		Community Enhancements

 * @since		11 Jan 2022
 */

namespace IPS\core\extensions\core\CommunityEnhancements;

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

/**
 * Community Enhancement
 */
class _IndexNow
{
	/**
	 * @brief	Enhancement is enabled?
	 */
	public $enabled	= FALSE;

	/**
	 * @brief	IPS-provided enhancement?
	 */
	public $ips	= FALSE;

	/**
	 * @brief	Enhancement has configuration options?
	 */
	public $hasOptions	= FALSE;

	/**
	 * @brief	Icon data
	 */
	public $icon	= "";
	
	/**
	 * Constructor
	 *
	 * @return	void
	 */
	public function __construct()
	{
		$this->enabled = \IPS\Settings::i()->indexnow_enabled;
	}
	
	/**
	 * Enable/Disable
	 *
	 * @param	$enabled	bool	Enable/Disable
	 * @return	void
	 * @throws	\LogicException
	 */
	public function toggle( $enabled )
	{
		/* If we're disabling, just disable */
		if( !$enabled )
		{
			\IPS\Settings::i()->changeValues( array( 'indexnow_enabled' => 0 ) );
			\IPS\Settings::i()->changeValues( array( 'indexnow_key' => '' ) );
		}
		else
		{
			$key = \IPS\Login::generateRandomString();

			\IPS\Settings::i()->changeValues( array( 'indexnow_enabled' => 1 ) );
			\IPS\Settings::i()->changeValues( array( 'indexnow_key' => $key ) );
		}
	}
}