1 | <?php
|
---|
2 |
|
---|
3 | if (!isset($_POST['n']) || !isset($_POST['d']))
|
---|
4 | return header('HTTP/1.0 400 Syntax error.');
|
---|
5 |
|
---|
6 | require_once 'config.php';
|
---|
7 |
|
---|
8 | function login()
|
---|
9 | {
|
---|
10 | global $ldaphost;
|
---|
11 | global $baseDN;
|
---|
12 | global $groupDN;
|
---|
13 |
|
---|
14 | $username = $_SERVER['PHP_AUTH_USER'];
|
---|
15 | $password = $_SERVER['PHP_AUTH_PW'];
|
---|
16 |
|
---|
17 | if (!isset($username) || !isset($password))
|
---|
18 | return "Unauthorized.";
|
---|
19 |
|
---|
20 | $con = @ldap_connect($ldaphost);
|
---|
21 | if (!$con)
|
---|
22 | return "ldap_connect failed to ".$ldaphost;
|
---|
23 |
|
---|
24 | //------------------ Look for user common name
|
---|
25 | $attributes = array('cn', 'mail');
|
---|
26 | $dn = 'ou=People,'.$baseDN;
|
---|
27 | $filter = '(uid='.$username.')';
|
---|
28 |
|
---|
29 | $sr = @ldap_search($con, $dn, $filter, $attributes);
|
---|
30 | if (!$sr)
|
---|
31 | return "ldap_search failed for dn=".$dn.": ".ldap_error($con);
|
---|
32 |
|
---|
33 | $srData = @ldap_get_entries($con, $sr);
|
---|
34 | if ($srData["count"]==0)
|
---|
35 | return "No results returned by ldap_get_entries for dn=".$dn.".";
|
---|
36 |
|
---|
37 | $email =$srData[0]['mail'][0];
|
---|
38 | $userCommonName=$srData[0]['cn'][0];
|
---|
39 | $userDN =$srData[0]['dn'];
|
---|
40 |
|
---|
41 | //------------------ Authenticate user
|
---|
42 | if (!@ldap_bind($con, $userDN, $password))
|
---|
43 | return "ldap_bind failed: ".ldap_error($con);
|
---|
44 |
|
---|
45 | //------------------ Check if the user is in FACT ldap group
|
---|
46 | $attributes= array("member");
|
---|
47 | $filter= '(objectClass=*)';
|
---|
48 |
|
---|
49 | // Get all members of the group.
|
---|
50 | $sr = @ldap_read($con, $groupDN, $filter, $attributes);
|
---|
51 | if (!$sr)
|
---|
52 | return "ldap_read failed for dn=".$groupDN.": ".ldap_error($con);
|
---|
53 |
|
---|
54 | // retrieve the corresponding data
|
---|
55 | $srData = @ldap_get_entries($con, $sr);
|
---|
56 | if ($srData["count"]==0)
|
---|
57 | return "No results returned by ldap_get_entries for dn=".$dn.".";
|
---|
58 |
|
---|
59 | @ldap_unbind($con);
|
---|
60 |
|
---|
61 | $found = false;
|
---|
62 | foreach ($srData[0]['member'] as $member)
|
---|
63 | if (strpos($member, "cn=".$userCommonName.",")===0)
|
---|
64 | return "";
|
---|
65 |
|
---|
66 | return "Authorization failed.";
|
---|
67 | }
|
---|
68 |
|
---|
69 | // --------------------------------------------------------------------
|
---|
70 |
|
---|
71 | if (isset($_GET['logout']))
|
---|
72 | {
|
---|
73 | if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
|
---|
74 | return;
|
---|
75 |
|
---|
76 | return header('HTTP/1.0 401 Successfull logout!');
|
---|
77 | }
|
---|
78 |
|
---|
79 | // --------------------------------------------------------------------
|
---|
80 |
|
---|
81 | $rc = login();
|
---|
82 | if ($rc!="")
|
---|
83 | {
|
---|
84 | header('WWW-Authenticate: Basic realm="FACT Schedule"');
|
---|
85 | header('HTTP/1.0 401 '.$rc);
|
---|
86 | }
|
---|
87 |
|
---|
88 | // ====================================================================
|
---|
89 |
|
---|
90 | // This is the day/night from which the data is to be deleted
|
---|
91 | // and to which the data is to be submitted
|
---|
92 | $day = $_POST['n'];
|
---|
93 |
|
---|
94 | // This is the time of the last diabled entry (or the time from which
|
---|
95 | // on the data should be deleted/submitted)
|
---|
96 | // Note that there is no sanity check yet, therefore the data and the
|
---|
97 | // time variable must be consistent
|
---|
98 | $time = isset($_POST['t']) ? $_POST['t'] : "12:00:00";
|
---|
99 |
|
---|
100 | // The data to be submitted
|
---|
101 | $data = json_decode($_POST['d']);
|
---|
102 |
|
---|
103 | // Get user
|
---|
104 | $user = $_SERVER['PHP_AUTH_USER'];
|
---|
105 |
|
---|
106 | // FIXME: Make sure that the date is valid (in the future)?
|
---|
107 |
|
---|
108 | // ----------------------------------------------------------------
|
---|
109 |
|
---|
110 | // Calculate the date for the next day, to have the correct
|
---|
111 | //date after midnight as well
|
---|
112 | $date = new DateTime($day);
|
---|
113 | $date->add(new DateInterval('P1D')); // PnYnMnDTnHnMnS
|
---|
114 | $nextDay = $date->format('Y-m-d');
|
---|
115 |
|
---|
116 | // ----------------------------------------------------------------
|
---|
117 |
|
---|
118 | // Calculate the lower limit from which on data should be deleted.
|
---|
119 | // This is either noon (if the date is in the future) or the provided
|
---|
120 | // time (different from 12:00:00) during the night
|
---|
121 | $cut = $day." ".$time;
|
---|
122 |
|
---|
123 | $d = new DateTime($cut);
|
---|
124 |
|
---|
125 | // If the time lays before noon, it belongs to the next day
|
---|
126 | if ($d->format("His")<120000)
|
---|
127 | {
|
---|
128 | $d->add(new DateInterval('P1D')); // PnYnMnDTnHnMnS
|
---|
129 | $cut = $d->format("Y-m-d H:i:s");
|
---|
130 | }
|
---|
131 |
|
---|
132 | // ================================================================
|
---|
133 |
|
---|
134 | $db = mysql_connect($dbhost,$dbuser,$dbpass);
|
---|
135 | if (!$db)
|
---|
136 | die(mysql_error());
|
---|
137 |
|
---|
138 | if (!mysql_select_db($dbname, $db))
|
---|
139 | die(mysql_error());
|
---|
140 |
|
---|
141 | $query = "SELECT * FROM Measurement";
|
---|
142 |
|
---|
143 | $sql = mysql_query($query);
|
---|
144 | if (!$sql)
|
---|
145 | die(mysql_error());
|
---|
146 |
|
---|
147 | $measurements = array();
|
---|
148 | while($row = mysql_fetch_assoc($sql))
|
---|
149 | $measurements[$row['fMeasurementKey']] = $row;
|
---|
150 |
|
---|
151 | // ----------------------------------------------------------------
|
---|
152 |
|
---|
153 | // Now create the queries with the correct dates (date and time)
|
---|
154 | // from the posted data and the times therein
|
---|
155 | $queries = array();
|
---|
156 |
|
---|
157 | array_push($queries, "LOCK TABLES Schedule WRITE");
|
---|
158 | array_push($queries, "DELETE FROM Schedule WHERE fStart>'".$cut."' AND DATE(ADDTIME(fStart, '-12:00')) = '".$day."'");
|
---|
159 |
|
---|
160 | // ----------------------------------------------------------------
|
---|
161 |
|
---|
162 | foreach ($data as $row)
|
---|
163 | {
|
---|
164 | $t = $row[0]; // time
|
---|
165 |
|
---|
166 | // If there is a time set (first task in an observation),
|
---|
167 | // remember the time, if not this is just a measurement
|
---|
168 | // within an observation so duplicate the time
|
---|
169 | if (!isset($t))
|
---|
170 | {
|
---|
171 | $t = $save;
|
---|
172 | $id++;
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | $save = $t;
|
---|
177 | $id = 0;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // Check if the time is before noon. If it is before noon,
|
---|
181 | // it belongs to the next day
|
---|
182 | $d = date_parse($t);
|
---|
183 | $t = $d['hour']<12 ? $nextDay." ".$t : $day." ".$t;
|
---|
184 |
|
---|
185 | if (!isset($last))
|
---|
186 | {
|
---|
187 | $last = $t;
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | // Check all but the last task in a measurement whether
|
---|
192 | // the are not unlimited
|
---|
193 | if ($last==$t)
|
---|
194 | {
|
---|
195 | if ($measurements[$m]['fIsUnlimited']==true)
|
---|
196 | die("Unlimited task '".$measurements[$m]['fMeasurement']."' detected before end of observation\n[".$last."|".($id-1)."]");
|
---|
197 | }
|
---|
198 |
|
---|
199 | if ($last>$t)
|
---|
200 | die("Times not sequential\n[".$last."|".$t."]");
|
---|
201 | $last = $t;
|
---|
202 | }
|
---|
203 |
|
---|
204 | $m = $row[1]; // measurement
|
---|
205 | $s = $row[2]; // source
|
---|
206 | $v = $row[3]; // value
|
---|
207 |
|
---|
208 | // Check if task need source or must not have a source
|
---|
209 | if ($measurements[$m]['fNeedsSource']==true && $s==0)
|
---|
210 | die("Task '".$measurements[$m]['fMeasurement']."' needs source.\n[".$t."|".$id."]");
|
---|
211 | if ($measurements[$m]['fNeedsSource']!=true && $s>0)
|
---|
212 | die("Task '".$measurements[$m]['fMeasurement']."' must not have source.\n[".$t."|".$id."]");
|
---|
213 |
|
---|
214 | // Compile query
|
---|
215 | $query = "INSERT INTO Schedule SET";
|
---|
216 | $query .= " fStart='".$t."'";
|
---|
217 | $query .= ",fMeasurementID=".$id;
|
---|
218 | $query .= ",fMeasurementTypeKey=".$m;
|
---|
219 | $query .= ",fUser='".$user."'";
|
---|
220 | if ($s>0)
|
---|
221 | $query .= ",fSourceKey=".$s;
|
---|
222 |
|
---|
223 | // Check if this is a valid JSON object
|
---|
224 | if (!json_decode('{'.$v.'}'))
|
---|
225 | {
|
---|
226 | switch (json_last_error())
|
---|
227 | {
|
---|
228 | case JSON_ERROR_NONE: break;
|
---|
229 | case JSON_ERROR_DEPTH: $err = 'Maximum stack depth exceeded'; break;
|
---|
230 | case JSON_ERROR_STATE_MISMATCH: $err = 'Invalid or malformed JSON'; break;
|
---|
231 | case JSON_ERROR_CTRL_CHAR: $err = 'Unexpected control character'; break;
|
---|
232 | case JSON_ERROR_SYNTAX: $err = 'Syntax error'; break;
|
---|
233 | case JSON_ERROR_UTF8: $err = 'Malformed UTF-8 characters'; break;
|
---|
234 | default: $err = 'Unknown error'; break;
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (isset($err))
|
---|
238 | die($err." at ".$t." [entry #".($id+1)."]:\n".$v);
|
---|
239 | }
|
---|
240 |
|
---|
241 | // PHP >= 5.5.0
|
---|
242 | // if (!json_decode('{'.$v.'}'))
|
---|
243 | // die("Invalid option at ".$t.": ".$v." [JSON - ".json_last_error_msg()."]");
|
---|
244 |
|
---|
245 |
|
---|
246 | $query .= ",fData='".$v."'";
|
---|
247 |
|
---|
248 | // add query to the list of queries
|
---|
249 | array_push($queries, $query);
|
---|
250 | }
|
---|
251 |
|
---|
252 | array_push($queries, "UNLOCK TABLES");
|
---|
253 |
|
---|
254 | // ================================================================
|
---|
255 | // Database interaction
|
---|
256 | // ================================================================
|
---|
257 |
|
---|
258 | foreach ($queries as $query)
|
---|
259 | if (!mysql_query($query))
|
---|
260 | die(mysql_error());
|
---|
261 |
|
---|
262 | mysql_close($db);
|
---|
263 |
|
---|
264 | ?>
|
---|