summaryrefslogtreecommitdiffstats
path: root/getjson.php
blob: d9b0891cdf7dab420fbee2abecf1429b512169ba (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
<?php

$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");
    $count = $DB->query("SELECT 1");
  }
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
	*/
	$result = $DB->query("SELECT base_uri, filename  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");

	$count = $DB->query("SELECT count(*) as total  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
      {
	$result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
	$count = $DB->query("SELECT count(*) as total FROM photos");
      }
  }

/* 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;

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

$return=array($count,$result);

echo json_encode($return);

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


?>