document.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 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.7.3 |
| 18 | */ |
| 19 | class VAPHttpDocument |
| 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 ($buffer) |
| 71 | { |
| 72 | // display buffer |
| 73 | echo $buffer; |
| 74 | } |
| 75 | |
| 76 | // terminate session |
| 77 | $this->app->close(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Echoes the given JSON by using the right content type. |
| 82 | * |
| 83 | * @param mixed $json Either a JSON string or a non-scalar value. |
| 84 | * |
| 85 | * @return void |
| 86 | */ |
| 87 | public function json($json) |
| 88 | { |
| 89 | $this->app->setHeader('Content-Type', 'application/json', $replace = true); |
| 90 | |
| 91 | if (!is_string($json)) |
| 92 | { |
| 93 | // stringify array/object |
| 94 | $json = json_encode($json); |
| 95 | } |
| 96 | |
| 97 | // terminate session by echoing the given JSON |
| 98 | $this->close(200, $json); |
| 99 | } |
| 100 | } |
| 101 |