File "document.php"

Full Path: /home/romayxjt/public_html/wp-content/plugins/vikbooking/admin/helpers/src/http/document.php
File size: 2.18 KB
MIME-type: text/x-php
Charset: utf-8

<?php
/** 
 * @package     VikBooking
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Helper class used to handle the interaction with HTTP documents.
 * 
 * @since 1.5
 */
class VBOHttpDocument
{
	/**
	 * A reference to the application object.
	 * 
	 * @var JApplication
	 */
	protected $app;

	/**
	 * Proxy used to construct the object.
	 * 
	 * @param 	mixed  $app  The application instance. If not specified the
	 *                       current one will be used.
	 * 
	 * @return 	self   A new instance of this class.
	 */
	public static function getInstance($app = null)
	{
		if (!$app)
		{
			$app = JFactory::getApplication();
		}

		return new static($app);
	}

	/**
	 * Class constructor.
	 * 
	 * @param 	JApplication  $app  The application instance.
	 */
	public function __construct($app)
	{
		$this->app = $app;
	}

	/**
	 * Method to close the application.
	 *
	 * @param 	integer  $code    The HTTP status code.
	 * @param   mixed    $buffer  An optional string to display.
	 *
	 * @return  void
	 */
	public function close($code = 200, $buffer = null)
	{
		// force HTTP status code
		$this->app->setHeader('status', $code, $replace = true);
		$this->app->sendHeaders();

		if (VBOPlatformDetection::isWordPress() && !headers_sent())
		{
			// this is necessary for the HTTP2 protocol
			http_response_code($code);
		}

		if ($buffer)
		{
			// display buffer
			echo $buffer;
		}

		// terminate session
		$this->app->close();
	}

	/**
	 * Echoes the given JSON by using the right content type.
	 *
	 * @param 	mixed  $json  Either a JSON string or a non-scalar value.
	 * @param 	int    $flags Bitmask for json_encode().
	 *
	 * @return 	void
	 */
	public function json($json, $flags = 0)
	{
		$this->app->setHeader('Content-Type', 'application/json', $replace = true);

		if (!is_string($json))
		{
			// stringify array/object
			$json = json_encode($json, $flags);
		}

		// terminate session by echoing the given JSON
		$this->close(200, $json);
	}
}