basic permissions work
[gwvp-mini.git] / gwvpmini / gwvpmini_gitrepo.php
1 <?php
2 global $HOME_PAGE_PROVIDERS;\r
3
4
5 $CALL_ME_FUNCTIONS["repoadmin"] = "gwvpmini_RepoCallMe";
6 $HOME_PAGE_PROVIDERS["gitlog"] = "gwvpmini_GitLogProvider";\r
7 \r
8 \r
9 // the home_page_provders bit is an array\r
10 \r
11 $MENU_ITEMS["10repos"]["text"] = "Repos";\r
12 $MENU_ITEMS["10repos"]["link"] = "$BASE_URL/repos";
13
14
15 function gwvpmini_RepoCallMe()\r
16 {\r
17 \r
18         error_log("in repoadmin callme - err?");
19         error_log(print_r($_REQUEST, true));\r
20         if(isset($_REQUEST["q"])) {
21                 error_log("in repoadmin callme, for Q");\r
22                 $query = $_REQUEST["q"];\r
23                 $qspl = explode("/", $query);\r
24                 if(isset($qspl[0])) {\r
25                         if($qspl[0] == "repos") {
26                                 error_log("in repos call");
27                                 if(isset($qspl[1])) {
28                                         if($qspl[1] == "create") {
29                                                 return "gwvpmini_RepoCreate";
30                                         } else {
31                                                 return "gwvpmini_RepoMainPage";
32                                         }
33                                 } else {\r
34                                         error_log("i got here, where next?");\r
35                                         return "gwvpmini_RepoMainPage";
36                                 }\r
37                         } else return false;\r
38                 }\r
39                 else return false;\r
40         }\r
41 \r
42         return false;\r
43 }\r
44
45
46 function gwvpmini_RepoMainPage()\r
47 {\r
48         gwvpmini_goMainPage("gwvpmini_RepoMainPageBody");\r
49 }\r
50 \r
51
52 function gwvpmini_RepoMainPageBody()
53 {
54         global $BASE_URL;
55         
56         if(gwvpmini_isLoggedIn()) {
57                 gwvpmini_GitCreateRepoForm();
58                 $repos = gwvpmini_GetOwnedRepos($_SESSION["username"]);
59                 if(!$repos) {
60                         echo "You currently own no repos<br>";  
61                 } else {
62                         echo "<h2>Your Repos</h2>";
63                         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Last Log</th></tr>";
64                         foreach($repos as $repo) {
65                                 $name = $repo["name"];
66                                 $desc = $repo["desc"];
67                                 echo "<tr><td><a href=\"$BASE_URL/view/$name\">$name</a></td><td>$desc</td>";
68                                 echo "<td>";\r
69                                 error_log("CMD: $cmd");
70                                 //system("$cmd");
71                                 $fls = popen($cmd, "r");
72                                 $tks = "";
73                                 if($fls !== false) while(!feof($fls)) {
74                                         $tks .= fread($fls,1024);
75                                 }
76                                 
77                                 if($tks == "") {
78                                         echo "No Log Info Yet";
79                                 } else echo $tks;
80                                 echo "</td>";
81                                 echo "</tr>";
82                         }
83                         echo "</table>";
84                 }
85         }
86         return true;
87 }
88
89
90 function gwvpmini_GitLogProvider()\r
91 {\r
92         /*\r
93          * The home page provider will:\r
94         * 1) show the last 10 commits for every repository - though, excluding private repos\r
95         * 2) if loged in, show the last commit on any repo's the user owns\r
96         *\r
97         * So i need a table thats going to list "writes" by user - as in POST writes but only\r
98         * put that info into the stats (doesnt exist) db if the repo is publically readable\r
99         *\r
100         * Or... should we instead just list every repo?\r
101         */
102         
103         global $BASE_URL;
104         
105         echo "<h2>Repo Activity</h2>";
106         if(gwvpmini_isLoggedIn()) {\r
107                 $repos = gwvpmini_GetOwnedRepos($_SESSION["username"]);
108                 if(!$repos) {
109                         echo "You currently own no repos<br>";  
110                 } else {
111                         echo "<h2>Your Repos</h2>";
112                         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Repo Log</th></tr>";
113                         foreach($repos as $repo) {
114                                 $name = $repo["name"];
115                                 $desc = $repo["desc"];
116                                 echo "<tr><td><a href=\"$BASE_URL/view/$name\">$name</a></td><td>$desc</td>";
117                                 echo "<td>";
118                                 $repo_base = gwvpmini_getConfigVal("repodir");
119                                 $cmd = "git --git-dir=\"$repo_base/$name.git\" log -1 2> /dev/null";
120                                 error_log("CMD: $cmd");
121                                 //system("$cmd");
122                                 $fls = popen($cmd, "r");
123                                 $tks = "";
124                                 if($fls !== false) while(!feof($fls)) {
125                                         $tks .= fread($fls,1024);
126                                 }
127                                 
128                                 if($tks == "") {
129                                         echo "No Log Info Yet";
130                                 } else echo $tks;
131                                 echo "</td>";
132                                 echo "</tr>";
133                         }
134                         echo "</table>";
135                 }\r
136         }
137 }
138
139 function gwvpmini_GitCreateRepoForm()
140 {
141         global $BASE_URL;
142         
143         echo "<form method=\"post\" action=\"$BASE_URL/repos/create\">";
144         echo "<table border=\"1\">";
145         echo "<tr><th colspan=\"2\">Create Repo</th></tr>";
146         echo "<tr><th>Repo Name</th><td><input type=\"text\" name=\"reponame\"></td></tr>";
147         echo "<tr><th>Repo Description</th><td><input type=\"text\" name=\"repodesc\"></td></tr>";
148         echo "<tr><th>Read Permissions</th><td>";
149         echo "<select name=\"perms\">";
150         echo "<option value=\"perms-public\">Anyone Can Read</option>";
151         echo "<option value=\"perms-registered\">Must be Registered To Read</option>";
152         echo "<option value=\"perms-onlywrite\">Only Writers can Read</option>";
153         echo "</select>";
154         echo "</td></tr>";
155         echo "<tr><td colspan=\"2\"><input type=\"submit\" name=\"Create\" value=\"Create\"></td></tr>";
156         echo "</table>";
157         echo "</form>";
158 }
159
160 function gwvpmini_RepoCreate()
161 {
162         
163         global $BASE_URL;
164         
165         // TODO: check the stuff out
166         // first reponame
167         $inputcheck = true;
168
169         // remove a .git at the end if it was input
170         $_REQUEST["reponame"] = preg_replace("/\.git$/", "", $_REQUEST["reponame"]);
171         
172         // check for valid chars
173         $replcheck = preg_replace("/[a-zA-Z0-9_\-\.]/", "", $_REQUEST["reponame"]);
174         if(strlen($replcheck)>0) {
175                 $inputcheck = false;
176                 $inputcheckerror = "Repo name contains invalid characters, repos can only contain a-z, A-Z, 0-9, _, - and .";
177         }
178         
179         if(!$inputcheck) {
180                 gwvpmini_SendMessage("error", "$inputcheckerror");\r
181                 header("Location: $BASE_URL/repos");
182         } else  if(gwvpmini_isLoggedIn()) {
183                 //gwvpmini_createGitRepo($name, $ownerid, $desc, $bundle=null, $defaultperms=0)
184                 if(gwvpmini_HaveRepo($_REQUEST["reponame"])) {
185                         gwvpmini_SendMessage("error", "Repo ".$_REQUEST["reponame"]." already exists");\r
186                         header("Location: $BASE_URL/repos");
187                 } else {
188                         gwvpmini_createGitRepo($_REQUEST["reponame"], $_SESSION["id"], $_REQUEST["repodesc"], $_REQUEST["perms"]);
189                         gwvpmini_SendMessage("info", "Repo ".$_REQUEST["reponame"]." has been created");
190                         header("Location: $BASE_URL/repos");
191                 }
192         } else {
193                 gwvpmini_SendMessage("info", "Must be logged in to create repo");
194                 header("Location: $BASE_URL/repos");
195         }
196 }
197
198 function gwvpmini_HaveRepo($reponame)
199 {
200         $repo_base = gwvpmini_getConfigVal("repodir");
201         
202         if(file_exists("$repo_base/$reponame.git")) return true;
203 }
204
205
206 function gwvpmini_RemoveRepo($rid)
207 {
208         $repo_base = gwvpmini_getConfigVal("repodir");
209         
210         $repdet = gwvpmini_getRepo(null, null, $rid);
211         
212         $rname = $repdet["name"];
213         
214         error_log("FROM PANTS:".print_r($repdet,true)." ----------- ".print_r($rname, true));
215         
216         if($repdet != false && $rname != "") {
217                 if(file_exists("$repo_base/$rname.git")) {
218                         // recursive remove - frightening
219                         if(gwvpmini_RecursiveDelete("$repo_base/$rname.git")) {
220                                 gwvpmini_RemoveRepoDB($rid);
221                         }
222                 }
223         } return false;
224 }
225
226 function gwvpmini_RecursiveDelete($fpath)
227 {
228         error_log("RECURSEDETELE: ".$fpath);
229         if(is_file($fpath)){\r
230                 return @unlink($fpath);\r
231         }\r
232         elseif(is_dir($fpath)){\r
233                 $scan = glob(rtrim($fpath,'/').'/*');\r
234                 foreach($scan as $index=>$path){\r
235                         gwvpmini_RecursiveDelete($path);\r
236                 }\r
237                 return @rmdir($fpath);\r
238         }
239 }
240         
241 ?>