// Copyright (C) 2010 Winch Gate Property Limited // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . //include('fake_request_interface.php'); include('request_interface.php'); /* displayViewTable * displays view $tid for user $uid in group $gid * $selection is an array of preselected lines in the form shard.server.service.variable * each address member might be incomplete (like '*' or 'GPM*') */ function displayViewTable($uid, $gid, $tid, $selection) { global $keep_shard, $keep_server, $keep_service, $display_view; // 1. create views tree buildVariableEnv($uid, $gid, $tid, $vardisp, $bounds, $privilege, $tree, $condensed, $autoDisplay); if ($autoDisplay || $display_view) { // 2. select branches if (count($selection) > 0) { foreach ($selection as $sel) { if ($sel != "") { $selarray = expandQuery($sel); foreach ($selarray as $sel) { $address = explode(".", $sel); //echo "select address ".$sel."
\n"; addToSelectNode($tree, $address, 0); } } } } // 3. remove unselected branches clearupNode($tree); // 4. build and factorize query $query = factorizeQuery(buildQuery($tree)); if ($numsteps == 5) { echo "Unsupported entity request now
\n"; return; } $bef = microtime(); $qstate = nel_query($query, $result); $aft = microtime(); list($usec, $sec) = explode(" ", $bef); $bef = ((float)$sec + (float)$usec); list($usec, $sec) = explode(" ", $aft); $aft = ((float)$sec + (float)$usec); $tm = (int)(($aft-$bef)*1000.0); if (!$qstate) echo "$result\n"; } displayResult($result, $vardisp, $bounds, $privilege, $condensed, $autoDisplay); return "Executed query '$query'
$tm milliseconds computation time
\n"; } function buildVariableEnv($uid, $gid, $tid, &$vardisp, &$bounds, &$privilege, &$tree, &$condensed, &$autoDisplay) { $result = sqlquery("SELECT view_row.name AS user_name, variable.name AS var_name, path, warning_bound, error_bound, alarm_order, view_row.filter as varfilter, view_table.filter as viewfilter, display, auto_display, view_row.vid AS vid ". "FROM variable, view_row, view_table ". "WHERE variable.command='variable' AND (view_table.uid='$uid' OR view_table.uid='$gid') AND view_table.tid='$tid' AND view_row.tid='$tid' AND variable.vid=view_row.vid ORDER BY view_row.ordering"); while ($result && ($arr=sqlfetch($result))) { $vid = $arr["vid"]; if (!hasAccessToVariable($vid)) continue; $path = $arr["path"]; if ($arr["varfilter"] != "" && ($path = filterPath($path, $arr["varfilter"])) == "") continue; if ($arr["viewfilter"] != "" && ($path = filterPath($path, $arr["viewfilter"])) == "") continue; $condensed = ($arr["display"] == "condensed"); $autoDisplay = ($arr["auto_display"] == "auto"); $varName = getVarName($path); $vardisp[$varName] = $arr["user_name"]; $bounds[$varName] = array($arr["warning_bound"], $arr["error_bound"], $arr["alarm_order"]); $privilege[$varName] = getVariableRight($vid); $address = explode(".", $path); //echo "add to tree address $path
\n"; if (!isset($numsteps)) $numsteps = count($address); if ($numsteps != count($address)) { echo "Invalid table $tid, contains different variable path length (typically, mixed shard/server/service variables with entity variables)\n"; return; } addToNode($tree, $address, 0); } } function humanReadableToRaw($str) { $l=strlen($str); if ($l==0) return 0; $regexp = '([\+-]?[0-9]+|[\+-]?[0-9]+\.[0-9]*|[\+-]?[0-9]+\.[0-9]*e[0-9]+) *(b|kb|mb|gb|h|mn|s|d|ms)?'; if (!eregi($regexp, strtolower($str), $regs)) return 0; $num = (float)$regs[1]; $unit = $regs[2]; switch($unit) { case "kb": return $num*1024; case "mb": return $num*1024*1024; case "gb": return $num*1024*1024*1024; case "mn": return $num*60; case "h": return $num*3600; case "ms": return $num/1000.0; case "d": return $num*86400; default: return $num; } return $num; } function vcmp($a, $b) { return ($a["_path_"] == $b["_path_"] ? 0 : $a["_path_"] < $b["_path_"] ? -1 : 1); } function displayResult($result, $vardisp, $bounds, $privilege, $condensed=false, $autoDisplay=true) { global $admlogin, $filter_shard, $filter_server, $filter_service, $filter_entity, $current_tid, $listPath, $current_tname, $usersData; global $uid, $REMOTE_ADDR, $shardLockState, $enablelock, $IsNevrax; addToLog($result); $colorKeyWords = array("Offline" => "#FF4422", "Failed" => "#FF4422"); //echo "result=$result
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
 Filters shardserverserviceentity    

\n"; unset($maxvarwidth); if (strlen($result)>0) { $arr = explode(" ", $result); $result = &$arr; $numRes = count($result); $numRows = current($result); if ($numRows < 1) { echo "NeL query returned no answer
\n"; return; } $numLines = ($numRes-$numRows-2)/$numRows; //echo "numRes=$numRes, numLines=$numLines, numRows=$numRows
\n"; next($result); for ($i=0; $i<$numRows; ++$i) { $vars[] = current($result); next($result); } unset($lineMap); unset($resultArray); for ($i=0; $i<$numLines; ++$i) { $shard = '*'; $server = '*'; $service = '*'; unset($entity); $path = ""; foreach($vars as $var) { $maxvarwidth[$var] = strlen($var); $val = current($result); next($result); if ($val == "???") unset($val); if ($var == "shard" || $var == "server" || $var == "service" || $var == "entity") { if ($path != "") $path .= "."; $path .= $val; if ($var == "shard") { $shard = $val; $listShard[] = $shard; } if ($var == "server") $server = $val; if ($var == "service") $service = $val; if ($var == "entity") $entity = $val; } else { $line = $lineMap[$path]; if (!isset($line)) { $line = count($resultArray); $lineMap[$path] = $line; $resultArray[$line]["shard"] = $shard; $resultArray[$line]["server"] = $server; $resultArray[$line]["service"] = $service; $resultArray[$line]["_path_"] = $path; $serviceDecomp = explode("/", $service); if (count($serviceDecomp) == 1) { $serviceAlias = $service; $serviceSid = $service; } else { $serviceAlias = $serviceDecomp[0]; $serviceSid = $serviceDecomp[1]; } if (isset($entity)) { $resultArray[$line]["entity"] = $entity; $listPath[] = $shard.".".$server.".".$service.".".$entity; } else { $listPath[] = $shard.".".$server.".".$service; } } if (isset($val)) $resultArray[$line][$var] = $val; //echo "set resultArray[$line][$var]=$val
\n"; } } } //echo "
\n";
			//print_r($resultArray);
			//echo "
\n"; if (count($listShard) > 0) { $listShard = array_unique($listShard); sort($listShard); echo "\n"; echo ""; if ($enablelock) echo ""; echo "\n"; foreach($listShard as $shard) { $annotation = &$shardLockState[$shard]; $hasColor = time()-strtotime($annotation['post_date'])< 120; $lockState = $annotation['lock_state']; echo ""; if ($enablelock) { echo ""; echo ""; } echo ""; echo ""; echo ""; echo "\n"; } echo "
ShardLocked byAnnotation byAnnotation
 $shard  ".($lockState != 0 ? ($usersData[$annotation['lock_user']]["login"]."/".$annotation['lock_ip']." 
 ".$annotation['lock_date']) : "not locked")." 
"; if ($lockState == 0) echo ""; else if ($lockState == 1) echo ""; else echo ""; echo " ".$usersData[$annotation['user_annot']]["login"]." / ".$annotation['post_date']."  ".($lockState == 1 ? ("") : $annotation['annot'])." ".($lockState == 1 ? ("") : (""))."

\n"; } echo "\n"; echo ""; foreach ($vars as $var) { $display = (isset($vardisp[$var]) ? $vardisp[$var] : "".$var.""); if ($var == "shard" || $var == "server" || $var == "service" || $var == "entity" || !$condensed) echo ""; else break; } if ($condensed) echo ""; echo "\n"; if (count($resultArray) > 0) usort($resultArray, "vcmp"); $prevShard = ""; $idxShard = 0; $dispUpdateButton = false; if ($condensed) { for ($i=0; $i"; for ($j=0; $j $val".((($admlogin=="root" || $IsNevrax) && $var=="service") ? " [cmd]" : "").""; } $numprev = $j; for (; $j"; for ($k=0; $k<$numprev; ++$k) echo ""; } $first = false; $maxvarwidth[$var] = max($maxvarwidth[$var], strlen($val)); $bound = &$bounds[$var]; $state = 0; if (isset($bound) && $val != "") { $realval = humanReadableToRaw($val); if ($bound[2] == 'gt') { if ($bound[0]!=-1 && $realval >= $bound[0]) $state = 1; if ($bound[1]!=-1 && $realval >= $bound[1]) $state = 2; } else { if ($bound[0]!=-1 && $realval <= $bound[0]) $state = 1; if ($bound[1]!=-1 && $realval <= $bound[1]) $state = 2; } } $btcolor = ($state==0 ? $bgcolor : ($state==1 ? "#FFCC88" : "#FF4422")); if (isset($colorKeyWords[$val])) $btcolor = $colorKeyWords[$val]; if ($privilege[$var] == 'rw' && $shardLockState[$shard]['lock_state'] == 1) { $valdisp = ""; $dispUpdateButton = true; } else { $valdisp = "$val"; } echo "\n"; } } } else { for ($i=0; $i= $bound[0]) $state = 1; if ($bound[1]!=-1 && $realval >= $bound[1]) $state = 2; } else { if ($bound[0]!=-1 && $realval <= $bound[0]) $state = 1; if ($bound[1]!=-1 && $realval <= $bound[1]) $state = 2; } } $btcolor = ($state==0 ? $bgcolor : ($state==1 ? "#FFCC88" : "#FF4422")); if (isset($colorKeyWords[$val])) $btcolor = $colorKeyWords[$val]; if (strstr($val, "((TIMEOUT))") != FALSE) $btcolor = "#FF4422"; if ($privilege[$var] == 'rw' && $shardLockState[$shard]['lock_state'] == 1) { $len = max(min($maxvarwidth[$var], 16), 4); $valdisp = ""; $dispUpdateButton = true; } else { $valdisp = "$val"; } if ($addlink) { $vval = ($var == "service" ? $serviceName : $val); if ($var == "shard") $selects = "&filter_shard=$shard"; else if ($var == "server") $selects = "&filter_shard=$shard&filter_server=$server"; else if ($var == "service") $selects = "&filter_shard=$shard&filter_server=$server&filter_service=$vval"; else if ($var == "entity") $selects = "&filter_shard=$shard&filter_server=$server&filter_service=$service&filter_entity=$vval"; $vval .= ",Default".ucfirst(strtolower($var == "entity" ? "player" : $var)).(isset($current_tname) ? ",$current_tname" : ""); $line .= ""; } else { $line .= ""; } } echo ""; echo $line; echo "\n"; } } if (!$condensed) { echo ""; echo ""; foreach($vars as $var) { $len = max(min($maxvarwidth[$var], 16), 4); if ($var != "shard" && $var != "server" && $var != "service" && $var != "entity" && $privilege[$var] == 'rw') echo ""; else echo ""; } echo "\n"; } echo "
$displayVariableValue
$var$valdisp
 $valdisp".((($admlogin=="root" || $IsNevrax) && $var=="service") ? " [cmd]" : "")."  $valdisp 
   All

\n"; if ($dispUpdateButton) echo "\n"; } else { echo "NeL query returned no answer
\n"; } } function addToNode(&$node, &$address, $step) { if ($step >= count($address)) return; //echo "addToNode:$address[$step]
\n"; addToNode($node[$address[$step]], $address, $step+1); } function filterPath($path, $filter) { $pnodes = explode(".", $path); $fnodes = explode(".", $filter); for ($i=0; $i\n"; return join(".", $rnodes); } function partialFilterPath($path, $filter) { $pnodes = explode(".", $path); $fnodes = explode(".", $filter); for ($i=0; $i\n"; return join(".", $rnodes); } function splitServiceName($service, &$serviceAlias, &$serviceName, &$serviceId) { if (($p1 = strpos($service, "/")) === FALSE || ($p2 = strpos($service, "-")) === FALSE || $p2<$p1) { $serviceName = $service; $serviceAlias = $service; $serviceId = $service; } else { $serviceName = substr($service, $p1+1, $p2-$p1-1); $serviceAlias = substr($service, 0, $p1); $serviceId = substr($service, $p2); } } function filterPathUsingAliases($path, $filter) { $pnodes = explode(".", $path); $fnodes = explode(".", $filter); for ($i=0; $i\n"; return join(".", $rnodes); } function addToSelectNode(&$node, &$address, $step) { //echo "--addToSelectNode(step=$step,count(address)=".count($address).")
\n"; if ($node == NULL) { return true; } else if ($step >= count($address)) { $nodes = array_keys($node); foreach($nodes as $nod) { if ($nod == "___flag") continue; $subnode = &$node[$nod]; addToSelectNode($subnode, $address, $step+1); $subnode["___flag"] = true; } return true; } $sel = $address[$step]; $sel_reg = "^".str_replace("*", ".*", $sel)."$"; $sel_cut = str_replace("*", "&&&&&", $sel); //echo "----addToSelectNode: sel=$sel sel_reg=$sel_reg sel_cut=$sel_cut
\n"; $nodes = array_keys($node); $flag = false; foreach($nodes as $nod) { if ($nod == "___flag") continue; $subnode = &$node[$nod]; $nod_reg = "^".str_replace("#", ".*", str_replace("*", ".*", $nod))."$"; //$nod_reg = "^".str_replace("*", ".*", $nod)."$"; $nod_cut = str_replace("*", "&&&&&", $nod); //echo "addToSelectNode: nod=$nod nod_reg=$nod_reg nod_cut=$nod_cut
\n"; // if subnode matches selection, subnode is more restrictive and then flag subnode and keep subnode if (eregi($sel_reg, $nod_cut)) { //echo "node $nod matches select $sel
\n"; if (addToSelectNode($subnode, $address, $step+1)) { $subnode["___flag"] = true; $flag = true; } } // if selection matches subnode, selection is more restrictive and then copy subnode as selection, flag new subnode and keep it else if (eregi($nod_reg, $sel_cut)) { //echo "selection $sel matches node $nod, adding node $sel
\n"; addNode($node[$sel], $subnode); $subnode = &$node[$sel]; if (addToSelectNode($subnode, $address, $step+1)) { $subnode["___flag"] = true; $flag = true; } } } return $flag; } function clearupNode(&$node) { if (!is_array($node)) return; $nodes = array_keys($node); foreach ($nodes as $key) { $subnode = &$node[$key]; if ($subnode["___flag"]) { unset($subnode["___flag"]); clearupNode($subnode); } else { unset($node[$key]); } } } function dispNode(&$node, $indent) { if (count($node) < 1) return; foreach ($node as $key => $subnode) { if ($key != "___flag") { for ($i=0;$i<$indent; ++$i) echo "-"; echo $key.($subnode["___flag"] ? " flag":"")."
\n"; dispNode($subnode, $indent+1); } } } function addNode(&$dest, &$src) { if (!is_array($src) || count($src) < 1) return; foreach ($src as $key => $subnode) if ($key != "___flag") addNode($dest[$key], $subnode); } function buildQuery(&$node) { if (!is_array($node) || count($node) < 1) return; $hasChild = count($node)>1; if ($hasChild) $sub .= "["; $i = 0; foreach ($node as $key => $subnode) { $sub .= "$key"; if (is_array($subnode) && count($subnode)>=1) { $sub .= "."; $sub .= buildQuery($subnode); } if ($i < count($node)-1) $sub .= ","; ++$i; } if ($hasChild) $sub .= "]"; return $sub; } // function factorizeQuery($query) { global $factorizeForward; $factorizeForward = true; $ret = factorizeQueryInternal($query); /* $factorizeForward = false; $ret = factorizeQueryInternal($ret) */ return $ret; } function factorizeQueryInternal($query) { global $alev; ++$alev; if ($query == "") return $query; //echo "$alev factorizeQuery($query)
\n"; $blocs = splitQuery($query); unset($newQuery); $first = true; foreach ($blocs as $bloc) { $newBloc = factorizeBloc($bloc); if (!$first) $newQuery .= '.'; $newQuery .= $newBloc; $first = false; } //echo "$alev factorizeQuery($query) = $newQuery
\n"; --$alev; return $newQuery; } function splitQuery($query) { $i=0; while ($i\n"; $arr[] = $bloc; } return $arr; } function separateLastQueryBloc($query, &$lead, &$last) { $i=strlen($query)-1; $level = 0; while ($i>=0 && (($char=$query{$i})!='.' || $level!=0)) { if ($char == '[') ++$level; else if ($char == ']') --$level; --$i; } if ($level != 0) echo "Error on query '$query', badly formed (missing end bracket?)
\n"; if ($i == -1) { $lead = ""; $last = $query; } else { $lead = substr($query, 0, $i); $last = substr($query, $i+1); } } function separateFirstQueryBloc($query, &$first, &$follow) { $i=0; $level = 0; while ($i\n"; if ($i == strlen($query)) { $follow = ""; $first = $query; } else { $first = substr($query, 0, $i); $follow = substr($query, $i+1); } } function factorizeBloc($bloc) { global $factorizeForward; return $factorizeForward ? factorizeForwardBloc($bloc) : factorizeBackwardBloc($bloc); } function factorizeForwardBloc($bloc) { if ($bloc{0} != '[') return $bloc; $queries = splitBloc($bloc); if (count($queries) == 0) return; if (count($queries) == 1) return $queries[0]; global $alev; ++$alev; //echo "$alev factorizeForwardBloc($bloc)
\n"; unset($factors); foreach ($queries as $query) { $newQuery = factorizeQueryInternal($query); separateFirstQueryBloc($query, $first, $follow); //echo "$alev found factor $first - $follow
\n"; $factors[$first][] = $follow; } if (count($factors) == 1) { reset($factors); $a = array_keys($factors); $factor = $a[0]; $follows = $factors[$factor]; if (count($follows) == 1) { $factorized = $factor.($follows[0] != "" ? '.'.$follows[0] : ""); } else { $fbloc = joinArray($follows, ","); $factorized = ($fbloc == "") ? $factor : $factor.'.'.factorizeBloc('['.$fbloc.']'); } } else { unset($subblocs); foreach ($factors as $factor => $follows) { //echo "$alev factor $factor, ".count($follows)." leads
\n"; if (count($follows) == 1) { $subblocs[] = $factor.($follows[0] != "" ? '.'.$follows[0] : ""); } else { $fbloc = joinArray($follows, ","); $subblocs[] = ($fbloc == "") ? $factor : $factor.'.'.factorizeBloc('['.$fbloc.']'); } } $factorized = '['.joinArray($subblocs, ",").']'; } //echo "$alev factorizeForwardBloc($bloc) = $factorized
\n"; --$alev; return $factorized; } function factorizeBackwardBloc($bloc) { if ($bloc{0} != '[') return $bloc; $queries = splitBloc($bloc); if (count($queries) == 0) return; if (count($queries) == 1) return $queries[0]; global $alev; ++$alev; //echo "$alev factorizeBloc($bloc)
\n"; unset($factors); foreach ($queries as $query) { $newQuery = factorizeQueryInternal($query); separateLastQueryBloc($query, $lead, $last); //echo "$alev found factor $lead - $last
\n"; $factors[$last][] = $lead; } if (count($factors) == 1) { reset($factors); $a = array_keys($factors); $factor = $a[0]; $leads = $factors[$factor]; if (count($leads) == 1) { $factorized = ($leads[0] != "" ? $leads[0].'.' : "").$factor; } else { $fbloc = joinArray($leads, ","); $factorized = ($fbloc == "") ? $factor : factorizeBloc('['.$fbloc.']').'.'.$factor; } } else { unset($subblocs); foreach ($factors as $factor => $leads) { //echo "$alev factor $factor, ".count($leads)." leads
\n"; if (count($leads) == 1) { $subblocs[] = ($leads[0] != "" ? $leads[0].'.' : "").$factor; } else { $fbloc = joinArray($leads, ","); $subblocs[] = ($fbloc == "") ? $factor : factorizeBloc('['.$fbloc.']').'.'.$factor; } } $factorized = '['.joinArray($subblocs, ",").']'; } //echo "$alev factorizeBloc($bloc) = $factorized
\n"; --$alev; return $factorized; } function splitBloc($bloc) { if ($bloc{0} != '[') { $arr[] = $bloc; } else { if ($bloc{strlen($bloc)-1}!=']') echo "Error on bloc '$bloc', unexpected character after ']'
\n"; $i=1; while ($i\n"; $arr[] = $alt; } } return $arr; } function joinArray(&$array, $sep) { if (count($array) == 0) return ""; $first = true; foreach ($array as $val) { $result .= ($first ? "" : $sep).$val; $first = false; } return $result; } function getVarName($path) { $arr = explode(".", $path); return $arr[count($arr)-1]; } function isAtom($atom) { for ($i=0; $i"; $blocs = splitQuery($query); //echo "splitted query: blocs="; //print_r($blocs); //echo "
-------------------
"; for ($i=0; $i-------------------
"; unset($nodes); $nodes = array(); for ($ii=0; $ii= 1) { $num_nodes = count($current_nodes); for ($j=0; $j<$num_nodes; ++$j) { for ($k=0; $k