check that the hostname is configured
[linksys-wrt3g-tools.git] / Wrt3g / Config.php
1 <?php
2 /**
3  * Part of Linksys WRT3G tools
4  *
5  * PHP version 5
6  *
7  * @category Tools
8  * @package  linksys-wrt3g-tools
9  * @author   Christian Weiske <cweiske@cweiske.de>
10  * @license  AGPL v3
11  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
12  */
13 require_once 'Config.php';
14 require_once 'System/Folders.php';
15
16 /**
17  * Loads and saves the router configuration data from a config file
18  *
19  * @category Tools
20  * @package  linksys-wrt3g-tools
21  * @author   Christian Weiske <cweiske@cweiske.de>
22  * @license  AGPL v3
23  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
24  */
25 class Wrt3g_Config
26 {
27     /**
28      * Router hostname/IP
29      *
30      * @var string
31      */
32     public $host;
33
34     /**
35      * Name of user with administration privileges
36      *
37      * @var string
38      */
39     public $user;
40
41     /**
42      * Password for $user
43      *
44      * @var string
45      */
46     public $password;
47
48     /**
49      * Object with a "log" method.
50      *
51      * @var Wrt3g
52      */
53     public $logger;
54
55
56
57     /**
58      * Create new instance
59      *
60      * @param object $logger Object with log() method
61      */
62     public function __construct($logger)
63     {
64         $this->logger = $logger;
65     }
66
67
68     /**
69      * Loads the router data from config file and takes the $options
70      * array into account when set, overwriting the config file values.
71      *
72      * Config values are loaded into class variables
73      *
74      * @param array $options Array of commandline options. Values used:
75      *                       - host - Hostname/IP
76      *                       - user - Username to log in with
77      *                       - password - Password for user
78      *
79      * @return void
80      */
81     public function load($options = array())
82     {
83         //system config file
84         $this->loadFromFile('/etc/linksys-wrt3g-tools');
85
86         //user config file
87         $file = $this->getConfigFilePath();
88         if ($file) {
89             $this->loadFromFile($file);
90         }
91         //environment variables: we use lowercase options, which should
92         // not collide with the uppercase env variables like USER
93         $this->loadOptions($_SERVER);
94         //commandline options
95         $this->loadOptions($options);
96
97         $this->logger->log(
98             sprintf(
99                 "Configuration: %s:%s@%s",
100                 $this->user, $this->password, $this->host
101             ),
102             2
103         );
104
105         $this->verify();
106
107         if (isset($options['save'])) {
108             $this->save($file);
109         }
110     }
111
112
113
114     /**
115      * Loads the options array from command line into the class
116      * variables.
117      *
118      * @param array $options Array of commandline options. Values used:
119      *                       - host - Hostname/IP
120      *                       - user - Username to log in with
121      *                       - password - Password for user
122      *
123      * @return void
124      */
125     protected function loadOptions($options)
126     {
127         foreach (array('host', 'user', 'password') as $variable) {
128             if (isset($options[$variable])) {
129                 $this->$variable = $options[$variable];
130             }
131         }
132     }
133
134
135
136     /**
137      * Loads configuration from config file ("ini" style).
138      * The file does not need to exist. Config values are stored
139      * in the public class variables.
140      *
141      * @param string $file Config file path to load
142      *
143      * @return void
144      *
145      * @throws Exception When something went wrong
146      */
147     protected function loadFromFile($file)
148     {
149         $this->logger->log('Trying to load config file ' . $file, 2);
150         $cfg = new Config();
151         $root = $cfg->parseConfig($file, 'inifile');
152
153         if (PEAR::isError($root)) {
154             $this->logger->log(
155                 'Error while reading configuration: ' . $root->getMessage(),
156                 1
157             );
158             return;
159         }
160         $settings = $root->toArray();
161         $this->loadOptions($settings['root']);
162     }
163
164
165
166     /**
167      * Returns the full path to the configuration file, if it exists
168      * or not.
169      *
170      * @return string Config file path or NULL if not determinable
171      */
172     public function getConfigFilePath()
173     {
174         $sf = new System_Folders();
175         $home = $sf->getHome();
176         if ($home === null) {
177             return null;
178         }
179         //works on linux only :)
180         return $home . '.config/linksys-wrt3g-tools';
181     }
182
183
184
185     /**
186      * Saves the config file
187      *
188      * @param string $file Config file path to save into
189      *
190      * @return void
191      */
192     public function save($file)
193     {
194         $cfg = new Config();
195         $cfg->parseConfig($file, 'inifile');
196         //ignore read errors
197
198         $root = $cfg->getRoot();
199         $root->setDirective('host', $this->host);
200         $root->setDirective('user', $this->user);
201         $root->setDirective('password', $this->password);
202         
203         $this->logger->log('Saving config into file ' . $file, 2);
204         $cfg->writeConfig($file, 'inifile');
205     }
206
207
208
209     /**
210      * Verifies that all required options are set
211      *
212      * @return void
213      *
214      * @throw Exception When something is missing
215      */
216     protected function verify()
217     {
218         if ($this->host == '') {
219             throw new Exception('Hostname missing');
220         }
221     }
222 }
223
224 ?>