PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/sources/admin/ManageSmileys.php

https://github.com/Arantor/Elkarte
PHP | 2041 lines | 1607 code | 219 blank | 215 comment | 217 complexity | 04ffad7af60bfc283840ad619e5ae06a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0

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

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

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