|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace App\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use Cake\Event\Event; |
|
16
|
|
|
use Cake\Http\Exception\BadRequestException; |
|
17
|
|
|
use Cake\Http\Response; |
|
18
|
|
|
|
|
19
|
|
|
class StatusController extends AppController |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
public $autoRender = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Sends status data to the frontend |
|
26
|
|
|
* |
|
27
|
|
|
* Even if no data is send for other functionality the ping keeps the |
|
28
|
|
|
* current user online. |
|
29
|
|
|
* |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function status() |
|
33
|
|
|
{ |
|
34
|
|
|
$data = []; |
|
35
|
|
|
$data = json_encode($data); |
|
36
|
|
|
if ($this->request->accepts('text/event-streams')) { |
|
37
|
|
|
$body = $this->_statusAsEventStream($data); |
|
38
|
|
|
} else { |
|
39
|
|
|
$body = $this->_statusAsJson($data); |
|
40
|
|
|
} |
|
41
|
|
|
$this->response = $this->response->withStringBody($body); |
|
42
|
|
|
|
|
43
|
|
|
return $this->response; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get status as event-stream |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $data json-encoded data |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function _statusAsEventStream($data) |
|
53
|
|
|
{ |
|
54
|
|
|
// time in ms to next request |
|
55
|
|
|
$retry = '10000'; |
|
56
|
|
|
$this->response = $this->response->withType(['eventstream' => 'text/event-stream']); |
|
|
|
|
|
|
57
|
|
|
$this->response = $this->response->withType('eventstream'); |
|
58
|
|
|
$this->response->disableCache(); |
|
|
|
|
|
|
59
|
|
|
$out = ''; |
|
60
|
|
|
$out .= "retry: $retry\n"; |
|
61
|
|
|
$out .= 'data: ' . $data . "\n\n"; |
|
62
|
|
|
|
|
63
|
|
|
return $out; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get status as JSON response |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $data json-encoded data |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function _statusAsJson($data) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->request->is('ajax') === false) { |
|
75
|
|
|
throw new BadRequestException(); |
|
76
|
|
|
} |
|
77
|
|
|
$this->response = $this->response->withType('json'); |
|
78
|
|
|
|
|
79
|
|
|
return $data; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
* |
|
85
|
|
|
* @param Event $event An Event instance |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function beforeFilter(Event $event) |
|
89
|
|
|
{ |
|
90
|
|
|
parent::beforeFilter($event); |
|
91
|
|
|
$this->components()->unload('Authentication'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|