fixed coding fails from last commit
[gitweb.git] / git_download.php
1 <?php
2 $args = explode(";", $_SERVER["QUERY_STRING"]);
3 foreach($args as $arg) {
4 $arg = explode("=", $arg, 2);
5 $_GET[$arg[0]] = $arg[1];
6 }
7
8 function scandir_replace($folder,$arg){
9 $out = array();
10 if($content = opendir($folder)){
11 while(false !== ($file = readdir($content))){
12 if(is_dir("$folder/$file") && $file != "." && $file != ".." && $file != ".git"){
13 scandir_replace("$folder/$file",$arg);
14 } elseif($file != "." && $file != ".." && $file != ".git"){
15 $out[] = "$folder/$file";
16 }
17 }
18 closedir($content);
19 }
20 foreach($out as $id => $v) {
21 chmod($v,0777);
22 $maincode=file_get_contents($v);
23 $maincode = str_replace('$__GIT__', '"'.$arg.'"', $maincode); // replace $__GIT__ with the git version
24 $fp = fopen($v, 'w');
25 fwrite($fp, $maincode);
26 fclose($fp);
27 }
28 }
29
30 if(isset($_GET['p'])){
31 if(file_exists("/home/git/".$_GET['p'])){
32 $hash = shell_exec("git --git-dir=/home/git/".$_GET["p"]." rev-list -n 1 --pretty='format:%h' --header master | grep '^[0-9a-f]*$'");
33 $con= shell_exec('git --git-dir=/home/git/'.$_GET["p"].' rev-list --oneline --header master | wc -l | sed "s/[ \t]//g"');
34 if(!$hash){
35 echo "No Download available";
36 }else{
37 $name=substr($_GET["p"],0,strlen($_GET["p"])-4);
38 echo "<a href='http://git.nexus-irc.de/git_download.php?build=".$_GET["p"]."'>".$name."-".substr($hash, 0, -1)." (tar.gz)</a>";
39 }
40 }else{
41 echo "unknown project";
42 }
43 }elseif(isset($_GET['build'])){
44
45 if(file_exists("/home/git/".$_GET['build'])){
46 $name=substr($_GET['build'],0,strlen($_GET['build'])-4);
47 if(isset($_GET['h'])){
48 if(strtolower($_GET['h']) == "head") {
49 $hash = substr(shell_exec("git --git-dir=/home/git/".$_GET['build']." rev-list -n 1 --pretty='format:%h' --header master | grep '^[0-9a-f]*$'"), 0, -1);
50 }else{
51 $hash = substr($_GET['h'], 0, 7);
52 }
53 }else{
54 $hash = substr(shell_exec("git --git-dir=/home/git/".$_GET['build']." rev-list -n 1 --pretty='format:%h' --header master | grep '^[0-9a-f]*$'"), 0, 7);
55 }
56 $id = sha1(time());
57 $b = shell_exec("cd /usr/share/gitweb/dl/; git clone git://git.nexus-irc.de/".$_GET['build']);
58 $c = shell_exec("cd /usr/share/gitweb/dl/".$name."/; git reset ".$_GET['h']);
59 $con= shell_exec('git --git-dir=/home/git/'.$_GET['build'].' rev-list --oneline --header master | wc -l | sed "s/[ \t]//g"');
60 if($_GET['build'] != "gitweb.git") {
61 scandir_replace("/usr/share/gitweb/dl/".$name,"git-".substr($con, 0, -1)."-".$hash);
62 }
63 $c = shell_exec("cd /usr/share/gitweb/dl; tar czfv ".$id." ".$name);
64 $d = shell_exec("rm -r /usr/share/gitweb/dl/".$name);
65 header("Content-Type: application/x-gzip");
66 header("Content-Disposition: attachment; filename=".$name."-".$hash.".tar.gz");
67 readfile("/usr/share/gitweb/dl/".$id);
68 unlink("/usr/share/gitweb/dl/".$id);
69
70 }else{
71 echo "unknown project";
72 }
73 }else{
74 echo "<table class='project_list'>";
75 echo "<tr><th>Project</th><th>Download</th></tr>";
76 $a = file_get_contents("http://git.nexus-irc.de/index.cgi?a=project_index2");
77 $b = explode("\n",$a);
78 foreach($b as $id => $v) {
79 if($v == ""){ }else{
80 $hash = shell_exec("git --git-dir=/home/git/".$v." rev-list -n 1 --pretty='format:%h' --header master | grep '^[0-9a-f]*$'");
81 $con= shell_exec('git --git-dir=/home/git/'.$v.' rev-list --oneline --header master | wc -l | sed "s/[ \t]//g"');
82 if(!$hash){
83 echo "<tr><td>".$v."</td><td>No Download available</td></tr>";
84 }else{
85 $name=substr($v,0,strlen($v)-4);
86 echo "<tr><td>".$v."</td><td><a href='http://git.nexus-irc.de/git_download.php?build=".$v."'>".$name."-".substr($hash, 0, -1)." (tar.gz)</a></td></tr>";
87 }
88 }
89 }
90 echo "</table>";
91 }
92 ?>