Revision | 4 (tree) |
---|---|
Time | 2021-06-03 21:46:27 |
Author | mateuszviste |
frozen ver 20210603 into tags
@@ -0,0 +1,110 @@ | ||
1 | +<?php | |
2 | + | |
3 | +// This is a very simple, naive and crude PHP implementation of a minimalist | |
4 | +// gopher server. It only opens a socket, awaits for a (single) client and | |
5 | +// displays back an informational screen. | |
6 | +// | |
7 | +// It is meant as a dumb (simplest possible) example of a gopher server for | |
8 | +// students and hobbyists to fiddle with and possibly build upon. | |
9 | +// | |
10 | +// Written by Mateusz Viste, who released it to the public domain (see below). | |
11 | +// --------------------------------------------------------------------------- | |
12 | +// This is free and unencumbered software released into the public domain. | |
13 | +// | |
14 | +// Anyone is free to copy, modify, publish, use, compile, sell, or distribute | |
15 | +// this software, either in source code form or as a compiled binary, for any | |
16 | +// purpose, commercial or non-commercial, and by any means. | |
17 | +// | |
18 | +// In jurisdictions that recognize copyright laws, the author or authors of | |
19 | +// this software dedicate any and all copyright interest in the software to | |
20 | +// the public domain. We make this dedication for the benefit of the public at | |
21 | +// large and to the detriment of our heirs and successors. We intend this | |
22 | +// dedication to be an overt act of relinquishment in perpetuity of all | |
23 | +// present and future rights to this software under copyright law. | |
24 | +// | |
25 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
26 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
27 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
28 | +// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
29 | +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
30 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
31 | +// | |
32 | +// For more information, please refer to <http://unlicense.org/> | |
33 | +// --------------------------------------------------------------------------- | |
34 | +// | |
35 | +// CHANGELOG | |
36 | +// 20210603: public release | |
37 | +// | |
38 | + | |
39 | + | |
40 | +// change this to make the server listen on another port (ports below 1024 may require root privileges!) | |
41 | +$PORT = 7070; | |
42 | + | |
43 | + | |
44 | +// helper function that builds a menu entry line | |
45 | +function infoline($txt, $type = 'i', $res = '', $host = 'localhost') { | |
46 | + global $PORT; | |
47 | + return($type . $txt . "\t{$res}\t{$host}\t$PORT\r\n"); | |
48 | +} | |
49 | + | |
50 | + | |
51 | +// make sure the socket-related functions are present in this PHP install | |
52 | +if (!function_exists('socket_create')) { | |
53 | + echo "ERROR: your PHP installation is missing support for sockets. Perhaps you should install something like php-sockets or alike.\n"; | |
54 | + exit(1); | |
55 | +} | |
56 | + | |
57 | +// initialize a socket | |
58 | +$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
59 | + | |
60 | +// bind the socket to 0.0.0.0 so it will accept connections from all local interfaces | |
61 | +socket_bind($sock, '0.0.0.0', $PORT); | |
62 | + | |
63 | +// switch the socket into listening mode (only 1 client at a time) | |
64 | +socket_listen($sock, 1); | |
65 | + | |
66 | +echo "phpgoph is running now and listens on port {$PORT}\n"; | |
67 | + | |
68 | +// processing loop | |
69 | +for (;;) { | |
70 | + | |
71 | + // wait for a client | |
72 | + $clisock = socket_accept($sock); | |
73 | + | |
74 | + // who is the client? | |
75 | + socket_getpeername($clisock, $client, $cliport); | |
76 | + | |
77 | + // what is my own IP address? | |
78 | + socket_getsockname($clisock, $selfip); | |
79 | + | |
80 | + echo "accepted connection from {$client}:{$cliport}\n"; | |
81 | + | |
82 | + // read a single line from stdin - ie. the gopher client's query (max 512 bytes) | |
83 | + $req = trim(socket_read($clisock, 512, PHP_NORMAL_READ)); | |
84 | + | |
85 | + if ($req == 'alphabet') { | |
86 | + socket_write($clisock, "abcdefghijklmnopqrstuvwxyz\r\n"); | |
87 | + socket_write($clisock, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"); | |
88 | + socket_write($clisock, ".\r\n"); | |
89 | + | |
90 | + } else if ($req == 'random') { | |
91 | + $r = rand(1, 10); | |
92 | + socket_write($clisock, "Your random number is " . $r); | |
93 | + socket_write($clisock, ".\r\n"); | |
94 | + | |
95 | + } else { | |
96 | + socket_write($clisock, infoline('Welcome to PHPGOPH - a crude gopher server implementation written in PHP')); | |
97 | + socket_write($clisock, infoline('')); | |
98 | + socket_write($clisock, infoline("Today is " . date('l \t\h\e jS'))); | |
99 | + socket_write($clisock, infoline('')); | |
100 | + socket_write($clisock, infoline("Your IP is {$client}")); | |
101 | + socket_write($clisock, infoline("This server's IP is {$selfip}")); | |
102 | + socket_write($clisock, infoline('')); | |
103 | + socket_write($clisock, infoline('Would you like to see the alphabet?', '0', 'alphabet', $selfip)); | |
104 | + socket_write($clisock, infoline('Here you will get a random number (1-10)', '0', 'random', $selfip)); | |
105 | + socket_write($clisock, ".\r\n"); | |
106 | + } | |
107 | + | |
108 | + socket_close($clisock); | |
109 | + | |
110 | +} |
@@ -0,0 +1,56 @@ | ||
1 | + | |
2 | +This is a very simple, naive and crude PHP implementation of a minimalist | |
3 | +gopher server. It only opens a socket, awaits for a (single) client and | |
4 | +displays back an informational screen. The program is about 40 lines long. | |
5 | + | |
6 | +It is meant as a dumb (simplest possible) example of a gopher server for | |
7 | +students and hobbyists to fiddle with and possibly build upon. It supports | |
8 | +only a single client at a time for the sake of simplicity. Any evolution, | |
9 | +improvements and fixes of this software are left to the users. Good luck. | |
10 | + | |
11 | +homepage: http://phpgoph.osdn.io | |
12 | + | |
13 | + | |
14 | +=== USAGE ==================================================================== | |
15 | + | |
16 | +To run phpgoph you need to have a working php installation with the "sockets" | |
17 | +extension (this usually means you need to install a package named php8-sockets | |
18 | +or similar). Then, simply run it as follows: | |
19 | + | |
20 | + php phpgoph.php | |
21 | + | |
22 | +phpgoph listens on port 7070. To test it, point a gopher client at the url | |
23 | +gopher://127.0.0.1:7070 and the phpgoph welcome screen should appear. | |
24 | + | |
25 | + | |
26 | +=== CONTACT ================================================================== | |
27 | + | |
28 | +If you'd like to contact me about this software, you should find the necessary | |
29 | +pointers at the http://mateusz.viste.fr address. | |
30 | + | |
31 | + | |
32 | +=== LICENSING ================================================================ | |
33 | + | |
34 | +Written by Mateusz Viste, who released it to the public domain (see below). | |
35 | + | |
36 | +This is free and unencumbered software released into the public domain. | |
37 | + | |
38 | +Anyone is free to copy, modify, publish, use, compile, sell, or distribute | |
39 | +this software, either in source code form or as a compiled binary, for any | |
40 | +purpose, commercial or non-commercial, and by any means. | |
41 | + | |
42 | +In jurisdictions that recognize copyright laws, the author or authors of | |
43 | +this software dedicate any and all copyright interest in the software to | |
44 | +the public domain. We make this dedication for the benefit of the public at | |
45 | +large and to the detriment of our heirs and successors. We intend this | |
46 | +dedication to be an overt act of relinquishment in perpetuity of all | |
47 | +present and future rights to this software under copyright law. | |
48 | + | |
49 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
50 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
51 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
52 | +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
53 | +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
54 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
55 | + | |
56 | +For more information, please refer to <http://unlicense.org/> |