forked from nyson/izwei
/
ajax.php
executable file
·172 lines (136 loc) · 3.39 KB
/
ajax.php
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
<?php
require_once("./lib/Tags.php");
require_once("./lib/Upload.php");
require_once("./lib/Search.php");
// we really don't want to have errors showing up here
if(!isset($_GET['debug']))
error_reporting(!E_ALL);
switch ($_GET['do']) {
/**
* This will fetch a full list of images
*/
case 'getImages':
$s = new Search();
// gets the range of our search
if(isset($_GET['offset'])) {
if(isset($_GET['count']))
$s->range($_GET['offset'], $_GET['count']);
else
$s->range($_GET['offset']);
}
else if(isset($_GET['count']))
$s->range(NULL, $_GET['count']);
// sets our tags
if(isset($_GET['include'])) {
$tags = explode(',', $_GET['include']);
foreach($tags as $k => $v) {
$tags[$k] = urldecode($v);
}
$s->with($tags);
}
if(isset($_GET['exclude'])) {
$tags = explode(',', $_GET['exclude']);
foreach($tags as $k => $v) {
$tags[$k] = urldecode($v);
}
$s->without($tags);
}
// sets our order
if(isset($_GET['order'])) {
$orders = explode(',', $_GET['order']);
foreach($orders as $key => &$o)
switch($o) {
case 'newest':
$o = SORT_NEWEST;
break;
case 'oldest':
$o = SORT_OLDEST;
break;
case 'best':
$o = SORT_POPULARITY;
break;
case 'worst':
$o = SORT_IMPOPULARITY;
break;
case 'random':
$o = SORT_RANDOM;
break;
default:
trigger_error("Sort mode $o is not supported!", E_USER_NOTICE);
}
$s->order($orders);
}
$result = $s->search();
$images = array();
while($image = $result->fetch_assoc())
$images[] = $image;
echo json_encode($images);
break;
/**
* Get the tags of an image
*
* @param image valid image id
*/
case 'getTags':
if(!isset($_GET['image'])) {
echo "-10 Image is not set!";
break;
}
if(!is_numeric($_GET['image'])){
echo "-11 Image ID given is not a number!";
break;
}
$t = new Tags();
echo json_encode($t->getImageTags($_GET['image']));
break;
/**
* Add tags to image
*
* @param tags commaseparated string of tags
* @param image valid image id
*/
case 'addTags':
if(!isset($_GET['tags']) || !isset($_GET['image'])) {
echo "-20 Tags or image where not given";
break;
}
if(!is_numeric($_GET['image'])) {
echo "-21 Image ID ".$_GET['image']." is not a number!";
break;
}
$t = new Tags();
echo json_encode($t->get($t->connect((int)$_GET['image'], $_GET['tags'])));
break;
/**
* Upload a file by URL
*
* @param url url to upload to imagescript
*/
case 'uploadByURL':
if(!isset($_GET['url'])){
echo "-20 URL not set!";
break;
}
$upload = new Upload();
echo $upload->byURL($_GET['url']);
break;
case 'getImage':
if(!isset($_GET['image']) || !is_numeric($_GET['image'])) {
echo "-10 Image id not valid";
break;
}
$result = SQL::Query("SELECT * FROM images"
. " WHERE id = '".$_GET['image']."' LIMIT 1");
if($result->num_rows == 0) {
echo "-11 No image at this ID!";
break;
}
echo json_encode($result->fetch_assoc());
break;
default:
if(isset($_GET['do']))
echo "-10 Operation '".$_GET['do']."' not valid!";
else
echo "-11 No operation set!";
}
?>