PageRenderTime 72ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/forum/Sources/ManageSmileys.php

https://github.com/leftnode/nooges.com
PHP | 1769 lines | 1407 code | 187 blank | 175 comment | 183 complexity | 6cc5e19ddefbc3e52826450002b39802 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**********************************************************************************
  3. * ManageSmileys.php *
  4. ***********************************************************************************
  5. * SMF: Simple Machines Forum *
  6. * Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
  7. * =============================================================================== *
  8. * Software Version: SMF 2.0 RC2 *
  9. * Software by: Simple Machines (http://www.simplemachines.org) *
  10. * Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
  11. * 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
  12. * Support, News, Updates at: http://www.simplemachines.org *
  13. ***********************************************************************************
  14. * This program is free software; you may redistribute it and/or modify it under *
  15. * the terms of the provided license as published by Simple Machines LLC. *
  16. * *
  17. * This program is distributed in the hope that it is and will be useful, but *
  18. * WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
  19. * or FITNESS FOR A PARTICULAR PURPOSE. *
  20. * *
  21. * See the "license.txt" file for details of the Simple Machines license. *
  22. * The latest version can always be found at http://www.simplemachines.org. *
  23. **********************************************************************************/
  24. if (!defined('SMF'))
  25. die('Hacking attempt...');
  26. /* // !!!
  27. void ManageSmileys()
  28. // !!!
  29. void EditSmileySettings()
  30. // !!!
  31. void EditSmileySets()
  32. // !!!
  33. void AddSmiley()
  34. // !!!
  35. void EditSmileys()
  36. // !!!
  37. void EditSmileyOrder()
  38. // !!!
  39. void InstallSmileySet()
  40. // !!!
  41. void ImportSmileys($smileyPath)
  42. // !!!
  43. void sortSmileyTable()
  44. // !!!
  45. */
  46. function ManageSmileys()
  47. {
  48. global $context, $txt, $scripturl, $modSettings;
  49. isAllowedTo('manage_smileys');
  50. loadLanguage('ManageSmileys');
  51. loadTemplate('ManageSmileys');
  52. $subActions = array(
  53. 'addsmiley' => 'AddSmiley',
  54. 'editicon' => 'EditMessageIcons',
  55. 'editicons' => 'EditMessageIcons',
  56. 'editsets' => 'EditSmileySets',
  57. 'editsmileys' => 'EditSmileys',
  58. 'import' => 'EditSmileySets',
  59. 'modifyset' => 'EditSmileySets',
  60. 'modifysmiley' => 'EditSmileys',
  61. 'setorder' => 'EditSmileyOrder',
  62. 'settings' => 'EditSmileySettings',
  63. 'install' => 'InstallSmileySet'
  64. );
  65. // Default the sub-action to 'edit smiley settings'.
  66. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'editsets';
  67. $context['page_title'] = $txt['smileys_manage'];
  68. $context['sub_action'] = $_REQUEST['sa'];
  69. $context['sub_template'] = $context['sub_action'];
  70. // Load up all the tabs...
  71. $context[$context['admin_menu_name']]['tab_data'] = array(
  72. 'title' => $txt['smileys_manage'],
  73. 'help' => 'smileys',
  74. 'description' => $txt['smiley_settings_explain'],
  75. 'tabs' => array(
  76. 'editsets' => array(
  77. 'description' => $txt['smiley_editsets_explain'],
  78. ),
  79. 'addsmiley' => array(
  80. 'description' => $txt['smiley_addsmiley_explain'],
  81. ),
  82. 'editsmileys' => array(
  83. 'description' => $txt['smiley_editsmileys_explain'],
  84. ),
  85. 'setorder' => array(
  86. 'description' => $txt['smiley_setorder_explain'],
  87. ),
  88. 'editicons' => array(
  89. 'description' => $txt['icons_edit_icons_explain'],
  90. ),
  91. 'settings' => array(
  92. 'description' => $txt['smiley_settings_explain'],
  93. ),
  94. ),
  95. );
  96. // Some settings may not be enabled, disallow these from the tabs as appropriate.
  97. if (empty($modSettings['messageIcons_enable']))
  98. $context[$context['admin_menu_name']]['tab_data']['tabs']['editicons']['disabled'] = true;
  99. if (empty($modSettings['smiley_enable']))
  100. {
  101. $context[$context['admin_menu_name']]['tab_data']['tabs']['addsmiley']['disabled'] = true;
  102. $context[$context['admin_menu_name']]['tab_data']['tabs']['editsmileys']['disabled'] = true;
  103. $context[$context['admin_menu_name']]['tab_data']['tabs']['setorder']['disabled'] = true;
  104. }
  105. // Call the right function for this sub-acton.
  106. $subActions[$_REQUEST['sa']]();
  107. }
  108. function EditSmileySettings($return_config = false)
  109. {
  110. global $modSettings, $context, $settings, $txt, $boarddir, $sourcedir, $scripturl;
  111. // The directories...
  112. $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
  113. $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
  114. // Get the names of the smiley sets.
  115. $smiley_sets = explode(',', $modSettings['smiley_sets_known']);
  116. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  117. $smiley_context = array();
  118. foreach ($smiley_sets as $i => $set)
  119. $smiley_context[$set] = $set_names[$i];
  120. // All the settings for the page...
  121. $config_vars = array(
  122. array('title', 'settings'),
  123. // Inline permissions.
  124. array('permissions', 'manage_smileys'),
  125. '',
  126. array('select', 'smiley_sets_default', $smiley_context),
  127. array('check', 'smiley_sets_enable'),
  128. array('check', 'smiley_enable', 'subtext' => $txt['smileys_enable_note']),
  129. array('text', 'smileys_url'),
  130. array('text', 'smileys_dir', 'invalid' => !$context['smileys_dir_found']),
  131. '',
  132. // Message icons.
  133. array('check', 'messageIcons_enable', 'subtext' => $txt['setting_messageIcons_enable_note']),
  134. );
  135. if ($return_config)
  136. return $config_vars;
  137. // Setup the basics of the settings template.
  138. require_once($sourcedir .'/ManageServer.php');
  139. $context['sub_template'] = 'show_settings';
  140. // Finish up the form...
  141. $context['post_url'] = $scripturl . '?action=admin;area=smileys;save;sa=settings';
  142. $context['permissions_excluded'] = array(-1);
  143. // Saving the settings?
  144. if (isset($_GET['save']))
  145. {
  146. // Validate the smiley set name.
  147. $_POST['smiley_sets_default'] = empty($smiley_context[$_POST['smiley_sets_default']]) ? 'default' : $_POST['smiley_sets_default'];
  148. // Make sure that the smileys are in the right order after enabling them.
  149. if (isset($_POST['smiley_enable']))
  150. sortSmileyTable();
  151. saveDBSettings($config_vars);
  152. cache_put_data('parsing_smileys', null, 480);
  153. cache_put_data('posting_smileys', null, 480);
  154. redirectexit('action=admin;area=smileys;sa=settings');
  155. }
  156. prepareDBSettingContext($config_vars);
  157. }
  158. function EditSmileySets()
  159. {
  160. global $modSettings, $context, $settings, $txt, $boarddir;
  161. global $smcFunc, $scripturl, $sourcedir;
  162. // Set the right tab to be selected.
  163. $context[$context['admin_menu_name']]['current_subsection'] = 'editsets';
  164. // They must've been submitted a form.
  165. if (isset($_POST[$context['session_var']]))
  166. {
  167. checkSession();
  168. // Delete selected smiley sets.
  169. if (!empty($_POST['delete']) && !empty($_POST['smiley_set']))
  170. {
  171. $set_paths = explode(',', $modSettings['smiley_sets_known']);
  172. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  173. foreach ($_POST['smiley_set'] as $id => $val)
  174. if (isset($set_paths[$id], $set_names[$id]) && !empty($id))
  175. unset($set_paths[$id], $set_names[$id]);
  176. updateSettings(array(
  177. 'smiley_sets_known' => implode(',', $set_paths),
  178. 'smiley_sets_names' => implode("\n", $set_names),
  179. 'smiley_sets_default' => in_array($modSettings['smiley_sets_default'], $set_paths) ? $modSettings['smiley_sets_default'] : $set_paths[0],
  180. ));
  181. cache_put_data('parsing_smileys', null, 480);
  182. cache_put_data('posting_smileys', null, 480);
  183. }
  184. // Add a new smiley set.
  185. elseif (!empty($_POST['add']))
  186. $context['sub_action'] = 'modifyset';
  187. // Create or modify a smiley set.
  188. elseif (isset($_POST['set']))
  189. {
  190. $set_paths = explode(',', $modSettings['smiley_sets_known']);
  191. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  192. // Create a new smiley set.
  193. if ($_POST['set'] == -1 && isset($_POST['smiley_sets_path']))
  194. {
  195. if (in_array($_POST['smiley_sets_path'], $set_paths))
  196. fatal_lang_error('smiley_set_already_exists');
  197. updateSettings(array(
  198. 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $_POST['smiley_sets_path'],
  199. 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $_POST['smiley_sets_name'],
  200. 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'],
  201. ));
  202. }
  203. // Modify an existing smiley set.
  204. else
  205. {
  206. // Make sure the smiley set exists.
  207. if (!isset($set_paths[$_POST['set']]) || !isset($set_names[$_POST['set']]))
  208. fatal_lang_error('smiley_set_not_found');
  209. // Make sure the path is not yet used by another smileyset.
  210. if (in_array($_POST['smiley_sets_path'], $set_paths) && $_POST['smiley_sets_path'] != $set_paths[$_POST['set']])
  211. fatal_lang_error('smiley_set_path_already_used');
  212. $set_paths[$_POST['set']] = $_POST['smiley_sets_path'];
  213. $set_names[$_POST['set']] = $_POST['smiley_sets_name'];
  214. updateSettings(array(
  215. 'smiley_sets_known' => implode(',', $set_paths),
  216. 'smiley_sets_names' => implode("\n", $set_names),
  217. 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path']
  218. ));
  219. }
  220. // The user might have checked to also import smileys.
  221. if (!empty($_POST['smiley_sets_import']))
  222. ImportSmileys($_POST['smiley_sets_path']);
  223. cache_put_data('parsing_smileys', null, 480);
  224. cache_put_data('posting_smileys', null, 480);
  225. }
  226. }
  227. // Load all available smileysets...
  228. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  229. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  230. foreach ($context['smiley_sets'] as $i => $set)
  231. $context['smiley_sets'][$i] = array(
  232. 'id' => $i,
  233. 'path' => $set,
  234. 'name' => $set_names[$i],
  235. 'selected' => $set == $modSettings['smiley_sets_default']
  236. );
  237. // Importing any smileys from an existing set?
  238. if ($context['sub_action'] == 'import')
  239. {
  240. checkSession('get');
  241. $_GET['set'] = (int) $_GET['set'];
  242. // Sanity check - then import.
  243. if (isset($context['smiley_sets'][$_GET['set']]))
  244. ImportSmileys($context['smiley_sets'][$_GET['set']]['path']);
  245. // Force the process to continue.
  246. $context['sub_action'] = 'modifyset';
  247. $context['sub_template'] = 'modifyset';
  248. }
  249. // If we're modifying or adding a smileyset, some context info needs to be set.
  250. if ($context['sub_action'] == 'modifyset')
  251. {
  252. $_GET['set'] = !isset($_GET['set']) ? -1 : (int) $_GET['set'];
  253. if ($_GET['set'] == -1 || !isset($context['smiley_sets'][$_GET['set']]))
  254. $context['current_set'] = array(
  255. 'id' => '-1',
  256. 'path' => '',
  257. 'name' => '',
  258. 'selected' => false,
  259. 'is_new' => true,
  260. );
  261. else
  262. {
  263. $context['current_set'] = &$context['smiley_sets'][$_GET['set']];
  264. $context['current_set']['is_new'] = false;
  265. // Calculate whether there are any smileys in the directory that can be imported.
  266. if (!empty($modSettings['smiley_enable']) && !empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']))
  267. {
  268. $smileys = array();
  269. $dir = dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']);
  270. while ($entry = $dir->read())
  271. {
  272. if (in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  273. $smileys[strtolower($entry)] = $entry;
  274. }
  275. $dir->close();
  276. // Exclude the smileys that are already in the database.
  277. $request = $smcFunc['db_query']('', '
  278. SELECT filename
  279. FROM {db_prefix}smileys
  280. WHERE filename IN ({array_string:smiley_list})',
  281. array(
  282. 'smiley_list' => $smileys,
  283. )
  284. );
  285. while ($row = $smcFunc['db_fetch_assoc']($request))
  286. if (isset($smileys[strtolower($row['filename'])]))
  287. unset($smileys[strtolower($row['filename'])]);
  288. $smcFunc['db_free_result']($request);
  289. $context['current_set']['can_import'] = count($smileys);
  290. // Setup this string to look nice.
  291. $txt['smiley_set_import_multiple'] = sprintf($txt['smiley_set_import_multiple'], $context['current_set']['can_import']);
  292. }
  293. }
  294. // Retrieve all potential smiley set directories.
  295. $context['smiley_set_dirs'] = array();
  296. if (!empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir']))
  297. {
  298. $dir = dir($modSettings['smileys_dir']);
  299. while ($entry = $dir->read())
  300. {
  301. if (!in_array($entry, array('.', '..')) && is_dir($modSettings['smileys_dir'] . '/' . $entry))
  302. $context['smiley_set_dirs'][] = array(
  303. 'id' => $entry,
  304. 'path' => $modSettings['smileys_dir'] . '/' . $entry,
  305. 'selectable' => $entry == $context['current_set']['path'] || !in_array($entry, explode(',', $modSettings['smiley_sets_known'])),
  306. 'current' => $entry == $context['current_set']['path'],
  307. );
  308. }
  309. $dir->close();
  310. }
  311. }
  312. $listOptions = array(
  313. 'id' => 'smiley_set_list',
  314. 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
  315. 'default_sort_col' => 'default',
  316. 'get_items' => array(
  317. 'function' => 'list_getSmileySets',
  318. ),
  319. 'get_count' => array(
  320. 'function' => 'list_getNumSmileySets',
  321. ),
  322. 'columns' => array(
  323. 'default' => array(
  324. 'header' => array(
  325. 'value' => $txt['smiley_sets_default'],
  326. ),
  327. 'data' => array(
  328. 'function' => create_function('$rowData', '
  329. return $rowData[\'selected\'] ? \'<strong>*</strong>\' : \'\';
  330. '),
  331. 'style' => 'text-align: center;',
  332. ),
  333. 'sort' => array(
  334. 'default' => 'selected DESC',
  335. ),
  336. ),
  337. 'name' => array(
  338. 'header' => array(
  339. 'value' => $txt['smiley_sets_name'],
  340. ),
  341. 'data' => array(
  342. 'db_htmlsafe' => 'name',
  343. 'class' => 'windowbg',
  344. ),
  345. 'sort' => array(
  346. 'default' => 'name',
  347. 'reverse' => 'name DESC',
  348. ),
  349. ),
  350. 'url' => array(
  351. 'header' => array(
  352. 'value' => $txt['smiley_sets_url'],
  353. ),
  354. 'data' => array(
  355. 'sprintf' => array(
  356. 'format' => $modSettings['smileys_url'] . '/<strong>%1$s</strong>/...',
  357. 'params' => array(
  358. 'path' => true,
  359. ),
  360. ),
  361. 'class' => 'windowbg',
  362. ),
  363. 'sort' => array(
  364. 'default' => 'path',
  365. 'reverse' => 'path DESC',
  366. ),
  367. ),
  368. 'modify' => array(
  369. 'header' => array(
  370. 'value' => $txt['smiley_set_modify'],
  371. ),
  372. 'data' => array(
  373. 'sprintf' => array(
  374. 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset;set=%1$d">' . $txt['smiley_set_modify'] . '</a>',
  375. 'params' => array(
  376. 'id' => true,
  377. ),
  378. ),
  379. 'style' => 'text-align: center;',
  380. ),
  381. ),
  382. 'check' => array(
  383. 'header' => array(
  384. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  385. ),
  386. 'data' => array(
  387. 'function' => create_function('$rowData', '
  388. return $rowData[\'id\'] == 0 ? \'\' : sprintf(\'<input type="checkbox" name="smiley_set[%1$d]" class="input_check" />\', $rowData[\'id\']);
  389. '),
  390. 'style' => 'text-align: center',
  391. ),
  392. ),
  393. ),
  394. 'form' => array(
  395. 'href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
  396. ),
  397. 'additional_rows' => array(
  398. array(
  399. 'position' => 'below_table_data',
  400. 'value' => '<input type="submit" name="delete" value="' . $txt['smiley_sets_delete'] . '" onclick="return confirm(\'' . $txt['smiley_sets_confirm'] . '\');" style="float: right;" class="button_submit" /> [<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset' . '">' . $txt['smiley_sets_add'] . '</a>]',
  401. 'class' => 'titlebg',
  402. ),
  403. ),
  404. );
  405. require_once($sourcedir . '/Subs-List.php');
  406. createList($listOptions);
  407. }
  408. // !!! to be moved to Subs-Smileys.
  409. function list_getSmileySets($start, $items_per_page, $sort)
  410. {
  411. global $modSettings;
  412. $known_sets = explode(',', $modSettings['smiley_sets_known']);
  413. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  414. $cols = array(
  415. 'id' => array(),
  416. 'selected' => array(),
  417. 'path' => array(),
  418. 'name' => array(),
  419. );
  420. foreach ($known_sets as $i => $set)
  421. {
  422. $cols['id'][] = $i;
  423. $cols['selected'][] = $i;
  424. $cols['path'][] = $set;
  425. $cols['name'][] = $set_names[$i];
  426. }
  427. $sort_flag = strpos($sort, 'DESC') === false ? SORT_ASC : SORT_DESC;
  428. if (substr($sort, 0, 4) === 'name')
  429. array_multisort($cols['name'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['selected'], $cols['id']);
  430. elseif (substr($sort, 0, 4) === 'path')
  431. array_multisort($cols['path'], $sort_flag, SORT_REGULAR, $cols['name'], $cols['selected'], $cols['id']);
  432. else
  433. array_multisort($cols['selected'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['name'], $cols['id']);
  434. $smiley_sets = array();
  435. foreach ($cols['id'] as $i => $id)
  436. $smiley_sets[] = array(
  437. 'id' => $id,
  438. 'path' => $cols['path'][$i],
  439. 'name' => $cols['name'][$i],
  440. 'selected' => $cols['path'][$i] == $modSettings['smiley_sets_default']
  441. );
  442. return $smiley_sets;
  443. }
  444. // !!! to be moved to Subs-Smileys.
  445. function list_getNumSmileySets()
  446. {
  447. global $modSettings;
  448. return count(explode(',', $modSettings['smiley_sets_known']));
  449. }
  450. function AddSmiley()
  451. {
  452. global $modSettings, $context, $settings, $txt, $boarddir, $smcFunc;
  453. // Get a list of all known smiley sets.
  454. $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
  455. $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
  456. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  457. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  458. foreach ($context['smiley_sets'] as $i => $set)
  459. $context['smiley_sets'][$i] = array(
  460. 'id' => $i,
  461. 'path' => $set,
  462. 'name' => $set_names[$i],
  463. 'selected' => $set == $modSettings['smiley_sets_default']
  464. );
  465. // Submitting a form?
  466. if (isset($_POST[$context['session_var']], $_POST['smiley_code']))
  467. {
  468. checkSession();
  469. // Some useful arrays... types we allow - and ports we don't!
  470. $allowedTypes = array('jpeg', 'jpg', 'gif', 'png', 'bmp');
  471. $disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php');
  472. $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
  473. $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
  474. $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
  475. // Make sure some code was entered.
  476. if (empty($_POST['smiley_code']))
  477. fatal_lang_error('smiley_has_no_code');
  478. // Check whether the new code has duplicates. It should be unique.
  479. $request = $smcFunc['db_query']('', '
  480. SELECT id_smiley
  481. FROM {db_prefix}smileys
  482. WHERE code = {raw:mysql_binary_statement} {string:smiley_code}',
  483. array(
  484. 'mysql_binary_statement' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
  485. 'smiley_code' => $_POST['smiley_code'],
  486. )
  487. );
  488. if ($smcFunc['db_num_rows']($request) > 0)
  489. fatal_lang_error('smiley_not_unique');
  490. $smcFunc['db_free_result']($request);
  491. // If we are uploading - check all the smiley sets are writable!
  492. if ($_POST['method'] != 'existing')
  493. {
  494. $writeErrors = array();
  495. foreach ($context['smiley_sets'] as $set)
  496. {
  497. if (!is_writable($context['smileys_dir'] . '/' . $set['path']))
  498. $writeErrors[] = $set['path'];
  499. }
  500. if (!empty($writeErrors))
  501. fatal_lang_error('smileys_upload_error_notwritable', true, array(implode(', ', $writeErrors)));
  502. }
  503. // Uploading just one smiley for all of them?
  504. if (isset($_POST['sameall']) && isset($_FILES['uploadSmiley']['name']) && $_FILES['uploadSmiley']['name'] != '')
  505. {
  506. if (!is_uploaded_file($_FILES['uploadSmiley']['tmp_name']) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['uploadSmiley']['tmp_name'])))
  507. fatal_lang_error('smileys_upload_error');
  508. // Sorry, no spaces, dots, or anything else but letters allowed.
  509. $_FILES['uploadSmiley']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['uploadSmiley']['name']);
  510. // We only allow image files - it's THAT simple - no messing around here...
  511. if (!in_array(strtolower(substr(strrchr($_FILES['uploadSmiley']['name'], '.'), 1)), $allowedTypes))
  512. fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
  513. // We only need the filename...
  514. $destName = basename($_FILES['uploadSmiley']['name']);
  515. // Make sure they aren't trying to upload a nasty file - for their own good here!
  516. if (in_array(strtolower($destName), $disabledFiles))
  517. fatal_lang_error('smileys_upload_error_illegal');
  518. // Check if the file already exists... and if not move it to EVERY smiley set directory.
  519. $i = 0;
  520. // Keep going until we find a set the file doesn't exist in. (or maybe it exists in all of them?)
  521. while (isset($context['smiley_sets'][$i]) && file_exists($context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['path'] . '/' . $destName))
  522. $i++;
  523. // Okay, we're going to put the smiley right here, since it's not there yet!
  524. if (isset($context['smiley_sets'][$i]['path']))
  525. {
  526. $smileyLocation = $context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['path'] . '/' . $destName;
  527. move_uploaded_file($_FILES['uploadSmiley']['tmp_name'], $smileyLocation);
  528. @chmod($smileyLocation, 0644);
  529. // Now, we want to move it from there to all the other sets.
  530. for ($n = count($context['smiley_sets']); $i < $n; $i++)
  531. {
  532. $currentPath = $context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['path'] . '/' . $destName;
  533. // The file is already there! Don't overwrite it!
  534. if (file_exists($currentPath))
  535. continue;
  536. // Okay, so copy the first one we made to here.
  537. copy($smileyLocation, $currentPath);
  538. @chmod($currentPath, 0644);
  539. }
  540. }
  541. // Finally make sure it's saved correctly!
  542. $_POST['smiley_filename'] = $destName;
  543. }
  544. // What about uploading several files?
  545. elseif ($_POST['method'] != 'existing')
  546. {
  547. foreach ($_FILES as $name => $data)
  548. {
  549. if ($_FILES[$name]['name'] == '')
  550. fatal_lang_error('smileys_upload_error_blank');
  551. if (empty($newName))
  552. $newName = basename($_FILES[$name]['name']);
  553. elseif (basename($_FILES[$name]['name']) != $newName)
  554. fatal_lang_error('smileys_upload_error_name');
  555. }
  556. foreach ($context['smiley_sets'] as $i => $set)
  557. {
  558. if (!isset($_FILES['individual_' . $set['name']]['name']) || $_FILES['individual_' . $set['name']]['name'] == '')
  559. continue;
  560. // Got one...
  561. if (!is_uploaded_file($_FILES['individual_' . $set['name']]['tmp_name']) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['individual_' . $set['name']]['tmp_name'])))
  562. fatal_lang_error('smileys_upload_error');
  563. // Sorry, no spaces, dots, or anything else but letters allowed.
  564. $_FILES['individual_' . $set['name']]['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['individual_' . $set['name']]['name']);
  565. // We only allow image files - it's THAT simple - no messing around here...
  566. if (!in_array(strtolower(substr(strrchr($_FILES['individual_' . $set['name']]['name'], '.'), 1)), $allowedTypes))
  567. fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
  568. // We only need the filename...
  569. $destName = basename($_FILES['individual_' . $set['name']]['name']);
  570. // Make sure they aren't trying to upload a nasty file - for their own good here!
  571. if (in_array(strtolower($destName), $disabledFiles))
  572. fatal_lang_error('smileys_upload_error_illegal');
  573. // If the file exists - ignore it.
  574. $smileyLocation = $context['smileys_dir'] . '/' . $set['path'] . '/' . $destName;
  575. if (file_exists($smileyLocation))
  576. continue;
  577. // Finally - move the image!
  578. move_uploaded_file($_FILES['individual_' . $set['name']]['tmp_name'], $smileyLocation);
  579. @chmod($smileyLocation, 0644);
  580. // Should always be saved correctly!
  581. $_POST['smiley_filename'] = $destName;
  582. }
  583. }
  584. // Also make sure a filename was given.
  585. if (empty($_POST['smiley_filename']))
  586. fatal_lang_error('smiley_has_no_filename');
  587. // Find the position on the right.
  588. $smiley_order = '0';
  589. if ($_POST['smiley_location'] != 1)
  590. {
  591. $request = $smcFunc['db_query']('', '
  592. SELECT MAX(smiley_order) + 1
  593. FROM {db_prefix}smileys
  594. WHERE hidden = {int:smiley_location}
  595. AND smiley_row = {int:first_row}',
  596. array(
  597. 'smiley_location' => $_POST['smiley_location'],
  598. 'first_row' => 0,
  599. )
  600. );
  601. list ($smiley_order) = $smcFunc['db_fetch_row']($request);
  602. $smcFunc['db_free_result']($request);
  603. if (empty($smiley_order))
  604. $smiley_order = '0';
  605. }
  606. $smcFunc['db_insert']('',
  607. '{db_prefix}smileys',
  608. array(
  609. 'code' => 'string-30', 'filename' => 'string-48', 'description' => 'string-80', 'hidden' => 'int', 'smiley_order' => 'int',
  610. ),
  611. array(
  612. $_POST['smiley_code'], $_POST['smiley_filename'], $_POST['smiley_description'], $_POST['smiley_location'], $smiley_order,
  613. ),
  614. array('id_smiley')
  615. );
  616. cache_put_data('parsing_smileys', null, 480);
  617. cache_put_data('posting_smileys', null, 480);
  618. // No errors? Out of here!
  619. redirectexit('action=admin;area=smileys;sa=editsmileys');
  620. }
  621. $context['selected_set'] = $modSettings['smiley_sets_default'];
  622. // Get all possible filenames for the smileys.
  623. $context['filenames'] = array();
  624. if ($context['smileys_dir_found'])
  625. {
  626. foreach ($context['smiley_sets'] as $smiley_set)
  627. {
  628. if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['path']))
  629. continue;
  630. $dir = dir($context['smileys_dir'] . '/' . $smiley_set['path']);
  631. while ($entry = $dir->read())
  632. {
  633. if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  634. $context['filenames'][strtolower($entry)] = array(
  635. 'id' => htmlspecialchars($entry),
  636. 'selected' => false,
  637. );
  638. }
  639. $dir->close();
  640. }
  641. ksort($context['filenames']);
  642. }
  643. // Create a new smiley from scratch.
  644. $context['filenames'] = array_values($context['filenames']);
  645. $context['current_smiley'] = array(
  646. 'id' => 0,
  647. 'code' => '',
  648. 'filename' => $context['filenames'][0]['id'],
  649. 'description' => $txt['smileys_default_description'],
  650. 'location' => 0,
  651. 'is_new' => true,
  652. );
  653. }
  654. function EditSmileys()
  655. {
  656. global $modSettings, $context, $settings, $txt, $boarddir;
  657. global $smcFunc, $scripturl, $sourcedir;
  658. // Force the correct tab to be displayed.
  659. $context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys';
  660. // Submitting a form?
  661. if (isset($_POST[$context['session_var']]))
  662. {
  663. checkSession();
  664. // Changing the selected smileys?
  665. if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys']))
  666. {
  667. foreach ($_POST['checked_smileys'] as $id => $smiley_id)
  668. $_POST['checked_smileys'][$id] = (int) $smiley_id;
  669. if ($_POST['smiley_action'] == 'delete')
  670. $smcFunc['db_query']('', '
  671. DELETE FROM {db_prefix}smileys
  672. WHERE id_smiley IN ({array_int:checked_smileys})',
  673. array(
  674. 'checked_smileys' => $_POST['checked_smileys'],
  675. )
  676. );
  677. // Changing the status of the smiley?
  678. else
  679. {
  680. // Check it's a valid type.
  681. $displayTypes = array(
  682. 'post' => 0,
  683. 'hidden' => 1,
  684. 'popup' => 2
  685. );
  686. if (isset($displayTypes[$_POST['smiley_action']]))
  687. $smcFunc['db_query']('', '
  688. UPDATE {db_prefix}smileys
  689. SET hidden = {int:display_type}
  690. WHERE id_smiley IN ({array_int:checked_smileys})',
  691. array(
  692. 'checked_smileys' => $_POST['checked_smileys'],
  693. 'display_type' => $displayTypes[$_POST['smiley_action']],
  694. )
  695. );
  696. }
  697. }
  698. // Create/modify a smiley.
  699. elseif (isset($_POST['smiley']))
  700. {
  701. // Is it a delete?
  702. if (!empty($_POST['deletesmiley']))
  703. {
  704. $smcFunc['db_query']('', '
  705. DELETE FROM {db_prefix}smileys
  706. WHERE id_smiley = {int:current_smiley}',
  707. array(
  708. 'current_smiley' => $_POST['smiley'],
  709. )
  710. );
  711. }
  712. // Otherwise an edit.
  713. else
  714. {
  715. $_POST['smiley'] = (int) $_POST['smiley'];
  716. $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
  717. $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
  718. $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
  719. // Make sure some code was entered.
  720. if (empty($_POST['smiley_code']))
  721. fatal_lang_error('smiley_has_no_code');
  722. // Also make sure a filename was given.
  723. if (empty($_POST['smiley_filename']))
  724. fatal_lang_error('smiley_has_no_filename');
  725. // Check whether the new code has duplicates. It should be unique.
  726. $request = $smcFunc['db_query']('', '
  727. SELECT id_smiley
  728. FROM {db_prefix}smileys
  729. WHERE code = {raw:mysql_binary_type} {string:smiley_code}' . (empty($_POST['smiley']) ? '' : '
  730. AND id_smiley != {int:current_smiley}'),
  731. array(
  732. 'current_smiley' => $_POST['smiley'],
  733. 'mysql_binary_type' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
  734. 'smiley_code' => $_POST['smiley_code'],
  735. )
  736. );
  737. if ($smcFunc['db_num_rows']($request) > 0)
  738. fatal_lang_error('smiley_not_unique');
  739. $smcFunc['db_free_result']($request);
  740. $smcFunc['db_query']('', '
  741. UPDATE {db_prefix}smileys
  742. SET
  743. code = {string:smiley_code},
  744. filename = {string:smiley_filename},
  745. description = {string:smiley_description},
  746. hidden = {int:smiley_location}
  747. WHERE id_smiley = {int:current_smiley}',
  748. array(
  749. 'smiley_location' => $_POST['smiley_location'],
  750. 'current_smiley' => $_POST['smiley'],
  751. 'smiley_code' => $_POST['smiley_code'],
  752. 'smiley_filename' => $_POST['smiley_filename'],
  753. 'smiley_description' => $_POST['smiley_description'],
  754. )
  755. );
  756. }
  757. // Sort all smiley codes for more accurate parsing (longest code first).
  758. sortSmileyTable();
  759. }
  760. cache_put_data('parsing_smileys', null, 480);
  761. cache_put_data('posting_smileys', null, 480);
  762. }
  763. // Load all known smiley sets.
  764. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  765. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  766. foreach ($context['smiley_sets'] as $i => $set)
  767. $context['smiley_sets'][$i] = array(
  768. 'id' => $i,
  769. 'path' => $set,
  770. 'name' => $set_names[$i],
  771. 'selected' => $set == $modSettings['smiley_sets_default']
  772. );
  773. // Prepare overview of all (custom) smileys.
  774. if ($context['sub_action'] == 'editsmileys')
  775. {
  776. // Determine the language specific sort order of smiley locations.
  777. $smiley_locations = array(
  778. $txt['smileys_location_form'],
  779. $txt['smileys_location_hidden'],
  780. $txt['smileys_location_popup'],
  781. );
  782. asort($smiley_locations);
  783. // Create a list of options for selecting smiley sets.
  784. $smileyset_option_list = '
  785. <select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">';
  786. foreach ($context['smiley_sets'] as $smiley_set)
  787. $smileyset_option_list .= '
  788. <option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected="selected"' : '') . '>' . $smiley_set['name'] . '</option>';
  789. $smileyset_option_list .= '
  790. </select>';
  791. $listOptions = array(
  792. 'id' => 'smiley_list',
  793. 'items_per_page' => 40,
  794. 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
  795. 'default_sort_col' => 'filename',
  796. 'get_items' => array(
  797. 'function' => 'list_getSmileys',
  798. ),
  799. 'get_count' => array(
  800. 'function' => 'list_getNumSmileys',
  801. ),
  802. 'no_items_label' => $txt['smileys_no_entries'],
  803. 'columns' => array(
  804. 'picture' => array(
  805. 'data' => array(
  806. 'sprintf' => array(
  807. 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d"><img src="' . $modSettings['smileys_url'] . '/' . $modSettings['smiley_sets_default'] . '/%2$s" alt="%3$s" style="padding: 2px;" id="smiley%1$d" /><input type="hidden" name="smileys[%1$d][filename]" value="%2$s" /></a>',
  808. 'params' => array(
  809. 'id_smiley' => false,
  810. 'filename' => true,
  811. 'description' => true,
  812. ),
  813. ),
  814. 'style' => 'text-align: center;',
  815. ),
  816. ),
  817. 'code' => array(
  818. 'header' => array(
  819. 'value' => $txt['smileys_code'],
  820. ),
  821. 'data' => array(
  822. 'db_htmlsafe' => 'code',
  823. ),
  824. 'sort' => array(
  825. 'default' => 'code',
  826. 'reverse' => 'code DESC',
  827. ),
  828. ),
  829. 'filename' => array(
  830. 'header' => array(
  831. 'value' => $txt['smileys_filename'],
  832. ),
  833. 'data' => array(
  834. 'db_htmlsafe' => 'filename',
  835. 'class' => 'windowbg',
  836. ),
  837. 'sort' => array(
  838. 'default' => 'filename',
  839. 'reverse' => 'filename DESC',
  840. ),
  841. ),
  842. 'location' => array(
  843. 'header' => array(
  844. 'value' => $txt['smileys_location'],
  845. ),
  846. 'data' => array(
  847. 'function' => create_function('$rowData', '
  848. global $txt;
  849. if (empty($rowData[\'hidden\']))
  850. return $txt[\'smileys_location_form\'];
  851. elseif ($rowData[\'hidden\'] == 1)
  852. return $txt[\'smileys_location_hidden\'];
  853. else
  854. return $txt[\'smileys_location_popup\'];
  855. '),
  856. 'class' => 'windowbg',
  857. ),
  858. 'sort' => array(
  859. 'default' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\')',
  860. 'reverse' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\') DESC',
  861. ),
  862. ),
  863. 'tooltip' => array(
  864. 'header' => array(
  865. 'value' => $txt['smileys_description'],
  866. ),
  867. 'data' => array(
  868. 'function' => create_function('$rowData', empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir']) ? '
  869. return htmlspecialchars($rowData[\'description\']);
  870. ' : '
  871. global $context, $txt, $modSettings;
  872. // Check if there are smileys missing in some sets.
  873. $missing_sets = array();
  874. foreach ($context[\'smiley_sets\'] as $smiley_set)
  875. if (!file_exists(sprintf(\'%1$s/%2$s/%3$s\', $modSettings[\'smileys_dir\'], $smiley_set[\'path\'], $rowData[\'filename\'])))
  876. $missing_sets[] = $smiley_set[\'path\'];
  877. $description = htmlspecialchars($rowData[\'description\']);
  878. if (!empty($missing_sets))
  879. $description .= sprintf(\'<br /><span class="smalltext"><strong>%1$s:</strong> %2$s</span>\', $txt[\'smileys_not_found_in_set\'], implode(\', \', $missing_sets));
  880. return $description;
  881. '),
  882. 'class' => 'windowbg',
  883. ),
  884. 'sort' => array(
  885. 'default' => 'description',
  886. 'reverse' => 'description DESC',
  887. ),
  888. ),
  889. 'modify' => array(
  890. 'header' => array(
  891. 'value' => $txt['smileys_modify'],
  892. ),
  893. 'data' => array(
  894. 'sprintf' => array(
  895. 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>',
  896. 'params' => array(
  897. 'id_smiley' => false,
  898. ),
  899. ),
  900. 'style' => 'text-align: center;',
  901. ),
  902. ),
  903. 'check' => array(
  904. 'header' => array(
  905. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  906. ),
  907. 'data' => array(
  908. 'sprintf' => array(
  909. 'format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d" class="input_check" />',
  910. 'params' => array(
  911. 'id_smiley' => false,
  912. ),
  913. ),
  914. 'style' => 'text-align: center',
  915. ),
  916. ),
  917. ),
  918. 'form' => array(
  919. 'href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
  920. 'name' => 'smileyForm',
  921. ),
  922. 'additional_rows' => array(
  923. array(
  924. 'position' => 'above_column_headers',
  925. 'value' => $smileyset_option_list,
  926. 'style' => 'text-align: right;',
  927. 'class' => 'titlebg',
  928. ),
  929. array(
  930. 'position' => 'below_table_data',
  931. 'value' => '
  932. <select name="smiley_action" onchange="makeChanges(this.value);">
  933. <option value="-1">' . $txt['smileys_with_selected'] . ':</option>
  934. <option value="-1">--------------</option>
  935. <option value="hidden">' . $txt['smileys_make_hidden'] . '</option>
  936. <option value="post">' . $txt['smileys_show_on_post'] . '</option>
  937. <option value="popup">' . $txt['smileys_show_on_popup'] . '</option>
  938. <option value="delete">' . $txt['smileys_remove'] . '</option>
  939. </select>
  940. <noscript><input type="submit" name="perform_action" value="'. $txt['go'] . '" class="button_submit" /></noscript>',
  941. 'style' => 'text-align: right;',
  942. 'class' => 'titlebg',
  943. ),
  944. ),
  945. 'javascript' => '
  946. function makeChanges(action)
  947. {
  948. if (action == \'-1\')
  949. return false;
  950. else if (action == \'delete\')
  951. {
  952. if (confirm(\'' . $txt['smileys_confirm'] . '\'))
  953. document.forms.smileyForm.submit();
  954. }
  955. else
  956. document.forms.smileyForm.submit();
  957. return true;
  958. }
  959. function changeSet(newSet)
  960. {
  961. var currentImage, i, knownSmileys = [];
  962. if (knownSmileys.length == 0)
  963. {
  964. for (var i = 0, n = document.images.length; i < n; i++)
  965. if (document.images[i].id.substr(0, 6) == \'smiley\')
  966. knownSmileys[knownSmileys.length] = document.images[i].id.substr(6);
  967. }
  968. for (i = 0; i < knownSmileys.length; i++)
  969. {
  970. currentImage = document.getElementById("smiley" + knownSmileys[i]);
  971. currentImage.src = "' . $modSettings['smileys_url'] . '/" + newSet + "/" + document.forms.smileyForm["smileys[" + knownSmileys[i] + "][filename]"].value;
  972. }
  973. }',
  974. );
  975. require_once($sourcedir . '/Subs-List.php');
  976. createList($listOptions);
  977. // The list is the only thing to show, so make it the main template.
  978. $context['default_list'] = 'smiley_list';
  979. $context['sub_template'] = 'show_list';
  980. }
  981. // Modifying smileys.
  982. elseif ($context['sub_action'] == 'modifysmiley')
  983. {
  984. // Get a list of all known smiley sets.
  985. $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
  986. $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
  987. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  988. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  989. foreach ($context['smiley_sets'] as $i => $set)
  990. $context['smiley_sets'][$i] = array(
  991. 'id' => $i,
  992. 'path' => $set,
  993. 'name' => $set_names[$i],
  994. 'selected' => $set == $modSettings['smiley_sets_default']
  995. );
  996. $context['selected_set'] = $modSettings['smiley_sets_default'];
  997. // Get all possible filenames for the smileys.
  998. $context['filenames'] = array();
  999. if ($context['smileys_dir_found'])
  1000. {
  1001. foreach ($context['smiley_sets'] as $smiley_set)
  1002. {
  1003. if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['path']))
  1004. continue;
  1005. $dir = dir($context['smileys_dir'] . '/' . $smiley_set['path']);
  1006. while ($entry = $dir->read())
  1007. {
  1008. if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  1009. $context['filenames'][strtolower($entry)] = array(
  1010. 'id' => htmlspecialchars($entry),
  1011. 'selected' => false,
  1012. );
  1013. }
  1014. $dir->close();
  1015. }
  1016. ksort($context['filenames']);
  1017. }
  1018. $request = $smcFunc['db_query']('', '
  1019. SELECT id_smiley AS id, code, filename, description, hidden AS location, 0 AS is_new
  1020. FROM {db_prefix}smileys
  1021. WHERE id_smiley = {int:current_smiley}',
  1022. array(
  1023. 'current_smiley' => (int) $_REQUEST['smiley'],
  1024. )
  1025. );
  1026. if ($smcFunc['db_num_rows']($request) != 1)
  1027. fatal_lang_error('smiley_not_found');
  1028. $context['current_smiley'] = $smcFunc['db_fetch_assoc']($request);
  1029. $smcFunc['db_free_result']($request);
  1030. $context['current_smiley']['code'] = htmlspecialchars($context['current_smiley']['code']);
  1031. $context['current_smiley']['filename'] = htmlspecialchars($context['current_smiley']['filename']);
  1032. $context['current_smiley']['description'] = htmlspecialchars($context['current_smiley']['description']);
  1033. if (isset($context['filenames'][strtolower($context['current_smiley']['filename'])]))
  1034. $context['filenames'][strtolower($context['current_smiley']['filename'])]['selected'] = true;
  1035. }
  1036. }
  1037. function list_getSmileys($start, $items_per_page, $sort)
  1038. {
  1039. global $smcFunc;
  1040. $request = $smcFunc['db_query']('', '
  1041. SELECT id_smiley, code, filename, description, smiley_row, smiley_order, hidden
  1042. FROM {db_prefix}smileys
  1043. ORDER BY ' . $sort,
  1044. array(
  1045. )
  1046. );
  1047. $smileys = array();
  1048. while ($row = $smcFunc['db_fetch_assoc']($request))
  1049. $smileys[] = $row;
  1050. $smcFunc['db_free_result']($request);
  1051. return $smileys;
  1052. }
  1053. function list_getNumSmileys()
  1054. {
  1055. global $smcFunc;
  1056. $request = $smcFunc['db_query']('', '
  1057. SELECT COUNT(*)
  1058. FROM {db_prefix}smileys',
  1059. array(
  1060. )
  1061. );
  1062. list($numSmileys) = $smcFunc['db_fetch_row'];
  1063. $smcFunc['db_free_result']($request);
  1064. return $numSmileys;
  1065. }
  1066. function EditSmileyOrder()
  1067. {
  1068. global $modSettings, $context, $settings, $txt, $boarddir, $smcFunc;
  1069. // Move smileys to another position.
  1070. if (isset($_REQUEST['reorder']))
  1071. {
  1072. checkSession('get');
  1073. $_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2;
  1074. $_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source'];
  1075. if (empty($_GET['source']))
  1076. fatal_lang_error('smiley_not_found');
  1077. if (!empty($_GET['after']))
  1078. {
  1079. $_GET['after'] = (int) $_GET['after'];
  1080. $request = $smcFunc['db_query']('', '
  1081. SELECT smiley_row, smiley_order, hidden
  1082. FROM {db_prefix}smileys
  1083. WHERE hidden = {int:location}
  1084. AND id_smiley = {int:after_smiley}',
  1085. array(
  1086. 'location' => $_GET['location'],
  1087. 'after_smiley' => $_GET['after'],
  1088. )
  1089. );
  1090. if ($smcFunc['db_num_rows']($request) != 1)
  1091. fatal_lang_error('smiley_not_found');
  1092. list ($smiley_row, $smiley_order, $smileyLocation) = $smcFunc['db_fetch_row']($request);
  1093. $smcFunc['db_free_result']($request);
  1094. }
  1095. else
  1096. {
  1097. $smiley_row = (int) $_GET['row'];
  1098. $smiley_order = -1;
  1099. $smileyLocation = (int) $_GET['location'];
  1100. }
  1101. $smcFunc['db_query']('', '
  1102. UPDATE {db_prefix}smileys
  1103. SET smiley_order = smiley_order + 1
  1104. WHERE hidden = {int:new_location}
  1105. AND smiley_row = {int:smiley_row}
  1106. AND smiley_order > {int:smiley_order}',
  1107. array(
  1108. 'new_location' => $_GET['location'],
  1109. 'smiley_row' => $smiley_row,
  1110. 'smiley_order' => $smiley_order,
  1111. )
  1112. );
  1113. $smcFunc['db_query']('', '
  1114. UPDATE {db_prefix}smileys
  1115. SET
  1116. smiley_order = {int:smiley_order} + 1,
  1117. smiley_row = {int:smiley_row},
  1118. hidden = {int:new_location}
  1119. WHERE id_smiley = {int:current_smiley}',
  1120. array(
  1121. 'smiley_order' => $smiley_order,
  1122. 'smiley_row' => $smiley_row,
  1123. 'new_location' => $smileyLocation,
  1124. 'current_smiley' => $_GET['source'],
  1125. )
  1126. );
  1127. cache_put_data('parsing_smileys', null, 480);
  1128. cache_put_data('posting_smileys', null, 480);
  1129. }
  1130. $request = $smcFunc['db_query']('', '
  1131. SELECT id_smiley, code, filename, description, smiley_row, smiley_order, hidden
  1132. FROM {db_prefix}smileys
  1133. WHERE hidden != {int:popup}
  1134. ORDER BY smiley_order, smiley_row',
  1135. array(
  1136. 'popup' => 1,
  1137. )
  1138. );
  1139. $context['smileys'] = array(
  1140. 'postform' => array(
  1141. 'rows' => array(),
  1142. ),
  1143. 'popup' => array(
  1144. 'rows' => array(),
  1145. ),
  1146. );
  1147. while ($row = $smcFunc['db_fetch_assoc']($request))
  1148. {
  1149. $location = empty($row['hidden']) ? 'postform' : 'popup';
  1150. $context['smileys'][$location]['rows'][$row['smiley_row']][] = array(
  1151. 'id' => $row['id_smiley'],
  1152. 'code' => htmlspecialchars($row['code']),
  1153. 'filename' => htmlspecialchars($row['filename']),
  1154. 'description' => htmlspecialchars($row['description']),
  1155. 'row' => $row['smiley_row'],
  1156. 'order' => $row['smiley_order'],
  1157. 'selected' => !empty($_REQUEST['move']) && $_REQUEST['move'] == $row['id_smiley'],
  1158. );
  1159. }
  1160. $smcFunc['db_free_result']($request);
  1161. $context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move'];
  1162. // Make sure all rows are sequential.
  1163. foreach (array_keys($context['smileys']) as $location)
  1164. $context['smileys'][$location] = array(
  1165. 'id' => $location,
  1166. 'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
  1167. 'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],
  1168. 'last_row' => count($context['smileys'][$location]['rows']),
  1169. 'rows' => array_values($context['smileys'][$location]['rows']),
  1170. );
  1171. // Check & fix smileys that are not ordered properly in the database.
  1172. foreach (array_keys($context['smileys']) as $location)
  1173. {
  1174. foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row)
  1175. {
  1176. // Fix empty rows if any.
  1177. if ($id != $smiley_row[0]['row'])
  1178. {
  1179. $smcFunc['db_query']('', '
  1180. UPDATE {db_prefix}smileys
  1181. SET smiley_row = {int:new_row}
  1182. WHERE smiley_row = {int:current_row}
  1183. AND hidden = {int:location}',
  1184. array(
  1185. 'new_row' => $id,
  1186. 'current_row' => $smiley_row[0]['row'],
  1187. 'location' => $location == 'postform' ? '0' : '2',
  1188. )
  1189. );
  1190. // Only change the first row value of the first smiley (we don't need the others :P).
  1191. $context['smileys'][$location]['rows'][$id][0]['row'] = $id;
  1192. }
  1193. // Make sure the smiley order is always sequential.
  1194. foreach ($smiley_row as $order_id => $smiley)
  1195. if ($order_id != $smiley['order'])
  1196. $smcFunc['db_query']('', '
  1197. UPDATE {db_prefix}smileys
  1198. SET smiley_order = {int:new_order}
  1199. WHERE id_smiley = {int:current_smiley}',
  1200. array(
  1201. 'new_order' => $order_id,
  1202. 'current_smiley' => $smiley['id'],
  1203. )
  1204. );
  1205. }
  1206. }
  1207. cache_put_data('parsing_smileys', null, 480);
  1208. cache_put_data('posting_smileys', null, 480);
  1209. }
  1210. function InstallSmileySet()
  1211. {
  1212. global $sourcedir, $boarddir, $modSettings, $smcFunc;
  1213. isAllowedTo('manage_smileys');
  1214. checkSession('request');
  1215. require_once($sourcedir . '/Subs-Package.php');
  1216. $name = strtok(basename(isset($_FILES['set_gz']) ? $_FILES['set_gz']['name'] : $_REQUEST['set_gz']), '.');
  1217. $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
  1218. // !!! Decide: overwrite or not?
  1219. if (isset($_FILES['set_gz']) && is_uploaded_file($_FILES['set_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['set_gz']['tmp_name'])))
  1220. $extracted = read_tgz_file($_FILES['set_gz']['tmp_name'], $boarddir . '/Smileys/' . $name);
  1221. elseif (isset($_REQUEST['set_gz']))
  1222. {
  1223. checkSession('request');
  1224. // Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
  1225. if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['set_gz']) == 0 || strpos($_REQUEST['set_gz'], 'dlattach') !== false)
  1226. fatal_lang_error('not_on_simplemachines');
  1227. $extracted = read_tgz_file($_REQUEST['set_gz'], $boarddir . '/Smileys/' . $name);
  1228. }
  1229. else
  1230. redirectexit('action=admin;area=smileys');
  1231. updateSettings(array(
  1232. 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $name,
  1233. 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . strtok(basename(isset($_FILES['set_gz']) ? $_FILES['set_gz']['name'] : $_REQUEST['set_gz']), '.'),
  1234. ));
  1235. cache_put_data('parsing_smileys', null, 480);
  1236. cache_put_data('posting_smileys', null, 480);
  1237. // !!! Add some confirmation?
  1238. redirectexit('action=admin;area=smileys');
  1239. }
  1240. // A function to import new smileys from an existing directory into the database.
  1241. function ImportSmileys($smileyPath)
  1242. {
  1243. global $modSettings, $smcFunc;
  1244. if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'] . '/' . $smileyPath))
  1245. fatal_lang_error('smiley_set_unable_to_import');
  1246. $smileys = array();
  1247. $dir = dir($modSettings['smileys_dir'] . '/' . $smileyPath);
  1248. while ($entry = $dir->read())
  1249. {
  1250. if (in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  1251. $smileys[strtolower($entry)] = $entry;
  1252. }
  1253. $dir->close();
  1254. // Exclude the smileys that are already in the database.
  1255. $request = $smcFunc['db_query']('', '
  1256. SELECT filename
  1257. FROM {db_prefix}smileys
  1258. WHERE filename IN ({array_string:smiley_list})',
  1259. array(
  1260. 'smiley_list' => $smileys,
  1261. )
  1262. );
  1263. while ($row = $smcFunc['db_fetch_assoc']($request))
  1264. if (isset($smileys[strtolower($row['filename'])]))
  1265. unset($smileys[strtolower($row['filename'])]);
  1266. $smcFunc['db_free_result']($request);
  1267. $request = $smcFunc['db_query']('', '
  1268. SELECT MAX(smiley_order)
  1269. FROM {db_prefix}smileys
  1270. WHERE hidden = {int:postform}
  1271. AND smiley_row = {int:first_row}',
  1272. array(
  1273. 'postform' => 0,
  1274. 'first_row' => 0,
  1275. )
  1276. );
  1277. list ($smiley_order) = $smcFunc['db_fetch_row']($request);
  1278. $smcFunc['db_free_result']($request);
  1279. $new_smileys = array();
  1280. foreach ($smileys as $smiley)
  1281. if (strlen($smiley) <= 48)
  1282. $new_smileys[] = array(':' . strtok($smiley, '.') . ':', $smiley, strtok($smiley, '.'), 0, ++$smiley_order);
  1283. if (!empty($new_smileys))
  1284. {
  1285. $smcFunc['db_insert']('',
  1286. '{db_prefix}smileys',
  1287. array(
  1288. 'code' => 'string-30', 'filename' => 'string-48', 'description' => 'string-80', 'smiley_row' => 'int', 'smiley_order' => 'int',
  1289. ),
  1290. $new_smileys,
  1291. array('id_smiley')
  1292. );
  1293. // Make sure the smiley codes are still in the right order.
  1294. sortSmileyTable();
  1295. cache_put_data('parsing_smileys', null, 480);
  1296. cache_put_data('posting_smileys', null, 480);
  1297. }
  1298. }
  1299. function EditMessageIcons()
  1300. {
  1301. global $user_info, $modSettings, $context, $settings, $txt;
  1302. global $boarddir, $smcFunc, $scripturl, $sourcedir;
  1303. // Get a list of icons.
  1304. $context['icons'] = array();
  1305. $request = $smcFunc['db_query']('', '
  1306. SELECT m.id_icon, m.title, m.filename, m.icon_orde

Large files files are truncated, but you can click here to view the full file