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