"Fossies" - the Fresh Open Source Software Archive

Member "ganglia-web-3.7.2/views_view.php" (8 Jun 2016, 13374 Bytes) of package /linux/www/ganglia-web-3.7.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "views_view.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.7.1_vs_3.7.2.

    1 <?php
    2 include_once("./eval_conf.php");
    3 include_once("./functions.php");
    4 include_once("./global.php");
    5 
    6 if (! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::VIEW, $conf))
    7   die("You do not have access to view views.");
    8 
    9 $view_name = NULL;
   10 
   11 if (isset($_GET['vn']) && !is_proper_view_name($_GET['vn'])) {
   12 ?>
   13 <div class="ui-widget">
   14   <div class="ui-state-default ui-corner-all" styledefault="padding: 0 .7em;"> 
   15     <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
   16     View names valid characters are 0-9, a-z, A-Z, -, _ and space. View has not been created.</p>
   17   </div>
   18 </div>
   19 <?php
   20   exit(0);
   21 } else {
   22   $view_name = $_GET['vn'];
   23 }
   24 
   25 function viewFileName($view_name) {
   26   global $conf;
   27 
   28   $view_suffix = str_replace(" ", "_", $view_name);
   29   return $conf['views_dir'] . 
   30     "/view_" . 
   31     preg_replace('/[^a-zA-Z0-9_\-]/', '', $view_suffix) . ".json";
   32 }
   33 
   34 $viewList = new ViewList();
   35 
   36 ///////////////////////////////////////////////////////////////////////////////
   37 // Create new view
   38 ///////////////////////////////////////////////////////////////////////////////
   39 if (isset($_GET['create_view'])) {
   40   if(! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::EDIT, $conf)) {
   41     $output = "You do not have access to edit views.";
   42   } else {
   43     if ($viewList->viewExists($view_name)) {
   44       $output = "<strong>Alert:</strong> View with the name " .
   45                 $view_name . 
   46                 " already exists.";
   47     } else {
   48       $empty_view = array ("view_name" => $view_name,
   49                            "items" => array());
   50       $view_filename = viewFileName($view_name);
   51       if (pathinfo($view_filename, PATHINFO_DIRNAME) != $conf['views_dir']) {
   52         die('Invalid path detected');
   53       }
   54 
   55       $json = json_encode($empty_view);
   56       if (file_put_contents($view_filename, 
   57                             json_prettyprint($json)) === FALSE) {
   58         $output = "<strong>Alert:</strong>" .
   59                   " Can't write to file " . htmlspecialchars($view_filename) .
   60                   " Perhaps permissions are wrong.";
   61       } else {
   62         $output = "View has been created successfully.";
   63       }
   64     }
   65   }
   66   $json = '{"output": "<div class=\"ui-widget\"><div class=\"ui-state-default ui-corner-all\" style=\"padding: 0 .7em;\"><p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>' . $output . '</div></div>"';
   67   if ($conf['display_views_using_tree']) {
   68     $json .= ',"tree_node": {"text":"' . $view_name . '","id":"' . viewId($view_name) . '"';
   69     $json .= ',"view_name":"' . $view_name . '"';
   70     $json .= '}';
   71   }
   72   $json .= '}';
   73   echo $json;
   74   exit(0);
   75 } 
   76 
   77 ///////////////////////////////////////////////////////////////////////////////
   78 // Delete view
   79 ///////////////////////////////////////////////////////////////////////////////
   80 if (isset($_GET['delete_view'])) {
   81   if (! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::EDIT, $conf)) {
   82     $output = "You do not have access to edit views.";
   83   } else {
   84     if (!$viewList->viewExists($view_name)) {
   85       $output = "<strong>Alert:</strong> View with the name " .
   86       $view_name . 
   87       " does not exist.";
   88     } else {
   89       $view_filename = viewFileName($view_name);
   90       if (pathinfo($view_filename, PATHINFO_DIRNAME) != $conf['views_dir']) {
   91         die('Invalid path detected');
   92       }
   93 
   94       if (unlink($view_filename) === FALSE) {
   95         $output = "<strong>Alert:</strong>" .
   96                   " Can't remove file $view_filename." .
   97                   " Perhaps permissions are wrong.";
   98       } else {
   99         $output = "View has been successfully removed.";
  100     $viewList->removeView($view_name);
  101       }
  102     }
  103   }
  104 } // delete_view
  105 
  106 ///////////////////////////////////////////////////////////////////////////////
  107 // Add to view
  108 ///////////////////////////////////////////////////////////////////////////////
  109 if (isset($_GET['add_to_view'])) {
  110   if (! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::EDIT, $conf)) {
  111     $output = "You do not have access to edit views.";
  112   } else {
  113     if (!$viewList->viewExists($view_name)) {
  114       $output = "<strong>Alert:</strong> View " .
  115       $view_name . 
  116       " does not exist. This should not happen.";
  117     } else {
  118       $view = $viewList->getView($view_name);
  119 
  120       # Check if we are adding an aggregate graph
  121       if (isset($_GET['aggregate'])) {
  122     foreach ($_GET['mreg'] as $key => $value) 
  123       $metric_regex_array[] = array("regex" => $value);
  124 
  125     foreach ($_GET['hreg'] as $key => $value) 
  126       $host_regex_array[] = array("regex" => $value);
  127 
  128     $item_array = array("aggregate_graph" => "true", 
  129                 "metric_regex" => $metric_regex_array, 
  130                 "host_regex" => $host_regex_array, 
  131                 "graph_type" => stripslashes($_GET['gtype']),
  132                 "vertical_label" => stripslashes($_GET['vl']),
  133                 "title" => $_GET['title'],
  134                 "glegend" => $_GET['glegend']);
  135 
  136     if (isset($_GET['x']) && is_numeric($_GET['x'])) {
  137       $item_array["upper_limit"] = $_GET['x'];
  138     }
  139 
  140     if (isset($_GET['n']) && is_numeric($_GET['n'])) {
  141       $item_array["lower_limit"] = $_GET['n'];
  142     }
  143     if (isset($_GET['c'])) {
  144       $item_array["cluster"] = $_GET['c'];
  145     }
  146 
  147     if (isset($_GET['h'])) { 
  148       $item_array['host'] = $_GET['h']; 
  149       unset($item_array['host_regex']); 
  150     }
  151     if (isset($_GET['m'])) { 
  152       $item_array['metric'] = $_GET['m']; 
  153       unset($item_array['metric_regex']); 
  154     }
  155     if (isset($_GET['g'])) { 
  156       $item_array['graph'] = $_GET['g']; 
  157     }
  158     if ($item_array['host_regex'] == null) 
  159       $item_array['host_regex'] = '.*';
  160 
  161     $view['items'][] = $item_array;
  162     unset($item_array);
  163       } else {
  164     if ($_GET['type'] == "metric") {
  165           $items = array("hostname" => $_GET['host_name'], 
  166                          "metric" => $_GET['metric_name']);
  167       if (isset($_GET['vertical_label']))
  168             $items["vertical_label"] = stripslashes($_GET['vertical_label']);
  169       if (isset($_GET['title']))
  170             $items["title"] = stripslashes($_GET['title']);
  171       if (isset($_GET['c']))
  172             $items["cluster"] = $_GET['c'];
  173           if (isset($_GET['warning']) && is_numeric($_GET['warning']))
  174             $items["warning"] = $_GET['warning'];
  175           if (isset($_GET['critical']) && is_numeric($_GET['critical']))
  176             $items["critical"] = $_GET['critical'];
  177           
  178       $view['items'][] = $items;
  179     } else
  180       $view['items'][] = array("hostname" => $_GET['host_name'], 
  181                                    "graph" => $_GET['metric_name']);
  182       }
  183 
  184       $view_filename = $view['file_name'];
  185       // Remove the file_name attribute, it is not stored in the view defn.
  186       unset($view['file_name']);
  187       $json = json_encode($view);
  188       if (file_put_contents($view_filename, 
  189                             json_prettyprint($json)) === FALSE ) {
  190         $output = "<strong>Alert:</strong>" .
  191       " Can't write to file: \"$view_filename\"." .
  192       " Perhaps permissions are wrong.";
  193       } else {
  194         $output = "View has been updated successfully.";
  195       } 
  196     }  
  197   }
  198 ?>
  199 <div class="ui-widget">
  200   <div class="ui-state-default ui-corner-all" style="padding: 0 .7em;"> 
  201     <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
  202     <?php echo $output ?></p>
  203   </div>
  204 </div>
  205 <?php
  206   exit(0);
  207 } 
  208 
  209 class ViewTreeNode {
  210   private $name = NULL;
  211   private $data = NULL;
  212   private $children = NULL;
  213   private $parent = NULL;
  214   
  215   public function __construct($name) {
  216     $this->name = $name;
  217   }
  218   
  219   public function hasChild($name) {
  220     if ($this->children == NULL)
  221       return false;
  222     return array_key_exists($name, $this->children);
  223   }
  224   
  225   public function getChild($name) {
  226     return $this->children[$name];
  227   }
  228 
  229   public function getChildren() {
  230     return $this->children;
  231   }
  232   
  233   public function addChild($node) {
  234     if ($this->children == NULL)
  235       $this->children = array();
  236     $this->children[$node->getName()] = $node;
  237     $node->setParent($this);
  238     return $node;
  239   }
  240   
  241   public function getNode($path) {
  242     $parent = $this;
  243     foreach (explode("/", $path) as $node_name) {
  244       if ($parent->hasChild($node_name)) {
  245     $parent = $parent->getChild($node_name);
  246       } else {
  247     $parent = $parent->addChild(new ViewTreeNode($node_name));
  248       }
  249     }
  250     return $parent;
  251   }
  252 
  253   public function getData() {
  254     return $this->data;
  255   }
  256   
  257   public function setData($data) {
  258     $this->data = $data;
  259   }
  260   
  261   public function getName() {
  262     return $this->name;
  263   }
  264 
  265   public function getParent() {
  266     return $this->parent;
  267   }
  268 
  269   public function setParent($parent) {
  270     $this->parent = $parent;
  271   }
  272 
  273   public function toJson($initially_open) {
  274     $pathName = $this->getPathName();
  275     $id = viewId($pathName);
  276     $json = '{"text":"' . $this->name . '","id":"' . $id . '"';
  277     if ($this->data != NULL)
  278       $json .= ',"view_name":"' . $pathName . '"';
  279     
  280     if ($initially_open && in_array($pathName, $initially_open))
  281       $json .= ',"state":{"opened": true}';
  282 
  283     if ($this->children != NULL) {
  284       $json .= ',"children":[';
  285       $i = 0;
  286       foreach ($this->children as $child_node) {
  287     if ($i++ > 0) $json .= ',';
  288     $json .= $child_node->toJson($initially_open);
  289       }
  290       $json .= ']';
  291     }
  292     $json .= '}';
  293     return $json;
  294   }
  295 
  296   function getPathName() {
  297     global $VIEW_NAME_SEP;
  298 
  299     if ($this->parent == NULL)
  300       return $this->name;
  301 
  302     $pathName = $this->name;
  303     $parent = $this->parent;
  304     while ($parent->getParent() != NULL) {
  305       $pathName = $parent->getName() . $VIEW_NAME_SEP . $pathName;
  306       $parent = $parent->getParent();
  307     }
  308     return $pathName;
  309   }
  310 }
  311 
  312 function build_view_tree($views) {
  313   $view_tree = new ViewTreeNode('root');
  314   foreach ($views as $view) {
  315     $node = new ViewTreeNode($view['view_name']);
  316     $node->setData($view);
  317     if ($view['parent'] != NULL) {
  318       $parent = $view_tree->getNode($view['parent']);
  319       $parent->addChild($node);
  320     } else {
  321       $view_tree->addChild($node);
  322     }
  323   }
  324   return $view_tree;
  325 }
  326 
  327 $existing_views = '';
  328 if ($conf['display_views_using_tree']) {
  329   $initially_open = NULL;
  330   if (!isset($_SESSION['view_tree_built']) && 
  331       isset($conf['view_tree_nodes_initially_open']))
  332     $initially_open = $conf['view_tree_nodes_initially_open'];
  333 
  334   $view_tree = build_view_tree($viewList->getViews());
  335   $existing_views = '[';
  336   $i = 0;
  337   foreach ($view_tree->getChildren() as $view_node) {
  338     if ($i++ > 0) $existing_views .= ',';
  339     $existing_views .= $view_node->toJson($initially_open);
  340     $i++;
  341   }
  342   $existing_views .= ']';
  343   $_SESSION['view_tree_built'] = TRUE;
  344 } else {
  345   foreach ($viewList->getViews() as $view) {
  346     if ($view['parent'] == NULL) {
  347       $v = $view['view_name'];
  348       $vid = viewId($v);
  349       $checked = ($_GET['vn'] == $v);
  350       $existing_views .= '<input type="radio" id="' . $vid . '" onClick="selectView(\'' . $v . '\'); return false;"' . ($checked ? " checked" : "") . '><label style="text-align:left;" class="nobr" for="' . $vid . '">' . $v . '</label>'; 
  351     }
  352   }
  353 }
  354 if (isset($_GET['views_menu'])) {
  355   if (!$conf['display_views_using_tree']) {
  356 ?>
  357 <div id="views_menu">
  358       <?php echo $existing_views ?>
  359 </div>
  360     <script type="text/javascript">$(function(){$("#views_menu").buttonsetv();});</script>
  361   <?php } else { ?>
  362 <?php
  363   }
  364   exit(0);
  365 }
  366 
  367 $tpl = new Dwoo_Template_File( template("views_view.tpl") );
  368 $data = new Dwoo_Data();
  369 $data->assign("range",$range);
  370 
  371 if (isset($conf['ad-hoc-views']) && 
  372     $conf['ad-hoc-views'] === true && 
  373     isset($_GET['ad-hoc-view'])) {
  374   $is_ad_hoc = true;
  375 }
  376 
  377 // Pop up a warning message if there are no available views
  378 // (Disable temporarily, otherwise we can't create views)
  379 if (count($viewList->getViews()) == -1 && !$is_ad_hoc) {
  380   $error_msg = '
  381     <div class="ui-widget">
  382       <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"> 
  383         <p><span class="ui-icon ui-icon-alert" 
  384                  style="float: left; margin-right: .3em;"></span> 
  385        <strong>Alert:</strong> There are no views defined.</p>
  386       </div>
  387     </div>';
  388 }
  389 
  390 $size = isset($clustergraphsize) ? $clustergraphsize : 'default';
  391 
  392 // set to 'default' to preserve old behavior
  393 if ($size == 'medium') 
  394   $size = 'default';
  395 
  396 $additional_host_img_css_classes = "";
  397 if (isset($conf['zoom_support']) && $conf['zoom_support'] === true)
  398   $additional_host_img_css_classes = "host_${size}_zoomable";
  399 
  400 $data->assign("additional_host_img_css_classes", 
  401               $additional_host_img_css_classes);
  402 
  403 $data->assign("existing_views", $existing_views);
  404 $data->assign("view_name", $view_name);
  405 $data->assign("display_views_using_tree", $conf["display_views_using_tree"]);
  406 
  407 $view_items = NULL;
  408 if ($is_ad_hoc) {
  409   $data->assign("ad_hoc_view", true);
  410   $data->assign("ad_hoc_view_json", rawurlencode($_GET['ad-hoc-view']));
  411   $ad_hoc_view_json = json_decode(heuristic_urldecode($_GET['ad-hoc-view']), 
  412                   true);
  413   $view_items = getViewItems($ad_hoc_view_json, $range, $cs, $ce);
  414 } else {
  415   $view = $viewList->getView($view_name);
  416   if ($view != NULL) {
  417     $view_items = getViewItems($view, $range, $cs, $ce);
  418     if ($view['common_y_axis'] != 0)
  419       $data->assign("common_y_axis", 1);
  420   }
  421 }
  422 
  423 if (isset($view_items)) {
  424   $data->assign("view_items", $view_items);
  425   $data->assign("number_of_view_items", count($view_items));
  426 }
  427 
  428 $data->assign('GRAPH_BASE_ID', $GRAPH_BASE_ID);
  429 $data->assign('SHOW_EVENTS_BASE_ID', $SHOW_EVENTS_BASE_ID);
  430 
  431 $dwoo->output($tpl, $data);
  432 
  433 ?>