Commit eeea7a1a authored by Benoit Mortier's avatar Benoit Mortier
Browse files

Fixes: #490 cleaning goto / systems plugins

parent b2567e92
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class filterGotoEvents extends filterLDAP {
static function query($parent,$base, $scope, $filter, $attributes, $category, $objectStorage= "")
{
global $config;
$o_queue = new supportDaemon();
$events = DaemonEvent::get_event_types( SYSTEM_EVENT);
/* Get tags that will be used in queue searches */
$event_tags = array("none");
foreach($events['SCHEDULED'] as $evt){
$event_tags[] = $evt['s_Queued_Action'];
}
$entries = $o_queue->get_queued_entries($event_tags,0,9999999,"id");
if ($o_queue->is_error()){
msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$o_queue->get_error()), ERROR_DIALOG);
}
/* Assign entries by id.
*/
$data= array();
$ui = get_userinfo();
$ui= get_userinfo();
$acl_base = $config->current['BASE'];
$acl_is_writeable = preg_match('/w/', $ui->get_permissions($acl_base, "gotomasses/gotomasses", "0"));
$acl_is_removeable = preg_match('/d/', $ui->get_permissions($acl_base, "gotomasses/gotomasses", "0"));
$acl_is_readable = preg_match('/r/', $ui->get_permissions($acl_base, "gotomasses/gotomasses", "0"));
foreach($entries as $entry){
// Detect entry status
$entry['objectClass'][] = "FAKE_OC__gotoEvent";
/* If WAITING add priority action
*/
if(in_array($entry['STATUS'],array("waiting")) && $acl_is_writeable){
$entry['objectClass'][] = "FAKE_OC__prioUp";
$entry['objectClass'][] = "FAKE_OC__prioDown";
$entry['objectClass'][] = "FAKE_OC__prioPause";
}
/* If PAUSED add resume action
*/
if(in_array($entry['STATUS'],array("paused")) && $acl_is_writeable){
$entry['objectClass'][] = "FAKE_OC__prioResume";
}
/* If PAUSED or WAITING add execution action
*/
if(in_array($entry['STATUS'],array("paused","waiting")) && $acl_is_writeable){
$entry['objectClass'][] = "FAKE_OC__processNow";
}
/* Add logview button, currently ever.
*/
if($acl_is_readable){
$entry['objectClass'][] = "FAKE_OC__viewLogs";
}
/* If PAUSED or WAITING add edit action
*/
if(in_array($entry['STATUS'],array("waiting")) && $acl_is_writeable){
$entry['objectClass'][] = "FAKE_OC__edit";
}
/* If PROCESSING add abort action
*/
if(in_array($entry['STATUS'],array("processing")) && preg_match("/install/",$entry['HEADERTAG']) && $acl_is_writeable){
$entry['objectClass'][] = "FAKE_OC__abort";
}
/* If WAITING or ERROR add remove action
*/
if( $acl_is_removeable && in_array($entry['STATUS'],array("waiting","error","processed"))){
$entry['objectClass'][] = "FAKE_OC__remove";
}
if($acl_is_writeable && in_array($entry['STATUS'],array("processing")) && !preg_match("/install/",$entry['HEADERTAG'])){
$entry['objectClass'][] = "FAKE_OC__remove";
}
$item = array();
$item['count'] = 0;
$item['EVENT'] = $entry;
$item['dn']= $entry['MACADDRESS'].",".$entry['TIMESTAMP'];
foreach($entry as $name => $value){
$item[] = $name;
if(!is_array($value)){
$item[$name]=array('count' => 1, $value);
}else{
$item[$name] = $value;
$item[$name]['count'] = count($value);
}
$item['count'] ++;
}
# /* Skip entries which will be removed within the next seconds */
# if(isset($entry['MACADDRESS']) && in_array($entry['MACADDRESS'],$recently_removed)){
# continue;
# }
$data[] = $item;
}
return($data);
}
}
?>
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class gotoLogView extends plugin
{
var $mac;
var $logs;
var $event;
var $parent;
var $config;
var $o_queue;
var $selected_date;
var $selected_file = 0;
var $attributes = array("macAddress");
var $macAddress = "";
var $sort_by = "time";
var $sort_dir = 1; // 1 => up, 0 => down
var $ignore_account = TRUE;
var $standalone = FALSE;
function __construct(&$config,$dn,$parent)
{
$this->config = $config;
$this->parent = $parent;
/* Try to fetch logs for the given event (mac)
*/
$this->o_queue = new supportDaemon();
/* Load ldap object if given
and use this macAddress.
*/
if(is_object($parent) && $dn != "" && $dn != "new"){
plugin::plugin($config,$dn,$parent);
}
/* Get correct macAddress.
Check if an event is given or a ldap object.
*/
if(is_array($this->parent) && isset($this->parent['MACADDRESS'])){
$this->mac = $this->parent['MACADDRESS'];
$this->standalone = TRUE;
}elseif(isset($parent->attrs['macAddress'][0])){
$this->mac = $parent->attrs['macAddress'][0];
$this->standalone = FALSE;
}
/* Query for log files
*/
$res = $this->o_queue->get_log_info_for_mac($this->mac);
if($this->o_queue->is_error()) {
msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
}
/* Check if there is at least one log file
*/
if(!isset($res[$this->mac]) || !is_array($res[$this->mac])){
$this->logs = array();
}else{
$this->selected_date = key($res[$this->mac]);
$this->logs = $res;
}
}
function execute()
{
$smarty = get_smarty();
$smarty->assign("logs",$this->logs);
$smarty->assign("logs_available", isset($this->logs[$this->mac]));
$smarty->assign("mac",$this->mac);
$smarty->assign("selected_file",$this->selected_file);
$smarty->assign("selected_date",$this->selected_date);
$smarty->assign("log_file", $this->get_log($this->mac,$this->selected_date,$this->selected_file));
$smarty->assign("standalone",$this->standalone);
if (isset($this->logs[$this->mac])){
$date = date("d.m.Y H:i:s",$this->logs[$this->mac][$this->selected_date]['REAL_DATE']);
}
$file = $this->selected_file;
$smarty->assign("selected_log",_("none"));
if(!empty($file)){
$smarty->assign("selected_log",$file.", ".$date);
}
$divlist = new divlist("log_view");
/* Create sort direction images
*/
if($this->sort_dir){
$img = "<img src='images/lists/sort-down.png' border='0' alt='\\/'>";
}else{
$img = "<img src='images/lists/sort-up.png' border='0' alt='/\\'";
}
if($this->sort_by == "file"){
$img1 = $img;
$img2 = "";
}else{
$img1 = "";
$img2 = $img;
}
/* Create list header
*/
$divlist->SetHeader(array(
array("string"=>"<a href='?plug=".$_GET['plug']."&sort_by=file'>"._("File")." ".$img1."</a>",
"attach"=>"width='200px;'"),
array("string"=>"<a href='?plug=".$_GET['plug']."&sort_by=time'>"._("Date")." ".$img2."</a>",
"attach" => "style='border-right:none;'"),
));
/* Create divlist list
*/
$divlist->SetEntriesPerPage(0);
$divlist->SetHeight(150);
/* Create sortable array
*/
$link = "<a href='?plug=".$_GET['plug']."&time=%time%&file=%file%&mac=%mac%'>%str%</a>";
$to_add = array();
$sort_by = $this->sort_by;
foreach($this->logs as $mac => $times){
foreach($times as $time => $data){
$rtime = $data['REAL_DATE'];
foreach($data['FILES'] as $file){
$highlight = "";
if($file == $this->selected_file && $time == $this->selected_date && $mac == $this->mac){
$highlight = "background-color:#CCCCCC";
}
$use_link = preg_replace(array("/%mac%/","/%time%/","/%file%/"),array($mac,$time,$file),$link);
$to_add[$$sort_by.$file.$time] = array(
array("string" => preg_replace("/%str%/",$file,$use_link),
"attach" => "style='width:200px; $highlight'"),
array("string" => preg_replace("/%str%/",date("d.m.Y H:i:s",$rtime),$use_link),
"attach" => "style='border-right:none; $highlight'"),
);
}
}
}
/* Sort entries
*/
if(!$this->sort_dir){
uksort($to_add, "strnatcasecmp");
}else{
uksort($to_add, "strnatcasecmp");
$to_add = array_reverse($to_add);
}
/* Append entries to list
*/
foreach($to_add as $entry){
$divlist->AddEntry($entry);
}
$smarty->assign("ACL",preg_match("/r/",$this->getacl("")));
$smarty->assign("divlist",$divlist->DrawList());
return($smarty->fetch(get_template_path('log_view.tpl', TRUE,dirname(__FILE__))));
}
function get_log($mac,$date,$file)
{
$res = $this->o_queue->get_log_file($mac,$date,$file);
if($this->o_queue->is_error()) {
msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
}
$res = nl2br(htmlentities($res));
return($res);
}
function save_object()
{
foreach(array("time"=>"selected_date","file"=>"selected_file") as $attr => $dest){
if(isset($_GET[$attr])){
$this->$dest = $_GET[$attr];
}
}
if(isset($_GET['sort_by']) && in_array($_GET['sort_by'],array("file","time"))){
if($_GET['sort_by'] == $this->sort_by){
$this->sort_dir = !$this->sort_dir;
}
$this->sort_by = $_GET['sort_by'];
}
}
/* Return plugin informations for acl handling */
static function plInfo()
{
return (array(
"plShortName" => _("Log view"),
"plDescription" => _("GOto log view"),
"plSelfModify" => FALSE,
"plDepends" => array(),
"plPriority" => 30,
"plSection" => array("administration"),
"plCategory" => array("workstation","server"),
"plProvidedAcls"=> array()
));
}
}
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class goto_import_file extends plugin
{
var $events = array();
var $csv_fields = array();
var $import_successful = FALSE; // Indicates that we have successfully imported everything.
public function __construct($config,&$parent)
{
plugin::plugin($config,NULL);
$this->parent = $parent;
$this->daemon_events = DaemonEvent::get_event_types( SYSTEM_EVENT | HIDDEN_EVENT);
$this->csv_fields = array(
"0"=>"TIMESTAMP","1" => "MAC", "2" => "HEADER", "3" => "OGROUP",
"4" => "BASE", "5" => "FQDN", "6" => "IP", "7" => "DHCP");
}
private function parse_csv($str)
{
/* Some file checks
*/
$lines = preg_split("/\n/",$str);
if(empty($str) || !count($lines)){
msg_dialog::display(_("Import"), msgPool::incorrectUpload(_("file is empty")),ERROR_DIALOG);
return;
}
/* Reset current events
*/
$this->events = array();
/* Parse each line of the given file
*/
foreach($lines as $line){
// Skip empty lines.
if(empty($line)) continue;
/* Load values from file
*/
$fields = explode(";",$line);
$event = array();
foreach($this->csv_fields as $key => $val) {
$event[$val] = "";
if(isset($fields[$key])){
$event[$val] = $fields[$key];
}
}
$this->events[] = $event;
}
$this->check_fields();
}
public function execute()
{
/* Import file
*/
if(isset($_POST['import']) && isset($_FILES['file'])) {
if(isset($_FILES['file']['tmp_name'])){
$str = file_get_contents($_FILES['file']['tmp_name']);
$this->parse_csv($str);
}
}
/* Import started
*/
$confirmed = FALSE;
foreach($_POST as $name => $value){
if(preg_match("/^MSG_OK/",$name)){
$confirmed = TRUE;
}
}
if(isset($_POST['start_import']) || $confirmed){
$error = FALSE;
if(!$confirmed){
foreach($this->events as $event){
if(!empty($event['ERROR'])){
$error = TRUE;
break;
}
}
if($error){
msg_dialog::display(_("Import"),
_("Selected entries will be skipped because of errors. Do you want to proceed?"),CONFIRM_DIALOG);
}
}
if(!$error){
$success = 0;
$fail = 0;
foreach($this->events as $key => $event){
if(!empty($event['ERROR'])){
$fail ++;
continue;
}
/* Create event
*/
$class= $this->daemon_events['QUEUED'][$event['HEADER']];
$o_data = $this->daemon_events['BY_CLASS'][$class];
$object = new $class($this->config);
$object->add_targets(array($event['MAC']));
if($o_data['s_Schedule_Action'] == $event['HEADER']){
$object->set_type(SCHEDULED_EVENT);
}else{
$object->set_type(TRIGGERED_EVENT);
}
/* Update values like fqdn a.s.o
*/
foreach($this->csv_fields as $name){
if($name == "TIMESTAMP" && empty($event[$name])) continue;
$object->set_value($name,$event[$name]);
}
if(!$this->parent->o_queue->append($object)){
msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->parent->o_queue->get_error()),ERROR_DIALOG);
$fail ++;
}else{
unset($this->events[$key]);
$success ++;
}
}
msg_dialog::display(_("Import"),sprintf(_("Import complete: %s events successfully send, %s failed"),$success,$fail),INFO_DIALOG);
$this->import_successful = count($this->events) == 0;
}
}
/* Prepare output
*/
$evts = $this->events;
foreach($this->events as $id => $evt){
foreach($evt as $key => $val){
if(in_array($key,$this->csv_fields)){
$evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>";
}
}
}
$smarty = get_smarty();
$smarty->assign("info",$evts);
$smarty->assign("count",count($evts));
return($smarty->fetch(get_template_path('goto_import_file.tpl', TRUE)));
}
public function check()
{
$message = plugin::check();
$this->check_fields();
return($message);
}
private function check_fields()
{
foreach($this->events as $key => $event){
$this->events[$key]['ERROR'] = "";
if(empty($event['MAC']) || !tests::is_mac($event['MAC'])){
$this->events[$key]['ERROR'] .= msgPool::invalid(_("MAC")).", ";
}
if(empty($event['HEADER']) || !isset($this->daemon_events['QUEUED'][$event['HEADER']])){
$this->events[$key]['ERROR'] .= msgPool::invalid(_("Event")).", ";
}
$this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'],", ");
}
}
}
// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class gotomasses extends management
{
var $plHeadline = "System deployment status";
var $plDescription = "System deployment status";