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".
32 else if (isset($_REQUEST["CLOUD"]))
34 $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
35 " LEFT JOIN tags t on t.id=pt.tag_id".
40 if (isset($_REQUEST["P"]))
41 $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
45 if (isset($_REQUEST["T"]))
47 /* single tag or part of tag */
48 $tags = $_REQUEST["T"];
49 $tags = explode(",",$tags);
50 $nrtags = count($tags);
52 foreach ($tags as $key => $value)
53 $tags[$key]=sqlite_escape_string(trim($value));
54 $tags = "'".implode("','",$tags)."'";
56 /* individual tags are seperated by ',' */
58 /* use and AND query between tags as a default
59 a good explanation on different ways of doing this can be found at:
60 http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
63 if (isset($_REQUEST["C"]))
65 $result = $DB->query("SELECT count(*) as total from (SELECT p.id FROM photo_tags pt, photos p, tags t".
66 " WHERE pt.tag_id = t.id".
67 " AND (t.name COLLATE NOCASE IN ($tags))".
68 " AND p.id = pt.photo_id ".
69 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags)");
73 $result = $DB->query("SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t".
74 " WHERE pt.tag_id = t.id".
75 " AND (t.name COLLATE NOCASE IN ($tags))".
76 " AND p.id = pt.photo_id ".
77 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
78 " LIMIT $OFFSET, $N");
84 if (isset($_REQUEST["C"]))
85 $result = $DB->query("SELECT count(*) as total FROM photos");
87 $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
91 /* encode result as an array */
95 /* convert results into array */
96 while($res = $result->fetchArray(SQLITE3_ASSOC))
101 foreach($result as $res)
106 echo json_encode($result);
108 /* close the database */