4 global $DB_CONNECTION;
\r
5 $DB_CONNECTION = false;
\r
8 global $db_url, $db_type;
\r
9 error_log("in include for database, $db_type, $db_name");
12 function gwvpmini_DBExists()
14 global $WEB_ROOT_FS, $BASE_URL, $data_directory, $db_type, $db_name;
16 // oh this isnt working. poo.
17 error_log("checking for $db_name, $db_type");
19 if($db_type == "sqlite") {
20 if(file_exists($db_name)) {
25 error_log("no exists");
31 function gwvpmini_getUser($username=null, $email=null, $id=null)
\r
33 $conn = gwvpmini_ConnectDB();
\r
35 if($username != null) {
\r
36 $res = $conn->query("select * from users where user_username='$username'");
\r
37 } else if($email != null) {
\r
38 $res = $conn->query("select * from users where user_email='$email'");
\r
39 } else if($id != null) {
\r
40 $res = $conn->query("select * from users where users_id='$id'");
\r
41 } else return false;
\r
44 foreach($res as $u_res) {
\r
45 $returns["id"] = $u_res["user_id"];
\r
46 $returns["fullname"] = $u_res["user_full_name"];
\r
47 $returns["password"] = $u_res["user_password"];
\r
48 $returns["username"] = $u_res["user_username"];
\r
49 $returns["email"] = $u_res["user_email"];
\r
50 $returns["desc"] = $u_res["user_desc"];
\r
51 $returns["status"] = $u_res["user_status"];
\r
58 function gwvpmini_ConnectDB()
\r
60 global $WEB_ROOT_FS, $BASE_URL, $data_directory, $db_type, $db_name, $DB_CONNECTION;
\r
62 // first check if $DB_CONNECTION IS live
\r
63 error_log("in connection $db_type, $db_name");
\r
65 if($DB_CONNECTION != false) return $DB_CONNECTION;
\r
67 if($db_type == "sqlite") {
\r
69 if(!file_exists($db_name)) {
\r
70 error_log("$db_name does not exist - problem");
71 // TODO: NEED A SETUP AGENT!
72 gwvpmini_dbCreateSQLiteStructure($db_name);
73 gwvpmini_setConfigVal("repodir", "$data_directory/repos");
\r
77 // and here we go with pdo.
\r
78 error_log("attmpting to open db, $db_type:$db_url");
\r
80 $DB_CONNECTION = new PDO("$db_type:$db_url");
\r
81 } catch(PDOException $exep) {
\r
82 error_log("execpt on db open");
\r
86 return $DB_CONNECTION;
\r
90 function gwvpmini_dbCreateSQLiteStructure($dbloc)
\r
93 CREATE TABLE "users" (
\r
94 "user_id" INTEGER PRIMARY KEY AUTOINCREMENT,
\r
95 "user_full_name" TEXT,
\r
96 "user_password" TEXT,
\r
97 "user_username" TEXT,
\r
104 $initialuser_admin = '
105 insert into "users" values ( null, "Administrator", "'.sha1("password").'", "admin", "admin@localhost", "the admin", "1", "0");
\r
108 $initialuser_user = '
\r
109 insert into "users" values ( null, "User", "'.sha1("password").'", "user", "user@localhost", "the user", "0", "0");
\r
113 CREATE TABLE "repos" (
\r
114 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
\r
116 "repos_description" TEXT,
\r
117 "repos_owner" INTEGER
\r
120 // this looks like null, <repoid>, <read|visible|write>, user:<uid>|group:<gid>|authed|anon
\r
121 // where authed = any authenticated user, anon = everyone (logged in, not logged in, etc)
\r
122 // read|visible|write = can clone from repo|can see repo exists and see description but not clone from it|can push to repo
\r
123 // TODO: is this sufficient? i have to think about it
\r
126 CREATE TABLE "config" (
\r
127 "config_name" TEXT,
\r
128 "config_value" TEXT
\r
132 $DB_CONNECTION = new PDO("sqlite:$dbloc");
\r
133 } catch(PDOException $exep) {
\r
134 error_log("execpt on db open");
\r
138 $DB_CONNECTION->query($usersql);
\r
139 $DB_CONNECTION->query($initialuser_admin);
140 $DB_CONNECTION->query($initialuser_user);
141 $DB_CONNECTION->query($reposql);
\r
142 $DB_CONNECTION->query($configsql);
\r
145 function gwvpmini_getConfigVal($confname)
\r
149 CREATE TABLE "config" (
\r
150 "config_name" TEXT,
\r
151 "config_value" TEXT
\r
156 $conn = gwvpmini_ConnectDB();
\r
158 $sql = "select config_value from config where config_name='$confname'";
\r
160 $res = $conn->query($sql);
\r
163 foreach($res as $val) {
\r
164 $return = $val["config_value"];
\r
170 function gwvpmini_eraseConfigVal($confname)
\r
174 CREATE TABLE "config" (
\r
175 "config_name" TEXT,
\r
176 "config_value" TEXT
\r
181 $conn = gwvpmini_ConnectDB();
\r
183 $sql = "delete from config where config_name='$confname'";
\r
185 return $conn->query($sql);
\r
188 function gwvpmini_GetRepoId($reponame)
193 CREATE TABLE "repos" (
194 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
196 "repos_description" TEXT,
197 "repos_owner" INTEGER
202 $conn = gwvpmini_ConnectDB();
204 $sql = "select repos_id from repos where repos_name='$reponame'";
206 $res = $conn->query($sql);
210 foreach($res as $row) {
211 $reval = (int)$row[0];
218 function gwvpmini_setConfigVal($confname, $confval)
\r
222 CREATE TABLE "config" (
\r
223 "config_name" TEXT,
\r
224 "config_value" TEXT
\r
228 gwvpmini_eraseConfigVal($confname);
\r
230 $conn = gwvpmini_ConnectDB();
\r
232 $sql = "insert into config values('$confname', '$confval')";
\r
234 return $conn->query($sql);
\r
237 function gwvpmini_AddRepo($name, $desc, $ownerid)
240 error_log("addrepo in db for $name, $desc, $ownerid");
241 $conn = gwvpmini_ConnectDB();
\r
243 $sql = "insert into repos values (null, '$name', '$desc', '$ownerid')";
\r
245 $conn->query($sql);
\r
248 function gwvpmini_GetUserId($username)
250 $conn = gwvpmini_ConnectDB();
\r
252 $sql = "select user_id from users where user_username='$username'";
254 error_log("userid sql $sql");
\r
256 $res = $conn->query($sql);
259 foreach($res as $row) {
266 function gwvpmini_GetOwnedRepos($username)
\r
269 * CREATE TABLE "repos" (
270 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
272 "repos_description" TEXT,
273 "repos_owner" INTEGER
277 $conn = gwvpmini_ConnectDB();
279 $uid = gwvpmini_GetUserId($username);
280 $sql = "select * from repos where repos_owner='$uid'";
281 error_log("owned repos sql $sql");
282 $res = $conn->query($sql);
285 foreach($res as $row) {
286 $id = $row["repos_id"];
287 $retval[$id]["name"] = $row["repos_name"];
288 $retval[$id]["desc"] = $row["repos_description"];
289 error_log(print_r($row, true));
292 error_log(print_r($retval, true));
\r
296 function gwvpmini_userLevel($id)
298 $conn = gwvpmini_ConnectDB();
300 $sql = "select user_level from users where user_id='$id'";
302 $res = $conn->query($sql);
306 foreach($res as $row) {
313 function gwvpmini_GetUsers()
315 $conn = gwvpmini_ConnectDB();
318 * CREATE TABLE "users" (
319 "user_id" INTEGER PRIMARY KEY AUTOINCREMENT,
320 "user_full_name" TEXT,
321 "user_password" TEXT,
322 "user_username" TEXT,
330 $sql = "select * from users";
332 $res = $conn->query($sql);
335 foreach($res as $row) {
336 $id = $row["user_id"];
337 $retval[$id]["fullname"] = $row["user_full_name"];
338 $retval[$id]["username"] = $row["user_username"];
339 $retval[$id]["email"] = $row["user_email"];
340 $retval[$id]["desc"] = $row["user_desc"];
341 $retval[$id]["level"] = $row["user_level"];
342 $retval[$id]["status"] = $row["user_status"];