fputs only returns unknown command - php

I have a very small script inwhich I'm trying to get it to post a command in IRC.
<?php
set_time_limit(0);
$socket = fsockopen("irc.twitch.tv", 6667);
fputs($socket,"USER greatbritishbgbot\n");
fputs($socket,"PASS oauth:95uf89rv74gawp93ilfafeqnihpxb74\n");
fputs($socket,"NICK greatbritishbgbot\n");
fputs($socket,"JOIN #greatbritishbg\n");
$ircChannel = "#greatbritishbg";
while($data = fgets($socket, 128)) {
$msg = "test";
fwrite($socket, "PRIVMSG " . $ircChannel . " :" . $msg . "\n");
echo nl2br($data);
flush();
}
?>
Every time I execute this, I receive the error: greatbritishbgbot HELP:Unknown command.
What is the correct way of using fputs to run an IRC command?
Edit: This question was requested to be closed and downvoted because I am apparently asking for debugging information. Which is incorrect. I have asked for an explanation into how one should use fputs in comparison to how I use it.
I ended up using WildPHP which only needed me to modify a config file.
I highly recommend it.

Related

Multiple fopens - only first one works

I've a strange issue I am encountering. Recently I switched from webhosting to a VPS, mainly because of flexibility a VPS provides me with.
I now however need to setup Apache on my own and I'm not to good at doing so.
I've a .php file and I have 2 fopen in it. The first one does it's job but the second one doesn't work for some reason.
I was wondering, is there some php.ini settings I need to make to allow multiple fopen in a file ?
EDIT
Code below:
$fp = fopen('ticket' . $_SESSION['id'] . '.txt', 'a+');
$savestring = "---";
fwrite($fp, $savestring);
fclose($fp);
$_SESSION['total'] = $total;
$fp = fopen('reqs.txt', 'a+');
$savestring = PHP_EOL . "Ticket Nou: " . $_SESSION['id'] . " | Ora: " . $ordertime . " | IP: " . $ip;
fwrite($fp, $savestring);
fclose($fp);
I shortened the $savestrings, in reality they are longer. The issue occurs with the second file, reqs.txt.
try to use flock
I have a file with 4 fopen it didn't work.
after using flock it worked.

connect master server stream_socket_client

I wrote a script to pull a server list from the master server from Enemy Territory; However it silently fails; I had it working; but for some unknown reason it doesn't anymore.
I used this document to "talk" with the server : http://src.gnu-darwin.org/ports/games/masterserver/work/masterserver-0.4.1/docs/PROTOCOLS
This seems valid since it worked before and it still works on game servers; This is my code :
<?php
// set sv_master1 "etmaster.idsoftware.com"
// set sv_master2 "master0.gamespy.com"
// set sv_master3 "wolfmaster.idsoftware.com"
// set sv_master4 "clanservers.net"
// set sv_master5 "master0.etmaster.net" 213.108.29.23
$host = 'etmaster.idsoftware.com';
$port = '27950';
// $status = chr(255) . chr(255) . chr(255) . chr(255) . chr(0x02) . 'getservers' . chr(0x00);
$status = "\xFF\xFF\xFF\xFFgetservers\x00";
$fp = stream_socket_client("udp://" . $host . ":" . $port, $errno, $errstr, 10);
if (!$fp) {
echo "ERROR: " . $errno . $errstr . "<br>\n";
} else {
fwrite($fp, $status);
//stream_set_timeout($fp, 10); // this is for debugging;
$data = fread($fp, 1024);
fclose($fp);
print_r($data);
}
I attempted 60 seconds execution time (its localhost) but it still doesn't work ... any help is appreciated !
When I check the steps (in cli) the longest time is spend on fread(); but $data contains only an empty string;
The error is small; there had to be 5* \xFF tag; and I only used 4.

Php SMTP Mail sending works only local

I'm trying since hours now to get a mailscript working on a live server. It works as intended on my localhost.
I'm using exact the same external SMTP server, the same PHP sources and the same databasetables too. The PHP version and MySQL version online and localhost are identical.
I tested everything. Everything works as pretended without the physical sending process. It doesn't send (or receive) email. I also went to the systemadministrator to let him check if there are any emails blocked or internal server errors, but here also everything fine.
I really tried everything I could think of but I didn't find the problem. Has anybody an idea what I could try next?
This is how the function for the physical sending looks like:
private function sendPhysical($sReciepient) {
if ($oSmtpIn = fsockopen(SMTPSERVER, SMTPPORT)) {
fputs($oSmtpIn, "EHLO " . SMTPSERVER . "\r\n");
$aCodes["hello"] = fgets($oSmtpIn, 1024);
fputs($oSmtpIn, "auth login\r\n");
$aCodes["res"] = fgets($oSmtpIn, 1024);
fputs($oSmtpIn, $this->encodeUser() . "\r\n");
$aCodes["user"] = fgets($oSmtpIn, 1024);
fputs($oSmtpIn, $this->encodePassword() . "\r\n");
$aCodes["pass"] = fgets($oSmtpIn, 256);
fputs($oSmtpIn, "MAIL FROM: <" . $this->sFrom . ">\r\n");
$aCodes["From"] = fgets($oSmtpIn, 1024);
fputs($oSmtpIn, "RCPT TO: <" . $sReciepient . ">\r\n");
$aCodes["To"] = fgets($oSmtpIn, 1024);
fputs($oSmtpIn, "DATA\r\n");
$aCodes["data"] = fgets($oSmtpIn, 1024);
fputs($oSmtpIn, $this->generateHeader($sReciepient) . "\r\n\r\n" . $this->returnCompiledTemplate() . "\r\n.\r\n");
$aCodes["send"] = fgets($oSmtpIn, 256);
fputs($oSmtpIn, "QUIT\r\n");
fclose($oSmtpIn);
} else {
$aCodes["connection"] = false;
}
return $aCodes;
}
Anybody a possible solution? I'm totally confused because it works at localhost and it already worked at the online server until last week...
It's quite tricky too see what's wrong on with this block of code but if you don't want to have much trouble sending mail via php, try PHPMailer.

Giving PHP permission to access COM port

I'm creating a php script that connects to a 3G modem connected via serial connection on COM5.
I'm getting the following error and I believe it is because php does not have r/w access to COM5:
Warning: fopen(COM5:) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\SMStest\test2.php on line 9
// mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off
$fp = fopen ("COM5:", "w+");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
$e = chr(27);
$string = $e . "A" . $e . "H300";
$string .= $e . "V100" . $e . "XL1SATO";
$string .= $e . "Q1" . $e . "Z";
echo $string;
fputs ($fp, $string );
fclose ($fp);
}
There are many ways to access COM ports on windows, alternatives to your method are opening it with the following paths:
\Device\00000123 (You can find the correct value in device manager, properties, details, physical device object name)
\\.\com5 (This is how I would open the port as a file if I was writing a program in C or something)

Writing then reading back via sockets in PHP issues

I writing a command and then reading back from a server via sockets in PHP. We have 20 servers that all run a Node JS script which can receive these commands and execute them. The Node JS script will return "ok" which PHP reads back to confirm the command has gone through.
The Node JS script listens on port 9000 and is set to allow half open.
Most of the time this works fine, but when a high volume of commands are sent we get errors back occasionally that say this:
Contents: Message received back from socket was 'Unexpected token {'
Transport endpoint is not connected
The transport endpoint message suggests to me that it has not connected successfully.
I am no expert in sockets so I don't know whether the implementation I have used is "correct". It does work most of the time but I am aware that there are functions like socket_bind and socket_listen that may work better, though I am not sure what they do.
Here is the PHP code that we are using. Any suggestions would be most appreciated.
public function sendDaemonCommand($address, $template_id, $params = array()) {
$hostname = $this->getHostnameFromPrivateIP($address);
$port = 9000;
$command = array('template_id' => $template_id, 'params' => $params);
$command = json_encode($command);
// Create a TCP Stream socket
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to create socket on " . $address . "\n\n" . socket_strerror(socket_last_error()) . "\n\nCommand:\n\n" . $command . "\n" . $this->functionTraceback());
return false;
}
// Connect to socket
if (socket_connect($sock, $address, $port) === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to connect to socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_close($sock);
return false;
}
// Write command to socket
$_command = $command;
$length = strlen($_command);
while (true) {
$sent = socket_write($sock, $_command, $length);
if ($sent === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to write command to socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_shutdown($sock, 2);
socket_close($sock);
return false;
}
if ($sent < $length) {
$_command = substr($_command, $sent);
$length -= $sent;
}
else {
break;
}
}
socket_shutdown($sock, 1);
// Read back from socket
if (($out = socket_read($sock, 1024)) !== false) {
#socket_shutdown($sock, 0);
$out = trim($out);
if ($out !== "ok") {
$this->mailError("Command Failed - " . $hostname, "Message received back from socket was '" . $out . "' on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_close($sock);
return false;
}
}
else {
$this->mailError("Command Failed - " . $hostname, "Failed to read from socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_shutdown($sock, 0);
socket_close($sock);
return false;
}
socket_close($sock);
return $out;
}
For a simple socket client such as this, I much prefer fsockopen() - it drastically reduces the complication of the client code and does not require the sockets extension, which is not available everywhere. The main disadvantage to this is that you lose the string error messages, but these are rarely that useful anyway - you still get an error string from creating the socket, and if read/write operations fail it's because the socket has become disconnected.
I'm also not sure how useful "allow half open" is in this context - I think it is likely to create more problems than it solves. The calls to socket_shutdown() are not doing anything useful here (and may be the source of your problems) - you would only use this if you are operating on a socket that will not be closed and may be operated on at a later point in time by some other code. Since you create a new socket and destroy it in the same routine, this is not the case.
Here is your code rewritten to use fsockopen() - as you can see, it is somewhat shorter and simpler. I have also modified it slightly so that it always returns a bool - your code returns either bool FALSE or string ok, and since there are only these two options it makes more sense for the function to always return a bool.
public function sendDaemonCommand($address, $template_id, $params = array()) {
// Prepare data
$hostname = $this->getHostnameFromPrivateIP($address);
$port = 9000;
$command = array('template_id' => $template_id, 'params' => $params);
$_command = $command = json_encode($command);
$length = strlen($_command);
// Connect to socket
if (!$sock = fsockopen($address, $port, $errNo, $errStr)) {
$this->mailError("Command Failed - " . $hostname, "Failed to connect to socket on " . $address . "\n\n" . $errStr . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
}
// Write command to socket
while (true) {
// Try and write data to socket
$sent = fwrite($sock, $_command, $length);
// If it failed, error out
if ($sent === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to write command to socket on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
fclose($sock);
return false;
}
// If there is data left to send, try again
if ($sent < $length) {
$_command = substr($_command, $sent);
$length -= $sent;
continue;
}
// If we get here the write operation was successful
break;
}
// Read back from socket and close it
$out = fread($sock, 1024);
fclose($sock);
// Test the response from the server
if ($out === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to read from socket on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
} else if (($out = trim($out)) !== "ok") {
$this->mailError("Command Failed - " . $hostname, "Message received back from socket was '" . $out . "' on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
} else {
return true;
}
}

Categories