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

Last change on this file since 14226 was 14226, checked in by tbretz, 12 years ago
For the moment adding logging also to 'msg'
File size: 5.8 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 return;
106}
107
108if (isset($_GET['logout']))
109{
110 if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
111 return;
112
113 return header('HTTP/1.0 401 Successfull logout!');
114}
115
116// --------------------------------------------------------------------
117
118if (!isset($_GET['start']) && !isset($_GET['stop']))
119 return header('HTTP/1.0 400 Command not supported');
120
121// --------------------------------------------------------------------
122
123if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
124{
125 header('WWW-Authenticate: Basic realm="SmartFACT++"');
126 header('HTTP/1.0 401 Unauthorized');
127 return;
128}
129
130$rc = login();
131if ($rc!="")
132 return header('HTTP/1.0 401 '.$rc);
133
134// --------------------------------------------------------------------
135
136$out = array();
137
138if (isset($_GET['stop']))
139 $str = exec($path."/dimctrl --user '".$_SERVER['PHP_AUTH_USER']."' --stop", $out, $rc);
140
141if (isset($_GET['start']))
142{
143 // Filename
144 $args = '"scripts/'.$_GET['start'].'"';
145
146 unset($_GET['start']);
147
148 /*
149 $args = "";
150 foreach ($_GET as $key => $value)
151 $args .= " --arg:".$key."=".$value;
152 $str = exec($path."/dimctrl --exec ".$args, $out, $rc);
153 */
154
155 // Label
156 if (isset($_GET['label']))
157 {
158 if ($_GET['label']>=0)
159 $args .= ":".$_GET['label'];
160 unset($_GET['label']);
161 }
162
163 $msg = "";
164 if (isset($_GET['msg']))
165 {
166 if ($_GET['msg']>=0)
167 $msg = $_GET['msg'];
168 unset($_GET['msg']);
169 }
170
171 // Arguments
172 foreach ($_GET as $key => $value)
173 $args .= ' --arg:"'.$key.'='.escape($value).'"';
174
175 if (!empty($args) && empty($msg))
176 {
177 // $args = "filename":label --arg:"key1=value" --arg:"key2=value"
178 $cmd = $path.'/dimctrl --user "'.$_SERVER['PHP_AUTH_USER'].'" --start '.$args;
179
180 // Execute
181 $str = exec($cmd, $out, $rc);
182
183 // Logging (mainly for debugging)
184 $d = date("Y/m");
185 $path = "log/".$d;
186 if (!file_exists($path))
187 mkdir($path, 0777, true);
188 $file = fopen($path."/exec.log", "a");
189 fwrite($file, $cmd."\n".$str."\n\n");
190 fclose($file);
191 }
192
193 if (!empty($msg))
194 {
195 $msg = escape($msg);
196
197 // $args = "filename":label --arg:"key1=value" --arg:"key2=value"
198 $cmd = $path.'/dimctrl --user "'.$_SERVER['PHP_AUTH_USER'].'" --msg "'.$msg.'"';
199
200 // Execute
201 $str = exec($cmd, $out, $rc);
202
203 // Logging (mainly for debugging)
204 $d = date("Y/m");
205 $path = "log/".$d;
206 if (!file_exists($path))
207 mkdir($path, 0777, true);
208 $file = fopen($path."/exec.log", "a");
209 fwrite($file, $cmd."\n".$str."\n\n");
210 fclose($file);
211 }
212
213 // -------------------------------------------
214}
215
216if ($rc!=1 && $rc!=2)
217 return header('HTTP/1.0 500 Execution failed [rc='.$rc."]");
218
219print($rc);
220
221if (isset($_GET['debug']))
222{
223 print("\n");
224 print_r($out);
225}
226
227?>
Note: See TracBrowser for help on using the repository browser.