added hash_base system (base refs gets stored in the _SESSION)
[phpgitweb.git] / htdocs / lib / CommitLoader.class.php
1 <?php
2 /* CommitLoader.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
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 3 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, see <http://www.gnu.org/licenses/>. 
17  */
18
19 class CommitLoader {
20         public static $commit_id, $commit_base_id;
21         private $project;
22         
23         public static function parse($log_page = false) {
24                 if(array_key_exists('h', $_GET) && Validation::validate_hash($_GET['h']))
25                         $commit_id = $_GET['h'];
26                 else
27                         $commit_id = "all";
28                 
29                 if(array_key_exists('hb', $_GET) && Validation::validate_hash($_GET['hb']))
30                         $commit_base_id = $_GET['hb'];
31                 else if(!$log_page && $_SESSION['hb'])
32                         $commit_base_id = $_SESSION['hb'];
33                 else
34                         $commit_base_id = $commit_id;
35                 
36                 if($log_page)
37                         $_SESSION['hb'] = $commit_base_id;
38                 
39                 ContentProvider::overall_set('commit_base_id', $commit_base_id);
40                 ContentProvider::overall_set('commit_id', $commit_id);
41                 
42                 self::$commit_id = $commit_id;
43                 self::$commit_base_id = $commit_base_id;
44                 return array('id' => $commit_id, 'base_id' => $commit_base_id);
45         }
46         
47         public function __construct($project) {
48                 $this->project = $project;
49         }
50         
51         public function load($id = null) {
52                 if(!$id && self::$commit_id) 
53                         $id = self::$commit_id;
54                 else if(!Validation::validate_hash($id)) {
55                         trigger_error("Invalid hash parameter", E_USER_ERROR);
56                         return false;
57                 }
58                 if(strtolower($id) == 'all')
59                         $id = null;
60                 $commit = GitCommand::get_commit($this->project['path'], $id);
61                 return $commit;
62         }
63         
64         
65 }