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["NP"]))
28 /* get +- 5 pics from ordered list to show next to a large image */
30 /* first create a temp table with all images and then use rowid to get +-5 images */
32 if (isset($_REQUEST["T"]))
34 /* single tag or part of tag */
35 $tags = $_REQUEST["T"];
36 $tags = explode(",",$tags);
37 $nrtags = count($tags);
39 foreach ($tags as $key => $value)
40 $tags[$key]=sqlite_escape_string(trim($value));
41 $tags = "'".implode("','",$tags)."'";
43 $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t".
44 " WHERE pt.tag_id = t.id".
45 " AND (t.name COLLATE NOCASE IN ($tags))".
46 " AND p.id = pt.photo_id ".
47 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
52 $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM photos p");
55 if (isset($_REQUEST["ID"]))
57 $ID=intval($_REQUEST["ID"]);
58 $result = $DB->query("SELECT * FROM NEXTPREV".
59 " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -5".
60 " AND rowid < (select rowid from NEXTPREV where id=$ID) +5");
64 $result = $DB->query("SELECT 1 where 1=2");
68 else if (isset($_REQUEST["ID"]))
70 $id = intval($_REQUEST["ID"]);
71 $result = $DB->query("SELECT base_uri, filename, id FROM photos".
74 else if (isset($_REQUEST["CLOUD"]))
76 $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
77 " LEFT JOIN tags t on t.id=pt.tag_id".
82 if (isset($_REQUEST["P"]))
83 $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
87 if (isset($_REQUEST["T"]))
89 /* single tag or part of tag */
90 $tags = $_REQUEST["T"];
91 $tags = explode(",",$tags);
92 $nrtags = count($tags);
94 foreach ($tags as $key => $value)
95 $tags[$key]=sqlite_escape_string(trim($value));
96 $tags = "'".implode("','",$tags)."'";
98 /* individual tags are seperated by ',' */
100 /* use and AND query between tags as a default
101 a good explanation on different ways of doing this can be found at:
102 http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
105 if (isset($_REQUEST["C"]))
107 $result = $DB->query("SELECT count(*) as total from (SELECT p.id FROM photo_tags pt, photos p, tags t".
108 " WHERE pt.tag_id = t.id".
109 " AND (t.name COLLATE NOCASE IN ($tags))".
110 " AND p.id = pt.photo_id ".
111 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags)");
115 $result = $DB->query("SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t".
116 " WHERE pt.tag_id = t.id".
117 " AND (t.name COLLATE NOCASE IN ($tags))".
118 " AND p.id = pt.photo_id ".
119 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
120 " LIMIT $OFFSET, $N");
126 if (isset($_REQUEST["C"]))
127 $result = $DB->query("SELECT count(*) as total FROM photos");
129 $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
133 /* encode result as an array */
137 /* convert results into array */
138 while($res = $result->fetchArray(SQLITE3_ASSOC))
143 foreach($result as $res)
148 echo json_encode($result);
150 /* close the database */