3 require_once("config.php");
5 header("Access-Control-Allow-Orgin: *");
6 header("Access-Control-Allow-Methods: *");
7 header("Content-Type: application/json");
9 $requestUri = explode('/', trim($_SERVER['REQUEST_URI'],'/'));
10 $requestParams = $_REQUEST;
12 $method = $_SERVER['REQUEST_METHOD'];
14 function requestStatus($code) {
18 405 => 'Method Not Allowed',
19 500 => 'Internal Server Error',
21 return ($status[$code])?$status[$code]:$status[500];
24 function response($data, $status = 500) {
25 header("HTTP/1.1 " . $status . " " . requestStatus($status));
26 print json_encode($data);
29 function connect_db() {
31 global $db,$mysql_host,$mysql_port,$mysql_schema,$mysql_user,$mysql_pwd;
33 if (! ($db = new PDO("mysql:host=$mysql_host;port=$mysql_port;dbname=$mysql_schema",$mysql_user,$mysql_pwd,array( PDO::ATTR_PERSISTENT => false)))) {
36 $db -> exec('SET CHARACTER SET utf8');
42 function exec_query($sql) {
46 $q = $db -> prepare( $sql );
47 $q -> bindParam(':s',$auth_token,PDO::PARAM_INT);
50 return $q -> fetchall(PDO::FETCH_ASSOC);
54 $api = $requestParams["method"];
59 case "get-base-config":
61 $data["site-header"] = $site_header;
65 $startdate = exec_query("select min(access_date) sd from access_log");
67 $cats = exec_query("select mnemo,name,description from rep_cat");
69 foreach ($cats as $cat => $value) {
71 $cmnemo = $cats[$cat]["mnemo"];
72 $reps = exec_query("select mnemo,name,description,graph_x,graph_y,graph_series from rep where cat_mnemo='$cmnemo'");
73 $cats[$cat]["reps"] = $reps;
77 $data["cats"] = $cats;
78 $data["templates"] = exec_query("select mnemo,body from web_templates");
79 $data["columns"] = exec_query("select * from column_names");
80 $users = exec_query("select id,username as name,alias from users");
81 $hosts = exec_query("select id,hostname as name,alias from hosts");
82 $data["dictionaries"] = Array( "user_id" => $users, "host_id" => $hosts);
83 $data["online_refresh"] = $online_refresh;
84 $data["online_history"] = $online_history;
85 $data["start_date"] = $startdate[0]["sd"];
91 $mnemo = $requestParams["mnemo"];
95 $rec = exec_query("select has_total,query from rep where mnemo='$mnemo'")[0];
97 $data["has_total"] = $rec["has_total"];
102 foreach ($requestParams as $name => $value) {
104 if (($name == "mnemo") || ($name == "method")) { continue; }
105 if ($name == "date_from") { $filter_str = $filter_str." and access_date>='$value'"; }
106 elseif ($name == "date_to") { $filter_str = $filter_str." and access_date<date_add('$value',interval 1 day)"; }
107 else { $filter_str = $filter_str." and $name = '$value'"; $filter[$name] = $value; }
111 $sql = preg_replace('/\$FILTER;/',$filter_str,$sql);
115 $data["filter"] = $filter;
117 $result = exec_query($sql);
119 $data["dictionary"] = array_keys($result[0]);
120 $data["data"] = $result;
129 if($squid_passwd != "") { $pwd.="cachemgr:$cachemgr_passwd@"; } else { $pwd = ""; }
130 $url = "http://".$pwd.$squid_host.":".$squid_port."/squid-internal-mgr/active_requests";
132 $ch = curl_init($url);
135 CURLOPT_RETURNTRANSFER => true, // return web page
136 CURLOPT_HEADER => false, // don't return headers
137 CURLOPT_FOLLOWLOCATION => false, // follow redirects
138 CURLOPT_MAXREDIRS => 0, // stop after 10 redirects
139 CURLOPT_ENCODING => "", // handle compressed
140 CURLOPT_USERAGENT => "web", // name of client
141 CURLOPT_AUTOREFERER => true, // set referrer on redirect
142 CURLOPT_CONNECTTIMEOUT => 3, // time-out on connect
143 CURLOPT_TIMEOUT => 5, // time-out on response
146 curl_setopt_array($ch, $options);
148 $reply = curl_exec($ch);
149 $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
151 // 400 means not found, 200 means found.
154 if($retcode == 200) {
156 $data["active"] = Array();
158 if(preg_match("/HTTP/1.0 200 OK/",$ptmp)){
160 $data=Array("error" => "No connection to Squid");
162 preg_match_all("/username(.+)/",$reply,$user);
163 preg_match_all("/(peer|remote):(.+)/",$reply,$remote);
164 preg_match_all("/uri(.+)/",$reply,$uri);
165 preg_match_all("/out\.size(.+)/",$reply,$size);
166 preg_match_all("/\((.+)seconds/",$reply,$sec);
168 for ($i=0; $i< count($user[1]); $i++) {
169 $ip=trim($remote[2][$i]);
170 $reversedParts = explode(':', strrev($ip), 2);
171 $ip = strrev($reversedParts[1]);
172 $port = strrev($reversedParts[0]);
173 if (preg_match('/\[(.*)\]/',$ip,$matches)) {
176 $host = gethostbyaddr($ip);
177 if (!$host) { $host = $ip; }
178 $username=trim($user[1][$i]);
179 $site=trim($uri[1][$i]);
180 $datasize=trim($size[1][$i]);
181 $seconds = trim($sec[1][$i]);
182 $record = Array("_user" => $username, "_ip" => $ip, "_port"=> $port, "host" => $host, "uri" => $site, "bytes" => $datasize, "seconds" => $seconds);
185 $data["data"] = $sess;
186 $data["dictionary"] = Array("_user","_ip","_port","host","uri","bytes","seconds");
194 $data["error"] = "Method not found";
195 $data["method"] = $api;
199 response($data,$code);