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 | | | // diff.php |
22 | | | // |
23 | | | // Show the differences between 2 revisions of a file. |
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 | | | $context = 5; |
32 | | | |
33 | | | $vars["action"] = $lang["DIFF"]; |
34 | | | $all = (@$_REQUEST["all"] == 1)?1:0; |
35 | | | |
36 | | | // Make sure that we have a repository |
37 | | | if (!isset($rep)) { |
38 | | | echo $lang["NOREP"]; |
39 | | | exit; |
40 | | | } |
41 | | | |
42 | | | $svnrep = new SVNRepository($rep); |
43 | | | |
44 | | | // If there's no revision info, go to the lastest revision for this path |
45 | | | $history = $svnrep->getLog($path, "", "", true); |
46 | | | $youngest = $history->entries[0]->rev; |
47 | | | |
48 | | | if (empty($rev)) { |
49 | | | $rev = $youngest; |
50 | | | } |
51 | | | |
52 | | | $history = $svnrep->getLog($path, $rev); |
53 | | | |
54 | | | if ($path{0} != "/") { |
55 | | | $ppath = "/".$path; |
56 | | | } else { |
57 | | | $ppath = $path; |
58 | | | } |
59 | | | |
60 | | | $prevrev = @$history->entries[1]->rev; |
61 | | | |
62 | | | $vars["repname"] = htmlentities($rep->getDisplayName(), ENT_QUOTES, 'UTF-8'); |
63 | | | $vars["rev"] = $rev; |
64 | | | $vars["path"] = htmlentities($ppath, ENT_QUOTES, 'UTF-8'); |
65 | | | $vars["prevrev"] = $prevrev; |
66 | | | |
67 | | | $vars["rev1"] = $history->entries[0]->rev; |
68 | | | $vars["rev2"] = $prevrev; |
69 | | | |
70 | | | createDirLinks($rep, $ppath, $rev); |
71 | | | |
72 | | | $listing = array(); |
73 | | | |
74 | | | $url = $config->getURL($rep, $path, "file"); |
75 | | | if ($rev != $youngest) { |
76 | | | $vars["goyoungestlink"] = '<a href="'.$url.'">'.$lang['GOYOUNGEST'].'</a>'; |
77 | | | } else { |
78 | | | $vars["goyoungestlink"] = ""; |
79 | | | } |
80 | | | |
81 | | | $vars['indexurl'] = $config->getURL($rep, '', 'index'); |
82 | | | $vars['repurl'] = $config->getURL($rep, '', 'dir'); |
83 | | | |
84 | | | $url = $config->getURL($rep, $path, "file"); |
85 | | | $vars["filedetaillink"] = "<a href=\"${url}rev=$rev&isdir=0\">${lang["FILEDETAIL"]}</a>"; |
86 | | | |
87 | | | $url = $config->getURL($rep, $path, "log"); |
88 | | | $vars["fileviewloglink"] = "<a href=\"${url}rev=$rev&isdir=0\">${lang["VIEWLOG"]}</a>"; |
89 | | | |
90 | | | $url = $config->getURL($rep, $path, "diff"); |
91 | | | $vars["prevdifflink"] = "<a href=\"${url}rev=$rev\">${lang["DIFFPREV"]}</a>"; |
92 | | | |
93 | | | $url = $config->getURL($rep, $path, "blame"); |
94 | | | $vars["blamelink"] = "<a href=\"${url}rev=$rev\">${lang["BLAME"]}</a>"; |
95 | | | |
96 | | | if ($prevrev) { |
97 | | | $url = $config->getURL($rep, $path, "diff"); |
98 | | | |
99 | | | if (!$all) { |
100 | | | $vars["showalllink"] = "<a href=\"${url}rev=$rev&all=1\">${lang["SHOWENTIREFILE"]}</a>"; |
101 | | | $vars["showcompactlink"] = ""; |
102 | | | } else { |
103 | | | $vars["showcompactlink"] = "<a href=\"${url}rev=$rev&all=0\">${lang["SHOWCOMPACT"]}</a>"; |
104 | | | $vars["showalllink"] = ""; |
105 | | | } |
106 | | | |
107 | | | // Get the contents of the two files |
108 | | | $newtname = tempnam("temp", ""); |
109 | | | $svnrep->getFileContents($history->entries[0]->path, $newtname, $history->entries[0]->rev, "", true); |
110 | | | |
111 | | | $oldtname = tempnam("temp", ""); |
112 | | | $svnrep->getFileContents($history->entries[1]->path, $oldtname, $history->entries[1]->rev, "", true); |
113 | | | |
114 | | | $ent = true; |
115 | | | $extension = strrchr(basename($path), "."); |
116 | | | if (($extension && isset($extEnscript[$extension]) && ('php' == $extEnscript[$extension])) || ($config->useEnscript || $config->useGeshi)) { |
117 | | | $ent = false; |
118 | | | } |
119 | | | |
120 | | | if ($all) { |
121 | | | // Setting the context to 0 makes diff generate the wrong line numbers! |
122 | | | $context = 1; |
123 | | | } |
124 | | | |
125 | | | // Open a pipe to the diff command with $context lines of context |
126 | | | |
127 | | | $cmd = quoteCommand($config->diff." -w -U $context \"$oldtname\" \"$newtname\""); |
128 | | | |
129 | | | if ($all) { |
130 | | | $ofile = fopen($oldtname, "r"); |
131 | | | $nfile = fopen($newtname, "r"); |
132 | | | } |
133 | | | |
134 | | | $descriptorspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); |
135 | | | |
136 | | | $resource = proc_open($cmd, $descriptorspec, $pipes); |
137 | | | $error = ""; |
138 | | | |
139 | | | if (is_resource($resource)) { |
140 | | | // We don't need to write |
141 | | | fclose($pipes[0]); |
142 | | | |
143 | | | $diff = $pipes[1]; |
144 | | | |
145 | | | // Ignore the 3 header lines |
146 | | | $line = fgets($diff); |
147 | | | $line = fgets($diff); |
148 | | | |
149 | | | // Get the first real line |
150 | | | $line = fgets($diff); |
151 | | | |
152 | | | $index = 0; |
153 | | | $listing = array(); |
154 | | | |
155 | | | $curoline = 1; |
156 | | | $curnline = 1; |
157 | | | |
158 | | | while (!feof($diff)) { |
159 | | | // Get the first line of this range |
160 | | | sscanf($line, "@@ -%d", $oline); |
161 | | | |
162 | | | $line = substr($line, strpos($line, "+")); |
163 | | | sscanf($line, "+%d", $nline); |
164 | | | |
165 | | | if ($all) { |
166 | | | while ($curoline < $oline || $curnline < $nline) { |
167 | | | $listing[$index]["rev1diffclass"] = "diff"; |
168 | | | $listing[$index]["rev2diffclass"] = "diff"; |
169 | | | |
170 | | | if ($curoline < $oline) { |
171 | | | $nl = fgets($ofile); |
172 | | | |
173 | | | $line = rtrim($nl); |
174 | | | if ($ent) $line = replaceEntities($line, $rep); |
175 | | | |
176 | | | $listing[$index]["rev1line"] = hardspace($line); |
177 | | | |
178 | | | $curoline++; |
179 | | | } else { |
180 | | | $listing[$index]["rev1line"] = " "; |
181 | | | } |
182 | | | |
183 | | | if ($curnline < $nline) { |
184 | | | $nl = fgets($nfile); |
185 | | | |
186 | | | $line = rtrim($nl); |
187 | | | if ($ent) $line = replaceEntities($line, $rep); |
188 | | | |
189 | | | $listing[$index]["rev2line"] = hardspace($line); |
190 | | | $curnline++; |
191 | | | } else { |
192 | | | $listing[$index]["rev2line"] = " "; |
193 | | | } |
194 | | | |
195 | | | $listing[$index]["rev1lineno"] = 0; |
196 | | | $listing[$index]["rev2lineno"] = 0; |
197 | | | |
198 | | | $index++; |
199 | | | } |
200 | | | |
201 | | | } else { |
202 | | | // Output the line numbers |
203 | | | $listing[$index]["rev1lineno"] = $oline; |
204 | | | $listing[$index]["rev2lineno"] = $nline; |
205 | | | $index++; |
206 | | | } |
207 | | | |
208 | | | $fin = false; |
209 | | | while (!feof($diff) && !$fin) { |
210 | | | $line = fgets($diff); |
211 | | | if ($line === false || strncmp($line, "@@", 2) == 0) { |
212 | | | $fin = true; |
213 | | | } else { |
214 | | | $listing[$index]["rev1lineno"] = 0; |
215 | | | $listing[$index]["rev2lineno"] = 0; |
216 | | | |
217 | | | $mod = $line{0}; |
218 | | | |
219 | | | $line = rtrim(substr($line, 1)); |
220 | | | if ($ent) $line = replaceEntities($line, $rep); |
221 | | | |
222 | | | if (strip_tags($line) == '') $line = ' '; |
223 | | | $listing[$index]["rev1line"] = hardspace($line); |
224 | | | |
225 | | | $text = hardspace($line); |
226 | | | |
227 | | | switch ($mod) { |
228 | | | case "-": |
229 | | | $listing[$index]["rev1diffclass"] = "diffdeleted"; |
230 | | | $listing[$index]["rev2diffclass"] = "diff"; |
231 | | | |
232 | | | $listing[$index]["rev1line"] = $text; |
233 | | | $listing[$index]["rev2line"] = " "; |
234 | | | |
235 | | | if ($all) { |
236 | | | fgets($ofile); |
237 | | | $curoline++; |
238 | | | } |
239 | | | |
240 | | | break; |
241 | | | |
242 | | | case "+": |
243 | | | // Try to mark "changed" line sensibly |
244 | | | if (!empty($listing[$index-1]) && empty($listing[$index-1]["rev1lineno"]) && @$listing[$index-1]["rev1diffclass"] == "diffdeleted" && @$listing[$index-1]["rev2diffclass"] == "diff") { |
245 | | | $i = $index - 1; |
246 | | | while (!empty($listing[$i-1]) && empty($listing[$i-1]["rev1lineno"]) && $listing[$i-1]["rev1diffclass"] == "diffdeleted" && $listing[$i-1]["rev2diffclass"] == "diff") { |
247 | | | $i--; |
248 | | | } |
249 | | | |
250 | | | $listing[$i]["rev1diffclass"] = "diffchanged"; |
251 | | | $listing[$i]["rev2diffclass"] = "diffchanged"; |
252 | | | $listing[$i]["rev2line"] = $text; |
253 | | | |
254 | | | if ($all) { |
255 | | | fgets($nfile); |
256 | | | $curnline++; |
257 | | | } |
258 | | | |
259 | | | // Don't increment the current index count |
260 | | | $index--; |
261 | | | |
262 | | | } else { |
263 | | | $listing[$index]["rev1diffclass"] = "diff"; |
264 | | | $listing[$index]["rev2diffclass"] = "diffadded"; |
265 | | | |
266 | | | $listing[$index]["rev1line"] = " "; |
267 | | | $listing[$index]["rev2line"] = $text; |
268 | | | |
269 | | | if ($all) { |
270 | | | fgets($nfile); |
271 | | | $curnline++; |
272 | | | } |
273 | | | } |
274 | | | break; |
275 | | | |
276 | | | default: |
277 | | | $listing[$index]["rev1diffclass"] = "diff"; |
278 | | | $listing[$index]["rev2diffclass"] = "diff"; |
279 | | | |
280 | | | $listing[$index]["rev1line"] = $text; |
281 | | | $listing[$index]["rev2line"] = $text; |
282 | | | |
283 | | | if ($all) { |
284 | | | fgets($ofile); |
285 | | | fgets($nfile); |
286 | | | $curoline++; |
287 | | | $curnline++; |
288 | | | } |
289 | | | |
290 | | | break; |
291 | | | } |
292 | | | } |
293 | | | |
294 | | | if (!$fin) { |
295 | | | $index++; |
296 | | | } |
297 | | | } |
298 | | | } |
299 | | | |
300 | | | // Output the rest of the files |
301 | | | if ($all) { |
302 | | | while (!feof($ofile) || !feof($nfile)) { |
303 | | | $listing[$index]["rev1diffclass"] = "diff"; |
304 | | | $listing[$index]["rev2diffclass"] = "diff"; |
305 | | | |
306 | | | $line = rtrim(fgets($ofile)); |
307 | | | if ($ent) $line = replaceEntities($line, $rep); |
308 | | | |
309 | | | if (!feof($ofile)) { |
310 | | | $listing[$index]["rev1line"] = hardspace($line); |
311 | | | } else { |
312 | | | $listing[$index]["rev1line"] = " "; |
313 | | | } |
314 | | | |
315 | | | $line = rtrim(fgets($nfile)); |
316 | | | if ($ent) $line = replaceEntities(rtrim(fgets($nfile)), $rep); |
317 | | | |
318 | | | if (!feof($nfile)) { |
319 | | | $listing[$index]["rev2line"] = hardspace($line); |
320 | | | } else { |
321 | | | $listing[$index]["rev2line"] = " "; |
322 | | | } |
323 | | | |
324 | | | $listing[$index]["rev1lineno"] = 0; |
325 | | | $listing[$index]["rev2lineno"] = 0; |
326 | | | |
327 | | | $index++; |
328 | | | } |
329 | | | } |
330 | | | |
331 | | | fclose($pipes[1]); |
332 | | | |
333 | | | while (!feof($pipes[2])) { |
334 | | | $error .= fgets($pipes[2]); |
335 | | | } |
336 | | | |
337 | | | $error = toOutputEncoding(trim($error)); |
338 | | | |
339 | | | if (!empty($error)) $error = "<p>".$lang['BADCMD'].": <code>".$cmd."</code></p><p>".nl2br($error)."</p>"; |
340 | | | |
341 | | | fclose($pipes[2]); |
342 | | | |
343 | | | proc_close($resource); |
344 | | | |
345 | | | } else { |
346 | | | $error = "<p>".$lang['BADCMD'].": <code>".$cmd."</code></p>"; |
347 | | | } |
348 | | | |
349 | | | if (!empty($error)) { |
350 | | | echo $error; |
351 | | | |
352 | | | if (is_resource($resource)) { |
353 | | | fclose($pipes[0]); |
354 | | | fclose($pipes[1]); |
355 | | | fclose($pipes[2]); |
356 | | | |
357 | | | proc_close($resource); |
358 | | | } |
359 | | | exit; |
360 | | | } |
361 | | | |
362 | | | if ($all) { |
363 | | | fclose($ofile); |
364 | | | fclose($nfile); |
365 | | | } |
366 | | | |
367 | | | // Remove our temporary files |
368 | | | @unlink($oldtname); |
369 | | | @unlink($newtname); |
370 | | | |
371 | | | } else { |
372 | | | $vars["noprev"] = 1; |
373 | | | $url = $config->getURL($rep, $path, "file"); |
374 | | | $vars["filedetaillink"] = "<a href=\"${url}rev=$rev\">${lang["SHOWENTIREFILE"]}.</a>"; |
375 | | | } |
376 | | | |
377 | | | $vars["version"] = $version; |
378 | | | |
379 | | | if (!$rep->hasReadAccess($path, false)) { |
380 | | | $vars["noaccess"] = true; |
381 | | | } |
382 | | | |
383 | | | parseTemplate($rep->getTemplatePath()."header.tmpl", $vars, $listing); |
384 | | | parseTemplate($rep->getTemplatePath()."diff.tmpl", $vars, $listing); |
385 | | | parseTemplate($rep->getTemplatePath()."footer.tmpl", $vars, $listing); |