6 $iniarray=parse_ini_file("config.ini");
7 $DBFILE=$iniarray["fspotdb"];
8 $usePDO=$iniarray["usePDO"];
9 $N=$iniarray["pics_per_page"];
10 /* end parse ini-file */
13 $DB = new PDO("sqlite:$DBFILE");
15 $DB = new SQlite3($DBFILE);
18 /* do database query */
19 if (isset($_REQUEST["S"]))
21 /* single tag or part of tag */
22 $tag = sqlite_escape_string($_REQUEST["S"]);
24 $result = $DB->query("SELECT name FROM tags");
26 else if (isset($_REQUEST["ID"]))
28 $id = intval($_REQUEST["ID"]);
29 $result = $DB->query("SELECT base_uri, filename, id FROM photos".
34 if (isset($_REQUEST["P"]))
35 $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
39 if (isset($_REQUEST["T"]))
41 /* single tag or part of tag */
42 $tags = $_REQUEST["T"];
43 $tags = explode(",",$tags);
44 $nrtags = count($tags);
46 foreach ($tags as $key => $value)
47 $tags[$key]=sqlite_escape_string(trim($value));
48 $tags = "'".implode("','",$tags)."'";
50 /* individual tags are seperated by ',' */
52 /* use and AND query between tags as a default
53 a good explanation on different ways of doing this can be found at:
54 http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
57 if (isset($_REQUEST["C"]))
59 $result = $DB->query("SELECT count(*) as total from (SELECT p.id FROM photo_tags pt, photos p, tags t".
60 " WHERE pt.tag_id = t.id".
61 " AND (t.name COLLATE NOCASE IN ($tags))".
62 " AND p.id = pt.photo_id ".
63 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags)");
67 $result = $DB->query("SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t".
68 " WHERE pt.tag_id = t.id".
69 " AND (t.name COLLATE NOCASE IN ($tags))".
70 " AND p.id = pt.photo_id ".
71 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
72 " LIMIT $OFFSET, $N");
78 if (isset($_REQUEST["C"]))
79 $result = $DB->query("SELECT count(*) as total FROM photos");
81 $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
85 /* encode result as an array */
89 /* convert results into array */
90 while($res = $result->fetchArray(SQLITE3_ASSOC))
95 foreach($result as $res)
100 echo json_encode($result);
102 /* close the database */