97b6f432bcd9cb94359f3b762af69cbfc596cedc
bernd First strike: list reposito...

bernd authored 12 years ago

1) <?php
2) require_role(ROLE_SYSTEMUSER);
3) 
4) $data_dir = realpath( dirname(__FILE__).'/../data/' );
5) $config_file = $data_dir.'/gitolite-admin/conf/webinterface.conf';
6) $config_dir = $data_dir.'/gitolite-admin/conf/webinterface';
7) $key_dir = $data_dir.'/gitolite-admin/keydir';
8) DEBUG("gitolite-data_dir: ".$data_dir);
9) $git_wrapper = $data_dir . '/git-wrapper.sh';
10) 
11) 
12) function check_env() 
13) {
14)   global $git_wrapper, $data_dir, $config_file, $config_dir, $key_dir;
15)   if (!is_executable($git_wrapper)) {
16)     system_failure("git_wrapper.sh is not executable: {$git_wrapper}");
17)   }
18)   if (! (is_file($data_dir.'/sshkey') && is_file($data_dir.'/sshkey.pub'))) {
19)     system_failure("SSH-key not found. Please setup the gitolite-module correctly. Run ./data/initialize.sh");
20)   }
21)   if (! is_dir($data_dir.'/gitolite-admin')) {
22)     system_failure("Repository gitolite-admin ot found. Initial checkout must be made manually. Run ./data/initialize.sh");
23)   }
24)   if (! is_dir($config_dir)) {
25)     system_failure("gitolite-admin repository is not prepared.");
26)   }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

27)   if (! (is_dir($key_dir) && is_writeable($config_file))) {
bernd First strike: list reposito...

bernd authored 12 years ago

28)     system_failure("Repository gitolite-admin is corrupted or webinterface.conf is not writeable.");
29)   }
30) }
31) 
32) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

33) function validate_name($name) {
34)   return (preg_match('/^[[:alnum:]][[:alnum:]._-]*$/', $name));
35) }
36) 
37) 
bernd First strike: list reposito...

bernd authored 12 years ago

38) function git_wrapper($commandline)
39) {
40)   global $git_wrapper, $data_dir;
41) 
42)   $command = $git_wrapper.' '.$commandline;
43)   $output = array();
44)   $retval = 0;
45)   DEBUG($command);
46)   exec($command, $output, $retval);
47)   DEBUG($output);
48)   DEBUG($retval);
49)   if ($retval > 0) {
50)     system_failure('Interner Fehler!');
51)     // FIXME: Hier sollte auf jeden Fall ein Logging angeworfen werden!
52)   }
53) }
54) 
55) function refresh_gitolite() 
56) {
57)   check_env();
58)   git_wrapper('pull');
59) }
60) 
61) 
62) 
63) function list_repos() 
64) {
65)   global $config_file, $config_dir;
66)   $username = $_SESSION['userinfo']['username'];
67)   $userconfig = $config_dir . '/' . $username . '.conf';
68)   DEBUG("using config file ".$userconfig);
69)   if (! is_file($userconfig)) {
70)     DEBUG("user-config does not exist");
71)     return array();
72)   }
73) 
74)   $repos = array();
75)   $lines = file($userconfig);
76)   $current_repo = NULL;
bernd First draft of gitolite-mod...

bernd authored 12 years ago

77)   $current_repo_users = array();
bernd First strike: list reposito...

bernd authored 12 years ago

78)   foreach ($lines as $line) {
79)     $m = array();
80)     if (preg_match('_^[ \t]*repo ([^]]+)_', $line, $m) != 0) {
bernd First draft of gitolite-mod...

bernd authored 12 years ago

81)       if ($current_repo) {
82)         $repos[$current_repo] = $current_repo_users;
83)       }
bernd First strike: list reposito...

bernd authored 12 years ago

84)       DEBUG("found repo ".$m[1]);
bernd First draft of gitolite-mod...

bernd authored 12 years ago

85)       $current_repo = chop($m[1]);
86)       $current_repo_users = array();
87)     } else if (preg_match('/^\s*(R|RW|RW+)\s*=\s*([[:alnum:]][[:alnum:]._-]*)\s*/', $line, $m) != 0) {
88)       DEBUG("found access rule: ".$m[1]." for ".$m[2]);
89)       $current_repo_users[chop($m[2])] = chop($m[1]);
bernd First strike: list reposito...

bernd authored 12 years ago

90)     }
91)   }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

92)   if ($current_repo) {
93)     $repos[$current_repo] = $current_repo_users;
94)   }
bernd First strike: list reposito...

bernd authored 12 years ago

95)   DEBUG($repos);
96)   return $repos;
97) }
98) 
99) 
100) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

101) function list_users() {
102)   global $config_file, $config_dir;
103)   $username = $_SESSION['userinfo']['username'];
104)   $userconfig = $config_dir . '/' . $username . '.conf';
105)   DEBUG("using config file ".$userconfig);
106)   if (! is_file($userconfig)) {
107)     DEBUG("user-config does not exist");
108)     return array();
109)   }
110)   
111)   $lines = file($userconfig);
112)   $users = array();
113)   foreach ($lines as $line) {
114)     $m = array();
115)     if (preg_match('_# user ([^]]+)_', $line, $m) != 0) {
116)       $users[] = chop($m[1]);
117)     }
118)     if (preg_match('_^\s*repo .*_', $line) != 0) {
119)       break;
120)     }
121)   }
122)   DEBUG($users);
123)   return $users;
124) }
bernd First strike: list reposito...

bernd authored 12 years ago

125) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

126) function get_pubkey($handle) {
127)   global $key_dir;
128)   if (! validate_name($handle)) {
129)     return '';
130)   }
131)   $keyfile = $key_dir.'/'.$handle.'.pub';
132)   if (! file_exists($keyfile)) {
133)     return '';
134)   }
135)   return file_get_contents($keyfile);
136) }
137) 
138) 
139) 
140) function newkey($pubkey, $handle)
bernd First strike: list reposito...

bernd authored 12 years ago

141) {
bernd First draft of gitolite-mod...

bernd authored 12 years ago

142)   global $key_dir, $config_dir;
143)   $username = $_SESSION['userinfo']['username'];
144)   
145)   $handle = $username.'-'.$handle;
146)   if (! validate_name($handle)) {
147)     system_failure("Der eingegebene Name enthält ungültige Zeichen. Bitte nur Buchstaben, Zahlen, Unterstrich, Binderstrich und Punkt benutzen.");
148)   }
149) 
150)   // FIXME: Muss man den SSH-Key auf Plausibilität prüfen? Aus Sicherheitsgründen vermutlich nicht.
151)   $keyfile = $key_dir.'/'.$handle.'.pub';
152)   file_put_contents($keyfile, $pubkey);
153)   git_wrapper('add '.$keyfile);
154) 
155)   $userconfig = $config_dir . '/' . $username . '.conf';
156)   DEBUG("using config file ".$userconfig);
157)   if (! is_file($userconfig)) {
158)     DEBUG("user-config does not exist, creating new one");
159)     file_put_contents($userconfig, '# user '.$handle."\n");
160)   } elseif (in_array($handle, list_users())) {
161)     # user ist schon eingetragen, nur neuer Key
162)   } else {
163)     $content = file_get_contents($userconfig);
164)     file_put_contents($userconfig, "# user {$handle}\n".$content);
165)   }
166)   git_wrapper('add '.$userconfig);
bernd First strike: list reposito...

bernd authored 12 years ago

167)   
bernd First draft of gitolite-mod...

bernd authored 12 years ago

168)   git_wrapper('commit --allow-empty -m "added new key for '.$handle.'"');
169)   git_wrapper('push');
bernd First strike: list reposito...

bernd authored 12 years ago

170) }
171) 
172) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

173) function delete_key($handle)
174) {
175)   global $key_dir, $config_dir;
176)   $username = $_SESSION['userinfo']['username'];
bernd First strike: list reposito...

bernd authored 12 years ago

177)