1<?php 2/***************************************************************************** 3 * 4 * CoreUserCfg.php - Class for handling user/profile specific configurations 5 * 6 * Copyright (c) 2004-2016 NagVis Project (Contact: info@nagvis.org) 7 * 8 * License: 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * 23 *****************************************************************************/ 24 25/** 26 * @author Lars Michelsen <lm@larsmichelsen.com> 27 */ 28class CoreUserCfg { 29 private $profilesDir; 30 31 // Optional list of value types to be fixed 32 private $types = Array( 33 'sidebar' => 'i', 34 'header' => 'b', 35 'eventlog' => 'b', 36 ); 37 38 public function __construct() { 39 $this->profilesDir = cfg('paths', 'profiles'); 40 } 41 42 public function doGet($onlyUserCfg = false) { 43 global $AUTH, $AUTHORISATION; 44 $opts = Array(); 45 if(!isset($AUTH) || !$AUTH->isAuthenticated()) 46 return $opts; 47 48 if(!file_exists($this->profilesDir)) 49 return $opts; 50 51 // Fetch all profile files to load 52 $files = Array(); 53 if(!$onlyUserCfg && isset($AUTHORISATION)) 54 foreach($AUTHORISATION->getUserRoles($AUTH->getUserId()) AS $role) 55 $files[] = $role['name'].'.profile'; 56 $files[] = $AUTH->getUser().'.profile'; 57 58 // Read all configurations and append to the option array 59 foreach($files AS $file) { 60 $f = $this->profilesDir.'/'.$file; 61 if(!file_exists($f)) 62 continue; 63 64 $a = json_decode(file_get_contents($f), true); 65 if(!is_array($a)) 66 throw new NagVisException(l('Invalid data in "[FILE]".', Array('FILE' => $f))); 67 68 $opts = array_merge($opts, $a); 69 } 70 71 return $opts; 72 } 73 74 public function doGetAsJson($onlyUserCfg = false) { 75 return json_encode($this->doGet($onlyUserCfg)); 76 } 77 78 public function doSet($opts) { 79 global $CORE, $AUTH; 80 $file = $this->profilesDir.'/'.$AUTH->getUser().'.profile'; 81 82 if(!$CORE->checkExisting(dirname($file), true) || !$CORE->checkWriteable(dirname($file), true)) 83 return false; 84 85 $cfg = $this->doGet(true); 86 87 foreach($opts AS $key => $value) { 88 if(isset($this->types[$key])) 89 $value = $this->fixType($value, $this->types[$key]); 90 $cfg[$key] = $value; 91 } 92 93 $ret = file_put_contents($file, json_encode($cfg)) !== false; 94 $CORE->setPerms($file); 95 return $ret; 96 } 97 98 public function getValue($key, $default = null) { 99 $opts = $this->doGet(); 100 return isset($opts[$key]) ? $opts[$key] : $default; 101 } 102 103 private function fixType($val, $type) { 104 if($type == 'i') 105 return (int) $val; 106 elseif($type == 'b') { 107 if($val == '1' || $val === 'true') 108 return true; 109 else 110 return false; 111 } else 112 return $val; 113 } 114} 115