add gen_id function and fixed link fails
[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 function gen_id($len = 6)
30 {
31 $r = '';
32 for($i=0; $i<$len; $i++) {
33 $r .= chr(rand(0, 25) + ord('a'));
34 }
35 return $r;
36 }
37 if(isset($_GET['p'])){
38 if(file_exists("/home/git/".$_GET['p'])){
39 $hash = shell_exec("git --git-dir=/home/git/".$_GET["p"]." rev-list -n 1 --pretty='format:%h' --header master | grep '^[0-9a-f]*$'");
40 $con= shell_exec('git --git-dir=/home/git/'.$_GET["p"].' rev-list --oneline --header master | wc -l | sed "s/[ \t]//g"');
41 if(!$hash){
42 echo "No Download available";
43 }else{
44 $name=substr($_GET["p"],0,strlen($_GET["p"])-4);
45 echo "<a href='http://git.nexus-irc.de/git_download.php?build=".$_GET["p"]."'>".$name."-".substr($hash, 0, -1)." (tar.gz)</a>";
46 }
47 }else{
48 echo "unknown project";
49 }
50 }elseif(isset($_GET['build'])){
51
52 if(file_exists("/home/git/".$_GET['build'])){
53 $name=substr($_GET['build'],0,strlen($_GET['build'])-4);
54 if(isset($_GET['h'])){
55 if(strtolower($_GET['h']) == "head") {
56 $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);
57 }else{
58 $hash = substr($_GET['h'], 0, 7);
59 }
60 }else{
61 $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);
62 }
63 $id = sha1(gen_id());
64 $b = shell_exec("cd /usr/share/gitweb/dl/; git clone git://git.nexus-irc.de/".$_GET['build']);
65 $c = shell_exec("cd /usr/share/gitweb/dl/".$name."/; git reset ".$_GET['h']);
66 $con= shell_exec('git --git-dir=/home/git/'.$_GET['build'].' rev-list --oneline --header master | wc -l | sed "s/[ \t]//g"');
67 if($_GET['build'] != "gitweb.git") {
68 scandir_replace("/usr/share/gitweb/dl/".$name,"git-".substr($con, 0, -1)."-".$hash);
69 }
70 $c = shell_exec("cd /usr/share/gitweb/dl; tar czfv ".$id." ".$name);
71 $d = shell_exec("rm -r /usr/share/gitweb/dl/".$name);
72 header("Content-Type: application/x-gzip");
73 header("Content-Disposition: attachment; filename=".$name."-".$hash.".tar.gz");
74 readfile("/usr/share/gitweb/dl/".$id);
75 unlink("/usr/share/gitweb/dl/".$id);
76
77 }else{
78 echo "unknown project";
79 }
80 }else{
81 echo "<table class='project_list'>";
82 echo "<tr><th>Project</th><th>Download</th></tr>";
83 $a = file_get_contents("http://git.nexus-irc.de/index.cgi?a=project_index2");
84 $b = explode("\n",$a);
85 foreach($b as $id => $v) {
86 if($v == ""){ }else{
87 $hash = shell_exec("git --git-dir=/home/git/".$v." rev-list -n 1 --pretty='format:%h' --header master | grep '^[0-9a-f]*$'");
88 $con= shell_exec('git --git-dir=/home/git/'.$v.' rev-list --oneline --header master | wc -l | sed "s/[ \t]//g"');
89 if(!$hash){
90 echo "<tr><td>".$v."</td><td>No Download available</td></tr>";
91 }else{
92 $name=substr($v,0,strlen($v)-4);
93 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>";
94 }
95 }
96 }
97 echo "</table>";
98 }
99 ?>