0
This script below is a php socket to receive data from a tk102 vehicular tracker. The problem is that I think it has to run in a specific folder on the server and I don’t know how to do it, and there’s this CLI script issue that I don’t know what it is. If someone has worked with something like this and can help me I appreciate
#!/usr/bin/php -q
<?php
/* Listens for requests and forks on each connection*/
$ip = 'xxx.xxx.xxx.xxx';
$port = 3007;
$__server_listening = true;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
declare(ticks = 1);
if(!isset($argv[1]) || $argv[1] != '-f') {
become_daemon();
}
/* nobody/nogroup, change to your host's uid/gid of the non-priv user */
change_identity(65534, 65534);
/* handle signals */
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
pcntl_signal(SIGCHLD, 'sig_handler');
/* change this to your own host / port */
server_loop($ip, $port);
/**
* Change the identity to a non-priv user
*/
function change_identity( $uid, $gid )
{
if( !posix_setgid( $gid ) )
{
print "Unable to setgid to " . $gid . "!\n";
exit;
}
if( !posix_setuid( $uid ) )
{
print "Unable to setuid to " . $uid . "!\n";
exit;
}
}
/**
* Creates a server socket and listens for incoming client connections
* @param string $address The address to listen on
* @param int $port The port to listen on
*/
function server_loop($address, $port)
{
GLOBAL $__server_listening;
if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)
{
echo "failed to create socket: ".socket_strerror($sock)."\n";
exit();
}
if(($ret = socket_bind($sock, $address, $port)) < 0)
{
echo "failed to bind socket: ".socket_strerror($ret)."\n";
exit();
}
if( ( $ret = socket_listen( $sock, 0 ) ) < 0 )
{
echo "failed to listen to socket: ".socket_strerror($ret)."\n";
exit();
}
socket_set_nonblock($sock);
echo "waiting for clients to connect\n";
while ($__server_listening)
{
$connection = @socket_accept($sock);
if ($connection === false)
{
usleep(100);
}elseif ($connection > 0)
{
handle_client($sock, $connection);
}else
{
echo "error: ".socket_strerror($connection);
die;
}
}
}
/**
* Signal handler
*/
function sig_handler($sig)
{
switch($sig)
{
case SIGTERM:
case SIGINT:
exit();
break;
case SIGCHLD:
pcntl_waitpid(-1, $status);
break;
}
}
/**
* Handle a new client connection
*/
function handle_client($ssock, $csock)
{
GLOBAL $__server_listening;
$pid = pcntl_fork();
if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
die;
}elseif ($pid == 0)
{
/* child process */
$__server_listening = false;
socket_close($ssock);
interact($csock);
socket_close($csock);
}else
{
socket_close($csock);
}
}
function interact($socket)
{
/* TALK TO YOUR CLIENT */
$rec = "";
socket_recv($socket, $rec, 2048, 0);
$parts = split(',',$rec);
$cnx = mysql_connect('localhost', 'mpitech', 'mpi0147#');
/*
Array
(
[0] => 0908242216
[1] => 0033663282263
[2] => GPRMC
[3] => 212442.000
[4] => A
[5] => 4849.0475
[6] => N
[7] => 00219.4763
[8] => E
[9] => 2.29
[10] =>
[11] => 220809
[12] =>
[13] =>
[14] => A*70
[15] => L
[16] => imei:359587017313647
[17] => 101Q
[18] =>
)
*/
$trackerdate = mysql_real_escape_string($parts[0]);
$phone = mysql_real_escape_string($parts[1]);
$gprmc = mysql_real_escape_string($parts[2]);
$satelliteDerivedTime = mysql_real_escape_string($parts[3]);
$satelliteFixStatus = mysql_real_escape_string($parts[4]);
$latitudeDecimalDegrees = mysql_real_escape_string($parts[5]);
$latitudeHemisphere = mysql_real_escape_string($parts[6]);
$longitudeDecimalDegrees = mysql_real_escape_string($parts[7]);
$longitudeHemisphere = mysql_real_escape_string($parts[8]);
$speed = mysql_real_escape_string($parts[9]);
$bearing = mysql_real_escape_string($parts[10]);
$utcDate = mysql_real_escape_string($parts[11]);
// = $parts[12];
// = $parts[13];
$checksum = mysql_real_escape_string($parts[14]);
$gpsSignalIndicator = mysql_real_escape_string($parts[15]);
if(ereg("imei",$parts[16]))
{
$imei = mysql_real_escape_string($parts[16]);
$other = mysql_real_escape_string($parts[17].'
'.$parts[18]);
}
else
{
$imei = mysql_real_escape_string($parts[17]);
$other = mysql_real_escape_string($parts[18].'
'.$parts[19]);
}
$today = date("Y_m_d");
$file = fopen("log/LogPagSeguro.$today.txt", "ab");
$hour = date("H:i:s T");
fwrite($file,"Log de Notificações e consulta\\\\r\\\\n");
fwrite($file,"Hora da consulta: $hour \\\\r\\\\n");
fwrite($file,"HTTP: ".$http['http_code']." \\\\r\\\\n");
fwrite($file,"Código de Notificação:".$notificationCode."
\\\\r\\\\n");
fwrite($file, "Código da transação:".$email."\\\\r\\\\n");
fwrite($file, "Status da transação:".$status."\\\\r\\\\n");
fwrite($file,"________________________________________________ \\\\r\\\\n");
fclose($file);
$imei = substr($imei,5);
//mysql_select_db('mpi2015_rio2016_triplem', $cnx);
//if($gpsSignalIndicator != 'L')
// mysql_query("INSERT INTO gprmc (date, imei, phone, trackerdate, satelliteDerivedTime, satelliteFixStatus, latitudeDecimalDegrees, latitudeHemisphere, longitudeDecimalDegrees, longitudeHemisphere, speed, Bearing, utcDate, Checksum, gpsSignalIndicator, other) VALUES (now(), '$imei', '$phone', '$trackerdate', '$satelliteDerivedTime', '$satelliteFixStatus', '$latitudeDecimalDegrees', '$latitudeHemisphere', '$longitudeDecimalDegrees', '$longitudeHemisphere', '$speed', '$bearing', '$utcDate', '$checksum', '$gpsSignalIndicator', '$other')", $cnx);
//mysql_close($cnx);
}
/**
* Become a daemon by forking and closing the parent
*/
function become_daemon()
{
$pid = pcntl_fork();
if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
exit();
}elseif ($pid)
{
/* close the parent */
exit();
}else
{
/* child becomes our daemon */
posix_setsid();
chdir('/');
umask(0);
return posix_getpid();
}
}
?>
This PHP inside bin is a folder?
– Fabio Henrique
is the compiler of php... a binary file...
– Daniel Omine