ManageSmileys.php 65 KB

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