Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.87 KB | None | 0 0
  1. From 116cc0934541e680239835567897eb271c36be53 Mon Sep 17 00:00:00 2001
  2. From: LaMi <lars@vertical-visions.de>
  3. Date: Sat, 9 Jan 2010 16:25:00 +0100
  4. Subject: [PATCH] Fixed redirection of frontend messages in nagvis-js frontend; Fixed new map deletion method
  5.  
  6. ---
  7. share/frontend/nagvis-js/js/frontendMessage.js | 4 +-
  8. share/frontend/wui/classes/WuiMainCfg.php | 18 -
  9. share/frontend/wui/classes/WuiMapCfg.php | 466 ------------------------
  10. share/server/core/classes/CoreModMap.php | 9 +-
  11. share/server/core/classes/GlobalMapCfg.php | 20 +-
  12. share/server/core/classes/WuiMapCfg.php | 466 ++++++++++++++++++++++++
  13. 6 files changed, 495 insertions(+), 488 deletions(-)
  14. delete mode 100644 share/frontend/wui/classes/WuiMapCfg.php
  15. create mode 100644 share/server/core/classes/WuiMapCfg.php
  16.  
  17. diff --git a/share/frontend/nagvis-js/js/frontendMessage.js b/share/frontend/nagvis-js/js/frontendMessage.js
  18. index 1cfd9c9..c1a160e 100644
  19. --- a/share/frontend/nagvis-js/js/frontendMessage.js
  20. +++ b/share/frontend/nagvis-js/js/frontendMessage.js
  21. @@ -171,10 +171,12 @@ function frontendMessage(oMessage, iTimeout) {
  22. var sUrl = window.location.href;
  23.  
  24. // Maybe enable redirect
  25. - if(typeof oMessage.realoadUrl !== 'undefined' && oMessage.realoadUrl !== null) {
  26. + if(typeof oMessage.reloadUrl !== 'undefined' && oMessage.reloadUrl !== null) {
  27. sUrl = oMessage.reloadUrl;
  28. }
  29.  
  30. + eventlog("frontendMessage", "note", "Schedule reload in "+oMessage.reloadTime+" sec. URL: "+sUrl);
  31. +
  32. // Register reload/redirect
  33. setTimeout(function() {window.location = sUrl;}, oMessage.reloadTime*1000);
  34. }
  35. diff --git a/share/frontend/wui/classes/WuiMainCfg.php b/share/frontend/wui/classes/WuiMainCfg.php
  36. index d5dd7ed..2a7a664 100644
  37. --- a/share/frontend/wui/classes/WuiMainCfg.php
  38. +++ b/share/frontend/wui/classes/WuiMainCfg.php
  39. @@ -214,23 +214,5 @@ class WuiMainCfg extends GlobalMainCfg {
  40. return FALSE;
  41. }
  42. }
  43. -
  44. - /**
  45. - * Checks for writeable MapCfgFolder
  46. - *
  47. - * @param Boolean $printErr
  48. - * @return Boolean Is Successful?
  49. - * @author Lars Michelsen <lars@vertical-visions.de>
  50. - */
  51. - function checkMapCfgFolderWriteable($printErr) {
  52. - if(file_exists(substr($this->getValue('paths', 'mapcfg'),0,-1)) && is_writable(substr($this->getValue('paths', 'mapcfg'),0,-1))) {
  53. - return TRUE;
  54. - } else {
  55. - if($printErr == 1) {
  56. - new GlobalMessage('ERROR', WuiCore::getInstance()->getLang()->getText('mapCfgDirNotWriteable','MAPPATH~'.$this->getValue('paths', 'mapcfg')), WuiCore::getInstance()->getMainCfg()->getValue('paths','htmlbase'));
  57. - }
  58. - return FALSE;
  59. - }
  60. - }
  61. }
  62. ?>
  63. diff --git a/share/frontend/wui/classes/WuiMapCfg.php b/share/frontend/wui/classes/WuiMapCfg.php
  64. deleted file mode 100644
  65. index 8b73b10..0000000
  66. --- a/share/frontend/wui/classes/WuiMapCfg.php
  67. +++ /dev/null
  68. @@ -1,466 +0,0 @@
  69. -<?php
  70. -/*****************************************************************************
  71. - *
  72. - * WuiMapCfg.php - Class for handling the map configuration in WUI
  73. - *
  74. - * Copyright (c) 2004-2008 NagVis Project (Contact: lars@vertical-visions.de)
  75. - *
  76. - * License:
  77. - *
  78. - * This program is free software; you can redistribute it and/or modify
  79. - * it under the terms of the GNU General Public License version 2 as
  80. - * published by the Free Software Foundation.
  81. - *
  82. - * This program is distributed in the hope that it will be useful,
  83. - * but WITHOUT ANY WARRANTY; without even the implied warranty of
  84. - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  85. - * GNU General Public License for more details.
  86. - *
  87. - * You should have received a copy of the GNU General Public License
  88. - * along with this program; if not, write to the Free Software
  89. - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  90. - *
  91. - *****************************************************************************/
  92. -
  93. -/**
  94. - * @author Lars Michelsen <lars@vertical-visions.de>
  95. - */
  96. -class WuiMapCfg extends GlobalMapCfg {
  97. -
  98. - /**
  99. - * Class Constructor
  100. - *
  101. - * @param WuiCore $CORE
  102. - * @param String $name Name of the map
  103. - * @author Lars Michelsen <lars@vertical-visions.de>
  104. - */
  105. - function WuiMapCfg($CORE, $name='') {
  106. - $this->CORE = $CORE;
  107. -
  108. - $this->name = $name;
  109. -
  110. - $this->getMap();
  111. - parent::__construct($CORE, $this->name);
  112. - }
  113. -
  114. - /**
  115. - * Reads which map should be displayed, primary use
  116. - * the map defined in the url, if there is no map
  117. - * in url, use first entry of "maps" defined in
  118. - * the NagVis main config
  119. - *
  120. - * @author Lars Michelsen <lars@vertical-visions.de>
  121. - */
  122. - function getMap() {
  123. - // check the $this->name string for security reasons (its the ONLY value we get directly from external...)
  124. - // Allow ONLY Characters, Numbers, - and _ inside the Name of a Map
  125. - $this->name = preg_replace("/[^a-zA-Z0-9_-]/",'',$this->name);
  126. - }
  127. -
  128. - /**
  129. - * Gets all informations about an object type
  130. - *
  131. - * @param String Type to get the informations for
  132. - * @return Array The validConfig array
  133. - * @author Lars Michelsen <lars@vertical-visions.de>
  134. - */
  135. - function getValidObjectType($type) {
  136. - return $this->validConfig[$type];
  137. - }
  138. -
  139. - /**
  140. - * Gets the valid configuration array
  141. - *
  142. - * @return Array The validConfig array
  143. - * @author Lars Michelsen <lars@vertical-visions.de>
  144. - */
  145. - function getValidConfig() {
  146. - return $this->validConfig;
  147. - }
  148. -
  149. - /**
  150. - * Reads the configuration file of the map and
  151. - * sends it as download to the client.
  152. - *
  153. - * @return Boolean Only returns FALSE if something went wrong
  154. - * @author Lars Michelsen <lars@vertical-visions.de>
  155. - */
  156. - function exportMap() {
  157. - if($this->checkMapConfigReadable(1)) {
  158. - $mapPath = $this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->getName().'.cfg';
  159. -
  160. - header('Content-Type: application/octet-stream');
  161. - header('Content-Disposition: attachment; filename='.$this->getName().'.cfg');
  162. - header('Content-Length: '.filesize($mapPath));
  163. -
  164. - if(readfile($mapPath)) {
  165. - exit;
  166. - } else {
  167. - return FALSE;
  168. - }
  169. - } else {
  170. - return FALSE;
  171. - }
  172. - }
  173. -
  174. - /**
  175. - * Deletes the map configfile
  176. - *
  177. - * @return Boolean Is Successful?
  178. - * @author Lars Michelsen <lars@vertical-visions.de>
  179. - */
  180. - function deleteMapConfig($printErr=1) {
  181. - // is file writeable?
  182. - if($this->checkMapConfigWriteable($printErr)) {
  183. - if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
  184. - return TRUE;
  185. - } else {
  186. - if($printErr) {
  187. - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('couldNotDeleteMapCfg','MAPPATH~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
  188. - }
  189. - return FALSE;
  190. - }
  191. - } else {
  192. - return FALSE;
  193. - }
  194. - }
  195. -
  196. - /**
  197. - * Checks for writeable map config file
  198. - *
  199. - * @param Boolean $printErr
  200. - * @return Boolean Is Successful?
  201. - * @author Lars Michelsen <lars@vertical-visions.de>
  202. - */
  203. - function checkMapConfigWriteable($printErr) {
  204. - if($this->checkMapConfigExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
  205. - return TRUE;
  206. - } else {
  207. - if($printErr) {
  208. - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapCfgNotWriteable','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
  209. - }
  210. - return FALSE;
  211. - }
  212. - }
  213. -
  214. - /**
  215. - * Writes the element from array to the config file
  216. - *
  217. - * @param String $type Type of the Element
  218. - * @param Integer $id Id of the Element
  219. - * @return Boolean Is Successful?
  220. - * @author Lars Michelsen <lars@vertical-visions.de>
  221. - */
  222. - function writeElement($type,$id) {
  223. - if($this->checkMapConfigExists(1) && $this->checkMapConfigReadable(1) && $this->checkMapConfigWriteable(1)) {
  224. - // read file in array
  225. - $file = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg');
  226. -
  227. - // number of lines in the file
  228. - $l = 0;
  229. - // number of elements of the given type
  230. - $a = 0;
  231. - // done?!
  232. - $done = FALSE;
  233. - while(isset($file[$l]) && $file[$l] != '' && $done == FALSE) {
  234. - // ignore comments
  235. - if(!preg_match('/^#/',$file[$l]) && !preg_match('/^;/',$file[$l])) {
  236. - $defineCln = explode('{', $file[$l]);
  237. - $define = explode(' ',$defineCln[0]);
  238. - // select only elements of the given type
  239. - if(isset($define[1]) && trim($define[1]) == $type) {
  240. - // check if element exists
  241. - if($a == $id) {
  242. - // check if element is an array...
  243. - if(is_array($this->mapConfig[$type][$a])) {
  244. - // ...array: update!
  245. -
  246. - // choose first parameter line
  247. - $l++;
  248. -
  249. - // Loop all parameters from array
  250. - foreach($this->mapConfig[$type][$id] AS $key => $val) {
  251. - // if key is not type
  252. - if($key != 'type' && $key != 'object_id') {
  253. - $cfgLines = 0;
  254. - $cfgLine = '';
  255. - $cfgLineNr = 0;
  256. -
  257. - // Loop parameters from file (Find line for this option)
  258. - while(isset($file[($l+$cfgLines)]) && trim($file[($l+$cfgLines)]) != '}') {
  259. - $entry = explode('=',$file[$l+$cfgLines], 2);
  260. - if($key == trim($entry[0])) {
  261. - $cfgLineNr = $l+$cfgLines;
  262. - if(is_array($val)) {
  263. - $val = implode(',',$val);
  264. - }
  265. - $cfgLine = $key.'='.$val."\n";
  266. - }
  267. - $cfgLines++;
  268. - }
  269. -
  270. - if($cfgLineNr != 0 && $val != '') {
  271. - // if a parameter was found in file and value is not empty, replace line
  272. - $file[$cfgLineNr] = $cfgLine;
  273. - } elseif($cfgLineNr != 0 && $val == '') {
  274. - // if a paremter is not in array or a value is empty, delete the line in the file
  275. - $file[$cfgLineNr] = '';
  276. - $cfgLines--;
  277. - } elseif($cfgLineNr == 0 && $val != '') {
  278. - // if a parameter is was not found in array and a value is not empty, create line
  279. - if(is_array($val)) {
  280. - $val = implode(',',$val);
  281. - }
  282. - $neu = $key.'='.$val."\n";
  283. -
  284. - for($i = $l; $i < count($file);$i++) {
  285. - $tmp = $file[$i];
  286. - $file[$i] = $neu;
  287. - $neu = $tmp;
  288. - }
  289. - $file[count($file)] = $neu;
  290. - } elseif($cfgLineNr == 0 && $val == '') {
  291. - // if a parameter is empty and a value is empty, do nothing
  292. - }
  293. - }
  294. - }
  295. - $l++;
  296. - } else {
  297. - // ...no array: delete!
  298. - $cfgLines = 0;
  299. - while(trim($file[($l+$cfgLines)]) != '}') {
  300. - $cfgLines++;
  301. - }
  302. - $cfgLines++;
  303. -
  304. - for($i = $l; $i <= $l+$cfgLines;$i++) {
  305. - unset($file[$i]);
  306. - }
  307. - }
  308. -
  309. - $done = TRUE;
  310. - }
  311. - $a++;
  312. - }
  313. - }
  314. - $l++;
  315. - }
  316. -
  317. - // reached end of file - couldn't find that element, create a new one...
  318. - if($done == FALSE) {
  319. - if(count($file) > 0 && $file[count($file)-1] != "\n") {
  320. - $file[] = "\n";
  321. - }
  322. - $file[] = 'define '.$type." {\n";
  323. - $aKeys = $this->getValidTypeKeys($type);
  324. - foreach($aKeys As $key) {
  325. - $val = $this->getValue($type, $id, $key, TRUE);
  326. - if(isset($val) && $val != '') {
  327. - $file[] = $key.'='.$val."\n";
  328. - }
  329. - }
  330. - $file[] = "}\n";
  331. - $file[] = "\n";
  332. - }
  333. -
  334. - // open file for writing and replace it
  335. - $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg','w');
  336. - fwrite($fp,implode('',$file));
  337. - fclose($fp);
  338. - return TRUE;
  339. - } else {
  340. - return FALSE;
  341. - }
  342. - }
  343. -
  344. - /**
  345. - * Gets lockfile informations
  346. - *
  347. - * @param Boolean $ignoreLock
  348. - * @param Boolean $printErr
  349. - * @return Array/Boolean Is Successful?
  350. - * @author Lars Michelsen <lars@vertical-visions.de>
  351. - */
  352. - function checkMapLocked($ignoreLock=0,$printErr=1) {
  353. - // read lockfile
  354. - $lockdata = $this->readMapLock();
  355. - if(is_array($lockdata)) {
  356. - // check if the lock is older than 5 Minutes and don't ignore lock
  357. - if($lockdata['time'] > time() - $this->CORE->getMainCfg()->getValue('wui','maplocktime') * 60) {
  358. - if($ignoreLock == 0) {
  359. - // the lock should be ignored
  360. - return FALSE;
  361. - } else {
  362. - // there is a lock and it should be recognized
  363. - // check if this is the lock of the current user (Happens e.g. by pressing F5)
  364. - if($this->CORE->getMainCfg()->getRuntimeValue('user') == $lockdata['user'] && $_SERVER['REMOTE_ADDR'] == $lockdata['ip']) {
  365. - // refresh the lock (write a new lock)
  366. - $this->writeMapLock();
  367. - // it's locked by the current user, so it's not locked for him
  368. - return FALSE;
  369. - }
  370. - if($printErr == 1) {
  371. - $LANG = new GlobalLanguage($this->CORE->getMainCfg(),'nagvis');
  372. -
  373. - // message the user that there is a lock by another user, the user can decide wether he want's to override it or not
  374. - 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>';
  375. - }
  376. - return TRUE;
  377. - }
  378. - } else {
  379. - // delete lockfile & continue
  380. - // try to delete map lock, if nothing to delete its OK
  381. - $this->deleteMapLock();
  382. - return FALSE;
  383. - }
  384. - } else {
  385. - // no valid informations in lock or no lock there
  386. - // try to delete map lock, if nothing to delete its OK
  387. - $this->deleteMapLock();
  388. - return FALSE;
  389. - }
  390. - }
  391. -
  392. - /**
  393. - * Reads the contents of the lockfile
  394. - *
  395. - * @return Array/Boolean Is Successful?
  396. - * @author Lars Michelsen <lars@vertical-visions.de>
  397. - */
  398. - function readMapLock() {
  399. - if($this->checkMapLockReadable(0)) {
  400. - $fileContent = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock');
  401. - // only recognize the first line, explode it by :
  402. - $arrContent = explode(':',$fileContent[0]);
  403. - // if there are more elements in the array it is OK
  404. - if(count($arrContent) > 0) {
  405. - return Array('time' => $arrContent[0], 'user' => $arrContent[1], 'ip' => $arrContent[2]);
  406. - } else {
  407. - return FALSE;
  408. - }
  409. - } else {
  410. - return FALSE;
  411. - }
  412. - }
  413. -
  414. - /**
  415. - * Writes the lockfile for a map
  416. - *
  417. - * @return Boolean Is Successful?
  418. - * @author Lars Michelsen <lars@vertical-visions.de>
  419. - */
  420. - function writeMapLock() {
  421. - if($this->checkMapLockWriteable(0)) {
  422. - // open file for writing and insert the needed informations
  423. - $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock','w');
  424. - fwrite($fp,time().':'.$this->CORE->getMainCfg()->getRuntimeValue('user').':'.$_SERVER['REMOTE_ADDR']);
  425. - fclose($fp);
  426. - return TRUE;
  427. - } else {
  428. - return FALSE;
  429. - }
  430. - }
  431. -
  432. - /**
  433. - * Deletes the lockfile for a map
  434. - *
  435. - * @return Boolean Is Successful?
  436. - * @author Lars Michelsen <lars@vertical-visions.de>
  437. - */
  438. - function deleteMapLock() {
  439. - if($this->checkMapLockWriteable(0)) {
  440. - if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  441. - // map lock deleted => OK
  442. - return TRUE;
  443. - } else {
  444. - return FALSE;
  445. - }
  446. - } else {
  447. - // no map lock to delete => OK
  448. - return TRUE;
  449. - }
  450. - }
  451. -
  452. - /**
  453. - * Checks for existing lockfile
  454. - *
  455. - * @param Boolean $printErr
  456. - * @return Boolean Is Successful?
  457. - * @author Lars Michelsen <lars@vertical-visions.de>
  458. - */
  459. - function checkMapLockExists($printErr) {
  460. - if($this->name != '') {
  461. - if(file_exists($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  462. - return TRUE;
  463. - } else {
  464. - if($printErr) {
  465. - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotExists','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
  466. - }
  467. - return FALSE;
  468. - }
  469. - } else {
  470. - return FALSE;
  471. - }
  472. - }
  473. -
  474. - /**
  475. - * Checks for readable lockfile
  476. - *
  477. - * @param Boolean $printErr
  478. - * @return Boolean Is Successful?
  479. - * @author Lars Michelsen <lars@vertical-visions.de>
  480. - */
  481. - function checkMapLockReadable($printErr) {
  482. - if($this->name != '') {
  483. - if($this->checkMapLockExists($printErr) && is_readable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  484. - return TRUE;
  485. - } else {
  486. - if($printErr) {
  487. - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotReadable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
  488. - }
  489. - return FALSE;
  490. - }
  491. - } else {
  492. - return FALSE;
  493. - }
  494. - }
  495. -
  496. - /**
  497. - * Checks for writeable lockfile
  498. - *
  499. - * @param Boolean $printErr
  500. - * @return Boolean Is Successful?
  501. - * @author Lars Michelsen <lars@vertical-visions.de>
  502. - */
  503. - function checkMapLockWriteable($printErr) {
  504. - if($this->name != '') {
  505. - if($this->checkMapLockExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  506. - return TRUE;
  507. - } else {
  508. - if($printErr) {
  509. - new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotWriteable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
  510. - }
  511. - return FALSE;
  512. - }
  513. - } else {
  514. - return FALSE;
  515. - }
  516. - }
  517. -
  518. - /**
  519. - * Parses WUI specific settings
  520. - *
  521. - * @return String JSON Code
  522. - * @author Lars Michelsen <lars@vertical-visions.de>
  523. - */
  524. - function parseViewProperties() {
  525. - $arr = Array();
  526. -
  527. - $arr['grid_show'] = intval($this->getValue('global', 0, 'grid_show'));
  528. - $arr['grid_color'] = $this->getValue('global', 0, 'grid_color');
  529. - $arr['grid_steps'] = intval($this->getValue('global', 0, 'grid_steps'));
  530. -
  531. - return json_encode($arr);
  532. - }
  533. -}
  534. -?>
  535. diff --git a/share/server/core/classes/CoreModMap.php b/share/server/core/classes/CoreModMap.php
  536. index 97f0463..2b2c3ce 100644
  537. --- a/share/server/core/classes/CoreModMap.php
  538. +++ b/share/server/core/classes/CoreModMap.php
  539. @@ -79,7 +79,12 @@ class CoreModMap extends CoreModule {
  540. if($aReturn !== false) {
  541. // Try to create the map
  542. if($this->doAdd($aReturn)) {
  543. - new GlobalMessage('NOTE', $this->CORE->getLang()->getText('The map has been created.'));
  544. + new GlobalMessage('NOTE',
  545. + $this->CORE->getLang()->getText('The map has been created.'),
  546. + null,
  547. + null,
  548. + 1,
  549. + $this->CORE->getMainCfg()->getValue('paths','htmlbase').'/frontend/wui/index.php?mod=Map&act=edit&show='.$aReturn['map_name']);
  550. $sReturn = '';
  551. } else {
  552. new GlobalMessage('ERROR', $this->CORE->getLang()->getText('The map could not be created.'));
  553. @@ -137,7 +142,7 @@ class CoreModMap extends CoreModule {
  554. // Return the data
  555. return Array(
  556. 'map_name' => $FHANDLER->get('map_name'),
  557. - 'allowed_users' => $FHANDLER->get('allowed_users'),
  558. + 'allowed_user' => $FHANDLER->get('allowed_users'),
  559. 'allowed_for_config' => $FHANDLER->get('allowed_for_config'),
  560. 'iconset' => $FHANDLER->get('map_iconset'),
  561. 'map_image' => $FHANDLER->get('map_image'));
  562. diff --git a/share/server/core/classes/GlobalMapCfg.php b/share/server/core/classes/GlobalMapCfg.php
  563. index aea8820..283d311 100644
  564. --- a/share/server/core/classes/GlobalMapCfg.php
  565. +++ b/share/server/core/classes/GlobalMapCfg.php
  566. @@ -1264,7 +1264,7 @@ class GlobalMapCfg {
  567. public function createMapConfig() {
  568. // does file exist?
  569. if(!$this->checkMapConfigReadable(FALSE)) {
  570. - if($this->CORE->getMainCfg()->checkMapCfgFolderWriteable(TRUE)) {
  571. + if($this->checkMapCfgFolderWriteable(TRUE)) {
  572. // create empty file
  573. $fp = fopen($this->configFile, 'w');
  574. fclose($fp);
  575. @@ -1703,6 +1703,24 @@ class GlobalMapCfg {
  576. }
  577.  
  578. /**
  579. + * Checks for writeable MapCfgFolder
  580. + *
  581. + * @param Boolean $printErr
  582. + * @return Boolean Is Successful?
  583. + * @author Lars Michelsen <lars@vertical-visions.de>
  584. + */
  585. + function checkMapCfgFolderWriteable($printErr) {
  586. + if(file_exists(substr($this->CORE->getMainCfg()->getValue('paths', 'mapcfg'),0,-1)) && is_writable(substr($this->CORE->getMainCfg()->getValue('paths', 'mapcfg'),0,-1))) {
  587. + return TRUE;
  588. + } else {
  589. + if($printErr == 1) {
  590. + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapCfgDirNotWriteable', Array('MAPPATH' => $this->CORE->getMainCfg()->getValue('paths', 'mapcfg'))), $this->CORE->getMainCfg()->getValue('paths','htmlbase'));
  591. + }
  592. + return FALSE;
  593. + }
  594. + }
  595. +
  596. + /**
  597. * Deletes an element of the specified type from the config array
  598. *
  599. * @param String $type
  600. diff --git a/share/server/core/classes/WuiMapCfg.php b/share/server/core/classes/WuiMapCfg.php
  601. new file mode 100644
  602. index 0000000..8b73b10
  603. --- /dev/null
  604. +++ b/share/server/core/classes/WuiMapCfg.php
  605. @@ -0,0 +1,466 @@
  606. +<?php
  607. +/*****************************************************************************
  608. + *
  609. + * WuiMapCfg.php - Class for handling the map configuration in WUI
  610. + *
  611. + * Copyright (c) 2004-2008 NagVis Project (Contact: lars@vertical-visions.de)
  612. + *
  613. + * License:
  614. + *
  615. + * This program is free software; you can redistribute it and/or modify
  616. + * it under the terms of the GNU General Public License version 2 as
  617. + * published by the Free Software Foundation.
  618. + *
  619. + * This program is distributed in the hope that it will be useful,
  620. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  621. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  622. + * GNU General Public License for more details.
  623. + *
  624. + * You should have received a copy of the GNU General Public License
  625. + * along with this program; if not, write to the Free Software
  626. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  627. + *
  628. + *****************************************************************************/
  629. +
  630. +/**
  631. + * @author Lars Michelsen <lars@vertical-visions.de>
  632. + */
  633. +class WuiMapCfg extends GlobalMapCfg {
  634. +
  635. + /**
  636. + * Class Constructor
  637. + *
  638. + * @param WuiCore $CORE
  639. + * @param String $name Name of the map
  640. + * @author Lars Michelsen <lars@vertical-visions.de>
  641. + */
  642. + function WuiMapCfg($CORE, $name='') {
  643. + $this->CORE = $CORE;
  644. +
  645. + $this->name = $name;
  646. +
  647. + $this->getMap();
  648. + parent::__construct($CORE, $this->name);
  649. + }
  650. +
  651. + /**
  652. + * Reads which map should be displayed, primary use
  653. + * the map defined in the url, if there is no map
  654. + * in url, use first entry of "maps" defined in
  655. + * the NagVis main config
  656. + *
  657. + * @author Lars Michelsen <lars@vertical-visions.de>
  658. + */
  659. + function getMap() {
  660. + // check the $this->name string for security reasons (its the ONLY value we get directly from external...)
  661. + // Allow ONLY Characters, Numbers, - and _ inside the Name of a Map
  662. + $this->name = preg_replace("/[^a-zA-Z0-9_-]/",'',$this->name);
  663. + }
  664. +
  665. + /**
  666. + * Gets all informations about an object type
  667. + *
  668. + * @param String Type to get the informations for
  669. + * @return Array The validConfig array
  670. + * @author Lars Michelsen <lars@vertical-visions.de>
  671. + */
  672. + function getValidObjectType($type) {
  673. + return $this->validConfig[$type];
  674. + }
  675. +
  676. + /**
  677. + * Gets the valid configuration array
  678. + *
  679. + * @return Array The validConfig array
  680. + * @author Lars Michelsen <lars@vertical-visions.de>
  681. + */
  682. + function getValidConfig() {
  683. + return $this->validConfig;
  684. + }
  685. +
  686. + /**
  687. + * Reads the configuration file of the map and
  688. + * sends it as download to the client.
  689. + *
  690. + * @return Boolean Only returns FALSE if something went wrong
  691. + * @author Lars Michelsen <lars@vertical-visions.de>
  692. + */
  693. + function exportMap() {
  694. + if($this->checkMapConfigReadable(1)) {
  695. + $mapPath = $this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->getName().'.cfg';
  696. +
  697. + header('Content-Type: application/octet-stream');
  698. + header('Content-Disposition: attachment; filename='.$this->getName().'.cfg');
  699. + header('Content-Length: '.filesize($mapPath));
  700. +
  701. + if(readfile($mapPath)) {
  702. + exit;
  703. + } else {
  704. + return FALSE;
  705. + }
  706. + } else {
  707. + return FALSE;
  708. + }
  709. + }
  710. +
  711. + /**
  712. + * Deletes the map configfile
  713. + *
  714. + * @return Boolean Is Successful?
  715. + * @author Lars Michelsen <lars@vertical-visions.de>
  716. + */
  717. + function deleteMapConfig($printErr=1) {
  718. + // is file writeable?
  719. + if($this->checkMapConfigWriteable($printErr)) {
  720. + if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
  721. + return TRUE;
  722. + } else {
  723. + if($printErr) {
  724. + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('couldNotDeleteMapCfg','MAPPATH~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
  725. + }
  726. + return FALSE;
  727. + }
  728. + } else {
  729. + return FALSE;
  730. + }
  731. + }
  732. +
  733. + /**
  734. + * Checks for writeable map config file
  735. + *
  736. + * @param Boolean $printErr
  737. + * @return Boolean Is Successful?
  738. + * @author Lars Michelsen <lars@vertical-visions.de>
  739. + */
  740. + function checkMapConfigWriteable($printErr) {
  741. + if($this->checkMapConfigExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg')) {
  742. + return TRUE;
  743. + } else {
  744. + if($printErr) {
  745. + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapCfgNotWriteable','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg'));
  746. + }
  747. + return FALSE;
  748. + }
  749. + }
  750. +
  751. + /**
  752. + * Writes the element from array to the config file
  753. + *
  754. + * @param String $type Type of the Element
  755. + * @param Integer $id Id of the Element
  756. + * @return Boolean Is Successful?
  757. + * @author Lars Michelsen <lars@vertical-visions.de>
  758. + */
  759. + function writeElement($type,$id) {
  760. + if($this->checkMapConfigExists(1) && $this->checkMapConfigReadable(1) && $this->checkMapConfigWriteable(1)) {
  761. + // read file in array
  762. + $file = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg');
  763. +
  764. + // number of lines in the file
  765. + $l = 0;
  766. + // number of elements of the given type
  767. + $a = 0;
  768. + // done?!
  769. + $done = FALSE;
  770. + while(isset($file[$l]) && $file[$l] != '' && $done == FALSE) {
  771. + // ignore comments
  772. + if(!preg_match('/^#/',$file[$l]) && !preg_match('/^;/',$file[$l])) {
  773. + $defineCln = explode('{', $file[$l]);
  774. + $define = explode(' ',$defineCln[0]);
  775. + // select only elements of the given type
  776. + if(isset($define[1]) && trim($define[1]) == $type) {
  777. + // check if element exists
  778. + if($a == $id) {
  779. + // check if element is an array...
  780. + if(is_array($this->mapConfig[$type][$a])) {
  781. + // ...array: update!
  782. +
  783. + // choose first parameter line
  784. + $l++;
  785. +
  786. + // Loop all parameters from array
  787. + foreach($this->mapConfig[$type][$id] AS $key => $val) {
  788. + // if key is not type
  789. + if($key != 'type' && $key != 'object_id') {
  790. + $cfgLines = 0;
  791. + $cfgLine = '';
  792. + $cfgLineNr = 0;
  793. +
  794. + // Loop parameters from file (Find line for this option)
  795. + while(isset($file[($l+$cfgLines)]) && trim($file[($l+$cfgLines)]) != '}') {
  796. + $entry = explode('=',$file[$l+$cfgLines], 2);
  797. + if($key == trim($entry[0])) {
  798. + $cfgLineNr = $l+$cfgLines;
  799. + if(is_array($val)) {
  800. + $val = implode(',',$val);
  801. + }
  802. + $cfgLine = $key.'='.$val."\n";
  803. + }
  804. + $cfgLines++;
  805. + }
  806. +
  807. + if($cfgLineNr != 0 && $val != '') {
  808. + // if a parameter was found in file and value is not empty, replace line
  809. + $file[$cfgLineNr] = $cfgLine;
  810. + } elseif($cfgLineNr != 0 && $val == '') {
  811. + // if a paremter is not in array or a value is empty, delete the line in the file
  812. + $file[$cfgLineNr] = '';
  813. + $cfgLines--;
  814. + } elseif($cfgLineNr == 0 && $val != '') {
  815. + // if a parameter is was not found in array and a value is not empty, create line
  816. + if(is_array($val)) {
  817. + $val = implode(',',$val);
  818. + }
  819. + $neu = $key.'='.$val."\n";
  820. +
  821. + for($i = $l; $i < count($file);$i++) {
  822. + $tmp = $file[$i];
  823. + $file[$i] = $neu;
  824. + $neu = $tmp;
  825. + }
  826. + $file[count($file)] = $neu;
  827. + } elseif($cfgLineNr == 0 && $val == '') {
  828. + // if a parameter is empty and a value is empty, do nothing
  829. + }
  830. + }
  831. + }
  832. + $l++;
  833. + } else {
  834. + // ...no array: delete!
  835. + $cfgLines = 0;
  836. + while(trim($file[($l+$cfgLines)]) != '}') {
  837. + $cfgLines++;
  838. + }
  839. + $cfgLines++;
  840. +
  841. + for($i = $l; $i <= $l+$cfgLines;$i++) {
  842. + unset($file[$i]);
  843. + }
  844. + }
  845. +
  846. + $done = TRUE;
  847. + }
  848. + $a++;
  849. + }
  850. + }
  851. + $l++;
  852. + }
  853. +
  854. + // reached end of file - couldn't find that element, create a new one...
  855. + if($done == FALSE) {
  856. + if(count($file) > 0 && $file[count($file)-1] != "\n") {
  857. + $file[] = "\n";
  858. + }
  859. + $file[] = 'define '.$type." {\n";
  860. + $aKeys = $this->getValidTypeKeys($type);
  861. + foreach($aKeys As $key) {
  862. + $val = $this->getValue($type, $id, $key, TRUE);
  863. + if(isset($val) && $val != '') {
  864. + $file[] = $key.'='.$val."\n";
  865. + }
  866. + }
  867. + $file[] = "}\n";
  868. + $file[] = "\n";
  869. + }
  870. +
  871. + // open file for writing and replace it
  872. + $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.cfg','w');
  873. + fwrite($fp,implode('',$file));
  874. + fclose($fp);
  875. + return TRUE;
  876. + } else {
  877. + return FALSE;
  878. + }
  879. + }
  880. +
  881. + /**
  882. + * Gets lockfile informations
  883. + *
  884. + * @param Boolean $ignoreLock
  885. + * @param Boolean $printErr
  886. + * @return Array/Boolean Is Successful?
  887. + * @author Lars Michelsen <lars@vertical-visions.de>
  888. + */
  889. + function checkMapLocked($ignoreLock=0,$printErr=1) {
  890. + // read lockfile
  891. + $lockdata = $this->readMapLock();
  892. + if(is_array($lockdata)) {
  893. + // check if the lock is older than 5 Minutes and don't ignore lock
  894. + if($lockdata['time'] > time() - $this->CORE->getMainCfg()->getValue('wui','maplocktime') * 60) {
  895. + if($ignoreLock == 0) {
  896. + // the lock should be ignored
  897. + return FALSE;
  898. + } else {
  899. + // there is a lock and it should be recognized
  900. + // check if this is the lock of the current user (Happens e.g. by pressing F5)
  901. + if($this->CORE->getMainCfg()->getRuntimeValue('user') == $lockdata['user'] && $_SERVER['REMOTE_ADDR'] == $lockdata['ip']) {
  902. + // refresh the lock (write a new lock)
  903. + $this->writeMapLock();
  904. + // it's locked by the current user, so it's not locked for him
  905. + return FALSE;
  906. + }
  907. + if($printErr == 1) {
  908. + $LANG = new GlobalLanguage($this->CORE->getMainCfg(),'nagvis');
  909. +
  910. + // message the user that there is a lock by another user, the user can decide wether he want's to override it or not
  911. + 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>';
  912. + }
  913. + return TRUE;
  914. + }
  915. + } else {
  916. + // delete lockfile & continue
  917. + // try to delete map lock, if nothing to delete its OK
  918. + $this->deleteMapLock();
  919. + return FALSE;
  920. + }
  921. + } else {
  922. + // no valid informations in lock or no lock there
  923. + // try to delete map lock, if nothing to delete its OK
  924. + $this->deleteMapLock();
  925. + return FALSE;
  926. + }
  927. + }
  928. +
  929. + /**
  930. + * Reads the contents of the lockfile
  931. + *
  932. + * @return Array/Boolean Is Successful?
  933. + * @author Lars Michelsen <lars@vertical-visions.de>
  934. + */
  935. + function readMapLock() {
  936. + if($this->checkMapLockReadable(0)) {
  937. + $fileContent = file($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock');
  938. + // only recognize the first line, explode it by :
  939. + $arrContent = explode(':',$fileContent[0]);
  940. + // if there are more elements in the array it is OK
  941. + if(count($arrContent) > 0) {
  942. + return Array('time' => $arrContent[0], 'user' => $arrContent[1], 'ip' => $arrContent[2]);
  943. + } else {
  944. + return FALSE;
  945. + }
  946. + } else {
  947. + return FALSE;
  948. + }
  949. + }
  950. +
  951. + /**
  952. + * Writes the lockfile for a map
  953. + *
  954. + * @return Boolean Is Successful?
  955. + * @author Lars Michelsen <lars@vertical-visions.de>
  956. + */
  957. + function writeMapLock() {
  958. + if($this->checkMapLockWriteable(0)) {
  959. + // open file for writing and insert the needed informations
  960. + $fp = fopen($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock','w');
  961. + fwrite($fp,time().':'.$this->CORE->getMainCfg()->getRuntimeValue('user').':'.$_SERVER['REMOTE_ADDR']);
  962. + fclose($fp);
  963. + return TRUE;
  964. + } else {
  965. + return FALSE;
  966. + }
  967. + }
  968. +
  969. + /**
  970. + * Deletes the lockfile for a map
  971. + *
  972. + * @return Boolean Is Successful?
  973. + * @author Lars Michelsen <lars@vertical-visions.de>
  974. + */
  975. + function deleteMapLock() {
  976. + if($this->checkMapLockWriteable(0)) {
  977. + if(unlink($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  978. + // map lock deleted => OK
  979. + return TRUE;
  980. + } else {
  981. + return FALSE;
  982. + }
  983. + } else {
  984. + // no map lock to delete => OK
  985. + return TRUE;
  986. + }
  987. + }
  988. +
  989. + /**
  990. + * Checks for existing lockfile
  991. + *
  992. + * @param Boolean $printErr
  993. + * @return Boolean Is Successful?
  994. + * @author Lars Michelsen <lars@vertical-visions.de>
  995. + */
  996. + function checkMapLockExists($printErr) {
  997. + if($this->name != '') {
  998. + if(file_exists($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  999. + return TRUE;
  1000. + } else {
  1001. + if($printErr) {
  1002. + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotExists','MAP~'.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
  1003. + }
  1004. + return FALSE;
  1005. + }
  1006. + } else {
  1007. + return FALSE;
  1008. + }
  1009. + }
  1010. +
  1011. + /**
  1012. + * Checks for readable lockfile
  1013. + *
  1014. + * @param Boolean $printErr
  1015. + * @return Boolean Is Successful?
  1016. + * @author Lars Michelsen <lars@vertical-visions.de>
  1017. + */
  1018. + function checkMapLockReadable($printErr) {
  1019. + if($this->name != '') {
  1020. + if($this->checkMapLockExists($printErr) && is_readable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  1021. + return TRUE;
  1022. + } else {
  1023. + if($printErr) {
  1024. + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotReadable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
  1025. + }
  1026. + return FALSE;
  1027. + }
  1028. + } else {
  1029. + return FALSE;
  1030. + }
  1031. + }
  1032. +
  1033. + /**
  1034. + * Checks for writeable lockfile
  1035. + *
  1036. + * @param Boolean $printErr
  1037. + * @return Boolean Is Successful?
  1038. + * @author Lars Michelsen <lars@vertical-visions.de>
  1039. + */
  1040. + function checkMapLockWriteable($printErr) {
  1041. + if($this->name != '') {
  1042. + if($this->checkMapLockExists($printErr) && is_writeable($this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock')) {
  1043. + return TRUE;
  1044. + } else {
  1045. + if($printErr) {
  1046. + new GlobalMessage('ERROR', $this->CORE->getLang()->getText('mapLockNotWriteable','MAP='.$this->CORE->getMainCfg()->getValue('paths', 'mapcfg').$this->name.'.lock'));
  1047. + }
  1048. + return FALSE;
  1049. + }
  1050. + } else {
  1051. + return FALSE;
  1052. + }
  1053. + }
  1054. +
  1055. + /**
  1056. + * Parses WUI specific settings
  1057. + *
  1058. + * @return String JSON Code
  1059. + * @author Lars Michelsen <lars@vertical-visions.de>
  1060. + */
  1061. + function parseViewProperties() {
  1062. + $arr = Array();
  1063. +
  1064. + $arr['grid_show'] = intval($this->getValue('global', 0, 'grid_show'));
  1065. + $arr['grid_color'] = $this->getValue('global', 0, 'grid_color');
  1066. + $arr['grid_steps'] = intval($this->getValue('global', 0, 'grid_steps'));
  1067. +
  1068. + return json_encode($arr);
  1069. + }
  1070. +}
  1071. +?>
  1072. --
  1073. 1.7.0.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement