Rev Author Line No. Line
130 kaklik 1 <?php
2 # vim:et:ts=3:sts=3:sw=3:fdm=marker:
3  
4 // WebSVN - Subversion repository viewing via the web using PHP
5 // Copyright © 2004-2006 Tim Armes, Matt Sicker
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // --
22 //
23 // command.inc
24 //
25 // External command handling
26  
27 // {{{ replaceEntities
28 //
29 // Replace character codes with HTML entities for display purposes.
30 // This routine assumes that the character encoding of the string is
31 // that of the local system (i.e., it's a string returned from a command
32 // line command).
33  
34 function replaceEntities($str, $rep)
35 {
36 global $config;
37  
38 // Ideally, we'd do this:
39 //
40 // $str = htmlentities($str, ENT_COMPAT, $config->inputEnc);
41 //
42 // However, htmlentities is very limited in it's ability to process
43 // character encodings. We have to rely on something more powerful.
44  
45 if (version_compare(phpversion(), "4.1.0", "<"))
46 {
47 // In this case, we can't do any better than assume that the
48 // input encoding is ISO-8859-1.
49  
50 $str = htmlentities($str);
51 }
52 else
53 {
54 $str = toOutputEncoding($str, $rep->getContentEncoding());
55  
56 // $str is now encoded as UTF-8.
57 $str = htmlentities($str, ENT_COMPAT, $config->outputEnc);
58 }
59  
60 return $str;
61 }
62  
63 // }}}
64  
65 // {{{ toOutputEncoding
66  
67 function toOutputEncoding($str, $inputEncoding = "")
68 {
69 global $config;
70  
71 if (empty($inputEncoding))
72 $inputEncoding = $config->inputEnc;
73  
74 // Try to convert the messages based on the locale information
75 if ($config->inputEnc && $config->outputEnc)
76 {
77 if (function_exists("iconv"))
78 {
79 $output = @iconv($inputEncoding, $config->outputEnc, $str);
80 if (!empty($output))
81 $str = $output;
82 }
83 }
84  
85 return $str;
86 }
87  
88 // }}}
89  
90 // {{{ quoteCommand
91  
92 function quoteCommand($cmd, $redirecterr)
93 {
94 global $config;
95  
96 if ($redirecterr)
97 $cmd .= " 2>&1";
98  
99 // On Windows machines, the whole line needs quotes round it so that it's
100 // passed to cmd.exe correctly
101  
102 if ($config->serverIsWindows)
103 $cmd = "\"$cmd\"";
104  
105 return $cmd;
106 }
107  
108 // }}}
109  
110 // {{{ runCommand
111  
112 function runCommand($cmd, $mayReturnNothing = false)
113 {
114 global $lang;
115  
116 $output = array ();
117 $err = false;
118  
119 $c = quoteCommand($cmd, false);
120  
121 // Try to run the command normally
122 if ($handle = popen($c, 'r'))
123 {
124 $firstline = true;
125 while (!feof($handle))
126 {
127 $line = fgets($handle);
128 if ($firstline && empty($line) && !$mayReturnNothing)
129 {
130 $err = true;
131 }
132 $firstline = false;
133 $output[] = toOutputEncoding(rtrim($line));
134 }
135  
136 pclose($handle);
137 if (!$err)
138 return $output;
139 }
140  
141 echo '<p>',$lang['BADCMD'],': <code>',$cmd,'</code></p>';
142  
143 // Rerun the command, this time grabbing the error information
144  
145 $c = quoteCommand($cmd, true);
146  
147 $output = toOutputEncoding(shell_exec($c));
148 if (!empty($output))
149 echo '<p>',nl2br($output),'</p>';
150 exit;
151 }
152  
153 // }}}
154  
155 // {{{ quote
156 //
157 // Quote a string to send to the command line
158  
159 function quote($str)
160 {
161 global $config;
162  
163 if ($config->serverIsWindows)
164 return "\"$str\"";
165 else
166 return escapeshellarg($str);
167 }
168  
169 // }}}
170  
171 ?>