Don't assign return value of new by reference.
[viewgit.git] / index.php
blob3a0d980338930de4197591b5198572faa0555ad3
1 <?php
2 /** @file
3 * The main "controller" file of ViewGit.
5 * All requests come to this file. You can think of it as the controller in the
6 * Model-View-Controller pattern. It reads config, processes user input,
7 * fetches required data using git commandline, and finally passes the data to
8 * templates to be shown to the user.
9 */
10 error_reporting(E_ALL | E_STRICT);
12 require_once('inc/config.php');
13 require_once('inc/functions.php');
14 require_once('inc/plugins.php');
16 // Include all plugins
17 foreach (glob('plugins/*/main.php') as $plugin) {
18 require_once($plugin);
20 $parts = explode('/', $plugin);
21 $name = $parts[1];
23 $classname = "${name}plugin";
24 $inst = new $classname;
27 $old_error_handler = set_error_handler('vg_error_handler');
29 // Adjust error_reporting based on config.
30 if (!$conf['debug']) {
31 error_reporting(E_ALL ^ E_NOTICE);
34 if (isset($conf['auth_lib'])){
35 require_once("inc/auth_{$conf['auth_lib']}.php");
36 auth_check();
39 if (isset($conf['projects_glob'])) {
40 foreach ($conf['projects_glob'] as $glob) {
41 foreach (glob($glob) as $path) {
42 // Get the last part of the path before .git
43 $name = preg_replace(array('#/?\.git$#', '#^.*/#'), array('', ''), $path);
45 // Workaround against name collisions; proj, proj1, proj2, ...
46 $i = '';
47 while (in_array($name . $i, array_keys($conf['projects']))) {
48 @$i++;
50 $name = $name . $i;
51 $conf['projects'][$name] = array('repo' => $path);
56 $action = 'index';
57 $template = 'index';
58 $page['title'] = 'ViewGit';
60 if (isset($_REQUEST['a'])) {
61 $action = strtolower($_REQUEST['a']);
63 $page['action'] = $action;
66 * index - list of projects
68 if ($action === 'index') {
69 $template = 'index';
70 $page['title'] = 'List of projects - ViewGit';
72 foreach (array_keys($conf['projects']) as $p) {
73 $page['projects'][] = get_project_info($p);
78 * archive - send a tree as an archive to client
79 * @param p project
80 * @param h tree hash
81 * @param t type, "targz" or "zip"
82 * @param n OPTIONAL name suggestion
84 elseif ($action === 'archive') {
85 $project = validate_project($_REQUEST['p']);
86 $tree = validate_hash($_REQUEST['h']);
87 $type = $_REQUEST['t'];
89 $basename = "$project-tree-". substr($tree, 0, 7);
90 if (isset($_REQUEST['n'])) {
91 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
94 if ($type === 'targz') {
95 header("Content-Type: application/x-tar-gz");
96 header("Content-Transfer-Encoding: binary");
97 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
98 run_git_passthru($project, "archive --format=tar $tree |gzip");
100 elseif ($type === 'zip') {
101 header("Content-Type: application/x-zip");
102 header("Content-Transfer-Encoding: binary");
103 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
104 run_git_passthru($project, "archive --format=zip $tree");
106 else {
107 die('Invalid archive type requested');
110 die();
114 * blob - send a blob to browser with filename suggestion
115 * @param p project
116 * @param h blob hash
117 * @param n filename
119 elseif ($action === 'blob') {
120 $project = validate_project($_REQUEST['p']);
121 $hash = validate_hash($_REQUEST['h']);
122 $name = $_REQUEST['n'];
124 header('Content-type: application/octet-stream');
125 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
127 run_git_passthru($project, "cat-file blob $hash");
128 die();
132 * co - git checkout. These requests come from mod_rewrite, see the .htaccess file.
133 * @param p project
134 * @param r path
136 elseif ($action === 'co') {
137 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
139 // For debugging
140 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
142 // eg. info/refs, HEAD
143 $p = validate_project($_REQUEST['p']); // project
144 $r = $_REQUEST['r']; // path
146 $gitdir = $conf['projects'][$p]['repo'];
147 $filename = $gitdir .'/'. $r;
149 // make sure the request is legit (no reading of other files besides those under git projects)
150 if ($r === 'HEAD' || $r === 'info/refs' || preg_match('!^objects/info/(packs|http-alternates|alternates)$!', $r) > 0 || preg_match('!^objects/[0-9a-f]{2}/[0-9a-f]{38}$!', $r) > 0 || preg_match('!^objects/pack/pack-[0-9a-f]{40}\.(idx|pack)$!', $r)) {
151 if (file_exists($filename)) {
152 debug('OK, sending');
153 readfile($filename);
154 } else {
155 debug('Not found');
156 header('HTTP/1.0 404 Not Found');
158 } else {
159 debug("Denied");
162 die();
166 * commit - view commit information
167 * @param p project
168 * @param h commit hash
170 elseif ($action === 'commit') {
171 $template = 'commit';
172 $page['project'] = validate_project($_REQUEST['p']);
173 $page['title'] = "$page[project] - Commit - ViewGit";
174 $page['commit_id'] = validate_hash($_REQUEST['h']);
175 $page['subtitle'] = "Commit ". substr($page['commit_id'], 0, 6);
177 $info = git_get_commit_info($page['project'], $page['commit_id']);
179 $page['author_name'] = $info['author_name'];
180 $page['author_mail'] = $info['author_mail'];
181 $page['author_datetime'] = gmstrftime($conf['datetime_full'], $info['author_utcstamp']);
182 $page['author_datetime_local'] = gmstrftime($conf['datetime_full'], $info['author_stamp']) .' '. $info['author_timezone'];
183 $page['committer_name'] = $info['committer_name'];
184 $page['committer_mail'] = $info['committer_mail'];
185 $page['committer_datetime'] = gmstrftime($conf['datetime_full'], $info['committer_utcstamp']);
186 $page['committer_datetime_local'] = gmstrftime($conf['datetime_full'], $info['committer_stamp']) .' '. $info['committer_timezone'];
187 $page['tree_id'] = $info['tree'];
188 $page['parents'] = $info['parents'];
189 $page['message'] = $info['message'];
190 $page['message_firstline'] = $info['message_firstline'];
191 $page['message_full'] = $info['message_full'];
196 * commitdiff - view diff of a commit
197 * @param p project
198 * @param h commit hash
200 elseif ($action === 'commitdiff') {
201 $template = 'commitdiff';
202 $page['project'] = validate_project($_REQUEST['p']);
203 $page['title'] = "$page[project] - Commitdiff - ViewGit";
204 $hash = validate_hash($_REQUEST['h']);
205 $page['commit_id'] = $hash;
206 $page['subtitle'] = "Commitdiff ". substr($page['commit_id'], 0, 6);
208 $info = git_get_commit_info($page['project'], $hash);
210 $page['tree_id'] = $info['tree'];
212 $page['message'] = $info['message'];
213 $page['message_firstline'] = $info['message_firstline'];
214 $page['message_full'] = $info['message_full'];
215 $page['author_name'] = $info['author_name'];
216 $page['author_mail'] = $info['author_mail'];
217 $page['author_datetime'] = gmstrftime($conf['datetime'], $info['author_utcstamp']);
219 $text = git_diff($page['project'], "$hash^", $hash);
220 list($page['files'], $page['diffdata']) = format_diff($text);
221 //$page['diffdata'] = format_diff($text);
224 elseif ($action === 'patch') {
225 $project = validate_project($_REQUEST['p']);
226 $hash = validate_hash($_REQUEST['h']);
227 $filename = "$project-". substr($hash, 0, 7) .".patch";
229 //header("Content-Type: text/x-diff");
230 header("Content-Type: application/octet-stream");
231 header("Content-Transfer-Encoding: binary");
232 // TODO git-style filename
233 header("Content-Disposition: attachment; filename=\"$filename\";");
235 run_git_passthru($project, "format-patch --stdout $hash^..$hash");
236 die();
240 * rss-log - RSS feed of project changes
241 * @param p project
243 elseif ($action === 'rss-log') {
244 $page['project'] = validate_project($_REQUEST['p']);
246 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
248 $page['rss_title'] = "Log for $page[project]";
249 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
250 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
251 $page['rss_pubDate'] = rss_pubdate(time());
252 $page['rss_ttl'] = $conf['rss_ttl'];
254 $page['rss_items'] = array();
256 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
258 $revs = git_get_rev_list($page['project'], 0, $conf['rss_max_items']);
259 foreach ($revs as $rev) {
260 $info = git_get_commit_info($page['project'], $rev);
261 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
262 if ($diffstat) {
263 $info['diffstat'] = git_diffstat($page['project'], $rev);
266 $page['rss_items'][] = array(
267 'title' => rss_item_format($conf['rss_item_title'], $info),
268 'guid' => $link,
269 'link' => $link,
270 'description' => rss_item_format($conf['rss_item_description'], $info),
271 'pubdate' => rss_pubdate($info['author_utcstamp']),
275 require('templates/rss.php');
276 die();
280 * search - search project history
281 * @param p project
282 * @param h branch
283 * @param st search type: commit,grep,author,committer,pickaxe
284 * @param s string to search for
286 elseif ($action === 'search') {
287 $template = 'shortlog';
289 $page['project'] = validate_project($_REQUEST['p']);
291 $info = git_get_commit_info($page['project']);
292 $page['commit_id'] = $info['h'];
293 $page['tree_id'] = $info['tree'];
295 $branch = validate_hash($_REQUEST['h']);
296 $type = $_REQUEST['st'];
297 $string = $_REQUEST['s'];
299 $page['search_t'] = $type;
300 $page['search_s'] = $string;
302 $commits = git_search_commits($page['project'], $branch, $type, $string);
303 $shortlog = array();
304 foreach ($commits as $c) {
305 $info = git_get_commit_info($page['project'], $c);
306 $shortlog[] = array(
307 'author' => $info['author_name'],
308 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
309 'message' => $info['message'],
310 'commit_id' => $info['h'],
311 'tree' => $info['tree'],
312 'refs' => array(),
315 $page['shortlog_no_more'] = true;
316 $page['shortlog'] = $shortlog;
320 * shortlog - project shortlog entries
321 * @param p project
322 * @param h OPTIONAL commit id to start showing log from
324 elseif ($action === 'shortlog') {
325 $template = 'shortlog';
326 $page['project'] = validate_project($_REQUEST['p']);
327 $page['title'] = "$page[project] - Shortlog - ViewGit";
328 $page['subtitle'] = "Shortlog";
329 if (isset($_REQUEST['h'])) {
330 $page['ref'] = validate_hash($_REQUEST['h']);
331 } else {
332 $page['ref'] = 'HEAD';
334 if (isset($_REQUEST['pg'])) {
335 $page['pg'] = intval($_REQUEST['pg']);
336 } else {
337 $page['pg'] = 0;
340 $info = git_get_commit_info($page['project'], $page['ref']);
341 $page['commit_id'] = $info['h'];
342 $page['tree_id'] = $info['tree'];
344 $page['shortlog'] = handle_shortlog($page['project'], $page['ref'], $page['pg']);
346 elseif ($action === 'summary') {
347 $template = 'summary';
348 $page['project'] = validate_project($_REQUEST['p']);
349 $page['title'] = "$page[project] - Summary - ViewGit";
350 $page['subtitle'] = "Summary";
352 $info = git_get_commit_info($page['project']);
353 $page['commit_id'] = $info['h'];
354 $page['tree_id'] = $info['tree'];
356 $page['shortlog'] = handle_shortlog($page['project']);
358 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
359 $page['ref'] = 'HEAD';
361 $heads = git_get_heads($page['project']);
362 $page['heads'] = array();
363 foreach ($heads as $h) {
364 $info = git_get_commit_info($page['project'], $h['h']);
365 $page['heads'][] = array(
366 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
367 'h' => $h['h'],
368 'fullname' => $h['fullname'],
369 'name' => $h['name'],
373 elseif ($action === 'tags') {
374 $template = 'tags';
375 $page['project'] = validate_project($_REQUEST['p']);
376 $page['title'] = "$page[project] - Tags - ViewGit";
378 $info = git_get_commit_info($page['project']);
379 $page['commit_id'] = $info['h'];
380 $page['tree_id'] = $info['tree'];
382 $page['tags'] = handle_tags($page['project']);
385 * Shows a tree, with list of directories/files, links to them and download
386 * links to archives.
388 * @param p project
389 * @param h tree hash
390 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
391 * one denotes which commit the user navigated from)
392 * @param f OPTIONAL path the user has followed to view this tree
394 elseif ($action === 'tree') {
395 $template = 'tree';
396 $page['project'] = validate_project($_REQUEST['p']);
397 if (isset($_REQUEST['h'])) {
398 $page['tree_id'] = validate_hash($_REQUEST['h']);
401 else {
402 // TODO walk the tree
403 $page['tree_id'] = 'HEAD';
406 $page['title'] = "$page[project] - Tree - ViewGit";
408 // 'hb' optionally contains the commit_id this tree is related to
409 if (isset($_REQUEST['hb'])) {
410 $page['commit_id'] = validate_hash($_REQUEST['hb']);
412 else {
413 // for the header
414 $info = git_get_commit_info($page['project']);
415 $page['commit_id'] = $info['h'];
418 $page['path'] = '';
419 if (isset($_REQUEST['f'])) {
420 $page['path'] = $_REQUEST['f']; // TODO validate?
423 // get path info for the header
424 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
425 if (!isset($page['tree_id'])) {
426 // Take the last hash from the tree
427 if (count($page['pathinfo']) > 0) {
428 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
429 } else {
430 $page['tree_id'] = 'HEAD';
434 $page['subtitle'] = "Tree ". substr($page['tree_id'], 0, 6);
435 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
438 * View a blob as inline, embedded on the page.
439 * @param p project
440 * @param h blob hash
441 * @param hb OPTIONAL base commit
443 elseif ($action === 'viewblob') {
444 $template = 'blob';
445 $page['project'] = validate_project($_REQUEST['p']);
446 $page['hash'] = validate_hash($_REQUEST['h']);
447 $page['title'] = "$page[project] - Blob - ViewGit";
448 if (isset($_REQUEST['hb'])) {
449 $page['commit_id'] = validate_hash($_REQUEST['hb']);
451 else {
452 $page['commit_id'] = 'HEAD';
454 $page['subtitle'] = "Blob ". substr($page['hash'], 0, 6);
456 $page['path'] = '';
457 if (isset($_REQUEST['f'])) {
458 $page['path'] = $_REQUEST['f']; // TODO validate?
461 // For the header's pagenav
462 $info = git_get_commit_info($page['project'], $page['commit_id']);
463 $page['commit_id'] = $info['h'];
464 $page['tree_id'] = $info['tree'];
466 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
468 $page['data'] = join("\n", run_git($page['project'], "cat-file blob $page[hash]"));
470 // GeSHi support
471 if ($conf['geshi'] && strpos($page['path'], '.')) {
472 $old_mask = error_reporting(E_ALL ^ E_NOTICE);
473 require_once($conf['geshi_path']);
474 $parts = explode('.', $page['path']);
475 $ext = array_pop($parts);
476 $lang = Geshi::get_language_name_from_extension($ext);
477 if (strlen($lang) > 0) {
478 $geshi = new Geshi($page['data'], $lang);
479 if (is_int($conf['geshi_line_numbers'])) {
480 if ($conf['geshi_line_numbers'] == 0) {
481 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
483 else {
484 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $conf['geshi_line_numbers']);
487 $page['html_data'] = $geshi->parse_code();
489 error_reporting($old_mask);
492 elseif (in_array($action, array_keys(VGPlugin::$plugin_actions))) {
493 VGPlugin::$plugin_actions[$action]->action($action);
494 die();
496 else {
497 die('Invalid action');
500 require 'templates/header.php';
501 require "templates/$template.php";
502 require 'templates/footer.php';