1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| <?php
$ip = '127.0.0.1';
$port = 8050;
$__server_listening = true;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
declare(ticks = 1);
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, 20480, 0);
$parts = preg_split(',',$rec);
$cnx = mysql_connect('localhost', 'user', 'password');
/*
Array
(
[0] => 0908242216
[1] => 0033663282263
[2] => GPRMC
[3] => 212442.000
[4] => A
[5] => 4849.0475
[6] => N
[7] => 00219.4763
=> 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);
$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]);
}
$imei = substr($imei,5);
$other=$rec;
mysql_select_db('tracker', $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();
}
}
?> |
Partager