ok now the api random function works.
[miniqdb.git] / api.php
blob3dd0b0a4d795aa22c6e5a1331c877021379c11a1
1 <?php
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. */
20 require "config.php";
22 header("Content-type: text/xml");
24 function return_data($method, $data) {
25 global $dom;
26 if ($method == 'rest') {
27 $dom = new DOMDocument('1.0');
28 $root = $dom->appendChild($dom->createElement('miniqdb'));
29 element_iter_rest($data, $root);
30 echo $dom->saveXML();
31 } else {
32 die("<b>Utter fail:</b> Caught in infinite invalid method loop.");
34 exit();
37 function element_iter_rest($array, $parent) {
38 global $dom;
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);
47 } else {
48 element_iter_rest($thatone, $element);
51 $parent->appendChild($element);
56 function throw_error($id) {
57 global $_method;
58 switch ($id) {
59 case 1:
60 $text = "invalid method";
61 break;
62 case 2:
63 $text = "invalid act";
64 break;
65 case 3:
66 $text = "no quote id";
67 break;
68 case 4:
69 $text = "invalid quote id";
70 break;
71 default:
72 $text = "unknown error";
74 return_data($_method, array('error' => array(0 => array('@id' => $id, '#text' => $text))));
77 function act_quote($method) {
78 // act quote
79 // required vars: id
80 global $db;
81 if (isset($_GET['id'])) {
82 $_id = $_GET['id'];
83 } else {
84 throw_error(3);
86 $st = $db->prepare("SELECT * FROM miniqdb WHERE id=?");
87 $st->execute(array($_id));
88 if ($st->rowCount() == 0) {
89 throw_error(4);
90 } else {
91 $data = array();
92 $data['quote'] = array();
93 foreach ($st->fetchAll() as $r) {
94 $quote = array();
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) {
106 global $db;
107 // act stats
108 // no vars
109 $totalq = $db->query("SELECT COUNT(*) FROM miniqdb");
110 $numq = $totalq->fetchColumn(0);
111 $data = array();
112 $data['stats'] = array();
113 $data['stats'][] = array('@count' => $numq);
114 return_data($method, $data);
117 function act_random($method) {
118 global $db;
119 // act random
120 // optional vars: count (=1)
121 if (isset($_GET['count'])) {
122 $count = $_GET['count'];
123 } else {
124 $count = 1;
126 $count = (int)$count;
127 $st = $db->query("SELECT * FROM miniqdb ORDER BY RAND() LIMIT $count");
128 $data = array();
129 $data['quote'] = array();
130 foreach ($st->fetchAll() as $r) {
131 $quote = array();
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);
141 // variable method
142 // You can use REST. This is the default. JSON soon.
143 if (!isset($_GET['method'])) {
144 $_method = 'rest';
145 } else if ($_GET['method'] == 'rest') {
146 $_method = 'rest';
147 } else {
148 $_method = 'rest';
149 throw_error(1);
152 // variable act
153 // Current available actions: quote stats
154 if (!isset($_GET['act'])) {
155 throw_error(2);
157 switch($_GET['act']) {
158 case 'quote':
159 act_quote($_method);
160 break;
161 case 'stats':
162 act_stats($_method);
163 break;
164 case 'random':
165 act_random($_method);
166 break;
167 default:
168 throw_error(2);