ManageSmileys.php 66 KB

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