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 $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= 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; } ?>