Subversion Repositories ALCASAR

Rev

Rev 2775 | Rev 3037 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2775 rexy 1
<?php
2
/**
3
 * XML Generator class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_XML
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   SVN: $Id: class.WebpageXML.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * class for xml output
17
 *
18
 * @category  PHP
19
 * @package   PSI_XML
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
21
 * @copyright 2009 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
26
class WebpageXML extends Output implements PSI_Interface_Output
27
{
28
    /**
29
     * xml object that holds the generated xml
30
     *
31
     * @var XML
32
     */
33
    private $_xml;
34
 
35
    /**
36
     * complete xml
37
     *
38
     * @var boolean
39
     */
40
    private $_completeXML = false;
41
 
42
    /**
43
     * name of the plugin
44
     *
45
     * @var string
46
     */
47
    private $_pluginName = null;
48
 
49
    /**
50
     * name of the block
51
     *
52
     * @var string
53
     */
54
    private $_blockName = null;
55
 
56
    /**
57
     * generate the output
58
     *
59
     * @return void
60
     */
61
    private function _prepare()
62
    {
63
        if ($this->_pluginName === null) {
2976 rexy 64
            if (((PSI_OS == 'WINNT') || (PSI_OS == 'Linux')) && defined('PSI_WMI_HOSTNAME')) {
65
                define('PSI_EMU_HOSTNAME', PSI_WMI_HOSTNAME);
66
                if (defined('PSI_WMI_USER') && defined('PSI_WMI_PASSWORD')) {
67
                    define('PSI_EMU_USER', PSI_WMI_USER);
68
                    define('PSI_EMU_PASSWORD', PSI_WMI_PASSWORD);
69
                } else {
70
                    define('PSI_EMU_USER', null);
71
                    define('PSI_EMU_PASSWORD', null);
72
                }
73
                if (!file_exists(PSI_APP_ROOT.'/includes/os/class.WINNT.inc.php')) {
74
                    $this->error->addError("file_exists(class.WINNT.inc.php)", "WINNT is not currently supported");
75
                }
76
            } else {
77
                // Figure out which OS we are running on, and detect support
78
                if (!file_exists(PSI_APP_ROOT.'/includes/os/class.'.PSI_OS.'.inc.php')) {
79
                    $this->error->addError("file_exists(class.".PSI_OS.".inc.php)", PSI_OS." is not currently supported");
80
                }
2775 rexy 81
            }
82
 
83
            if (!defined('PSI_MBINFO') && (!$this->_blockName || in_array($this->_blockName, array('voltage','current','temperature','fans','power','other')))) {
84
                // check if there is a valid sensor configuration in phpsysinfo.ini
85
                $foundsp = array();
86
                if (defined('PSI_SENSOR_PROGRAM') && is_string(PSI_SENSOR_PROGRAM)) {
87
                    if (preg_match(ARRAY_EXP, PSI_SENSOR_PROGRAM)) {
88
                        $sensorprograms = eval(strtolower(PSI_SENSOR_PROGRAM));
89
                    } else {
90
                        $sensorprograms = array(strtolower(PSI_SENSOR_PROGRAM));
91
                    }
92
                    foreach ($sensorprograms as $sensorprogram) {
93
                        if (!file_exists(PSI_APP_ROOT.'/includes/mb/class.'.$sensorprogram.'.inc.php')) {
94
                            $this->error->addError("file_exists(class.".htmlspecialchars($sensorprogram).".inc.php)", "specified sensor program is not supported");
95
                        } else {
96
                            $foundsp[] = $sensorprogram;
97
                        }
98
                    }
99
                }
100
 
101
                /**
102
                 * motherboard information
103
                 *
104
                 * @var string serialized array
105
                 */
106
                define('PSI_MBINFO', serialize($foundsp));
107
            }
108
 
109
            if (!defined('PSI_UPSINFO') && (!$this->_blockName || ($this->_blockName==='ups'))) {
110
                // check if there is a valid ups configuration in phpsysinfo.ini
111
                $foundup = array();
112
                if (defined('PSI_UPS_PROGRAM') && is_string(PSI_UPS_PROGRAM)) {
113
                    if (preg_match(ARRAY_EXP, PSI_UPS_PROGRAM)) {
114
                        $upsprograms = eval(strtolower(PSI_UPS_PROGRAM));
115
                    } else {
116
                        $upsprograms = array(strtolower(PSI_UPS_PROGRAM));
117
                    }
118
                    foreach ($upsprograms as $upsprogram) {
119
                        if (!file_exists(PSI_APP_ROOT.'/includes/ups/class.'.$upsprogram.'.inc.php')) {
120
                            $this->error->addError("file_exists(class.".htmlspecialchars($upsprogram).".inc.php)", "specified UPS program is not supported");
121
                        } else {
122
                            $foundup[] = $upsprogram;
123
                        }
124
                    }
125
                }
126
                /**
127
                 * ups information
128
                 *
129
                 * @var string serialized array
130
                 */
131
                define('PSI_UPSINFO', serialize($foundup));
132
            }
133
 
134
            // if there are errors stop executing the script until they are fixed
135
            if ($this->error->errorsExist()) {
136
                $this->error->errorsAsXML();
137
            }
138
 
139
            // Create the XML
140
            $this->_xml = new XML($this->_completeXML, '', $this->_blockName);
141
        } else {
2976 rexy 142
            if ((PSI_OS == 'WINNT') || (PSI_OS == 'Linux')) {
143
                $plugname = strtoupper(trim($this->_pluginName));
144
                if (defined('PSI_PLUGIN_'.$plugname.'_WMI_HOSTNAME')) {
145
                    define('PSI_EMU_HOSTNAME', constant('PSI_PLUGIN_'.$plugname.'_WMI_HOSTNAME'));
146
                    if (defined('PSI_PLUGIN_'.$plugname.'_WMI_USER') && defined('PSI_PLUGIN_'.$plugname.'_WMI_PASSWORD')) {
147
                        define('PSI_EMU_USER', constant('PSI_PLUGIN_'.$plugname.'_WMI_USER'));
148
                        define('PSI_EMU_PASSWORD', constant('PSI_PLUGIN_'.$plugname.'_WMI_PASSWORD'));
149
                    } else {
150
                        define('PSI_EMU_USER', null);
151
                        define('PSI_EMU_PASSWORD', null);
152
                    }
153
                } elseif (defined('PSI_WMI_HOSTNAME')) {
154
                    define('PSI_EMU_HOSTNAME', PSI_WMI_HOSTNAME);
155
                    if (defined('PSI_WMI_USER') && defined('PSI_WMI_PASSWORD')) {
156
                        define('PSI_EMU_USER', PSI_WMI_USER);
157
                        define('PSI_EMU_PASSWORD', PSI_WMI_PASSWORD);
158
                    } else {
159
                        define('PSI_EMU_USER', null);
160
                        define('PSI_EMU_PASSWORD', null);
161
                    }
162
                }
163
            }
164
 
2775 rexy 165
            // Create the XML
166
            $this->_xml = new XML(false, $this->_pluginName);
167
        }
168
    }
169
 
170
    /**
171
     * render the output
172
     *
173
     * @return void
174
     */
175
    public function run()
176
    {
177
        header("Cache-Control: no-cache, must-revalidate\n");
178
        header("Content-Type: text/xml\n\n");
179
        $xml = $this->_xml->getXml();
180
        echo $xml->asXML();
181
    }
182
 
183
    /**
184
     * get XML as pure string
185
     *
186
     * @return string
187
     */
188
    public function getXMLString()
189
    {
190
        $xml = $this->_xml->getXml();
191
 
192
        return $xml->asXML();
193
    }
194
 
195
    /**
196
     * get json string
197
     *
198
     * @return string
199
     */
200
    public function getJsonString()
201
    {
202
        if (defined('PSI_JSON_ISSUE') && (PSI_JSON_ISSUE)) {
203
            return json_encode(simplexml_load_string(str_replace(">", ">\n", $this->getXMLString()))); // solving json_encode issue
204
        } else {
205
            return json_encode(simplexml_load_string($this->getXMLString()));
206
        }
207
    }
208
 
209
    /**
210
     * get array
211
     *
212
     * @return array
213
     */
214
    public function getArray()
215
    {
216
        return json_decode($this->getJsonString());
217
    }
218
 
219
    /**
220
     * set parameters for the XML generation process
221
     *
222
     * @param string $plugin name of the plugin, block or 'complete' for all plugins
223
     *
224
     * @return void
225
     */
226
    public function __construct($plugin = "")
227
    {
228
        parent::__construct();
229
 
230
        if (is_string($plugin) && ($plugin !== "")) {
231
            $plugin = strtolower($plugin);
232
            if ($plugin === "complete") {
233
                $this->_completeXML = true;
234
            } else {
235
                $validblocks = array('vitals','hardware','memory','filesystem','network','voltage','current','temperature','fans','power','other','ups');
236
                if (in_array($plugin, $validblocks)) {
237
                    $this->_blockName = $plugin;
238
                } elseif (in_array($plugin, CommonFunctions::getPlugins())) {
239
                    $this->_pluginName = $plugin;
240
                } else {
241
                    $this->_blockName = ' '; //disable all blocks
242
                }
243
            }
244
        }
245
        $this->_prepare();
246
    }
247
}