Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 116cc0934541e680239835567897eb271c36be53 Mon Sep 17 00:00:00 2001
- From: LaMi <lars@vertical-visions.de>
- Date: Sat, 9 Jan 2010 16:25:00 +0100
- Subject: [PATCH] Fixed redirection of frontend messages in nagvis-js frontend; Fixed new map deletion method
- ---
- share/frontend/nagvis-js/js/frontendMessage.js | 4 +-
- share/frontend/wui/classes/WuiMainCfg.php | 18 -
- share/frontend/wui/classes/WuiMapCfg.php | 466 ------------------------
- share/server/core/classes/CoreModMap.php | 9 +-
- share/server/core/classes/GlobalMapCfg.php | 20 +-
- share/server/core/classes/WuiMapCfg.php | 466 ++++++++++++++++++++++++
- 6 files changed, 495 insertions(+), 488 deletions(-)
- delete mode 100644 share/frontend/wui/classes/WuiMapCfg.php
- create mode 100644 share/server/core/classes/WuiMapCfg.php
- diff --git a/share/frontend/nagvis-js/js/frontendMessage.js b/share/frontend/nagvis-js/js/frontendMessage.js
- index 1cfd9c9..c1a160e 100644
- --- a/share/frontend/nagvis-js/js/frontendMessage.js
- +++ b/share/frontend/nagvis-js/js/frontendMessage.js
- @@ -171,10 +171,12 @@ function frontendMessage(oMessage, iTimeout) {
- var sUrl = window.location.href;
- // Maybe enable redirect
- - if(typeof oMessage.realoadUrl !== 'undefined' && oMessage.realoadUrl !== null) {
- + if(typeof oMessage.reloadUrl !== 'undefined' && oMessage.reloadUrl !== null) {
- sUrl = oMessage.reloadUrl;
- }
- + eventlog("frontendMessage", "note", "Schedule reload in "+oMessage.reloadTime+" sec. URL: "+sUrl);
- +
- // Register reload/redirect
- setTimeout(function() {window.location = sUrl;}, oMessage.reloadTime*1000);
- }
- diff --git a/share/frontend/wui/classes/WuiMainCfg.php b/share/frontend/wui/classes/WuiMainCfg.php
- index d5dd7ed..2a7a664 100644
- --- a/share/frontend/wui/classes/WuiMainCfg.php
- +++ b/share/frontend/wui/classes/WuiMainCfg.php
- @@ -214,23 +214,5 @@ class WuiMainCfg extends GlobalMainCfg {
- return FALSE;
- }
- }
- -
- - /**
- - * Checks for writeable MapCfgFolder
- - *
- - * @param Boolean $printErr
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function checkMapCfgFolderWriteable($printErr) {
- - if(file_exists(substr($this->getValue('paths', 'mapcfg'),0,-1)) && is_writable(substr($this->getValue('paths', 'mapcfg'),0,-1))) {
- - return TRUE;
- - } else {
- - if($printErr == 1) {
- - new GlobalMessage('ERROR', WuiCore::getInstance()->getLang()->getText('mapCfgDirNotWriteable','MAPPATH~'.$this->getValue('paths', 'mapcfg')), WuiCore::getInstance()->getMainCfg()->getValue('paths','htmlbase'));
- - }
- - return FALSE;
- - }
- - }
- }
- ?>
- diff --git a/share/frontend/wui/classes/WuiMapCfg.php b/share/frontend/wui/classes/WuiMapCfg.php
- deleted file mode 100644
- index 8b73b10..0000000
- --- a/share/frontend/wui/classes/WuiMapCfg.php
- +++ /dev/null
- @@ -1,466 +0,0 @@
- -<?php
- -/*****************************************************************************
- - *
- - * WuiMapCfg.php - Class for handling the map configuration in WUI
- - *
- - * Copyright (c) 2004-2008 NagVis Project (Contact: lars@vertical-visions.de)
- - *
- - * License:
- - *
- - * This program is free software; you can redistribute it and/or modify
- - * it under the terms of the GNU General Public License version 2 as
- - * published by the Free Software Foundation.
- - *
- - * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
- - *
- - *****************************************************************************/
- -
- -/**
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- -class WuiMapCfg extends GlobalMapCfg {
- -
- - /**
- - * Class Constructor
- - *
- - * @param WuiCore $CORE
- - * @param String $name Name of the map
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function WuiMapCfg($CORE, $name='') {
- - $this->CORE = $CORE;
- -
- - $this->name = $name;
- -
- - $this->getMap();
- - parent::__construct($CORE, $this->name);
- - }
- -
- - /**
- - * Reads which map should be displayed, primary use
- - * the map defined in the url, if there is no map
- - * in url, use first entry of "maps" defined in
- - * the NagVis main config
- - *
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function getMap() {
- - // check the $this->name string for security reasons (its the ONLY value we get directly from external...)
- - // Allow ONLY Characters, Numbers, - and _ inside the Name of a Map
- - $this->name = preg_replace("/[^a-zA-Z0-9_-]/",'',$this->name);
- - }
- -
- - /**
- - * Gets all informations about an object type
- - *
- - * @param String Type to get the informations for
- - * @return Array The validConfig array
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function getValidObjectType($type) {
- - return $this->validConfig[$type];
- - }
- -
- - /**
- - * Gets the valid configuration array
- - *
- - * @return Array The validConfig array
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function getValidConfig() {
- - return $this->validConfig;
- - }
- -
- - /**
- - * Reads the configuration file of the map and
- - * sends it as download to the client.
- - *
- - * @return Boolean Only returns FALSE if something went wrong
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function exportMap() {
- - if($this->checkMapConfigReadable(1)) {
- - $mapPath = $this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->getName().'.cfg';
- -
- - header('Content-Type: application/octet-stream');
- - header('Content-Disposition: attachment; filename='.$this->getName().'.cfg');
- - header('Content-Length: '.filesize($mapPath));
- -
- - if(readfile($mapPath)) {
- - exit;
- - } else {
- - return FALSE;
- - }
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Deletes the map configfile
- - *
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function deleteMapConfig($printErr=1) {
- - // is file writeable?
- - if($this->checkMapConfigWriteable($printErr)) {
- - if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
- - return TRUE;
- - } else {
- - if($printErr) {
- - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('couldNotDeleteMapCfg','MAPPATH~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
- - }
- - return FALSE;
- - }
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Checks for writeable map config file
- - *
- - * @param Boolean $printErr
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function checkMapConfigWriteable($printErr) {
- - if($this->checkMapConfigExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
- - return TRUE;
- - } else {
- - if($printErr) {
- - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapCfgNotWriteable','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
- - }
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Writes the element from array to the config file
- - *
- - * @param String $type Type of the Element
- - * @param Integer $id Id of the Element
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function writeElement($type,$id) {
- - if($this->checkMapConfigExists(1) && $this->checkMapConfigReadable(1) && $this->checkMapConfigWriteable(1)) {
- - // read file in array
- - $file = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg');
- -
- - // number of lines in the file
- - $l = 0;
- - // number of elements of the given type
- - $a = 0;
- - // done?!
- - $done = FALSE;
- - while(isset($file[$l]) && $file[$l] != '' && $done == FALSE) {
- - // ignore comments
- - if(!preg_match('/^#/',$file[$l]) && !preg_match('/^;/',$file[$l])) {
- - $defineCln = explode('{', $file[$l]);
- - $define = explode(' ',$defineCln[0]);
- - // select only elements of the given type
- - if(isset($define[1]) && trim($define[1]) == $type) {
- - // check if element exists
- - if($a == $id) {
- - // check if element is an array...
- - if(is_array($this->mapConfig[$type][$a])) {
- - // ...array: update!
- -
- - // choose first parameter line
- - $l++;
- -
- - // Loop all parameters from array
- - foreach($this->mapConfig[$type][$id] AS $key => $val) {
- - // if key is not type
- - if($key != 'type' && $key != 'object_id') {
- - $cfgLines = 0;
- - $cfgLine = '';
- - $cfgLineNr = 0;
- -
- - // Loop parameters from file (Find line for this option)
- - while(isset($file[($l+$cfgLines)]) && trim($file[($l+$cfgLines)]) != '}') {
- - $entry = explode('=',$file[$l+$cfgLines], 2);
- - if($key == trim($entry[0])) {
- - $cfgLineNr = $l+$cfgLines;
- - if(is_array($val)) {
- - $val = implode(',',$val);
- - }
- - $cfgLine = $key.'='.$val."\n";
- - }
- - $cfgLines++;
- - }
- -
- - if($cfgLineNr != 0 && $val != '') {
- - // if a parameter was found in file and value is not empty, replace line
- - $file[$cfgLineNr] = $cfgLine;
- - } elseif($cfgLineNr != 0 && $val == '') {
- - // if a paremter is not in array or a value is empty, delete the line in the file
- - $file[$cfgLineNr] = '';
- - $cfgLines--;
- - } elseif($cfgLineNr == 0 && $val != '') {
- - // if a parameter is was not found in array and a value is not empty, create line
- - if(is_array($val)) {
- - $val = implode(',',$val);
- - }
- - $neu = $key.'='.$val."\n";
- -
- - for($i = $l; $i < count($file);$i++) {
- - $tmp = $file[$i];
- - $file[$i] = $neu;
- - $neu = $tmp;
- - }
- - $file[count($file)] = $neu;
- - } elseif($cfgLineNr == 0 && $val == '') {
- - // if a parameter is empty and a value is empty, do nothing
- - }
- - }
- - }
- - $l++;
- - } else {
- - // ...no array: delete!
- - $cfgLines = 0;
- - while(trim($file[($l+$cfgLines)]) != '}') {
- - $cfgLines++;
- - }
- - $cfgLines++;
- -
- - for($i = $l; $i <= $l+$cfgLines;$i++) {
- - unset($file[$i]);
- - }
- - }
- -
- - $done = TRUE;
- - }
- - $a++;
- - }
- - }
- - $l++;
- - }
- -
- - // reached end of file - couldn't find that element, create a new one...
- - if($done == FALSE) {
- - if(count($file) > 0 && $file[count($file)-1] != "\n") {
- - $file[] = "\n";
- - }
- - $file[] = 'define '.$type." {\n";
- - $aKeys = $this->getValidTypeKeys($type);
- - foreach($aKeys As $key) {
- - $val = $this->getValue($type, $id, $key, TRUE);
- - if(isset($val) && $val != '') {
- - $file[] = $key.'='.$val."\n";
- - }
- - }
- - $file[] = "}\n";
- - $file[] = "\n";
- - }
- -
- - // open file for writing and replace it
- - $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg','w');
- - fwrite($fp,implode('',$file));
- - fclose($fp);
- - return TRUE;
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Gets lockfile informations
- - *
- - * @param Boolean $ignoreLock
- - * @param Boolean $printErr
- - * @return Array/Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function checkMapLocked($ignoreLock=0,$printErr=1) {
- - // read lockfile
- - $lockdata = $this->readMapLock();
- - if(is_array($lockdata)) {
- - // check if the lock is older than 5 Minutes and don't ignore lock
- - if($lockdata['time'] > time() - $this->CORE->getMainCfg()->getValue('wui','maplocktime') * 60) {
- - if($ignoreLock == 0) {
- - // the lock should be ignored
- - return FALSE;
- - } else {
- - // there is a lock and it should be recognized
- - // check if this is the lock of the current user (Happens e.g. by pressing F5)
- - if($this->CORE->getMainCfg()->getRuntimeValue('user') == $lockdata['user'] && $_SERVER['REMOTE_ADDR'] == $lockdata['ip']) {
- - // refresh the lock (write a new lock)
- - $this->writeMapLock();
- - // it's locked by the current user, so it's not locked for him
- - return FALSE;
- - }
- - if($printErr == 1) {
- - $LANG = new GlobalLanguage($this->CORE->getMainCfg(),'nagvis');
- -
- - // message the user that there is a lock by another user, the user can decide wether he want's to override it or not
- - print '<script>if(!confirm(\''.$LANG->getText('mapLocked','MAP~'.$this->name.',TIME~'.date('d.m.Y H:i',$lockdata['time']).',USER~'.$lockdata['user'].',IP~'.$lockdata['ip']).'\',\'\')) { history.back(); }</script>';
- - }
- - return TRUE;
- - }
- - } else {
- - // delete lockfile & continue
- - // try to delete map lock, if nothing to delete its OK
- - $this->deleteMapLock();
- - return FALSE;
- - }
- - } else {
- - // no valid informations in lock or no lock there
- - // try to delete map lock, if nothing to delete its OK
- - $this->deleteMapLock();
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Reads the contents of the lockfile
- - *
- - * @return Array/Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function readMapLock() {
- - if($this->checkMapLockReadable(0)) {
- - $fileContent = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock');
- - // only recognize the first line, explode it by :
- - $arrContent = explode(':',$fileContent[0]);
- - // if there are more elements in the array it is OK
- - if(count($arrContent) > 0) {
- - return Array('time' => $arrContent[0], 'user' => $arrContent[1], 'ip' => $arrContent[2]);
- - } else {
- - return FALSE;
- - }
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Writes the lockfile for a map
- - *
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function writeMapLock() {
- - if($this->checkMapLockWriteable(0)) {
- - // open file for writing and insert the needed informations
- - $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock','w');
- - fwrite($fp,time().':'.$this->CORE->getMainCfg()->getRuntimeValue('user').':'.$_SERVER['REMOTE_ADDR']);
- - fclose($fp);
- - return TRUE;
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Deletes the lockfile for a map
- - *
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function deleteMapLock() {
- - if($this->checkMapLockWriteable(0)) {
- - if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- - // map lock deleted => OK
- - return TRUE;
- - } else {
- - return FALSE;
- - }
- - } else {
- - // no map lock to delete => OK
- - return TRUE;
- - }
- - }
- -
- - /**
- - * Checks for existing lockfile
- - *
- - * @param Boolean $printErr
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function checkMapLockExists($printErr) {
- - if($this->name != '') {
- - if(file_exists($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- - return TRUE;
- - } else {
- - if($printErr) {
- - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotExists','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
- - }
- - return FALSE;
- - }
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Checks for readable lockfile
- - *
- - * @param Boolean $printErr
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function checkMapLockReadable($printErr) {
- - if($this->name != '') {
- - if($this->checkMapLockExists($printErr) && is_readable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- - return TRUE;
- - } else {
- - if($printErr) {
- - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotReadable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
- - }
- - return FALSE;
- - }
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Checks for writeable lockfile
- - *
- - * @param Boolean $printErr
- - * @return Boolean Is Successful?
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function checkMapLockWriteable($printErr) {
- - if($this->name != '') {
- - if($this->checkMapLockExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- - return TRUE;
- - } else {
- - if($printErr) {
- - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotWriteable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
- - }
- - return FALSE;
- - }
- - } else {
- - return FALSE;
- - }
- - }
- -
- - /**
- - * Parses WUI specific settings
- - *
- - * @return String JSON Code
- - * @author Lars Michelsen <lars@vertical-visions.de>
- - */
- - function parseViewProperties() {
- - $arr = Array();
- -
- - $arr['grid_show'] = intval($this->getValue('global', 0, 'grid_show'));
- - $arr['grid_color'] = $this->getValue('global', 0, 'grid_color');
- - $arr['grid_steps'] = intval($this->getValue('global', 0, 'grid_steps'));
- -
- - return json_encode($arr);
- - }
- -}
- -?>
- diff --git a/share/server/core/classes/CoreModMap.php b/share/server/core/classes/CoreModMap.php
- index 97f0463..2b2c3ce 100644
- --- a/share/server/core/classes/CoreModMap.php
- +++ b/share/server/core/classes/CoreModMap.php
- @@ -79,7 +79,12 @@ class CoreModMap extends CoreModule {
- if($aReturn !== false) {
- // Try to create the map
- if($this->doAdd($aReturn)) {
- - new GlobalMessage('NOTE', $this->CORE->getLang()->getText('The map has been created.'));
- + new GlobalMessage('NOTE',
- + $this->CORE->getLang()->getText('The map has been created.'),
- + null,
- + null,
- + 1,
- + $this->CORE->getMainCfg()->getValue('paths','htmlbase').'/frontend/wui/index.php?mod=Map&act=edit&show='.$aReturn['map_name']);
- $sReturn = '';
- } else {
- new GlobalMessage('ERROR', $this->CORE->getLang()->getText('The map could not be created.'));
- @@ -137,7 +142,7 @@ class CoreModMap extends CoreModule {
- // Return the data
- return Array(
- 'map_name' => $FHANDLER->get('map_name'),
- - 'allowed_users' => $FHANDLER->get('allowed_users'),
- + 'allowed_user' => $FHANDLER->get('allowed_users'),
- 'allowed_for_config' => $FHANDLER->get('allowed_for_config'),
- 'iconset' => $FHANDLER->get('map_iconset'),
- 'map_image' => $FHANDLER->get('map_image'));
- diff --git a/share/server/core/classes/GlobalMapCfg.php b/share/server/core/classes/GlobalMapCfg.php
- index aea8820..283d311 100644
- --- a/share/server/core/classes/GlobalMapCfg.php
- +++ b/share/server/core/classes/GlobalMapCfg.php
- @@ -1264,7 +1264,7 @@ class GlobalMapCfg {
- public function createMapConfig() {
- // does file exist?
- if(!$this->checkMapConfigReadable(FALSE)) {
- - if($this->CORE->getMainCfg()->checkMapCfgFolderWriteable(TRUE)) {
- + if($this->checkMapCfgFolderWriteable(TRUE)) {
- // create empty file
- $fp = fopen($this->configFile, 'w');
- fclose($fp);
- @@ -1703,6 +1703,24 @@ class GlobalMapCfg {
- }
- /**
- + * Checks for writeable MapCfgFolder
- + *
- + * @param Boolean $printErr
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function checkMapCfgFolderWriteable($printErr) {
- + if(file_exists(substr($this->CORE->getMainCfg()->getValue('paths', 'mapcfg'),0,-1)) && is_writable(substr($this->CORE->getMainCfg()->getValue('paths', 'mapcfg'),0,-1))) {
- + return TRUE;
- + } else {
- + if($printErr == 1) {
- + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapCfgDirNotWriteable', Array('MAPPATH' => $this->CORE->getMainCfg()->getValue('paths', 'mapcfg'))), $this->CORE->getMainCfg()->getValue('paths','htmlbase'));
- + }
- + return FALSE;
- + }
- + }
- +
- + /**
- * Deletes an element of the specified type from the config array
- *
- * @param String $type
- diff --git a/share/server/core/classes/WuiMapCfg.php b/share/server/core/classes/WuiMapCfg.php
- new file mode 100644
- index 0000000..8b73b10
- --- /dev/null
- +++ b/share/server/core/classes/WuiMapCfg.php
- @@ -0,0 +1,466 @@
- +<?php
- +/*****************************************************************************
- + *
- + * WuiMapCfg.php - Class for handling the map configuration in WUI
- + *
- + * Copyright (c) 2004-2008 NagVis Project (Contact: lars@vertical-visions.de)
- + *
- + * License:
- + *
- + * This program is free software; you can redistribute it and/or modify
- + * it under the terms of the GNU General Public License version 2 as
- + * published by the Free Software Foundation.
- + *
- + * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
- + *
- + *****************************************************************************/
- +
- +/**
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- +class WuiMapCfg extends GlobalMapCfg {
- +
- + /**
- + * Class Constructor
- + *
- + * @param WuiCore $CORE
- + * @param String $name Name of the map
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function WuiMapCfg($CORE, $name='') {
- + $this->CORE = $CORE;
- +
- + $this->name = $name;
- +
- + $this->getMap();
- + parent::__construct($CORE, $this->name);
- + }
- +
- + /**
- + * Reads which map should be displayed, primary use
- + * the map defined in the url, if there is no map
- + * in url, use first entry of "maps" defined in
- + * the NagVis main config
- + *
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function getMap() {
- + // check the $this->name string for security reasons (its the ONLY value we get directly from external...)
- + // Allow ONLY Characters, Numbers, - and _ inside the Name of a Map
- + $this->name = preg_replace("/[^a-zA-Z0-9_-]/",'',$this->name);
- + }
- +
- + /**
- + * Gets all informations about an object type
- + *
- + * @param String Type to get the informations for
- + * @return Array The validConfig array
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function getValidObjectType($type) {
- + return $this->validConfig[$type];
- + }
- +
- + /**
- + * Gets the valid configuration array
- + *
- + * @return Array The validConfig array
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function getValidConfig() {
- + return $this->validConfig;
- + }
- +
- + /**
- + * Reads the configuration file of the map and
- + * sends it as download to the client.
- + *
- + * @return Boolean Only returns FALSE if something went wrong
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function exportMap() {
- + if($this->checkMapConfigReadable(1)) {
- + $mapPath = $this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->getName().'.cfg';
- +
- + header('Content-Type: application/octet-stream');
- + header('Content-Disposition: attachment; filename='.$this->getName().'.cfg');
- + header('Content-Length: '.filesize($mapPath));
- +
- + if(readfile($mapPath)) {
- + exit;
- + } else {
- + return FALSE;
- + }
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Deletes the map configfile
- + *
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function deleteMapConfig($printErr=1) {
- + // is file writeable?
- + if($this->checkMapConfigWriteable($printErr)) {
- + if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
- + return TRUE;
- + } else {
- + if($printErr) {
- + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('couldNotDeleteMapCfg','MAPPATH~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
- + }
- + return FALSE;
- + }
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Checks for writeable map config file
- + *
- + * @param Boolean $printErr
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function checkMapConfigWriteable($printErr) {
- + if($this->checkMapConfigExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
- + return TRUE;
- + } else {
- + if($printErr) {
- + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapCfgNotWriteable','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
- + }
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Writes the element from array to the config file
- + *
- + * @param String $type Type of the Element
- + * @param Integer $id Id of the Element
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function writeElement($type,$id) {
- + if($this->checkMapConfigExists(1) && $this->checkMapConfigReadable(1) && $this->checkMapConfigWriteable(1)) {
- + // read file in array
- + $file = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg');
- +
- + // number of lines in the file
- + $l = 0;
- + // number of elements of the given type
- + $a = 0;
- + // done?!
- + $done = FALSE;
- + while(isset($file[$l]) && $file[$l] != '' && $done == FALSE) {
- + // ignore comments
- + if(!preg_match('/^#/',$file[$l]) && !preg_match('/^;/',$file[$l])) {
- + $defineCln = explode('{', $file[$l]);
- + $define = explode(' ',$defineCln[0]);
- + // select only elements of the given type
- + if(isset($define[1]) && trim($define[1]) == $type) {
- + // check if element exists
- + if($a == $id) {
- + // check if element is an array...
- + if(is_array($this->mapConfig[$type][$a])) {
- + // ...array: update!
- +
- + // choose first parameter line
- + $l++;
- +
- + // Loop all parameters from array
- + foreach($this->mapConfig[$type][$id] AS $key => $val) {
- + // if key is not type
- + if($key != 'type' && $key != 'object_id') {
- + $cfgLines = 0;
- + $cfgLine = '';
- + $cfgLineNr = 0;
- +
- + // Loop parameters from file (Find line for this option)
- + while(isset($file[($l+$cfgLines)]) && trim($file[($l+$cfgLines)]) != '}') {
- + $entry = explode('=',$file[$l+$cfgLines], 2);
- + if($key == trim($entry[0])) {
- + $cfgLineNr = $l+$cfgLines;
- + if(is_array($val)) {
- + $val = implode(',',$val);
- + }
- + $cfgLine = $key.'='.$val."\n";
- + }
- + $cfgLines++;
- + }
- +
- + if($cfgLineNr != 0 && $val != '') {
- + // if a parameter was found in file and value is not empty, replace line
- + $file[$cfgLineNr] = $cfgLine;
- + } elseif($cfgLineNr != 0 && $val == '') {
- + // if a paremter is not in array or a value is empty, delete the line in the file
- + $file[$cfgLineNr] = '';
- + $cfgLines--;
- + } elseif($cfgLineNr == 0 && $val != '') {
- + // if a parameter is was not found in array and a value is not empty, create line
- + if(is_array($val)) {
- + $val = implode(',',$val);
- + }
- + $neu = $key.'='.$val."\n";
- +
- + for($i = $l; $i < count($file);$i++) {
- + $tmp = $file[$i];
- + $file[$i] = $neu;
- + $neu = $tmp;
- + }
- + $file[count($file)] = $neu;
- + } elseif($cfgLineNr == 0 && $val == '') {
- + // if a parameter is empty and a value is empty, do nothing
- + }
- + }
- + }
- + $l++;
- + } else {
- + // ...no array: delete!
- + $cfgLines = 0;
- + while(trim($file[($l+$cfgLines)]) != '}') {
- + $cfgLines++;
- + }
- + $cfgLines++;
- +
- + for($i = $l; $i <= $l+$cfgLines;$i++) {
- + unset($file[$i]);
- + }
- + }
- +
- + $done = TRUE;
- + }
- + $a++;
- + }
- + }
- + $l++;
- + }
- +
- + // reached end of file - couldn't find that element, create a new one...
- + if($done == FALSE) {
- + if(count($file) > 0 && $file[count($file)-1] != "\n") {
- + $file[] = "\n";
- + }
- + $file[] = 'define '.$type." {\n";
- + $aKeys = $this->getValidTypeKeys($type);
- + foreach($aKeys As $key) {
- + $val = $this->getValue($type, $id, $key, TRUE);
- + if(isset($val) && $val != '') {
- + $file[] = $key.'='.$val."\n";
- + }
- + }
- + $file[] = "}\n";
- + $file[] = "\n";
- + }
- +
- + // open file for writing and replace it
- + $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg','w');
- + fwrite($fp,implode('',$file));
- + fclose($fp);
- + return TRUE;
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Gets lockfile informations
- + *
- + * @param Boolean $ignoreLock
- + * @param Boolean $printErr
- + * @return Array/Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function checkMapLocked($ignoreLock=0,$printErr=1) {
- + // read lockfile
- + $lockdata = $this->readMapLock();
- + if(is_array($lockdata)) {
- + // check if the lock is older than 5 Minutes and don't ignore lock
- + if($lockdata['time'] > time() - $this->CORE->getMainCfg()->getValue('wui','maplocktime') * 60) {
- + if($ignoreLock == 0) {
- + // the lock should be ignored
- + return FALSE;
- + } else {
- + // there is a lock and it should be recognized
- + // check if this is the lock of the current user (Happens e.g. by pressing F5)
- + if($this->CORE->getMainCfg()->getRuntimeValue('user') == $lockdata['user'] && $_SERVER['REMOTE_ADDR'] == $lockdata['ip']) {
- + // refresh the lock (write a new lock)
- + $this->writeMapLock();
- + // it's locked by the current user, so it's not locked for him
- + return FALSE;
- + }
- + if($printErr == 1) {
- + $LANG = new GlobalLanguage($this->CORE->getMainCfg(),'nagvis');
- +
- + // message the user that there is a lock by another user, the user can decide wether he want's to override it or not
- + print '<script>if(!confirm(\''.$LANG->getText('mapLocked','MAP~'.$this->name.',TIME~'.date('d.m.Y H:i',$lockdata['time']).',USER~'.$lockdata['user'].',IP~'.$lockdata['ip']).'\',\'\')) { history.back(); }</script>';
- + }
- + return TRUE;
- + }
- + } else {
- + // delete lockfile & continue
- + // try to delete map lock, if nothing to delete its OK
- + $this->deleteMapLock();
- + return FALSE;
- + }
- + } else {
- + // no valid informations in lock or no lock there
- + // try to delete map lock, if nothing to delete its OK
- + $this->deleteMapLock();
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Reads the contents of the lockfile
- + *
- + * @return Array/Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function readMapLock() {
- + if($this->checkMapLockReadable(0)) {
- + $fileContent = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock');
- + // only recognize the first line, explode it by :
- + $arrContent = explode(':',$fileContent[0]);
- + // if there are more elements in the array it is OK
- + if(count($arrContent) > 0) {
- + return Array('time' => $arrContent[0], 'user' => $arrContent[1], 'ip' => $arrContent[2]);
- + } else {
- + return FALSE;
- + }
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Writes the lockfile for a map
- + *
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function writeMapLock() {
- + if($this->checkMapLockWriteable(0)) {
- + // open file for writing and insert the needed informations
- + $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock','w');
- + fwrite($fp,time().':'.$this->CORE->getMainCfg()->getRuntimeValue('user').':'.$_SERVER['REMOTE_ADDR']);
- + fclose($fp);
- + return TRUE;
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Deletes the lockfile for a map
- + *
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function deleteMapLock() {
- + if($this->checkMapLockWriteable(0)) {
- + if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- + // map lock deleted => OK
- + return TRUE;
- + } else {
- + return FALSE;
- + }
- + } else {
- + // no map lock to delete => OK
- + return TRUE;
- + }
- + }
- +
- + /**
- + * Checks for existing lockfile
- + *
- + * @param Boolean $printErr
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function checkMapLockExists($printErr) {
- + if($this->name != '') {
- + if(file_exists($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- + return TRUE;
- + } else {
- + if($printErr) {
- + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotExists','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
- + }
- + return FALSE;
- + }
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Checks for readable lockfile
- + *
- + * @param Boolean $printErr
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function checkMapLockReadable($printErr) {
- + if($this->name != '') {
- + if($this->checkMapLockExists($printErr) && is_readable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- + return TRUE;
- + } else {
- + if($printErr) {
- + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotReadable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
- + }
- + return FALSE;
- + }
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Checks for writeable lockfile
- + *
- + * @param Boolean $printErr
- + * @return Boolean Is Successful?
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function checkMapLockWriteable($printErr) {
- + if($this->name != '') {
- + if($this->checkMapLockExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
- + return TRUE;
- + } else {
- + if($printErr) {
- + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotWriteable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
- + }
- + return FALSE;
- + }
- + } else {
- + return FALSE;
- + }
- + }
- +
- + /**
- + * Parses WUI specific settings
- + *
- + * @return String JSON Code
- + * @author Lars Michelsen <lars@vertical-visions.de>
- + */
- + function parseViewProperties() {
- + $arr = Array();
- +
- + $arr['grid_show'] = intval($this->getValue('global', 0, 'grid_show'));
- + $arr['grid_color'] = $this->getValue('global', 0, 'grid_color');
- + $arr['grid_steps'] = intval($this->getValue('global', 0, 'grid_steps'));
- +
- + return json_encode($arr);
- + }
- +}
- +?>
- --
- 1.7.0.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement