| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Helper class used to handle the interaction with HTTP documents. |
| 16 |
* |
| 17 |
* @since 1.5 |
| 18 |
*/ |
| 19 |
class VBOHttpDocument |
| 20 |
{ |
| 21 |
/** |
| 22 |
* A reference to the application object. |
| 23 |
* |
| 24 |
* @var JApplication |
| 25 |
*/ |
| 26 |
protected $app; |
| 27 |
|
| 28 |
/** |
| 29 |
* Proxy used to construct the object. |
| 30 |
* |
| 31 |
* @param mixed $app The application instance. If not specified the |
| 32 |
* current one will be used. |
| 33 |
* |
| 34 |
* @return self A new instance of this class. |
| 35 |
*/ |
| 36 |
public static function getInstance($app = null) |
| 37 |
{ |
| 38 |
if (!$app) |
| 39 |
{ |
| 40 |
$app = JFactory::getApplication(); |
| 41 |
} |
| 42 |
|
| 43 |
return new static($app); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Class constructor. |
| 48 |
* |
| 49 |
* @param JApplication $app The application instance. |
| 50 |
*/ |
| 51 |
public function __construct($app) |
| 52 |
{ |
| 53 |
$this->app = $app; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Method to close the application. |
| 58 |
* |
| 59 |
* @param integer $code The HTTP status code. |
| 60 |
* @param mixed $buffer An optional string to display. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function close($code = 200, $buffer = null) |
| 65 |
{ |
| 66 |
// force HTTP status code |
| 67 |
$this->app->setHeader('status', $code, $replace = true); |
| 68 |
$this->app->sendHeaders(); |
| 69 |
|
| 70 |
if (VBOPlatformDetection::isWordPress() && !headers_sent()) |
| 71 |
{ |
| 72 |
// this is necessary for the HTTP2 protocol |
| 73 |
http_response_code($code); |
| 74 |
} |
| 75 |
|
| 76 |
if ($buffer) |
| 77 |
{ |
| 78 |
// display buffer |
| 79 |
echo $buffer; |
| 80 |
} |
| 81 |
|
| 82 |
// terminate session |
| 83 |
$this->app->close(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Echoes the given JSON by using the right content type. |
| 88 |
* |
| 89 |
* @param mixed $json Either a JSON string or a non-scalar value. |
| 90 |
* @param int $flags Bitmask for json_encode(). |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function json($json, $flags = 0) |
| 95 |
{ |
| 96 |
$this->app->setHeader('Content-Type', 'application/json', $replace = true); |
| 97 |
|
| 98 |
if (!is_string($json)) |
| 99 |
{ |
| 100 |
// stringify array/object |
| 101 |
$json = json_encode($json, $flags); |
| 102 |
} |
| 103 |
|
| 104 |
// terminate session by echoing the given JSON |
| 105 |
$this->close(200, $json); |
| 106 |
} |
| 107 |
} |
| 108 |
|