Expose the theme RSS feeds
[rockboxthemes.git] / private / db.class.php
blob0f8406172e93199438e429b8b184f3bafceb257a
1 <?php
2 /***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
11 * Copyright (C) 2009 Jonas Häggqvist
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
25 * Simple DB class using sqlite and a bunch of assumptions.
27 class db {
28 private $file;
29 private $dh;
30 public function __construct($file) {
31 $this->file = $file;
32 $this->dh = @sqlite_open($file, 0666, $err);
33 if ($this->dh === false) {
34 $this->error($err);
36 else {
37 $res = $this->query("SELECT COUNT(*) as count FROM sqlite_master WHERE type='table'");
38 if ($res->next('count') === "0") {
39 $checkwps_table = <<<END
40 CREATE TABLE checkwps (
41 themeid INTEGER,
42 version_type TEXT,
43 version_number TEXT,
44 target TEXT,
45 pass INTEGER
47 END;
49 $theme_table = <<<END
50 CREATE TABLE themes (
51 name TEXT,
52 approved INTEGER,
53 shortname TEXT,
54 author TEXT,
55 email TEXT,
56 mainlcd TEXT,
57 remotelcd TEXT,
58 description TEXT,
59 zipfile TEXT,
60 sshot_wps TEXT,
61 sshot_menu TEXT,
62 emailverification TEXT,
63 reason TEXT,
64 timestamp FLOAT
66 END;
67 $admin_table = <<<END
68 CREATE TABLE admins (
69 name TEXT PRIMARY KEY,
70 pass TEXT
72 END;
73 $target_table = <<<END
74 CREATE TABLE targets (
75 shortname TEXT PRIMARY KEY,
76 fullname TEXT,
77 mainlcd TEXT,
78 remotelcd TEXT,
79 pic TEXT,
80 depth INTEGER
82 END;
83 $log_table = <<<END
84 CREATE TABLE log (
85 time TEXT,
86 ip TEXT,
87 admin TEXT,
88 msg TEXT
90 END;
91 $this->query($target_table);
92 $this->query($checkwps_table);
93 $this->query($theme_table);
94 $this->query($admin_table);
95 $this->query($log_table);
100 public function query($sql) {
101 $res = @sqlite_query(
102 $this->dh,
103 $sql,
104 SQLITE_ASSOC,
105 $err
107 if ($res === false) {
108 $this->error($err, $sql);
110 else {
111 return new result($res, $this->dh);
115 private function error($err, $sql = "") {
117 * Sometimes the error is empty, in which case the explanation can be
118 * found like this
120 if ($err == "") {
121 $code = sqlite_last_error($this->dh);
122 $err = sprintf("%s (%d)",
123 sqlite_error_string($code),
124 $code
127 $msg = sprintf("<b>DB Error:</b> %s", $err);
128 if ($sql != "") {
129 $msg .= sprintf("<br />\n<b>SQL:</b> %s", $sql);
131 /* xxx: We'd probably want to log this rather than output it */
132 die($msg);
135 public static function quote($input) {
136 return sqlite_escape_string($input);
141 * Simple OO wrapper around the regular sqlite functions. Newer PHP versions
142 * have something like this, but use this to avoid depending on that.
144 class result {
145 private $rh;
146 private $dh;
148 public function __construct($rh, &$dh) {
149 $this->rh = $rh;
150 $this->dh = $dh;
153 public function numrows() {
154 return sqlite_num_rows($this->rh);
157 public function next($field = false) {
158 $row = sqlite_fetch_array($this->rh);
159 if ($field !== false && isset($row[$field])) {
160 return $row[$field];
162 else {
163 return $row;
167 public function insertid() {
168 return sqlite_last_insert_rowid($this->dh);
171 public function rowsaffected() {
172 return sqlite_changes($this->dh);