]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - api/common_api.php
Move extra header where it should be
[irc/unrealircd/unrealircd-webpanel.git] / api / common_api.php
CommitLineData
fd4848e4 1<?php
c06c1713 2include "../inc/common.php";
fd4848e4 3
9c9d2cdf
BM
4if(session_status() !== PHP_SESSION_ACTIVE) session_start();
5
fd4848e4
BM
6if (!isset($_SESSION['id']))
7 die("Access denied");
8
9// Close the session now, otherwise other pages block
10session_write_close();
11
65b63bbe
BM
12// Apache w/FPM is shit because it doesn't have flushpackets=on
13// or not by default anyway, so we will fill up 4k buffers.
14// Yeah, really silly... I know.
15$fpm_workaround_needed = false;
16if (str_contains($_SERVER['SERVER_SOFTWARE'], 'Apache') &&
17 function_exists('fpm_get_status') &&
18 is_array(fpm_get_status()))
19{
20 $fpm_workaround_needed = true;
21}
22
fd4848e4 23// Only now make the connection (this can take a short while)
c06c1713 24include "../inc/connection.php";
fd4848e4 25
6745a128 26header("Content-type: application/json; charset=utf-8");
fd4848e4 27// Server Side Events
63026197
BM
28if (!defined('NO_EVENT_STREAM_HEADER'))
29 header('Content-Type: text/event-stream');
fd4848e4
BM
30
31// Explicitly disable caching so Varnish and other upstreams won't cache.
32header("Cache-Control: no-cache, must-revalidate");
33
34// Setting this header instructs Nginx to disable fastcgi_buffering and disable
35// gzip for this request.
36header('X-Accel-Buffering: no');
37
38// No time limit
39set_time_limit(0);
40
41// Send content immediately
42ob_implicit_flush(1);
43
f9558c92 44// Flush and stop output buffering (eg fastcgi w/NGINX)
65b63bbe 45function flush_completely()
3c5d6298 46{
65b63bbe 47 while (1)
3c5d6298 48 {
65b63bbe
BM
49 try {
50 $ret = @ob_end_flush();
51 if ($ret === false)
52 break;
53 } catch(Exception $e)
54 {
55 break;
56 }
3c5d6298 57 }
65b63bbe
BM
58}
59
60flush_completely();
fd4848e4
BM
61
62/* Send server-sent events (SSE) message */
63function send_sse($json)
64{
65b63bbe
BM
65 GLOBAL $fpm_workaround_needed;
66 $str = "data: ".json_encode($json)."\n\n";
67 if ($fpm_workaround_needed)
68 $str .= str_repeat(" ", 4096 - ((strlen($str)+1) % 4096))."\n";
69 echo $str;
fd4848e4
BM
70}
71
72function api_log_loop($sources)
73{
74 GLOBAL $rpc;
65b63bbe 75 GLOBAL $fpm_workaround_needed;
fd4848e4
BM
76
77 $rpc->log()->subscribe($sources);
78 if ($rpc->error)
79 {
80 echo $rpc->error;
81 die;
82 }
83
84 for(;;)
85 {
86 $res = $rpc->eventloop();
87 if (!$res)
0a2b3443
BM
88 {
89 /* Output at least something every timeout (10) seconds,
90 * otherwise PHP may not
91 * notice when the webclient is gone.
92 */
65b63bbe
BM
93 if ($fpm_workaround_needed)
94 echo str_repeat(" ", 4095)."\n";
95 else
96 echo "\n";
fd4848e4 97 continue;
0a2b3443 98 }
fd4848e4
BM
99 send_sse($res);
100 }
101}
102
103function api_timer_loop(int $every_msec, string $method, array|null $params = null)
104{
105 GLOBAL $rpc;
106
c16c239b
BM
107 /* First, execute it immediately */
108 $res = $rpc->query($method, $params);
109 if (!$res)
110 die;
111 send_sse($res);
fd4848e4
BM
112 $rpc->rpc()->add_timer("timer", $every_msec, $method, $params);
113 if ($rpc->error)
114 {
69e00a0c
BM
115 /* Have to resort to old style: client-side timer */
116 while(1)
117 {
118 $res = $rpc->query($method, $params);
119 if (!$res)
120 die;
121 send_sse($res);
563ebce7 122 usleep($every_msec * 1000);
69e00a0c 123 }
fd4848e4
BM
124 }
125
69e00a0c 126 /* New style: use server-side timers */
c16c239b
BM
127 /* - First, execute it immediately */
128 $res = $rpc->query($method, $params);
129 if (!$res)
130 die;
131 send_sse($res);
132 /* - Then add the timer */
fd4848e4
BM
133 for(;;)
134 {
135 $res = $rpc->eventloop();
136 if (!$res)
0a2b3443
BM
137 {
138 /* Output at least something every timeout (10) seconds,
139 * otherwise PHP may not
140 * notice when the webclient is gone.
141 */
65b63bbe
BM
142 if ($fpm_workaround_needed)
143 echo str_repeat(" ", 4095)."\n";
144 else
145 echo "\n";
fd4848e4 146 continue;
0a2b3443 147 }
fd4848e4
BM
148 send_sse($res);
149 }
150}