Browse Source

initial commit

bedna-KU 8 years ago
commit
a1e59374c8
8 changed files with 326 additions and 0 deletions
  1. 27 0
      bin/javascript/connect.js
  2. 27 0
      bin/javascript/main.js
  3. 31 0
      bin/javascript/readResponse.js
  4. 34 0
      bin/javascript/send.js
  5. 88 0
      connect.php
  6. 48 0
      index.php
  7. 35 0
      read_response.php
  8. 36 0
      send.php

+ 27 - 0
bin/javascript/connect.js View File

@@ -0,0 +1,27 @@
1
+//#########################################################
2
+// Part of project php-IRC-js
3
+//
4
+// xhr_write.php - connect to server
5
+//
6
+// Author: Mario Chorvath - Bedna
7
+// Start 2016
8
+//
9
+// Licence GNU General Public License
10
+// Version 2
11
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
12
+//#########################################################
13
+
14
+// Connect to server
15
+function connect () {
16
+	// Create new XHR
17
+    var xhr = new XMLHttpRequest ();
18
+    // If "connect.php" send data back
19
+	xhr.onreadystatechange = function() {
20
+		if (xhr.readyState == 4 && xhr.status == 200) {
21
+			document.getElementById("status").innerHTML += "@@@" + xhr.responseText + "</b><br>";
22
+		}
23
+	};
24
+    
25
+    xhr.open ("GET","connect.php", true);
26
+    xhr.send ();
27
+}

+ 27 - 0
bin/javascript/main.js View File

@@ -0,0 +1,27 @@
1
+//#########################################################
2
+// Part of project php-IRC-js
3
+//
4
+// xhr_write.php - main.js
5
+//
6
+// Author: Mario Chorvath - Bedna
7
+// Start 2016
8
+//
9
+// Licence GNU General Public License
10
+// Version 2
11
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
12
+//#########################################################
13
+
14
+// Read rsponse and sroll page down by timeout
15
+function main () {
16
+	readResponse ();
17
+	document.getElementById("bottom").scrollIntoView();
18
+	setTimeout (main, 1000);
19
+}
20
+
21
+// Scroll page down
22
+function scrollIntoView(eleID) {
23
+   var e = document.getElementById(eleID);
24
+   if (!!e && e.scrollIntoView) {
25
+       e.scrollIntoView();
26
+   }
27
+}

+ 31 - 0
bin/javascript/readResponse.js View File

@@ -0,0 +1,31 @@
1
+//#########################################################
2
+// Part of project php-IRC-js
3
+//
4
+// xhr_write.php - Read responses
5
+//
6
+// Author: Mario Chorvath - Bedna
7
+// Start 2016
8
+//
9
+// Licence GNU General Public License
10
+// Version 2
11
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
12
+//#########################################################
13
+
14
+// Write data on server
15
+function readResponse () {
16
+	// Create new XHR
17
+	var xhr = new XMLHttpRequest(); // New XHR2
18
+	// If "read_response.php" send data back
19
+	xhr.onreadystatechange = function() {
20
+		if (xhr.readyState == 4 && xhr.status == 200) {
21
+			document.getElementById("status").innerHTML = "&gt;&gt;&gt;" + xhr.responseText + "</b><br>";
22
+		}
23
+	};
24
+
25
+	// Send data to server
26
+	xhr.open ("GET", "read_response.php", true); // Sync open file
27
+	xhr.responseType = 'text';
28
+	xhr.setRequestHeader ("Content-Type", "application/octet-stream");
29
+	xhr.setRequestHeader ("X-TEXT", message); // Custom header - file name
30
+	xhr.send (); // Send
31
+};

+ 34 - 0
bin/javascript/send.js View File

@@ -0,0 +1,34 @@
1
+//#########################################################
2
+// Part of project php-IRC-js
3
+//
4
+// xhr_write.php - Read response
5
+//
6
+// Author: Mario Chorvath - Bedna
7
+// Start 2016
8
+//
9
+// Licence GNU General Public License
10
+// Version 2
11
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
12
+//#########################################################
13
+
14
+// Write data on server
15
+function send () {
16
+	// New XHR2 request
17
+	var xhr = new XMLHttpRequest(); // New XHR2
18
+	message = encodeURIComponent (document.getElementById("message").value);
19
+
20
+	console.log ('message :' + message);
21
+
22
+	if (message) {
23
+		// Send data to server
24
+		xhr.open ("GET", "send.php", true); // Sync open file
25
+		xhr.responseType = 'text';
26
+		xhr.setRequestHeader ("Content-Type", "application/octet-stream");
27
+		xhr.setRequestHeader ("X-TEXT", message); // Custom header - file name
28
+		xhr.send (); // Send
29
+	}
30
+	// If mesage is empty
31
+	else {
32
+		alert ("Message is empty");
33
+	}
34
+};

+ 88 - 0
connect.php View File

@@ -0,0 +1,88 @@
1
+<?php
2
+//#########################################################
3
+// Part of project php-IRC-js
4
+//
5
+// connect.php - connect to server and send commands
6
+//
7
+// Author: Mario Chorvath - Bedna
8
+// Start 2016
9
+//
10
+// Licence GNU General Public License
11
+// Version 2
12
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
13
+//#########################################################
14
+
15
+// Settings
16
+$ircServer = "irc.freenode.net";
17
+$ircPort = "6667";
18
+$ircChannel = "#KERNEL_ULTRAS";
19
+
20
+// Set variables
21
+$loop = "loop";
22
+
23
+// Set unlimite execution time
24
+set_time_limit(0);
25
+
26
+// Connect to server
27
+$ircSocket = pfsockopen ($ircServer, $ircPort, $eN, $eS);
28
+
29
+// If socket open
30
+if ($ircSocket) {
31
+	// Login bot
32
+	fwrite ($ircSocket, "USER KU-bot127c 0 lol :Kubo137c\r\n");
33
+	fwrite ($ircSocket, "NICK KU-bot137c\r\n");
34
+	// Join to channel
35
+	fwrite ($ircSocket, "JOIN " . $ircChannel . "\r\n");
36
+	// Send message to channel
37
+	fwrite ($ircSocket, "PRIVMSG #KERNEL_ULTRAS :Im here\r\n");
38
+	// Open file "server_response.txt" for write
39
+	$handle = fopen ("server_response.txt", "w");
40
+	// Open file for logs
41
+	$handle_log = fopen ("log.txt", "w");
42
+	// Write log
43
+	fwrite ($handle_log, "FIRST\n");
44
+
45
+	while (1) {
46
+		// Write log
47
+		fwrite ($handle_log, "MAIN\n");
48
+		// Read command from file "send"
49
+		if (file_exists ("send")) {
50
+			// Read command
51
+			$command = file_get_contents ("send");
52
+			// Remove file
53
+			unlink ("send");
54
+			// Timeout "for sure"
55
+			sleep (1);
56
+			// Write command to socket
57
+			fwrite ($ircSocket, "$command"."\r\n");
58
+			// Flush command to socket
59
+			flush ();
60
+			// Write log
61
+			fwrite ($handle_log, "SEND :$command\n");
62
+		}
63
+		// If IRC server send message to client
64
+		while ($data = fgets ($ircSocket, 128)) {
65
+			// fwrite ($handle_log, "DATA :".$data."\n");
66
+			fwrite ($handle_log, "READ\n");
67
+			// Separate all data by space
68
+			$exData = explode (' ', $data);
69
+			// Send PONG back to the server
70
+			if ($exData[0] == "PING") {
71
+				// Write log
72
+				fwrite ($handle_log, "PING :".$exData[1]."\n");
73
+				// Write PONG to server
74
+				fwrite ($ircSocket, "PONG ".$exData[1]."\r\n");
75
+			}
76
+			// If response other than PING
77
+			else {
78
+				// Write response to file "server_response.txt"
79
+				fwrite ($handle, nl2br ($data));
80
+			}
81
+		}
82
+	}
83
+}
84
+// If error on connection
85
+else {
86
+    echo $eS . ": " . $eN;
87
+}
88
+?>

+ 48 - 0
index.php View File

@@ -0,0 +1,48 @@
1
+<?php
2
+//#########################################################
3
+// Part of project php-IRC-js
4
+//
5
+// index.php - main file
6
+//
7
+// Author: Mario Chorvath - Bedna
8
+// Start 2016
9
+//
10
+// Licence GNU General Public License
11
+// Version 2
12
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
13
+//#########################################################
14
+
15
+echo "
16
+<!DOCTYPE html>
17
+<html>
18
+	<head>
19
+		<meta http-equiv='content-type' content='text/html; charset=UTF-8'>
20
+		<meta name='author' content='Mario Chorvath - Bedňa' >
21
+		<meta name='company' content='KERNEL ULTRAS'>
22
+		<meta name='date' content='(D.M.Y) 31.03.2016'>
23
+		<meta name='robots' content='index,follow'>
24
+		<meta name='description' content='IRC client in PHP'>
25
+		<meta name='keywords' content='Linux,FSF,Free Software,Open Source,Android,Programing,Programovanie'>
26
+		<script type='text/javascript' src='bin/javascript/connect.js'></script>
27
+		<script type='text/javascript' src='bin/javascript/send.js'></script>
28
+		<script type='text/javascript' src='bin/javascript/readResponse.js'></script>
29
+		<script type='text/javascript' src='bin/javascript/main.js'></script>
30
+	</head>
31
+";
32
+
33
+echo "
34
+	<body onload=\"main()\">
35
+		<p><b>STATUS:</b></p>
36
+		<p id=\"status\"></p>
37
+		<form name=\"form_connect\" action=\"javascript:connect()\">
38
+			<input type=\"submit\" name=\"connect\" value=\"Connect\">
39
+		</form>
40
+		<form name=\"formSend\" action=\"javascript:send()\">
41
+			<input type=\"text\" id=\"message\" name=\"message\">
42
+			<input type=\"submit\" name=\"submit\" value=\"Submit\">
43
+		</form>
44
+		<div id=\"bottom\">-----------------------------------------------------</div>
45
+	</body>
46
+</html>
47
+";
48
+?>

+ 35 - 0
read_response.php View File

@@ -0,0 +1,35 @@
1
+<?php
2
+//#########################################################
3
+// Part of project php-IRC-js
4
+//
5
+// connect.php - connect to server and send commands
6
+//
7
+// Author: Mario Chorvath - Bedna
8
+// Start 2016
9
+//
10
+// Licence GNU General Public License
11
+// Version 2
12
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
13
+//#########################################################
14
+
15
+// Define viarable
16
+$response = "";
17
+
18
+// Open file for read responses
19
+if ($handle = @fopen ("server_response.txt", "r")) {
20
+	// Reads lines from file
21
+	while ($line = fgets ($handle)) {
22
+		$response .= $line;
23
+	}
24
+	// Close file
25
+	fclose ($handle);
26
+	// Send lines back to JavaScript
27
+	echo $response;
28
+	flush ();
29
+}
30
+// If error
31
+else {
32
+	echo "ERROR open file 'server_response.txt'";
33
+	flush ();
34
+}
35
+?>

+ 36 - 0
send.php View File

@@ -0,0 +1,36 @@
1
+<?php
2
+//#########################################################
3
+// Part of project php-IRC-js
4
+//
5
+// xhr_write.php - write command to file "send"
6
+//
7
+// Author: Mario Chorvath - Bedna
8
+// Start 2016
9
+//
10
+// Licence GNU General Public License
11
+// Version 2
12
+// http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
13
+//#########################################################
14
+
15
+// Get data from JavaScript
16
+$text = rawurldecode ($_SERVER['HTTP_X_TEXT']);
17
+
18
+// Name must be set
19
+if (!isset ($text)) {
20
+  echo 'ERROR: Name required';
21
+}
22
+
23
+// If file open
24
+if ($handle = fopen ("send", "w")) {
25
+	// Write command to file
26
+	fwrite ($handle, $text);
27
+	fclose ($handle);
28
+	// Send data back to JavaScript
29
+	echo $text;
30
+	flush ();
31
+}
32
+// If error
33
+else {
34
+	echo "ERROR open file 'send'";
35
+}
36
+?>