<?php
# vim:et:ts=3:sts=3:sw=3:fdm=marker:
// WebSVN - Subversion repository viewing via the web using PHP
// Copyright © 2004-2006 Tim Armes, Matt Sicker
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// --
//
// command.inc
//
// External command handling
// {{{ replaceEntities
//
// Replace character codes with HTML entities for display purposes.
// This routine assumes that the character encoding of the string is
// that of the local system (i.e., it's a string returned from a command
// line command).
function replaceEntities($str, $rep)
{
global $config;
// Ideally, we'd do this:
//
// $str = htmlentities($str, ENT_COMPAT, $config->inputEnc);
//
// However, htmlentities is very limited in it's ability to process
// character encodings. We have to rely on something more powerful.
if (version_compare(phpversion(), "4.1.0", "<"))
{
// In this case, we can't do any better than assume that the
// input encoding is ISO-8859-1.
$str = htmlentities($str);
}
else
{
$str = toOutputEncoding($str, $rep->getContentEncoding());
// $str is now encoded as UTF-8.
$str = htmlentities($str, ENT_COMPAT, $config->outputEnc);
}
return $str;
}
// }}}
// {{{ toOutputEncoding
function toOutputEncoding($str, $inputEncoding = "")
{
global $config;
if (empty($inputEncoding))
$inputEncoding = $config->inputEnc;
// Try to convert the messages based on the locale information
if ($config->inputEnc && $config->outputEnc)
{
if (function_exists("iconv"))
{
$output = @iconv($inputEncoding, $config->outputEnc, $str);
if (!empty($output))
$str = $output;
}
}
return $str;
}
// }}}
// {{{ quoteCommand
function quoteCommand($cmd, $redirecterr)
{
global $config;
if ($redirecterr)
$cmd .= " 2>&1";
// On Windows machines, the whole line needs quotes round it so that it's
// passed to cmd.exe correctly
if ($config->serverIsWindows)
$cmd = "\"$cmd\"";
return $cmd;
}
// }}}
// {{{ runCommand
function runCommand($cmd, $mayReturnNothing = false)
{
global $lang;
$output = array ();
$err = false;
$c = quoteCommand($cmd, false);
// Try to run the command normally
if ($handle = popen($c, 'r'))
{
$firstline = true;
while (!feof($handle))
{
$line = fgets($handle);
if ($firstline && empty($line) && !$mayReturnNothing)
{
$err = true;
}
$firstline = false;
$output[] = toOutputEncoding(rtrim($line));
}
pclose($handle);
if (!$err)
return $output;
}
echo '<p>',$lang['BADCMD'],': <code>',$cmd,'</code></p>';
// Rerun the command, this time grabbing the error information
$c = quoteCommand($cmd, true);
$output = toOutputEncoding(shell_exec($c));
if (!empty($output))
echo '<p>',nl2br($output),'</p>';
exit;
}
// }}}
// {{{ quote
//
// Quote a string to send to the command line
function quote($str)
{
global $config;
if ($config->serverIsWindows)
return "\"$str\"";
else
return escapeshellarg($str);
}
// }}}
?>