-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
353 lines (321 loc) · 13.9 KB
/
index.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* This file is part of Open Clipart Library <http://openclipart.org>
*
* Open Clipart Library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Open Clipart Library 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 Affero General Public License
* along with Open Clipart Library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* author: Jakub Jankiewicz <http://jcubic.pl>
*/
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', 'On');
define('DEBUG', true);
require_once('libs/utils.php');
require_once('libs/Template.php');
require_once('libs/System.php');
require_once('libs/Clipart.php');
/* TODO: logs (using slim) and cache in Template::render
*
*
*
*/
$app = new System(function() {
/*
* System config need those variables
* db_user, db_host, db_pass, db_name
*/
$config = get_object_vars(json_decode(file_get_contents('config.json')));
$protocol = (isset($_SERVER['HTTPS'])) ? 'https' : 'http';
return array_merge($config, array(
'root' => $protocol . '://' . $_SERVER['HTTP_HOST'],
'root_directory' => $_SERVER['DOCUMENT_ROOT'], // dirname(__FILE__),
'tag_limit' => 100,
'top_artist_last_month_limit' => 10,
'home_page_thumbs_limit' => 8,
'home_page_collections_limit' => 5,
'home_page_news_limit' => 3,
'bitmap_resolution_limit' => 3840,
'google_analytics' => false,
'show_facebook' => false,
'debug' => true,
//user for admin only; track - initialy to disable download count in edit button
'forward_query_list' => array('nsfw', 'track', 'user'),
'nsfw_image' => array(
'user' => 'h0us3s',
'filename' => 'h0us3s_Signs_Hazard_Warning_1'
),
'pd_issue_image' => array(
'user' => 'h0us3s',
'filename' => 'h0us3s_Signs_Hazard_Warning_1'
),
));
});
$app->notFound(function () use ($app) {
$response = $app->response();
$response['Content-Type'] = 'text/html';
$main = new Template('main', function() {
return array('content' => new Template('error_404', null));
});
echo $main->render();
});
$app->error(function(Exception $e) {
echo 'error';
});
$app->post('/login', function() use ($app) {
if (is_set($_POST['login']) && is_set($_POST['password'])) {
}
});
$app->get('/login', function() use ($app) {
echo 'redirect to ' . $app->GET->redirect;
});
$app->get("/register", function() use ($app) {
});
$app->get("/detail/:id/:link", function($id, $link) use ($app) {
});
$app->get("/user-detail/:username", function($username) use ($app) {
});
function create_thumbs($where, $order_by) {
global $app;
if ($app->nsfw()) {
$nsfw = "AND openclipart_clipart.id not in (SELECT clipart FROM openclipart_clipart_tags INNER JOIN openclipart_tags ON tag = openclipart_tags.id WHERE name = 'nsfw')";
} else {
$nsfw = '';
}
if ($app->is_logged()) {
$fav_check = $app->get_user_id() . ' in '.
'(SELECT user FROM openclipart_favorites'.
' WHERE openclipart_clipart.id = clipart)';
} else {
$fav_check = '0';
}
if ($where != '' && $where != null) {
$where = "AND $where";
}
$query = "SELECT openclipart_clipart.id, title, filename, link, created, username, count(DISTINCT user) as num_favorites, created, date, $fav_check as user_favm, downloads FROM openclipart_clipart INNER JOIN openclipart_favorites ON clipart = openclipart_clipart.id INNER JOIN openclipart_users ON openclipart_users.id = owner WHERE openclipart_clipart.id NOT IN (SELECT clipart FROM openclipart_clipart_tags INNER JOIN openclipart_tags ON openclipart_tags.id = tag WHERE clipart = openclipart_clipart.id AND openclipart_tags.name = 'pd_issue') $nsfw $where GROUP BY openclipart_clipart.id ORDER BY $order_by DESC LIMIT " . $app->config->home_page_thumbs_limit;
$clipart_list = array();
foreach ($app->db->get_array($query) as $row) {
$filename_png = preg_replace("/.svg$/",
".png",
$row['filename']);
$human_date = human_date($row['created']);
$data = array(
'filename_png' => $filename_png,
'human_date' => $human_date
//TODO: check when close this query
//'user_fav' => false
);
$clipart_list[] = array_merge($row, $data);
}
return array('cliparts' => $clipart_list);
}
$app->get('/', function() {
global $app;
$main = new Template('main', function() {
return array(
'content' => array(new Template('wellcome', null),
new Template('most_popular_thumbs', function() {
return array(
'content' => new Template('clipart_list', function() {
$last_week = "(SELECT WEEK(max(date)) FROM ".
"openclipart_favorites) = WEEK(date) AND ".
"YEAR(NOW()) = YEAR(date)";
return create_thumbs($last_week, "num_favorites");
})
);
}),
new Template('new_clipart_thumbs', function() {
return array(
'content' => new Template('clipart_list', function() {
return create_thumbs(null, "created");
})
);
}),
new Template('top_download_thumbs', function() {
return array(
'content' => new Template('clipart_list', function() {
$top_download = "YEAR(created) = YEAR(CURRENT_".
"DATE) AND MONTH(created) = MONTH(CURRENT_".
"DATE)";
return create_thumbs($top_download, "downloads");
})
);
})
),
'sidebar' => array(
new Template('join', null),
new Template('facebook_box', null),
new Template('follow_us_box', null),
new Template('news_box', function() {
global $app;
$query = "SELECT link, title FROM openclipart_news ORDER by date DESC LIMIT " . $app->config->home_page_news_limit;
return array('news' =>
array_reverse($app->db->get_array($query)));
}),
new Template('tag_cloud', function() {
global $app;
$query = "SELECT count(openclipart_tags.id) as tag_count FROM openclipart_clipart_tags INNER JOIN openclipart_tags ON openclipart_tags.id = tag GROUP BY tag ORDER BY tag_count DESC LIMIT 1";
$max = $app->db->get_value($query);
$query = "SELECT openclipart_tags.name, count(openclipart_tags.id) as tag_count FROM openclipart_clipart_tags INNER JOIN openclipart_tags ON openclipart_tags.id = tag GROUP BY tag ORDER BY tag_count DESC LIMIT " . $app->config->tag_limit;
$result = array();
$rows = $app->db->get_array($query);
shuffle($rows);
$normalize = size('20', $max);
return array('tags' =>
array_map(function($row) use ($normalize) {
return array(
'name' => $row['name'],
'size' => $normalize($row['tag_count'])
);
}, $rows)
);
}),
new Template('top_artists_last_month', function() {
global $app;
$query = "SELECT full_name, username, count(filename) AS num_uploads FROM openclipart_clipart INNER JOIN openclipart_users ON owner = openclipart_users.id WHERE date_format(created, '%Y-%c') = date_format(now(), '%Y-%c') GROUP BY openclipart_users.id ORDER BY num_uploads DESC LIMIT " . $app->config->top_artist_last_month_limit;
return array('artists' => $app->db->get_array($query));
}),
new Template('latest_collections_box', function() {
global $app;
$query = "SELECT openclipart_collections.id, name, title, username, date FROM openclipart_collections INNER JOIN openclipart_users ON user = openclipart_users.id ORDER BY date DESC LIMIT " . $app->config->home_page_collections_limit;
return array('collections' => array_map(function($row) {
return array_merge($row, array(
'human_date' => human_date($row['date'])
));
}, $app->db->get_array($query)));
})
)
); //array('content'
}); // new Template('main'
echo $main->render();
});
$app->get('/test', function() {
global $app;
$response = $app->response();
$response['Content-Type'] = 'text/plain';
print_r($_SERVER) . "\n";
echo $_SERVER['REQUEST_URI'] . "\n";
echo 'nsfw: ' . $app->nsfw() ? 'true' : 'false';
echo "\n";
echo (empty($_GET) ? 'true' : 'false') . "\n";
return;
$main = new Template('test', function() {
return array('foo' => function($query) {
global $app;
$array = $app->db->get_array($query);
return implode(' | ', $array[0]);
});
});
echo $main->render();
});
$app->get('/clipart/:id/:link', function($id, $link) {
$main = new Template('main', function() {
return array('content' => array(
new Template('clipart_detail', function() {
$tags = "SELECT ";
//editable - librarian or clipart owner
})
));
});
return $main->render();
});
// routing /people/*.svg
$app->get('/download/:user/:filename', function($user, $filename) {
global $app;
$clipart = new Clipart($user, $filename);
if (!$clipart->exists($svg) || $clipart->size() == 0) {
// old OCAL have some 0 size files
$app->notFound();
} else {
$response = $app->response();
$response['Content-Type'] = 'application/octet-stream';
if ($app->track()) {
$clipart->inc_download();
}
if ($app->nsfw() && $clipart->nsfw()) {
$filename = $app->config->root_directory . "/people/" .
$app->config->nsfw_image['user'] . "/" .
$app->config->nsfw_image['filename'] . ".svg";
} else if ($clipart->have_pd_issue()) {
$filename = $app->config->root_directory . "/people/" .
$app->config->pd_issue_image['user'] . "/" .
$app->config->pd_issue_image['filename'] . ".svg";
} else {
$filename = $clipart->full_path();
}
echo file_get_contents($filename);
}
});
$app->get('/image/:width/:user/:filename', function($w, $user, $file) {
global $app;
$width = intval($w);
$svg_filename = preg_replace("/.png$/", '.svg', $file);
$png = $app->config->root_directory . "/people/$user/${width}px-$file";
$svg = $app->config->root_directory . "/people/$user/" . $svg_filename;
$response = $app->response();
$clipart = new Clipart($user, $file);
if ($width > $app->config->bitmap_resolution_limit) {
$response->status(400);
// TODO: error template
echo "Resolution couldn't be higher then 3840px! Please download SVG and " .
"produce such huge bitmap locally.";
} else if (!$clipart->exists() || $clipart->size() == 0) {
// NOTE: you don't need to check user and file for script injection because
// file_exists will prevent this
$app->notFound();
} else {
// NSFW check
if ($app->nsfw() && $clipart->nsfw()) {
$user = $app->config->nsfw_image['user'];
$filename = $app->config->nsfw_image['filename'];
$png = $app->config->root_directory . "/people/$user/${width}px-$file.png";
$svg = $app->config->root_directory . "/people/$user/$filename.svg";
}
$response['Content-Type'] = 'image/png';
if (file_exists($png)) {
echo file_get_contents($png);
} else {
exec("rsvg --width $width $svg $png");
if (!file_exists($png)) {
$response['Content-Type'] = "text/html";
$app->pass();
} else {
echo file_get_contents($png);
}
}
}
});
// TODO: rpc permission system
/*
class Foo extends LibrarianPermission {
}
*/
$app->post('/rpc/:name', function($name) use ($app) {
$filename = $app->config->root_directory . "/rpc/".$name.".php";
require('libs/json-rpc/json-rpc.php');
if (class_exists($name)) {
handle_json_rpc(new $name());
} else {
if (file_exists($filename)) {
require_once($filename);
handle_json_rpc(new $name());
} else {
$msg = "ERROR: service `$name' not found";
echo json_encode(array(
"error" => array("code" => 108, "message" => $msg)
));
}
}
});
$app->run();
?>