3 /* miniqdb - A minimalistic quote database
4 Copyright (C) 2008 Ian Weller <ianweller@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
22 header("Content-type: text/xml");
24 function return_data($method, $data) {
26 if ($method == 'rest') {
27 $dom = new DOMDocument('1.0');
28 $root = $dom->appendChild($dom->createElement('miniqdb'));
29 element_iter_rest($data, $root);
32 die("<b>Utter fail:</b> Caught in infinite invalid method loop.");
37 function element_iter_rest($array, $parent) {
39 foreach($array as $key=>$value) {
40 foreach($value as $thatone) {
41 $element = $dom->createElement($key);
42 foreach($thatone as $stuff=>$moar) {
43 if ($stuff == '#text') {
44 $element->appendChild($dom->createTextNode($moar));
45 } else if (substr($stuff, 0, 1) == '@') {
46 $element->setAttribute(substr($stuff, 1), $moar);
48 element_iter_rest($thatone, $element);
51 $parent->appendChild($element);
56 function throw_error($id) {
60 $text = "invalid method";
63 $text = "invalid act";
66 $text = "no quote id";
69 $text = "invalid quote id";
72 $text = "unknown error";
74 return_data($_method, array('error' => array(0 => array('@id' => $id, '#text' => $text))));
77 function act_quote($method) {
81 if (isset($_GET['id'])) {
86 $st = $db->prepare("SELECT * FROM miniqdb WHERE id=?");
87 $st->execute(array($_id));
88 if ($st->rowCount() == 0) {
92 $data['quote'] = array();
93 foreach ($st->fetchAll() as $r) {
95 $quote['@id'] = $r['id'];
96 $quote['@timestamp'] = $r['epoch'];
97 $quote['#text'] = implode("\n", explode("\r\n", $r['quote']));
98 $quote['@lines'] = count(explode("\r\n", $r['quote']));
99 $data['quote'][] = $quote;
101 return_data($method, $data);
105 function act_stats($method) {
109 $totalq = $db->query("SELECT COUNT(*) FROM miniqdb");
110 $numq = $totalq->fetchColumn(0);
112 $data['stats'] = array();
113 $data['stats'][] = array('@count' => $numq);
114 return_data($method, $data);
117 function act_random($method) {
120 // optional vars: count (=1)
121 if (isset($_GET['count'])) {
122 $count = $_GET['count'];
126 $count = (int)$count;
127 $st = $db->query("SELECT * FROM miniqdb ORDER BY RAND() LIMIT $count");
129 $data['quote'] = array();
130 foreach ($st->fetchAll() as $r) {
132 $quote['@id'] = $r['id'];
133 $quote['@timestamp'] = $r['epoch'];
134 $quote['#text'] = implode("\n", explode("\r\n", $r['quote']));
135 $quote['@lines'] = count(explode("\r\n", $r['quote']));
136 $data['quote'][] = $quote;
138 return_data($method, $data);
142 // You can use REST. This is the default. JSON soon.
143 if (!isset($_GET['method'])) {
145 } else if ($_GET['method'] == 'rest') {
153 // Current available actions: quote stats
154 if (!isset($_GET['act'])) {
157 switch($_GET['act']) {
165 act_random($_method);