]>
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";
27 if (!defined('NO_EVENT_STREAM_HEADER'))
28 header('Content-Type: text/event-stream');
30 // Explicitly disable caching so Varnish and other upstreams won't cache.
31 header("Cache-Control: no-cache, must-revalidate");
33 // Setting this header instructs Nginx to disable fastcgi_buffering and disable
34 // gzip for this request.
35 header('X-Accel-Buffering: no');
40 // Send content immediately
43 // Flush and stop output buffering (eg fastcgi w/NGINX)
44 function flush_completely()
49 $ret = @ob_end_flush();
61 /* Send server-sent events (SSE) message */
62 function send_sse($json)
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";
71 function api_log_loop($sources)
74 GLOBAL $fpm_workaround_needed;
76 $rpc->log()->subscribe($sources);
85 $res = $rpc->eventloop();
88 /* Output at least something every timeout (10) seconds,
89 * otherwise PHP may not
90 * notice when the webclient is gone.
92 if ($fpm_workaround_needed)
93 echo str_repeat(" ", 4095)."\n";
102 function api_timer_loop(int $every_msec, string $method, array|null $params = null)
106 /* First, execute it immediately */
107 $res = $rpc->query($method, $params);
111 $rpc->rpc()->add_timer("timer", $every_msec, $method, $params);
114 /* Have to resort to old style: client-side timer */
117 $res = $rpc->query($method, $params);
121 usleep($every_msec * 1000);
125 /* New style: use server-side timers */
126 /* - First, execute it immediately */
127 $res = $rpc->query($method, $params);
131 /* - Then add the timer */
134 $res = $rpc->eventloop();
137 /* Output at least something every timeout (10) seconds,
138 * otherwise PHP may not
139 * notice when the webclient is gone.
141 if ($fpm_workaround_needed)
142 echo str_repeat(" ", 4095)."\n";