Jump to content
minho

PHP + Telnet

Recommended Posts

minho

Boas tardes!

Estou a tentar acrescentar a um SW de PHP/Zend uma função de enviar SMSs, através de cartões SIM inseridos neste Gateway GSM.

Percebo pouco disto, mas do que pesquisei, decidi recorrer à classe PHPTelnet.

Para começar e ir testando, criei um PHP só com o código que interessa, que vou correndo pela shell do servidor.

Um dos problemas que tenho é o comprimento da SMS. Pelo software do gateway, acessível pelo IP, consigo enviar até 160chars, e caso envie um caracter especial, é substituído por código.

Pelo código, o máximo de chars que consigo enviar, é o que está em $message (128char). Caso ultrapasse, ou a mensagem inclua algum acento ou etc, tudo corre como se enviasse, mas a SMS não chega. Mesmo com a msg com menos de 128, nem sempre envia.

O que estarei a fazer de mal? Será algum buffer que não estou a esvaziar ou assim?

Se for fazendo o echo do $result, retorna-me sempre coisas aleatórias, incompletas.

Este é o código que tenho por enquanto:

<?php

require_once "PHPTelnet.php";

$success = false;
$message = '123456789a12345679b12345679c12345679d12345679e12345679f12345679g12345679h12345679i12345679j12345679k12345679l12345679m12345679n';
$number = '"91XXXXXXX"';
$sendcommand= 'at+cmgs=';

$telnet = new PHPTelnet(); // using a function from the included script

$result = $telnet->Connect('xxx.xxx.xxx.xxx','user','pass'); //IP address of the router, username, password

//if( !is_null($req ['p_ref']) && !is_null($req ['numero']) && !is_null($req ['mensagem'])) {


switch ($result)
{
case 0:
echo "Connected!!\n";

$telnet->DoCommand('module1', $result);
//echo $result,"\n";
//$result='';

$telnet->DoCommand('ate1', $result);
//echo $result,"\n";
//$result='';

$telnet->DoCommand('at+cmgf=1', $result); // to see the active telnet session
//echo $result,"\n";
//$result='';

$telnet->DoCommand($cenas.$number, $result);
//echo $result,"\n";
//$result='';

$telnet->DoCommand($message, $result);
// echo $result,"\n";
// $result='';


$telnet->DoCommand(chr(26), $result); //envio de ctrl+Z para suspender o processo, só assim envia a sms.
// echo $result,"\n";
// $result='';

sleep(3); //Se estiver com tempo menor, não envia a SMS.


break;
case 1:
echo '[php Telnet] Connect failed: Unable to open network connection\n';
break;
case 2:
echo '[php Telnet] Connect failed: Unknown host\n';
break;
case 3:
echo '[php Telnet] Connect failed: Login failed\n';
break;
case 4:
echo '[php Telnet] Connect failed: Your PHP version does not support PHP Telnet\n';
break;
}

// }?>

A classe está assim:

<?php
/*
PHPTelnet 1.1.1
by Antone Roundy
adapted from code found on the PHP website
public domain
*/
class PHPTelnet {
var $show_connect_error=1;
var $use_usleep=0; // change to 1 for faster execution
// don't change to 1 on Windows servers unless you have PHP 5
var $sleeptime=125000;
var $loginsleeptime=1000000;
var $fp=NULL;
var $loginprompt;
var $conn1;
var $conn2;

/*
0 = success
1 = couldn't open network connection
2 = unknown host
3 = login failed
4 = PHP version too low
*/
function Connect($server,$user,$pass) {
$rv=0;
$vers=explode('.',PHP_VERSION);
$needvers=array(4,3,0);
$j=count($vers);
$k=count($needvers);
if ($k<$j) $j=$k;
for ($i=0;$i<$j;$i++) {
if (($vers[$i]+0)>$needvers[$i]) break;
if (($vers[$i]+0)<$needvers[$i]) {
$this->ConnectError(4);
return 4;
}
}

$this->Disconnect();

if (strlen($server)) {
if (preg_match('/[^0-9.]/',$server)) {
$ip=gethostbyname($server);
if ($ip==$server) {	
 $ip='';
 $rv=2;
}
} else $ip=$server;
} else $ip='127.0.0.1';

if (strlen($ip)) {
echo $ip, "\n";
if ($this->fp=fsockopen($ip,8023)) {
fputs($this->fp,$this->conn1);
$this->Sleep();

fputs($this->fp,$this->conn2);
$this->Sleep();
$this->GetResponse($r);
$r=explode("\n",$r);
$this->loginprompt=$r[count($r)-1];
echo $user, "\n";
fputs($this->fp,"$user\r");
$this->Sleep();
fputs($this->fp,"$pass\r");
if ($this->use_usleep) usleep($this->loginsleeptime);
else sleep(1);
$this->GetResponse($r);
$r=explode("\n",$r);
if (($r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1])) {
 $rv=3;

 $this->Disconnect();
}
} else $rv=1;
}

if ($rv) $this->ConnectError($rv);
return $rv;
}

function Disconnect($exit=1) {
if ($this->fp) {
if ($exit) $this->DoCommand('exit',$junk);
fclose($this->fp);
$this->fp=NULL;
}
}
function DoCommand($c,&$r) {
if ($this->fp) {
fputs($this->fp,"$c\r");
$this->Sleep();
$this->GetResponse($r);
//$r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r);
}
return $this->fp?1:0;
}

function GetResponse(&$r) {
$r='';
do {
$r.=fread($this->fp,90000000);
$s=socket_get_status($this->fp);
//print_r($s);
} while ($s['unread_bytes']);
}

function Sleep() {
if ($this->use_usleep) usleep($this->sleeptime);
else sleep(1);
}

function PHPTelnet() {
$this->conn1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$this->conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
}

function ConnectError($num) {
if ($this->show_connect_error) switch ($num) {
case 1: echo '[php Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/fsockopen.php">Connect failed: Unable to open network connection</a>'; break;
case 2: echo '[php Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/unknown-host.php">Connect failed: Unknown host</a>'; break;
case 3: echo '[php Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/login.php">Connect failed: Login failed</a>'; break;
case 4: echo '[php Telnet] <a href="http://www.geckotribe.com/php-telnet/errors/php-version.php">Connect failed: Your server\'s PHP version is too low for PHP Telnet</a>'; break;
}
}
}
return;
?>

Agradeço já a quem se disponibilizar a perder tempo com isto :P

Edited by minho

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.