1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file takes care of all administration of smileys. |
||
5 | * |
||
6 | * Simple Machines Forum (SMF) |
||
7 | * |
||
8 | * @package SMF |
||
9 | * @author Simple Machines https://www.simplemachines.org |
||
10 | * @copyright 2020 Simple Machines and individual contributors |
||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1 RC2 |
||
14 | */ |
||
15 | |||
16 | if (!defined('SMF')) |
||
17 | die('No direct access...'); |
||
18 | |||
19 | /** |
||
20 | * This is the dispatcher of smileys administration. |
||
21 | */ |
||
22 | function ManageSmileys() |
||
23 | { |
||
24 | global $context, $txt, $modSettings; |
||
25 | |||
26 | isAllowedTo('manage_smileys'); |
||
27 | |||
28 | loadLanguage('ManageSmileys'); |
||
29 | loadTemplate('ManageSmileys'); |
||
30 | |||
31 | $subActions = array( |
||
32 | 'addsmiley' => 'AddSmiley', |
||
33 | 'editicon' => 'EditMessageIcons', |
||
34 | 'editicons' => 'EditMessageIcons', |
||
35 | 'editsets' => 'EditSmileySets', |
||
36 | 'editsmileys' => 'EditSmileys', |
||
37 | 'import' => 'EditSmileySets', |
||
38 | 'modifyset' => 'EditSmileySets', |
||
39 | 'modifysmiley' => 'EditSmileys', |
||
40 | 'setorder' => 'EditSmileyOrder', |
||
41 | 'settings' => 'EditSmileySettings', |
||
42 | 'install' => 'InstallSmileySet' |
||
43 | ); |
||
44 | |||
45 | // If customized smileys is disabled don't show the setting page |
||
46 | if (empty($modSettings['smiley_enable'])) |
||
47 | { |
||
48 | unset($subActions['addsmiley']); |
||
49 | unset($subActions['editsmileys']); |
||
50 | unset($subActions['setorder']); |
||
51 | unset($subActions['modifysmiley']); |
||
52 | } |
||
53 | if (empty($modSettings['messageIcons_enable'])) |
||
54 | { |
||
55 | unset($subActions['editicon']); |
||
56 | unset($subActions['editicons']); |
||
57 | } |
||
58 | |||
59 | // Default the sub-action to 'edit smiley settings'. |
||
60 | $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'editsets'; |
||
61 | |||
62 | $context['page_title'] = $txt['smileys_manage']; |
||
63 | $context['sub_action'] = $_REQUEST['sa']; |
||
64 | $context['sub_template'] = $context['sub_action']; |
||
65 | |||
66 | // Load up all the tabs... |
||
67 | $context[$context['admin_menu_name']]['tab_data'] = array( |
||
68 | 'title' => $txt['smileys_manage'], |
||
69 | 'help' => 'smileys', |
||
70 | 'description' => $txt['smiley_settings_explain'], |
||
71 | 'tabs' => array( |
||
72 | 'editsets' => array( |
||
73 | 'description' => $txt['smiley_editsets_explain'], |
||
74 | ), |
||
75 | 'addsmiley' => array( |
||
76 | 'description' => $txt['smiley_addsmiley_explain'], |
||
77 | ), |
||
78 | 'editsmileys' => array( |
||
79 | 'description' => $txt['smiley_editsmileys_explain'], |
||
80 | ), |
||
81 | 'setorder' => array( |
||
82 | 'description' => $txt['smiley_setorder_explain'], |
||
83 | ), |
||
84 | 'editicons' => array( |
||
85 | 'description' => $txt['icons_edit_icons_explain'], |
||
86 | ), |
||
87 | 'settings' => array( |
||
88 | 'description' => $txt['smiley_settings_explain'], |
||
89 | ), |
||
90 | ), |
||
91 | ); |
||
92 | |||
93 | // Some settings may not be enabled, disallow these from the tabs as appropriate. |
||
94 | if (empty($modSettings['messageIcons_enable'])) |
||
95 | $context[$context['admin_menu_name']]['tab_data']['tabs']['editicons']['disabled'] = true; |
||
96 | if (empty($modSettings['smiley_enable'])) |
||
97 | { |
||
98 | $context[$context['admin_menu_name']]['tab_data']['tabs']['addsmiley']['disabled'] = true; |
||
99 | $context[$context['admin_menu_name']]['tab_data']['tabs']['editsmileys']['disabled'] = true; |
||
100 | $context[$context['admin_menu_name']]['tab_data']['tabs']['setorder']['disabled'] = true; |
||
101 | } |
||
102 | |||
103 | call_integration_hook('integrate_manage_smileys', array(&$subActions)); |
||
104 | |||
105 | // Call the right function for this sub-action. |
||
106 | call_helper($subActions[$_REQUEST['sa']]); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Handles modifying smileys settings. |
||
111 | * |
||
112 | * @param bool $return_config Whether or not to return the config_vars array (used for admin search) |
||
113 | * @return void|array Returns nothing or returns the $config_vars array if $return_config is true |
||
114 | */ |
||
115 | function EditSmileySettings($return_config = false) |
||
116 | { |
||
117 | global $modSettings, $context, $txt, $boarddir, $sourcedir, $scripturl; |
||
118 | |||
119 | // The directories... |
||
120 | $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir']; |
||
121 | $context['smileys_dir_found'] = is_dir($context['smileys_dir']); |
||
122 | |||
123 | // Get the names of the smiley sets. |
||
124 | $smiley_sets = explode(',', $modSettings['smiley_sets_known']); |
||
125 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
126 | |||
127 | $smiley_context = array(); |
||
128 | foreach ($smiley_sets as $i => $set) |
||
129 | $smiley_context[$set] = $set_names[$i]; |
||
130 | |||
131 | // All the settings for the page... |
||
132 | $config_vars = array( |
||
133 | array('title', 'settings'), |
||
134 | // Inline permissions. |
||
135 | array('permissions', 'manage_smileys'), |
||
136 | '', |
||
137 | |||
138 | array('select', 'smiley_sets_default', $smiley_context), |
||
139 | array('check', 'smiley_sets_enable'), |
||
140 | array('check', 'smiley_enable', 'subtext' => $txt['smileys_enable_note']), |
||
141 | array('text', 'smileys_url', 40), |
||
142 | array('warning', !is_dir($context['smileys_dir']) ? 'setting_smileys_dir_wrong' : ''), |
||
143 | array('text', 'smileys_dir', 'invalid' => !$context['smileys_dir_found'], 40), |
||
144 | '', |
||
145 | |||
146 | // Message icons. |
||
147 | array('check', 'messageIcons_enable', 'subtext' => $txt['setting_messageIcons_enable_note']), |
||
148 | array('check', 'messageIconChecks_enable', 'subtext' => $txt['setting_messageIconChecks_enable_note']) |
||
149 | ); |
||
150 | |||
151 | call_integration_hook('integrate_modify_smiley_settings', array(&$config_vars)); |
||
152 | |||
153 | if ($return_config) |
||
154 | return $config_vars; |
||
155 | |||
156 | // Setup the basics of the settings template. |
||
157 | require_once($sourcedir . '/ManageServer.php'); |
||
158 | $context['sub_template'] = 'show_settings'; |
||
159 | |||
160 | // Finish up the form... |
||
161 | $context['post_url'] = $scripturl . '?action=admin;area=smileys;save;sa=settings'; |
||
162 | |||
163 | // Saving the settings? |
||
164 | if (isset($_GET['save'])) |
||
165 | { |
||
166 | checkSession(); |
||
167 | |||
168 | // Validate the smiley set name. |
||
169 | $_POST['smiley_sets_default'] = empty($smiley_context[$_POST['smiley_sets_default']]) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_default']; |
||
170 | |||
171 | call_integration_hook('integrate_save_smiley_settings'); |
||
172 | |||
173 | saveDBSettings($config_vars); |
||
174 | $_SESSION['adm-save'] = true; |
||
175 | |||
176 | foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set) |
||
177 | { |
||
178 | cache_put_data('parsing_smileys_' . $smiley_set, null, 480); |
||
179 | cache_put_data('posting_smileys_' . $smiley_set, null, 480); |
||
180 | } |
||
181 | |||
182 | redirectexit('action=admin;area=smileys;sa=settings'); |
||
183 | } |
||
184 | |||
185 | // We need this for the in-line permissions |
||
186 | createToken('admin-mp'); |
||
187 | |||
188 | prepareDBSettingContext($config_vars); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * List, add, remove, modify smileys sets. |
||
193 | */ |
||
194 | function EditSmileySets() |
||
195 | { |
||
196 | global $modSettings, $context, $txt; |
||
197 | global $smcFunc, $scripturl, $sourcedir; |
||
198 | |||
199 | // Set the right tab to be selected. |
||
200 | $context[$context['admin_menu_name']]['current_subsection'] = 'editsets'; |
||
201 | |||
202 | $allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg'); |
||
203 | |||
204 | // They must've been submitted a form. |
||
205 | if (isset($_POST['smiley_save'])) |
||
206 | { |
||
207 | checkSession(); |
||
208 | validateToken('admin-mss', 'request'); |
||
209 | |||
210 | // Delete selected smiley sets. |
||
211 | if (!empty($_POST['delete']) && !empty($_POST['smiley_set'])) |
||
212 | { |
||
213 | $set_paths = explode(',', $modSettings['smiley_sets_known']); |
||
214 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
215 | foreach ($_POST['smiley_set'] as $id => $val) |
||
216 | { |
||
217 | // If this is the set you've marked as default, or the only one remaining, you can't delete it |
||
218 | if ($modSettings['smiley_sets_default'] != $set_paths[$id] && count($set_paths) != 1 && isset($set_paths[$id], $set_names[$id])) |
||
219 | { |
||
220 | // Delete this set's entries from the smiley_files table |
||
221 | $smcFunc['db_query']('', ' |
||
222 | DELETE FROM {db_prefix}smiley_files |
||
223 | WHERE smiley_set = {string:smiley_set}', |
||
224 | array( |
||
225 | 'smiley_set' => $set_paths[$id], |
||
226 | ) |
||
227 | ); |
||
228 | |||
229 | // Remove this set from our lists |
||
230 | unset($set_paths[$id], $set_names[$id]); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | // Shortcut... array_merge() on a single array resets the numeric keys |
||
235 | $set_paths = array_merge($set_paths); |
||
236 | $set_names = array_merge($set_names); |
||
237 | |||
238 | updateSettings(array( |
||
239 | 'smiley_sets_known' => implode(',', $set_paths), |
||
240 | 'smiley_sets_names' => implode("\n", $set_names), |
||
241 | 'smiley_sets_default' => in_array($modSettings['smiley_sets_default'], $set_paths) ? $modSettings['smiley_sets_default'] : $set_paths[0], |
||
242 | )); |
||
243 | } |
||
244 | // Add a new smiley set. |
||
245 | elseif (!empty($_POST['add'])) |
||
246 | $context['sub_action'] = 'modifyset'; |
||
247 | // Create or modify a smiley set. |
||
248 | elseif (isset($_POST['set'])) |
||
249 | { |
||
250 | $set_paths = explode(',', $modSettings['smiley_sets_known']); |
||
251 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
252 | |||
253 | // Create a new smiley set. |
||
254 | if ($_POST['set'] == -1 && isset($_POST['smiley_sets_path'])) |
||
255 | { |
||
256 | if (in_array($_POST['smiley_sets_path'], $set_paths)) |
||
257 | fatal_lang_error('smiley_set_already_exists'); |
||
258 | |||
259 | updateSettings(array( |
||
260 | 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $_POST['smiley_sets_path'], |
||
261 | 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $_POST['smiley_sets_name'], |
||
262 | 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'], |
||
263 | )); |
||
264 | } |
||
265 | // Modify an existing smiley set. |
||
266 | else |
||
267 | { |
||
268 | // Make sure the smiley set exists. |
||
269 | if (!isset($set_paths[$_POST['set']]) || !isset($set_names[$_POST['set']])) |
||
270 | fatal_lang_error('smiley_set_not_found'); |
||
271 | |||
272 | // Make sure the path is not yet used by another smileyset. |
||
273 | if (in_array($_POST['smiley_sets_path'], $set_paths) && $_POST['smiley_sets_path'] != $set_paths[$_POST['set']]) |
||
274 | fatal_lang_error('smiley_set_path_already_used'); |
||
275 | |||
276 | $set_paths[$_POST['set']] = $_POST['smiley_sets_path']; |
||
277 | $set_names[$_POST['set']] = $_POST['smiley_sets_name']; |
||
278 | updateSettings(array( |
||
279 | 'smiley_sets_known' => implode(',', $set_paths), |
||
280 | 'smiley_sets_names' => implode("\n", $set_names), |
||
281 | 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'] |
||
282 | )); |
||
283 | } |
||
284 | |||
285 | // Import, but only the ones that match existing smileys |
||
286 | ImportSmileys($_POST['smiley_sets_path'], false); |
||
287 | } |
||
288 | |||
289 | foreach ($set_paths as $smiley_set) |
||
290 | { |
||
291 | cache_put_data('parsing_smileys_' . $smiley_set, null, 480); |
||
292 | cache_put_data('posting_smileys_' . $smiley_set, null, 480); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | // Load all available smileysets... |
||
297 | $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']); |
||
298 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
299 | foreach ($context['smiley_sets'] as $i => $set) |
||
300 | $context['smiley_sets'][$i] = array( |
||
301 | 'id' => $i, |
||
302 | 'raw_path' => $set, |
||
303 | 'path' => $smcFunc['htmlspecialchars']($set), |
||
304 | 'name' => $smcFunc['htmlspecialchars']($set_names[$i]), |
||
305 | 'selected' => $set == $modSettings['smiley_sets_default'] |
||
306 | ); |
||
307 | |||
308 | // Importing any smileys from an existing set? |
||
309 | if ($context['sub_action'] == 'import') |
||
310 | { |
||
311 | checkSession('get'); |
||
312 | validateToken('admin-mss', 'request'); |
||
313 | |||
314 | $_GET['set'] = (int) $_GET['set']; |
||
315 | |||
316 | // Sanity check - then import. |
||
317 | if (isset($context['smiley_sets'][$_GET['set']])) |
||
318 | ImportSmileys(un_htmlspecialchars($context['smiley_sets'][$_GET['set']]['path']), true); |
||
319 | |||
320 | // Force the process to continue. |
||
321 | $context['sub_action'] = 'modifyset'; |
||
322 | $context['sub_template'] = 'modifyset'; |
||
323 | } |
||
324 | // If we're modifying or adding a smileyset, some context info needs to be set. |
||
325 | if ($context['sub_action'] == 'modifyset') |
||
326 | { |
||
327 | $_GET['set'] = !isset($_GET['set']) ? -1 : (int) $_GET['set']; |
||
328 | if ($_GET['set'] == -1 || !isset($context['smiley_sets'][$_GET['set']])) |
||
329 | $context['current_set'] = array( |
||
330 | 'id' => '-1', |
||
331 | 'raw_path' => '', |
||
332 | 'path' => '', |
||
333 | 'name' => '', |
||
334 | 'selected' => false, |
||
335 | 'is_new' => true, |
||
336 | ); |
||
337 | else |
||
338 | { |
||
339 | $context['current_set'] = &$context['smiley_sets'][$_GET['set']]; |
||
340 | $context['current_set']['is_new'] = false; |
||
341 | |||
342 | // Calculate whether there are any smileys in the directory that can be imported. |
||
343 | if (!empty($modSettings['smiley_enable']) && !empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path'])) |
||
344 | { |
||
345 | $smileys = array(); |
||
346 | $dir = dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']); |
||
347 | while ($entry = $dir->read()) |
||
348 | { |
||
349 | $pathinfo = pathinfo($entry); |
||
350 | if (empty($pathinfo['filename']) || empty($pathinfo['extension'])) |
||
351 | continue; |
||
352 | if (in_array($pathinfo['extension'], $allowedTypes) && $pathinfo['filename'] != 'blank') |
||
353 | $smileys[strtolower($entry)] = $entry; |
||
354 | } |
||
355 | $dir->close(); |
||
356 | |||
357 | if (empty($smileys)) |
||
358 | fatal_lang_error('smiley_set_dir_not_found', false, array($context['current_set']['name'])); |
||
359 | |||
360 | // Exclude the smileys that are already in the database. |
||
361 | $request = $smcFunc['db_query']('', ' |
||
362 | SELECT filename |
||
363 | FROM {db_prefix}smiley_files |
||
364 | WHERE filename IN ({array_string:smiley_list}) |
||
365 | AND smiley_set = {string:smiley_set}', |
||
366 | array( |
||
367 | 'smiley_list' => $smileys, |
||
368 | 'smiley_set' => $context['current_set']['path'], |
||
369 | ) |
||
370 | ); |
||
371 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
372 | { |
||
373 | if (isset($smileys[strtolower($row['filename'])])) |
||
374 | unset($smileys[strtolower($row['filename'])]); |
||
375 | } |
||
376 | |||
377 | $smcFunc['db_free_result']($request); |
||
378 | |||
379 | $context['current_set']['can_import'] = count($smileys); |
||
380 | $context['current_set']['import_url'] = $scripturl . '?action=admin;area=smileys;sa=import;set=' . $context['current_set']['id'] . ';' . $context['session_var'] . '=' . $context['session_id']; |
||
381 | } |
||
382 | } |
||
383 | |||
384 | // Retrieve all potential smiley set directories. |
||
385 | $context['smiley_set_dirs'] = array(); |
||
386 | if (!empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'])) |
||
387 | { |
||
388 | $dir = dir($modSettings['smileys_dir']); |
||
389 | while ($entry = $dir->read()) |
||
390 | { |
||
391 | if (!in_array($entry, array('.', '..')) && is_dir($modSettings['smileys_dir'] . '/' . $entry)) |
||
392 | $context['smiley_set_dirs'][] = array( |
||
393 | 'id' => $entry, |
||
394 | 'path' => $modSettings['smileys_dir'] . '/' . $entry, |
||
395 | 'selectable' => $entry == $context['current_set']['path'] || !in_array($entry, explode(',', $modSettings['smiley_sets_known'])), |
||
396 | 'current' => $entry == $context['current_set']['path'], |
||
397 | ); |
||
398 | } |
||
399 | $dir->close(); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | // This is our save haven. |
||
404 | createToken('admin-mss', 'request'); |
||
405 | |||
406 | // In case we need to import smileys, we need to add the token in now. |
||
407 | if (isset($context['current_set']['import_url'])) |
||
408 | { |
||
409 | $context['current_set']['import_url'] .= ';' . $context['admin-mss_token_var'] . '=' . $context['admin-mss_token']; |
||
410 | $context['smiley_set_unused_message'] = sprintf($txt['smiley_set_unused'], $scripturl . '?action=admin;area=smileys;sa=editsmileys', $scripturl . '?action=admin;area=smileys;sa=addsmiley', $context['current_set']['import_url']); |
||
411 | } |
||
412 | |||
413 | $listOptions = array( |
||
414 | 'id' => 'smiley_set_list', |
||
415 | 'title' => $txt['smiley_sets'], |
||
416 | 'no_items_label' => $txt['smiley_sets_none'], |
||
417 | 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsets', |
||
418 | 'default_sort_col' => 'name', |
||
419 | 'get_items' => array( |
||
420 | 'function' => 'list_getSmileySets', |
||
421 | ), |
||
422 | 'get_count' => array( |
||
423 | 'function' => 'list_getNumSmileySets', |
||
424 | ), |
||
425 | 'columns' => array( |
||
426 | 'default' => array( |
||
427 | 'header' => array( |
||
428 | 'value' => $txt['smiley_sets_default'], |
||
429 | 'class' => 'centercol', |
||
430 | ), |
||
431 | 'data' => array( |
||
432 | 'function' => function($rowData) |
||
433 | { |
||
434 | return $rowData['selected'] ? '<span class="main_icons valid"></span>' : ''; |
||
435 | }, |
||
436 | 'class' => 'centercol', |
||
437 | ), |
||
438 | 'sort' => array( |
||
439 | 'default' => 'selected', |
||
440 | 'reverse' => 'selected DESC', |
||
441 | ), |
||
442 | ), |
||
443 | 'name' => array( |
||
444 | 'header' => array( |
||
445 | 'value' => $txt['smiley_sets_name'], |
||
446 | ), |
||
447 | 'data' => array( |
||
448 | 'db_htmlsafe' => 'name', |
||
449 | ), |
||
450 | 'sort' => array( |
||
451 | 'default' => 'name', |
||
452 | 'reverse' => 'name DESC', |
||
453 | ), |
||
454 | ), |
||
455 | 'url' => array( |
||
456 | 'header' => array( |
||
457 | 'value' => $txt['smiley_sets_url'], |
||
458 | ), |
||
459 | 'data' => array( |
||
460 | 'sprintf' => array( |
||
461 | 'format' => $modSettings['smileys_url'] . '/<strong>%1$s</strong>/...', |
||
462 | 'params' => array( |
||
463 | 'path' => true, |
||
464 | ), |
||
465 | ), |
||
466 | ), |
||
467 | 'sort' => array( |
||
468 | 'default' => 'path', |
||
469 | 'reverse' => 'path DESC', |
||
470 | ), |
||
471 | ), |
||
472 | 'modify' => array( |
||
473 | 'header' => array( |
||
474 | 'value' => $txt['smiley_set_modify'], |
||
475 | 'class' => 'centercol', |
||
476 | ), |
||
477 | 'data' => array( |
||
478 | 'sprintf' => array( |
||
479 | 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset;set=%1$d">' . $txt['smiley_set_modify'] . '</a>', |
||
480 | 'params' => array( |
||
481 | 'id' => true, |
||
482 | ), |
||
483 | ), |
||
484 | 'class' => 'centercol', |
||
485 | ), |
||
486 | ), |
||
487 | 'check' => array( |
||
488 | 'header' => array( |
||
489 | 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">', |
||
490 | 'class' => 'centercol', |
||
491 | ), |
||
492 | 'data' => array( |
||
493 | 'function' => function($rowData) |
||
494 | { |
||
495 | return $rowData['selected'] ? '' : sprintf('<input type="checkbox" name="smiley_set[%1$d]">', $rowData['id']); |
||
496 | }, |
||
497 | 'class' => 'centercol', |
||
498 | ), |
||
499 | ), |
||
500 | ), |
||
501 | 'form' => array( |
||
502 | 'href' => $scripturl . '?action=admin;area=smileys;sa=editsets', |
||
503 | 'token' => 'admin-mss', |
||
504 | ), |
||
505 | 'additional_rows' => array( |
||
506 | array( |
||
507 | 'position' => 'above_table_headers', |
||
508 | 'value' => '<input type="hidden" name="smiley_save"><input type="submit" name="delete" value="' . $txt['smiley_sets_delete'] . '" data-confirm="' . $txt['smiley_sets_confirm'] . '" class="button you_sure"> <a class="button" href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset' . '">' . $txt['smiley_sets_add'] . '</a> ', |
||
509 | ), |
||
510 | array( |
||
511 | 'position' => 'below_table_data', |
||
512 | 'value' => '<input type="hidden" name="smiley_save"><input type="submit" name="delete" value="' . $txt['smiley_sets_delete'] . '" data-confirm="' . $txt['smiley_sets_confirm'] . '" class="button you_sure"> <a class="button" href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset' . '">' . $txt['smiley_sets_add'] . '</a> ', |
||
513 | ), |
||
514 | ), |
||
515 | ); |
||
516 | |||
517 | require_once($sourcedir . '/Subs-List.php'); |
||
518 | createList($listOptions); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Callback function for createList(). |
||
523 | * |
||
524 | * @todo to be moved to Subs-Smileys? |
||
525 | * |
||
526 | * @param int $start The item to start with (not used here) |
||
527 | * @param int $items_per_page The number of items to show per page (not used here) |
||
528 | * @param string $sort A string indicating how to sort the results |
||
529 | * @return array An array of info about the smiley sets |
||
530 | */ |
||
531 | function list_getSmileySets($start, $items_per_page, $sort) |
||
532 | { |
||
533 | global $modSettings; |
||
534 | |||
535 | $known_sets = explode(',', $modSettings['smiley_sets_known']); |
||
536 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
537 | $cols = array( |
||
538 | 'id' => array(), |
||
539 | 'selected' => array(), |
||
540 | 'path' => array(), |
||
541 | 'name' => array(), |
||
542 | ); |
||
543 | foreach ($known_sets as $i => $set) |
||
544 | { |
||
545 | $cols['id'][] = $i; |
||
546 | $cols['selected'][] = $set == $modSettings['smiley_sets_default']; |
||
547 | $cols['path'][] = $set; |
||
548 | $cols['name'][] = $set_names[$i]; |
||
549 | } |
||
550 | $sort_flag = strpos($sort, 'DESC') === false ? SORT_ASC : SORT_DESC; |
||
551 | if (substr($sort, 0, 4) === 'name') |
||
552 | array_multisort($cols['name'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['selected'], $cols['id']); |
||
553 | elseif (substr($sort, 0, 4) === 'path') |
||
554 | array_multisort($cols['path'], $sort_flag, SORT_REGULAR, $cols['name'], $cols['selected'], $cols['id']); |
||
555 | else |
||
556 | array_multisort($cols['selected'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['name'], $cols['id']); |
||
557 | |||
558 | $smiley_sets = array(); |
||
559 | foreach ($cols['id'] as $i => $id) |
||
560 | $smiley_sets[] = array( |
||
561 | 'id' => $id, |
||
562 | 'path' => $cols['path'][$i], |
||
563 | 'name' => $cols['name'][$i], |
||
564 | 'selected' => $cols['selected'][$i], |
||
565 | ); |
||
566 | |||
567 | return $smiley_sets; |
||
568 | } |
||
569 | |||
570 | /** |
||
571 | * Callback function for createList(). |
||
572 | * |
||
573 | * @todo to be moved to Subs-Smileys? |
||
574 | * @return int The total number of known smiley sets |
||
575 | */ |
||
576 | function list_getNumSmileySets() |
||
577 | { |
||
578 | global $modSettings; |
||
579 | |||
580 | return count(explode(',', $modSettings['smiley_sets_known'])); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Add a smiley, that's right. |
||
585 | */ |
||
586 | function AddSmiley() |
||
587 | { |
||
588 | global $modSettings, $context, $txt, $boarddir, $smcFunc; |
||
589 | |||
590 | // Get a list of all known smiley sets. |
||
591 | $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir']; |
||
592 | $context['smileys_dir_found'] = is_dir($context['smileys_dir']); |
||
593 | $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']); |
||
594 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
595 | foreach ($context['smiley_sets'] as $i => $set) |
||
596 | $context['smiley_sets'][$i] = array( |
||
597 | 'id' => $i, |
||
598 | 'raw_path' => $set, |
||
599 | 'path' => $smcFunc['htmlspecialchars']($set), |
||
600 | 'name' => $smcFunc['htmlspecialchars']($set_names[$i]), |
||
601 | 'selected' => $set == $modSettings['smiley_sets_default'] |
||
602 | ); |
||
603 | |||
604 | // Some useful arrays... types we allow - and ports we don't! |
||
605 | $allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg'); |
||
606 | $disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php'); |
||
607 | |||
608 | // This will hold the names of the added files for each set |
||
609 | $filename_array = array(); |
||
610 | |||
611 | // Submitting a form? |
||
612 | if (isset($_POST[$context['session_var']], $_POST['smiley_code'])) |
||
613 | { |
||
614 | checkSession(); |
||
615 | |||
616 | $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']); |
||
617 | $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location']; |
||
618 | $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']); |
||
619 | |||
620 | // Make sure some code was entered. |
||
621 | if (empty($_POST['smiley_code'])) |
||
622 | fatal_lang_error('smiley_has_no_code'); |
||
623 | |||
624 | // Check whether the new code has duplicates. It should be unique. |
||
625 | $request = $smcFunc['db_query']('', ' |
||
626 | SELECT id_smiley |
||
627 | FROM {db_prefix}smileys |
||
628 | WHERE code = {raw:mysql_binary_statement} {string:smiley_code}', |
||
629 | array( |
||
630 | 'mysql_binary_statement' => $smcFunc['db_title'] == MYSQL_TITLE ? 'BINARY' : '', |
||
631 | 'smiley_code' => $_POST['smiley_code'], |
||
632 | ) |
||
633 | ); |
||
634 | if ($smcFunc['db_num_rows']($request) > 0) |
||
635 | fatal_lang_error('smiley_not_unique'); |
||
636 | $smcFunc['db_free_result']($request); |
||
637 | |||
638 | // If we are uploading - check all the smiley sets are writable! |
||
639 | if ($_POST['method'] != 'existing') |
||
640 | { |
||
641 | $writeErrors = array(); |
||
642 | foreach ($context['smiley_sets'] as $set) |
||
643 | { |
||
644 | if (!is_writable($context['smileys_dir'] . '/' . $set['raw_path'])) |
||
645 | $writeErrors[] = $set['path']; |
||
646 | } |
||
647 | if (!empty($writeErrors)) |
||
648 | fatal_lang_error('smileys_upload_error_notwritable', true, array(implode(', ', $writeErrors))); |
||
649 | } |
||
650 | |||
651 | // Uploading just one smiley for all of them? |
||
652 | if (isset($_POST['sameall']) && isset($_FILES['uploadSmiley']['name']) && $_FILES['uploadSmiley']['name'] != '') |
||
653 | { |
||
654 | if (!is_uploaded_file($_FILES['uploadSmiley']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['uploadSmiley']['tmp_name']))) |
||
655 | fatal_lang_error('smileys_upload_error'); |
||
656 | |||
657 | // Sorry, no spaces, dots, or anything else but letters allowed. |
||
658 | $_FILES['uploadSmiley']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['uploadSmiley']['name']); |
||
659 | |||
660 | // We only allow image files - it's THAT simple - no messing around here... |
||
661 | if (!in_array(strtolower(pathinfo($_FILES['uploadSmiley']['name'], PATHINFO_EXTENSION)), $allowedTypes)) |
||
662 | fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes))); |
||
663 | |||
664 | // We only need the filename... |
||
665 | $destName = basename($_FILES['uploadSmiley']['name']); |
||
666 | |||
667 | // Make sure they aren't trying to upload a nasty file - for their own good here! |
||
668 | if (in_array(strtolower($destName), $disabledFiles)) |
||
669 | fatal_lang_error('smileys_upload_error_illegal'); |
||
670 | |||
671 | // Check if the file already exists... and if not move it to EVERY smiley set directory. |
||
672 | $smileyLocation = null; |
||
673 | foreach ($context['smiley_sets'] as $i => $set) |
||
674 | { |
||
675 | // Okay, we're going to put the smiley right here, since it's not there yet! |
||
676 | if (!file_exists($context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['raw_path']) . '/' . $destName) |
||
677 | { |
||
678 | $smileyLocation = $context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['raw_path'] . '/' . $destName; |
||
679 | move_uploaded_file($_FILES['uploadSmiley']['tmp_name'], $smileyLocation); |
||
680 | smf_chmod($smileyLocation, 0644); |
||
681 | break; |
||
682 | } |
||
683 | } |
||
684 | |||
685 | // Now, we want to move it from there to all the other sets. |
||
686 | foreach ($context['smiley_sets'] as $j => $set) |
||
687 | { |
||
688 | $currentPath = $context['smileys_dir'] . '/' . $context['smiley_sets'][$j]['raw_path'] . '/' . $destName; |
||
689 | |||
690 | // Copy the first one we made to here, unless it already exists there |
||
691 | if (!empty($smileyLocation) && !file_exists($currentPath)) |
||
692 | { |
||
693 | copy($smileyLocation, $currentPath); |
||
694 | smf_chmod($currentPath, 0644); |
||
695 | } |
||
696 | |||
697 | // Double-check |
||
698 | if (!file_exists($currentPath)) |
||
699 | fatal_lang_error('smiley_not_found'); |
||
700 | |||
701 | // Finally make sure it's saved correctly! |
||
702 | $filename_array[$context['smiley_sets'][$j]['raw_path']] = $destName; |
||
703 | } |
||
704 | } |
||
705 | // What about uploading several files? |
||
706 | elseif ($_POST['method'] != 'existing') |
||
707 | { |
||
708 | $newName = ''; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
709 | foreach ($_FILES as $name => $data) |
||
710 | { |
||
711 | if ($_FILES[$name]['name'] == '') |
||
712 | fatal_lang_error('smileys_upload_error_blank'); |
||
713 | |||
714 | // if (empty($newName)) |
||
715 | // $newName = basename($_FILES[$name]['name']); |
||
716 | // elseif (basename($_FILES[$name]['name']) != $newName) |
||
717 | // fatal_lang_error('smileys_upload_error_name'); |
||
718 | } |
||
719 | |||
720 | foreach ($context['smiley_sets'] as $i => $set) |
||
721 | { |
||
722 | $set['name'] = un_htmlspecialchars($set['name']); |
||
723 | |||
724 | if (!isset($_FILES['individual_' . $set['raw_path']]['name']) || $_FILES['individual_' . $set['raw_path']]['name'] == '') |
||
725 | continue; |
||
726 | |||
727 | // Got one... |
||
728 | if (!is_uploaded_file($_FILES['individual_' . $set['raw_path']]['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['individual_' . $set['raw_path']]['tmp_name']))) |
||
729 | fatal_lang_error('smileys_upload_error'); |
||
730 | |||
731 | // Sorry, no spaces, dots, or anything else but letters allowed. |
||
732 | $_FILES['individual_' . $set['raw_path']]['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['individual_' . $set['raw_path']]['name']); |
||
733 | |||
734 | // We only allow image files - it's THAT simple - no messing around here... |
||
735 | if (!in_array(strtolower(pathinfo($_FILES['individual_' . $set['raw_path']]['name'], PATHINFO_EXTENSION)), $allowedTypes)) |
||
736 | fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes))); |
||
737 | |||
738 | // We only need the filename... |
||
739 | $destName = basename($_FILES['individual_' . $set['raw_path']]['name']); |
||
740 | |||
741 | // Make sure they aren't trying to upload a nasty file - for their own good here! |
||
742 | if (in_array(strtolower($destName), $disabledFiles)) |
||
743 | fatal_lang_error('smileys_upload_error_illegal'); |
||
744 | |||
745 | // If the file exists - ignore it. |
||
746 | $smileyLocation = $context['smileys_dir'] . '/' . $set['raw_path'] . '/' . $destName; |
||
747 | if (!file_exists($smileyLocation)) |
||
748 | { |
||
749 | // Finally - move the image! |
||
750 | move_uploaded_file($_FILES['individual_' . $set['raw_path']]['tmp_name'], $smileyLocation); |
||
751 | smf_chmod($smileyLocation, 0644); |
||
752 | } |
||
753 | |||
754 | // Double-check |
||
755 | if (!file_exists($smileyLocation)) |
||
756 | fatal_lang_error('smiley_not_found'); |
||
757 | |||
758 | // Should always be saved correctly! |
||
759 | $filename_array[$set['raw_path']] = $destName; |
||
760 | } |
||
761 | } |
||
762 | // Re-using an existing image |
||
763 | else |
||
764 | { |
||
765 | // Make sure a filename was given |
||
766 | if (empty($_POST['smiley_filename'])) |
||
767 | fatal_lang_error('smiley_has_no_filename'); |
||
768 | |||
769 | // And make sure it is legitimate |
||
770 | $pathinfo = pathinfo($_POST['smiley_filename']); |
||
771 | |||
772 | if (!in_array($pathinfo['extension'], $allowedTypes)) |
||
773 | fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes))); |
||
774 | if (strpos($pathinfo['filename'], '.') !== false) |
||
775 | fatal_lang_error('smileys_upload_error_illegal'); |
||
776 | if (!in_array($pathinfo['dirname'], explode(',', $modSettings['smiley_sets_known']))) |
||
777 | fatal_lang_error('smiley_set_not_found'); |
||
778 | if (!file_exists($context['smileys_dir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['basename'])) |
||
779 | fatal_lang_error('smiley_not_found'); |
||
780 | |||
781 | // Now ensure every set has a file to use for this smiley |
||
782 | foreach (explode(',', $modSettings['smiley_sets_known']) as $set) |
||
783 | { |
||
784 | unset($basename); |
||
785 | |||
786 | // Check whether any similarly named files exist in the other set's directory |
||
787 | $similar_files = glob($context['smileys_dir'] . '/' . $set . '/' . $pathinfo['filename'] . '.{' . implode(',', $allowedTypes) . '}', GLOB_BRACE); |
||
788 | |||
789 | // If there's a similarly named file already there, use it |
||
790 | if (!empty($similar_files)) |
||
791 | { |
||
792 | // Prefer an exact match if there is one |
||
793 | foreach ($similar_files as $similar_file) |
||
794 | { |
||
795 | if (basename($similar_file) == $pathinfo['basename']) |
||
796 | $basename = $pathinfo['basename']; |
||
797 | } |
||
798 | |||
799 | // Same name, different extension |
||
800 | if (empty($basename)) |
||
801 | $basename = basename(reset($similar_files)); |
||
802 | } |
||
803 | // Otherwise, copy the image to the other set's directory |
||
804 | else |
||
805 | { |
||
806 | copy($context['smileys_dir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['basename'], $context['smileys_dir'] . '/' . $set . '/' . $pathinfo['basename']); |
||
807 | smf_chmod($context['smileys_dir'] . '/' . $set . '/' . $pathinfo['basename'], 0644); |
||
808 | |||
809 | $basename = $pathinfo['basename']; |
||
810 | } |
||
811 | |||
812 | // Double-check that everything went as expected |
||
813 | if (empty($basename) || !file_exists($context['smileys_dir'] . '/' . $set . '/' . $basename)) |
||
814 | fatal_lang_error('smiley_not_found'); |
||
815 | |||
816 | // Okay, let's add this one |
||
817 | $filename_array[$set] = $basename; |
||
818 | } |
||
819 | } |
||
820 | |||
821 | // Find the position on the right. |
||
822 | $smiley_order = '0'; |
||
823 | if ($_POST['smiley_location'] != 1) |
||
824 | { |
||
825 | $request = $smcFunc['db_query']('', ' |
||
826 | SELECT MAX(smiley_order) + 1 |
||
827 | FROM {db_prefix}smileys |
||
828 | WHERE hidden = {int:smiley_location} |
||
829 | AND smiley_row = {int:first_row}', |
||
830 | array( |
||
831 | 'smiley_location' => $_POST['smiley_location'], |
||
832 | 'first_row' => 0, |
||
833 | ) |
||
834 | ); |
||
835 | list ($smiley_order) = $smcFunc['db_fetch_row']($request); |
||
836 | $smcFunc['db_free_result']($request); |
||
837 | |||
838 | if (empty($smiley_order)) |
||
839 | $smiley_order = '0'; |
||
840 | } |
||
841 | |||
842 | // Add the new smiley to the main smileys table |
||
843 | $new_id_smiley = $smcFunc['db_insert']('', |
||
844 | '{db_prefix}smileys', |
||
845 | array( |
||
846 | 'code' => 'string-30', 'description' => 'string-80', 'hidden' => 'int', 'smiley_order' => 'int', |
||
847 | ), |
||
848 | array( |
||
849 | $_POST['smiley_code'], $_POST['smiley_description'], $_POST['smiley_location'], $smiley_order, |
||
850 | ), |
||
851 | array('id_smiley'), |
||
852 | 1 |
||
853 | ); |
||
854 | |||
855 | // Add the filename info to the smiley_files table |
||
856 | $inserts = array(); |
||
857 | foreach ($filename_array as $set => $basename) |
||
858 | $inserts[] = array($new_id_smiley, $set, $basename); |
||
859 | |||
860 | $smcFunc['db_insert']('ignore', |
||
861 | '{db_prefix}smiley_files', |
||
862 | array( |
||
863 | 'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48', |
||
864 | ), |
||
865 | $inserts, |
||
866 | array('id_smiley', 'smiley_set') |
||
867 | ); |
||
868 | |||
869 | foreach ($context['smiley_sets'] as $smiley_set) |
||
870 | { |
||
871 | cache_put_data('parsing_smileys_' . $smiley_set['raw_path'], null, 480); |
||
872 | cache_put_data('posting_smileys_' . $smiley_set['raw_path'], null, 480); |
||
873 | } |
||
874 | |||
875 | // No errors? Out of here! |
||
876 | redirectexit('action=admin;area=smileys;sa=editsmileys'); |
||
877 | } |
||
878 | |||
879 | $context['selected_set'] = $modSettings['smiley_sets_default']; |
||
880 | |||
881 | // Get all possible filenames for the smileys. |
||
882 | $context['filenames'] = array(); |
||
883 | if ($context['smileys_dir_found']) |
||
884 | { |
||
885 | foreach ($context['smiley_sets'] as $smiley_set) |
||
886 | { |
||
887 | if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['raw_path'])) |
||
888 | continue; |
||
889 | |||
890 | $dir = dir($context['smileys_dir'] . '/' . $smiley_set['raw_path']); |
||
891 | while ($entry = $dir->read()) |
||
892 | { |
||
893 | $entry_info = pathinfo($entry); |
||
894 | if (empty($entry_info['filename']) || empty($entry_info['extension'])) |
||
895 | continue; |
||
896 | if (empty($context['filenames'][$smiley_set['path']][strtolower($entry_info['filename'])]) && in_array(strtolower($entry_info['extension']), $allowedTypes)) |
||
897 | $context['filenames'][$smiley_set['path']][strtolower($entry_info['filename'])] = array( |
||
898 | 'id' => $smcFunc['htmlspecialchars']($entry), |
||
899 | 'selected' => $entry_info['filename'] == 'smiley' && $smiley_set['path'] == $context['selected_set'], |
||
900 | ); |
||
901 | } |
||
902 | $dir->close(); |
||
903 | ksort($context['filenames'][$smiley_set['path']]); |
||
904 | } |
||
905 | ksort($context['filenames']); |
||
906 | } |
||
907 | |||
908 | // Create a new smiley from scratch. |
||
909 | $context['current_smiley'] = array( |
||
910 | 'id' => 0, |
||
911 | 'code' => '', |
||
912 | 'filename' => $context['filenames'][$smcFunc['htmlspecialchars']($context['selected_set'])]['smiley']['id'], |
||
913 | 'description' => $txt['smileys_default_description'], |
||
914 | 'location' => 0, |
||
915 | 'is_new' => true, |
||
916 | ); |
||
917 | } |
||
918 | |||
919 | /** |
||
920 | * Add, remove, edit smileys. |
||
921 | */ |
||
922 | function EditSmileys() |
||
923 | { |
||
924 | global $modSettings, $context, $txt, $boarddir; |
||
925 | global $smcFunc, $scripturl, $sourcedir; |
||
926 | |||
927 | // Force the correct tab to be displayed. |
||
928 | $context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys'; |
||
929 | |||
930 | $allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg'); |
||
931 | $known_sets = explode(',', $modSettings['smiley_sets_known']); |
||
932 | |||
933 | // Submitting a form? |
||
934 | if (isset($_POST['smiley_save']) || isset($_POST['smiley_action']) || isset($_POST['deletesmiley'])) |
||
935 | { |
||
936 | checkSession(); |
||
937 | |||
938 | // Changing the selected smileys? |
||
939 | if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys'])) |
||
940 | { |
||
941 | foreach ($_POST['checked_smileys'] as $id => $smiley_id) |
||
942 | $_POST['checked_smileys'][$id] = (int) $smiley_id; |
||
943 | |||
944 | if ($_POST['smiley_action'] == 'delete') |
||
945 | { |
||
946 | $smcFunc['db_query']('', ' |
||
947 | DELETE FROM {db_prefix}smileys |
||
948 | WHERE id_smiley IN ({array_int:checked_smileys})', |
||
949 | array( |
||
950 | 'checked_smileys' => $_POST['checked_smileys'], |
||
951 | ) |
||
952 | ); |
||
953 | $smcFunc['db_query']('', ' |
||
954 | DELETE FROM {db_prefix}smiley_files |
||
955 | WHERE id_smiley IN ({array_int:checked_smileys})', |
||
956 | array( |
||
957 | 'checked_smileys' => $_POST['checked_smileys'], |
||
958 | ) |
||
959 | ); |
||
960 | } |
||
961 | // Changing the status of the smiley? |
||
962 | else |
||
963 | { |
||
964 | // Check it's a valid type. |
||
965 | $displayTypes = array( |
||
966 | 'post' => 0, |
||
967 | 'hidden' => 1, |
||
968 | 'popup' => 2 |
||
969 | ); |
||
970 | if (isset($displayTypes[$_POST['smiley_action']])) |
||
971 | $smcFunc['db_query']('', ' |
||
972 | UPDATE {db_prefix}smileys |
||
973 | SET hidden = {int:display_type} |
||
974 | WHERE id_smiley IN ({array_int:checked_smileys})', |
||
975 | array( |
||
976 | 'checked_smileys' => $_POST['checked_smileys'], |
||
977 | 'display_type' => $displayTypes[$_POST['smiley_action']], |
||
978 | ) |
||
979 | ); |
||
980 | } |
||
981 | } |
||
982 | // Create/modify a smiley. |
||
983 | elseif (isset($_POST['smiley'])) |
||
984 | { |
||
985 | // Is it a delete? |
||
986 | if (!empty($_POST['deletesmiley']) && $_POST['smiley'] == (int) $_POST['smiley']) |
||
987 | { |
||
988 | $smcFunc['db_query']('', ' |
||
989 | DELETE FROM {db_prefix}smileys |
||
990 | WHERE id_smiley = {int:current_smiley}', |
||
991 | array( |
||
992 | 'current_smiley' => $_POST['smiley'], |
||
993 | ) |
||
994 | ); |
||
995 | $smcFunc['db_query']('', ' |
||
996 | DELETE FROM {db_prefix}smiley_files |
||
997 | WHERE id_smiley = {int:current_smiley}', |
||
998 | array( |
||
999 | 'current_smiley' => $_POST['smiley'], |
||
1000 | ) |
||
1001 | ); |
||
1002 | } |
||
1003 | // Otherwise an edit. |
||
1004 | else |
||
1005 | { |
||
1006 | $_POST['smiley'] = (int) $_POST['smiley']; |
||
1007 | $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']); |
||
1008 | $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location']; |
||
1009 | |||
1010 | // Make sure some code was entered. |
||
1011 | if (empty($_POST['smiley_code'])) |
||
1012 | fatal_lang_error('smiley_has_no_code'); |
||
1013 | |||
1014 | // Make sure all submitted filenames are clean. |
||
1015 | $filenames = array(); |
||
1016 | foreach ($_POST['smiley_filename'] as $posted_set => $posted_filename) |
||
1017 | { |
||
1018 | $posted_set = htmltrim__recursive($posted_set); |
||
1019 | $posted_filename = htmltrim__recursive($posted_filename); |
||
1020 | |||
1021 | // Make sure the set already exists. |
||
1022 | if (!in_array($posted_set, $known_sets)) |
||
1023 | continue; |
||
1024 | |||
1025 | $filenames[$posted_set] = pathinfo($posted_filename, PATHINFO_BASENAME); |
||
1026 | } |
||
1027 | // Fill in any missing sets. |
||
1028 | foreach ($known_sets as $known_set) |
||
1029 | { |
||
1030 | // Uh-oh, something is missing. |
||
1031 | if (empty($filenames[$known_set])) |
||
1032 | { |
||
1033 | // Try to make it the same as the default set. |
||
1034 | if (!empty($filenames[$modSettings['smiley_sets_default']])) |
||
1035 | $filenames[$known_set] = $filenames[$modSettings['smiley_sets_default']]; |
||
1036 | // As a last resort, just try to get whatever the first one is. |
||
1037 | elseif (!empty($filenames)) |
||
1038 | $filenames[$known_set] = reset($filenames); |
||
1039 | } |
||
1040 | } |
||
1041 | |||
1042 | // Can't do anything without filenames for the smileys. |
||
1043 | if (empty($filenames)) |
||
1044 | fatal_lang_error('smiley_has_no_filename'); |
||
1045 | |||
1046 | // Check whether the new code has duplicates. It should be unique. |
||
1047 | $request = $smcFunc['db_query']('', ' |
||
1048 | SELECT id_smiley |
||
1049 | FROM {db_prefix}smileys |
||
1050 | WHERE code = {raw:mysql_binary_type} {string:smiley_code}' . (empty($_POST['smiley']) ? '' : ' |
||
1051 | AND id_smiley != {int:current_smiley}'), |
||
1052 | array( |
||
1053 | 'current_smiley' => $_POST['smiley'], |
||
1054 | 'mysql_binary_type' => $smcFunc['db_title'] == MYSQL_TITLE ? 'BINARY' : '', |
||
1055 | 'smiley_code' => $_POST['smiley_code'], |
||
1056 | ) |
||
1057 | ); |
||
1058 | if ($smcFunc['db_num_rows']($request) > 0) |
||
1059 | fatal_lang_error('smiley_not_unique'); |
||
1060 | $smcFunc['db_free_result']($request); |
||
1061 | |||
1062 | $smcFunc['db_query']('', ' |
||
1063 | UPDATE {db_prefix}smileys |
||
1064 | SET |
||
1065 | code = {string:smiley_code}, |
||
1066 | description = {string:smiley_description}, |
||
1067 | hidden = {int:smiley_location} |
||
1068 | WHERE id_smiley = {int:current_smiley}', |
||
1069 | array( |
||
1070 | 'smiley_location' => $_POST['smiley_location'], |
||
1071 | 'current_smiley' => $_POST['smiley'], |
||
1072 | 'smiley_code' => $_POST['smiley_code'], |
||
1073 | 'smiley_description' => $_POST['smiley_description'], |
||
1074 | ) |
||
1075 | ); |
||
1076 | |||
1077 | // Update filename info in the smiley_files table |
||
1078 | $inserts = array(); |
||
1079 | foreach ($filenames as $set => $filename) |
||
1080 | $inserts[] = array($_POST['smiley'], $set, $filename); |
||
1081 | |||
1082 | $smcFunc['db_insert']('replace', |
||
1083 | '{db_prefix}smiley_files', |
||
1084 | array( |
||
1085 | 'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48', |
||
1086 | ), |
||
1087 | $inserts, |
||
1088 | array('id_smiley', 'smiley_set') |
||
1089 | ); |
||
1090 | } |
||
1091 | } |
||
1092 | |||
1093 | foreach ($known_sets as $smiley_set) |
||
1094 | { |
||
1095 | cache_put_data('parsing_smileys_' . $smiley_set, null, 480); |
||
1096 | cache_put_data('posting_smileys_' . $smiley_set, null, 480); |
||
1097 | } |
||
1098 | } |
||
1099 | |||
1100 | // Load all known smiley sets. |
||
1101 | $context['smiley_sets'] = array_flip($known_sets); |
||
1102 | $set_names = explode("\n", $modSettings['smiley_sets_names']); |
||
1103 | foreach ($context['smiley_sets'] as $set => $i) |
||
1104 | $context['smiley_sets'][$set] = array( |
||
1105 | 'id' => $i, |
||
1106 | 'raw_path' => $set, |
||
1107 | 'path' => $smcFunc['htmlspecialchars']($set), |
||
1108 | 'name' => $smcFunc['htmlspecialchars']($set_names[$i]), |
||
1109 | 'selected' => $set == $modSettings['smiley_sets_default'] |
||
1110 | ); |
||
1111 | |||
1112 | // Prepare overview of all (custom) smileys. |
||
1113 | if ($context['sub_action'] == 'editsmileys') |
||
1114 | { |
||
1115 | // Determine the language specific sort order of smiley locations. |
||
1116 | $smiley_locations = array( |
||
1117 | $txt['smileys_location_form'], |
||
1118 | $txt['smileys_location_hidden'], |
||
1119 | $txt['smileys_location_popup'], |
||
1120 | ); |
||
1121 | asort($smiley_locations); |
||
1122 | |||
1123 | // Create a list of options for selecting smiley sets. |
||
1124 | $smileyset_option_list = ' |
||
1125 | <select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">'; |
||
1126 | foreach ($context['smiley_sets'] as $smiley_set) |
||
1127 | $smileyset_option_list .= ' |
||
1128 | <option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected' : '') . '>' . $smiley_set['name'] . '</option>'; |
||
1129 | $smileyset_option_list .= ' |
||
1130 | </select>'; |
||
1131 | |||
1132 | $listOptions = array( |
||
1133 | 'id' => 'smiley_list', |
||
1134 | 'title' => $txt['smileys_edit'], |
||
1135 | 'items_per_page' => 40, |
||
1136 | 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys', |
||
1137 | 'default_sort_col' => 'filename', |
||
1138 | 'get_items' => array( |
||
1139 | 'function' => 'list_getSmileys', |
||
1140 | ), |
||
1141 | 'get_count' => array( |
||
1142 | 'function' => 'list_getNumSmileys', |
||
1143 | ), |
||
1144 | 'no_items_label' => $txt['smileys_no_entries'], |
||
1145 | 'columns' => array( |
||
1146 | 'picture' => array( |
||
1147 | 'data' => array( |
||
1148 | 'function' => function($rowData) use ($scripturl, $modSettings, $context) |
||
0 ignored issues
–
show
|
|||
1149 | { |
||
1150 | $return = ''; |
||
1151 | |||
1152 | foreach ($rowData['filename_array'] as $set => $filename) |
||
1153 | { |
||
1154 | $return .= ' <a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=' . $rowData['id_smiley'] . '" class="smiley_set ' . $set . '"><img src="' . $modSettings['smileys_url'] . '/' . $set . '/' . $filename . '" alt="' . $rowData['description'] . '" style="padding: 2px;" id="smiley' . $rowData['id_smiley'] . '"><input type="hidden" name="smileys[' . $rowData['id_smiley'] . '][filename]" value="' . $filename . '"></a>'; |
||
1155 | } |
||
1156 | |||
1157 | return $return; |
||
1158 | }, |
||
1159 | 'class' => 'centercol', |
||
1160 | ), |
||
1161 | ), |
||
1162 | 'smileys_code' => array( |
||
1163 | 'header' => array( |
||
1164 | 'value' => $txt['smileys_code'], |
||
1165 | ), |
||
1166 | 'data' => array( |
||
1167 | 'db_htmlsafe' => 'code', |
||
1168 | ), |
||
1169 | 'sort' => array( |
||
1170 | 'default' => 'code', |
||
1171 | 'reverse' => 'code DESC', |
||
1172 | ), |
||
1173 | ), |
||
1174 | 'filename' => array( |
||
1175 | 'header' => array( |
||
1176 | 'value' => $txt['smileys_filename'], |
||
1177 | ), |
||
1178 | 'data' => array( |
||
1179 | 'function' => function($rowData) |
||
1180 | { |
||
1181 | $return = '<span style="display:none">' . $rowData['filename'] . '</span>'; |
||
1182 | |||
1183 | foreach ($rowData['filename_array'] as $set => $filename) |
||
1184 | $return .= ' <span class="smiley_set ' . $set . '">' . $filename . '</span>'; |
||
1185 | |||
1186 | return $return; |
||
1187 | }, |
||
1188 | ), |
||
1189 | 'sort' => array( |
||
1190 | 'default' => 'filename', |
||
1191 | 'reverse' => 'filename DESC', |
||
1192 | ), |
||
1193 | ), |
||
1194 | 'location' => array( |
||
1195 | 'header' => array( |
||
1196 | 'value' => $txt['smileys_location'], |
||
1197 | ), |
||
1198 | 'data' => array( |
||
1199 | 'function' => function($rowData) use ($txt) |
||
1200 | { |
||
1201 | if (empty($rowData['hidden'])) |
||
1202 | return $txt['smileys_location_form']; |
||
1203 | elseif ($rowData['hidden'] == 1) |
||
1204 | return $txt['smileys_location_hidden']; |
||
1205 | else |
||
1206 | return $txt['smileys_location_popup']; |
||
1207 | }, |
||
1208 | ), |
||
1209 | 'sort' => array( |
||
1210 | 'default' => $smcFunc['db_custom_order']('hidden', array_keys($smiley_locations)), |
||
1211 | 'reverse' => $smcFunc['db_custom_order']('hidden', array_keys($smiley_locations), true), |
||
1212 | ), |
||
1213 | ), |
||
1214 | 'description' => array( |
||
1215 | 'header' => array( |
||
1216 | 'value' => $txt['smileys_description'], |
||
1217 | ), |
||
1218 | 'data' => array( |
||
1219 | 'function' => function($rowData) use ($context, $txt, $modSettings, $smcFunc) |
||
1220 | { |
||
1221 | if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'])) |
||
1222 | return $smcFunc['htmlspecialchars']($rowData['description']); |
||
1223 | |||
1224 | // Check if there are smileys missing in some sets. |
||
1225 | $missing_sets = array(); |
||
1226 | foreach ($context['smiley_sets'] as $smiley_set) |
||
1227 | if (empty($rowData['filename_array'][$smiley_set['path']]) || !file_exists(sprintf('%1$s/%2$s/%3$s', $modSettings['smileys_dir'], $smiley_set['path'], $rowData['filename_array'][$smiley_set['path']]))) |
||
1228 | $missing_sets[] = $smiley_set['path']; |
||
1229 | |||
1230 | $description = $smcFunc['htmlspecialchars']($rowData['description']); |
||
1231 | |||
1232 | if (!empty($missing_sets)) |
||
1233 | $description .= sprintf('<br><span class="smalltext"><strong>%1$s:</strong> %2$s</span>', $txt['smileys_not_found_in_set'], implode(', ', $missing_sets)); |
||
1234 | |||
1235 | return $description; |
||
1236 | }, |
||
1237 | ), |
||
1238 | 'sort' => array( |
||
1239 | 'default' => 'description', |
||
1240 | 'reverse' => 'description DESC', |
||
1241 | ), |
||
1242 | ), |
||
1243 | 'modify' => array( |
||
1244 | 'header' => array( |
||
1245 | 'value' => $txt['smileys_modify'], |
||
1246 | 'class' => 'centercol', |
||
1247 | ), |
||
1248 | 'data' => array( |
||
1249 | 'sprintf' => array( |
||
1250 | 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>', |
||
1251 | 'params' => array( |
||
1252 | 'id_smiley' => false, |
||
1253 | ), |
||
1254 | ), |
||
1255 | 'class' => 'centercol', |
||
1256 | ), |
||
1257 | ), |
||
1258 | 'check' => array( |
||
1259 | 'header' => array( |
||
1260 | 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">', |
||
1261 | 'class' => 'centercol', |
||
1262 | ), |
||
1263 | 'data' => array( |
||
1264 | 'sprintf' => array( |
||
1265 | 'format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d">', |
||
1266 | 'params' => array( |
||
1267 | 'id_smiley' => false, |
||
1268 | ), |
||
1269 | ), |
||
1270 | 'class' => 'centercol', |
||
1271 | ), |
||
1272 | ), |
||
1273 | ), |
||
1274 | 'form' => array( |
||
1275 | 'href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys', |
||
1276 | 'name' => 'smileyForm', |
||
1277 | ), |
||
1278 | 'additional_rows' => array( |
||
1279 | array( |
||
1280 | 'position' => 'above_column_headers', |
||
1281 | 'value' => $smileyset_option_list, |
||
1282 | 'class' => 'righttext', |
||
1283 | ), |
||
1284 | array( |
||
1285 | 'position' => 'below_table_data', |
||
1286 | 'value' => ' |
||
1287 | <select name="smiley_action" onchange="makeChanges(this.value);"> |
||
1288 | <option value="-1">' . $txt['smileys_with_selected'] . ':</option> |
||
1289 | <option value="-1" disabled>--------------</option> |
||
1290 | <option value="hidden">' . $txt['smileys_make_hidden'] . '</option> |
||
1291 | <option value="post">' . $txt['smileys_show_on_post'] . '</option> |
||
1292 | <option value="popup">' . $txt['smileys_show_on_popup'] . '</option> |
||
1293 | <option value="delete">' . $txt['smileys_remove'] . '</option> |
||
1294 | </select> |
||
1295 | <noscript> |
||
1296 | <input type="submit" name="perform_action" value="' . $txt['go'] . '" class="button"> |
||
1297 | </noscript>', |
||
1298 | 'class' => 'righttext', |
||
1299 | ), |
||
1300 | ), |
||
1301 | 'javascript' => ' |
||
1302 | function makeChanges(action) |
||
1303 | { |
||
1304 | if (action == \'-1\') |
||
1305 | return false; |
||
1306 | else if (action == \'delete\') |
||
1307 | { |
||
1308 | if (confirm(\'' . $txt['smileys_confirm'] . '\')) |
||
1309 | document.forms.smileyForm.submit(); |
||
1310 | } |
||
1311 | else |
||
1312 | document.forms.smileyForm.submit(); |
||
1313 | return true; |
||
1314 | } |
||
1315 | function changeSet(newSet) |
||
1316 | { |
||
1317 | $(".smiley_set").hide(); |
||
1318 | $(".smiley_set." + newSet).show(); |
||
1319 | }', |
||
1320 | ); |
||
1321 | |||
1322 | require_once($sourcedir . '/Subs-List.php'); |
||
1323 | createList($listOptions); |
||
1324 | |||
1325 | // The list is the only thing to show, so make it the main template. |
||
1326 | $context['default_list'] = 'smiley_list'; |
||
1327 | $context['sub_template'] = 'show_list'; |
||
1328 | |||
1329 | addInlineJavaScript("\n\t" . 'changeSet("' . $modSettings['smiley_sets_default'] . '");', true); |
||
1330 | } |
||
1331 | // Modifying smileys. |
||
1332 | elseif ($context['sub_action'] == 'modifysmiley') |
||
1333 | { |
||
1334 | $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir']; |
||
1335 | $context['smileys_dir_found'] = is_dir($context['smileys_dir']); |
||
1336 | |||
1337 | $context['selected_set'] = $modSettings['smiley_sets_default']; |
||
1338 | |||
1339 | $request = $smcFunc['db_query']('', ' |
||
1340 | SELECT s.id_smiley AS id, s.code, f.filename, f.smiley_set, s.description, s.hidden AS location |
||
1341 | FROM {db_prefix}smileys AS s |
||
1342 | JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley) |
||
1343 | WHERE s.id_smiley = {int:current_smiley}', |
||
1344 | array( |
||
1345 | 'current_smiley' => (int) $_REQUEST['smiley'], |
||
1346 | ) |
||
1347 | ); |
||
1348 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1349 | { |
||
1350 | // The empty() bit is for just in case the default set is missing this smiley |
||
1351 | if ($row['smiley_set'] == $context['selected_set'] || empty($context['current_smiley'])) |
||
1352 | $context['current_smiley'] = $row; |
||
1353 | |||
1354 | $filenames[$row['smiley_set']] = $row['filename']; |
||
1355 | } |
||
1356 | $smcFunc['db_free_result']($request); |
||
1357 | |||
1358 | if (empty($context['current_smiley'])) |
||
1359 | fatal_lang_error('smiley_not_found'); |
||
1360 | |||
1361 | $context['current_smiley']['code'] = $smcFunc['htmlspecialchars']($context['current_smiley']['code']); |
||
1362 | $context['current_smiley']['description'] = $smcFunc['htmlspecialchars']($context['current_smiley']['description']); |
||
1363 | $context['current_smiley']['filename'] = $smcFunc['htmlspecialchars']($context['current_smiley']['filename']); |
||
1364 | |||
1365 | // Get all possible filenames for the smileys. |
||
1366 | $context['filenames'] = array(); |
||
1367 | $context['missing_sets'] = array(); |
||
1368 | if ($context['smileys_dir_found']) |
||
1369 | { |
||
1370 | foreach ($context['smiley_sets'] as $smiley_set) |
||
1371 | { |
||
1372 | if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['raw_path'])) |
||
1373 | continue; |
||
1374 | |||
1375 | // No file currently defined for this smiley in this set? That's no good. |
||
1376 | if (!isset($filenames[$smiley_set['raw_path']])) |
||
1377 | { |
||
1378 | $context['missing_sets'][] = $smiley_set['raw_path']; |
||
1379 | $context['filenames'][$smiley_set['path']][''] = array('id' => '', 'selected' => true, 'disabled' => true); |
||
1380 | } |
||
1381 | |||
1382 | $dir = dir($context['smileys_dir'] . '/' . $smiley_set['raw_path']); |
||
1383 | while ($entry = $dir->read()) |
||
1384 | { |
||
1385 | if (empty($context['filenames'][$smiley_set['path']][$entry]) && in_array(pathinfo($entry, PATHINFO_EXTENSION), $allowedTypes)) |
||
1386 | $context['filenames'][$smiley_set['path']][$entry] = array( |
||
1387 | 'id' => $smcFunc['htmlspecialchars']($entry), |
||
1388 | 'selected' => isset($filenames[$smiley_set['raw_path']]) && strtolower($entry) == strtolower($filenames[$smiley_set['raw_path']]), |
||
1389 | 'disabled' => false, |
||
1390 | ); |
||
1391 | } |
||
1392 | $dir->close(); |
||
1393 | ksort($context['filenames'][$smiley_set['path']]); |
||
1394 | } |
||
1395 | ksort($context['filenames']); |
||
1396 | } |
||
1397 | } |
||
1398 | } |
||
1399 | |||
1400 | /** |
||
1401 | * Callback function for createList(). |
||
1402 | * |
||
1403 | * @param int $start The item to start with (not used here) |
||
1404 | * @param int $items_per_page The number of items to show per page (not used here) |
||
1405 | * @param string $sort A string indicating how to sort the results |
||
1406 | * @return array An array of info about the smileys |
||
1407 | */ |
||
1408 | function list_getSmileys($start, $items_per_page, $sort) |
||
1409 | { |
||
1410 | global $smcFunc, $modSettings; |
||
1411 | |||
1412 | $request = $smcFunc['db_query']('', ' |
||
1413 | SELECT s.id_smiley, s.code, f.filename, f.smiley_set, s.description, s.smiley_row, s.smiley_order, s.hidden |
||
1414 | FROM {db_prefix}smileys AS s |
||
1415 | JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley) |
||
1416 | ORDER BY {raw:sort}', |
||
1417 | array( |
||
1418 | 'sort' => $sort, |
||
1419 | ) |
||
1420 | ); |
||
1421 | $smileys = array(); |
||
1422 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1423 | { |
||
1424 | if (empty($smileys[$row['id_smiley']])) |
||
1425 | { |
||
1426 | $smileys[$row['id_smiley']] = $row; |
||
1427 | unset($smileys[$row['id_smiley']]['smiley_set']); |
||
1428 | $smileys[$row['id_smiley']]['filename_array'] = array($row['smiley_set'] => $row['filename']); |
||
1429 | } |
||
1430 | else |
||
1431 | { |
||
1432 | $smileys[$row['id_smiley']]['filename_array'][$row['smiley_set']] = $row['filename']; |
||
1433 | } |
||
1434 | |||
1435 | // Use the filename for the default set as the primary filename for this smiley |
||
1436 | if (isset($smileys[$row['id_smiley']]['filename_array'][$modSettings['smiley_sets_default']])) |
||
1437 | $smileys[$row['id_smiley']]['filename'] = $smileys[$row['id_smiley']]['filename_array'][$modSettings['smiley_sets_default']]; |
||
1438 | else |
||
1439 | $smileys[$row['id_smiley']]['filename'] = reset($smileys[$row['id_smiley']]['filename_array']); |
||
1440 | } |
||
1441 | $smcFunc['db_free_result']($request); |
||
1442 | |||
1443 | return $smileys; |
||
1444 | } |
||
1445 | |||
1446 | /** |
||
1447 | * Callback function for createList(). |
||
1448 | * |
||
1449 | * @return int The number of smileys |
||
1450 | */ |
||
1451 | function list_getNumSmileys() |
||
1452 | { |
||
1453 | global $smcFunc; |
||
1454 | |||
1455 | $request = $smcFunc['db_query']('', ' |
||
1456 | SELECT COUNT(*) |
||
1457 | FROM {db_prefix}smileys', |
||
1458 | array() |
||
1459 | ); |
||
1460 | list($numSmileys) = $smcFunc['db_fetch_row']; |
||
1461 | $smcFunc['db_free_result']($request); |
||
1462 | |||
1463 | return $numSmileys; |
||
1464 | } |
||
1465 | |||
1466 | /** |
||
1467 | * Allows to edit smileys order. |
||
1468 | */ |
||
1469 | function EditSmileyOrder() |
||
1470 | { |
||
1471 | global $context, $txt, $smcFunc, $modSettings; |
||
1472 | |||
1473 | // Move smileys to another position. |
||
1474 | if (isset($_REQUEST['reorder'])) |
||
1475 | { |
||
1476 | checkSession('get'); |
||
1477 | |||
1478 | $_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2; |
||
1479 | $_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source']; |
||
1480 | |||
1481 | if (empty($_GET['source'])) |
||
1482 | fatal_lang_error('smiley_not_found'); |
||
1483 | |||
1484 | if (!empty($_GET['after'])) |
||
1485 | { |
||
1486 | $_GET['after'] = (int) $_GET['after']; |
||
1487 | |||
1488 | $request = $smcFunc['db_query']('', ' |
||
1489 | SELECT smiley_row, smiley_order, hidden |
||
1490 | FROM {db_prefix}smileys |
||
1491 | WHERE hidden = {int:location} |
||
1492 | AND id_smiley = {int:after_smiley}', |
||
1493 | array( |
||
1494 | 'location' => $_GET['location'], |
||
1495 | 'after_smiley' => $_GET['after'], |
||
1496 | ) |
||
1497 | ); |
||
1498 | if ($smcFunc['db_num_rows']($request) != 1) |
||
1499 | fatal_lang_error('smiley_not_found'); |
||
1500 | list ($smiley_row, $smiley_order, $smileyLocation) = $smcFunc['db_fetch_row']($request); |
||
1501 | $smcFunc['db_free_result']($request); |
||
1502 | } |
||
1503 | else |
||
1504 | { |
||
1505 | $smiley_row = (int) $_GET['row']; |
||
1506 | $smiley_order = -1; |
||
1507 | $smileyLocation = (int) $_GET['location']; |
||
1508 | } |
||
1509 | |||
1510 | $smcFunc['db_query']('', ' |
||
1511 | UPDATE {db_prefix}smileys |
||
1512 | SET smiley_order = smiley_order + 1 |
||
1513 | WHERE hidden = {int:new_location} |
||
1514 | AND smiley_row = {int:smiley_row} |
||
1515 | AND smiley_order > {int:smiley_order}', |
||
1516 | array( |
||
1517 | 'new_location' => $_GET['location'], |
||
1518 | 'smiley_row' => $smiley_row, |
||
1519 | 'smiley_order' => $smiley_order, |
||
1520 | ) |
||
1521 | ); |
||
1522 | |||
1523 | $smcFunc['db_query']('', ' |
||
1524 | UPDATE {db_prefix}smileys |
||
1525 | SET |
||
1526 | smiley_order = {int:smiley_order} + 1, |
||
1527 | smiley_row = {int:smiley_row}, |
||
1528 | hidden = {int:new_location} |
||
1529 | WHERE id_smiley = {int:current_smiley}', |
||
1530 | array( |
||
1531 | 'smiley_order' => $smiley_order, |
||
1532 | 'smiley_row' => $smiley_row, |
||
1533 | 'new_location' => $smileyLocation, |
||
1534 | 'current_smiley' => $_GET['source'], |
||
1535 | ) |
||
1536 | ); |
||
1537 | |||
1538 | foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set) |
||
1539 | { |
||
1540 | cache_put_data('parsing_smileys_' . $smiley_set, null, 480); |
||
1541 | cache_put_data('posting_smileys_' . $smiley_set, null, 480); |
||
1542 | } |
||
1543 | } |
||
1544 | |||
1545 | $request = $smcFunc['db_query']('', ' |
||
1546 | SELECT s.id_smiley, s.code, f.filename, s.description, s.smiley_row, s.smiley_order, s.hidden |
||
1547 | FROM {db_prefix}smileys AS s |
||
1548 | JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley) |
||
1549 | WHERE s.hidden != {int:popup} |
||
1550 | AND f.smiley_set = {string:smiley_set} |
||
1551 | ORDER BY s.smiley_order, s.smiley_row', |
||
1552 | array( |
||
1553 | 'popup' => 1, |
||
1554 | 'smiley_set' => $modSettings['smiley_sets_default'], |
||
1555 | ) |
||
1556 | ); |
||
1557 | $context['smileys'] = array( |
||
1558 | 'postform' => array( |
||
1559 | 'rows' => array(), |
||
1560 | ), |
||
1561 | 'popup' => array( |
||
1562 | 'rows' => array(), |
||
1563 | ), |
||
1564 | ); |
||
1565 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1566 | { |
||
1567 | $location = empty($row['hidden']) ? 'postform' : 'popup'; |
||
1568 | $context['smileys'][$location]['rows'][$row['smiley_row']][] = array( |
||
1569 | 'id' => $row['id_smiley'], |
||
1570 | 'code' => $smcFunc['htmlspecialchars']($row['code']), |
||
1571 | 'filename' => $smcFunc['htmlspecialchars']($row['filename']), |
||
1572 | 'description' => $smcFunc['htmlspecialchars']($row['description']), |
||
1573 | 'row' => $row['smiley_row'], |
||
1574 | 'order' => $row['smiley_order'], |
||
1575 | 'selected' => !empty($_REQUEST['move']) && $_REQUEST['move'] == $row['id_smiley'], |
||
1576 | ); |
||
1577 | } |
||
1578 | $smcFunc['db_free_result']($request); |
||
1579 | |||
1580 | $context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move']; |
||
1581 | |||
1582 | // Make sure all rows are sequential. |
||
1583 | foreach (array_keys($context['smileys']) as $location) |
||
1584 | $context['smileys'][$location] = array( |
||
1585 | 'id' => $location, |
||
1586 | 'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'], |
||
1587 | 'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'], |
||
1588 | 'last_row' => count($context['smileys'][$location]['rows']), |
||
1589 | 'rows' => array_values($context['smileys'][$location]['rows']), |
||
1590 | ); |
||
1591 | |||
1592 | // Check & fix smileys that are not ordered properly in the database. |
||
1593 | foreach (array_keys($context['smileys']) as $location) |
||
1594 | { |
||
1595 | foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row) |
||
1596 | { |
||
1597 | // Fix empty rows if any. |
||
1598 | if ($id != $smiley_row[0]['row']) |
||
1599 | { |
||
1600 | $smcFunc['db_query']('', ' |
||
1601 | UPDATE {db_prefix}smileys |
||
1602 | SET smiley_row = {int:new_row} |
||
1603 | WHERE smiley_row = {int:current_row} |
||
1604 | AND hidden = {int:location}', |
||
1605 | array( |
||
1606 | 'new_row' => $id, |
||
1607 | 'current_row' => $smiley_row[0]['row'], |
||
1608 | 'location' => $location == 'postform' ? '0' : '2', |
||
1609 | ) |
||
1610 | ); |
||
1611 | // Only change the first row value of the first smiley (we don't need the others :P). |
||
1612 | $context['smileys'][$location]['rows'][$id][0]['row'] = $id; |
||
1613 | } |
||
1614 | // Make sure the smiley order is always sequential. |
||
1615 | foreach ($smiley_row as $order_id => $smiley) |
||
1616 | if ($order_id != $smiley['order']) |
||
1617 | $smcFunc['db_query']('', ' |
||
1618 | UPDATE {db_prefix}smileys |
||
1619 | SET smiley_order = {int:new_order} |
||
1620 | WHERE id_smiley = {int:current_smiley}', |
||
1621 | array( |
||
1622 | 'new_order' => $order_id, |
||
1623 | 'current_smiley' => $smiley['id'], |
||
1624 | ) |
||
1625 | ); |
||
1626 | } |
||
1627 | } |
||
1628 | |||
1629 | foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set) |
||
1630 | { |
||
1631 | cache_put_data('parsing_smileys_' . $smiley_set, null, 480); |
||
1632 | cache_put_data('posting_smileys_' . $smiley_set, null, 480); |
||
1633 | } |
||
1634 | } |
||
1635 | |||
1636 | /** |
||
1637 | * Install a smiley set. |
||
1638 | */ |
||
1639 | function InstallSmileySet() |
||
1640 | { |
||
1641 | global $sourcedir, $boarddir, $packagesdir, $modSettings, $smcFunc, $scripturl, $context, $txt, $user_info; |
||
1642 | |||
1643 | isAllowedTo('manage_smileys'); |
||
1644 | checkSession('request'); |
||
1645 | // One of these two may be necessary |
||
1646 | loadLanguage('Errors'); |
||
1647 | loadLanguage('Packages'); |
||
1648 | |||
1649 | require_once($sourcedir . '/Subs-Package.php'); |
||
1650 | |||
1651 | // Installing unless proven otherwise |
||
1652 | $testing = false; |
||
1653 | |||
1654 | if (isset($_REQUEST['set_gz'])) |
||
1655 | { |
||
1656 | $base_name = strtr(basename($_REQUEST['set_gz']), ':/', '-_'); |
||
1657 | $name = $smcFunc['htmlspecialchars'](strtok(basename($_REQUEST['set_gz']), '.')); |
||
1658 | $context['filename'] = $base_name; |
||
1659 | |||
1660 | // Check that the smiley is from simplemachines.org, for now... maybe add mirroring later. |
||
1661 | // @ TODO: Our current xml files serve http links. Allowing both for now until we serve https. |
||
1662 | if (preg_match('~^https?://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['set_gz']) == 0 || strpos($_REQUEST['set_gz'], 'dlattach') !== false) |
||
1663 | fatal_lang_error('not_on_simplemachines'); |
||
1664 | |||
1665 | $destination = $packagesdir . '/' . $base_name; |
||
1666 | |||
1667 | if (file_exists($destination)) |
||
1668 | fatal_lang_error('package_upload_error_exists'); |
||
1669 | |||
1670 | // Let's copy it to the Packages directory |
||
1671 | file_put_contents($destination, fetch_web_data($_REQUEST['set_gz'])); |
||
1672 | $testing = true; |
||
1673 | } |
||
1674 | elseif (isset($_REQUEST['package'])) |
||
1675 | { |
||
1676 | $base_name = basename($_REQUEST['package']); |
||
1677 | $name = $smcFunc['htmlspecialchars'](strtok(basename($_REQUEST['package']), '.')); |
||
1678 | $context['filename'] = $base_name; |
||
1679 | |||
1680 | $destination = $packagesdir . '/' . basename($_REQUEST['package']); |
||
1681 | } |
||
1682 | |||
1683 | if (empty($destination) || !file_exists($destination)) |
||
1684 | fatal_lang_error('package_no_file', false); |
||
1685 | |||
1686 | // Make sure temp directory exists and is empty. |
||
1687 | if (file_exists($packagesdir . '/temp')) |
||
1688 | deltree($packagesdir . '/temp', false); |
||
1689 | |||
1690 | if (!mktree($packagesdir . '/temp', 0755)) |
||
1691 | { |
||
1692 | deltree($packagesdir . '/temp', false); |
||
1693 | if (!mktree($packagesdir . '/temp', 0777)) |
||
1694 | { |
||
1695 | deltree($packagesdir . '/temp', false); |
||
1696 | // @todo not sure about url in destination_url |
||
1697 | create_chmod_control(array($packagesdir . '/temp/delme.tmp'), array('destination_url' => $scripturl . '?action=admin;area=smileys;sa=install;set_gz=' . $_REQUEST['set_gz'], 'crash_on_error' => true)); |
||
1698 | |||
1699 | deltree($packagesdir . '/temp', false); |
||
1700 | if (!mktree($packagesdir . '/temp', 0777)) |
||
1701 | fatal_lang_error('package_cant_download', false); |
||
1702 | } |
||
1703 | } |
||
1704 | |||
1705 | $extracted = read_tgz_file($destination, $packagesdir . '/temp'); |
||
1706 | if (!$extracted) |
||
1707 | fatal_lang_error('packageget_unable', false, array('https://custom.simplemachines.org/mods/index.php?action=search;type=12;basic_search=' . $name)); |
||
1708 | if ($extracted && !file_exists($packagesdir . '/temp/package-info.xml')) |
||
1709 | foreach ($extracted as $file) |
||
1710 | if (basename($file['filename']) == 'package-info.xml') |
||
1711 | { |
||
1712 | $base_path = dirname($file['filename']) . '/'; |
||
1713 | break; |
||
1714 | } |
||
1715 | |||
1716 | if (!isset($base_path)) |
||
1717 | $base_path = ''; |
||
1718 | |||
1719 | if (!file_exists($packagesdir . '/temp/' . $base_path . 'package-info.xml')) |
||
1720 | fatal_lang_error('package_get_error_missing_xml', false); |
||
1721 | |||
1722 | $smileyInfo = getPackageInfo($context['filename']); |
||
1723 | if (!is_array($smileyInfo)) |
||
1724 | fatal_lang_error($smileyInfo); |
||
1725 | |||
1726 | // See if it is installed? |
||
1727 | $request = $smcFunc['db_query']('', ' |
||
1728 | SELECT version, themes_installed, db_changes |
||
1729 | FROM {db_prefix}log_packages |
||
1730 | WHERE package_id = {string:current_package} |
||
1731 | AND install_state != {int:not_installed} |
||
1732 | ORDER BY time_installed DESC |
||
1733 | LIMIT 1', |
||
1734 | array( |
||
1735 | 'not_installed' => 0, |
||
1736 | 'current_package' => $smileyInfo['id'], |
||
1737 | ) |
||
1738 | ); |
||
1739 | |||
1740 | if ($smcFunc['db_num_rows']($request) > 0) |
||
1741 | fatal_lang_error('package_installed_warning1'); |
||
1742 | |||
1743 | // Everything is fine, now it's time to do something |
||
1744 | $actions = parsePackageInfo($smileyInfo['xml'], true, 'install'); |
||
1745 | |||
1746 | $context['post_url'] = $scripturl . '?action=admin;area=smileys;sa=install;package=' . $base_name; |
||
1747 | $context['has_failure'] = false; |
||
1748 | $context['actions'] = array(); |
||
1749 | $context['ftp_needed'] = false; |
||
1750 | |||
1751 | foreach ($actions as $action) |
||
1752 | { |
||
1753 | if ($action['type'] == 'readme' || $action['type'] == 'license') |
||
1754 | { |
||
1755 | $type = 'package_' . $action['type']; |
||
1756 | if (file_exists($packagesdir . '/temp/' . $base_path . $action['filename'])) |
||
1757 | $context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($packagesdir . '/temp/' . $base_path . $action['filename']), "\n\r")); |
||
1758 | elseif (file_exists($action['filename'])) |
||
1759 | $context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($action['filename']), "\n\r")); |
||
1760 | |||
1761 | if (!empty($action['parse_bbc'])) |
||
1762 | { |
||
1763 | require_once($sourcedir . '/Subs-Post.php'); |
||
1764 | preparsecode($context[$type]); |
||
1765 | $context[$type] = parse_bbc($context[$type]); |
||
1766 | } |
||
1767 | else |
||
1768 | $context[$type] = nl2br($context[$type]); |
||
1769 | |||
1770 | continue; |
||
1771 | } |
||
1772 | elseif ($action['type'] == 'require-dir') |
||
1773 | { |
||
1774 | // Do this one... |
||
1775 | $thisAction = array( |
||
1776 | 'type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']), |
||
1777 | 'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.'))) |
||
1778 | ); |
||
1779 | |||
1780 | $file = $packagesdir . '/temp/' . $base_path . $action['filename']; |
||
1781 | if (isset($action['filename']) && (!file_exists($file) || !is_writable(dirname($action['destination'])))) |
||
1782 | { |
||
1783 | $context['has_failure'] = true; |
||
1784 | |||
1785 | $thisAction += array( |
||
1786 | 'description' => $txt['package_action_error'], |
||
1787 | 'failed' => true, |
||
1788 | ); |
||
1789 | } |
||
1790 | // @todo None given? |
||
1791 | if (empty($thisAction['description'])) |
||
1792 | $thisAction['description'] = isset($action['description']) ? $action['description'] : ''; |
||
1793 | |||
1794 | $context['actions'][] = $thisAction; |
||
1795 | } |
||
1796 | elseif ($action['type'] == 'credits') |
||
1797 | { |
||
1798 | // Time to build the billboard |
||
1799 | $credits_tag = array( |
||
1800 | 'url' => $action['url'], |
||
1801 | 'license' => $action['license'], |
||
1802 | 'copyright' => $action['copyright'], |
||
1803 | 'title' => $action['title'], |
||
1804 | ); |
||
1805 | } |
||
1806 | } |
||
1807 | |||
1808 | if ($testing) |
||
1809 | { |
||
1810 | $context['sub_template'] = 'view_package'; |
||
1811 | $context['uninstalling'] = false; |
||
1812 | $context['is_installed'] = false; |
||
1813 | $context['package_name'] = $smileyInfo['name']; |
||
1814 | loadTemplate('Packages'); |
||
1815 | } |
||
1816 | // Do the actual install |
||
1817 | else |
||
1818 | { |
||
1819 | // @TODO Does this call have side effects? ($actions is not used) |
||
1820 | $actions = parsePackageInfo($smileyInfo['xml'], false, 'install'); |
||
0 ignored issues
–
show
|
|||
1821 | foreach ($context['actions'] as $action) |
||
1822 | { |
||
1823 | updateSettings(array( |
||
1824 | 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . basename($action['action']), |
||
1825 | 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $smileyInfo['name'] . (count($context['actions']) > 1 ? ' ' . (!empty($action['description']) ? $smcFunc['htmlspecialchars']($action['description']) : basename($action['action'])) : ''), |
||
1826 | )); |
||
1827 | } |
||
1828 | |||
1829 | package_flush_cache(); |
||
1830 | |||
1831 | // Credits tag? |
||
1832 | $credits_tag = (empty($credits_tag)) ? '' : $smcFunc['json_encode']($credits_tag); |
||
1833 | $smcFunc['db_insert']('', |
||
1834 | '{db_prefix}log_packages', |
||
1835 | array( |
||
1836 | 'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string', |
||
1837 | 'id_member_installed' => 'int', 'member_installed' => 'string', 'time_installed' => 'int', |
||
1838 | 'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string', |
||
1839 | 'member_removed' => 'int', 'db_changes' => 'string', 'credits' => 'string', |
||
1840 | ), |
||
1841 | array( |
||
1842 | $smileyInfo['filename'], $smileyInfo['name'], $smileyInfo['id'], $smileyInfo['version'], |
||
1843 | $user_info['id'], $user_info['name'], time(), |
||
1844 | 1, '', '', |
||
1845 | 0, '', $credits_tag, |
||
1846 | ), |
||
1847 | array('id_install') |
||
1848 | ); |
||
1849 | |||
1850 | logAction('install_package', array('package' => $smcFunc['htmlspecialchars']($smileyInfo['name']), 'version' => $smcFunc['htmlspecialchars']($smileyInfo['version'])), 'admin'); |
||
1851 | |||
1852 | foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set) |
||
1853 | { |
||
1854 | cache_put_data('parsing_smileys_' . $smiley_set, null, 480); |
||
1855 | cache_put_data('posting_smileys_' . $smiley_set, null, 480); |
||
1856 | } |
||
1857 | } |
||
1858 | |||
1859 | if (file_exists($packagesdir . '/temp')) |
||
1860 | deltree($packagesdir . '/temp'); |
||
1861 | |||
1862 | if (!$testing) |
||
1863 | redirectexit('action=admin;area=smileys'); |
||
1864 | } |
||
1865 | |||
1866 | /** |
||
1867 | * A function to import new smileys from an existing directory into the database. |
||
1868 | * |
||
1869 | * @param string $smileyPath The path to the directory to import smileys from |
||
1870 | * @param bool $create Whether or not to make brand new smileys for files that don't match any existing smileys |
||
1871 | */ |
||
1872 | function ImportSmileys($smileyPath, $create = false) |
||
1873 | { |
||
1874 | global $modSettings, $smcFunc; |
||
1875 | |||
1876 | if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'] . '/' . $smileyPath)) |
||
1877 | fatal_lang_error('smiley_set_unable_to_import'); |
||
1878 | |||
1879 | $allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg'); |
||
1880 | $known_sets = explode(',', $modSettings['smiley_sets_known']); |
||
1881 | sort($known_sets); |
||
1882 | |||
1883 | // Get the smileys in the folder |
||
1884 | $smileys = array(); |
||
0 ignored issues
–
show
|
|||
1885 | $dir = dir($modSettings['smileys_dir'] . '/' . $smileyPath); |
||
1886 | while ($entry = $dir->read()) |
||
1887 | { |
||
1888 | $pathinfo = pathinfo($entry); |
||
1889 | if (empty($pathinfo['filename']) || empty($pathinfo['extension'])) |
||
1890 | continue; |
||
1891 | if (in_array($pathinfo['extension'], $allowedTypes) && $pathinfo['filename'] != 'blank' && strlen($pathinfo['basename']) <= 48) |
||
1892 | $smiley_files[strtolower($pathinfo['basename'])] = $pathinfo['basename']; |
||
1893 | } |
||
1894 | $dir->close(); |
||
1895 | |||
1896 | // Get the smileys that are already in the database. |
||
1897 | $existing_smileys = array(); |
||
1898 | $request = $smcFunc['db_query']('', ' |
||
1899 | SELECT id_smiley, smiley_set, filename |
||
1900 | FROM {db_prefix}smiley_files', |
||
1901 | array() |
||
1902 | ); |
||
1903 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1904 | $existing_smileys[pathinfo($row['filename'], PATHINFO_FILENAME)][$row['id_smiley']][] = $row['smiley_set']; |
||
1905 | $smcFunc['db_free_result']($request); |
||
1906 | |||
1907 | // Filter $smiley_files down to just the ones not already in the database. |
||
1908 | $to_unset = array(); |
||
1909 | $to_fix = array(); |
||
1910 | foreach ($smiley_files as $key => $smiley_file) |
||
1911 | { |
||
1912 | $smiley_name = pathinfo($smiley_file, PATHINFO_FILENAME); |
||
1913 | |||
1914 | // A brand new one |
||
1915 | if (empty($existing_smileys[$smiley_name])) |
||
1916 | continue; |
||
1917 | |||
1918 | // A file with this name is already being used for at least one smiley, so we have more work to do... |
||
1919 | foreach ($existing_smileys[$smiley_name] as $existing_id => $existing_sets) |
||
1920 | { |
||
1921 | $to_unset[$key][$existing_id] = false; |
||
1922 | |||
1923 | sort($existing_sets); |
||
1924 | |||
1925 | // Already done |
||
1926 | if ($existing_sets === $known_sets) |
||
1927 | $to_unset[$key][$existing_id] = true; |
||
1928 | |||
1929 | // Used in some sets but not others |
||
1930 | else |
||
1931 | { |
||
1932 | // Do the other sets have some other file already defined? |
||
1933 | foreach ($existing_smileys as $file => $info) |
||
1934 | { |
||
1935 | foreach ($info as $info_id => $info_sets) |
||
1936 | { |
||
1937 | if ($existing_id == $info_id) |
||
1938 | $existing_sets = array_unique(array_merge($existing_sets, $info_sets)); |
||
1939 | } |
||
1940 | } |
||
1941 | sort($existing_sets); |
||
1942 | |||
1943 | // If every set already has a file for this smiley, we can skip it |
||
1944 | if ($known_sets == $existing_sets) |
||
1945 | $to_unset[$key][$existing_id] = true; |
||
1946 | |||
1947 | // Need to add the file for these sets |
||
1948 | else |
||
1949 | $to_fix[$key][$existing_id] = array_diff($known_sets, $existing_sets); |
||
1950 | } |
||
1951 | } |
||
1952 | } |
||
1953 | |||
1954 | // Fix any sets with missing files |
||
1955 | // This part handles files for pre-existing smileys in a newly created smiley set |
||
1956 | $inserts = array(); |
||
1957 | foreach ($to_fix as $key => $ids) |
||
1958 | { |
||
1959 | foreach ($ids as $id_smiley => $sets_missing) |
||
1960 | { |
||
1961 | // Find the file we need to copy to the other sets |
||
1962 | if (file_exists($modSettings['smileys_dir'] . '/' . $smileyPath . '/' . $smiley_files[$key])) |
||
1963 | $p = $smileyPath; |
||
1964 | else |
||
1965 | { |
||
1966 | foreach (array_diff($known_sets, $sets_missing) as $set) |
||
1967 | { |
||
1968 | if (file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key])) |
||
1969 | { |
||
1970 | $p = $set; |
||
1971 | break; |
||
1972 | } |
||
1973 | } |
||
1974 | } |
||
1975 | |||
1976 | foreach ($sets_missing as $set) |
||
1977 | { |
||
1978 | if ($set !== $p) |
||
1979 | { |
||
1980 | // Copy the file into the set's folder |
||
1981 | copy($modSettings['smileys_dir'] . '/' . $p . '/' . $smiley_files[$key], $modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]); |
||
1982 | smf_chmod($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key], 0644); |
||
1983 | } |
||
1984 | |||
1985 | // Double-check that everything went as expected |
||
1986 | if (!file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key])) |
||
1987 | continue; |
||
1988 | |||
1989 | // Update the database |
||
1990 | $inserts[] = array($id_smiley, $set, $smiley_files[$key]); |
||
1991 | |||
1992 | // This isn't a new smiley |
||
1993 | $to_unset[$key][$id_smiley] = true; |
||
1994 | } |
||
1995 | } |
||
1996 | } |
||
1997 | |||
1998 | // Remove anything that isn't actually new from our list of files |
||
1999 | foreach ($to_unset as $key => $ids) |
||
2000 | { |
||
2001 | if (array_reduce($ids, function ($carry, $item) { return $carry * $item; }, true) == true) |
||
2002 | unset($smiley_files[$key]); |
||
2003 | } |
||
2004 | |||
2005 | // We only create brand new smileys if asked. |
||
2006 | if (empty($create)) |
||
2007 | $smiley_files = array(); |
||
2008 | |||
2009 | // New smileys go at the end of the list |
||
2010 | $request = $smcFunc['db_query']('', ' |
||
2011 | SELECT MAX(smiley_order) |
||
2012 | FROM {db_prefix}smileys |
||
2013 | WHERE hidden = {int:postform} |
||
2014 | AND smiley_row = {int:first_row}', |
||
2015 | array( |
||
2016 | 'postform' => 0, |
||
2017 | 'first_row' => 0, |
||
2018 | ) |
||
2019 | ); |
||
2020 | list ($smiley_order) = $smcFunc['db_fetch_row']($request); |
||
2021 | $smcFunc['db_free_result']($request); |
||
2022 | |||
2023 | // This part handles brand new smileys that don't exist in any set |
||
2024 | $new_smileys = array(); |
||
2025 | foreach ($smiley_files as $key => $smiley_file) |
||
2026 | { |
||
2027 | // Ensure every set has a file to use for the new smiley |
||
2028 | foreach ($known_sets as $set) |
||
2029 | { |
||
2030 | unset($basename); |
||
2031 | |||
2032 | if ($smileyPath != $set) |
||
2033 | { |
||
2034 | // Check whether any similarly named files exist in the other set's directory |
||
2035 | $similar_files = glob($modSettings['smileys_dir'] . '/' . $set . '/' . pathinfo($smiley_file, PATHINFO_FILENAME) . '.{' . implode(',', $allowedTypes) . '}', GLOB_BRACE); |
||
2036 | |||
2037 | // If there's a similarly named file already there, use it |
||
2038 | if (!empty($similar_files)) |
||
2039 | { |
||
2040 | // Prefer an exact match if there is one |
||
2041 | foreach ($similar_files as $similar_file) |
||
2042 | { |
||
2043 | if (basename($similar_file) == $smiley_file) |
||
2044 | $basename = $smiley_file; |
||
2045 | } |
||
2046 | |||
2047 | // Same name, different extension |
||
2048 | if (empty($basename)) |
||
2049 | $basename = basename(reset($similar_files)); |
||
2050 | } |
||
2051 | // Otherwise, copy the image to the other set's directory |
||
2052 | else |
||
2053 | { |
||
2054 | copy($modSettings['smileys_dir'] . '/' . $smileyPath . '/' . $smiley_file, $modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_file); |
||
2055 | smf_chmod($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_file, 0644); |
||
2056 | |||
2057 | $basename = $smiley_file; |
||
2058 | } |
||
2059 | |||
2060 | // Double-check that everything went as expected |
||
2061 | if (empty($basename) || !file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $basename)) |
||
2062 | continue; |
||
2063 | } |
||
2064 | else |
||
2065 | $basename = $smiley_file; |
||
2066 | |||
2067 | $new_smileys[$key]['files'][$set] = $basename; |
||
2068 | } |
||
2069 | |||
2070 | $new_smileys[$key]['info'] = array(':' . pathinfo($smiley_file, PATHINFO_FILENAME) . ':', pathinfo($smiley_file, PATHINFO_FILENAME), 0, ++$smiley_order); |
||
2071 | } |
||
2072 | |||
2073 | // Add the info for any new smileys to the database |
||
2074 | foreach ($new_smileys as $new_smiley) |
||
2075 | { |
||
2076 | $new_id_smiley = $smcFunc['db_insert']('', |
||
2077 | '{db_prefix}smileys', |
||
2078 | array( |
||
2079 | 'code' => 'string-30', 'description' => 'string-80', 'smiley_row' => 'int', 'smiley_order' => 'int', |
||
2080 | ), |
||
2081 | $new_smiley['info'], |
||
2082 | array('id_smiley'), |
||
2083 | 1 |
||
2084 | ); |
||
2085 | |||
2086 | // We'll also need to add filename info to the smiley_files table |
||
2087 | foreach ($new_smiley['files'] as $set => $filename) |
||
2088 | $inserts[] = array($new_id_smiley, $set, $filename); |
||
2089 | } |
||
2090 | |||
2091 | // Finally, update the smiley_files table with all our new files |
||
2092 | if (!empty($inserts)) |
||
2093 | { |
||
2094 | $smcFunc['db_insert']('replace', |
||
2095 | '{db_prefix}smiley_files', |
||
2096 | array( |
||
2097 | 'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48', |
||
2098 | ), |
||
2099 | $inserts, |
||
2100 | array('id_smiley', 'smiley_set') |
||
2101 | ); |
||
2102 | |||
2103 | foreach ($known_sets as $set) |
||
2104 | { |
||
2105 | cache_put_data('parsing_smileys_' . $set, null, 480); |
||
2106 | cache_put_data('posting_smileys_' . $set, null, 480); |
||
2107 | } |
||
2108 | } |
||
2109 | } |
||
2110 | |||
2111 | /** |
||
2112 | * Handles editing message icons |
||
2113 | */ |
||
2114 | function EditMessageIcons() |
||
2115 | { |
||
2116 | global $context, $settings, $txt; |
||
2117 | global $smcFunc, $scripturl, $sourcedir; |
||
2118 | |||
2119 | // Get a list of icons. |
||
2120 | $context['icons'] = array(); |
||
2121 | $request = $smcFunc['db_query']('', ' |
||
2122 | SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name |
||
2123 | FROM {db_prefix}message_icons AS m |
||
2124 | LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) |
||
2125 | WHERE ({query_see_board} OR b.id_board IS NULL) |
||
2126 | ORDER BY m.icon_order', |
||
2127 | array( |
||
2128 | ) |
||
2129 | ); |
||
2130 | $last_icon = 0; |
||
2131 | $trueOrder = 0; |
||
2132 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
2133 | { |
||
2134 | $context['icons'][$row['id_icon']] = array( |
||
2135 | 'id' => $row['id_icon'], |
||
2136 | 'title' => $row['title'], |
||
2137 | 'filename' => $row['filename'], |
||
2138 | 'image_url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $row['filename'] . '.png') ? 'actual_images_url' : 'default_images_url'] . '/post/' . $row['filename'] . '.png', |
||
2139 | 'board_id' => $row['id_board'], |
||
2140 | 'board' => empty($row['board_name']) ? $txt['icons_edit_icons_all_boards'] : $row['board_name'], |
||
2141 | 'order' => $row['icon_order'], |
||
2142 | 'true_order' => $trueOrder++, |
||
2143 | 'after' => $last_icon, |
||
2144 | ); |
||
2145 | $last_icon = $row['id_icon']; |
||
2146 | } |
||
2147 | $smcFunc['db_free_result']($request); |
||
2148 | |||
2149 | // Submitting a form? |
||
2150 | if (isset($_POST['icons_save']) || isset($_POST['delete'])) |
||
2151 | { |
||
2152 | checkSession(); |
||
2153 | |||
2154 | // Deleting icons? |
||
2155 | if (isset($_POST['delete']) && !empty($_POST['checked_icons'])) |
||
2156 | { |
||
2157 | $deleteIcons = array(); |
||
2158 | foreach ($_POST['checked_icons'] as $icon) |
||
2159 | $deleteIcons[] = (int) $icon; |
||
2160 | |||
2161 | // Do the actual delete! |
||
2162 | $smcFunc['db_query']('', ' |
||
2163 | DELETE FROM {db_prefix}message_icons |
||
2164 | WHERE id_icon IN ({array_int:icon_list})', |
||
2165 | array( |
||
2166 | 'icon_list' => $deleteIcons, |
||
2167 | ) |
||
2168 | ); |
||
2169 | } |
||
2170 | // Editing/Adding an icon? |
||
2171 | elseif ($context['sub_action'] == 'editicon' && isset($_GET['icon'])) |
||
2172 | { |
||
2173 | $_GET['icon'] = (int) $_GET['icon']; |
||
2174 | |||
2175 | // Do some preperation with the data... like check the icon exists *somewhere* |
||
2176 | if (strpos($_POST['icon_filename'], '.png') !== false) |
||
2177 | $_POST['icon_filename'] = substr($_POST['icon_filename'], 0, -4); |
||
2178 | if (!file_exists($settings['default_theme_dir'] . '/images/post/' . $_POST['icon_filename'] . '.png')) |
||
2179 | fatal_lang_error('icon_not_found'); |
||
2180 | // There is a 16 character limit on message icons... |
||
2181 | elseif (strlen($_POST['icon_filename']) > 16) |
||
2182 | fatal_lang_error('icon_name_too_long'); |
||
2183 | elseif ($_POST['icon_location'] == $_GET['icon'] && !empty($_GET['icon'])) |
||
2184 | fatal_lang_error('icon_after_itself'); |
||
2185 | |||
2186 | // First do the sorting... if this is an edit reduce the order of everything after it by one ;) |
||
2187 | if ($_GET['icon'] != 0) |
||
2188 | { |
||
2189 | $oldOrder = $context['icons'][$_GET['icon']]['true_order']; |
||
2190 | foreach ($context['icons'] as $id => $data) |
||
2191 | if ($data['true_order'] > $oldOrder) |
||
2192 | $context['icons'][$id]['true_order']--; |
||
2193 | } |
||
2194 | |||
2195 | // If there are no existing icons and this is a new one, set the id to 1 (mainly for non-mysql) |
||
2196 | if (empty($_GET['icon']) && empty($context['icons'])) |
||
2197 | $_GET['icon'] = 1; |
||
2198 | |||
2199 | // Get the new order. |
||
2200 | $newOrder = $_POST['icon_location'] == 0 ? 0 : $context['icons'][$_POST['icon_location']]['true_order'] + 1; |
||
2201 | // Do the same, but with the one that used to be after this icon, done to avoid conflict. |
||
2202 | foreach ($context['icons'] as $id => $data) |
||
2203 | if ($data['true_order'] >= $newOrder) |
||
2204 | $context['icons'][$id]['true_order']++; |
||
2205 | |||
2206 | // Finally set the current icon's position! |
||
2207 | $context['icons'][$_GET['icon']]['true_order'] = $newOrder; |
||
2208 | |||
2209 | // Simply replace the existing data for the other bits. |
||
2210 | $context['icons'][$_GET['icon']]['title'] = $_POST['icon_description']; |
||
2211 | $context['icons'][$_GET['icon']]['filename'] = $_POST['icon_filename']; |
||
2212 | $context['icons'][$_GET['icon']]['board_id'] = (int) $_POST['icon_board']; |
||
2213 | |||
2214 | // Do a huge replace ;) |
||
2215 | $iconInsert = array(); |
||
2216 | $iconInsert_new = array(); |
||
2217 | foreach ($context['icons'] as $id => $icon) |
||
2218 | { |
||
2219 | if ($id != 0) |
||
2220 | { |
||
2221 | $iconInsert[] = array($id, $icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']); |
||
2222 | } |
||
2223 | else |
||
2224 | { |
||
2225 | $iconInsert_new[] = array($icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']); |
||
2226 | } |
||
2227 | } |
||
2228 | |||
2229 | $smcFunc['db_insert']('replace', |
||
2230 | '{db_prefix}message_icons', |
||
2231 | array('id_icon' => 'int', 'id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'), |
||
2232 | $iconInsert, |
||
2233 | array('id_icon') |
||
2234 | ); |
||
2235 | |||
2236 | if (!empty($iconInsert_new)) |
||
2237 | { |
||
2238 | $smcFunc['db_insert']('insert', |
||
2239 | '{db_prefix}message_icons', |
||
2240 | array('id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'), |
||
2241 | $iconInsert_new, |
||
2242 | array('id_icon') |
||
2243 | ); |
||
2244 | } |
||
2245 | } |
||
2246 | |||
2247 | // Unless we're adding a new thing, we'll escape |
||
2248 | if (!isset($_POST['add'])) |
||
2249 | redirectexit('action=admin;area=smileys;sa=editicons'); |
||
2250 | } |
||
2251 | |||
2252 | $context[$context['admin_menu_name']]['current_subsection'] = 'editicons'; |
||
2253 | |||
2254 | $listOptions = array( |
||
2255 | 'id' => 'message_icon_list', |
||
2256 | 'title' => $txt['icons_edit_message_icons'], |
||
2257 | 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editicons', |
||
2258 | 'get_items' => array( |
||
2259 | 'function' => 'list_getMessageIcons', |
||
2260 | ), |
||
2261 | 'no_items_label' => $txt['icons_no_entries'], |
||
2262 | 'columns' => array( |
||
2263 | 'icon' => array( |
||
2264 | 'data' => array( |
||
2265 | 'function' => function($rowData) use ($settings, $smcFunc) |
||
2266 | { |
||
2267 | $images_url = $settings[file_exists(sprintf('%1$s/images/post/%2$s.png', $settings['theme_dir'], $rowData['filename'])) ? 'actual_images_url' : 'default_images_url']; |
||
2268 | return sprintf('<img src="%1$s/post/%2$s.png" alt="%3$s">', $images_url, $rowData['filename'], $smcFunc['htmlspecialchars']($rowData['title'])); |
||
2269 | }, |
||
2270 | 'class' => 'centercol', |
||
2271 | ), |
||
2272 | ), |
||
2273 | 'filename' => array( |
||
2274 | 'header' => array( |
||
2275 | 'value' => $txt['smileys_filename'], |
||
2276 | ), |
||
2277 | 'data' => array( |
||
2278 | 'sprintf' => array( |
||
2279 | 'format' => '%1$s.png', |
||
2280 | 'params' => array( |
||
2281 | 'filename' => true, |
||
2282 | ), |
||
2283 | ), |
||
2284 | ), |
||
2285 | ), |
||
2286 | 'description' => array( |
||
2287 | 'header' => array( |
||
2288 | 'value' => $txt['smileys_description'], |
||
2289 | ), |
||
2290 | 'data' => array( |
||
2291 | 'db_htmlsafe' => 'title', |
||
2292 | ), |
||
2293 | ), |
||
2294 | 'board' => array( |
||
2295 | 'header' => array( |
||
2296 | 'value' => $txt['icons_board'], |
||
2297 | ), |
||
2298 | 'data' => array( |
||
2299 | 'function' => function($rowData) use ($txt) |
||
2300 | { |
||
2301 | return empty($rowData['board_name']) ? $txt['icons_edit_icons_all_boards'] : $rowData['board_name']; |
||
2302 | }, |
||
2303 | ), |
||
2304 | ), |
||
2305 | 'modify' => array( |
||
2306 | 'header' => array( |
||
2307 | 'value' => $txt['smileys_modify'], |
||
2308 | 'class' => 'centercol', |
||
2309 | ), |
||
2310 | 'data' => array( |
||
2311 | 'sprintf' => array( |
||
2312 | 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon;icon=%1$s">' . $txt['smileys_modify'] . '</a>', |
||
2313 | 'params' => array( |
||
2314 | 'id_icon' => false, |
||
2315 | ), |
||
2316 | ), |
||
2317 | 'class' => 'centercol', |
||
2318 | ), |
||
2319 | ), |
||
2320 | 'check' => array( |
||
2321 | 'header' => array( |
||
2322 | 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">', |
||
2323 | 'class' => 'centercol', |
||
2324 | ), |
||
2325 | 'data' => array( |
||
2326 | 'sprintf' => array( |
||
2327 | 'format' => '<input type="checkbox" name="checked_icons[]" value="%1$d">', |
||
2328 | 'params' => array( |
||
2329 | 'id_icon' => false, |
||
2330 | ), |
||
2331 | ), |
||
2332 | 'class' => 'centercol', |
||
2333 | ), |
||
2334 | ), |
||
2335 | ), |
||
2336 | 'form' => array( |
||
2337 | 'href' => $scripturl . '?action=admin;area=smileys;sa=editicons', |
||
2338 | ), |
||
2339 | 'additional_rows' => array( |
||
2340 | array( |
||
2341 | 'position' => 'below_table_data', |
||
2342 | 'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button"> <a class="button" href="' . $scripturl . '?action=admin;area=smileys;sa=editicon">' . $txt['icons_add_new'] . '</a>', |
||
2343 | ), |
||
2344 | ), |
||
2345 | ); |
||
2346 | |||
2347 | require_once($sourcedir . '/Subs-List.php'); |
||
2348 | createList($listOptions); |
||
2349 | |||
2350 | // If we're adding/editing an icon we'll need a list of boards |
||
2351 | if ($context['sub_action'] == 'editicon' || isset($_POST['add'])) |
||
2352 | { |
||
2353 | // Force the sub_template just in case. |
||
2354 | $context['sub_template'] = 'editicon'; |
||
2355 | |||
2356 | $context['new_icon'] = !isset($_GET['icon']); |
||
2357 | |||
2358 | // Get the properties of the current icon from the icon list. |
||
2359 | if (!$context['new_icon']) |
||
2360 | $context['icon'] = $context['icons'][$_GET['icon']]; |
||
2361 | |||
2362 | // Get a list of boards needed for assigning this icon to a specific board. |
||
2363 | $boardListOptions = array( |
||
2364 | 'use_permissions' => true, |
||
2365 | 'selected_board' => isset($context['icon']['board_id']) ? $context['icon']['board_id'] : 0, |
||
2366 | ); |
||
2367 | require_once($sourcedir . '/Subs-MessageIndex.php'); |
||
2368 | $context['categories'] = getBoardList($boardListOptions); |
||
2369 | } |
||
2370 | } |
||
2371 | |||
2372 | /** |
||
2373 | * Callback function for createList(). |
||
2374 | * |
||
2375 | * @param int $start The item to start with (not used here) |
||
2376 | * @param int $items_per_page The number of items to display per page (not used here) |
||
2377 | * @param string $sort A string indicating how to sort the items (not used here) |
||
2378 | * @return array An array of information about message icons |
||
2379 | */ |
||
2380 | function list_getMessageIcons($start, $items_per_page, $sort) |
||
2381 | { |
||
2382 | global $smcFunc; |
||
2383 | |||
2384 | $request = $smcFunc['db_query']('', ' |
||
2385 | SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name |
||
2386 | FROM {db_prefix}message_icons AS m |
||
2387 | LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) |
||
2388 | WHERE ({query_see_board} OR b.id_board IS NULL) |
||
2389 | ORDER BY m.icon_order', |
||
2390 | array() |
||
2391 | ); |
||
2392 | |||
2393 | $message_icons = array(); |
||
2394 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
2395 | $message_icons[] = $row; |
||
2396 | $smcFunc['db_free_result']($request); |
||
2397 | |||
2398 | return $message_icons; |
||
2399 | } |
||
2400 | |||
2401 | ?> |