]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - api/common_api.php
Don't start session if we already have one. Actually, do we need this at all?
[irc/unrealircd/unrealircd-webpanel.git] / api / common_api.php
CommitLineData
fd4848e4
BM
1<?php
2include "../common.php";
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
12// Only now make the connection (this can take a short while)
13include "../connection.php";
14
15// Server Side Events
16header('Content-Type: text/event-stream');
17
18// Explicitly disable caching so Varnish and other upstreams won't cache.
19header("Cache-Control: no-cache, must-revalidate");
20
21// Setting this header instructs Nginx to disable fastcgi_buffering and disable
22// gzip for this request.
23header('X-Accel-Buffering: no');
24
25// No time limit
26set_time_limit(0);
27
28// Send content immediately
29ob_implicit_flush(1);
30
31// Eh.. yeah...
32ob_end_flush();
33
34// If we use fastcgi, then finish the request now (UNTESTED)
35if (function_exists('fastcgi_finish_request'))
36 fastcgi_finish_request();
37
38/* Send server-sent events (SSE) message */
39function send_sse($json)
40{
41 echo "data: ".json_encode($json)."\n\n";
42}
43
44function api_log_loop($sources)
45{
46 GLOBAL $rpc;
47
48 $rpc->log()->subscribe($sources);
49 if ($rpc->error)
50 {
51 echo $rpc->error;
52 die;
53 }
54
55 for(;;)
56 {
57 $res = $rpc->eventloop();
58 if (!$res)
59 continue;
60 send_sse($res);
61 }
62}
63
64function api_timer_loop(int $every_msec, string $method, array|null $params = null)
65{
66 GLOBAL $rpc;
67
68 $rpc->rpc()->add_timer("timer", $every_msec, $method, $params);
69 if ($rpc->error)
70 {
69e00a0c
BM
71 /* Have to resort to old style: client-side timer */
72 while(1)
73 {
74 $res = $rpc->query($method, $params);
75 if (!$res)
76 die;
77 send_sse($res);
78 }
fd4848e4
BM
79 }
80
69e00a0c 81 /* New style: use server-side timers */
fd4848e4
BM
82 for(;;)
83 {
84 $res = $rpc->eventloop();
85 if (!$res)
86 continue;
87 send_sse($res);
88 }
89}