forked from cccraig/MugShot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.inc.php
261 lines (206 loc) · 5.65 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
<?php
/*
Plugin Name: Mug Shot
Version: 1.2.1
Description: Improved face tagging for Piwigo
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=867
Author: cccraig
*/
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
/*
* 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_VERSION', '1.2.1');
define('MUGSHOT_TABLE', '`face_tag_positions`');
/*
* API Functions
*/
$ws_file = MUGSHOT_PATH . 'include/capture.php';
/*
* Admin Event Handlers
*/
add_event_handler('get_admin_plugin_menu_links', 'mugshot_admin_menu');
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');
/*
* Conditional Logic for groups
*/
$current_user_groups = query_mugshot_groups();
if ($current_user_groups != 0) {
$plugin_config = unserialize(conf_get_param(MUGSHOT_ID));
$group_list = $plugin_config['groups'];
// From github cccraig/MugShot commit f12b1a40a4e0f20968dcbfb20b39e28217e7a34c
$intersect = [];
if (is_array ($group_list)) {
$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);
}
/*
* Fetches Sql
*/
function fetch_sql($sql, $col, $ser) {
$result = pwg_query($sql);
while ($row = pwg_db_fetch_assoc($result)) {
$data[] = $row;
}
if (!isset($data)) {
$data = 0;
} else {
if($col !== false) {
$data = array_column($data, $col);
}
}
return ($ser) ? json_encode($data) : $data;
}
/*
* Loads translations
*/
function mugshot_lang_init(){
load_language('plugin.lang', MUGSHOT_PATH);
}
/*
* Initializes the admin menu
*/
function mugshot_admin_menu( $menu ) {
array_push(
$menu,
array(
'NAME' => 'MugShot',
'URL' => get_admin_plugin_menu_link(dirname(__FILE__)).'/admin.php'
)
);
return $menu;
}
/*
* 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 0;
}
$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 empty($res) ? 0 : $res;
}
/*
* Queries all tags in database
*/
function defined_tags() {
$sql = 'SELECT name FROM ' . TAGS_TABLE . ' ORDER BY lastmodified ASC LIMIT 100;';
$x = fetch_sql($sql, 'name', false);
return ($x == 0) ? [] : $x;
}
/*
* Queries tagged faces for the image id
*/
function defined_mugshots( $id ) {
$sql = '
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 . ';';
return fetch_sql($sql, false, true);
}
/*
* 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;
/*
* Array of tags
*/
$template -> assign('MUGSHOT_TAG_LIST', defined_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));
}
?>