Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | Details | 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) {
64
            // Figure out which OS we are running on, and detect support
65
            if (!file_exists(PSI_APP_ROOT.'/includes/os/class.'.PSI_OS.'.inc.php')) {
66
                $this->error->addError("file_exists(class.".PSI_OS.".inc.php)", PSI_OS." is not currently supported");
67
            }
68
 
69
            if (!defined('PSI_MBINFO') && (!$this->_blockName || in_array($this->_blockName, array('voltage','current','temperature','fans','power','other')))) {
70
                // check if there is a valid sensor configuration in phpsysinfo.ini
71
                $foundsp = array();
72
                if (defined('PSI_SENSOR_PROGRAM') && is_string(PSI_SENSOR_PROGRAM)) {
73
                    if (preg_match(ARRAY_EXP, PSI_SENSOR_PROGRAM)) {
74
                        $sensorprograms = eval(strtolower(PSI_SENSOR_PROGRAM));
75
                    } else {
76
                        $sensorprograms = array(strtolower(PSI_SENSOR_PROGRAM));
77
                    }
78
                    foreach ($sensorprograms as $sensorprogram) {
79
                        if (!file_exists(PSI_APP_ROOT.'/includes/mb/class.'.$sensorprogram.'.inc.php')) {
80
                            $this->error->addError("file_exists(class.".htmlspecialchars($sensorprogram).".inc.php)", "specified sensor program is not supported");
81
                        } else {
82
                            $foundsp[] = $sensorprogram;
83
                        }
84
                    }
85
                }
86
 
87
                /**
88
                 * motherboard information
89
                 *
90
                 * @var string serialized array
91
                 */
92
                define('PSI_MBINFO', serialize($foundsp));
93
            }
94
 
95
            if (!defined('PSI_UPSINFO') && (!$this->_blockName || ($this->_blockName==='ups'))) {
96
                // check if there is a valid ups configuration in phpsysinfo.ini
97
                $foundup = array();
98
                if (defined('PSI_UPS_PROGRAM') && is_string(PSI_UPS_PROGRAM)) {
99
                    if (preg_match(ARRAY_EXP, PSI_UPS_PROGRAM)) {
100
                        $upsprograms = eval(strtolower(PSI_UPS_PROGRAM));
101
                    } else {
102
                        $upsprograms = array(strtolower(PSI_UPS_PROGRAM));
103
                    }
104
                    foreach ($upsprograms as $upsprogram) {
105
                        if (!file_exists(PSI_APP_ROOT.'/includes/ups/class.'.$upsprogram.'.inc.php')) {
106
                            $this->error->addError("file_exists(class.".htmlspecialchars($upsprogram).".inc.php)", "specified UPS program is not supported");
107
                        } else {
108
                            $foundup[] = $upsprogram;
109
                        }
110
                    }
111
                }
112
                /**
113
                 * ups information
114
                 *
115
                 * @var string serialized array
116
                 */
117
                define('PSI_UPSINFO', serialize($foundup));
118
            }
119
 
120
            // if there are errors stop executing the script until they are fixed
121
            if ($this->error->errorsExist()) {
122
                $this->error->errorsAsXML();
123
            }
124
 
125
            // Create the XML
126
            $this->_xml = new XML($this->_completeXML, '', $this->_blockName);
127
        } else {
128
            // Create the XML
129
            $this->_xml = new XML(false, $this->_pluginName);
130
        }
131
    }
132
 
133
    /**
134
     * render the output
135
     *
136
     * @return void
137
     */
138
    public function run()
139
    {
140
        header("Cache-Control: no-cache, must-revalidate\n");
141
        header("Content-Type: text/xml\n\n");
142
        $xml = $this->_xml->getXml();
143
        echo $xml->asXML();
144
    }
145
 
146
    /**
147
     * get XML as pure string
148
     *
149
     * @return string
150
     */
151
    public function getXMLString()
152
    {
153
        $xml = $this->_xml->getXml();
154
 
155
        return $xml->asXML();
156
    }
157
 
158
    /**
159
     * get json string
160
     *
161
     * @return string
162
     */
163
    public function getJsonString()
164
    {
165
        if (defined('PSI_JSON_ISSUE') && (PSI_JSON_ISSUE)) {
166
            return json_encode(simplexml_load_string(str_replace(">", ">\n", $this->getXMLString()))); // solving json_encode issue
167
        } else {
168
            return json_encode(simplexml_load_string($this->getXMLString()));
169
        }
170
    }
171
 
172
    /**
173
     * get array
174
     *
175
     * @return array
176
     */
177
    public function getArray()
178
    {
179
        return json_decode($this->getJsonString());
180
    }
181
 
182
    /**
183
     * set parameters for the XML generation process
184
     *
185
     * @param string $plugin name of the plugin, block or 'complete' for all plugins
186
     *
187
     * @return void
188
     */
189
    public function __construct($plugin = "")
190
    {
191
        parent::__construct();
192
 
193
        if (is_string($plugin) && ($plugin !== "")) {
194
            $plugin = strtolower($plugin);
195
            if ($plugin === "complete") {
196
                $this->_completeXML = true;
197
            } else {
198
                $validblocks = array('vitals','hardware','memory','filesystem','network','voltage','current','temperature','fans','power','other','ups');
199
                if (in_array($plugin, $validblocks)) {
200
                    $this->_blockName = $plugin;
201
                } elseif (in_array($plugin, CommonFunctions::getPlugins())) {
202
                    $this->_pluginName = $plugin;
203
                } else {
204
                    $this->_blockName = ' '; //disable all blocks
205
                }
206
            }
207
        }
208
        $this->_prepare();
209
    }
210
}