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

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