Passed
Push — master ( 6ce43c...1031ec )
by Patrick
01:53
created

TrapConfig::getDBConfigIfSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Trapdirector;
4
5
6
/**
7
 * Read configuration and options
8
 *
9
 * @license GPL
10
 * @author Patrick Proy
11
 * @package trapdirector
12
 * @subpackage Processing
13
 *
14
 */
15
trait TrapConfig
16
{
17
18
    /** @return \Trapdirector\Logging   */
19
    abstract public function getLogging();
20
    
21
    /**
22
     * Get option from array of ini file, send message if empty
23
     * @param string $option_array Array of ini file
24
     * @param string $option_category category in ini file
25
     * @param string $option_name name of option in category
26
     * @param mixed $option_var variable to fill if found, left untouched if not found
27
     * @param integer $log_level default 2 (warning)
28
     * @param string $message warning message if not found
29
     * @return boolean true if found, or false
30
     */
31
    protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null)
32
    {
33
        if (!isset($option_array[$option_category][$option_name]))
34
        {
35
            if ($message === null)
36
            {
37
                $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig;
38
            }
39
            $this->getLogging()->log($message,$log_level);
40
            return false;
41
        }
42
        else
43
        {
44
            $option_var=$option_array[$option_category][$option_name];
45
            return true;
46
        }
47
    }
48
49
    /**
50
     * Get options in database
51
     */
52
    protected function getDatabaseOptions()
53
    {
54
        // Database options
55
        if ($this->logSetup === false) // Only if logging was no setup in constructor
56
        {
57
            $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel);
58
            $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode);
59
            $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile);
60
        }
61
    }
62
        
63
    /** Set $variable to value if $element found in database config table
64
     * @param string $element
65
     * @param string $variable
66
     */
67
    protected function getDBConfigIfSet($element,&$variable)
68
    {
69
        $value=$this->getDBConfig($element);
70
        if ($value != null) $variable=$value;
71
    }
72
    
73
    /**
74
     *   Get data from db_config
75
     *	@param $element string name of param
76
     *	@return mixed : value (or null)
77
     */
78
    protected function getDBConfig($element)  // TODO : put this in DB class
79
    {
80
        $db_conn=$this->trapsDB->db_connect_trap();
81
        $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )';
82
        if (($ret_code=$db_conn->query($sql)) === false) {
83
            $this->logging->log('No result in query : ' . $sql,WARN,'');
84
            return null;
85
        }
86
        $value=$ret_code->fetch();
87
        if ($value != null && isset($value['value']))
88
        {
89
            return $value['value'];
90
        }
91
        return null;
92
    }
93
    
94
    /**
95
     * Get options from ini file
96
     * @param array $trap_config : ini file array
97
     */
98
    protected function getMainOptions($trapConfig)
99
    {
100
        
101
        // Snmptranslate binary path
102
        $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate);
103
        
104
        // mibs path
105
        $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs);
106
        
107
        // icinga2cmd path
108
        $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd);
109
        
110
        // table prefix
111
        $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix);
112
        
113
        // API options
114
        if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname))
115
        {
116
            $this->apiUse=true;
0 ignored issues
show
Bug Best Practice introduced by
The property apiUse does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
            $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort);
118
            $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername);
0 ignored issues
show
Bug introduced by
The property apiUsername does not exist on Trapdirector\TrapConfig. Did you mean apiUse?
Loading history...
119
            $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword);
120
        }
121
    }
122
    
123
    /**
124
     * Create and setup database class for trap & ido (if no api) db
125
     * @param array $trap_config : ini file array
126
     */
127
    protected function setupDatabase($trapConfig)
128
    {
129
        // Trap database
130
        if (!array_key_exists('database',$trapConfig['config']))
131
        {
132
            $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,'');
133
            return;
134
        }
135
        $dbTrapName=$trapConfig['config']['database'];
136
        $this->logging->log("Found database in config file: ".$dbTrapName,INFO );
137
        
138
        if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false)
139
        {
140
            $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,'');
141
            return;
142
        }
143
        if (!array_key_exists($dbTrapName,$dbConfig))
144
        {
145
            $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,'');
146
            return;
147
        }
148
        
149
        $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix);
0 ignored issues
show
Bug Best Practice introduced by
The property trapsDB does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
150
        
151
        if ($this->apiUse === true) return; // In case of API use, no IDO is necessary
152
        
153
        // IDO Database
154
        if (!array_key_exists('IDOdatabase',$trapConfig['config']))
155
        {
156
            $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,'');
157
        }
158
        $dbIdoName=$trapConfig['config']['IDOdatabase'];
159
        
160
        $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO );
161
        if (!array_key_exists($dbIdoName,$dbConfig))
162
        {
163
            $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,'');
164
            return;
165
        }
166
        
167
        $this->trapsDB->setupIDO($dbConfig[$dbIdoName]);
168
    }
169
    
170
}