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