-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
132 lines (120 loc) · 3.91 KB
/
Copy pathindex.php
File metadata and controls
132 lines (120 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
//initialization
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\CloseFrame;
use Swoole\Coroutine\Http\Server;
use function Swoole\Coroutine\run;
include "msgs.php";
function openFile($name) {
ob_start();
try {
require $name;
} catch (Exception $e) {
echo $e;
}
$data = ob_get_contents();
ob_end_clean();
return $data;
};
function host($http, $path, $type) {
$http->handle("/".$path, function ($request, $response) use ($type, $path) {
$response->header('Content-Type', $type);
$response->end(openFile("files/".$path));
});
};
$sockets = array();
// the function below is what will be ran
run(function () use ($sockets) {
function emitAll($data) {
foreach ($GLOBALS['sockets'] as $socket) {
//echo "emitting $data to $socket... \n";
$socket->push($data);
}
};
$deleteSocket = function ($socket) use ($sockets) {
echo "deleted a socket \n";
$index = array_search($socket, $GLOBALS['sockets']);
array_splice($GLOBALS['sockets'], $index, 1);
};
$paths = array(
"css/ripple.css" => "text/css; charset=utf-8",
"css/style.css" => "text/css; charset=utf-8",
"js/bg.js" => "application/javascript; charset=utf-8",
"js/popup.js" => "application/javascript; charset=utf-8",
"js/protect.js" => "application/javascript; charset=utf-8",
"js/ripple.js" => "application/javascript; charset=utf-8",
"js/auth.js" => "application/javascript; charset=utf-8",
"js/filter.js" => "application/javascript; charset=utf-8",
"js/styling.js" => "application/javascript; charset=utf-8",
"js/userdata.js" => "application/javascript; charset=utf-8",
"images/favicon.webp" => "image/webp",
"images/favicon.ico" => "image/x-icon",
"images/favicon.svg" => "image/svg+xml",
"html/404.html"=> "text/html; charset=utf-8"
);
$http = new Server('0.0.0.0', 8000); // create new server
$http->handle('/', function ($request, $response) {
// handle page requests
$headers = $request->header;
if ($headers["x-replit-user-name"] && $headers["x-replit-user-id"]) {
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end(str_replace(
"{{name}}",
$headers["x-replit-user-name"],
openFile("templates/index.html")
));
//$response->end("Hi");,
} else {
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end("<script>window.location.href = 'login';</script>");
}
});
$http->handle("/login", function ($request, $response) {
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end(openFile("templates/login.html"));
});
foreach($paths as $path => $type) {
host($http, $path, $type);
}
$http->handle('/ws-server', function (Request $request, Response $ws) use ($sockets, $deleteSocket) {
// upgrade to websocket
echo "new websocket connected \n";
$ws->upgrade();
array_push($GLOBALS['sockets'], $ws);
sendMsgs($ws);
$ws->push(json_encode(array(
"channel"=>"lastMsg"
)));
while (true) {
$frame = $ws->recv();
if ($frame === '') {
// ???
$ws->close();
$deleteSocket($ws);
echo "something happened, deleted socket\n";
break;
} else if ($frame === false) {
// handle error
echo 'errorCode: ' . swoole_last_error() . "\n";
$ws->close();
$deleteSocket($ws);
break;
} else {
// checks for the closing signal
if ($frame->data == 'close' || get_class($frame) === CloseFrame::class) {
echo "manual close\n";
$ws->close();
$deleteSocket($ws);
break;
}
// and finally send the data
echo "received message \n";
$msg = storeMsg($frame->data, $ws);
if ($msg != null) emitAll($msg, $GLOBALS['sockets']);
}
}
});
print("server started\n");
$http->start();
});