4 copyright 2012,2013 Arun Persaud <arun@nubati.net>
6 This file is part of photo-tags.
8 Photo-tags is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Photo-tags is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Photo-tags. If not, see <http://www.gnu.org/licenses/>.
26 $iniarray=parse_ini_file("config.ini");
27 $DBFILE=$iniarray["fspotdb"];
28 $usePDO=$iniarray["usePDO"];
29 $N=$iniarray["pics_per_page"];
30 /* end parse ini-file */
33 $DB = new PDO("sqlite:$DBFILE");
35 $DB = new SQlite3($DBFILE);
38 /* do database query */
39 if (isset($_REQUEST["S"]))
41 /* single tag or part of tag */
42 $tag = sqlite_escape_string($_REQUEST["S"]);
44 $result = $DB->query("SELECT name FROM tags");
46 else if (isset($_REQUEST["NP"]))
48 /* get +- 3 pics from ordered list to show next to a large image */
50 /* first create a temp table with all images and then use rowid to get +-5 images */
52 if (isset($_REQUEST["T"]))
54 /* single tag or part of tag */
55 $tags = $_REQUEST["T"];
56 $tags = explode(",",$tags);
57 $nrtags = count($tags);
59 foreach ($tags as $key => $value)
60 $tags[$key]=sqlite_escape_string(trim($value));
61 $tags = "'".implode("','",$tags)."'";
63 $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t".
64 " WHERE pt.tag_id = t.id".
65 " AND (t.name COLLATE NOCASE IN ($tags))".
66 " AND p.id = pt.photo_id ".
67 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
71 $DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM photos p");
74 if (isset($_REQUEST["ID"]))
76 $ID=intval($_REQUEST["ID"]);
77 $result = $DB->query("SELECT * FROM NEXTPREV".
78 " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -3".
79 " AND rowid < (select rowid from NEXTPREV where id=$ID) +3");
83 $result = $DB->query("SELECT 1 where 1=2");
87 else if (isset($_REQUEST["ID"]) && !isset($_REQUEST["C"]))
89 $id = intval($_REQUEST["ID"]);
90 $result = $DB->query("SELECT base_uri, filename, id, description, time FROM photos".
93 else if (isset($_REQUEST["IDT"]))
94 { /* tags of a single image */
95 $id = intval($_REQUEST["IDT"]);
96 $result = $DB->query("SELECT t.name as name FROM photo_tags pt ".
97 " LEFT JOIN tags t on t.id=pt.tag_id".
98 " WHERE pt.photo_id=$id");
100 else if (isset($_REQUEST["CLOUD"]))
102 $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
103 " LEFT JOIN tags t on t.id=pt.tag_id".
108 if (isset($_REQUEST["P"]))
109 $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
113 if (isset($_REQUEST["T"]))
115 /* single tag or part of tag */
116 $tags = $_REQUEST["T"];
117 $tags = explode(",",$tags);
118 $nrtags = count($tags);
120 foreach ($tags as $key => $value)
121 $tags[$key]=sqlite_escape_string(trim($value));
122 $tags = "'".implode("','",$tags)."'";
124 /* individual tags are seperated by ',' */
126 /* use and AND query between tags as a default
127 a good explanation on different ways of doing this can be found at:
128 http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
131 if (isset($_REQUEST["C"]))
133 $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT p.id as id FROM photo_tags pt, photos p, tags t".
134 " WHERE pt.tag_id = t.id".
135 " AND (t.name COLLATE NOCASE IN ($tags))".
136 " AND p.id = pt.photo_id ".
137 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
139 if (isset($_REQUEST["ID"]))
141 $ID = $_REQUEST["ID"];
142 $result = $DB->query("SELECT count(*) as total, (SELECT rowid from TEMPPICS WHERE id = $ID) as row from TEMPPICS");
145 $result = $DB->query("SELECT count(*) as total, -1 as row from TEMPPICS");
149 $result = $DB->query("SELECT base_uri, filename, p.id as id FROM photo_tags pt, photos p, tags t".
150 " WHERE pt.tag_id = t.id".
151 " AND (t.name COLLATE NOCASE IN ($tags))".
152 " AND p.id = pt.photo_id ".
153 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
154 " LIMIT $OFFSET, $N");
159 if (isset($_REQUEST["C"]))
161 $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT id from photos");
163 if (isset($_REQUEST["ID"]))
165 $ID = $_REQUEST["ID"];
166 $result = $DB->query("SELECT count(*) as total, (SELECT rowid FROM TEMPPICS WHERE id=$ID) as row FROM TEMPPICS");
169 $result = $DB->query("SELECT count(*) as total, -1 as row FROM TEMPPICS");
172 $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
176 /* encode result as an array */
180 /* convert results into array */
181 while($res = $result->fetchArray(SQLITE3_ASSOC))
186 foreach($result as $res)
191 echo json_encode($result);
193 /* close the database */