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