load system config file; document config options
[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         $this->loadOptions($options);
92
93         //commandline options
94         if (isset($options['save'])) {
95             $this->save($file);
96         }
97     }
98
99
100
101     /**
102      * Loads the options array from command line into the class
103      * variables.
104      *
105      * @param array $options Array of commandline options. Values used:
106      *                       - host - Hostname/IP
107      *                       - user - Username to log in with
108      *                       - password - Password for user
109      *
110      * @return void
111      */
112     protected function loadOptions($options)
113     {
114         foreach (array('host', 'user', 'password') as $variable) {
115             if (isset($options[$variable])) {
116                 $this->$variable = $options[$variable];
117             }
118         }
119     }
120
121
122
123     /**
124      * Loads configuration from config file ("ini" style).
125      * The file does not need to exist. Config values are stored
126      * in the public class variables.
127      *
128      * @param string $file Config file path to load
129      *
130      * @return void
131      *
132      * @throws Exception When something went wrong
133      */
134     protected function loadFromFile($file)
135     {
136         $this->logger->log('Trying to load config file ' . $file, 2);
137         $cfg = new Config();
138         $root = $cfg->parseConfig($file, 'inifile');
139
140         if (PEAR::isError($root)) {
141             $this->logger->log(
142                 'Error while reading configuration: ' . $root->getMessage(),
143                 1
144             );
145             return;
146         }
147         $settings = $root->toArray();
148         $this->loadOptions($settings['root']);
149     }
150
151
152
153     /**
154      * Returns the full path to the configuration file, if it exists
155      * or not.
156      *
157      * @return string Config file path or NULL if not determinable
158      */
159     public function getConfigFilePath()
160     {
161         $sf = new System_Folders();
162         $home = $sf->getHome();
163         if ($home === null) {
164             return null;
165         }
166         //works on linux only :)
167         return $home . '.config/linksys-wrt3g-tools';
168     }
169
170
171
172     /**
173      * Saves the config file
174      *
175      * @param string $file Config file path to save into
176      *
177      * @return void
178      */
179     public function save($file)
180     {
181         $cfg = new Config();
182         $cfg->parseConfig($file, 'inifile');
183         //ignore read errors
184
185         $root = $cfg->getRoot();
186         $root->setDirective('host', $this->host);
187         $root->setDirective('user', $this->user);
188         $root->setDirective('password', $this->password);
189         
190         $this->logger->log('Saving config into file ' . $file, 2);
191         $cfg->writeConfig($file, 'inifile');
192     }
193 }
194
195 ?>