1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// ------------------------------------------------------------------------- |
4
|
|
|
// OVIDENTIA http://www.ovidentia.org |
5
|
|
|
// Ovidentia is free software; you can redistribute it and/or modify |
6
|
|
|
// it under the terms of the GNU General Public License as published by |
7
|
|
|
// the Free Software Foundation; either version 2, or (at your option) |
8
|
|
|
// any later version. |
9
|
|
|
// |
10
|
|
|
// This program is distributed in the hope that it will be useful, but |
11
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13
|
|
|
// See the GNU General Public License for more details. |
14
|
|
|
// |
15
|
|
|
// You should have received a copy of the GNU General Public License |
16
|
|
|
// along with this program; if not, write to the Free Software |
17
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18
|
|
|
// USA. |
19
|
|
|
// ------------------------------------------------------------------------- |
20
|
|
|
/** |
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL) |
22
|
|
|
* @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com}) |
23
|
|
|
*/ |
24
|
|
|
namespace Capwelton\LibApp\Ctrl; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This controller manages actions that can be performed on notifications |
28
|
|
|
* |
29
|
|
|
* @method Func_App App() |
30
|
|
|
* @method self proxy() |
31
|
|
|
*/ |
32
|
|
|
class AppCtrlSSE extends AppController |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
private $currentUser; |
36
|
|
|
|
37
|
|
|
private $refreshFrequency; |
38
|
|
|
|
39
|
|
|
public function server() |
40
|
|
|
{ |
41
|
|
|
session_write_close(); |
42
|
|
|
|
43
|
|
|
header("Content-Type: text/event-stream"); |
44
|
|
|
header('Cache-Control: no-cache'); |
45
|
|
|
header('Connection: keep-alive'); |
46
|
|
|
header('X-Accel-Buffering: no'); |
47
|
|
|
|
48
|
|
|
$refreshFrequency = $this->getRefreshFrequency(); |
49
|
|
|
|
50
|
|
|
while (1){ |
51
|
|
|
if(connection_aborted()){ |
52
|
|
|
exit(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
$data = $this->getSSEData(); |
55
|
|
|
if($data != '{}'){ |
56
|
|
|
echo "event: ping\n"; |
57
|
|
|
echo "\n\n"; |
58
|
|
|
echo 'data: ' . $this->getSSEData() . "\n\n"; |
59
|
|
|
} |
60
|
|
|
ob_end_flush(); |
61
|
|
|
flush(); |
62
|
|
|
sleep($refreshFrequency); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function dummy() |
67
|
|
|
{ |
68
|
|
|
$App = $this->App(); |
69
|
|
|
$notifSet = $App->NotificationSet(); |
70
|
|
|
$dummy = $notifSet->newRecord(); |
71
|
|
|
|
72
|
|
|
$dummy->title = 'Dummy title'; |
73
|
|
|
$dummy->description = 'Lorem ipsum dolor sit amet, consectetur adisciping elit'; |
74
|
|
|
$dummy->user = bab_getUserId(); |
75
|
|
|
$dummy->save(); |
76
|
|
|
|
77
|
|
|
$dummy->createSSENotification(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function getRefreshFrequency() |
81
|
|
|
{ |
82
|
|
|
if(! isset($this->refreshFrequency)){ |
83
|
|
|
$registry = app_getRegistry(); |
84
|
|
|
$registry->changeDirectory('configuration'); |
85
|
|
|
$registry->changeDirectory('SSE'); |
86
|
|
|
$this->refreshFrequency = $registry->getValue('refreshFrequency', 3); |
87
|
|
|
} |
88
|
|
|
return $this->refreshFrequency; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getSSEData() |
92
|
|
|
{ |
93
|
|
|
$SSESet = $this->getSSERecordSet(); |
94
|
|
|
$currentUser = $this->getCurrentUser(); |
95
|
|
|
|
96
|
|
|
return $SSESet->getSSEData($currentUser, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getCurrentUser() |
100
|
|
|
{ |
101
|
|
|
if(isset($this->currentUser)){ |
102
|
|
|
return $this->currentUser; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$App = $this->App(); |
106
|
|
|
$this->currentUser = $App->getCurrentUser(); |
107
|
|
|
return $this->currentUser; |
108
|
|
|
} |
109
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.