-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.php
167 lines (149 loc) · 8 KB
/
logs.php
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
<?php
// http://localhost/prindot/logs.php?action=create
// _REQUEST:
// action=create
// von GUI : ...
// von Maschine : ...
// {"LID": 1,"JID": 1,"MID": "1234-56-7890-abc","HID": 0,"timestamp": "2013-03-06 16:42:49","type": 0,"subtype": 0,"description": ""}
// LID autoincrement
// timestamp autoset
// all other mandatory (even if left blank/0)
require_once('config.inc.php');
require_once('functions.inc.php');
require_once('connect2db.php');
function logs($REQUEST) {
global $prindot;
global $db;
$debuglog = false;
$debuglogfirst = false;
$debugloglast = false;
$errorlog = false;
if ($debuglogfirst) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") _REQUEST='" . my_print_r($REQUEST) . "'");
$function = true; // prevent calling included functions below ...:
require_once($prindot['urls']['jobs']);
require_once($prindot['urls']['machines']);
$action = @$REQUEST['action'];
if (isset($REQUEST['action']))
unset($REQUEST['action']);
$answer = array();
$answer['result'] = 'success';
switch ($action)
{
case 'create': // create log entry by GUI or backend
if (!isset($REQUEST['JID']) || !isset($REQUEST['MID']) || !isset($REQUEST['HID']) || !isset($REQUEST['type']) || !isset($REQUEST['subtype']) || !isset($REQUEST['description'])) {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-1 : missing parameter';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
try {
$remoteaddr = $_SERVER["REMOTE_ADDR"];
$remoteport = $_SERVER["REMOTE_PORT"];
if ($remoteport != "80") {
$remoteaddr .= ":" . $remoteport;
}
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") remoteaddr='" . $remoteaddr . "'");
$REQUEST['remoteaddr'] = $remoteaddr;
if (isset($REQUEST['timestamp'])) unset($REQUEST['timestamp']);
$sql = "INSERT INTO `" . $prindot['database']['tablenames']['logs'] . "` SET ";
$sql .= sqlcreatesetstringfromarray($REQUEST, $prindot['database']['tablekeys']['logs']);
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") sql='" . my_print_r($sql) . "'");
$q = $db->prepare($sql);
$q->execute();
$c = $q->rowCount();
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") c='" . my_print_r($c) . "'");
if ($c == 0) {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-2 : no log entry written - serious ...';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
} catch (PDOException $ex) {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-3 : fetched exception : ' . $ex->getMessage();
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
break;
case 'status':
if (isset($REQUEST['JID']) && $REQUEST['JID'] != '') {
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") call jobs with status");
$atos = array('action' => 'setstatus', 'JID' => @$REQUEST['JID'], 'status' => @$REQUEST['status']);
$server_output = calltarget('jobs', $atos);
$ra = json_decode($server_output, true);
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") calltarget RETURN='" . my_print_r($ra) . "'");
if ($ra['result'] != 'success') {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-4 : cannot update status in jobs database (' . @$ra['reason'] . ')';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
}
// JID in machine MID setzen (verbinden)
// nur von calcstrand aus !
if (@$REQUEST['status'] == JOBSTATUS_CALCSTRAND)
{
if (isset($REQUEST['MID']) && $REQUEST['MID'] != '') {
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") call machines set jid");
$atos = array('action' => 'setjid', 'MID' => @$REQUEST['MID'], 'JID' => @$REQUEST['JID']);
$server_output = calltarget('machines', $atos);
$ra = json_decode($server_output, true);
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") calltarget RETURN='" . my_print_r($ra) . "'");
if ($ra['result'] != 'success') {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-5 : cannot set JID in machines database (' . @$ra['reason'] . ')';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
}
}
break;
case 'setmachinestatus':
if (isset($REQUEST['MID']) && $REQUEST['MID'] != '') {
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") call machines with status");
$server_output = calltarget('machines', array('action' => 'setstatus', 'MID' => @$REQUEST['MID'], 'status' => @$REQUEST['status']));
$ra = json_decode($server_output, true);
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") calltarget RETURN='" . my_print_r($ra) . "'");
if ($ra['result'] != 'success') {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-9 : cannot update status in machines database (' . @$ra['reason'] . ')';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
}
break;
case 'progress':
if (isset($REQUEST['JID']) && $REQUEST['JID'] != '') {
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") call jobs with progress");
$atos = array('action' => 'setprogress', 'JID' => @$REQUEST['JID'], 'progress' => @$REQUEST['progress']);
$server_output = calltarget('jobs', $atos);
$ra = json_decode($server_output, true);
if ($debuglog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") calltarget RETURN='" . my_print_r($ra) . "'");
if ($ra['result'] != 'success') {
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-6 : cannot update progress in jobs database (' . @$ra['reason'] . ')';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
}
}
break;
default:
$answer['result'] = 'error';
$answer['reason'] = 'Error logs-8 : unknown or missing action';
if ($errorlog) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
goto ende;
break;
}
ende:
if ($debugloglast) error_log($_SERVER["SCRIPT_NAME"] . " : " . __FILE__ . " : " . __FUNCTION__ . "(" . __LINE__ . ") _REQUEST='" . my_print_r($REQUEST) . "' answer='" . my_print_r($answer) . "'");
return json_encode($answer);
}
// hier json mit Antwort zurueck
// result: "success" | "error" | "warning"
// bei "success" wird optional value zurueckgegeben (array)
// bei "error" oder "warning" zusaetzlich noch reason: "blablabl"
//if (!isset($function) || ($function == false)) {
if (!isset($function)) {
echo(logs($_REQUEST));
}
?>