ff794853e40a108bce1c456e2f6ad0702b3fb655
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 Setze Berechtigung für gitw...

bernd authored 12 years ago

91)     if (preg_match('/^(\S+) "[^"]+" = "([^"]+)"$/', $line, $m) != 0) {
92)       if (!array_key_exists($m[1], $repos)) {
93)         $repos[$m[1]] = array('users' => NULL, 'description' => '');
94)       }
95)       DEBUG("found description: {$m[1]} = \"{$m[2]}\"");
96)       $repos[$m[1]]['description'] = $m[2];
97)     } elseif (preg_match('_^\s*repo (\S+)\s*$_', $line, $m) != 0) {
98)       if (!array_key_exists($m[1], $repos)) {
99)         $repos[$m[1]] = array('users' => NULL, 'description' => '');
100)       }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

101)       if ($current_repo) {
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

102)         $repos[$current_repo]['users'] = $current_repo_users;
bernd First draft of gitolite-mod...

bernd authored 12 years ago

103)       }
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

107)     } 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

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

bernd authored 12 years ago

110)     }
111)   }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

112)   if ($current_repo) {
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

113)     $repos[$current_repo]['users'] = $current_repo_users;
bernd First draft of gitolite-mod...

bernd authored 12 years ago

114)   }
bernd First strike: list reposito...

bernd authored 12 years ago

115)   DEBUG($repos);
116)   return $repos;
117) }
118) 
119) 
120) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

146) function get_pubkey($handle) {
147)   global $key_dir;
148)   if (! validate_name($handle)) {
149)     return '';
150)   }
151)   $keyfile = $key_dir.'/'.$handle.'.pub';
152)   if (! file_exists($keyfile)) {
153)     return '';
154)   }
155)   return file_get_contents($keyfile);
156) }
157) 
158) 
159) 
160) function newkey($pubkey, $handle)
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

190) }
191) 
192) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

193) function delete_key($handle)
194) {
195)   global $key_dir, $config_dir;
196)   $username = $_SESSION['userinfo']['username'];
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

247) 
248) 
249) function remove_repo_from_array($data, $repo) {
250)   DEBUG("Request to remove repo »{$repo}«...");
251)   $inside = false;
252)   $outdata = array();
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

253)   $blank = true;
bernd Fürs erste feature-complete

bernd authored 12 years ago

254)   foreach ($data as $line) {
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

255)     if ($blank && chop($line) == '') {
256)       continue;
257)     }
258)     $blank = (chop($line) == '');
bernd Fürs erste feature-complete

bernd authored 12 years ago

259)     $m = array();
260)     if (preg_match('_^\s*repo (\S+)\s*$_', $line, $m) != 0) {
261)       $inside = ($m[1] == $repo);
262)     }
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

263)     if (! $inside && ! preg_match('/^'.$repo.'\s.*/', $line)) {
bernd Fürs erste feature-complete

bernd authored 12 years ago

264)       $outdata[] = $line;
265)     }
266)   }
267)   DEBUG($outdata);
268)   return $outdata;
269) }
270) 
271) 
272) function repo_exists_globally($repo) 
273) {
274)   global $config_dir;
275)   $files = scandir($config_dir);
276)   foreach ($files as $f) {
277)     if (is_file(realpath($config_dir.'/'.$f))) {
278)       $data = file(realpath($config_dir.'/'.$f));
279)       foreach ($data as $line) {
280)         if (preg_match('/^\s*repo '.$repo.'\s*$/', $line) != 0) {
281)           return true;
282)         }
283)       }
284)     }
285)   }
286)   return false;
287) }
288) 
289) 
290) function delete_repo($repo) 
291) {
292)   $repos = list_repos();
293)   if (!array_key_exists($repo, $repos)) {
294)     system_failure("Ein solches Repository existiert nicht!");
295)   }
296)   
297)   global $config_dir;
298)   $username = $_SESSION['userinfo']['username'];
299)   $userconfig = $config_dir . '/' . $username . '.conf';
300)   DEBUG("using config file ".$userconfig);
301)   $data = file($userconfig);
302)   $data = remove_repo_from_array($data, $repo);
303)   file_put_contents($userconfig, implode('', $data));
304)   git_wrapper('add '.$userconfig);
305)   
306)   git_wrapper('commit --allow-empty -m "deleted repo '.$repo.'"');
307)   git_wrapper('push');
308) }
309) 
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

310) function save_repo($repo, $permissions, $description) 
bernd Fürs erste feature-complete

bernd authored 12 years ago

311) {
312)   if (!validate_name($repo)) {
313)     system_failure("Der gewählte name entspricht nicht den Konventionen!");
314)   }
315)   if (!array_key_exists($repo, list_repos()) && repo_exists_globally($repo)) {
316)     system_failure("Der gewählte Name existiert bereits auf diesem Server. Bitte wählen Sie einen spezifischeren Namen.");
317)   } 
318)   global $config_dir;
319)   $username = $_SESSION['userinfo']['username'];
320)   $userconfig = $config_dir . '/' . $username . '.conf';
321)   DEBUG("using config file ".$userconfig);
322)   $data = array();
323)   if (! is_file($userconfig)) {
324)     DEBUG("user-config does not exist, creating new one");
325)   } else {
326)     $data = file($userconfig);
327)   }
328) 
329)   $repos = list_repos();
330)   if (array_key_exists($repo, $repos)) {
331)     $data = remove_repo_from_array($data, $repo);
332)   }
333) 
334)   $data[] = "\n";
bernd Setze Berechtigung für gitw...

bernd authored 12 years ago

335)   if ($description) {
336)     $realname = $_SESSION['userinfo']['name'];
337)     $data[] = "{$repo} \"{$realname}\" = \"{$description}\"\n";
338)   }