ManageSmileys.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. <?php
  2. /**
  3. * This file takes care of all administration of smileys.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2012 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * This is the dispatcher of smileys administration.
  18. */
  19. function ManageSmileys()
  20. {
  21. global $context, $txt, $scripturl, $modSettings;
  22. isAllowedTo('manage_smileys');
  23. loadLanguage('ManageSmileys');
  24. loadTemplate('ManageSmileys');
  25. $subActions = array(
  26. 'addsmiley' => 'AddSmiley',
  27. 'editicon' => 'EditMessageIcons',
  28. 'editicons' => 'EditMessageIcons',
  29. 'editsets' => 'EditSmileySets',
  30. 'editsmileys' => 'EditSmileys',
  31. 'import' => 'EditSmileySets',
  32. 'modifyset' => 'EditSmileySets',
  33. 'modifysmiley' => 'EditSmileys',
  34. 'setorder' => 'EditSmileyOrder',
  35. 'settings' => 'EditSmileySettings',
  36. 'install' => 'InstallSmileySet'
  37. );
  38. call_integration_hook('integrate_manage_smileys', array(&$subActions));
  39. // If customized smileys is disabled don't show the setting page
  40. if (empty($modSettings['smiley_enable']))
  41. {
  42. unset($subActions['addsmiley']);
  43. unset($subActions['editsmileys']);
  44. unset($subActions['setorder']);
  45. unset($subActions['modifysmiley']);
  46. }
  47. if (empty($modSettings['messageIcons_enable']))
  48. {
  49. unset($subActions['editicon']);
  50. unset($subActions['editicons']);
  51. }
  52. // Default the sub-action to 'edit smiley settings'.
  53. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'editsets';
  54. $context['page_title'] = $txt['smileys_manage'];
  55. $context['sub_action'] = $_REQUEST['sa'];
  56. $context['sub_template'] = $context['sub_action'];
  57. // Load up all the tabs...
  58. $context[$context['admin_menu_name']]['tab_data'] = array(
  59. 'title' => $txt['smileys_manage'],
  60. 'help' => 'smileys',
  61. 'description' => $txt['smiley_settings_explain'],
  62. 'tabs' => array(
  63. 'editsets' => array(
  64. 'description' => $txt['smiley_editsets_explain'],
  65. ),
  66. 'addsmiley' => array(
  67. 'description' => $txt['smiley_addsmiley_explain'],
  68. ),
  69. 'editsmileys' => array(
  70. 'description' => $txt['smiley_editsmileys_explain'],
  71. ),
  72. 'setorder' => array(
  73. 'description' => $txt['smiley_setorder_explain'],
  74. ),
  75. 'editicons' => array(
  76. 'description' => $txt['icons_edit_icons_explain'],
  77. ),
  78. 'settings' => array(
  79. 'description' => $txt['smiley_settings_explain'],
  80. ),
  81. ),
  82. );
  83. // Some settings may not be enabled, disallow these from the tabs as appropriate.
  84. if (empty($modSettings['messageIcons_enable']))
  85. $context[$context['admin_menu_name']]['tab_data']['tabs']['editicons']['disabled'] = true;
  86. if (empty($modSettings['smiley_enable']))
  87. {
  88. $context[$context['admin_menu_name']]['tab_data']['tabs']['addsmiley']['disabled'] = true;
  89. $context[$context['admin_menu_name']]['tab_data']['tabs']['editsmileys']['disabled'] = true;
  90. $context[$context['admin_menu_name']]['tab_data']['tabs']['setorder']['disabled'] = true;
  91. }
  92. // Call the right function for this sub-acton.
  93. $subActions[$_REQUEST['sa']]();
  94. }
  95. /**
  96. * Allows to modify smileys settings.
  97. *
  98. * @param bool $return_config = false
  99. */
  100. function EditSmileySettings($return_config = false)
  101. {
  102. global $modSettings, $context, $settings, $txt, $boarddir, $sourcedir, $scripturl;
  103. // The directories...
  104. $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
  105. $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
  106. // Get the names of the smiley sets.
  107. $smiley_sets = explode(',', $modSettings['smiley_sets_known']);
  108. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  109. $smiley_context = array();
  110. foreach ($smiley_sets as $i => $set)
  111. $smiley_context[$set] = $set_names[$i];
  112. // All the settings for the page...
  113. $config_vars = array(
  114. array('title', 'settings'),
  115. // Inline permissions.
  116. array('permissions', 'manage_smileys'),
  117. '',
  118. array('select', 'smiley_sets_default', $smiley_context),
  119. array('check', 'smiley_sets_enable'),
  120. array('check', 'smiley_enable', 'subtext' => $txt['smileys_enable_note']),
  121. array('text', 'smileys_url', 40),
  122. array('text', 'smileys_dir', 'invalid' => !$context['smileys_dir_found'], 40),
  123. '',
  124. // Message icons.
  125. array('check', 'messageIcons_enable', 'subtext' => $txt['setting_messageIcons_enable_note']),
  126. );
  127. call_integration_hook('integrate_modify_smiley_settings', array(&$config_vars));
  128. if ($return_config)
  129. return $config_vars;
  130. // Setup the basics of the settings template.
  131. require_once($sourcedir . '/ManageServer.php');
  132. $context['sub_template'] = 'show_settings';
  133. // Finish up the form...
  134. $context['post_url'] = $scripturl . '?action=admin;area=smileys;save;sa=settings';
  135. $context['permissions_excluded'] = array(-1);
  136. // Saving the settings?
  137. if (isset($_GET['save']))
  138. {
  139. checkSession();
  140. // Validate the smiley set name.
  141. $_POST['smiley_sets_default'] = empty($smiley_context[$_POST['smiley_sets_default']]) ? 'default' : $_POST['smiley_sets_default'];
  142. // Make sure that the smileys are in the right order after enabling them.
  143. if (isset($_POST['smiley_enable']))
  144. sortSmileyTable();
  145. call_integration_hook('integrate_save_smiley_settings');
  146. saveDBSettings($config_vars);
  147. cache_put_data('parsing_smileys', null, 480);
  148. cache_put_data('posting_smileys', null, 480);
  149. redirectexit('action=admin;area=smileys;sa=settings');
  150. }
  151. // We need this for the in-line permissions
  152. createToken('admin-mp');
  153. prepareDBSettingContext($config_vars);
  154. }
  155. /**
  156. * List, add, remove, modify smileys sets.
  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['smiley_save']))
  166. {
  167. checkSession();
  168. validateToken('admin-mss', 'request');
  169. // Delete selected smiley sets.
  170. if (!empty($_POST['delete']) && !empty($_POST['smiley_set']))
  171. {
  172. $set_paths = explode(',', $modSettings['smiley_sets_known']);
  173. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  174. foreach ($_POST['smiley_set'] as $id => $val)
  175. {
  176. if (isset($set_paths[$id], $set_names[$id]) && !empty($id))
  177. unset($set_paths[$id], $set_names[$id]);
  178. }
  179. updateSettings(array(
  180. 'smiley_sets_known' => implode(',', $set_paths),
  181. 'smiley_sets_names' => implode("\n", $set_names),
  182. 'smiley_sets_default' => in_array($modSettings['smiley_sets_default'], $set_paths) ? $modSettings['smiley_sets_default'] : $set_paths[0],
  183. ));
  184. }
  185. // Add a new smiley set.
  186. elseif (!empty($_POST['add']))
  187. $context['sub_action'] = 'modifyset';
  188. // Create or modify a smiley set.
  189. elseif (isset($_POST['set']))
  190. {
  191. $set_paths = explode(',', $modSettings['smiley_sets_known']);
  192. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  193. // Create a new smiley set.
  194. if ($_POST['set'] == -1 && isset($_POST['smiley_sets_path']))
  195. {
  196. if (in_array($_POST['smiley_sets_path'], $set_paths))
  197. fatal_lang_error('smiley_set_already_exists');
  198. updateSettings(array(
  199. 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $_POST['smiley_sets_path'],
  200. 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $_POST['smiley_sets_name'],
  201. 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'],
  202. ));
  203. }
  204. // Modify an existing smiley set.
  205. else
  206. {
  207. // Make sure the smiley set exists.
  208. if (!isset($set_paths[$_POST['set']]) || !isset($set_names[$_POST['set']]))
  209. fatal_lang_error('smiley_set_not_found');
  210. // Make sure the path is not yet used by another smileyset.
  211. if (in_array($_POST['smiley_sets_path'], $set_paths) && $_POST['smiley_sets_path'] != $set_paths[$_POST['set']])
  212. fatal_lang_error('smiley_set_path_already_used');
  213. $set_paths[$_POST['set']] = $_POST['smiley_sets_path'];
  214. $set_names[$_POST['set']] = $_POST['smiley_sets_name'];
  215. updateSettings(array(
  216. 'smiley_sets_known' => implode(',', $set_paths),
  217. 'smiley_sets_names' => implode("\n", $set_names),
  218. 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path']
  219. ));
  220. }
  221. // The user might have checked to also import smileys.
  222. if (!empty($_POST['smiley_sets_import']))
  223. ImportSmileys($_POST['smiley_sets_path']);
  224. }
  225. cache_put_data('parsing_smileys', null, 480);
  226. cache_put_data('posting_smileys', null, 480);
  227. }
  228. // Load all available smileysets...
  229. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  230. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  231. foreach ($context['smiley_sets'] as $i => $set)
  232. $context['smiley_sets'][$i] = array(
  233. 'id' => $i,
  234. 'path' => htmlspecialchars($set),
  235. 'name' => htmlspecialchars($set_names[$i]),
  236. 'selected' => $set == $modSettings['smiley_sets_default']
  237. );
  238. // Importing any smileys from an existing set?
  239. if ($context['sub_action'] == 'import')
  240. {
  241. checkSession('get');
  242. validateToken('admin-mss', 'request');
  243. $_GET['set'] = (int) $_GET['set'];
  244. // Sanity check - then import.
  245. if (isset($context['smiley_sets'][$_GET['set']]))
  246. ImportSmileys(un_htmlspecialchars($context['smiley_sets'][$_GET['set']]['path']));
  247. // Force the process to continue.
  248. $context['sub_action'] = 'modifyset';
  249. $context['sub_template'] = 'modifyset';
  250. }
  251. // If we're modifying or adding a smileyset, some context info needs to be set.
  252. if ($context['sub_action'] == 'modifyset')
  253. {
  254. $_GET['set'] = !isset($_GET['set']) ? -1 : (int) $_GET['set'];
  255. if ($_GET['set'] == -1 || !isset($context['smiley_sets'][$_GET['set']]))
  256. $context['current_set'] = array(
  257. 'id' => '-1',
  258. 'path' => '',
  259. 'name' => '',
  260. 'selected' => false,
  261. 'is_new' => true,
  262. );
  263. else
  264. {
  265. $context['current_set'] = &$context['smiley_sets'][$_GET['set']];
  266. $context['current_set']['is_new'] = false;
  267. // Calculate whether there are any smileys in the directory that can be imported.
  268. if (!empty($modSettings['smiley_enable']) && !empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']))
  269. {
  270. $smileys = array();
  271. $dir = dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']);
  272. while ($entry = $dir->read())
  273. {
  274. if (in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  275. $smileys[strtolower($entry)] = $entry;
  276. }
  277. $dir->close();
  278. if (empty($smileys))
  279. fatal_lang_error('smiley_set_dir_not_found', false, array($context['current_set']['name']));
  280. // Exclude the smileys that are already in the database.
  281. $request = $smcFunc['db_query']('', '
  282. SELECT filename
  283. FROM {db_prefix}smileys
  284. WHERE filename IN ({array_string:smiley_list})',
  285. array(
  286. 'smiley_list' => $smileys,
  287. )
  288. );
  289. while ($row = $smcFunc['db_fetch_assoc']($request))
  290. if (isset($smileys[strtolower($row['filename'])]))
  291. unset($smileys[strtolower($row['filename'])]);
  292. $smcFunc['db_free_result']($request);
  293. $context['current_set']['can_import'] = count($smileys);
  294. // Setup this string to look nice.
  295. $txt['smiley_set_import_multiple'] = sprintf($txt['smiley_set_import_multiple'], $context['current_set']['can_import']);
  296. }
  297. }
  298. // Retrieve all potential smiley set directories.
  299. $context['smiley_set_dirs'] = array();
  300. if (!empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir']))
  301. {
  302. $dir = dir($modSettings['smileys_dir']);
  303. while ($entry = $dir->read())
  304. {
  305. if (!in_array($entry, array('.', '..')) && is_dir($modSettings['smileys_dir'] . '/' . $entry))
  306. $context['smiley_set_dirs'][] = array(
  307. 'id' => $entry,
  308. 'path' => $modSettings['smileys_dir'] . '/' . $entry,
  309. 'selectable' => $entry == $context['current_set']['path'] || !in_array($entry, explode(',', $modSettings['smiley_sets_known'])),
  310. 'current' => $entry == $context['current_set']['path'],
  311. );
  312. }
  313. $dir->close();
  314. }
  315. }
  316. // This is our save haven.
  317. createToken('admin-mss', 'request');
  318. $listOptions = array(
  319. 'id' => 'smiley_set_list',
  320. 'title' => $txt['smiley_sets'],
  321. 'no_items_label' => $txt['smiley_sets_none'],
  322. 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
  323. 'default_sort_col' => 'default',
  324. 'get_items' => array(
  325. 'function' => 'list_getSmileySets',
  326. ),
  327. 'get_count' => array(
  328. 'function' => 'list_getNumSmileySets',
  329. ),
  330. 'columns' => array(
  331. 'default' => array(
  332. 'header' => array(
  333. 'value' => $txt['smiley_sets_default'],
  334. ),
  335. 'data' => array(
  336. 'function' => create_function('$rowData', '
  337. global $settings;
  338. return $rowData[\'selected\'] ? \'<img src="\' . $settings[\'images_url\'] . \'/icons/field_valid.png" alt="*" class="icon" />\' : \'\';
  339. '),
  340. 'style' => 'text-align: center;',
  341. ),
  342. 'sort' => array(
  343. 'default' => 'selected DESC',
  344. ),
  345. ),
  346. 'name' => array(
  347. 'header' => array(
  348. 'value' => $txt['smiley_sets_name'],
  349. ),
  350. 'data' => array(
  351. 'db_htmlsafe' => 'name',
  352. ),
  353. 'sort' => array(
  354. 'default' => 'name',
  355. 'reverse' => 'name DESC',
  356. ),
  357. ),
  358. 'url' => array(
  359. 'header' => array(
  360. 'value' => $txt['smiley_sets_url'],
  361. ),
  362. 'data' => array(
  363. 'sprintf' => array(
  364. 'format' => $modSettings['smileys_url'] . '/<strong>%1$s</strong>/...',
  365. 'params' => array(
  366. 'path' => true,
  367. ),
  368. ),
  369. ),
  370. 'sort' => array(
  371. 'default' => 'path',
  372. 'reverse' => 'path DESC',
  373. ),
  374. ),
  375. 'modify' => array(
  376. 'header' => array(
  377. 'value' => $txt['smiley_set_modify'],
  378. ),
  379. 'data' => array(
  380. 'sprintf' => array(
  381. 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset;set=%1$d">' . $txt['smiley_set_modify'] . '</a>',
  382. 'params' => array(
  383. 'id' => true,
  384. ),
  385. ),
  386. 'style' => 'text-align: center;',
  387. ),
  388. ),
  389. 'check' => array(
  390. 'header' => array(
  391. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  392. ),
  393. 'data' => array(
  394. 'function' => create_function('$rowData', '
  395. return $rowData[\'id\'] == 0 ? \'\' : sprintf(\'<input type="checkbox" name="smiley_set[%1$d]" class="input_check" />\', $rowData[\'id\']);
  396. '),
  397. 'style' => 'text-align: center',
  398. ),
  399. ),
  400. ),
  401. 'form' => array(
  402. 'href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
  403. 'token' => 'admin-mss',
  404. ),
  405. 'additional_rows' => array(
  406. array(
  407. 'position' => 'below_table_data',
  408. '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> ',
  409. ),
  410. ),
  411. );
  412. require_once($sourcedir . '/Subs-List.php');
  413. createList($listOptions);
  414. }
  415. /**
  416. * Callback function for createList().
  417. * @todo to be moved to Subs-Smileys?
  418. *
  419. * @param $start
  420. * @param $items_per_page
  421. * @param $sort
  422. */
  423. function list_getSmileySets($start, $items_per_page, $sort)
  424. {
  425. global $modSettings;
  426. $known_sets = explode(',', $modSettings['smiley_sets_known']);
  427. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  428. $cols = array(
  429. 'id' => array(),
  430. 'selected' => array(),
  431. 'path' => array(),
  432. 'name' => array(),
  433. );
  434. foreach ($known_sets as $i => $set)
  435. {
  436. $cols['id'][] = $i;
  437. $cols['selected'][] = $i;
  438. $cols['path'][] = $set;
  439. $cols['name'][] = $set_names[$i];
  440. }
  441. $sort_flag = strpos($sort, 'DESC') === false ? SORT_ASC : SORT_DESC;
  442. if (substr($sort, 0, 4) === 'name')
  443. array_multisort($cols['name'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['selected'], $cols['id']);
  444. elseif (substr($sort, 0, 4) === 'path')
  445. array_multisort($cols['path'], $sort_flag, SORT_REGULAR, $cols['name'], $cols['selected'], $cols['id']);
  446. else
  447. array_multisort($cols['selected'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['name'], $cols['id']);
  448. $smiley_sets = array();
  449. foreach ($cols['id'] as $i => $id)
  450. $smiley_sets[] = array(
  451. 'id' => $id,
  452. 'path' => $cols['path'][$i],
  453. 'name' => $cols['name'][$i],
  454. 'selected' => $cols['path'][$i] == $modSettings['smiley_sets_default']
  455. );
  456. return $smiley_sets;
  457. }
  458. /**
  459. * Callback function for createList().
  460. * @todo to be moved to Subs-Smileys?
  461. */
  462. function list_getNumSmileySets()
  463. {
  464. global $modSettings;
  465. return count(explode(',', $modSettings['smiley_sets_known']));
  466. }
  467. /**
  468. * Add a smiley, that's right.
  469. */
  470. function AddSmiley()
  471. {
  472. global $modSettings, $context, $settings, $txt, $boarddir, $smcFunc;
  473. // Get a list of all known smiley sets.
  474. $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
  475. $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
  476. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  477. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  478. foreach ($context['smiley_sets'] as $i => $set)
  479. $context['smiley_sets'][$i] = array(
  480. 'id' => $i,
  481. 'path' => htmlspecialchars($set),
  482. 'name' => htmlspecialchars($set_names[$i]),
  483. 'selected' => $set == $modSettings['smiley_sets_default']
  484. );
  485. // Submitting a form?
  486. if (isset($_POST[$context['session_var']], $_POST['smiley_code']))
  487. {
  488. checkSession();
  489. // Some useful arrays... types we allow - and ports we don't!
  490. $allowedTypes = array('jpeg', 'jpg', 'gif', 'png', 'bmp');
  491. $disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php');
  492. $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
  493. $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
  494. $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
  495. // Make sure some code was entered.
  496. if (empty($_POST['smiley_code']))
  497. fatal_lang_error('smiley_has_no_code');
  498. // Check whether the new code has duplicates. It should be unique.
  499. $request = $smcFunc['db_query']('', '
  500. SELECT id_smiley
  501. FROM {db_prefix}smileys
  502. WHERE code = {raw:mysql_binary_statement} {string:smiley_code}',
  503. array(
  504. 'mysql_binary_statement' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
  505. 'smiley_code' => $_POST['smiley_code'],
  506. )
  507. );
  508. if ($smcFunc['db_num_rows']($request) > 0)
  509. fatal_lang_error('smiley_not_unique');
  510. $smcFunc['db_free_result']($request);
  511. // If we are uploading - check all the smiley sets are writable!
  512. if ($_POST['method'] != 'existing')
  513. {
  514. $writeErrors = array();
  515. foreach ($context['smiley_sets'] as $set)
  516. {
  517. if (!is_writable($context['smileys_dir'] . '/' . un_htmlspecialchars($set['path'])))
  518. $writeErrors[] = $set['path'];
  519. }
  520. if (!empty($writeErrors))
  521. fatal_lang_error('smileys_upload_error_notwritable', true, array(implode(', ', $writeErrors)));
  522. }
  523. // Uploading just one smiley for all of them?
  524. if (isset($_POST['sameall']) && isset($_FILES['uploadSmiley']['name']) && $_FILES['uploadSmiley']['name'] != '')
  525. {
  526. if (!is_uploaded_file($_FILES['uploadSmiley']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['uploadSmiley']['tmp_name'])))
  527. fatal_lang_error('smileys_upload_error');
  528. // Sorry, no spaces, dots, or anything else but letters allowed.
  529. $_FILES['uploadSmiley']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['uploadSmiley']['name']);
  530. // We only allow image files - it's THAT simple - no messing around here...
  531. if (!in_array(strtolower(substr(strrchr($_FILES['uploadSmiley']['name'], '.'), 1)), $allowedTypes))
  532. fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
  533. // We only need the filename...
  534. $destName = basename($_FILES['uploadSmiley']['name']);
  535. // Make sure they aren't trying to upload a nasty file - for their own good here!
  536. if (in_array(strtolower($destName), $disabledFiles))
  537. fatal_lang_error('smileys_upload_error_illegal');
  538. // Check if the file already exists... and if not move it to EVERY smiley set directory.
  539. $i = 0;
  540. // Keep going until we find a set the file doesn't exist in. (or maybe it exists in all of them?)
  541. while (isset($context['smiley_sets'][$i]) && file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($context['smiley_sets'][$i]['path']) . '/' . $destName))
  542. $i++;
  543. // Okay, we're going to put the smiley right here, since it's not there yet!
  544. if (isset($context['smiley_sets'][$i]['path']))
  545. {
  546. $smileyLocation = $context['smileys_dir'] . '/' . un_htmlspecialchars($context['smiley_sets'][$i]['path']) . '/' . $destName;
  547. move_uploaded_file($_FILES['uploadSmiley']['tmp_name'], $smileyLocation);
  548. @chmod($smileyLocation, 0644);
  549. // Now, we want to move it from there to all the other sets.
  550. for ($n = count($context['smiley_sets']); $i < $n; $i++)
  551. {
  552. $currentPath = $context['smileys_dir'] . '/' . un_htmlspecialchars($context['smiley_sets'][$i]['path']) . '/' . $destName;
  553. // The file is already there! Don't overwrite it!
  554. if (file_exists($currentPath))
  555. continue;
  556. // Okay, so copy the first one we made to here.
  557. copy($smileyLocation, $currentPath);
  558. @chmod($currentPath, 0644);
  559. }
  560. }
  561. // Finally make sure it's saved correctly!
  562. $_POST['smiley_filename'] = $destName;
  563. }
  564. // What about uploading several files?
  565. elseif ($_POST['method'] != 'existing')
  566. {
  567. $newName = '';
  568. foreach ($_FILES as $name => $data)
  569. {
  570. if ($_FILES[$name]['name'] == '')
  571. fatal_lang_error('smileys_upload_error_blank');
  572. if (empty($newName))
  573. $newName = basename($_FILES[$name]['name']);
  574. elseif (basename($_FILES[$name]['name']) != $newName)
  575. fatal_lang_error('smileys_upload_error_name');
  576. }
  577. foreach ($context['smiley_sets'] as $i => $set)
  578. {
  579. $set['name'] = un_htmlspecialchars($set['name']);
  580. $set['path'] = un_htmlspecialchars($set['path']);
  581. if (!isset($_FILES['individual_' . $set['name']]['name']) || $_FILES['individual_' . $set['name']]['name'] == '')
  582. continue;
  583. // Got one...
  584. if (!is_uploaded_file($_FILES['individual_' . $set['name']]['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['individual_' . $set['name']]['tmp_name'])))
  585. fatal_lang_error('smileys_upload_error');
  586. // Sorry, no spaces, dots, or anything else but letters allowed.
  587. $_FILES['individual_' . $set['name']]['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['individual_' . $set['name']]['name']);
  588. // We only allow image files - it's THAT simple - no messing around here...
  589. if (!in_array(strtolower(substr(strrchr($_FILES['individual_' . $set['name']]['name'], '.'), 1)), $allowedTypes))
  590. fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
  591. // We only need the filename...
  592. $destName = basename($_FILES['individual_' . $set['name']]['name']);
  593. // Make sure they aren't trying to upload a nasty file - for their own good here!
  594. if (in_array(strtolower($destName), $disabledFiles))
  595. fatal_lang_error('smileys_upload_error_illegal');
  596. // If the file exists - ignore it.
  597. $smileyLocation = $context['smileys_dir'] . '/' . $set['path'] . '/' . $destName;
  598. if (file_exists($smileyLocation))
  599. continue;
  600. // Finally - move the image!
  601. move_uploaded_file($_FILES['individual_' . $set['name']]['tmp_name'], $smileyLocation);
  602. @chmod($smileyLocation, 0644);
  603. // Should always be saved correctly!
  604. $_POST['smiley_filename'] = $destName;
  605. }
  606. }
  607. // Also make sure a filename was given.
  608. if (empty($_POST['smiley_filename']))
  609. fatal_lang_error('smiley_has_no_filename');
  610. // Find the position on the right.
  611. $smiley_order = '0';
  612. if ($_POST['smiley_location'] != 1)
  613. {
  614. $request = $smcFunc['db_query']('', '
  615. SELECT MAX(smiley_order) + 1
  616. FROM {db_prefix}smileys
  617. WHERE hidden = {int:smiley_location}
  618. AND smiley_row = {int:first_row}',
  619. array(
  620. 'smiley_location' => $_POST['smiley_location'],
  621. 'first_row' => 0,
  622. )
  623. );
  624. list ($smiley_order) = $smcFunc['db_fetch_row']($request);
  625. $smcFunc['db_free_result']($request);
  626. if (empty($smiley_order))
  627. $smiley_order = '0';
  628. }
  629. $smcFunc['db_insert']('',
  630. '{db_prefix}smileys',
  631. array(
  632. 'code' => 'string-30', 'filename' => 'string-48', 'description' => 'string-80', 'hidden' => 'int', 'smiley_order' => 'int',
  633. ),
  634. array(
  635. $_POST['smiley_code'], $_POST['smiley_filename'], $_POST['smiley_description'], $_POST['smiley_location'], $smiley_order,
  636. ),
  637. array('id_smiley')
  638. );
  639. cache_put_data('parsing_smileys', null, 480);
  640. cache_put_data('posting_smileys', null, 480);
  641. // No errors? Out of here!
  642. redirectexit('action=admin;area=smileys;sa=editsmileys');
  643. }
  644. $context['selected_set'] = $modSettings['smiley_sets_default'];
  645. // Get all possible filenames for the smileys.
  646. $context['filenames'] = array();
  647. if ($context['smileys_dir_found'])
  648. {
  649. foreach ($context['smiley_sets'] as $smiley_set)
  650. {
  651. if (!file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path'])))
  652. continue;
  653. $dir = dir($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path']));
  654. while ($entry = $dir->read())
  655. {
  656. if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  657. $context['filenames'][strtolower($entry)] = array(
  658. 'id' => htmlspecialchars($entry),
  659. 'selected' => false,
  660. );
  661. }
  662. $dir->close();
  663. }
  664. ksort($context['filenames']);
  665. }
  666. // Create a new smiley from scratch.
  667. $context['filenames'] = array_values($context['filenames']);
  668. $context['current_smiley'] = array(
  669. 'id' => 0,
  670. 'code' => '',
  671. 'filename' => $context['filenames'][0]['id'],
  672. 'description' => $txt['smileys_default_description'],
  673. 'location' => 0,
  674. 'is_new' => true,
  675. );
  676. }
  677. /**
  678. * Add, remove, edit smileys.
  679. */
  680. function EditSmileys()
  681. {
  682. global $modSettings, $context, $settings, $txt, $boarddir;
  683. global $smcFunc, $scripturl, $sourcedir;
  684. // Force the correct tab to be displayed.
  685. $context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys';
  686. // Submitting a form?
  687. if (isset($_POST['smiley_save']) || isset($_POST['smiley_action']))
  688. {
  689. checkSession();
  690. // Changing the selected smileys?
  691. if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys']))
  692. {
  693. foreach ($_POST['checked_smileys'] as $id => $smiley_id)
  694. $_POST['checked_smileys'][$id] = (int) $smiley_id;
  695. if ($_POST['smiley_action'] == 'delete')
  696. $smcFunc['db_query']('', '
  697. DELETE FROM {db_prefix}smileys
  698. WHERE id_smiley IN ({array_int:checked_smileys})',
  699. array(
  700. 'checked_smileys' => $_POST['checked_smileys'],
  701. )
  702. );
  703. // Changing the status of the smiley?
  704. else
  705. {
  706. // Check it's a valid type.
  707. $displayTypes = array(
  708. 'post' => 0,
  709. 'hidden' => 1,
  710. 'popup' => 2
  711. );
  712. if (isset($displayTypes[$_POST['smiley_action']]))
  713. $smcFunc['db_query']('', '
  714. UPDATE {db_prefix}smileys
  715. SET hidden = {int:display_type}
  716. WHERE id_smiley IN ({array_int:checked_smileys})',
  717. array(
  718. 'checked_smileys' => $_POST['checked_smileys'],
  719. 'display_type' => $displayTypes[$_POST['smiley_action']],
  720. )
  721. );
  722. }
  723. }
  724. // Create/modify a smiley.
  725. elseif (isset($_POST['smiley']))
  726. {
  727. // Is it a delete?
  728. if (!empty($_POST['deletesmiley']))
  729. {
  730. $smcFunc['db_query']('', '
  731. DELETE FROM {db_prefix}smileys
  732. WHERE id_smiley = {int:current_smiley}',
  733. array(
  734. 'current_smiley' => $_POST['smiley'],
  735. )
  736. );
  737. }
  738. // Otherwise an edit.
  739. else
  740. {
  741. $_POST['smiley'] = (int) $_POST['smiley'];
  742. $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
  743. $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
  744. $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
  745. // Make sure some code was entered.
  746. if (empty($_POST['smiley_code']))
  747. fatal_lang_error('smiley_has_no_code');
  748. // Also make sure a filename was given.
  749. if (empty($_POST['smiley_filename']))
  750. fatal_lang_error('smiley_has_no_filename');
  751. // Check whether the new code has duplicates. It should be unique.
  752. $request = $smcFunc['db_query']('', '
  753. SELECT id_smiley
  754. FROM {db_prefix}smileys
  755. WHERE code = {raw:mysql_binary_type} {string:smiley_code}' . (empty($_POST['smiley']) ? '' : '
  756. AND id_smiley != {int:current_smiley}'),
  757. array(
  758. 'current_smiley' => $_POST['smiley'],
  759. 'mysql_binary_type' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
  760. 'smiley_code' => $_POST['smiley_code'],
  761. )
  762. );
  763. if ($smcFunc['db_num_rows']($request) > 0)
  764. fatal_lang_error('smiley_not_unique');
  765. $smcFunc['db_free_result']($request);
  766. $smcFunc['db_query']('', '
  767. UPDATE {db_prefix}smileys
  768. SET
  769. code = {string:smiley_code},
  770. filename = {string:smiley_filename},
  771. description = {string:smiley_description},
  772. hidden = {int:smiley_location}
  773. WHERE id_smiley = {int:current_smiley}',
  774. array(
  775. 'smiley_location' => $_POST['smiley_location'],
  776. 'current_smiley' => $_POST['smiley'],
  777. 'smiley_code' => $_POST['smiley_code'],
  778. 'smiley_filename' => $_POST['smiley_filename'],
  779. 'smiley_description' => $_POST['smiley_description'],
  780. )
  781. );
  782. }
  783. // Sort all smiley codes for more accurate parsing (longest code first).
  784. sortSmileyTable();
  785. }
  786. cache_put_data('parsing_smileys', null, 480);
  787. cache_put_data('posting_smileys', null, 480);
  788. }
  789. // Load all known smiley sets.
  790. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  791. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  792. foreach ($context['smiley_sets'] as $i => $set)
  793. $context['smiley_sets'][$i] = array(
  794. 'id' => $i,
  795. 'path' => htmlspecialchars($set),
  796. 'name' => htmlspecialchars($set_names[$i]),
  797. 'selected' => $set == $modSettings['smiley_sets_default']
  798. );
  799. // Prepare overview of all (custom) smileys.
  800. if ($context['sub_action'] == 'editsmileys')
  801. {
  802. // Determine the language specific sort order of smiley locations.
  803. $smiley_locations = array(
  804. $txt['smileys_location_form'],
  805. $txt['smileys_location_hidden'],
  806. $txt['smileys_location_popup'],
  807. );
  808. asort($smiley_locations);
  809. // Create a list of options for selecting smiley sets.
  810. $smileyset_option_list = '
  811. <select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">';
  812. foreach ($context['smiley_sets'] as $smiley_set)
  813. $smileyset_option_list .= '
  814. <option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected="selected"' : '') . '>' . $smiley_set['name'] . '</option>';
  815. $smileyset_option_list .= '
  816. </select>';
  817. $listOptions = array(
  818. 'id' => 'smiley_list',
  819. 'title' => $txt['smileys_edit'],
  820. 'items_per_page' => 40,
  821. 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
  822. 'default_sort_col' => 'filename',
  823. 'get_items' => array(
  824. 'function' => 'list_getSmileys',
  825. ),
  826. 'get_count' => array(
  827. 'function' => 'list_getNumSmileys',
  828. ),
  829. 'no_items_label' => $txt['smileys_no_entries'],
  830. 'columns' => array(
  831. 'picture' => array(
  832. 'data' => array(
  833. 'sprintf' => array(
  834. '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>',
  835. 'params' => array(
  836. 'id_smiley' => false,
  837. 'filename' => true,
  838. 'description' => true,
  839. ),
  840. ),
  841. 'style' => 'text-align: center;',
  842. ),
  843. ),
  844. 'code' => array(
  845. 'header' => array(
  846. 'value' => $txt['smileys_code'],
  847. ),
  848. 'data' => array(
  849. 'db_htmlsafe' => 'code',
  850. ),
  851. 'sort' => array(
  852. 'default' => 'code',
  853. 'reverse' => 'code DESC',
  854. ),
  855. ),
  856. 'filename' => array(
  857. 'header' => array(
  858. 'value' => $txt['smileys_filename'],
  859. ),
  860. 'data' => array(
  861. 'db_htmlsafe' => 'filename',
  862. ),
  863. 'sort' => array(
  864. 'default' => 'filename',
  865. 'reverse' => 'filename DESC',
  866. ),
  867. ),
  868. 'location' => array(
  869. 'header' => array(
  870. 'value' => $txt['smileys_location'],
  871. ),
  872. 'data' => array(
  873. 'function' => create_function('$rowData', '
  874. global $txt;
  875. if (empty($rowData[\'hidden\']))
  876. return $txt[\'smileys_location_form\'];
  877. elseif ($rowData[\'hidden\'] == 1)
  878. return $txt[\'smileys_location_hidden\'];
  879. else
  880. return $txt[\'smileys_location_popup\'];
  881. '),
  882. ),
  883. 'sort' => array(
  884. 'default' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\')',
  885. 'reverse' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\') DESC',
  886. ),
  887. ),
  888. 'tooltip' => array(
  889. 'header' => array(
  890. 'value' => $txt['smileys_description'],
  891. ),
  892. 'data' => array(
  893. 'function' => create_function('$rowData', empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir']) ? '
  894. return htmlspecialchars($rowData[\'description\']);
  895. ' : '
  896. global $context, $txt, $modSettings;
  897. // Check if there are smileys missing in some sets.
  898. $missing_sets = array();
  899. foreach ($context[\'smiley_sets\'] as $smiley_set)
  900. if (!file_exists(sprintf(\'%1$s/%2$s/%3$s\', $modSettings[\'smileys_dir\'], $smiley_set[\'path\'], $rowData[\'filename\'])))
  901. $missing_sets[] = $smiley_set[\'path\'];
  902. $description = htmlspecialchars($rowData[\'description\']);
  903. if (!empty($missing_sets))
  904. $description .= sprintf(\'<br /><span class="smalltext"><strong>%1$s:</strong> %2$s</span>\', $txt[\'smileys_not_found_in_set\'], implode(\', \', $missing_sets));
  905. return $description;
  906. '),
  907. ),
  908. 'sort' => array(
  909. 'default' => 'description',
  910. 'reverse' => 'description DESC',
  911. ),
  912. ),
  913. 'modify' => array(
  914. 'header' => array(
  915. 'value' => $txt['smileys_modify'],
  916. ),
  917. 'data' => array(
  918. 'sprintf' => array(
  919. 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>',
  920. 'params' => array(
  921. 'id_smiley' => false,
  922. ),
  923. ),
  924. 'style' => 'text-align: center;',
  925. ),
  926. ),
  927. 'check' => array(
  928. 'header' => array(
  929. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  930. ),
  931. 'data' => array(
  932. 'sprintf' => array(
  933. 'format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d" class="input_check" />',
  934. 'params' => array(
  935. 'id_smiley' => false,
  936. ),
  937. ),
  938. 'style' => 'text-align: center',
  939. ),
  940. ),
  941. ),
  942. 'form' => array(
  943. 'href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
  944. 'name' => 'smileyForm',
  945. ),
  946. 'additional_rows' => array(
  947. array(
  948. 'position' => 'above_column_headers',
  949. 'value' => $smileyset_option_list,
  950. 'style' => 'text-align: right;',
  951. ),
  952. array(
  953. 'position' => 'below_table_data',
  954. 'value' => '
  955. <select name="smiley_action" onchange="makeChanges(this.value);">
  956. <option value="-1">' . $txt['smileys_with_selected'] . ':</option>
  957. <option value="-1">--------------</option>
  958. <option value="hidden">' . $txt['smileys_make_hidden'] . '</option>
  959. <option value="post">' . $txt['smileys_show_on_post'] . '</option>
  960. <option value="popup">' . $txt['smileys_show_on_popup'] . '</option>
  961. <option value="delete">' . $txt['smileys_remove'] . '</option>
  962. </select>
  963. <noscript><input type="submit" name="perform_action" value="' . $txt['go'] . '" class="button_submit" /></noscript>',
  964. 'style' => 'text-align: right;',
  965. ),
  966. ),
  967. 'javascript' => '
  968. function makeChanges(action)
  969. {
  970. if (action == \'-1\')
  971. return false;
  972. else if (action == \'delete\')
  973. {
  974. if (confirm(\'' . $txt['smileys_confirm'] . '\'))
  975. document.forms.smileyForm.submit();
  976. }
  977. else
  978. document.forms.smileyForm.submit();
  979. return true;
  980. }
  981. function changeSet(newSet)
  982. {
  983. var currentImage, i, knownSmileys = [];
  984. if (knownSmileys.length == 0)
  985. {
  986. for (var i = 0, n = document.images.length; i < n; i++)
  987. if (document.images[i].id.substr(0, 6) == \'smiley\')
  988. knownSmileys[knownSmileys.length] = document.images[i].id.substr(6);
  989. }
  990. for (i = 0; i < knownSmileys.length; i++)
  991. {
  992. currentImage = document.getElementById("smiley" + knownSmileys[i]);
  993. currentImage.src = "' . $modSettings['smileys_url'] . '/" + newSet + "/" + document.forms.smileyForm["smileys[" + knownSmileys[i] + "][filename]"].value;
  994. }
  995. }',
  996. );
  997. require_once($sourcedir . '/Subs-List.php');
  998. createList($listOptions);
  999. // The list is the only thing to show, so make it the main template.
  1000. $context['default_list'] = 'smiley_list';
  1001. $context['sub_template'] = 'show_list';
  1002. }
  1003. // Modifying smileys.
  1004. elseif ($context['sub_action'] == 'modifysmiley')
  1005. {
  1006. // Get a list of all known smiley sets.
  1007. $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
  1008. $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
  1009. $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
  1010. $set_names = explode("\n", $modSettings['smiley_sets_names']);
  1011. foreach ($context['smiley_sets'] as $i => $set)
  1012. $context['smiley_sets'][$i] = array(
  1013. 'id' => $i,
  1014. 'path' => htmlspecialchars($set),
  1015. 'name' => htmlspecialchars($set_names[$i]),
  1016. 'selected' => $set == $modSettings['smiley_sets_default']
  1017. );
  1018. $context['selected_set'] = $modSettings['smiley_sets_default'];
  1019. // Get all possible filenames for the smileys.
  1020. $context['filenames'] = array();
  1021. if ($context['smileys_dir_found'])
  1022. {
  1023. foreach ($context['smiley_sets'] as $smiley_set)
  1024. {
  1025. if (!file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path'])))
  1026. continue;
  1027. $dir = dir($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path']));
  1028. while ($entry = $dir->read())
  1029. {
  1030. if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  1031. $context['filenames'][strtolower($entry)] = array(
  1032. 'id' => htmlspecialchars($entry),
  1033. 'selected' => false,
  1034. );
  1035. }
  1036. $dir->close();
  1037. }
  1038. ksort($context['filenames']);
  1039. }
  1040. $request = $smcFunc['db_query']('', '
  1041. SELECT id_smiley AS id, code, filename, description, hidden AS location, 0 AS is_new
  1042. FROM {db_prefix}smileys
  1043. WHERE id_smiley = {int:current_smiley}',
  1044. array(
  1045. 'current_smiley' => (int) $_REQUEST['smiley'],
  1046. )
  1047. );
  1048. if ($smcFunc['db_num_rows']($request) != 1)
  1049. fatal_lang_error('smiley_not_found');
  1050. $context['current_smiley'] = $smcFunc['db_fetch_assoc']($request);
  1051. $smcFunc['db_free_result']($request);
  1052. $context['current_smiley']['code'] = htmlspecialchars($context['current_smiley']['code']);
  1053. $context['current_smiley']['filename'] = htmlspecialchars($context['current_smiley']['filename']);
  1054. $context['current_smiley']['description'] = htmlspecialchars($context['current_smiley']['description']);
  1055. if (isset($context['filenames'][strtolower($context['current_smiley']['filename'])]))
  1056. $context['filenames'][strtolower($context['current_smiley']['filename'])]['selected'] = true;
  1057. }
  1058. }
  1059. /**
  1060. * Callback function for createList().
  1061. *
  1062. * @param unknown_type $start
  1063. * @param unknown_type $items_per_page
  1064. * @param unknown_type $sort
  1065. */
  1066. function list_getSmileys($start, $items_per_page, $sort)
  1067. {
  1068. global $smcFunc;
  1069. $request = $smcFunc['db_query']('', '
  1070. SELECT id_smiley, code, filename, description, smiley_row, smiley_order, hidden
  1071. FROM {db_prefix}smileys
  1072. ORDER BY ' . $sort,
  1073. array(
  1074. )
  1075. );
  1076. $smileys = array();
  1077. while ($row = $smcFunc['db_fetch_assoc']($request))
  1078. $smileys[] = $row;
  1079. $smcFunc['db_free_result']($request);
  1080. return $smileys;
  1081. }
  1082. /**
  1083. * Callback function for createList().
  1084. */
  1085. function list_getNumSmileys()
  1086. {
  1087. global $smcFunc;
  1088. $request = $smcFunc['db_query']('', '
  1089. SELECT COUNT(*)
  1090. FROM {db_prefix}smileys',
  1091. array(
  1092. )
  1093. );
  1094. list($numSmileys) = $smcFunc['db_fetch_row'];
  1095. $smcFunc['db_free_result']($request);
  1096. return $numSmileys;
  1097. }
  1098. /**
  1099. * Allows to edit smileys order.
  1100. */
  1101. function EditSmileyOrder()
  1102. {
  1103. global $modSettings, $context, $settings, $txt, $boarddir, $smcFunc;
  1104. // Move smileys to another position.
  1105. if (isset($_REQUEST['reorder']))
  1106. {
  1107. checkSession('get');
  1108. $_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2;
  1109. $_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source'];
  1110. if (empty($_GET['source']))
  1111. fatal_lang_error('smiley_not_found');
  1112. if (!empty($_GET['after']))
  1113. {
  1114. $_GET['after'] = (int) $_GET['after'];
  1115. $request = $smcFunc['db_query']('', '
  1116. SELECT smiley_row, smiley_order, hidden
  1117. FROM {db_prefix}smileys
  1118. WHERE hidden = {int:location}
  1119. AND id_smiley = {int:after_smiley}',
  1120. array(
  1121. 'location' => $_GET['location'],
  1122. 'after_smiley' => $_GET['after'],
  1123. )
  1124. );
  1125. if ($smcFunc['db_num_rows']($request) != 1)
  1126. fatal_lang_error('smiley_not_found');
  1127. list ($smiley_row, $smiley_order, $smileyLocation) = $smcFunc['db_fetch_row']($request);
  1128. $smcFunc['db_free_result']($request);
  1129. }
  1130. else
  1131. {
  1132. $smiley_row = (int) $_GET['row'];
  1133. $smiley_order = -1;
  1134. $smileyLocation = (int) $_GET['location'];
  1135. }
  1136. $smcFunc['db_query']('', '
  1137. UPDATE {db_prefix}smileys
  1138. SET smiley_order = smiley_order + 1
  1139. WHERE hidden = {int:new_location}
  1140. AND smiley_row = {int:smiley_row}
  1141. AND smiley_order > {int:smiley_order}',
  1142. array(
  1143. 'new_location' => $_GET['location'],
  1144. 'smiley_row' => $smiley_row,
  1145. 'smiley_order' => $smiley_order,
  1146. )
  1147. );
  1148. $smcFunc['db_query']('', '
  1149. UPDATE {db_prefix}smileys
  1150. SET
  1151. smiley_order = {int:smiley_order} + 1,
  1152. smiley_row = {int:smiley_row},
  1153. hidden = {int:new_location}
  1154. WHERE id_smiley = {int:current_smiley}',
  1155. array(
  1156. 'smiley_order' => $smiley_order,
  1157. 'smiley_row' => $smiley_row,
  1158. 'new_location' => $smileyLocation,
  1159. 'current_smiley' => $_GET['source'],
  1160. )
  1161. );
  1162. cache_put_data('parsing_smileys', null, 480);
  1163. cache_put_data('posting_smileys', null, 480);
  1164. }
  1165. $request = $smcFunc['db_query']('', '
  1166. SELECT id_smiley, code, filename, description, smiley_row, smiley_order, hidden
  1167. FROM {db_prefix}smileys
  1168. WHERE hidden != {int:popup}
  1169. ORDER BY smiley_order, smiley_row',
  1170. array(
  1171. 'popup' => 1,
  1172. )
  1173. );
  1174. $context['smileys'] = array(
  1175. 'postform' => array(
  1176. 'rows' => array(),
  1177. ),
  1178. 'popup' => array(
  1179. 'rows' => array(),
  1180. ),
  1181. );
  1182. while ($row = $smcFunc['db_fetch_assoc']($request))
  1183. {
  1184. $location = empty($row['hidden']) ? 'postform' : 'popup';
  1185. $context['smileys'][$location]['rows'][$row['smiley_row']][] = array(
  1186. 'id' => $row['id_smiley'],
  1187. 'code' => htmlspecialchars($row['code']),
  1188. 'filename' => htmlspecialchars($row['filename']),
  1189. 'description' => htmlspecialchars($row['description']),
  1190. 'row' => $row['smiley_row'],
  1191. 'order' => $row['smiley_order'],
  1192. 'selected' => !empty($_REQUEST['move']) && $_REQUEST['move'] == $row['id_smiley'],
  1193. );
  1194. }
  1195. $smcFunc['db_free_result']($request);
  1196. $context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move'];
  1197. // Make sure all rows are sequential.
  1198. foreach (array_keys($context['smileys']) as $location)
  1199. $context['smileys'][$location] = array(
  1200. 'id' => $location,
  1201. 'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
  1202. 'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],
  1203. 'last_row' => count($context['smileys'][$location]['rows']),
  1204. 'rows' => array_values($context['smileys'][$location]['rows']),
  1205. );
  1206. // Check & fix smileys that are not ordered properly in the database.
  1207. foreach (array_keys($context['smileys']) as $location)
  1208. {
  1209. foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row)
  1210. {
  1211. // Fix empty rows if any.
  1212. if ($id != $smiley_row[0]['row'])
  1213. {
  1214. $smcFunc['db_query']('', '
  1215. UPDATE {db_prefix}smileys
  1216. SET smiley_row = {int:new_row}
  1217. WHERE smiley_row = {int:current_row}
  1218. AND hidden = {int:location}',
  1219. array(
  1220. 'new_row' => $id,
  1221. 'current_row' => $smiley_row[0]['row'],
  1222. 'location' => $location == 'postform' ? '0' : '2',
  1223. )
  1224. );
  1225. // Only change the first row value of the first smiley (we don't need the others :P).
  1226. $context['smileys'][$location]['rows'][$id][0]['row'] = $id;
  1227. }
  1228. // Make sure the smiley order is always sequential.
  1229. foreach ($smiley_row as $order_id => $smiley)
  1230. if ($order_id != $smiley['order'])
  1231. $smcFunc['db_query']('', '
  1232. UPDATE {db_prefix}smileys
  1233. SET smiley_order = {int:new_order}
  1234. WHERE id_smiley = {int:current_smiley}',
  1235. array(
  1236. 'new_order' => $order_id,
  1237. 'current_smiley' => $smiley['id'],
  1238. )
  1239. );
  1240. }
  1241. }
  1242. cache_put_data('parsing_smileys', null, 480);
  1243. cache_put_data('posting_smileys', null, 480);
  1244. }
  1245. /**
  1246. * Install a smiley set.
  1247. */
  1248. function InstallSmileySet()
  1249. {
  1250. global $sourcedir, $boarddir, $modSettings, $smcFunc;
  1251. isAllowedTo('manage_smileys');
  1252. checkSession('request');
  1253. require_once($sourcedir . '/Subs-Package.php');
  1254. $name = strtok(basename(isset($_FILES['set_gz']) ? $_FILES['set_gz']['name'] : $_REQUEST['set_gz']), '.');
  1255. $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
  1256. //@todo Decide: overwrite or not?
  1257. if (isset($_FILES['set_gz']) && is_uploaded_file($_FILES['set_gz']['tmp_name']) && (ini_get('open_basedir') != '' || file_exists($_FILES['set_gz']['tmp_name'])))
  1258. $extracted = read_tgz_file($_FILES['set_gz']['tmp_name'], $boarddir . '/Smileys/' . $name);
  1259. elseif (isset($_REQUEST['set_gz']))
  1260. {
  1261. // Check that the smiley is from simplemachines.org, for now... maybe add mirroring later.
  1262. if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['set_gz']) == 0 || strpos($_REQUEST['set_gz'], 'dlattach') !== false)
  1263. fatal_lang_error('not_on_simplemachines');
  1264. $extracted = read_tgz_file($_REQUEST['set_gz'], $boarddir . '/Smileys/' . $name);
  1265. }
  1266. else
  1267. redirectexit('action=admin;area=smileys');
  1268. updateSettings(array(
  1269. 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $name,
  1270. 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . strtok(basename(isset($_FILES['set_gz']) ? $_FILES['set_gz']['name'] : $_REQUEST['set_gz']), '.'),
  1271. ));
  1272. cache_put_data('parsing_smileys', null, 480);
  1273. cache_put_data('posting_smileys', null, 480);
  1274. // @todo Add some confirmation?
  1275. redirectexit('action=admin;area=smileys');
  1276. }
  1277. /**
  1278. * A function to import new smileys from an existing directory into the database.
  1279. *
  1280. * @param string $smileyPath
  1281. */
  1282. function ImportSmileys($smileyPath)
  1283. {
  1284. global $modSettings, $smcFunc;
  1285. if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'] . '/' . $smileyPath))
  1286. fatal_lang_error('smiley_set_unable_to_import');
  1287. $smileys = array();
  1288. $dir = dir($modSettings['smileys_dir'] . '/' . $smileyPath);
  1289. while ($entry = $dir->read())
  1290. {
  1291. if (in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
  1292. $smileys[strtolower($entry)] = $entry;
  1293. }
  1294. $dir->close();
  1295. // Exclude the smileys that are already in the database.
  1296. $request = $smcFunc['db_query']('', '
  1297. SELECT filename
  1298. FROM {db_prefix}smileys
  1299. WHERE filename IN ({array_string:smiley_list})',
  1300. array(
  1301. 'smiley_list' => $smileys,
  1302. )
  1303. );
  1304. while ($row = $smcFunc['db_fetch_assoc']($request))
  1305. if (isset($smileys[strtolower($row['filename'])]))
  1306. unset($smileys[strtolower($row['filename'])]);
  1307. $smcFunc['db_free_result']($request);
  1308. $request = $smcFunc['db_query']('', '
  1309. SELECT MAX(smiley_order)
  1310. FROM {db_prefix}smileys
  1311. WHERE hidden = {int:postform}
  1312. AND smiley_row = {int:first_row}',
  1313. array(
  1314. 'postform' => 0,
  1315. 'first_row' => 0,
  1316. )
  1317. );
  1318. list ($smiley_order) = $smcFunc['db_fetch_row']($request);
  1319. $smcFunc['db_free_result']($request);
  1320. $new_smileys = array();
  1321. foreach ($smileys as $smiley)
  1322. if (strlen($smiley) <= 48)
  1323. $new_smileys[] = array(':' . strtok($smiley, '.') . ':', $smiley, strtok($smiley, '.'), 0, ++$smiley_order);
  1324. if (!empty($new_smileys))
  1325. {
  1326. $smcFunc['db_insert']('',
  1327. '{db_prefix}smileys',
  1328. array(
  1329. 'code' => 'string-30', 'filename' => 'string-48', 'description' => 'string-80', 'smiley_row' => 'int', 'smiley_order' => 'int',
  1330. ),
  1331. $new_smileys,
  1332. array('id_smiley')
  1333. );
  1334. // Make sure the smiley codes are still in the right order.
  1335. sortSmileyTable();
  1336. cache_put_data('parsing_smileys', null, 480);
  1337. cache_put_data('posting_smileys', null, 480);
  1338. }
  1339. }
  1340. /**
  1341. * Allows to edit the message icons.
  1342. */
  1343. function EditMessageIcons()
  1344. {
  1345. global $user_info, $modSettings, $context, $settings, $txt;
  1346. global $boarddir, $smcFunc, $scripturl, $sourcedir;
  1347. // Get a list of icons.
  1348. $context['icons'] = array();
  1349. $request = $smcFunc['db_query']('', '
  1350. SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
  1351. FROM {db_prefix}message_icons AS m
  1352. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  1353. WHERE ({query_see_board} OR b.id_board IS NULL)',
  1354. array(
  1355. )
  1356. );
  1357. $last_icon = 0;
  1358. $trueOrder = 0;
  1359. while ($row = $smcFunc['db_fetch_assoc']($request))
  1360. {
  1361. $context['icons'][$row['id_icon']] = array(
  1362. 'id' => $row['id_icon'],
  1363. 'title' => $row['title'],
  1364. 'filename' => $row['filename'],
  1365. 'image_url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $row['filename'] . '.png') ? 'actual_images_url' : 'default_images_url'] . '/post/' . $row['filename'] . '.png',
  1366. 'board_id' => $row['id_board'],
  1367. 'board' => empty($row['board_name']) ? $txt['icons_edit_icons_all_boards'] : $row['board_name'],
  1368. 'order' => $row['icon_order'],
  1369. 'true_order' => $trueOrder++,
  1370. 'after' => $last_icon,
  1371. );
  1372. $last_icon = $row['id_icon'];
  1373. }
  1374. $smcFunc['db_free_result']($request);
  1375. // Submitting a form?
  1376. if (isset($_POST['icons_save']))
  1377. {
  1378. checkSession();
  1379. // Deleting icons?
  1380. if (isset($_POST['delete']) && !empty($_POST['checked_icons']))
  1381. {
  1382. $deleteIcons = array();
  1383. foreach ($_POST['checked_icons'] as $icon)
  1384. $deleteIcons[] = (int) $icon;
  1385. // Do the actual delete!
  1386. $smcFunc['db_query']('', '
  1387. DELETE FROM {db_prefix}message_icons
  1388. WHERE id_icon IN ({array_int:icon_list})',
  1389. array(
  1390. 'icon_list' => $deleteIcons,
  1391. )
  1392. );
  1393. }
  1394. // Editing/Adding an icon?
  1395. elseif ($context['sub_action'] == 'editicon' && isset($_GET['icon']))
  1396. {
  1397. $_GET['icon'] = (int) $_GET['icon'];
  1398. // Do some preperation with the data... like check the icon exists *somewhere*
  1399. if (strpos($_POST['icon_filename'], '.png') !== false)
  1400. $_POST['icon_filename'] = substr($_POST['icon_filename'], 0, -4);
  1401. if (!file_exists($settings['default_theme_dir'] . '/images/post/' . $_POST['icon_filename'] . '.png'))
  1402. fatal_lang_error('icon_not_found');
  1403. // There is a 16 character limit on message icons...
  1404. elseif (strlen($_POST['icon_filename']) > 16)
  1405. fatal_lang_error('icon_name_too_long');
  1406. elseif ($_POST['icon_location'] == $_GET['icon'] && !empty($_GET['icon']))
  1407. fatal_lang_error('icon_after_itself');
  1408. // First do the sorting... if this is an edit reduce the order of everything after it by one ;)
  1409. if ($_GET['icon'] != 0)
  1410. {
  1411. $oldOrder = $context['icons'][$_GET['icon']]['true_order'];
  1412. foreach ($context['icons'] as $id => $data)
  1413. if ($data['true_order'] > $oldOrder)
  1414. $context['icons'][$id]['true_order']--;
  1415. }
  1416. // If there are no existing icons and this is a new one, set the id to 1 (mainly for non-mysql)
  1417. if (empty($_GET['icon']) && empty($context['icons']))
  1418. $_GET['icon'] = 1;
  1419. // Get the new order.
  1420. $newOrder = $_POST['icon_location'] == 0 ? 0 : $context['icons'][$_POST['icon_location']]['true_order'] + 1;
  1421. // Do the same, but with the one that used to be after this icon, done to avoid conflict.
  1422. foreach ($context['icons'] as $id => $data)
  1423. if ($data['true_order'] >= $newOrder)
  1424. $context['icons'][$id]['true_order']++;
  1425. // Finally set the current icon's position!
  1426. $context['icons'][$_GET['icon']]['true_order'] = $newOrder;
  1427. // Simply replace the existing data for the other bits.
  1428. $context['icons'][$_GET['icon']]['title'] = $_POST['icon_description'];
  1429. $context['icons'][$_GET['icon']]['filename'] = $_POST['icon_filename'];
  1430. $context['icons'][$_GET['icon']]['board_id'] = (int) $_POST['icon_board'];
  1431. // Do a huge replace ;)
  1432. $iconInsert = array();
  1433. $iconInsert_new = array();
  1434. foreach ($context['icons'] as $id => $icon)
  1435. {
  1436. if ($id != 0)
  1437. {
  1438. $iconInsert[] = array($id, $icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
  1439. }
  1440. else
  1441. {
  1442. $iconInsert_new[] = array($icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
  1443. }
  1444. }
  1445. $smcFunc['db_insert']('replace',
  1446. '{db_prefix}message_icons',
  1447. array('id_icon' => 'int', 'id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
  1448. $iconInsert,
  1449. array('id_icon')
  1450. );
  1451. if (!empty($iconInsert_new))
  1452. {
  1453. $smcFunc['db_insert']('replace',
  1454. '{db_prefix}message_icons',
  1455. array('id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
  1456. $iconInsert_new,
  1457. array('id_icon')
  1458. );
  1459. }
  1460. }
  1461. // Sort by order, so it is quicker :)
  1462. $smcFunc['db_query']('alter_table_icons', '
  1463. ALTER TABLE {db_prefix}message_icons
  1464. ORDER BY icon_order',
  1465. array(
  1466. 'db_error_skip' => true,
  1467. )
  1468. );
  1469. // Unless we're adding a new thing, we'll escape
  1470. if (!isset($_POST['add']))
  1471. redirectexit('action=admin;area=smileys;sa=editicons');
  1472. }
  1473. $context[$context['admin_menu_name']]['current_subsection'] = 'editicons';
  1474. $listOptions = array(
  1475. 'id' => 'message_icon_list',
  1476. 'title' => $txt['icons_edit_message_icons'],
  1477. 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
  1478. 'get_items' => array(
  1479. 'function' => 'list_getMessageIcons',
  1480. ),
  1481. 'no_items_label' => $txt['icons_no_entries'],
  1482. 'columns' => array(
  1483. 'icon' => array(
  1484. 'data' => array(
  1485. 'function' => create_function('$rowData', '
  1486. global $settings;
  1487. $images_url = $settings[file_exists(sprintf(\'%1$s/images/post/%2$s.png\', $settings[\'theme_dir\'], $rowData[\'filename\'])) ? \'actual_images_url\' : \'default_images_url\'];
  1488. return sprintf(\'<img src="%1$s/post/%2$s.png" alt="%3$s" />\', $images_url, $rowData[\'filename\'], htmlspecialchars($rowData[\'title\']));
  1489. '),
  1490. ),
  1491. 'style' => 'text-align: center;',
  1492. ),
  1493. 'filename' => array(
  1494. 'header' => array(
  1495. 'value' => $txt['smileys_filename'],
  1496. ),
  1497. 'data' => array(
  1498. 'sprintf' => array(
  1499. 'format' => '%1$s.png',
  1500. 'params' => array(
  1501. 'filename' => true,
  1502. ),
  1503. ),
  1504. ),
  1505. ),
  1506. 'tooltip' => array(
  1507. 'header' => array(
  1508. 'value' => $txt['smileys_description'],
  1509. ),
  1510. 'data' => array(
  1511. 'db_htmlsafe' => 'title',
  1512. ),
  1513. ),
  1514. 'board' => array(
  1515. 'header' => array(
  1516. 'value' => $txt['icons_board'],
  1517. ),
  1518. 'data' => array(
  1519. 'function' => create_function('$rowData', '
  1520. global $txt;
  1521. return empty($rowData[\'board_name\']) ? $txt[\'icons_edit_icons_all_boards\'] : $rowData[\'board_name\'];
  1522. '),
  1523. ),
  1524. ),
  1525. 'modify' => array(
  1526. 'header' => array(
  1527. 'value' => $txt['smileys_modify'],
  1528. ),
  1529. 'data' => array(
  1530. 'sprintf' => array(
  1531. 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon;icon=%1$s">' . $txt['smileys_modify'] . '</a>',
  1532. 'params' => array(
  1533. 'id_icon' => false,
  1534. ),
  1535. ),
  1536. 'style' => 'text-align: center',
  1537. ),
  1538. ),
  1539. 'check' => array(
  1540. 'header' => array(
  1541. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  1542. ),
  1543. 'data' => array(
  1544. 'sprintf' => array(
  1545. 'format' => '<input type="checkbox" name="checked_icons[]" value="%1$d" class="input_check" />',
  1546. 'params' => array(
  1547. 'id_icon' => false,
  1548. ),
  1549. ),
  1550. 'style' => 'text-align: center',
  1551. ),
  1552. ),
  1553. ),
  1554. 'form' => array(
  1555. 'href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
  1556. ),
  1557. 'additional_rows' => array(
  1558. array(
  1559. 'position' => 'below_table_data',
  1560. 'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button_submit" /> <a class="button_link" href="' . $scripturl . '?action=admin;area=smileys;sa=editicon">' . $txt['icons_add_new'] . '</a>',
  1561. ),
  1562. ),
  1563. );
  1564. require_once($sourcedir . '/Subs-List.php');
  1565. createList($listOptions);
  1566. // If we're adding/editing an icon we'll need a list of boards
  1567. if ($context['sub_action'] == 'editicon' || isset($_POST['add']))
  1568. {
  1569. // Force the sub_template just in case.
  1570. $context['sub_template'] = 'editicon';
  1571. $context['new_icon'] = !isset($_GET['icon']);
  1572. // Get the properties of the current icon from the icon list.
  1573. if (!$context['new_icon'])
  1574. $context['icon'] = $context['icons'][$_GET['icon']];
  1575. // Get a list of boards needed for assigning this icon to a specific board.
  1576. $boardListOptions = array(
  1577. 'use_permissions' => true,
  1578. 'selected_board' => isset($context['icon']['board_id']) ? $context['icon']['board_id'] : 0,
  1579. );
  1580. require_once($sourcedir . '/Subs-MessageIndex.php');
  1581. $context['categories'] = getBoardList($boardListOptions);
  1582. }
  1583. }
  1584. /**
  1585. * Callback function for createList().
  1586. *
  1587. * @param $start
  1588. * @param $items_per_page
  1589. * @param $sort
  1590. */
  1591. function list_getMessageIcons($start, $items_per_page, $sort)
  1592. {
  1593. global $smcFunc, $user_info;
  1594. $request = $smcFunc['db_query']('', '
  1595. SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
  1596. FROM {db_prefix}message_icons AS m
  1597. LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  1598. WHERE ({query_see_board} OR b.id_board IS NULL)',
  1599. array(
  1600. )
  1601. );
  1602. $message_icons = array();
  1603. while ($row = $smcFunc['db_fetch_assoc']($request))
  1604. $message_icons[] = $row;
  1605. $smcFunc['db_free_result']($request);
  1606. return $message_icons;
  1607. }
  1608. /**
  1609. * This function sorts the smiley table by code length,
  1610. * it is needed as MySQL withdrew support for functions in order by.
  1611. * @todo is this ordering itself needed?
  1612. */
  1613. function sortSmileyTable()
  1614. {
  1615. global $smcFunc;
  1616. db_extend('packages');
  1617. // Add a sorting column.
  1618. $smcFunc['db_add_column']('{db_prefix}smileys', array('name' => 'temp_order', 'size' => 8, 'type' => 'mediumint', 'null' => false));
  1619. // Set the contents of this column.
  1620. $smcFunc['db_query']('set_smiley_order', '
  1621. UPDATE {db_prefix}smileys
  1622. SET temp_order = LENGTH(code)',
  1623. array(
  1624. )
  1625. );
  1626. // Order the table by this column.
  1627. $smcFunc['db_query']('alter_table_smileys', '
  1628. ALTER TABLE {db_prefix}smileys
  1629. ORDER BY temp_order DESC',
  1630. array(
  1631. 'db_error_skip' => true,
  1632. )
  1633. );
  1634. // Remove the sorting column.
  1635. $smcFunc['db_remove_column']('{db_prefix}smileys', 'temp_order');
  1636. }
  1637. ?>