summaryrefslogtreecommitdiffstats
path: root/getjson.php
blob: ff41ac46ee498655186d30ecd6643786a5bd79c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php

  /**
    copyright 2012,2013 Arun Persaud <arun@nubati.net>

    This file is part of photo-tags.

    Photo-tags is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Photo-tags is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Photo-tags.  If not, see <http://www.gnu.org/licenses/>.

  **/

$N=30;

/* parse ini -file */
$iniarray=parse_ini_file("config.ini");
$DBFILE=$iniarray["fspotdb"];
$usePDO=$iniarray["usePDO"];
$N=$iniarray["pics_per_page"];
/* end parse ini-file */

if($usePDO)
  $DB = new PDO("sqlite:$DBFILE");
else
  $DB = new SQlite3($DBFILE);


/* do database query */
if (isset($_REQUEST["S"]))
  {
    /* single tag or part of tag */
    $tag = sqlite_escape_string($_REQUEST["S"]);

    $result = $DB->query("SELECT name FROM tags");
  }
 else if (isset($_REQUEST["NP"]))
  {
    /* get +- 3 pics from ordered list to show next to a large image */

    /* first create a temp table with all images and then use rowid to get +-5 images */

    if (isset($_REQUEST["T"]))
      {
	/* single tag or part of tag */
	$tags = $_REQUEST["T"];
	$tags = explode(",",$tags);
	$nrtags = count($tags);

	foreach ($tags as $key => $value)
	  $tags[$key]=sqlite_escape_string(trim($value));
	$tags = "'".implode("','",$tags)."'";

	$DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
		   " WHERE pt.tag_id = t.id".
		   " AND (t.name COLLATE NOCASE IN ($tags))".
		   " AND p.id = pt.photo_id ".
		   " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");
      }
    else
      {
	$DB->query("CREATE TEMP TABLE NEXTPREV AS SELECT base_uri, filename, p.id as id FROM  photos p");
      };

    if (isset($_REQUEST["ID"]))
      {
	$ID=intval($_REQUEST["ID"]);
	$result = $DB->query("SELECT * FROM NEXTPREV".
			     " WHERE rowid > (select rowid from NEXTPREV where id=$ID) -3".
			     "   AND rowid < (select rowid from NEXTPREV where id=$ID) +3");
      }
    else
      {
	$result = $DB->query("SELECT 1 where 1=2");
      }

  }
 else if (isset($_REQUEST["ID"]) && !isset($_REQUEST["C"]))
  {
    $id  = intval($_REQUEST["ID"]);
    $result = $DB->query("SELECT base_uri, filename, id, description, time FROM photos".
			 " WHERE id=$id");
  }
 else if (isset($_REQUEST["IDT"]))
   {  /* tags of a single image */
    $id  = intval($_REQUEST["IDT"]);
    $result = $DB->query("SELECT t.name as name FROM photo_tags pt ".
			 " LEFT JOIN tags t on t.id=pt.tag_id".
			 " WHERE pt.photo_id=$id");
  }
 else if (isset($_REQUEST["CLOUD"]))
  {
    $result = $DB->query("SELECT t.name as name, count(*) as count FROM photo_tags pt ".
			 " LEFT JOIN tags t on t.id=pt.tag_id".
			 " GROUP BY t.id");
  }
 else
  {
    if (isset($_REQUEST["P"]))
      $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
    else
      $OFFSET = "0";

    if (isset($_REQUEST["T"]))
      {
	/* single tag or part of tag */
	$tags = $_REQUEST["T"];
	$tags = explode(",",$tags);
	$nrtags = count($tags);

	foreach ($tags as $key => $value)
	  $tags[$key]=sqlite_escape_string(trim($value));
	$tags = "'".implode("','",$tags)."'";

	/* individual tags are seperated by ',' */

	/* use and AND query between tags as a default
	 a good explanation on different ways of doing this can be found at:
	 http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
	*/

	if (isset($_REQUEST["C"]))
	  {
	    $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT p.id as id FROM photo_tags pt, photos p, tags t".
		       " WHERE pt.tag_id = t.id".
		       " AND (t.name COLLATE NOCASE IN ($tags))".
		       " AND p.id = pt.photo_id ".
		       " GROUP BY p.id HAVING COUNT( p.id )=$nrtags");

	    if (isset($_REQUEST["ID"]))
	      {
		$ID = $_REQUEST["ID"];
		$result = $DB->query("SELECT count(*) as total, (SELECT rowid from TEMPPICS WHERE id = $ID) as row from TEMPPICS");
	      }
	    else
	      $result = $DB->query("SELECT count(*) as total, -1 as row from TEMPPICS");
	  }
	else
	  {
	    $result = $DB->query("SELECT base_uri, filename, p.id as id  FROM photo_tags pt, photos p, tags t".
				 " WHERE pt.tag_id = t.id".
				 " AND (t.name COLLATE NOCASE IN ($tags))".
				 " AND p.id = pt.photo_id ".
				 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
				 "    LIMIT $OFFSET, $N");
	  }
      }
    else
      {
	if (isset($_REQUEST["C"]))
	  {
	    $DB->query("CREATE TEMP TABLE TEMPPICS AS SELECT id from photos");

	    if (isset($_REQUEST["ID"]))
	      {
		$ID = $_REQUEST["ID"];
		$result = $DB->query("SELECT count(*) as total, (SELECT rowid FROM TEMPPICS WHERE id=$ID) as row FROM TEMPPICS");
	      }
	    else
	      $result = $DB->query("SELECT count(*) as total, -1 as row FROM TEMPPICS");
	  }
	else
	  $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
      }
  }

/* encode result as an array */
$tmp=array();
if(!$usePDO)
  {
    /* convert results into array */
    while($res = $result->fetchArray(SQLITE3_ASSOC))
      $tmp[]=$res;
  }
else
  {
    foreach($result as $res)
      $tmp[]=$res;
  }
$result=$tmp;

echo json_encode($result);

/* close the database */
if($usePDO)
  $DB=null;
else
  $DB->close();


?>