• R/O
  • SSH
  • HTTPS

phpgoph: Commit


Commit MetaInfo

Revision1 (tree)
Time2021-06-03 20:39:02
Authormateuszviste

Log Message

initial import

Change Summary

Incremental Difference

--- trunk/phpgoph.php (nonexistent)
+++ trunk/phpgoph.php (revision 1)
@@ -0,0 +1,105 @@
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+// This 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+// change this to make the server listen on another port (ports below 1024 may require root privileges!)
36+$PORT = 7070;
37+
38+
39+// helper function that builds a menu entry line
40+function infoline($txt, $type = 'i', $res = '', $host = 'localhost') {
41+ global $PORT;
42+ return($type . $txt . "\t{$res}\t{$host}\t$PORT\r\n");
43+}
44+
45+
46+// make sure the socket-related functions are present in this PHP install
47+if (!function_exists('socket_create')) {
48+ echo "ERROR: your PHP installation is missing support for sockets. Perhaps you should install something like php-sockets or alike.\n";
49+ exit(1);
50+}
51+
52+// initialize a socket
53+$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
54+
55+// bind the socket to 0.0.0.0 so it will accept connections from all local interfaces
56+socket_bind($sock, '0.0.0.0', $PORT);
57+
58+// switch the socket into listening mode (only 1 client at a time)
59+socket_listen($sock, 1);
60+
61+echo "phpgoph is running now and listens on port {$PORT}\n";
62+
63+// processing loop
64+for (;;) {
65+
66+ // wait for a client
67+ $clisock = socket_accept($sock);
68+
69+ // who is the client?
70+ socket_getpeername($clisock, $client, $cliport);
71+
72+ // what is my own IP address?
73+ socket_getsockname($clisock, $selfip);
74+
75+ echo "accepted connection from {$client}:{$cliport}\n";
76+
77+ // read a single line from stdin - ie. the gopher client's query (max 512 bytes)
78+ $req = trim(socket_read($clisock, 512, PHP_NORMAL_READ));
79+
80+ if ($req == 'alphabet') {
81+ socket_write($clisock, "abcdefghijklmnopqrstuvwxyz\r\n");
82+ socket_write($clisock, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n");
83+ socket_write($clisock, ".\r\n");
84+
85+ } else if ($req == 'random') {
86+ $r = rand(1, 10);
87+ socket_write($clisock, "Your random number is " . $r);
88+ socket_write($clisock, ".\r\n");
89+
90+ } else {
91+ socket_write($clisock, infoline('Welcome to PHPGOPH - a crude gopher server implementation written in PHP'));
92+ socket_write($clisock, infoline(''));
93+ socket_write($clisock, infoline("Today is " . date('l \t\h\e jS')));
94+ socket_write($clisock, infoline(''));
95+ socket_write($clisock, infoline("Your IP is {$client}"));
96+ socket_write($clisock, infoline("This server's IP is {$selfip}"));
97+ socket_write($clisock, infoline(''));
98+ socket_write($clisock, infoline('Would you like to see the alphabet?', '0', 'alphabet', $selfip));
99+ socket_write($clisock, infoline('Here you will get a random number (1-10)', '0', 'random', $selfip));
100+ socket_write($clisock, ".\r\n");
101+ }
102+
103+ socket_close($clisock);
104+
105+}
--- trunk/phpgoph.txt (nonexistent)
+++ trunk/phpgoph.txt (revision 1)
@@ -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+This 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/>
Show on old repository browser