1002 lines
34 KiB
PHP
1002 lines
34 KiB
PHP
<?php
|
|
/** \file include\secure_server.php
|
|
* \brief DI webinterface secure serverfunctions
|
|
* \author Rob Schalken, Core|Vision
|
|
* \version 1.0
|
|
* \date 17-10-2008
|
|
*
|
|
* This file contains the secure server functions.
|
|
*/
|
|
|
|
|
|
/*
|
|
* Required pages
|
|
*/
|
|
require_once("db.php");
|
|
require_once("cp3000-tcpclient.php");
|
|
|
|
|
|
/*
|
|
* Definitions
|
|
*/
|
|
define("TIMEOUT_RELEASE", 30); // Timeout releasing project
|
|
define("TIMEOUT_DEFAULT", 10); // Default timeout
|
|
|
|
|
|
/**
|
|
* Setup the connection with the secure server
|
|
*
|
|
* Return: Handle to secure server
|
|
*/
|
|
function sc_connect() {
|
|
GLOBAL $_DEFAULT;
|
|
|
|
// Initial values
|
|
$socket = 0;
|
|
|
|
// Find 'THE' secure server (default.php)
|
|
$server = $_DEFAULT['di-secure'];
|
|
|
|
// Check if the server exists
|
|
if (strlen($server)) {
|
|
// Setup connection
|
|
$socket = zkl_connect($server);
|
|
}
|
|
else {
|
|
// Store to system log
|
|
DBG("Invalid secure server: " . $server);
|
|
}
|
|
|
|
return $socket;
|
|
}
|
|
|
|
|
|
/**
|
|
* Disconnect the connection with the secure server
|
|
*
|
|
* Inputs:
|
|
* - socket: Secure server socket
|
|
*/
|
|
function sc_disconnect($socket) {
|
|
// Disconnect the connection
|
|
zkl_disconnect($socket);
|
|
}
|
|
|
|
|
|
/**
|
|
* Create debug stream (for debug purposes)
|
|
*
|
|
* Inputs:
|
|
* - format: Data format
|
|
* - Arg: Arguments
|
|
*
|
|
* Return: Data stream
|
|
*/
|
|
function sc_debug_stream($fmt, $args = NULL) {
|
|
// Create stream for debug purposes)
|
|
$cmd = vsprintf($fmt, $args);
|
|
|
|
// Return value
|
|
return sprintf("%s*%02X", $cmd, calc_checksum($cmd));
|
|
}
|
|
|
|
|
|
/**
|
|
* Get private key
|
|
*
|
|
* Return: private key
|
|
*/
|
|
function sc_getprivatekey() {
|
|
// Get private key
|
|
return openssl_pkey_get_private(implode("", file(CERT)), CERT_PASSPHRASE);
|
|
}
|
|
|
|
|
|
/**
|
|
* Calculate signature
|
|
*
|
|
* Inputs:
|
|
* - sign_data Data array needed for signature
|
|
* - sign_data sha1 only, or use openssl signature?
|
|
*
|
|
* Return: private key
|
|
*/
|
|
function sc_calculate_signature($sign_data, $sha1_only = FALSE) {
|
|
$sign = "";
|
|
|
|
if ($sha1_only) {
|
|
// Generate signature
|
|
$sign = sha1(implode("," ,$sign_data));
|
|
}
|
|
else {
|
|
// Generate signature
|
|
$result = openssl_sign(implode("," ,$sign_data), $signature, sc_getprivatekey());
|
|
|
|
// Return result
|
|
$sign = ($result) ? bin2hex($signature) : "";
|
|
}
|
|
|
|
// Return signature
|
|
return $sign;
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI start
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - project: Array containing project info (users/equip/general)
|
|
* - reason: Reason
|
|
* - required_state: Required state
|
|
* - sign_data Data array needed for signature
|
|
* - stream_data Data stream for debug purposes
|
|
* Return: OK (1)/NOK (0)
|
|
*/
|
|
function sc_wi_start($socket, $project, $reason, $required_state, &$sign_data, &$stream_data, &$device_size) {
|
|
GLOBAL $_PAGE_INFO;
|
|
|
|
// Create array
|
|
$WISTART = array();
|
|
|
|
// Add id
|
|
array_push($WISTART, $project['general']['id']);
|
|
|
|
// Add name
|
|
array_push($WISTART, $project['general']['naam']);
|
|
|
|
// Add reason
|
|
array_push($WISTART, $reason);
|
|
|
|
// Add required state (when required)
|
|
if (!strlen($required_state)) {
|
|
switch($reason) {
|
|
case "ontwerp gewijzigd":
|
|
case "ontwerp gereed":
|
|
case "ontwerp geverifieerd":
|
|
case "ontwerp gevalideerd":
|
|
case "nieuw":
|
|
array_push($WISTART, $project['general']['ostatus']);
|
|
break;
|
|
case "planning gewijzigd":
|
|
case "planning gereed":
|
|
case "planning geverifieerd":
|
|
case "planning gevalideerd":
|
|
array_push($WISTART, $project['general']['pstatus']);
|
|
break;
|
|
case "vrijgave":
|
|
case "teruggegeven":
|
|
array_push($WISTART, $project['general']['sstatus']);
|
|
break;
|
|
case "verzoek tot inschakelen":
|
|
case "verzoek tot uitschakelen":
|
|
case "inschakelen":
|
|
case "uitschakelen":
|
|
array_push($WISTART, $reason);
|
|
break;
|
|
default:
|
|
array_push($WISTART, "");
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
array_push($WISTART, $required_state);
|
|
}
|
|
|
|
// Add size of project equipment with switching capabilities (and save, needed for WIEND later on)
|
|
$device_size = ((is_array($project['equip'])) && (!empty($project['equip']))) ? sizeof($project['equip']) : 0;
|
|
array_push($WISTART, $device_size);
|
|
|
|
// Add size of project users with switching rights
|
|
array_push($WISTART, ((is_array($project['users'])) && (!empty($project['users']))) ? sizeof($project['users']) : 0);
|
|
|
|
// Add current user (needed for logging/switching)
|
|
array_push($WISTART, $_SESSION[$_PAGE_INFO['id']]['login']['user']['id']);
|
|
|
|
// Retrieve data needed for signature
|
|
if (is_array($WISTART)) {
|
|
foreach($WISTART as $item) {
|
|
array_push($sign_data, $item);
|
|
}
|
|
}
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WISTART,\"%s\"", implode("\",\"", $WISTART)) . "; ";
|
|
|
|
// Calculate checksum and send start wi command
|
|
return zkl_send_command($socket, "\$WISTART,\"%s\"", implode("\",\"", $WISTART));
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI timeout
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - user: timeout in seconds
|
|
*
|
|
* Return: result
|
|
*/
|
|
function sc_wi_timeout($socket, $timeout, &$sign_data, &$stream_data) {
|
|
GLOBAL $_PAGE_INFO;
|
|
|
|
// Create array
|
|
$WITO = array();
|
|
|
|
// Add timeout
|
|
array_push($WITO, $timeout);
|
|
|
|
// Retrieve data needed for signature
|
|
if (is_array($WITO)) {
|
|
foreach($WITO as $item) {
|
|
array_push($sign_data, $item);
|
|
}
|
|
}
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WITO,\"%s\"", implode("\",\"", $WITO)) . "; ";
|
|
|
|
// Calculate checksum and send wi timeout command
|
|
return zkl_send_command($socket, "\$WITO,\"%s\"", implode("\",\"", $WITO));
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI user
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - user: Array containing user info
|
|
* - sign_data Data array needed for signature
|
|
* - stream_data Data stream for debug purposes
|
|
*
|
|
* Return: result
|
|
*/
|
|
function sc_wi_user($socket, $user, &$sign_data, &$stream_data) {
|
|
GLOBAL $_PAGE_INFO;
|
|
|
|
// Create array
|
|
$WIUSER = array();
|
|
|
|
// Add id
|
|
array_push($WIUSER, $user['id']);
|
|
|
|
// Add username
|
|
array_push($WIUSER, $user['gebruikersnaam']);
|
|
|
|
// Add i18n
|
|
array_push($WIUSER, $user['i18n']);
|
|
|
|
// Add timezone
|
|
array_push($WIUSER, $user['tz']);
|
|
|
|
// Add verification method
|
|
array_push($WIUSER, $user['verificatie']);
|
|
|
|
// Add verification info
|
|
switch($user['verificatie']) {
|
|
case "sms":
|
|
array_push($WIUSER, $user['alarmnr']);
|
|
break;
|
|
case "pin":
|
|
array_push($WIUSER, $user['pin']);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
// Retrieve data needed for signature
|
|
if (is_array($WIUSER)) {
|
|
foreach($WIUSER as $item) {
|
|
array_push($sign_data, $item);
|
|
}
|
|
}
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WIUSER,\"%s\"", implode("\",\"", $WIUSER)) . "; ";
|
|
|
|
// Calculate checksum and send wi user command
|
|
return zkl_send_command($socket, "\$WIUSER,\"%s\"", implode("\",\"", $WIUSER));
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI device
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - equip: Array containing equipment info
|
|
* - sign_data Data array needed for signature
|
|
* - stream_data Data stream for debug purposes
|
|
*
|
|
* Return: result
|
|
*/
|
|
function sc_wi_device($socket, $equip, &$sign_data, &$stream_data) {
|
|
GLOBAL $_PAGE_INFO;
|
|
|
|
// Create array
|
|
$WIDEVICE = array();
|
|
|
|
// Add id
|
|
array_push($WIDEVICE, $equip['id']);
|
|
|
|
// Add switchable
|
|
array_push($WIDEVICE, (db_check_system_device_capabilities($equip['device'], array("kortsluiting schakelen"))) ? "1" : "0");
|
|
|
|
// Add imei
|
|
array_push($WIDEVICE, substr($equip['imei'], -15));
|
|
|
|
// Add serial
|
|
array_push($WIDEVICE, $equip['serienr']);
|
|
|
|
// Add idcode
|
|
array_push($WIDEVICE, $equip['idcode']);
|
|
|
|
// Add identification code
|
|
array_push($WIDEVICE, sha1($equip['imei']));
|
|
|
|
// Add tcp server
|
|
$tcp_server = db_fetch("server", "adres","id='" . $equip['tcp_server'] . "'");
|
|
|
|
if (is_array($tcp_server)) {
|
|
array_push($WIDEVICE, $tcp_server[0]['adres']);
|
|
}
|
|
else {
|
|
array_push($WIDEVICE, "");
|
|
}
|
|
|
|
// Retrieve data needed for signature
|
|
if (is_array($WIDEVICE)) {
|
|
foreach($WIDEVICE as $item) {
|
|
array_push($sign_data, $item);
|
|
}
|
|
}
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WIDEVICE,\"%s\"", implode("\",\"", $WIDEVICE)) . "; ";
|
|
|
|
// Calculate checksum and send wi device command
|
|
return zkl_send_command($socket, "\$WIDEVICE,\"%s\"", implode("\",\"", $WIDEVICE));
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI request
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - user: Array containing user info
|
|
* - sign_data Data array needed for signature
|
|
* - stream_data Data stream for debug purposes
|
|
*
|
|
* Return: result
|
|
*/
|
|
function sc_wi_req($socket, $user, &$sign_data, &$stream_data) {
|
|
GLOBAL $_PAGE_INFO;
|
|
|
|
// Create array
|
|
$WIREQ = array();
|
|
|
|
// Add id
|
|
array_push($WIREQ, $user['id']);
|
|
|
|
// Add verification method
|
|
array_push($WIREQ, $user['verificatie']);
|
|
|
|
// Retrieve data needed for signature
|
|
if (is_array($WIREQ)) {
|
|
foreach($WIREQ as $item) {
|
|
array_push($sign_data, $item);
|
|
}
|
|
}
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WIREQ,\"%s\"", implode("\",\"", $WIREQ)) . "; ";
|
|
|
|
// Calculate checksum and send wi request command
|
|
return zkl_send_command($socket, "\$WIREQ,\"%s\"", implode("\",\"", $WIREQ));
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI verify
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - user: Array containing user info
|
|
* - code: Verification code
|
|
* - sign_data Data array needed for signature
|
|
* - stream_data Data stream for debug purposes
|
|
*
|
|
* Return: result
|
|
*/
|
|
function sc_wi_ver($socket, $user, $code, &$sign_data, &$stream_data) {
|
|
GLOBAL $_PAGE_INFO;
|
|
|
|
// Create array
|
|
$WIVERIFY = array();
|
|
|
|
// Add id
|
|
array_push($WIVERIFY, $user['id']);
|
|
|
|
// Add verification method
|
|
array_push($WIVERIFY, $user['verificatie']);
|
|
|
|
// Add verification code
|
|
array_push($WIVERIFY, $code);
|
|
|
|
// Retrieve data needed for signature
|
|
if (is_array($WIVERIFY)) {
|
|
foreach($WIVERIFY as $item) {
|
|
array_push($sign_data, $item);
|
|
}
|
|
}
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WIVERIFY,\"%s\"", implode("\",\"", $WIVERIFY)) . "; ";
|
|
|
|
// Calculate checksum and send wi verify command
|
|
return zkl_send_command($socket, "\$WIVERIFY,\"%s\"", implode("\",\"", $WIVERIFY));
|
|
}
|
|
|
|
|
|
/**
|
|
* Send WI end (and calculate signature)
|
|
*
|
|
* Inputs:
|
|
* - socket Secure server socket
|
|
* - reason WI update reason
|
|
* - data array containing intermediate info
|
|
* - sign_data Data array needed for signature
|
|
* - device_size Number of devices, needed to handle intermediate replies
|
|
* - stream_data Data stream for debug purposes
|
|
* - seqnr Work instruction sequence number
|
|
*
|
|
* Return: result/empty when ok
|
|
*/
|
|
function sc_wi_end($socket, $reason, &$data, $sign_data, $device_size, &$stream_data, &$seqnr) {
|
|
// End storing wi
|
|
$WIEND = array(sc_calculate_signature($sign_data, TRUE));
|
|
|
|
// Calculate checksum and send end wi command
|
|
$result = zkl_send_command($socket, "\$WIEND,\"%s\"", implode("\",\"", $WIEND));
|
|
|
|
// Create stream (debug purposes)
|
|
$stream_data .= sc_debug_stream("\$WIEND,\"%s\"", implode("\",\"", $WIEND)) . "; ";
|
|
|
|
if ($result) {
|
|
// "Stop" session because this is blocking other xmlhttp request
|
|
SessionStop();
|
|
|
|
// Read result (intermediates catch by zklclient.php)
|
|
$result = zkl_read_result($socket, $zkl_data);
|
|
|
|
// Restart session
|
|
SessionStart();
|
|
|
|
// Parse result
|
|
$zkl_data_array = explode(",", $zkl_data);
|
|
|
|
// Array containing intermediates
|
|
$intermediates = array("vrijgave" ,
|
|
"teruggegeven" ,
|
|
"verzoek tot inschakelen" ,
|
|
"inschakelen" ,
|
|
"verzoek tot uitschakelen" ,
|
|
"uitschakelen" ,
|
|
"led vol" ,
|
|
"led gedimd" ,
|
|
"extern contact aan" ,
|
|
"extern contact uit" ,
|
|
"lantaarn aan" ,
|
|
"lantaarn uit" ,
|
|
"relais aan" ,
|
|
"relais uit" ,
|
|
"gps aan" );
|
|
|
|
// Parse intermediates
|
|
if (((int)$result < 0x10) && (in_array($reason, $intermediates))) {
|
|
if (is_array($zkl_data_array)) {
|
|
for($i=0; $i < ($device_size * 2); $i+=2) {
|
|
if (is_array($data)) {
|
|
// Intermediate format: !FF,"<device-id>,<statuscode>"*<checksum>
|
|
array_push($data, array("id" => $zkl_data_array[$i], "code" => $zkl_data_array[$i + 1]));
|
|
}
|
|
|
|
// Store intermediates
|
|
$stream_data .= "Intermediate result: !FF, \"" . $zkl_data_array[$i] . "," . $zkl_data_array[$i + 1] . "\"*; ";
|
|
}
|
|
}
|
|
}
|
|
|
|
// Store result
|
|
$stream_data .= "Result: !" . dechex($result) . ",\"";
|
|
|
|
if (is_array($zkl_data_array)) {
|
|
$offset = (((int)$result < 0x10) && (in_array($reason, $intermediates))) ? $device_size : 0;
|
|
for($i=($offset * 2); $i < sizeof($zkl_data_array); $i++) {
|
|
if ($i != ($offset * 2)) {
|
|
$stream_data .= ",";
|
|
}
|
|
|
|
// Get sequence number
|
|
if ($i == (($offset * 2) + 1)) {
|
|
$seqnr = $zkl_data_array[$i];
|
|
}
|
|
|
|
// Store extra result info
|
|
$stream_data .= $zkl_data_array[$i];
|
|
}
|
|
}
|
|
|
|
$stream_data .= "\"*; ";
|
|
}
|
|
|
|
// Valid result and
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Store/update/handle work instruction using the secure server
|
|
*
|
|
* Inputs:
|
|
* - project_id: Project id
|
|
* - Parent: Parent project or child (period)
|
|
* - data array containing intermediate info
|
|
* - original: Array containing original project info (users/equip/general)
|
|
* - reason WI update reason
|
|
* - required_state WI required state
|
|
* - equipment Equipment (overrules project settings)
|
|
* - code Verification code
|
|
* - emulated_status Emulated project status, this to make it possible to store the complete db
|
|
* - seqnr Work instruction sequence number
|
|
*
|
|
* Return: Ok(0)/Error(1)
|
|
*/
|
|
function sc_wi($project_id, $parent = TRUE, &$data = null, $original = "", $reason = "", $required_state = "", $equipment = "", $code = "", $emulated_status = "", &$seqnr = null) {
|
|
GLOBAL $_PAGE_INFO;
|
|
GLOBAL $_SERVER;
|
|
|
|
// Initial values
|
|
$result = TCPSTAT_OK;
|
|
$sign_data = array();
|
|
$stream_data = "";
|
|
$seqnr = "";
|
|
$debug_info = array();
|
|
|
|
// Start time
|
|
$start = microtime(TRUE);
|
|
|
|
// Create result array
|
|
if (!is_array($data)) {
|
|
$data = array();
|
|
}
|
|
|
|
// Get project info
|
|
$project['general'] = db_fetch_project($project_id, "", 1);
|
|
$project['equip'] = db_fetch_project_lances($project_id, "", array(array("gsm","gprs")));
|
|
$project['users'] = db_fetch_project_users($project_id, "", "schakelen");
|
|
|
|
// Fix project name (remove trailing '_#NEW' when available), only for project change!!
|
|
if (($_SESSION[$_PAGE_INFO['id']]['action'] == "project_design") && (is_array($original))) {
|
|
if ((strpos($project['general']['naam'], '_#NEW') !== FALSE)) {
|
|
$project['general']['naam'] = (strpos($project['general']['naam'], '_#NEW') == (strlen($project['general']['naam']) - strlen('_#NEW'))) ? substr($project['general']['naam'], 0, (strlen($project['general']['naam']) - strlen('_#NEW'))) : $project['general']['naam'];
|
|
}
|
|
}
|
|
|
|
// Debug info
|
|
array_push($debug_info, "Action: " . $_SESSION[$_PAGE_INFO['id']]['action']);
|
|
if (strlen($reason)) {
|
|
array_push($debug_info, "Changes: " . $reason . "(forced)");
|
|
}
|
|
|
|
// Fix sstatus => 'request for release' equals 'non-released' => Needed to power the devices
|
|
if (is_array($original)) {
|
|
$original['general']['sstatus'] = ($original['general']['sstatus'] == "verzoek tot vrijgeven") ? "niet vrijgegeven" : $original['general']['sstatus'];
|
|
}
|
|
$project['general']['sstatus'] = ($project['general']['sstatus'] == "verzoek tot vrijgeven") ? "niet vrijgegeven" : $project['general']['sstatus'];
|
|
|
|
// Get non-switching equipment from parent (including RS3000)
|
|
if ((!$parent) && (!is_array($equipment))) {
|
|
$project['equip_non_switching'] = db_fetch_project_lances($project['general']['parent'], "", array(array("gsm","gprs")));
|
|
|
|
if (is_array($project['equip_non_switching'])) {
|
|
foreach($project['equip_non_switching'] as $item) {
|
|
// non-switching?
|
|
if (!db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) {
|
|
// valid array?
|
|
if (!is_array($project['equip'])) {
|
|
$project['equip'] = array();
|
|
}
|
|
array_push($project['equip'], $item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Overruled equipment? Be aware that the reason must be known, otherwhise the original
|
|
// equipment probably differs from the project equipment and the wrong update reason will be chosen!
|
|
$project['equip'] = (is_array($equipment)) ? $equipment : $project['equip'];
|
|
|
|
// Find out the update reason (this array includes priority)
|
|
$changes = array("type","status","sstatus","naam","ostatus","pstatus");
|
|
|
|
// Emulate project status?
|
|
if ((is_array($emulated_status)) && (is_array($changes))) {
|
|
foreach ($changes as $change) {
|
|
if (array_key_exists($change, $emulated_status)) {
|
|
$project['general'][$change] = $emulated_status[$change];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reason known?
|
|
if (!strlen($reason)) {
|
|
// Regular project? => Skip, or the type must be changed
|
|
if (($project['general']['type'] != "normaal") || ((is_array($original)) && (!empty($original)) && ($original['general']['type'] != "normaal"))) {
|
|
// New project?
|
|
if (((is_array($original)) && (empty($original))) || ((!is_array($original)) && (!strlen($original)))) {
|
|
$reason = "nieuw";
|
|
}
|
|
else {
|
|
// Something changed on the planning?
|
|
if (is_array($changes)) {
|
|
foreach ($changes as $change) {
|
|
// Already reason found?
|
|
if (!strlen($reason)) {
|
|
// Key exists?
|
|
if ((array_key_exists($change, $project['general'])) && (array_key_exists($change, $original['general']))) {
|
|
// Key changes?
|
|
if ($project['general'][$change] != $original['general'][$change]) {
|
|
// Debug info
|
|
array_push($debug_info, "Changes: " . $change . ", old: " . $original['general'][$change] . ", new: " . $project['general'][$change]);
|
|
|
|
switch($change) {
|
|
// Something changed on the project type?
|
|
case "type":
|
|
switch($project['general']['type']) {
|
|
case "normaal":
|
|
$reason = "verwijderen";
|
|
break;
|
|
case "rc":
|
|
$reason = "nieuw";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
// Something changed on the project status?
|
|
case "status":
|
|
switch($project['general']['status']) {
|
|
case "afgesloten":
|
|
$reason = "verwijderen";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
// Something changed on the project release status?
|
|
case "sstatus":
|
|
switch($project['general']['sstatus']) {
|
|
case "niet vrijgegeven":
|
|
$reason = "teruggegeven";
|
|
break;
|
|
case "vrijgegeven":
|
|
$reason = "vrijgave";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
// Something changed on the design?
|
|
case "ostatus":
|
|
switch($project['general']['ostatus']) {
|
|
case "concept":
|
|
case "gereed":
|
|
$reason = "ontwerp gewijzigd";
|
|
break;
|
|
case "geverifieerd":
|
|
$reason = "ontwerp geverifieerd";
|
|
break;
|
|
case "gevalideerd":
|
|
$reason = "ontwerp gevalideerd";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
// Something changed on the planning?
|
|
case "pstatus":
|
|
switch($project['general']['pstatus']) {
|
|
case "concept":
|
|
// Be aware!
|
|
// When a safety critical change has been made to the design, the ostatus can be unchanged (stays on "gereed")
|
|
// But the pstatus is changed to "concept" (because of period changes).
|
|
// So first check if switching equipment has been changed! before accepting this update reason
|
|
if (($project['general']['ostatus'] == "concept") || ($project['general']['ostatus'] == "gereed")) {
|
|
if ((is_array($project['equip'])) && (is_array($original['equip']))) {
|
|
// Equipment added?
|
|
foreach($project['equip'] as $item) {
|
|
$found = FALSE;
|
|
foreach($original['equip'] as $search_item) {
|
|
$found = ($search_item['id'] == $item['id']) ? TRUE : $found;
|
|
}
|
|
|
|
if (!$found) {
|
|
// Switching equipment?
|
|
if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) {
|
|
$reason = "ontwerp gewijzigd";
|
|
}
|
|
}
|
|
}
|
|
|
|
// Equipment removed?
|
|
foreach($original['equip'] as $item) {
|
|
$found = FALSE;
|
|
foreach($project['equip'] as $search_item) {
|
|
$found = ($search_item['id'] == $item['id']) ? TRUE : $found;
|
|
}
|
|
|
|
if (!$found) {
|
|
// Switching equipment?
|
|
if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) {
|
|
$reason = "ontwerp gewijzigd";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// This was not the reason? => then the planning was changed
|
|
if (!strlen($reason)) {
|
|
$reason = "planning gewijzigd";
|
|
}
|
|
break;
|
|
case "gereed":
|
|
$reason = "planning gewijzigd";
|
|
break;
|
|
case "geverifieerd":
|
|
$reason = "planning geverifieerd";
|
|
break;
|
|
case "gevalideerd":
|
|
$reason = "planning gevalideerd";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
// Project name changed?
|
|
case "naam":
|
|
$reason = "ontwerp gewijzigd";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check only when no parent => equipment/users not stored in wi on secure server
|
|
if (!$parent) {
|
|
// Found reason? => Check equipment on project
|
|
if (!strlen($reason)) {
|
|
// We do not have to check when there is no old equip info or no new equip info
|
|
// Because this is handled by adding/removing periods or in the parent (which has no equipment for the secure server)
|
|
if ((is_array($project['equip'])) && (is_array($original['equip']))) {
|
|
// Equipment added?
|
|
foreach($project['equip'] as $item) {
|
|
$found = FALSE;
|
|
foreach($original['equip'] as $search_item) {
|
|
$found = ($search_item['id'] == $item['id']) ? TRUE : $found;
|
|
}
|
|
|
|
if (!$found) {
|
|
// Debug info
|
|
array_push($debug_info, "Changes: equipment added (" . $item['idcode'] . ")");
|
|
|
|
// Switching equipment?
|
|
if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) {
|
|
$reason = "ontwerp gewijzigd";
|
|
}
|
|
else {
|
|
$reason = "planning gewijzigd";
|
|
}
|
|
}
|
|
}
|
|
|
|
// Equipment removed?
|
|
foreach($original['equip'] as $item) {
|
|
$found = FALSE;
|
|
foreach($project['equip'] as $search_item) {
|
|
$found = ($search_item['id'] == $item['id']) ? TRUE : $found;
|
|
}
|
|
|
|
if (!$found) {
|
|
// Debug info
|
|
array_push($debug_info, "Changes: equipment removed (" . $item['idcode'] . ")");
|
|
|
|
// Switching equipment?
|
|
if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) {
|
|
$reason = "ontwerp gewijzigd";
|
|
}
|
|
else {
|
|
$reason = "planning gewijzigd";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Found reason? => Check users on project
|
|
if (!strlen($reason)) {
|
|
if (((is_array($project['users'])) && (!is_array($original['users']))) ||
|
|
((!is_array($project['users'])) && (is_array($original['users'])))) {
|
|
// Debug info
|
|
array_push($debug_info, "Changes: users changed");
|
|
|
|
$reason = "planning gewijzigd";
|
|
}
|
|
else if ((is_array($project['users'])) && (is_array($original['users']))) {
|
|
// Users added?
|
|
foreach($project['users'] as $item) {
|
|
$found = FALSE;
|
|
foreach($original['users'] as $search_item) {
|
|
$found = ($search_item['id'] == $item['id']) ? TRUE : $found;
|
|
}
|
|
|
|
if (!$found) {
|
|
// Debug info
|
|
array_push($debug_info, "Changes: user added (" . $item['gebruikersnaam'] . ")");
|
|
|
|
$reason = "planning gewijzigd";
|
|
}
|
|
}
|
|
|
|
// Users removed?
|
|
foreach($original['users'] as $item) {
|
|
$found = FALSE;
|
|
foreach($project['users'] as $search_item) {
|
|
$found = ($search_item['id'] == $item['id']) ? TRUE : $found;
|
|
}
|
|
|
|
if (!$found) {
|
|
// Debug info
|
|
array_push($debug_info, "Changes: user removed (" . $item['gebruikersnaam'] . ")");
|
|
|
|
$reason = "planning gewijzigd";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find legitimate reason?
|
|
if (strlen($reason)) {
|
|
// Setup connection with secure server
|
|
$socket = sc_connect();
|
|
|
|
// Valid connection?
|
|
if ($socket) {
|
|
// Send WI start
|
|
if (!$parent) {
|
|
$result = sc_wi_start($socket, $project, $reason, $required_state, $sign_data, $stream_data, $device_size);
|
|
}
|
|
else {
|
|
// Skip users and equipment for parent
|
|
$result = sc_wi_start($socket, array("general" => $project['general'], "equip" => array(), "users" => array()), $reason, $required_state, $sign_data, $stream_data, $device_size);
|
|
}
|
|
|
|
// Send WI timeout
|
|
$result = sc_wi_timeout($socket, (($reason == "vrijgave") ? TIMEOUT_RELEASE : TIMEOUT_DEFAULT), $sign_data, $stream_data);
|
|
|
|
// Send WI user (only for periods)
|
|
if ($result) {
|
|
if (!$parent) {
|
|
if (is_array($project['users'])) {
|
|
foreach($project['users'] as $user) {
|
|
sc_wi_user($socket, $user, $sign_data, $stream_data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Send WI device (Only for periods, the non-switching equipment from the parent has been stored (needed for the RS3000))
|
|
if ($result) {
|
|
if (!$parent) {
|
|
if (is_array($project['equip'])) {
|
|
foreach($project['equip'] as $equip) {
|
|
sc_wi_device($socket, $equip, $sign_data, $stream_data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Send WI request (only for request for switch action)
|
|
if ($result) {
|
|
if (($reason == "verzoek tot inschakelen") || ($reason == "verzoek tot uitschakelen")) {
|
|
if (is_array($project['users'])) {
|
|
foreach($project['users'] as $user) {
|
|
// Current user?
|
|
if ($user['id'] == $_SESSION[$_PAGE_INFO['id']]['login']['user']['id']) {
|
|
// Check if RS3000 has been used? => rs3000 used as verification method => overrule sms/pin
|
|
$user['verificatie'] = ((is_dev("RS3000")) || (is_dev("APP") && (stristr($_SERVER['HTTP_USER_AGENT'],"_RS")))) ? "rs3000" : $user['verificatie'];
|
|
|
|
sc_wi_req($socket, $user, $sign_data, $stream_data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Send WI verify (only for for switch action and user verification)
|
|
if ($result) {
|
|
if (($reason == "inschakelen") || ($reason == "uitschakelen") || ($reason == "controle verificatie")) {
|
|
if (is_array($project['users'])) {
|
|
foreach($project['users'] as $user) {
|
|
// Current user?
|
|
if ($user['id'] == $_SESSION[$_PAGE_INFO['id']]['login']['user']['id']) {
|
|
// Check if RS3000 has been used? => rs3000 used as verification method => overrule sms/pin
|
|
$user['verificatie'] = ((is_dev("RS3000")) || (is_dev("APP") && (stristr($_SERVER['HTTP_USER_AGENT'],"_RS")))) ? "rs3000" : $user['verificatie'];
|
|
|
|
sc_wi_ver($socket, $user, $code, $sign_data, $stream_data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Send WI end
|
|
if ($result) {
|
|
$result = sc_wi_end($socket, $reason, $data, $sign_data, $device_size, $stream_data, $seqnr);
|
|
}
|
|
|
|
// Store total time
|
|
$stream_data .= " Time: " . (microtime(TRUE) - $start);
|
|
|
|
// Split up debug stream
|
|
$stream_array = explode(';', $stream_data);
|
|
|
|
// Add extra debug info to debug stream
|
|
if (is_array($debug_info)) {
|
|
foreach($debug_info as $item) {
|
|
array_push($stream_array, $item);
|
|
}
|
|
}
|
|
|
|
if (is_array($stream_array)) {
|
|
foreach($stream_array as $line) {
|
|
if (intval($result,16) != TCPSTAT_OK) {
|
|
DBG("wi error (" . $seqnr . "): " . trim($line));
|
|
}
|
|
else if (LOG_SC) {
|
|
DBG("wi debug (" . $seqnr . "): " . trim($line));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Close secure server socket
|
|
sc_disconnect($socket);
|
|
}
|
|
else {
|
|
DBG("wi error: invalid socket");
|
|
}
|
|
}
|
|
else {
|
|
DBG("wi debug: no legitimate reason");
|
|
}
|
|
|
|
// Return value
|
|
return $result;
|
|
}
|
|
?>
|