ManageSmileys.php 64 KB

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