Skip to content

Commit

Permalink
Merge pull request #301 from andig/debug-cleanup
Browse files Browse the repository at this point in the history
Added fail-safe shell_exec
  • Loading branch information
andig committed May 6, 2015
2 parents 6aa91d0 + 64f1c66 commit fb71017
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
36 changes: 19 additions & 17 deletions lib/Volkszaehler/Util/Debug.php
Expand Up @@ -172,6 +172,20 @@ public static function getInstance() { return self::$instance; }
*/
public function getLevel() { return $this->level; }

/**
* Fail-safe, non-warning, portable shell_exec
*/
public static function safeExec($cmd) {
// shell_exec doesn't exist in safe mode
if (!function_exists('shell_exec')) {
return FALSE;
}

// platform-independent null device to silence STDERR
$null = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'nul' : '/dev/null';
return @shell_exec($cmd . ' 2>' . $null);
}

/**
* Tries to determine the current SHA1 hash of your git commit
*
Expand All @@ -184,15 +198,7 @@ public static function getCurrentCommit() {
}
}

if (function_exists("shell_exec") && ($commit = @shell_exec('git show --pretty=format:%H --quiet'))) {
return $commit;
}

return FALSE;
}

public static function getPhpVersion() {
return phpversion();
return self::safeExec('git show --pretty=format:%H --quiet');
}

/**
Expand All @@ -208,10 +214,8 @@ public static function getLoadAvg() {
$load = file_get_contents('/proc/loadavg');
$load = array_slice(explode(' ', $load), 0, 3);
}
elseif (function_exists('shell_exec')) {
// fail-safe shell exec
$null = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'nul' : '/dev/null';
$load = explode(', ', substr(shell_exec('uptime 2>' . $null), -16));
elseif ($res = self::safeExec('uptime')) {
$load = explode(', ', substr($res, -16));
}

return (isset($load)) ? array_map('floatval', $load) : FALSE;
Expand All @@ -227,11 +231,9 @@ public static function getUptime() {
$uptime = explode(' ', file_get_contents("/proc/uptime"));
return (float) $uptime[0];
}
elseif (function_exists("shell_exec")) {
elseif ($res = self::safeExec('uptime')) {
$matches = array();
// fail-safe shell exec
$null = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'nul' : '/dev/null';
if (preg_match("/up (?:(?P<days>\d+) days?,? )?(?P<hours>\d+):(?P<minutes>\d{2})/", shell_exec('uptime 2>' . $null), $matches)) {
if (preg_match("/up (?:(?P<days>\d+) days?,? )?(?P<hours>\d+):(?P<minutes>\d{2})/", $res, $matches)) {
$uptime = 60*$matches['hours'] + $matches['minutes'];

if (isset($matches['days'])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Volkszaehler/View/CSV.php
Expand Up @@ -89,7 +89,7 @@ protected function addDebug(Util\Debug $debug) {
if ($uptime = Util\Debug::getUptime()) echo '# uptime:' . CSV::DELIMITER . $uptime*1000;
if ($load = Util\Debug::getLoadAvg()) echo '# load:' . CSV::DELIMITER . implode(', ', $load) . PHP_EOL;
if ($commit = Util\Debug::getCurrentCommit()) echo '# commit-hash:' . CSV::DELIMITER . $commit;
if ($version = Util\Debug::getPhpVersion()) echo '# php-version:' . CSV::DELIMITER . $version;
if ($version = phpversion()) echo '# php-version:' . CSV::DELIMITER . $version;

foreach ($debug->getMessages() as $message) {
echo '# message:' . CSV::DELIMITER . $message['message'] . PHP_EOL; // TODO add more information
Expand Down
2 changes: 1 addition & 1 deletion lib/Volkszaehler/View/JSON.php
Expand Up @@ -160,7 +160,7 @@ protected function addDebug(Util\Debug $debug) {
if ($uptime = Util\Debug::getUptime()) $jsonDebug['uptime'] = $uptime*1000;
if ($load = Util\Debug::getLoadAvg()) $jsonDebug['load'] = $load;
if ($commit = Util\Debug::getCurrentCommit()) $jsonDebug['commit-hash'] = $commit;
if ($version = Util\Debug::getPhpVersion()) $jsonDebug['php-version'] = $version;
if ($version = phpversion()) $jsonDebug['php-version'] = $version;

$jsonDebug['messages'] = $debug->getMessages();

Expand Down
2 changes: 1 addition & 1 deletion lib/Volkszaehler/View/XML.php
Expand Up @@ -179,7 +179,7 @@ protected function addDebug(Util\Debug $debug) {
if ($uptime = Util\Debug::getUptime()) $xmlDebug->appendChild($this->xmlDoc->createElement('uptime', $uptime*1000));
if ($load = Util\Debug::getLoadAvg()) $xmlDebug->appendChild($this->xmlDoc->createElement('load', implode(', ', $load)));
if ($commit = Util\Debug::getCurrentCommit()) $xmlDebug->appendChild($this->xmlDoc->createElement('commit-hash', $commit));
if ($version = Util\Debug::getPhpVersion()) $xmlDebug->appendChild($this->xmlDoc->createElement('php-version', $version));
if ($version = phpversion()) $xmlDebug->appendChild($this->xmlDoc->createElement('php-version', $version));

$xmlMessages = $this->xmlDoc->createElement('messages');
foreach ($debug->getMessages() as $message) {
Expand Down

0 comments on commit fb71017

Please sign in to comment.