|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* SendResponseTrait.php |
|
5
|
|
|
* |
|
6
|
|
|
* Send Jaxon ajax response. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
9
|
|
|
* @author Jared White |
|
10
|
|
|
* @author J. Max Wilson |
|
11
|
|
|
* @author Thierry Feuzeu |
|
12
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
13
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
|
14
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
15
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Jaxon\App\Ajax\Traits; |
|
19
|
|
|
|
|
20
|
|
|
use Jaxon\App\Config\ConfigManager; |
|
21
|
|
|
use Jaxon\App\I18n\Translator; |
|
22
|
|
|
use Jaxon\Di\Container; |
|
23
|
|
|
use Jaxon\Exception\RequestException; |
|
24
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
|
25
|
|
|
|
|
26
|
|
|
use function gmdate; |
|
27
|
|
|
use function header; |
|
28
|
|
|
use function headers_sent; |
|
29
|
|
|
use function http_response_code; |
|
30
|
|
|
use function intval; |
|
31
|
|
|
|
|
32
|
|
|
trait SendResponseTrait |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @return Container |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract public function di(): Container; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return ConfigManager |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public function config(): ConfigManager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return Translator |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract public function translator(): Translator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return ResponseManager |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract public function getResponseManager(): ResponseManager; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Prints the response to the output stream, thus sending the response to the browser |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
* @throws RequestException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function httpResponse(string $sCode = '200'): mixed |
|
61
|
|
|
{ |
|
62
|
|
|
if(!$this->config()->getOption('core.response.send', false)) |
|
63
|
|
|
{ |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Check to see if headers have already been sent out, in which case we can't do our job |
|
68
|
|
|
if(headers_sent($sFilename, $nLineNumber)) |
|
69
|
|
|
{ |
|
70
|
|
|
throw new RequestException($this->translator() |
|
71
|
|
|
->trans('errors.output.already-sent', [ |
|
72
|
|
|
'location' => "$sFilename:$nLineNumber", |
|
73
|
|
|
]) . "\n" . $this->translator()->trans('errors.output.advice')); |
|
74
|
|
|
} |
|
75
|
|
|
if(empty($sContent = $this->getResponseManager()->getOutput())) |
|
76
|
|
|
{ |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Set the HTTP response code |
|
81
|
|
|
http_response_code(intval($sCode)); |
|
82
|
|
|
|
|
83
|
|
|
if($this->di()->getRequest()->getMethod() === 'GET') |
|
84
|
|
|
{ |
|
85
|
|
|
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
|
86
|
|
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
|
87
|
|
|
header("Cache-Control: no-cache, must-revalidate"); |
|
88
|
|
|
header("Pragma: no-cache"); |
|
89
|
|
|
} |
|
90
|
|
|
header('Content-Type: ' . $this->getResponseManager()->getContentType()); |
|
91
|
|
|
|
|
92
|
|
|
print $sContent; |
|
93
|
|
|
if($this->config()->getOption('core.process.exit', false)) |
|
94
|
|
|
{ |
|
95
|
|
|
exit(); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.