aboutsummaryrefslogtreecommitdiff
path: root/Wrt3g/Config.php
blob: fc058edb4940bd551adb17bfbf2653db0c9f3fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
 * Part of Linksys WRT3G tools
 *
 * PHP version 5
 *
 * @category Tools
 * @package  linksys-wrt3g-tools
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL v3
 * @link     http://cweiske.de/linksys-wrt3g-tools.htm
 */
require_once 'Config.php';
require_once 'System/Folders.php';

/**
 * Loads and saves the router configuration data from a config file
 *
 * @category Tools
 * @package  linksys-wrt3g-tools
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  AGPL v3
 * @link     http://cweiske.de/linksys-wrt3g-tools.htm
 */
class Wrt3g_Config
{
    /**
     * Router hostname/IP
     *
     * @var string
     */
    public $host;

    /**
     * Name of user with administration privileges
     *
     * @var string
     */
    public $user;

    /**
     * Password for $user
     *
     * @var string
     */
    public $password;

    /**
     * Object with a "log" method.
     *
     * @var Wrt3g
     */
    public $logger;



    /**
     * Create new instance
     *
     * @param object $logger Object with log() method
     */
    public function __construct($logger)
    {
        $this->logger = $logger;
    }


    /**
     * Loads the router data from config file and takes the $options
     * array into account when set, overwriting the config file values.
     *
     * Config values are loaded into class variables
     *
     * @param array $options Array of commandline options. Values used:
     *                       - host - Hostname/IP
     *                       - user - Username to log in with
     *                       - password - Password for user
     *
     * @return void
     */
    public function load($options = array())
    {
        //system config file
        $this->loadFromFile('/etc/linksys-wrt3g-tools');

        //user config file
        $file = $this->getConfigFilePath();
        if ($file) {
            $this->loadFromFile($file);
        }
        //environment variables: we use lowercase options, which should
        // not collide with the uppercase env variables like USER
        $this->loadOptions($_SERVER);
        //commandline options
        $this->loadOptions($options);

        $this->logger->log(
            sprintf(
                "Configuration: %s:%s@%s",
                $this->user, $this->password, $this->host
            ),
            2
        );

        $this->verify();

        if (isset($options['save'])) {
            $this->save($file);
        }
    }



    /**
     * Loads the options array from command line into the class
     * variables.
     *
     * @param array $options Array of commandline options. Values used:
     *                       - host - Hostname/IP
     *                       - user - Username to log in with
     *                       - password - Password for user
     *
     * @return void
     */
    protected function loadOptions($options)
    {
        foreach (array('host', 'user', 'password') as $variable) {
            if (isset($options[$variable])) {
                $this->$variable = $options[$variable];
            }
        }
    }



    /**
     * Loads configuration from config file ("ini" style).
     * The file does not need to exist. Config values are stored
     * in the public class variables.
     *
     * @param string $file Config file path to load
     *
     * @return void
     *
     * @throws Exception When something went wrong
     */
    protected function loadFromFile($file)
    {
        $this->logger->log('Trying to load config file ' . $file, 2);
        $cfg = new Config();
        $root = $cfg->parseConfig($file, 'inifile');

        if (PEAR::isError($root)) {
            $this->logger->log(
                'Error while reading configuration: ' . $root->getMessage(),
                1
            );
            return;
        }
        $settings = $root->toArray();
        $this->loadOptions($settings['root']);
    }



    /**
     * Returns the full path to the configuration file, if it exists
     * or not.
     *
     * @return string Config file path or NULL if not determinable
     */
    public function getConfigFilePath()
    {
        $sf = new System_Folders();
        $home = $sf->getHome();
        if ($home === null) {
            return null;
        }
        //works on linux only :)
        return $home . '.config/linksys-wrt3g-tools';
    }



    /**
     * Saves the config file
     *
     * @param string $file Config file path to save into
     *
     * @return void
     */
    public function save($file)
    {
        $cfg = new Config();
        $cfg->parseConfig($file, 'inifile');
        //ignore read errors

        $root = $cfg->getRoot();
        $root->setDirective('host', $this->host);
        $root->setDirective('user', $this->user);
        $root->setDirective('password', $this->password);
        
        $this->logger->log('Saving config into file ' . $file, 2);
        $cfg->writeConfig($file, 'inifile');
    }



    /**
     * Verifies that all required options are set
     *
     * @return void
     *
     * @throw Exception When something is missing
     */
    protected function verify()
    {
        if ($this->host == '') {
            throw new Exception('Hostname missing');
        }
    }
}

?>