jablonka.czprosek.czf

websvn

Subversion Repositories:
[/] [include/] [command.php] - Blame information for rev 5

 

Line No. Rev Author Line
11simandl<?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 
32function 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 
61function 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 
85function 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 
1003simandl// {{{ execCommand
101 
102function execCommand($cmd, &$retcode) {
103 global $config;
104 
105 // On Windows machines, the whole line needs quotes round it so that it's
106 // passed to cmd.exe correctly
107 // Since php 5.3.0 the quoting seems to be done internally
108 
109 if ($config->serverIsWindows && version_compare(PHP_VERSION, '5.3.0alpha') === -1) {
110 $cmd = "\"$cmd\"";
111 }
112 
113 return @exec($cmd, $tmp, $retcode);
114}
115 
116// }}}
117 
118// {{{ popenCommand
119 
120function popenCommand($cmd, $mode) {
121 global $config;
122 
123 // On Windows machines, the whole line needs quotes round it so that it's
124 // passed to cmd.exe correctly
125 // Since php 5.3.0 the quoting seems to be done internally
126 
127 if ($config->serverIsWindows && version_compare(PHP_VERSION, '5.3.0alpha') === -1) {
128 $cmd = "\"$cmd\"";
129 }
130 
131 return popen($cmd, $mode);
132}
133 
134// }}}
135 
136// {{{ passthruCommand
137 
138function passthruCommand($cmd) {
139 global $config;
140 
141 // On Windows machines, the whole line needs quotes round it so that it's
142 // passed to cmd.exe correctly
143 // Since php 5.3.0 the quoting seems to be done internally
144 
145 if ($config->serverIsWindows && version_compare(PHP_VERSION, '5.3.0alpha') === -1) {
146 $cmd = "\"$cmd\"";
147 }
148 
149 return passthru($cmd);
150}
151 
152// }}}
153 
1541simandl// {{{ runCommand
155 
156function runCommand($cmd, $mayReturnNothing = false) {
157 global $lang;
158 
159 $output = array();
160 $err = false;
161 
162 $c = quoteCommand($cmd);
163 
164 $descriptorspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
165 
166 $resource = proc_open($c, $descriptorspec, $pipes);
167 $error = "";
168 
169 if (!is_resource($resource)) {
170 echo"<p>".$lang['BADCMD'].": <code>".$cmd."</code></p>";
171 exit;
172 }
173 
174 $handle = $pipes[1];
175 $firstline = true;
176 while (!feof($handle)) {
177 $line = fgets($handle);
178 if ($firstline && empty($line) && !$mayReturnNothing) {
179 $err = true;
180 }
181 
182 $firstline = false;
183 $output[] = toOutputEncoding(rtrim($line));
184 }
185 
186 while (!feof($pipes[2])) {
187 $error .= fgets($pipes[2]);
188 }
189 
190 $error = toOutputEncoding(trim($error));
191 
192 fclose($pipes[0]);
193 fclose($pipes[1]);
194 fclose($pipes[2]);
195 
196 proc_close($resource);
197 
198 if (!$err) {
199 return $output;
200 } else {
201 echo"<p>".$lang['BADCMD'].": <code>".$cmd."</code></p><p>".nl2br($error)."</p>";
202 }
203}
204 
205// }}}
206 
207// {{{ quote
208//
209// Quote a string to send to the command line
210 
211function quote($str) {
212 global $config;
213 
214 if ($config->serverIsWindows) {
215 return "\"$str\"";
216 } else {
217 return escapeshellarg($str);
218 }
219}
220 
221// }}}

Powered by WebSVN 2.2.1