small test trying to dev filters
[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                                 $repo_base = gwvpmini_getConfigVal("repodir");\r
70                                 $cmd = "git --git-dir=\"$repo_base/$name.git\" log -1 2>&1";\r
71                                 error_log("CMD: $cmd");\r
72                                 system("$cmd");\r
73                                 echo "</td>";
74                                 echo "</tr>";
75                         }
76                         echo "</table>";
77                 }
78         }
79         return true;
80 }
81
82
83 function gwvpmini_GitLogProvider()\r
84 {\r
85         /*\r
86          * The home page provider will:\r
87         * 1) show the last 10 commits for every repository - though, excluding private repos\r
88         * 2) if loged in, show the last commit on any repo's the user owns\r
89         *\r
90         * So i need a table thats going to list "writes" by user - as in POST writes but only\r
91         * put that info into the stats (doesnt exist) db if the repo is publically readable\r
92         *\r
93         * Or... should we instead just list every repo?\r
94         */
95         
96         global $BASE_URL;
97         
98         echo "<h2>Repo Activity</h2>";
99         if(gwvpmini_isLoggedIn()) {\r
100                 $repos = gwvpmini_GetOwnedRepos($_SESSION["username"]);
101                 if(!$repos) {
102                         echo "You currently own no repos<br>";  
103                 } else {
104                         echo "<h2>Your Repos</h2>";
105                         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Repo Log</th></tr>";
106                         foreach($repos as $repo) {
107                                 $name = $repo["name"];
108                                 $desc = $repo["desc"];
109                                 echo "<tr><td><a href=\"$BASE_URL/view/$name\">$name</a></td><td>$desc</td>";
110                                 echo "<td>";
111                                 $repo_base = gwvpmini_getConfigVal("repodir");
112                                 $cmd = "git --git-dir=\"$repo_base/$name.git\" log -1 2>&1";
113                                 error_log("CMD: $cmd");
114                                 system("$cmd");
115                                 echo "</td>";
116                                 echo "</tr>";
117                         }
118                         echo "</table>";
119                 }\r
120         }
121 }
122
123 function gwvpmini_GitCreateRepoForm()
124 {
125         global $BASE_URL;
126         
127         echo "<form method=\"post\" action=\"$BASE_URL/repos/create\">";
128         echo "<table border=\"1\">";
129         echo "<tr><th colspan=\"2\">Create Repo</th></tr>";
130         echo "<tr><th>Repo Name</th><td><input type=\"text\" name=\"reponame\"></td></tr>";
131         echo "<tr><th>Repo Description</th><td><input type=\"text\" name=\"repodesc\"></td></tr>";
132         echo "<tr><th>Read Permissions</th><td>";
133         echo "<select name=\"perms\">";
134         echo "<option value=\"perms-public\">Anyone Can Read</option>";
135         echo "<option value=\"perms-registered\">Must be Registered To Read</option>";
136         echo "<option value=\"perms-onlywrite\">Only Writers can Read</option>";
137         echo "</select>";
138         echo "</td></tr>";
139         echo "<tr><td colspan=\"2\"><input type=\"submit\" name=\"Create\" value=\"Create\"></td></tr>";
140         echo "</table>";
141         echo "</form>";
142 }
143
144 function gwvpmini_RepoCreate()
145 {
146         
147         global $BASE_URL;
148         
149         // TODO: check the stuff out
150         // first reponame
151         $inputcheck = true;
152
153         // remove a .git at the end if it was input
154         $_REQUEST["reponame"] = preg_replace("/\.git$/", "", $_REQUEST["reponame"]);
155         
156         // check for valid chars
157         $replcheck = preg_replace("/[a-zA-Z0-9_\-\.]/", "", $_REQUEST["reponame"]);
158         if(strlen($replcheck)>0) {
159                 $inputcheck = false;
160                 $inputcheckerror = "Repo name contains invalid characters, repos can only contain a-z, A-Z, 0-9, _, - and .";
161         }
162         
163         if(!$inputcheck) {
164                 gwvpmini_SendMessage("error", "$inputcheckerror");\r
165                 header("Location: $BASE_URL/repos");
166         } else  if(gwvpmini_isLoggedIn()) {
167                 //gwvpmini_createGitRepo($name, $ownerid, $desc, $bundle=null, $defaultperms=0)
168                 if(gwvpmini_HaveRepo($_REQUEST["reponame"])) {
169                         gwvpmini_SendMessage("error", "Repo ".$_REQUEST["reponame"]." already exists");\r
170                         header("Location: $BASE_URL/repos");
171                 } else {
172                         gwvpmini_createGitRepo($_REQUEST["reponame"], $_SESSION["id"], $_REQUEST["repodesc"], $_REQUEST["perms"]);
173                         gwvpmini_SendMessage("info", "Repo ".$_REQUEST["reponame"]." has been created");
174                         header("Location: $BASE_URL/repos");
175                 }
176         } else {
177                 gwvpmini_SendMessage("info", "Must be logged in to create repo");
178                 header("Location: $BASE_URL/repos");
179         }
180 }
181
182 function gwvpmini_HaveRepo($reponame)
183 {
184         $repo_base = gwvpmini_getConfigVal("repodir");
185         
186         if(file_exists("$repo_base/$reponame.git")) return true;
187 }
188
189
190 function gwvpmini_RemoveRepo($rid)
191 {
192         $repo_base = gwvpmini_getConfigVal("repodir");
193         
194         $repdet = gwvpmini_getRepo(null, null, $rid);
195         
196         $rname = $repdet["name"];
197         
198         error_log("FROM PANTS:".print_r($repdet,true)." ----------- ".print_r($rname, true));
199         
200         if($repdet != false && $rname != "") {
201                 if(file_exists("$repo_base/$rname.git")) {
202                         // recursive remove - frightening
203                         if(gwvpmini_RecursiveDelete("$repo_base/$rname.git")) {
204                                 gwvpmini_RemoveRepoDB($rid);
205                         }
206                 }
207         } return false;
208 }
209
210 function gwvpmini_RecursiveDelete($fpath)
211 {
212         error_log("RECURSEDETELE: ".$fpath);
213         if(is_file($fpath)){\r
214                 return @unlink($fpath);\r
215         }\r
216         elseif(is_dir($fpath)){\r
217                 $scan = glob(rtrim($fpath,'/').'/*');\r
218                 foreach($scan as $index=>$path){\r
219                         gwvpmini_RecursiveDelete($path);\r
220                 }\r
221                 return @rmdir($fpath);\r
222         }
223 }
224         
225 ?>