|
| 1 | +<?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 | +// blame.php |
| 22 | +// |
| 23 | +// Show the blame information 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 | +$vars['action'] = $lang['BLAME']; |
| 32 | + |
| 33 | +// Make sure that we have a repository |
| 34 | +if ($rep) { |
| 35 | + $svnrep = new SVNRepository($rep); |
| 36 | + |
| 37 | + // If there's no revision info, go to the lastest revision for this path |
| 38 | + $history = $svnrep->getLog($path, 'HEAD', 1, false, 2, ($path == '/') ? '' : $peg); |
| 39 | + if (!$history) { |
| 40 | + unset($vars['error']); |
| 41 | + $history = $svnrep->getLog($path, '', '', false, 2, ($path == '/') ? '' : $peg); |
| 42 | + if (!$history) { |
| 43 | + header('HTTP/1.x 404 Not Found', true, 404); |
| 44 | + $vars['error'] = $lang['NOPATH']; |
| 45 | + } |
| 46 | + } |
| 47 | + $youngest = ($history && isset($history->entries[0])) ? $history->entries[0]->rev : false; |
| 48 | + |
| 49 | + if (empty($rev)) { |
| 50 | + $rev = $youngest; |
| 51 | + } else { |
| 52 | + $history = $svnrep->getLog($path, $rev, '', false, 2, $peg); |
| 53 | + if (!$history) { |
| 54 | + header('HTTP/1.x 404 Not Found', true, 404); |
| 55 | + $vars['error'] = $lang['NOPATH']; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if ($path{0} != '/') { |
| 60 | + $ppath = '/'.$path; |
| 61 | + } else { |
| 62 | + $ppath = $path; |
| 63 | + } |
| 64 | + |
| 65 | + // Find the parent path (or the whole path if it's already a directory) |
| 66 | + $pos = strrpos($ppath, '/'); |
| 67 | + $parent = substr($ppath, 0, $pos + 1); |
| 68 | + |
| 69 | + $vars['rev'] = $rev; |
| 70 | + $vars['peg'] = $peg; |
| 71 | + $vars['path'] = escape($ppath); |
| 72 | + |
| 73 | + if (isset($history->entries[0])) { |
| 74 | + $vars['log'] = xml_entities($history->entries[0]->msg); |
| 75 | + $vars['date'] = $history->entries[0]->date; |
| 76 | + $vars['age'] = datetimeFormatDuration(time() - strtotime($history->entries[0]->date)); |
| 77 | + $vars['author'] = $history->entries[0]->author; |
| 78 | + } |
| 79 | + |
| 80 | + createPathLinks($rep, $ppath, $passrev, $peg); |
| 81 | + $passRevString = createRevAndPegString($rev, $peg); |
| 82 | + |
| 83 | + if ($rev != $youngest) { |
| 84 | + $vars['goyoungesturl'] = $config->getURL($rep, $path, 'blame').createRevAndPegString('', $peg); |
| 85 | + $vars['goyoungestlink'] = '<a href="'.$vars['goyoungesturl'].'"'.($youngest ? ' title="'.$lang['REV'].' '.$youngest.'"' : '').'>'.$lang['GOYOUNGEST'].'</a>'; |
| 86 | + } |
| 87 | + |
| 88 | + $revurl = $config->getURL($rep, $path, 'blame'); |
| 89 | + if ($rev < $youngest) { |
| 90 | + $history2 = $svnrep->getLog($path, $rev, $youngest, false, 2, $peg); |
| 91 | + if (isset($history2->entries[1])) { |
| 92 | + $nextRev = $history2->entries[1]->rev; |
| 93 | + if ($nextRev != $youngest) { |
| 94 | + $vars['nextrev'] = $nextRev; |
| 95 | + $vars['nextrevurl'] = $revurl.createRevAndPegString($nextRev, $peg); |
| 96 | + } |
| 97 | + } |
| 98 | + unset($vars['error']); |
| 99 | + } |
| 100 | + |
| 101 | + if (isset($history->entries[1])) { |
| 102 | + $prevRev = $history->entries[1]->rev; |
| 103 | + $prevPath = $history->entries[1]->path; |
| 104 | + $vars['prevrev'] = $prevRev; |
| 105 | + $vars['prevrevurl'] = $revurl.createRevAndPegString($prevRev, $peg); |
| 106 | + } |
| 107 | + |
| 108 | + $vars['revurl'] = $config->getURL($rep, $path, 'revision').$passRevString; |
| 109 | + $vars['revlink'] = '<a href="'.$vars['revurl'].'">'.$lang['LASTMOD'].'</a>'; |
| 110 | + |
| 111 | + $vars['logurl'] = $config->getURL($rep, $path, 'log').$passRevString; |
| 112 | + $vars['loglink'] = '<a href="'.$vars['logurl'].'">'.$lang['VIEWLOG'].'</a>'; |
| 113 | + |
| 114 | + $vars['filedetailurl'] = $config->getURL($rep, $path, 'file').$passRevString; |
| 115 | + $vars['filedetaillink'] = '<a href="'.$vars['filedetailurl'].'">'.$lang['FILEDETAIL'].'</a>'; |
| 116 | + |
| 117 | + if ($history == null || count($history->entries) > 1) { |
| 118 | + $vars['diffurl'] = $config->getURL($rep, $path, 'diff').$passRevString; |
| 119 | + $vars['difflink'] = '<a href="'.$vars['diffurl'].'">'.$lang['DIFFPREV'].'</a>'; |
| 120 | + } |
| 121 | + |
| 122 | + if ($rep->isRssEnabled()) { |
| 123 | + $vars['rssurl'] = $config->getURL($rep, $path, 'rss').createRevAndPegString('', $peg); |
| 124 | + $vars['rsslink'] = '<a href="'.$vars['rssurl'].'">'.$lang['RSSFEED'].'</a>'; |
| 125 | + } |
| 126 | + |
| 127 | + // Check for binary file type before grabbing blame information. |
| 128 | + $svnMimeType = $svnrep->getProperty($path, 'svn:mime-type', $rev, $peg); |
| 129 | + |
| 130 | + if (!$rep->getIgnoreSvnMimeTypes() && preg_match('~application/*~', $svnMimeType)) { |
| 131 | + $vars['warning'] = 'Cannot display blame info for binary file. (svn:mime-type = '.$svnMimeType.')'; |
| 132 | + $vars['javascript'] = ''; |
| 133 | + } else { |
| 134 | + // Get the contents of the file |
| 135 | + $tfname = tempnamWithCheck($config->getTempDir(), ''); |
| 136 | + $highlighted = $svnrep->getFileContents($path, $tfname, $rev, $peg, '', 'line'); |
| 137 | + |
| 138 | + if ($file = fopen($tfname, 'r')) { |
| 139 | + // Get the blame info |
| 140 | + $tbname = tempnamWithCheck($config->getTempDir(), ''); |
| 141 | + |
| 142 | + $svnrep->getBlameDetails($path, $tbname, $rev, $peg); |
| 143 | + |
| 144 | + if ($blame = fopen($tbname, 'r')) { |
| 145 | + // Create an array of version/author/line |
| 146 | + |
| 147 | + $index = 0; |
| 148 | + $seen_rev = array(); |
| 149 | + $last_rev = ''; |
| 150 | + $row_class = ''; |
| 151 | + |
| 152 | + while (!feof($blame) && !feof($file)) { |
| 153 | + $blameline = fgets($blame); |
| 154 | + |
| 155 | + if ($blameline != '') { |
| 156 | + list($revision, $author, $remainder) = sscanf($blameline, '%d %s %s'); |
| 157 | + $empty = !$remainder; |
| 158 | + |
| 159 | + $listing[$index]['lineno'] = $index + 1; |
| 160 | + |
| 161 | + if ($last_rev != $revision) { |
| 162 | + $url = $config->getURL($rep, $path, 'blame'); |
| 163 | + $listing[$index]['revision'] = '<a id="l'.$index.'-rev" class="blame-revision" href="'.$url.'rev='.$revision.'&peg='.$rev.'">'.$revision.'</a>'; |
| 164 | + $seen_rev[$revision] = 1; |
| 165 | + $row_class = ($row_class == 'light') ? 'dark' : 'light'; |
| 166 | + $listing[$index]['author'] = $author; |
| 167 | + } else { |
| 168 | + $listing[$index]['revision'] = ''; |
| 169 | + $listing[$index]['author'] = ''; |
| 170 | + } |
| 171 | + |
| 172 | + $listing[$index]['row_class'] = $row_class; |
| 173 | + $last_rev = $revision; |
| 174 | + |
| 175 | + $line = rtrim(fgets($file)); |
| 176 | + if (!$highlighted) |
| 177 | + $line = escape(toOutputEncoding($line)); |
| 178 | + $listing[$index]['line'] = ($empty) ? ' ' : wrapInCodeTagIfNecessary($line); |
| 179 | + $index++; |
| 180 | + } |
| 181 | + } |
| 182 | + fclose($blame); |
| 183 | + } |
| 184 | + fclose($file); |
| 185 | + @unlink($tbname); |
| 186 | + } |
| 187 | + @unlink($tfname); |
| 188 | + |
| 189 | + // Build the necessary JavaScript as an array of lines, then join them with \n |
| 190 | + $javascript = array(); |
| 191 | + $javascript[] = '<script type="text/javascript" src="'.$locwebsvnhttp.'/javascript/blame-popup.js"></script>'; |
| 192 | + $javascript[] = '<script type="text/javascript">'; |
| 193 | + $javascript[] = '/* <![CDATA[ */'; |
| 194 | + $javascript[] = 'var rev = new Array();'; |
| 195 | + |
| 196 | + ksort($seen_rev); // Sort revisions in descending order by key |
| 197 | + if (empty($peg)) |
| 198 | + $peg = $rev; |
| 199 | + if (!isset($vars['warning'])) { |
| 200 | + foreach ($seen_rev as $key => $val) { |
| 201 | + $history = $svnrep->getLog($path, $key, $key, false, 1, $peg); |
| 202 | + if ($history) { |
| 203 | + $javascript[] = 'rev['.$key.'] = \'<div class="date">'.$history->curEntry->date.'</div><div class="msg">'.addslashes(preg_replace('/\n/', ' ', $history->curEntry->msg)).'</div>\';'; |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + $javascript[] = '/* ]]> */'; |
| 208 | + $javascript[] = '</script>'; |
| 209 | + $vars['javascript'] = implode("\n", $javascript); |
| 210 | + } |
| 211 | + |
| 212 | + if (!$rep->hasReadAccess($path, false)) { |
| 213 | + $vars['error'] = $lang['NOACCESS']; |
| 214 | + checkSendingAuthHeader($rep); |
| 215 | + } |
| 216 | + |
| 217 | +} else { |
| 218 | + header('HTTP/1.x 404 Not Found', true, 404); |
| 219 | +} |
| 220 | + |
| 221 | +renderTemplate('blame'); |
0 commit comments