src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/include/tcpclient.php

576 lines
17 KiB
PHP

<?php
/** \file scripts\page\tcpclient.php
* \brief DI webinterface tcp client script.
* \author Rob Schalken, Core|Vision
* \version 1.0
* \date 17-10-2008
*
* This file contains the tcp client scripts.
*/
/**
* tcpclient sync tcp server
*
* Inputs:
* - lance_id: lance database id
* - tcp: tcp server handle
* - items Update item(s) (phonebook/release/id/threshold/i18n/timers/geofence/tz)
* - StoreDb Set flag in syncdb entry zkl tabel, needed when a device is not accessible
* - tcp_server Overrule tcp server from database
*
* Return: Ok(1)/Error(0)
*/
function tcpclient_sync($lance_id, $tcp = FALSE, $items, $storedb = TRUE, $tcp_server = "") {
// Intial value
$result = 0;
// Store sync db items to zkl tabel
// The TCP server will check these at login, so it is no problem when the device is not accessible
if ($storedb) {
db_update_lance_syncdb($lance_id, $items);
}
// Created handle?
if ($tcp === FALSE) {
// Setup new connection
$tcp_channel = tcpclient_connection($lance_id, $tcp_server);
}
else {
$tcp_channel = $tcp;
}
// No Valid connection? => no problem, the next login this action will be handled
if ($tcp_channel !== FALSE) {
// Item not stored in db => parse as argument
if ($storedb) {
// Sync all (so the TCP server will check the zkl tabel)
$result = zkl_send_command($tcp_channel,"\$SYNCDB");
}
else {
// Sync all (so the TCP server will check the zkl tabel)
$result = zkl_send_command($tcp_channel,"\$SYNCDB,\"" . $items. "\"");
}
// Split up entry
$separated_items = split(",", $items);
if (is_array($separated_items)) {
foreach($separated_items as $item) {
// Extra actions (debugging for example)
switch($item) {
case "server":
// Abort tcp server
if (zkl_read_result($tcp_channel, $result, TCP_TIMEOUT) == TCPSTAT_OK) {
zkl_send_command($tcp_channel, "\$ABORT");
}
break;
case "phonebook":
// Download current alarmnummers
if (LOG_ALARM) {
// Intial value
$dummy = "";
// Wait for result of the syncdb
zkl_read_result($tcp_channel, $dummy, TCP_TIMEOUT);
$info = split(",", zkl_retrieve($tcp_channel, "TEL" , TCP_TIMEOUT));
// Debug info
syslog(LOG_NOTICE, "MTinfo amount alarmnr: " . $info[0] . ", amount servernr: " . $info[1]);
for ($i=1; $i <= $info[0]; $i++) {
syslog(LOG_NOTICE, "MTinfo alarmnr (TEL) " . $i . ": " . zkl_retrieve($tcp_channel,"TEL" . $i . "", TCP_TIMEOUT));
}
for ($i=1; $i <= $info[1]; $i++) {
syslog(LOG_NOTICE, "MTinfo alarmnr (TELS) " . $i . ": " . zkl_retrieve($tcp_channel,"TELS" . $i . "", TCP_TIMEOUT));
}
}
break;
default:
// Do nothing
break;
}
}
}
// Close handle?
if ($tcp === FALSE) {
// close ZKL tcp connection
zkl_tcplogout($tcp_channel);
}
}
// Return value
return $result;
}
/**
* tcpclient status (non cached)
*
* Inputs:
* - equipement: Array with equipment info
* - handle_session: Start/stop session
* - max_time: Timeout
* - skip_found_items: Skip found items
*
* Return: array containing $status info (id, idcode, device, found)
*/
function tcpclient_non_cached_status($equipment, $handle_session = TRUE, $max_time = TCP_TIMEOUT, $skip_found_items = TRUE) {
// Initial values
$tcp_channel = array();
$count = 0;
// Setup connection
if (is_array($equipment)) {
foreach ($equipment as $item) {
array_push($tcp_channel, array('id' => $item['id'],
'idcode' => $item['idcode'],
'tcp' => tcpclient_connection($item['id']),
'device' => $item['device'],
'disconnect' => FALSE,
'found' => ((isset($item['found'])) ? $item['found'] : FALSE)));
}
}
// Get zkl cached status
if (is_array($tcp_channel)) {
foreach ($tcp_channel as $item) {
if (((!$item['found']) || (!$skip_found_items)) && ($item['tcp'] !== FALSE)) {
// Extra debug info
//DBG("Non cached status: " . $item['idcode'] . ", db id: " . $item['id'] . ", Found: " . (($item['found']) ? "TRUE" : "FALSE"));
zkl_send_command($item['tcp'], "\$RETR,\"STAT\"");
}
else {
// Extra debug info
//DBG("Non cached status invalid socket: " . $item['idcode'] . ", db id: " . $item['id'] . ", Found: " . (($item['found']) ? "TRUE" : "FALSE"));
// This is a non-valid socket
$count++;
}
}
}
// Define timeout
$timeout = $max_time;
while(($count != sizeof($tcp_channel)) && ($timeout > 0)) {
// Clear array
$sockets = array();
foreach ($tcp_channel as $item) {
// Equipment found? Disconnected meanwhile?
if (((!$item['found']) || (!$skip_found_items)) && ($item['tcp'] !== FALSE) && ($item['disconnect'] === FALSE)) {
array_push($sockets, $item['tcp']);
}
}
if ($handle_session) {
// "Stop" session
SessionStop();
}
// To be sure
if ($timeout < 0.0) $timeout = 0.0;
// Wait for socket
$n = wait_for_sockets($sockets, $timeout);
if ($handle_session) {
// Restart session
SessionStart();
}
// Socket found?
if (($n < 0) || ($n == 0)) {
// Not ok/Timeout => End of search
break;
}
else {
for($i=0; $i<sizeof($tcp_channel); $i++) {
if (in_array($tcp_channel[$i]['tcp'], $sockets)) {
// Timeout is '0' because there is data on the socket
if (intval(zkl_read_result($tcp_channel[$i]['tcp'], $status, 0),16) == TCPSTAT_OK) {
// The status string becomes array
$tcp_channel[$i]['status'] = zkl_interpret_status($status);
// Set flag
$tcp_channel[$i]['found'] = TRUE;
// Increment counter
$count++;
}
else {
// Probably disconnected
$tcp_channel[$i]['disconnect'] = TRUE;
// This is a non-valid socket any more
$count++;
}
}
}
}
}
// Disable connections
if (is_array($tcp_channel)) {
foreach ($tcp_channel as $item) {
if ($item['tcp'] !== FALSE) {
zkl_tcplogout($item['tcp']);
}
}
}
// Remove array
return $tcp_channel;
}
/**
* tcpclient status (cached)
*
* Inputs:
* - equipement: Array with equipment info
* - handle_session: Start/stop session
* - skip_found_items Skip found items
*
* Return: array containing $status info (id, idcode, device, found)
*/
function tcpclient_cached_status($equipment, $handle_session = TRUE, $skip_found_items = TRUE) {
// Initial values
$tcp_channel = array();
$count = 0;
// Setup connection
if (is_array($equipment)) {
foreach ($equipment as $item) {
array_push($tcp_channel, array('id' => $item['id'],
'idcode' => $item['idcode'],
'tcp' => tcpclient_connection($item['id']),
'device' => $item['device'],
'disconnect' => FALSE,
'found' => ((isset($item['found'])) ? $item['found'] : FALSE)));
}
}
// Get zkl cached status
if (is_array($tcp_channel)) {
foreach ($tcp_channel as $item) {
if (((!$item['found']) || (!$skip_found_items)) && ($item['tcp'] !== FALSE) && ($item['disconnect'] === FALSE)) {
// Extra debug info
//DBG("Cached status: " . $item['idcode'] . ", db id: " . $item['id'] . ", Found: " . (($item['found']) ? "TRUE" : "FALSE"));
zkl_send_command($item['tcp'], "\$STAT");
}
else {
// Extra debug info
//DBG("Cached status invalid socket: " . $item['idcode'] . ", db id: " . $item['id'] . ", Found: " . (($item['found']) ? "TRUE" : "FALSE"));
$count++;
}
}
}
// Define timeout
$timeout = TCP_TIMEOUT;
while(($count != sizeof($tcp_channel)) && ($timeout > 0)) {
// Clear array
$sockets = array();
foreach ($tcp_channel as $item) {
// Equipment found?
if (((!$item['found']) || (!$skip_found_items)) && ($item['tcp'] !== FALSE)) {
array_push($sockets, $item['tcp']);
}
}
if ($handle_session) {
// "Stop" session
SessionStop();
}
// To be sure
if ($timeout < 0.0) $timeout = 0.0;
// Wait for socket
$n = wait_for_sockets($sockets, $timeout);
if ($handle_session) {
// Restart session
SessionStart();
}
// Socket found?
if (($n < 0) || ($n == 0)) {
// Not ok/Timeout => End of search
break;
}
else {
for($i=0; $i<sizeof($tcp_channel); $i++) {
if (in_array($tcp_channel[$i]['tcp'], $sockets)) {
// Timeout is '0' because there is data on the socket
if (intval(zkl_read_result($tcp_channel[$i]['tcp'], $status, 0),16) == TCPSTAT_OK) {
// The status string becomes array
$tcp_channel[$i]['status'] = zkl_interpret_status($status);
// Set flag
$tcp_channel[$i]['found'] = TRUE;
// Increment counter
$count++;
}
else {
// Probably disconnected
$tcp_channel[$i]['disconnect'] = TRUE;
// This is a non-valid socket any more
$count++;
}
}
}
}
}
// Disable connections
if (is_array($tcp_channel)) {
foreach ($tcp_channel as $item) {
if ($item['tcp'] !== FALSE) {
zkl_tcplogout($item['tcp']);
}
}
}
// Remove array
return $tcp_channel;
}
/**
* tcpclient store
*
* Inputs:
* - lance_id: lance database id
*
* Return: OK(1)/Error(0)
*/
function tcpclient_store($lance_id, $command, $value, $check = TRUE) {
// Intial value
$result = 0;
// Setup connection
$tcp_channel = tcpclient_connection($lance_id);
// Valid connection?
if ($tcp_channel !== FALSE) {
// Store new value
zkl_store($tcp_channel, $command, $value);
if ($check) {
// Initial value
$status = "";
// Timeout initiate
$timeout = TCP_TIMEOUT;
$start = microtime_float();
while (($status != $value) && ($timeout)) {
// Retrieve status
$status = (zkl_retrieve($tcp_channel, $command, $timeout) == "ON") ? 1 : 0;
// Reinitiate timeout
$end = (microtime_float() - $start);
$timeout = ($end >= TCP_TIMEOUT) ? 0 : (TCP_TIMEOUT - $end);
}
}
else {
// Force an OK result
$status = $value;
}
// close ZKL tcp connection
zkl_tcplogout($tcp_channel);
}
// Return value
return (($tcp_channel === FALSE) || ($value != $status)) ? 0 : 1;
}
/**
* setup tcpclient connection
*
* Inputs:
* - lance_id: lance database id"
* - adres: tcp server adres
*
* Return: tcp_channel (socket)
*/
function tcpclient_connection($lance_id, $adres = "") {
if (!strlen($adres)) {
// Retrieve lance info
$lance_info = db_fetch_lance($lance_id, "", 1);
// Fetch tcp server
$tcp_server = db_fetch("server", "adres","id='" . $lance_info['tcp_server'] . "'");
// Extra check
if (is_array($tcp_server)) {
// Get adres from database
$adres = $tcp_server[0]['adres'];
}
}
// Set up TCP connection
if (strlen($adres)) {
$tcp_channel = zkl_tcplogin($lance_id,
$adres,
NULL,
NULL);
// Return value
return $tcp_channel;
}
else return FALSE;
}
/**
* tcpclient send sms
*
* Inputs:
* - lance Lance array (with at least 'telefoonnr', 'imsi' and 'sms_server') _or_ telephone number (string)
* - value Message
*
* Return: OK(1)/Error(0)
*/
function tcpclient_send_sms($lance, $value) {
global $zkl_error;
global $_PAGE_INFO;
require_once("m2mapi.php");
// Intial value
$status = FALSE;
$max_length = 160;
// is the 'lance' parameter an array with lance info? if not, it must be a telephone number
if( is_array($lance) ) {
if( m2mapi_enabled_for_device($lance) ) {
// message will be sent via the M2M API
$tel_nmr = FALSE;
$sms_server = FALSE;
}
else {
// send message directly, as an SMS, to the device
$tel_nmr = $lance['telefoonnr'];
// send the message via the default SMS server
$sms_server = db_fetch("server", "adres","type='sms-server' AND `default`='1'");
$sms_server = $sms_server[0]['adres'];
}
}
else {
// send message, as SMS, to the provided telephone number
$tel_nmr = $lance;
// Get SMS server
$sms_server = db_fetch("server", "adres","type='sms-server' AND `default`='1'");
$sms_server = $sms_server[0]['adres'];
}
if( $tel_nmr ) {
// Remove chars ().- from telephone
$tel_nmr = str_replace(array("(",")",","," ","-"), "", $tel_nmr);
}
// Get next page and new page chars
if( $_SESSION[$_PAGE_INFO['id']]['i18n'] ) {
$next_page = db_fetch("sms_vertaling", "tekst", "`KEY`='next_page' and i18n='" . $_SESSION[$_PAGE_INFO['id']]['i18n'] . "'");
$next_page = " " . $next_page[0]['tekst'];
$new_page = db_fetch("sms_vertaling", "tekst", "`KEY`='new_page' and i18n='" . $_SESSION[$_PAGE_INFO['id']]['i18n'] . "'");
$new_page = $new_page[0]['tekst'] . " ";
}
else {
$next_page = "";
$new_page = "";
}
// Initial values
$ivalue = $value;
$count = 0;
do {
// Determine max length
$search_space = FALSE;
if (!$count) {
if (strlen($ivalue) > ($max_length - strlen($new_page) - strlen($next_page))) {
$length_message = $max_length - strlen($new_page) - strlen($next_page);
$search_space = TRUE;
}
else {
$length_message = strlen($ivalue);
}
}
else {
if (strlen($ivalue) > ($max_length - strlen($next_page))) {
$length_message = $max_length - strlen($next_page);
$search_space = TRUE;
}
else {
$length_message = strlen($ivalue);
}
}
// Get substring
$temp = substr($ivalue, 0, $length_message);
// Get last space position
if ($search_space) {
$pos = strripos($temp, " ");
if ($pos !== FALSE) {
$temp = substr($ivalue, 0, $pos);
}
}
// Retrieve rest of SMS message (needed to determine of next page symbol must be placed)
$ivalue = substr($ivalue, strlen($temp) + 1);
// Trim message
$temp = trim($temp);
// Add text
if ($count) {
$temp = sprintf($new_page . $temp, ($count + 1));
}
// Some SMS message left?
if (strlen($ivalue)) {
$temp = sprintf($temp . $next_page);
}
// Send sms
if( $tel_nmr ) {
$status = zkl_send_sms($sms_server, $tel_nmr, $temp);
}
else {
$status = m2mapi_send_sms($lance, $temp);
}
// Write result to syslog!
if ($status === FALSE) {
DBG("SMS error, " . ($sms_server ? ("Server: " . $sms_server . ", ") : "") . ($tel_nmr ? ("Tel: " . $tel_nmr) : ("IMSI (via M2M API): " . $lance['imsi'])) . ", Text: " . $temp . ", Error: " . $zkl_error);
}
else {
DBG("SMS ok, " . ($sms_server ? ("Server: " . $sms_server . ", ") : "") . ($tel_nmr ? ("Tel: " . $tel_nmr) : ("IMSI (via M2M API): " . $lance['imsi'])) . ", Text: " . $temp);
}
// Increment counter
$count++;
}
while((strlen($ivalue)) && ($status));
// Return value
return $status;
}
?>