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