]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - api/common_api.php
2 include "../inc/common.php";
4 if(session_status() !== PHP_SESSION_ACTIVE
) session_start();
6 if (!isset($_SESSION['id']))
9 // Close the session now, otherwise other pages block
10 session_write_close();
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()))
20 $fpm_workaround_needed = true;
23 // Only now make the connection (this can take a short while)
24 include "../inc/connection.php";
26 header("Content-type: application/json; charset=utf-8");
28 if (!defined('NO_EVENT_STREAM_HEADER'))
29 header('Content-Type: text/event-stream');
31 // Explicitly disable caching so Varnish and other upstreams won't cache.
32 header("Cache-Control: no-cache, must-revalidate");
34 // Setting this header instructs Nginx to disable fastcgi_buffering and disable
35 // gzip for this request.
36 header('X-Accel-Buffering: no');
41 // Send content immediately
44 // Flush and stop output buffering (eg fastcgi w/NGINX)
45 function flush_completely()
50 $ret = @ob_end_flush();
60 if (!defined('NO_EVENT_STREAM_HEADER'))
63 /* Send server-sent events (SSE) message */
64 function send_sse($json)
66 GLOBAL $fpm_workaround_needed;
67 $str = "data: ".json_encode($json)."\n\n";
68 if ($fpm_workaround_needed)
69 $str .= str_repeat(" ", 4096 - ((strlen($str)+
1) %
4096))."\n";
73 function api_log_loop($sources)
76 GLOBAL $fpm_workaround_needed;
78 $rpc->log()->subscribe($sources);
87 $res = $rpc->eventloop();
90 /* Output at least something every timeout (10) seconds,
91 * otherwise PHP may not
92 * notice when the webclient is gone.
94 if ($fpm_workaround_needed)
95 echo str_repeat(" ", 4095)."\n";
104 function api_timer_loop(int $every_msec, string $method, array|null $params = null)
108 /* First, execute it immediately */
109 $res = $rpc->query($method, $params);
113 $rpc->rpc()->add_timer("timer", $every_msec, $method, $params);
116 /* Have to resort to old style: client-side timer */
119 $res = $rpc->query($method, $params);
123 usleep($every_msec * 1000);
127 /* New style: use server-side timers */
128 /* - First, execute it immediately */
129 $res = $rpc->query($method, $params);
133 /* - Then add the timer */
136 $res = $rpc->eventloop();
139 /* Output at least something every timeout (10) seconds,
140 * otherwise PHP may not
141 * notice when the webclient is gone.
143 if ($fpm_workaround_needed)
144 echo str_repeat(" ", 4095)."\n";