637cd93429e340caaceacb72ba620a411e8c5b2d
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) 
38) 
bernd First strike: list reposito...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

82)     if (preg_match('_^\s*repo (\S+)\s*$_', $line, $m) != 0) {
bernd First draft of gitolite-mod...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

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

bernd authored 12 years ago

92)     }
93)   }
bernd First draft of gitolite-mod...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

172) }
173) 
174) 
bernd First draft of gitolite-mod...

bernd authored 12 years ago

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

bernd authored 12 years ago

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

bernd authored 12 years ago

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