source: trunk/FACT++/www/smartfact/index.php@ 17023

Last change on this file since 17023 was 17023, checked in by tbretz, 11 years ago
Added the possibility to send interrupts to the dim server.
File size: 7.9 KB
Line 
1<?PHP
2
3require_once("config.php");
4
5function escape($msg)
6{
7 $msg = str_replace("\\", "\\\\", $msg);
8 $msg = str_replace('\"', '\"', $msg);
9 return $msg;
10}
11
12function login()
13{
14 global $ldaphost;
15 global $baseDN;
16 global $groupDN;
17
18 $username = $_SERVER['PHP_AUTH_USER'];
19 $password = $_SERVER['PHP_AUTH_PW'];
20
21 $con = @ldap_connect($ldaphost);
22 if (!$con)
23 return "ldap_connect failed to ".$ldaphost;
24
25 //------------------ Look for user common name
26 $attributes = array('cn', 'mail');
27 $dn = 'ou=People,'.$baseDN;
28 $filter = '(uid='.$username.')';
29
30 $sr = @ldap_search($con, $dn, $filter, $attributes);
31 if (!$sr)
32 return "ldap_search failed for dn=".$dn.": ".ldap_error($con);
33
34 $srData = @ldap_get_entries($con, $sr);
35 if ($srData["count"]==0)
36 return "No results returned by ldap_get_entries for dn=".$dn.".";
37
38 $email =$srData[0]['mail'][0];
39 $userCommonName=$srData[0]['cn'][0];
40 $userDN =$srData[0]['dn'];
41
42 //------------------ Authenticate user
43 if (!@ldap_bind($con, $userDN, $password))
44 return "ldap_bind failed: ".ldap_error($con);
45
46 //------------------ Check if the user is in FACT ldap group
47 $attributes= array("member");
48 $filter= '(objectClass=*)';
49
50 // Get all members of the group.
51 $sr = @ldap_read($con, $groupDN, $filter, $attributes);
52 if (!$sr)
53 return "ldap_read failed for dn=".$groupDN.": ".ldap_error($con);
54
55 // retrieve the corresponding data
56 $srData = @ldap_get_entries($con, $sr);
57 if ($srData["count"]==0)
58 return "No results returned by ldap_get_entries for dn=".$dn.".";
59
60 @ldap_unbind($con);
61
62 $found = false;
63 foreach ($srData[0]['member'] as $member)
64 if (strpos($member, "cn=".$userCommonName.",")===0)
65 return "";
66
67 return "Sorry, your credentials don't match!";
68}
69// --------------------------------------------------------------------
70
71if (isset($_GET['load']))
72{
73 require_once('log/Browscap.php');
74
75 $d = date("Y/m");
76
77 $path = "log/".$d;
78
79 if (!file_exists("log/cache"))
80 mkdir("log/cache", 0777, true);
81
82 if (!file_exists($path))
83 mkdir($path, 0777, true);
84
85 $addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "";
86 $user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : "";
87 $dns = gethostbyaddr($addr);
88
89 $bcap = new phpbrowscap\Browscap('log/cache');
90 $info = $bcap->getBrowser();
91
92 $file = fopen($path."/smartfact.log", "a");
93 fwrite($file,
94 date("Y-m-d H:i:s\t").$addr.
95 "\t".$info->Platform.
96 "\t".$info->Browser.
97 "\t".$info->Version.
98 "\t".($info->isMobileDevice?"mobile":"").
99 "\t".$user.
100 "\t".$dns."\n");
101 fclose($file);
102
103 // http://ip-address-lookup-v4.com/ip/92.205.118.219
104
105 print($user);
106
107 return;
108}
109
110if (isset($_GET['sourcelist']))
111{
112 $server = mysql_connect($dbhost, $dbuser, $dbpass);
113 if (!$server)
114 die(mysql_error());
115
116 if (!mysql_select_db($dbname, $server))
117 die(mysql_error());
118
119 $result = mysql_query("SELECT fSourceName AS name FROM source", $server);
120 if (!$result)
121 die(mysql_error());
122
123
124// var res = db.query("SELECT fSourceName, fRightAscension, fDeclination ",
125// "FROM source");
126
127 // store the record of the "example" table into $row
128
129 // Print out the contents of the entry
130
131 while ($row=mysql_fetch_array($result, MYSQL_NUM))
132 print("'".$row[0]."'\n");
133
134 mysql_close($server);
135
136 return;
137}
138
139if (isset($_GET['source']) && isset($_GET['time']))
140{
141 // $args = "filename":label --arg:"key1=value" --arg:"key2=value"
142 $cmd = $path.'/makedata '.escapeshellarg($_GET['source']).' '.escapeshellarg($_GET['time']);
143
144 // Execute
145 passthru($cmd, $str);
146
147 // Logging (mainly for debugging)
148 $d = date("Y/m");
149 $path = "log/".$d;
150 if (!file_exists($path))
151 mkdir($path, 0777, true);
152 $file = fopen($path."/exec.log", "a");
153 fwrite($file, $cmd."\n".$str."\n\n");
154 fclose($file);
155
156 print_r($str);
157
158 return;
159}
160
161if (isset($_GET['logout']))
162{
163 if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
164 return;
165
166 return header('HTTP/1.0 401 Successfull logout!');
167}
168
169// --------------------------------------------------------------------
170
171if (!isset($_GET['start']) && !isset($_GET['stop']) && !isset($_GET['interrupt']))
172 return header('HTTP/1.0 400 Command not supported');
173
174// --------------------------------------------------------------------
175
176if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
177{
178 header('WWW-Authenticate: Basic realm="SmartFACT++"');
179 header('HTTP/1.0 401 Unauthorized');
180 return;
181}
182
183$rc = login();
184if ($rc!="")
185 return header('HTTP/1.0 401 '.$rc);
186
187// --------------------------------------------------------------------
188
189$out = array();
190
191if (isset($_GET['stop']))
192 $str = exec($path."/dimctrl --user '".$_SERVER['PHP_AUTH_USER']."' --stop", $out, $rc);
193
194if (isset($_GET['start']))
195{
196 // Filename
197 $script = '"scripts/'.$_GET['start'].'"';
198
199 unset($_GET['start']);
200
201 /*
202 $args = "";
203 foreach ($_GET as $key => $value)
204 $args .= " --arg:".$key."=".$value;
205 $str = exec($path."/dimctrl --exec ".$args, $out, $rc);
206 */
207
208 // Label
209 if (isset($_GET['label']))
210 {
211 if ($_GET['label']>=0)
212 $script .= ":".$_GET['label'];
213 unset($_GET['label']);
214 }
215
216 $msg = "";
217 if (isset($_GET['msg']))
218 {
219 $msg = $_GET['msg'];
220 unset($_GET['msg']);
221 }
222
223 // Arguments
224 if (!empty($script) && empty($msg))
225 {
226 //foreach ($_GET as $key => $value)
227 // $args .= ' --arg:"'.$key.'='.escape($value).'"';
228
229 $args = "";
230 foreach ($_GET as $key => $value)
231 $args .= ' "'.$key.'"="'.$value.'"';
232
233 // $args = "filename":label --arg:"key1=value" --arg:"key2=value"
234 $cmd = $path.'/dimctrl --user "'.$_SERVER['PHP_AUTH_USER'].'" --start '.escapeshellarg($script.$args);
235
236 // Execute
237 $str = exec($cmd, $out, $rc);
238
239 // Logging (mainly for debugging)
240 $d = date("Y/m");
241 $path = "log/".$d;
242 if (!file_exists($path))
243 mkdir($path, 0777, true);
244 $file = fopen($path."/exec.log", "a");
245 fwrite($file, $cmd."\n".$str."\n\n");
246 fclose($file);
247 }
248
249 if (!empty($msg))
250 {
251 $msg = escape($msg);
252
253 // $args = "filename":label --arg:"key1=value" --arg:"key2=value"
254 $cmd = $path.'/dimctrl --user "'.$_SERVER['PHP_AUTH_USER'].'" --msg '.escapeshellarg($msg);
255
256 // Execute
257 $str = exec($cmd, $out, $rc);
258
259 // Logging (mainly for debugging)
260 $d = date("Y/m");
261 $path = "log/".$d;
262 if (!file_exists($path))
263 mkdir($path, 0777, true);
264 $file = fopen($path."/exec.log", "a");
265 fwrite($file, $cmd."\n".$str."\n\n");
266 fclose($file);
267 }
268
269 // -------------------------------------------
270}
271
272if (isset($_GET['interrupt']))
273{
274 unset($_GET['interrupt']);
275
276 $irq = "";
277 if (isset($_GET['interrupt']))
278 {
279 $irq = $_GET['irq'];
280 unset($_GET['interrupt']);
281 }
282
283 $args = "";
284 foreach ($_GET as $key => $value)
285 $args .= ' "'.$key.'"="'.$value.'"';
286
287 $cmd = $path.'/dimctrl --user "'.$_SERVER['PHP_AUTH_USER'].'" --interrupt '.escapeshellarg($irq.$args);
288
289 // Execute
290 $str = exec($cmd, $out, $rc);
291
292 // Logging (mainly for debugging)
293 $d = date("Y/m");
294 $path = "log/".$d;
295 if (!file_exists($path))
296 mkdir($path, 0777, true);
297 $file = fopen($path."/exec.log", "a");
298 fwrite($file, $cmd."\n".$str."\n\n");
299 fclose($file);
300}
301
302if ($rc>1)
303 return header('HTTP/1.0 500 Execution failed [rc='.$rc."]");
304if ($rc==1)
305 return header('HTTP/1.0 500 Sending command failed.');
306
307print($_SERVER['PHP_AUTH_USER']);
308
309if (isset($_GET['debug']))
310{
311 print("\n");
312 print_r($out);
313}
314
315?>
Note: See TracBrowser for help on using the repository browser.