ManageSmileys.php 64 KB

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