5c30f40b9aca3e560011e0a54883b51472e61a8e
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) 
bernd Fürs erste feature-complete

bernd authored 12 years ago

12) 
bernd First strike: list reposito...

bernd authored 12 years ago

13) function check_env() 
14) {
15)   global $git_wrapper, $data_dir, $config_file, $config_dir, $key_dir;
16)   if (!is_executable($git_wrapper)) {
17)     system_failure("git_wrapper.sh is not executable: {$git_wrapper}");
18)   }
19)   if (! (is_file($data_dir.'/sshkey') && is_file($data_dir.'/sshkey.pub'))) {
20)     system_failure("SSH-key not found. Please setup the gitolite-module correctly. Run ./data/initialize.sh");
21)   }
22)   if (! is_dir($data_dir.'/gitolite-admin')) {
23)     system_failure("Repository gitolite-admin ot found. Initial checkout must be made manually. Run ./data/initialize.sh");
24)   }
25)   if (! is_dir($config_dir)) {
26)     system_failure("gitolite-admin repository is not prepared.");
27)   }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

38) function get_git_url($repo) {
39)   $remote = git_wrapper('remote --verbose');
40)   DEBUG('gitolite-admin repo: '.$remote[0]);
41)   $url = preg_replace('#^.*\s+(\S+):gitolite-admin.*#', '$1', $remote[0]);
42)   DEBUG('URL: '.$url);
43)   return $url.':'.$repo;
44) }
45) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

46) 
bernd First strike: list reposito...

bernd authored 12 years ago

47) function git_wrapper($commandline)
48) {
49)   global $git_wrapper, $data_dir;
50) 
51)   $command = $git_wrapper.' '.$commandline;
52)   $output = array();
53)   $retval = 0;
54)   DEBUG($command);
55)   exec($command, $output, $retval);
56)   DEBUG($output);
57)   DEBUG($retval);
58)   if ($retval > 0) {
59)     system_failure('Interner Fehler!');
60)     // FIXME: Hier sollte auf jeden Fall ein Logging angeworfen werden!
61)   }
bernd show URL

bernd authored 12 years ago

62)   return $output;
bernd First strike: list reposito...

bernd authored 12 years ago

63) }
64) 
65) function refresh_gitolite() 
66) {
67)   check_env();
68)   git_wrapper('pull');
69) }
70) 
71) 
72) 
73) function list_repos() 
74) {
75)   global $config_file, $config_dir;
76)   $username = $_SESSION['userinfo']['username'];
77)   $userconfig = $config_dir . '/' . $username . '.conf';
78)   DEBUG("using config file ".$userconfig);
79)   if (! is_file($userconfig)) {
80)     DEBUG("user-config does not exist");
81)     return array();
82)   }
83) 
84)   $repos = array();
85)   $lines = file($userconfig);
86)   $current_repo = NULL;
bernd First draft of gitolite-mod...

bernd authored 12 years ago

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

bernd authored 12 years ago

88)   foreach ($lines as $line) {
bernd Fürs erste feature-complete

bernd authored 12 years ago

89)     DEBUG("LINE: ".$line);
bernd First strike: list reposito...

bernd authored 12 years ago

90)     $m = array();
bernd Fürs erste feature-complete

bernd authored 12 years ago

91)     if (preg_match('_^\s*repo (\S+)\s*$_', $line, $m) != 0) {
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("found repo ".$m[1]);
bernd First draft of gitolite-mod...

bernd authored 12 years ago

96)       $current_repo = chop($m[1]);
97)       $current_repo_users = array();
bernd Fürs erste feature-complete

bernd authored 12 years ago

98)     } else if (preg_match('/^\s*(R|RW|RW\+)\s*=\s*([[:alnum:]][[:alnum:]._-]*)\s*$/', $line, $m) != 0) {
bernd First draft of gitolite-mod...

bernd authored 12 years ago

99)       DEBUG("found access rule: ".$m[1]." for ".$m[2]);
100)       $current_repo_users[chop($m[2])] = chop($m[1]);
bernd First strike: list reposito...

bernd authored 12 years ago

101)     }
102)   }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

103)   if ($current_repo) {
104)     $repos[$current_repo] = $current_repo_users;
105)   }
bernd First strike: list reposito...

bernd authored 12 years ago

106)   DEBUG($repos);
107)   return $repos;
108) }
109) 
110) 
111) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

112) function list_users() {
113)   global $config_file, $config_dir;
114)   $username = $_SESSION['userinfo']['username'];
115)   $userconfig = $config_dir . '/' . $username . '.conf';
116)   DEBUG("using config file ".$userconfig);
117)   if (! is_file($userconfig)) {
118)     DEBUG("user-config does not exist");
119)     return array();
120)   }
121)   
122)   $lines = file($userconfig);
123)   $users = array();
124)   foreach ($lines as $line) {
125)     $m = array();
126)     if (preg_match('_# user ([^]]+)_', $line, $m) != 0) {
127)       $users[] = chop($m[1]);
128)     }
129)     if (preg_match('_^\s*repo .*_', $line) != 0) {
130)       break;
131)     }
132)   }
133)   DEBUG($users);
134)   return $users;
135) }
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

137) function get_pubkey($handle) {
138)   global $key_dir;
139)   if (! validate_name($handle)) {
140)     return '';
141)   }
142)   $keyfile = $key_dir.'/'.$handle.'.pub';
143)   if (! file_exists($keyfile)) {
144)     return '';
145)   }
146)   return file_get_contents($keyfile);
147) }
148) 
149) 
150) 
151) function newkey($pubkey, $handle)
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

181) }
182) 
183) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

184) function delete_key($handle)
185) {
186)   global $key_dir, $config_dir;
187)   $username = $_SESSION['userinfo']['username'];
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

189)   if (! validate_name($handle)) {
190)     system_failure("Der eingegebene Name enthält ungültige Zeichen. Bitte nur Buchstaben, Zahlen, Unterstrich, Binderstrich und Punkt benutzen.");
191)   }
192)   if (!in_array($handle, list_users())) {
193)     DEBUG("key {$handle} not in");
194)     DEBUG(list_users());
195)     system_failure("Den angegebenen Key scheint es nicht zu geben");
196)   }
197) 
198)   // FIXME: Muss man den SSH-Key auf Plausibilität prüfen? Aus Sicherheitsgründen vermutlich nicht.
199)   $keyfile = $key_dir.'/'.$handle.'.pub';
200)   if (! file_exists($keyfile)) {
201)     system_failure("Der angegebene Schlüssel scheint nicht mehr vorhanden zu sein. Bitte manuelle Korrektur anfordern!");
202)   } 
203)   git_wrapper('rm '.$keyfile);
204) 
205) 
206)   $userconfig = $config_dir . '/' . $username . '.conf';
207)   DEBUG("using config file ".$userconfig);
208)   if (! is_file($userconfig)) {
209)     DEBUG("user-config does not exist, wtf?");
210)     system_failure("Es gibt für diesen Benutzer noch keine Konfiguration. Das sollte nicht sein!");
211)   } else {
212)     $content = file($userconfig);
213)     DEBUG("Old file:");
214)     DEBUG($content);
215)     $newcontent = array();
216)     foreach ($content as $line) {
217)       if (preg_match('/^# user '.$handle.'$/', $line)) {
218)         DEBUG("delete1: ".$line);
219)         continue;
220)       }
221)       if (preg_match('/^\s*(R|RW|RW+)\s*=\s*'.$handle.'\s*$/', $line)) {
222)         DEBUG("delete2: ".$line);
223)         continue;
224)       }
225)       $newcontent[] = $line;
226)     }
227)     DEBUG("Modified file:");
228)     DEBUG($newcontent);
229)     file_put_contents($userconfig, implode('', $newcontent));
230)   }
231)   git_wrapper('add '.$userconfig);
232)  
233)   git_wrapper('commit -m "deleted key for '.$handle.'"');
234)   git_wrapper('push');
235) 
236) 
237) }