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