forked from pommes-frites/piwigo-facetag
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.inc.php
executable file
·292 lines (223 loc) · 6.79 KB
/
main.inc.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
<?php
/*
Plugin Name: Mug Shot
Version: 2.0.3
Description: Improved face tagging for Piwigo
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=910
Author: ccraige90
Has Settings: webmaster
*/
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
global $conf;
if (!isset($conf['MugShot'])):
include(dirname(__FILE__).'/include/config_default.inc.php');
conf_update_param('MugShot', $config_default);
load_conf_from_db();
endif;
$conf['MugShot'] = safe_unserialize($conf['MugShot']);
/*
* Plugin Constants
*/
define('MUGSHOT_ID', basename(dirname(__FILE__)));
define('MUGSHOT_PATH' , PHPWG_PLUGINS_PATH . MUGSHOT_ID . '/');
define('MUGSHOT_ADMIN', get_root_url() . 'admin.php?page=plugin-' . MUGSHOT_ID);
define('MUGSHOT_BASE_URL', get_root_url() . 'admin.php?page=plugin-' . MUGSHOT_ID);
define('MUGSHOT_VERSION', '2.0.3');
define('MUGSHOT_TABLE', '`face_tag_positions`');
define('MUGSHOT_QUEUE_TABLE', '`face_tag_queue`');
/*
* API Functions
*/
$ws_file = MUGSHOT_PATH . 'include/capture.php';
/*
* Admin Event Handlers
*/
add_event_handler('init', 'mugshot_lang_init');
add_event_handler('loc_begin_page_header', 'mugshot_files', 40, 2);
add_event_handler('loc_end_picture', 'mugshot_button');
add_event_handler('loc_end_add_uploaded_file', 'mugshot_queue_for_processing');
/*
* Include custom helper functions
*/
include_once(MUGSHOT_PATH . 'include/helpers.php');
/*
* Conditional Logic for groups
*/
$current_user_groups = query_mugshot_groups();
$intersect = array();
if (count($current_user_groups) != 0) {
$plugin_config = conf_get_param(MUGSHOT_ID);
$group_list = $plugin_config['groups'] ?? array();
if(is_array($group_list) && count($group_list) != 0) {
$intersect = array_intersect($group_list, $current_user_groups);
}
}
if (is_array($current_user_groups) && count($intersect) != 0) {
// Retrieve the current user theme
$query = 'SELECT theme FROM ' . USER_INFOS_TABLE . ';';
$theme = strtolower(pwg_db_fetch_assoc(pwg_query($query))['theme']);
switch ($theme) {
case 'bootstrap_darkroom':
define('BOOT', 1);
break;
case 'bootstrapdefault':
define('BOOT', 1);
break;
default:
define('BOOT', 0);
break;
}
define('MUGSHOT_USER_ADMIN', true);
if(script_basename() != 'admin') {
add_event_handler('loc_end_page_tail', 'insert_tag_list');
}
add_event_handler('ws_add_methods', 'add_MUGSHOT_methods', EVENT_HANDLER_PRIORITY_NEUTRAL, $ws_file);
} else {
define('MUGSHOT_USER_ADMIN', false);
}
/*
* Test to see if the upload fires as expected
*/
function create_facetag_tableb() {
}
function mugshot_queue_for_processing($image_info) {
$createTableQuery = 'CREATE TABLE IF NOT EXISTS `face_tag_queue` (
`image_id` mediumint(8) unsigned NOT NULL default "0",
`stuff` varchar(5000) NOT NULL default "0",
PRIMARY KEY (`image_id`)
)';
pwg_query($createTableQuery);
$item = serialize($image_info);
$id = $image_info['id'];
$insertImageQueueQuery = "INSERT INTO `face_tag_queue` (image_id, stuff) VALUES ('$id','$item')";
pwg_query($insertImageQueueQuery);
}
/*
* Loads translations
*/
function mugshot_lang_init(){
load_language('plugin.lang', MUGSHOT_PATH);
}
/*
* Catch the page header and combine our css
*/
function mugshot_files() {
if(script_basename() != 'admin') {
global $template;
if(MUGSHOT_USER_ADMIN) {
$style_path = 'plugins/MugShot/css/admin_style.css';
$script_path = 'plugins/MugShot/js/admin_mug.js';
} else {
$style_path = 'plugins/MugShot/css/style.css';
$script_path = 'plugins/MugShot/js/mug.js';
}
$template -> func_combine_css(array('id' => 'customMugCss', 'path' => $style_path));
$template -> func_combine_script( array('id' => 'customMugJs', 'path' => $script_path, 'load' => 'async'));
}
}
/*
* Queries current user groups
*/
function query_mugshot_groups() {
if (isset($_SESSION['pwg_uid'])) {
$user = $_SESSION['pwg_uid'];
} else {
return array();
}
$sql = 'SELECT gt.id FROM ' . USER_GROUP_TABLE . ' AS ugt
INNER JOIN ' . GROUPS_TABLE . ' AS gt
ON ugt.group_id=gt.id
WHERE ugt.user_id=' . $user . ';';
$res = fetch_sql($sql, 'id', false);
return ($res && count($res) == 0) ? array() : $res;
}
/*
* Queries all tags in database
*/
function defined_tags($max_tags) {
$sql = 'SELECT name FROM ' . TAGS_TABLE . " ORDER BY lastmodified ASC LIMIT $max_tags;";
$x = fetch_sql($sql, 'name', false);
return ($x == 0) ? [] : $x;
}
/*
* Queries tagged faces for the image id
*/
function defined_mugshots( $id ) {
$mugshotSql = '
SELECT
mst.image_id,
mst.tag_id,
mst.top,
mst.lft,
mst.width,
mst.height,
mst.image_width,
mst.image_height,
tt.name
FROM ' . MUGSHOT_TABLE . ' AS mst
INNER JOIN `' . TAGS_TABLE . '` AS tt ON mst.tag_id = tt.id
WHERE mst.image_id = ' . $id . ';';
$mugshotSqlResult = fetch_sql($mugshotSql, false, false);
if (is_array($mugshotSqlResult)) {
foreach($mugshotSqlResult as $key => $mugshot) {
$tagSql = '
SELECT
id,
url_name
FROM ' . TAGS_TABLE . '
WHERE id=' . $mugshot['tag_id'] . ';
';
$tagSqlResult = fetch_sql($tagSql, false, false);
$tagUrl = make_index_url(array('tags' => array($tagSqlResult[0])));
$mugshotSqlResult[$key]['tag_url'] = $tagUrl;
}
}
return json_encode($mugshotSqlResult);
}
/*
* Insert the tag button on photo pages
*/
function mugshot_button() {
if(script_basename() != 'admin') {
global $template, $page;
/*
* Path to processing file
*/
$url = get_root_url() . 'ws.php?format=json&method=mugshot.bookem';
/*
* Assign template variables
*/
$template -> assign('MUGSHOT_BUTTON', realpath(MUGSHOT_PATH));
$template -> assign('MUGSHOT_ACTION', $url);
$template -> assign('IMAGE_ID', $page['image_id']);
$template -> assign('MUGSHOTS', defined_mugshots($page['image_id']));
/*
* Parse button template file and append to picture buttons
*/
$template -> set_filename('button', realpath(MUGSHOT_PATH . 'template/button.tpl'));
$button = $template -> parse('button', true);
$template -> add_picture_button($button, 1);
$template -> parse_picture_buttons();
}
}
/*
* Insert list of tags for autopopulating tags
*/
function insert_tag_list() {
global $template;
$plugin_config = safe_unserialize(conf_get_param(MUGSHOT_ID));
$max_tags = $plugin_config['max_tags'] ?? 500;
/*
* Array of tags
*/
$template -> assign('MUGSHOT_TAG_LIST', defined_tags($max_tags));
/*
* Specify the tag list template file
*/
$template -> set_filename('MUGSHOT_TAG_TEMP', realpath(MUGSHOT_PATH . 'template/taglist.tpl'));
/*
* Parse template file and append to main template
*/
$template -> append('footer_elements', $template -> parse('MUGSHOT_TAG_TEMP', false));
}
?>