ManageSmileys.php 66 KB

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