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 | | | // comp.php |
22 | | | // |
23 | | | // Compare two paths using "svn diff" |
24 | | | // |
25 | | | |
26 | | | require_once("include/setup.php"); |
27 | | | require_once("include/svnlook.php"); |
28 | | | require_once("include/utils.php"); |
29 | | | require_once("include/template.php"); |
30 | | | |
31 | | | function checkRevision($rev) { |
32 | | | if (is_numeric($rev) && ((int)$rev > 0)) { |
33 | | | return $rev; |
34 | | | } |
35 | | | |
36 | | | $rev = strtoupper($rev); |
37 | | | |
38 | | | switch($rev) { |
39 | | | case "HEAD": |
40 | | | // fall through |
41 | | | case "PREV": |
42 | | | // fall through |
43 | | | case "COMMITTED": |
44 | | | return $rev; |
45 | | | } |
46 | | | |
47 | | | return "HEAD"; |
48 | | | } |
49 | | | |
50 | | | $svnrep = new SVNRepository($rep); |
51 | | | |
52 | | | // Retrieve the request information |
53 | | | $path1 = @$_REQUEST["compare"][0]; |
54 | | | $path2 = @$_REQUEST["compare"][1]; |
55 | | | $rev1 = (int)@$_REQUEST["compare_rev"][0]; |
56 | | | $rev2 = (int)@$_REQUEST["compare_rev"][1]; |
57 | 3 | simandl | $manualorder = (@$_REQUEST["manualorder"] == 1); |
58 | | | $ignoreWhitespace = (@$_REQUEST["ignorews"] == 1); |
59 | 1 | simandl | |
60 | | | // Some page links put the revision with the path... |
61 | | | if (strpos($path1, "@")) list($path1, $rev1) = explode("@", $path1); |
62 | 3 | simandl | // Something went wrong. The path is missing. |
63 | | | else if (strpos($path1, "@") === 0) { |
64 | | | $rev1 = substr($path1, 1); |
65 | | | $path1 = '/'; |
66 | | | } |
67 | 1 | simandl | if (strpos($path2, "@")) list($path2, $rev2) = explode("@", $path2); |
68 | 3 | simandl | else if (strpos($path2, "@") === 0) { |
69 | | | $rev2 = substr($path2, 1); |
70 | | | $path2 = '/'; |
71 | | | } |
72 | 1 | simandl | |
73 | | | $rev1 = checkRevision($rev1); |
74 | | | $rev2 = checkRevision($rev2); |
75 | | | |
76 | | | // Choose a sensible comparison order unless told not to |
77 | | | |
78 | 3 | simandl | if (!$manualorder && is_numeric($rev1) && is_numeric($rev2) && $rev1 > $rev2) { |
79 | | | $temppath = $path1; |
80 | | | $temprev = $rev1; |
81 | 1 | simandl | |
82 | 3 | simandl | $path1 = $path2; |
83 | | | $rev1 = $rev2; |
84 | | | |
85 | | | $path2 = $temppath; |
86 | | | $rev2 = $temprev; |
87 | 1 | simandl | } |
88 | | | |
89 | | | $vars['indexurl'] = $config->getURL($rep, '', 'index'); |
90 | | | $vars['repurl'] = $config->getURL($rep, '', 'dir'); |
91 | | | |
92 | | | $url = $config->getURL($rep, "/", "comp"); |
93 | 3 | simandl | $vars["revlink"] = '<a href="'.$url.'compare%5B%5D='.urlencode($path2).'@'.$rev2.'&compare%5B%5D='.urlencode($path1).'@'.$rev1.'&manualorder=1&ignorews='.($ignoreWhitespace ? '1' : '0').'">'.$lang['REVCOMP'].'</a>'; |
94 | | | if (!$ignoreWhitespace) { |
95 | | | $vars['ignorewhitespacelink'] = '<a href="'.$url.'compare%5B%5D='.urlencode($path1).'@'.$rev1.'&compare%5B%5D='.urlencode($path2).'@'.$rev2.'&manualorder='.($manualorder ? '1' : '0').'&ignorews=1">'.$lang['IGNOREWHITESPACE'].'</a>'; |
96 | | | $vars['regardwhitespacelink'] = ''; |
97 | | | } else { |
98 | | | $vars['regardwhitespacelink'] = '<a href="'.$url.'compare%5B%5D='.urlencode($path1).'@'.$rev1.'&compare%5B%5D='.urlencode($path2).'@'.$rev2.'&manualorder='.($manualorder ? '1' : '0').'&ignorews=0">'.$lang['REGARDWHITESPACE'].'</a>'; |
99 | | | $vars['ignorewhitespacelink'] = ''; |
100 | | | } |
101 | 1 | simandl | |
102 | | | if ($rev1 == 0) $rev1 = "HEAD"; |
103 | | | if ($rev2 == 0) $rev2 = "HEAD"; |
104 | | | |
105 | | | $vars["repname"] = $rep->getDisplayName(); |
106 | | | $vars["action"] = $lang["PATHCOMPARISON"]; |
107 | | | $vars["compare_form"] = "<form action=\"$url\" method=\"post\">"; |
108 | | | $vars["compare_path1input"] = "<input type=\"text\" size=\"40\" name=\"compare[0]\" value=\"".htmlentities($path1, ENT_QUOTES, 'UTF-8')."\" />"; |
109 | | | $vars["compare_rev1input"] = "<input type=\"text\" size=\"5\" name=\"compare_rev[0]\" value=\"$rev1\" />"; |
110 | | | $vars["compare_path2input"] = "<input type=\"text\" size=\"40\" name=\"compare[1]\" value=\"".htmlentities($path2, ENT_QUOTES, 'UTF-8')."\" />"; |
111 | | | $vars["compare_rev2input"] = "<input type=\"text\" size=\"5\" name=\"compare_rev[1]\" value=\"$rev2\" />"; |
112 | | | $vars["compare_submit"] = "<input name=\"comparesubmit\" type=\"submit\" value=\"${lang["COMPAREPATHS"]}\" />"; |
113 | | | $vars["compare_hidden"] = "<input type=\"hidden\" name=\"op\" value=\"comp\" /><input type=\"hidden\" name=\"manualorder\" value=\"1\" />"; |
114 | | | $vars["compare_endform"] = "</form>"; |
115 | | | |
116 | | | // safe paths are a hack for fixing XSS sploit |
117 | | | $vars["path1"] = htmlentities($path1, ENT_QUOTES, 'UTF-8'); |
118 | | | $vars['safepath1'] = htmlentities($path1, ENT_QUOTES, 'UTF-8'); |
119 | | | $vars["path2"] = htmlentities($path2, ENT_QUOTES, 'UTF-8'); |
120 | | | $vars['safepath2'] = htmlentities($path2, ENT_QUOTES, 'UTF-8'); |
121 | | | |
122 | | | $vars["rev1"] = $rev1; |
123 | | | $vars["rev2"] = $rev2; |
124 | | | |
125 | | | $noinput = empty($path1) || empty($path2); |
126 | | | $listing = array(); |
127 | | | |
128 | | | // Generate the diff listing |
129 | | | |
130 | | | $relativePath1 = $path1; |
131 | | | $relativePath2 = $path2; |
132 | | | |
133 | 3 | simandl | $svnpath1 = encodepath($svnrep->getSvnpath(str_replace(DIRECTORY_SEPARATOR, '/', $path1))); |
134 | | | $svnpath2 = encodepath($svnrep->getSvnpath(str_replace(DIRECTORY_SEPARATOR, '/', $path2))); |
135 | 1 | simandl | |
136 | | | $debug = false; |
137 | | | |
138 | | | if (!$noinput) { |
139 | 3 | simandl | $cmd = $config->svn." diff ".($ignoreWhitespace ? '-x -w ' : '').$rep->svnParams().quote($svnpath1."@".$rev1)." ".quote($svnpath2."@".$rev2); |
140 | 1 | simandl | if ($debug) echo "$cmd\n"; |
141 | | | } |
142 | | | |
143 | | | function clearVars() { |
144 | | | global $listing, $index; |
145 | | | |
146 | | | $listing[$index]["newpath"] = null; |
147 | | | $listing[$index]["endpath"] = null; |
148 | | | $listing[$index]["info"] = null; |
149 | | | $listing[$index]["diffclass"] = null; |
150 | | | $listing[$index]["difflines"] = null; |
151 | | | $listing[$index]["enddifflines"] = null; |
152 | | | $listing[$index]["properties"] = null; |
153 | | | } |
154 | | | |
155 | | | $vars["success"] = false; |
156 | | | |
157 | | | if (!$noinput) { |
158 | 3 | simandl | if ($diff = popenCommand($cmd, "r")) { |
159 | 1 | simandl | $index = 0; |
160 | | | $indiff = false; |
161 | | | $indiffproper = false; |
162 | | | $getLine = true; |
163 | | | $node = null; |
164 | | | $bufferedLine = false; |
165 | | | |
166 | | | $vars["success"] = true; |
167 | | | |
168 | | | while (!feof($diff)) { |
169 | | | if ($getLine) { |
170 | | | if ($bufferedLine === false) { |
171 | | | $bufferedLine = rtrim(fgets($diff), "\r\n"); |
172 | | | } |
173 | | | $newlineR = strpos($bufferedLine, "\r"); |
174 | | | $newlineN = strpos($bufferedLine, "\n"); |
175 | | | if ($newlineR === false && $newlineN === false) { |
176 | | | $line = $bufferedLine; |
177 | | | $bufferedLine = false; |
178 | | | } else { |
179 | | | $newline = ($newlineR < $newlineN ? $newlineR : $newlineN); |
180 | | | $line = substr($bufferedLine, 0, $newline); |
181 | | | $bufferedLine = substr($bufferedLine, $newline + 1); |
182 | | | } |
183 | | | } |
184 | | | |
185 | | | clearVars(); |
186 | | | $getLine = true; |
187 | | | if ($debug) print "Line = '$line'<br />"; |
188 | | | if ($indiff) { |
189 | | | // If we're in a diff proper, just set up the line |
190 | | | if ($indiffproper) { |
191 | | | if (strlen($line) > 0 && ($line[0] == " " || $line[0] == "+" || $line[0] == "-")) { |
192 | | | $subline = replaceEntities(substr($line, 1), $rep); |
193 | 3 | simandl | if ($subline == '') $subline = " "; |
194 | 1 | simandl | $subline = hardspace($subline); |
195 | | | $listing[$index]["line"] = $subline; |
196 | | | |
197 | | | switch ($line[0]) { |
198 | | | case " ": |
199 | | | $listing[$index]["diffclass"] = "diff"; |
200 | | | if ($debug) print "Including as diff: $subline<br />"; |
201 | | | break; |
202 | | | |
203 | | | case "+": |
204 | | | $listing[$index]["diffclass"] = "diffadded"; |
205 | | | if ($debug) print "Including as added: $subline<br />"; |
206 | | | break; |
207 | | | |
208 | | | case "-": |
209 | | | $listing[$index]["diffclass"] = "diffdeleted"; |
210 | | | if ($debug) print "Including as removed: $subline<br />"; |
211 | | | break; |
212 | | | } |
213 | | | |
214 | | | $index++; |
215 | | | |
216 | | | continue; |
217 | | | |
218 | | | } else if ($line == '\ No newline at end of file') { |
219 | | | continue; |
220 | | | |
221 | | | } else { |
222 | | | $indiffproper = false; |
223 | | | $listing[$index++]["enddifflines"] = true; |
224 | | | $getLine = false; |
225 | | | if ($debug) print "Ending lines<br />"; |
226 | | | continue; |
227 | | | } |
228 | | | } |
229 | | | |
230 | | | // Check for the start of a new diff area |
231 | | | if (!strncmp($line, "@@", 2)) { |
232 | | | $pos = strpos($line, "+"); |
233 | | | $posline = substr($line, $pos); |
234 | 3 | simandl | $sline = 0; |
235 | | | $eline = 0; |
236 | 1 | simandl | sscanf($posline, "+%d,%d", $sline, $eline); |
237 | | | if ($debug) print "sline = '$sline', eline = '$eline'<br />"; |
238 | | | // Check that this isn't a file deletion |
239 | | | if ($sline == 0 && $eline == 0) { |
240 | | | $line = fgets($diff); |
241 | | | if ($debug) print "Ignoring: $line<br />" ; |
242 | | | while ($line[0] == " " || $line[0] == "+" || $line[0] == "-") { |
243 | | | $line = fgets($diff); |
244 | | | if ($debug) print "Ignoring: $line<br />" ; |
245 | | | } |
246 | | | |
247 | | | $getLine = false; |
248 | | | if ($debug) print "Unignoring previous - marking as deleted<b>"; |
249 | | | $listing[$index++]["info"] = $lang["FILEDELETED"]; |
250 | | | |
251 | | | } else { |
252 | | | $listing[$index]["difflines"] = $line; |
253 | 3 | simandl | $sline = 0; |
254 | | | $slen = 0; |
255 | | | $eline = 0; |
256 | | | $elen = 0; |
257 | 1 | simandl | sscanf($line, "@@ -%d,%d +%d,%d @@", $sline, $slen, $eline, $elen); |
258 | | | $listing[$index]["rev1line"] = $sline; |
259 | | | $listing[$index]["rev1len"] = $slen; |
260 | | | $listing[$index]["rev2line"] = $eline; |
261 | | | $listing[$index]["rev2len"] = $elen; |
262 | | | |
263 | | | $indiffproper = true; |
264 | | | |
265 | | | $index++; |
266 | | | } |
267 | | | |
268 | | | continue; |
269 | | | |
270 | | | } else { |
271 | | | $indiff = false; |
272 | | | if ($debug) print "Ending diff"; |
273 | | | } |
274 | | | } |
275 | | | |
276 | | | // Check for a new node entry |
277 | | | if (strncmp(trim($line), "Index: ", 7) == 0) { |
278 | | | // End the current node |
279 | | | if ($node) { |
280 | | | $listing[$index++]["endpath"] = true; |
281 | | | clearVars(); |
282 | | | } |
283 | | | |
284 | | | $node = trim($line); |
285 | | | $node = substr($node, 7); |
286 | | | if ($node == '' || $node{0} != '/') $node = '/'.$node; |
287 | | | |
288 | 3 | simandl | if (substr($path2, -strlen($node)) === $node) { |
289 | | | $absnode = $path2; |
290 | | | } else { |
291 | | | $absnode = $path2; |
292 | | | if (substr($absnode, -1) == '/') $absnode = substr($absnode, 0, -1); |
293 | | | $absnode .= $node; |
294 | | | } |
295 | 1 | simandl | |
296 | 3 | simandl | $listing[$index]["newpath"] = $absnode; |
297 | 1 | simandl | |
298 | 3 | simandl | $listing[$index]["fileurl"] = $config->getURL($rep, $absnode, "file").'rev='.$rev2; |
299 | | | |
300 | 1 | simandl | if ($debug) echo "Creating node $node<br />"; |
301 | | | |
302 | | | // Skip past the line of ='s |
303 | | | $line = fgets($diff); |
304 | | | if ($debug) print "Skipping: $line<br />" ; |
305 | | | |
306 | | | // Check for a file addition |
307 | | | $line = fgets($diff); |
308 | | | if ($debug) print "Examining: $line<br />" ; |
309 | | | if (strpos($line, "(revision 0)")) { |
310 | | | $listing[$index]["info"] = $lang["FILEADDED"]; |
311 | | | } |
312 | | | |
313 | | | if (strncmp(trim($line), "Cannot display:", 15) == 0) { |
314 | | | $index++; |
315 | | | clearVars(); |
316 | | | $listing[$index++]["info"] = $line; |
317 | | | continue; |
318 | | | } |
319 | | | |
320 | | | // Skip second file info |
321 | | | $line = fgets($diff); |
322 | | | if ($debug) print "Skipping: $line<br />" ; |
323 | | | |
324 | | | $indiff = true; |
325 | | | $index++; |
326 | | | |
327 | | | continue; |
328 | | | } |
329 | | | |
330 | | | if (strncmp(trim($line), "Property changes on: ", 21) == 0) { |
331 | | | $propnode = trim($line); |
332 | | | $propnode = substr($propnode, 21); |
333 | | | |
334 | | | if ($debug) print "Properties on $propnode (cur node $ $node)"; |
335 | | | if ($propnode != $node) { |
336 | | | if ($node) { |
337 | | | $listing[$index++]["endpath"] = true; |
338 | | | clearVars(); |
339 | | | } |
340 | | | |
341 | | | $node = $propnode; |
342 | | | |
343 | | | $listing[$index++]["newpath"] = $node; |
344 | | | clearVars(); |
345 | | | } |
346 | | | |
347 | | | $listing[$index++]["properties"] = true; |
348 | | | clearVars(); |
349 | | | if ($debug) echo "Creating node $node<br />"; |
350 | | | |
351 | | | // Skip the row of underscores |
352 | | | $line = fgets($diff); |
353 | | | if ($debug) print "Skipping: $line<br />" ; |
354 | | | |
355 | | | while ($line = trim(fgets($diff))) { |
356 | | | $listing[$index++]["info"] = $line; |
357 | | | clearVars(); |
358 | | | } |
359 | | | |
360 | | | continue; |
361 | | | } |
362 | | | |
363 | | | // Check for error messages |
364 | | | if (strncmp(trim($line), "svn: ", 5) == 0) { |
365 | | | $listing[$index++]["info"] = urldecode($line); |
366 | | | $vars["success"] = false; |
367 | | | continue; |
368 | | | } |
369 | | | |
370 | | | $listing[$index++]["info"] = $line; |
371 | | | } |
372 | | | |
373 | | | if ($node) { |
374 | | | clearVars(); |
375 | | | $listing[$index++]["endpath"] = true; |
376 | | | } |
377 | | | |
378 | | | if ($debug) print_r($listing); |
379 | | | |
380 | | | pclose($diff); |
381 | | | } |
382 | | | } |
383 | | | |
384 | | | $vars["version"] = $version; |
385 | | | |
386 | | | if (!$rep->hasUnrestrictedReadAccess($relativePath1) || !$rep->hasUnrestrictedReadAccess($relativePath2, false)) { |
387 | | | $vars["noaccess"] = true; |
388 | | | } |
389 | | | |
390 | | | parseTemplate($rep->getTemplatePath()."header.tmpl", $vars, $listing); |
391 | | | parseTemplate($rep->getTemplatePath()."compare.tmpl", $vars, $listing); |
392 | | | parseTemplate($rep->getTemplatePath()."footer.tmpl", $vars, $listing); |