ManageLanguages.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. <?php
  2. /**
  3. * This file handles the administration of languages tasks.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines
  9. *
  10. * @copyright 2012 Simple Machines Forum contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('Hacking attempt...');
  17. /**
  18. * This is the main function for the languages area.
  19. * It dispatches the requests.
  20. * Loads the ManageLanguages template. (sub-actions will use it)
  21. * @todo lazy loading.
  22. *
  23. * @uses ManageSettings language file
  24. */
  25. function ManageLanguages()
  26. {
  27. global $context, $txt, $scripturl, $modSettings;
  28. loadTemplate('ManageLanguages');
  29. loadLanguage('ManageSettings');
  30. $context['page_title'] = $txt['edit_languages'];
  31. $context['sub_template'] = 'show_settings';
  32. $subActions = array(
  33. 'edit' => 'ModifyLanguages',
  34. 'add' => 'AddLanguage',
  35. 'settings' => 'ModifyLanguageSettings',
  36. 'downloadlang' => 'DownloadLanguage',
  37. 'editlang' => 'ModifyLanguage',
  38. );
  39. $config_vars = array();
  40. call_integration_hook('integrate_manage_languages', array(&$config_vars));
  41. // By default we're managing languages.
  42. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'edit';
  43. $context['sub_action'] = $_REQUEST['sa'];
  44. // Load up all the tabs...
  45. $context[$context['admin_menu_name']]['tab_data'] = array(
  46. 'title' => $txt['language_configuration'],
  47. 'description' => $txt['language_description'],
  48. );
  49. // Call the right function for this sub-acton.
  50. $subActions[$_REQUEST['sa']]();
  51. }
  52. /**
  53. * Interface for adding a new language
  54. *
  55. * @uses ManageLanguages template, add_language sub-template.
  56. */
  57. function AddLanguage()
  58. {
  59. global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl;
  60. // Are we searching for new languages courtesy of Simple Machines?
  61. if (!empty($_POST['smf_add_sub']))
  62. {
  63. // Need fetch_web_data.
  64. require_once($sourcedir . '/Subs-Package.php');
  65. $context['smf_search_term'] = htmlspecialchars(trim($_POST['smf_add']));
  66. $listOptions = array(
  67. 'id' => 'smf_languages',
  68. 'get_items' => array(
  69. 'function' => 'list_getLanguagesList',
  70. ),
  71. 'columns' => array(
  72. 'name' => array(
  73. 'header' => array(
  74. 'value' => $txt['name'],
  75. ),
  76. 'data' => array(
  77. 'db' => 'name',
  78. ),
  79. ),
  80. 'description' => array(
  81. 'header' => array(
  82. 'value' => $txt['add_language_smf_desc'],
  83. ),
  84. 'data' => array(
  85. 'db' => 'description',
  86. ),
  87. ),
  88. 'version' => array(
  89. 'header' => array(
  90. 'value' => $txt['add_language_smf_version'],
  91. ),
  92. 'data' => array(
  93. 'db' => 'version',
  94. ),
  95. ),
  96. 'utf8' => array(
  97. 'header' => array(
  98. 'value' => $txt['add_language_smf_utf8'],
  99. ),
  100. 'data' => array(
  101. 'db' => 'utf8',
  102. ),
  103. ),
  104. 'install_link' => array(
  105. 'header' => array(
  106. 'value' => $txt['add_language_smf_install'],
  107. 'class' => 'centercol',
  108. ),
  109. 'data' => array(
  110. 'db' => 'install_link',
  111. 'class' => 'centercol',
  112. ),
  113. ),
  114. ),
  115. );
  116. require_once($sourcedir . '/Subs-List.php');
  117. createList($listOptions);
  118. $context['default_list'] = 'smf_languages';
  119. }
  120. $context['sub_template'] = 'add_language';
  121. }
  122. /**
  123. * Gets a list of available languages from the mother ship
  124. * Will return a subset if searching, otherwise all avaialble
  125. *
  126. * @return string
  127. */
  128. function list_getLanguagesList()
  129. {
  130. global $forum_version, $context, $sourcedir, $smcFunc, $txt, $scripturl;
  131. // We're going to use this URL.
  132. $url = 'http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => '')));
  133. // Load the class file and stick it into an array.
  134. require_once($sourcedir . '/Class-Package.php');
  135. $language_list = new xmlArray(fetch_web_data($url), true);
  136. // Check that the site responded and that the language exists.
  137. if (!$language_list->exists('languages'))
  138. $context['smf_error'] = 'no_response';
  139. elseif (!$language_list->exists('languages/language'))
  140. $context['smf_error'] = 'no_files';
  141. else
  142. {
  143. $language_list = $language_list->path('languages[0]');
  144. $lang_files = $language_list->set('language');
  145. $smf_languages = array();
  146. foreach ($lang_files as $file)
  147. {
  148. // Were we searching?
  149. if (!empty($context['smf_search_term']) && strpos($file->fetch('name'), $smcFunc['strtolower']($context['smf_search_term'])) === false)
  150. continue;
  151. $smf_languages[] = array(
  152. 'id' => $file->fetch('id'),
  153. 'name' => $smcFunc['ucwords']($file->fetch('name')),
  154. 'version' => $file->fetch('version'),
  155. 'utf8' => $file->fetch('utf8') ? $txt['yes'] : $txt['no'],
  156. 'description' => $file->fetch('description'),
  157. 'install_link' => '<a href="' . $scripturl . '?action=admin;area=languages;sa=downloadlang;did=' . $file->fetch('id') . ';' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['add_language_smf_install'] . '</a>',
  158. );
  159. }
  160. if (empty($smf_languages))
  161. $context['smf_error'] = 'no_files';
  162. else
  163. return $smf_languages;
  164. }
  165. }
  166. /**
  167. * Download a language file from the Simple Machines website.
  168. * Requires a valid download ID ("did") in the URL.
  169. * Also handles installing language files.
  170. * Attempts to chmod things as needed.
  171. * Uses a standard list to display information about all the files and where they'll be put.
  172. *
  173. * @uses ManageLanguages template, download_language sub-template.
  174. * @uses Admin template, show_list sub-template.
  175. */
  176. function DownloadLanguage()
  177. {
  178. global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl, $modSettings;
  179. loadLanguage('ManageSettings');
  180. require_once($sourcedir . '/Subs-Package.php');
  181. // Clearly we need to know what to request.
  182. if (!isset($_GET['did']))
  183. fatal_lang_error('no_access', false);
  184. // Some lovely context.
  185. $context['download_id'] = $_GET['did'];
  186. $context['sub_template'] = 'download_language';
  187. $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'add';
  188. // Can we actually do the installation - and do they want to?
  189. if (!empty($_POST['do_install']) && !empty($_POST['copy_file']))
  190. {
  191. checkSession('get');
  192. validateToken('admin-dlang');
  193. $chmod_files = array();
  194. $install_files = array();
  195. // Check writable status.
  196. foreach ($_POST['copy_file'] as $file)
  197. {
  198. // Check it's not very bad.
  199. if (strpos($file, '..') !== false || (strpos($file, 'Themes') !== 0 && !preg_match('~agreement\.[A-Za-z-_0-9]+\.txt$~', $file)))
  200. fatal_error($txt['languages_download_illegal_paths']);
  201. $chmod_files[] = $boarddir . '/' . $file;
  202. $install_files[] = $file;
  203. }
  204. // Call this in case we have work to do.
  205. $file_status = create_chmod_control($chmod_files);
  206. $files_left = $file_status['files']['notwritable'];
  207. // Something not writable?
  208. if (!empty($files_left))
  209. $context['error_message'] = $txt['languages_download_not_chmod'];
  210. // Otherwise, go go go!
  211. elseif (!empty($install_files))
  212. {
  213. $archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), $boarddir, false, true, $install_files);
  214. // Make sure the files aren't stuck in the cache.
  215. package_flush_cache();
  216. $context['install_complete'] = sprintf($txt['languages_download_complete_desc'], $scripturl . '?action=admin;area=languages');
  217. return;
  218. }
  219. }
  220. // Open up the old china.
  221. if (!isset($archive_content))
  222. $archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), null);
  223. if (empty($archive_content))
  224. fatal_error($txt['add_language_error_no_response']);
  225. // Now for each of the files, let's do some *stuff*
  226. $context['files'] = array(
  227. 'lang' => array(),
  228. 'other' => array(),
  229. );
  230. $context['make_writable'] = array();
  231. foreach ($archive_content as $file)
  232. {
  233. $dirname = dirname($file['filename']);
  234. $filename = basename($file['filename']);
  235. $extension = substr($filename, strrpos($filename, '.') + 1);
  236. // Don't do anything with files we don't understand.
  237. if (!in_array($extension, array('php', 'jpg', 'gif', 'jpeg', 'png', 'txt')))
  238. continue;
  239. // Basic data.
  240. $context_data = array(
  241. 'name' => $filename,
  242. 'destination' => $boarddir . '/' . $file['filename'],
  243. 'generaldest' => $file['filename'],
  244. 'size' => $file['size'],
  245. // Does chmod status allow the copy?
  246. 'writable' => false,
  247. // Should we suggest they copy this file?
  248. 'default_copy' => true,
  249. // Does the file already exist, if so is it same or different?
  250. 'exists' => false,
  251. );
  252. // Does the file exist, is it different and can we overwrite?
  253. if (file_exists($boarddir . '/' . $file['filename']))
  254. {
  255. if (is_writable($boarddir . '/' . $file['filename']))
  256. $context_data['writable'] = true;
  257. // Finally, do we actually think the content has changed?
  258. if ($file['size'] == filesize($boarddir . '/' . $file['filename']) && $file['md5'] == md5_file($boarddir . '/' . $file['filename']))
  259. {
  260. $context_data['exists'] = 'same';
  261. $context_data['default_copy'] = false;
  262. }
  263. // Attempt to discover newline character differences.
  264. elseif ($file['md5'] == md5(preg_replace("~[\r]?\n~", "\r\n", file_get_contents($boarddir . '/' . $file['filename']))))
  265. {
  266. $context_data['exists'] = 'same';
  267. $context_data['default_copy'] = false;
  268. }
  269. else
  270. $context_data['exists'] = 'different';
  271. }
  272. // No overwrite?
  273. else
  274. {
  275. // Can we at least stick it in the directory...
  276. if (is_writable($boarddir . '/' . $dirname))
  277. $context_data['writable'] = true;
  278. }
  279. // I love PHP files, that's why I'm a developer and not an artistic type spending my time drinking absinth and living a life of sin...
  280. if ($extension == 'php' && preg_match('~\w+\.\w+(?:-utf8)?\.php~', $filename))
  281. {
  282. $context_data += array(
  283. 'version' => '??',
  284. 'cur_version' => false,
  285. 'version_compare' => 'newer',
  286. );
  287. list ($name, $language) = explode('.', $filename);
  288. // Let's get the new version, I like versions, they tell me that I'm up to date.
  289. if (preg_match('~\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '~i', $file['preview'], $match) == 1)
  290. $context_data['version'] = $match[1];
  291. // Now does the old file exist - if so what is it's version?
  292. if (file_exists($boarddir . '/' . $file['filename']))
  293. {
  294. // OK - what is the current version?
  295. $fp = fopen($boarddir . '/' . $file['filename'], 'rb');
  296. $header = fread($fp, 768);
  297. fclose($fp);
  298. // Find the version.
  299. if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1)
  300. {
  301. $context_data['cur_version'] = $match[1];
  302. // How does this compare?
  303. if ($context_data['cur_version'] == $context_data['version'])
  304. $context_data['version_compare'] = 'same';
  305. elseif ($context_data['cur_version'] > $context_data['version'])
  306. $context_data['version_compare'] = 'older';
  307. // Don't recommend copying if the version is the same.
  308. if ($context_data['version_compare'] != 'newer')
  309. $context_data['default_copy'] = false;
  310. }
  311. }
  312. // Add the context data to the main set.
  313. $context['files']['lang'][] = $context_data;
  314. }
  315. else
  316. {
  317. // If we think it's a theme thing, work out what the theme is.
  318. if (strpos($dirname, 'Themes') === 0 && preg_match('~Themes[\\/]([^\\/]+)[\\/]~', $dirname, $match))
  319. $theme_name = $match[1];
  320. else
  321. $theme_name = 'misc';
  322. // Assume it's an image, could be an acceptance note etc but rare.
  323. $context['files']['images'][$theme_name][] = $context_data;
  324. }
  325. // Collect together all non-writable areas.
  326. if (!$context_data['writable'])
  327. $context['make_writable'][] = $context_data['destination'];
  328. }
  329. // So, I'm a perfectionist - let's get the theme names.
  330. $theme_indexes = array();
  331. foreach ($context['files']['images'] as $k => $dummy)
  332. $indexes[] = $k;
  333. $context['theme_names'] = array();
  334. if (!empty($indexes))
  335. {
  336. $value_data = array(
  337. 'query' => array(),
  338. 'params' => array(),
  339. );
  340. foreach ($indexes as $k => $index)
  341. {
  342. $value_data['query'][] = 'value LIKE {string:value_' . $k . '}';
  343. $value_data['params']['value_' . $k] = '%' . $index;
  344. }
  345. $request = $smcFunc['db_query']('', '
  346. SELECT id_theme, value
  347. FROM {db_prefix}themes
  348. WHERE id_member = {int:no_member}
  349. AND variable = {string:theme_dir}
  350. AND (' . implode(' OR ', $value_data['query']) . ')',
  351. array_merge($value_data['params'], array(
  352. 'no_member' => 0,
  353. 'theme_dir' => 'theme_dir',
  354. 'index_compare_explode' => 'value LIKE \'%' . implode('\' OR value LIKE \'%', $indexes) . '\'',
  355. ))
  356. );
  357. $themes = array();
  358. while ($row = $smcFunc['db_fetch_assoc']($request))
  359. {
  360. // Find the right one.
  361. foreach ($indexes as $index)
  362. if (strpos($row['value'], $index) !== false)
  363. $themes[$row['id_theme']] = $index;
  364. }
  365. $smcFunc['db_free_result']($request);
  366. if (!empty($themes))
  367. {
  368. // Now we have the id_theme we can get the pretty description.
  369. $request = $smcFunc['db_query']('', '
  370. SELECT id_theme, value
  371. FROM {db_prefix}themes
  372. WHERE id_member = {int:no_member}
  373. AND variable = {string:name}
  374. AND id_theme IN ({array_int:theme_list})',
  375. array(
  376. 'theme_list' => array_keys($themes),
  377. 'no_member' => 0,
  378. 'name' => 'name',
  379. )
  380. );
  381. while ($row = $smcFunc['db_fetch_assoc']($request))
  382. {
  383. // Now we have it...
  384. $context['theme_names'][$themes[$row['id_theme']]] = $row['value'];
  385. }
  386. $smcFunc['db_free_result']($request);
  387. }
  388. }
  389. // Before we go to far can we make anything writable, eh, eh?
  390. if (!empty($context['make_writable']))
  391. {
  392. // What is left to be made writable?
  393. $file_status = create_chmod_control($context['make_writable']);
  394. $context['still_not_writable'] = $file_status['files']['notwritable'];
  395. // Mark those which are now writable as such.
  396. foreach ($context['files'] as $type => $data)
  397. {
  398. if ($type == 'lang')
  399. {
  400. foreach ($data as $k => $file)
  401. if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable']))
  402. $context['files'][$type][$k]['writable'] = true;
  403. }
  404. else
  405. {
  406. foreach ($data as $theme => $files)
  407. foreach ($files as $k => $file)
  408. if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable']))
  409. $context['files'][$type][$theme][$k]['writable'] = true;
  410. }
  411. }
  412. // Are we going to need more language stuff?
  413. if (!empty($context['still_not_writable']))
  414. loadLanguage('Packages');
  415. }
  416. // This is the list for the main files.
  417. $listOptions = array(
  418. 'id' => 'lang_main_files_list',
  419. 'title' => $txt['languages_download_main_files'],
  420. 'get_items' => array(
  421. 'function' => create_function('', '
  422. global $context;
  423. return $context[\'files\'][\'lang\'];
  424. '),
  425. ),
  426. 'columns' => array(
  427. 'name' => array(
  428. 'header' => array(
  429. 'value' => $txt['languages_download_filename'],
  430. ),
  431. 'data' => array(
  432. 'function' => create_function('$rowData', '
  433. global $context, $txt;
  434. return \'<strong>\' . $rowData[\'name\'] . \'</strong><br /><span class="smalltext">\' . $txt[\'languages_download_dest\'] . \': \' . $rowData[\'destination\'] . \'</span>\' . ($rowData[\'version_compare\'] == \'older\' ? \'<br />\' . $txt[\'languages_download_older\'] : \'\');
  435. '),
  436. ),
  437. ),
  438. 'writable' => array(
  439. 'header' => array(
  440. 'value' => $txt['languages_download_writable'],
  441. ),
  442. 'data' => array(
  443. 'function' => create_function('$rowData', '
  444. global $txt;
  445. return \'<span style="color: \' . ($rowData[\'writable\'] ? \'green\' : \'red\') . \';">\' . ($rowData[\'writable\'] ? $txt[\'yes\'] : $txt[\'no\']) . \'</span>\';
  446. '),
  447. ),
  448. ),
  449. 'version' => array(
  450. 'header' => array(
  451. 'value' => $txt['languages_download_version'],
  452. ),
  453. 'data' => array(
  454. 'function' => create_function('$rowData', '
  455. global $txt;
  456. return \'<span style="color: \' . ($rowData[\'version_compare\'] == \'older\' ? \'red\' : ($rowData[\'version_compare\'] == \'same\' ? \'orange\' : \'green\')) . \';">\' . $rowData[\'version\'] . \'</span>\';
  457. '),
  458. ),
  459. ),
  460. 'exists' => array(
  461. 'header' => array(
  462. 'value' => $txt['languages_download_exists'],
  463. ),
  464. 'data' => array(
  465. 'function' => create_function('$rowData', '
  466. global $txt;
  467. return $rowData[\'exists\'] ? ($rowData[\'exists\'] == \'same\' ? $txt[\'languages_download_exists_same\'] : $txt[\'languages_download_exists_different\']) : $txt[\'no\'];
  468. '),
  469. ),
  470. ),
  471. 'copy' => array(
  472. 'header' => array(
  473. 'value' => $txt['languages_download_copy'],
  474. 'class' => 'centercol',
  475. ),
  476. 'data' => array(
  477. 'function' => create_function('$rowData', '
  478. return \'<input type="checkbox" name="copy_file[]" value="\' . $rowData[\'generaldest\'] . \'" \' . ($rowData[\'default_copy\'] ? \'checked="checked"\' : \'\') . \' class="input_check" />\';
  479. '),
  480. 'style' => 'width: 4%;',
  481. 'class' => 'centercol',
  482. ),
  483. ),
  484. ),
  485. );
  486. // Kill the cache, as it is now invalid..
  487. if (!empty($modSettings['cache_enable']))
  488. {
  489. cache_put_data('known_languages', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
  490. cache_put_data('known_languages_all', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
  491. }
  492. require_once($sourcedir . '/Subs-List.php');
  493. createList($listOptions);
  494. $context['default_list'] = 'lang_main_files_list';
  495. createToken('admin-dlang');
  496. }
  497. /**
  498. * This lists all the current languages and allows editing of them.
  499. */
  500. function ModifyLanguages()
  501. {
  502. global $txt, $context, $scripturl;
  503. global $user_info, $smcFunc, $sourcedir, $language, $boarddir, $forum_version;
  504. // Setting a new default?
  505. if (!empty($_POST['set_default']) && !empty($_POST['def_language']))
  506. {
  507. checkSession();
  508. validateToken('admin-lang');
  509. if ($_POST['def_language'] != $language)
  510. {
  511. require_once($sourcedir . '/Subs-Admin.php');
  512. updateSettingsFile(array('language' => '\'' . $_POST['def_language'] . '\''));
  513. $language = $_POST['def_language'];
  514. }
  515. }
  516. // Create another one time token here.
  517. createToken('admin-lang');
  518. $listOptions = array(
  519. 'id' => 'language_list',
  520. 'items_per_page' => 20,
  521. 'base_href' => $scripturl . '?action=admin;area=languages',
  522. 'title' => $txt['edit_languages'],
  523. 'get_items' => array(
  524. 'function' => 'list_getLanguages',
  525. ),
  526. 'get_count' => array(
  527. 'function' => 'list_getNumLanguages',
  528. ),
  529. 'columns' => array(
  530. 'default' => array(
  531. 'header' => array(
  532. 'value' => $txt['languages_default'],
  533. 'class' => 'centercol',
  534. ),
  535. 'data' => array(
  536. 'function' => create_function('$rowData', '
  537. return \'<input type="radio" name="def_language" value="\' . $rowData[\'id\'] . \'" \' . ($rowData[\'default\'] ? \'checked="checked"\' : \'\') . \' onclick="highlightSelected(\\\'list_language_list_\' . $rowData[\'id\'] . \'\\\');" class="input_radio" />\';
  538. '),
  539. 'style' => 'width: 8%;',
  540. 'class' => 'centercol',
  541. ),
  542. ),
  543. 'name' => array(
  544. 'header' => array(
  545. 'value' => $txt['languages_lang_name'],
  546. ),
  547. 'data' => array(
  548. 'function' => create_function('$rowData', '
  549. global $scripturl, $context;
  550. return sprintf(\'<a href="%1$s?action=admin;area=languages;sa=editlang;lid=%2$s">%3$s</a>\', $scripturl, $rowData[\'id\'], $rowData[\'name\']);
  551. '),
  552. ),
  553. ),
  554. 'character_set' => array(
  555. 'header' => array(
  556. 'value' => $txt['languages_character_set'],
  557. ),
  558. 'data' => array(
  559. 'db_htmlsafe' => 'char_set',
  560. ),
  561. ),
  562. 'count' => array(
  563. 'header' => array(
  564. 'value' => $txt['languages_users'],
  565. ),
  566. 'data' => array(
  567. 'db_htmlsafe' => 'count',
  568. ),
  569. ),
  570. 'locale' => array(
  571. 'header' => array(
  572. 'value' => $txt['languages_locale'],
  573. ),
  574. 'data' => array(
  575. 'db_htmlsafe' => 'locale',
  576. ),
  577. ),
  578. ),
  579. 'form' => array(
  580. 'href' => $scripturl . '?action=admin;area=languages',
  581. 'token' => 'admin-lang',
  582. ),
  583. 'additional_rows' => array(
  584. array(
  585. 'position' => 'bottom_of_list',
  586. 'value' => '<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" /><input type="submit" name="set_default" value="' . $txt['save'] . '"' . (is_writable($boarddir . '/Settings.php') ? '' : ' disabled="disabled"') . ' class="button_submit" />',
  587. ),
  588. ),
  589. // For highlighting the default.
  590. 'javascript' => '
  591. var prevClass = "";
  592. var prevDiv = "";
  593. function highlightSelected(box)
  594. {
  595. if (prevClass != "")
  596. prevDiv.className = prevClass;
  597. prevDiv = document.getElementById(box);
  598. prevClass = prevDiv.className;
  599. prevDiv.className = "highlight2";
  600. }
  601. highlightSelected("list_language_list_' . ($language == '' ? 'english' : $language). '");
  602. ',
  603. );
  604. // Display a warning if we cannot edit the default setting.
  605. if (!is_writable($boarddir . '/Settings.php'))
  606. $listOptions['additional_rows'][] = array(
  607. 'position' => 'after_title',
  608. 'value' => $txt['language_settings_writable'],
  609. 'class' => 'smalltext alert',
  610. );
  611. require_once($sourcedir . '/Subs-List.php');
  612. createList($listOptions);
  613. $context['sub_template'] = 'show_list';
  614. $context['default_list'] = 'language_list';
  615. }
  616. /**
  617. * How many languages?
  618. * Callback for the list in ManageLanguageSettings().
  619. */
  620. function list_getNumLanguages()
  621. {
  622. return count(getLanguages(true, false));
  623. }
  624. /**
  625. * Fetch the actual language information.
  626. * Callback for $listOptions['get_items']['function'] in ManageLanguageSettings.
  627. * Determines which languages are available by looking for the "index.{language}.php" file.
  628. * Also figures out how many users are using a particular language.
  629. */
  630. function list_getLanguages()
  631. {
  632. global $settings, $smcFunc, $language, $context, $txt;
  633. $languages = array();
  634. // Keep our old entries.
  635. $old_txt = $txt;
  636. $backup_actual_theme_dir = $settings['actual_theme_dir'];
  637. $backup_base_theme_dir = !empty($settings['base_theme_dir']) ? $settings['base_theme_dir'] : '';
  638. // Override these for now.
  639. $settings['actual_theme_dir'] = $settings['base_theme_dir'] = $settings['default_theme_dir'];
  640. getLanguages(true, false);
  641. // Put them back.
  642. $settings['actual_theme_dir'] = $backup_actual_theme_dir;
  643. if (!empty($backup_base_theme_dir))
  644. $settings['base_theme_dir'] = $backup_base_theme_dir;
  645. else
  646. unset($settings['base_theme_dir']);
  647. // Get the language files and data...
  648. foreach ($context['languages'] as $lang)
  649. {
  650. // Load the file to get the character set.
  651. require($settings['default_theme_dir'] . '/languages/index.' . $lang['filename'] . '.php');
  652. $languages[$lang['filename']] = array(
  653. 'id' => $lang['filename'],
  654. 'count' => 0,
  655. 'char_set' => $txt['lang_character_set'],
  656. 'default' => $language == $lang['filename'] || ($language == '' && $lang['filename'] == 'english'),
  657. 'locale' => $txt['lang_locale'],
  658. 'name' => $smcFunc['ucwords'](strtr($lang['filename'], array('_' => ' ', '-utf8' => ''))),
  659. );
  660. }
  661. // Work out how many people are using each language.
  662. $request = $smcFunc['db_query']('', '
  663. SELECT lngfile, COUNT(*) AS num_users
  664. FROM {db_prefix}members
  665. GROUP BY lngfile',
  666. array(
  667. )
  668. );
  669. while ($row = $smcFunc['db_fetch_assoc']($request))
  670. {
  671. // Default?
  672. if (empty($row['lngfile']) || !isset($languages[$row['lngfile']]))
  673. $row['lngfile'] = $language;
  674. if (!isset($languages[$row['lngfile']]) && isset($languages['english']))
  675. $languages['english']['count'] += $row['num_users'];
  676. elseif (isset($languages[$row['lngfile']]))
  677. $languages[$row['lngfile']]['count'] += $row['num_users'];
  678. }
  679. $smcFunc['db_free_result']($request);
  680. // Restore the current users language.
  681. $txt = $old_txt;
  682. // Return how many we have.
  683. return $languages;
  684. }
  685. /**
  686. * Edit language related settings.
  687. *
  688. * @param bool $return_config = false
  689. */
  690. function ModifyLanguageSettings($return_config = false)
  691. {
  692. global $scripturl, $context, $txt, $boarddir, $settings, $smcFunc, $sourcedir;
  693. // We'll want to save them someday.
  694. require_once $sourcedir . '/ManageServer.php';
  695. // Warn the user if the backup of Settings.php failed.
  696. $settings_not_writable = !is_writable($boarddir . '/Settings.php');
  697. $settings_backup_fail = !@is_writable($boarddir . '/Settings_bak.php') || !@copy($boarddir . '/Settings.php', $boarddir . '/Settings_bak.php');
  698. /* If you're writing a mod, it's a bad idea to add things here....
  699. For each option:
  700. variable name, description, type (constant), size/possible values, helptext.
  701. OR an empty string for a horizontal rule.
  702. OR a string for a titled section. */
  703. $config_vars = array(
  704. 'language' => array('language', $txt['default_language'], 'file', 'select', array(), null, 'disabled' => $settings_not_writable),
  705. array('userLanguage', $txt['userLanguage'], 'db', 'check', null, 'userLanguage'),
  706. );
  707. call_integration_hook('integrate_language_settings', array(&$config_vars));
  708. if ($return_config)
  709. return $config_vars;
  710. // Get our languages. No cache and use utf8.
  711. getLanguages(false, false);
  712. foreach ($context['languages'] as $lang)
  713. $config_vars['language'][4][$lang['filename']] = array($lang['filename'], strtr($lang['name'], array('-utf8' => ' (UTF-8)')));
  714. // Saving settings?
  715. if (isset($_REQUEST['save']))
  716. {
  717. checkSession();
  718. call_integration_hook('integrate_save_language_settings');
  719. saveSettings($config_vars);
  720. redirectexit('action=admin;area=languages;sa=settings');
  721. }
  722. // Setup the template stuff.
  723. $context['post_url'] = $scripturl . '?action=admin;area=languages;sa=settings;save';
  724. $context['settings_title'] = $txt['language_settings'];
  725. $context['save_disabled'] = $settings_not_writable;
  726. if ($settings_not_writable)
  727. $context['settings_message'] = '<div class="centertext"><strong>' . $txt['settings_not_writable'] . '</strong></div><br />';
  728. elseif ($settings_backup_fail)
  729. $context['settings_message'] = '<div class="centertext"><strong>' . $txt['admin_backup_fail'] . '</strong></div><br />';
  730. // Fill the config array.
  731. prepareServerSettingsContext($config_vars);
  732. }
  733. /**
  734. * Edit a particular set of language entries.
  735. */
  736. function ModifyLanguage()
  737. {
  738. global $settings, $context, $smcFunc, $txt, $modSettings, $boarddir, $sourcedir, $language;
  739. loadLanguage('ManageSettings');
  740. // Select the languages tab.
  741. $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'edit';
  742. $context['page_title'] = $txt['edit_languages'];
  743. $context['sub_template'] = 'modify_language_entries';
  744. $context['lang_id'] = $_GET['lid'];
  745. list($theme_id, $file_id) = empty($_REQUEST['tfid']) || strpos($_REQUEST['tfid'], '+') === false ? array(1, '') : explode('+', $_REQUEST['tfid']);
  746. // Clean the ID - just in case.
  747. preg_match('~([A-Za-z0-9_-]+)~', $context['lang_id'], $matches);
  748. $context['lang_id'] = $matches[1];
  749. // Get all the theme data.
  750. $request = $smcFunc['db_query']('', '
  751. SELECT id_theme, variable, value
  752. FROM {db_prefix}themes
  753. WHERE id_theme != {int:default_theme}
  754. AND id_member = {int:no_member}
  755. AND variable IN ({string:name}, {string:theme_dir})',
  756. array(
  757. 'default_theme' => 1,
  758. 'no_member' => 0,
  759. 'name' => 'name',
  760. 'theme_dir' => 'theme_dir',
  761. )
  762. );
  763. $themes = array(
  764. 1 => array(
  765. 'name' => $txt['dvc_default'],
  766. 'theme_dir' => $settings['default_theme_dir'],
  767. ),
  768. );
  769. while ($row = $smcFunc['db_fetch_assoc']($request))
  770. $themes[$row['id_theme']][$row['variable']] = $row['value'];
  771. $smcFunc['db_free_result']($request);
  772. // This will be where we look
  773. $lang_dirs = array();
  774. // Check we have themes with a path and a name - just in case - and add the path.
  775. foreach ($themes as $id => $data)
  776. {
  777. if (count($data) != 2)
  778. unset($themes[$id]);
  779. elseif (is_dir($data['theme_dir'] . '/languages'))
  780. $lang_dirs[$id] = $data['theme_dir'] . '/languages';
  781. // How about image directories?
  782. if (is_dir($data['theme_dir'] . '/images/' . $context['lang_id']))
  783. $images_dirs[$id] = $data['theme_dir'] . '/images/' . $context['lang_id'];
  784. }
  785. $current_file = $file_id ? $lang_dirs[$theme_id] . '/' . $file_id . '.' . $context['lang_id'] . '.php' : '';
  786. // Now for every theme get all the files and stick them in context!
  787. $context['possible_files'] = array();
  788. foreach ($lang_dirs as $theme => $theme_dir)
  789. {
  790. // Open it up.
  791. $dir = dir($theme_dir);
  792. while ($entry = $dir->read())
  793. {
  794. // We're only after the files for this language.
  795. if (preg_match('~^([A-Za-z]+)\.' . $context['lang_id'] . '\.php$~', $entry, $matches) == 0)
  796. continue;
  797. if (!isset($context['possible_files'][$theme]))
  798. $context['possible_files'][$theme] = array(
  799. 'id' => $theme,
  800. 'name' => $themes[$theme]['name'],
  801. 'files' => array(),
  802. );
  803. $context['possible_files'][$theme]['files'][] = array(
  804. 'id' => $matches[1],
  805. 'name' => isset($txt['lang_file_desc_' . $matches[1]]) ? $txt['lang_file_desc_' . $matches[1]] : $matches[1],
  806. 'selected' => $theme_id == $theme && $file_id == $matches[1],
  807. );
  808. }
  809. $dir->close();
  810. usort($context['possible_files'][$theme]['files'], create_function('$val1, $val2', 'return strcmp($val1[\'name\'], $val2[\'name\']);'));
  811. }
  812. // We no longer wish to speak this language.
  813. if (!empty($_POST['delete_main']) && $context['lang_id'] != 'english')
  814. {
  815. checkSession();
  816. validateToken('admin-mlang');
  817. // @todo Todo: FTP Controls?
  818. require_once($sourcedir . '/Subs-Package.php');
  819. // First, Make a backup?
  820. if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['lang_id'] . '$$$'))
  821. {
  822. $_SESSION['last_backup_for'] = $context['lang_id'] . '$$$';
  823. package_create_backup('backup_lang_' . $context['lang_id']);
  824. }
  825. // Second, loop through the array to remove the files.
  826. foreach ($lang_dirs as $curPath)
  827. {
  828. foreach ($context['possible_files'][1]['files'] as $lang)
  829. if (file_exists($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php'))
  830. unlink($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php');
  831. // Check for the email template.
  832. if (file_exists($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php'))
  833. unlink($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php');
  834. }
  835. // Third, the agreement file.
  836. if (file_exists($boarddir . '/agreement.' . $context['lang_id'] . '.txt'))
  837. unlink($boarddir . '/agreement.' . $context['lang_id'] . '.txt');
  838. // Fourth, a related images folder?
  839. foreach ($images_dirs as $curPath)
  840. if (is_dir($curPath))
  841. deltree($curPath);
  842. // Members can no longer use this language.
  843. $smcFunc['db_query']('', '
  844. UPDATE {db_prefix}members
  845. SET lngfile = {string:empty_string}
  846. WHERE lngfile = {string:current_language}',
  847. array(
  848. 'empty_string' => '',
  849. 'current_language' => $context['lang_id'],
  850. )
  851. );
  852. // Fifth, update getLanguages() cache.
  853. if (!empty($modSettings['cache_enable']))
  854. {
  855. cache_put_data('known_languages', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
  856. cache_put_data('known_languages_all', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
  857. }
  858. // Sixth, if we deleted the default language, set us back to english?
  859. if ($context['lang_id'] == $language)
  860. {
  861. require_once($sourcedir . '/Subs-Admin.php');
  862. $language = 'english';
  863. updateSettingsFile(array('language' => '\'' . $language . '\''));
  864. }
  865. // Seventh, get out of here.
  866. redirectexit('action=admin;area=languages;sa=edit;' . $context['session_var'] . '=' . $context['session_id']);
  867. }
  868. // Saving primary settings?
  869. $madeSave = false;
  870. if (!empty($_POST['save_main']) && !$current_file)
  871. {
  872. checkSession();
  873. validateToken('admin-mlang');
  874. // Read in the current file.
  875. $current_data = implode('', file($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'));
  876. // These are the replacements. old => new
  877. $replace_array = array(
  878. '~\$txt\[\'lang_character_set\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_character_set\'] = \'' . addslashes($_POST['character_set']) . '\';',
  879. '~\$txt\[\'lang_locale\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_locale\'] = \'' . addslashes($_POST['locale']) . '\';',
  880. '~\$txt\[\'lang_dictionary\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_dictionary\'] = \'' . addslashes($_POST['dictionary']) . '\';',
  881. '~\$txt\[\'lang_spelling\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_spelling\'] = \'' . addslashes($_POST['spelling']) . '\';',
  882. '~\$txt\[\'lang_rtl\'\]\s=\s[A-Za-z0-9]+;~' => '$txt[\'lang_rtl\'] = ' . (!empty($_POST['rtl']) ? 'true' : 'false') . ';',
  883. );
  884. $current_data = preg_replace(array_keys($replace_array), array_values($replace_array), $current_data);
  885. $fp = fopen($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php', 'w+');
  886. fwrite($fp, $current_data);
  887. fclose($fp);
  888. $madeSave = true;
  889. }
  890. // Quickly load index language entries.
  891. $old_txt = $txt;
  892. require($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php');
  893. $context['lang_file_not_writable_message'] = is_writable($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php') ? '' : sprintf($txt['lang_file_not_writable'], $settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php');
  894. // Setup the primary settings context.
  895. $context['primary_settings'] = array(
  896. 'name' => $smcFunc['ucwords'](strtr($context['lang_id'], array('_' => ' ', '-utf8' => ''))),
  897. 'character_set' => $txt['lang_character_set'],
  898. 'locale' => $txt['lang_locale'],
  899. 'dictionary' => $txt['lang_dictionary'],
  900. 'spelling' => $txt['lang_spelling'],
  901. 'rtl' => $txt['lang_rtl'],
  902. );
  903. // Restore normal service.
  904. $txt = $old_txt;
  905. // Are we saving?
  906. $save_strings = array();
  907. if (isset($_POST['save_entries']) && !empty($_POST['entry']))
  908. {
  909. checkSession();
  910. validateToken('admin-mlang');
  911. // Clean each entry!
  912. foreach ($_POST['entry'] as $k => $v)
  913. {
  914. // Only try to save if it's changed!
  915. if ($_POST['entry'][$k] != $_POST['comp'][$k])
  916. $save_strings[$k] = cleanLangString($v, false);
  917. }
  918. }
  919. // If we are editing a file work away at that.
  920. if ($current_file)
  921. {
  922. $context['entries_not_writable_message'] = is_writable($current_file) ? '' : sprintf($txt['lang_entries_not_writable'], $current_file);
  923. $entries = array();
  924. // We can't just require it I'm afraid - otherwise we pass in all kinds of variables!
  925. $multiline_cache = '';
  926. foreach (file($current_file) as $line)
  927. {
  928. // Got a new entry?
  929. if ($line[0] == '$' && !empty($multiline_cache))
  930. {
  931. preg_match('~\$(helptxt|txt|editortxt)\[\'(.+)\'\]\s?=\s?(.+);~ms', strtr($multiline_cache, array("\r" => '')), $matches);
  932. if (!empty($matches[3]))
  933. {
  934. $entries[$matches[2]] = array(
  935. 'type' => $matches[1],
  936. 'full' => $matches[0],
  937. 'entry' => $matches[3],
  938. );
  939. $multiline_cache = '';
  940. }
  941. }
  942. $multiline_cache .= $line;
  943. }
  944. // Last entry to add?
  945. if ($multiline_cache)
  946. {
  947. preg_match('~\$(helptxt|txt|editortxt)\[\'(.+)\'\]\s?=\s?(.+);~ms', strtr($multiline_cache, array("\r" => '')), $matches);
  948. if (!empty($matches[3]))
  949. $entries[$matches[2]] = array(
  950. 'type' => $matches[1],
  951. 'full' => $matches[0],
  952. 'entry' => $matches[3],
  953. );
  954. }
  955. // These are the entries we can definitely save.
  956. $final_saves = array();
  957. $context['file_entries'] = array();
  958. foreach ($entries as $entryKey => $entryValue)
  959. {
  960. // Ignore some things we set separately.
  961. $ignore_files = array('lang_character_set', 'lang_locale', 'lang_dictionary', 'lang_spelling', 'lang_rtl');
  962. if (in_array($entryKey, $ignore_files))
  963. continue;
  964. // These are arrays that need breaking out.
  965. $arrays = array('days', 'days_short', 'months', 'months_titles', 'months_short', 'happy_birthday_author', 'karlbenson1_author', 'nite0859_author', 'zwaldowski_author', 'geezmo_author', 'karlbenson2_author');
  966. if (in_array($entryKey, $arrays))
  967. {
  968. // Get off the first bits.
  969. $entryValue['entry'] = substr($entryValue['entry'], strpos($entryValue['entry'], '(') + 1, strrpos($entryValue['entry'], ')') - strpos($entryValue['entry'], '('));
  970. $entryValue['entry'] = explode(',', strtr($entryValue['entry'], array(' ' => '')));
  971. // Now create an entry for each item.
  972. $cur_index = 0;
  973. $save_cache = array(
  974. 'enabled' => false,
  975. 'entries' => array(),
  976. );
  977. foreach ($entryValue['entry'] as $id => $subValue)
  978. {
  979. // Is this a new index?
  980. if (preg_match('~^(\d+)~', $subValue, $matches))
  981. {
  982. $cur_index = $matches[1];
  983. $subValue = substr($subValue, strpos($subValue, '\''));
  984. }
  985. // Clean up some bits.
  986. $subValue = strtr($subValue, array('"' => '', '\'' => '', ')' => ''));
  987. // Can we save?
  988. if (isset($save_strings[$entryKey . '-+- ' . $cur_index]))
  989. {
  990. $save_cache['entries'][$cur_index] = strtr($save_strings[$entryKey . '-+- ' . $cur_index], array('\'' => ''));
  991. $save_cache['enabled'] = true;
  992. }
  993. else
  994. $save_cache['entries'][$cur_index] = $subValue;
  995. $context['file_entries'][] = array(
  996. 'key' => $entryKey . '-+- ' . $cur_index,
  997. 'value' => $subValue,
  998. 'rows' => 1,
  999. );
  1000. $cur_index++;
  1001. }
  1002. // Do we need to save?
  1003. if ($save_cache['enabled'])
  1004. {
  1005. // Format the string, checking the indexes first.
  1006. $items = array();
  1007. $cur_index = 0;
  1008. foreach ($save_cache['entries'] as $k2 => $v2)
  1009. {
  1010. // Manually show the custom index.
  1011. if ($k2 != $cur_index)
  1012. {
  1013. $items[] = $k2 . ' => \'' . $v2 . '\'';
  1014. $cur_index = $k2;
  1015. }
  1016. else
  1017. $items[] = '\'' . $v2 . '\'';
  1018. $cur_index++;
  1019. }
  1020. // Now create the string!
  1021. $final_saves[$entryKey] = array(
  1022. 'find' => $entryValue['full'],
  1023. 'replace' => '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = array(' . implode(', ', $items) . ');',
  1024. );
  1025. }
  1026. }
  1027. else
  1028. {
  1029. // Saving?
  1030. if (isset($save_strings[$entryKey]) && $save_strings[$entryKey] != $entryValue['entry'])
  1031. {
  1032. // @todo Fix this properly.
  1033. if ($save_strings[$entryKey] == '')
  1034. $save_strings[$entryKey] = '\'\'';
  1035. // Set the new value.
  1036. $entryValue['entry'] = $save_strings[$entryKey];
  1037. // And we know what to save now!
  1038. $final_saves[$entryKey] = array(
  1039. 'find' => $entryValue['full'],
  1040. 'replace' => '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = ' . $save_strings[$entryKey] . ';',
  1041. );
  1042. }
  1043. $editing_string = cleanLangString($entryValue['entry'], true);
  1044. $context['file_entries'][] = array(
  1045. 'key' => $entryKey,
  1046. 'value' => $editing_string,
  1047. 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1,
  1048. );
  1049. }
  1050. }
  1051. // Any saves to make?
  1052. if (!empty($final_saves))
  1053. {
  1054. checkSession();
  1055. $file_contents = implode('', file($current_file));
  1056. foreach ($final_saves as $save)
  1057. $file_contents = strtr($file_contents, array($save['find'] => $save['replace']));
  1058. // Save the actual changes.
  1059. $fp = fopen($current_file, 'w+');
  1060. fwrite($fp, strtr($file_contents, array("\r" => '')));
  1061. fclose($fp);
  1062. $madeSave = true;
  1063. }
  1064. // Another restore.
  1065. $txt = $old_txt;
  1066. }
  1067. // If we saved, redirect.
  1068. if ($madeSave)
  1069. redirectexit('action=admin;area=languages;sa=editlang;lid=' . $context['lang_id']);
  1070. createToken('admin-mlang');
  1071. }
  1072. /**
  1073. * This function cleans language entries to/from display.
  1074. * @todo This function could be two functions?
  1075. *
  1076. * @param $string
  1077. * @param $to_display
  1078. */
  1079. function cleanLangString($string, $to_display = true)
  1080. {
  1081. global $smcFunc;
  1082. // If going to display we make sure it doesn't have any HTML in it - etc.
  1083. $new_string = '';
  1084. if ($to_display)
  1085. {
  1086. // Are we in a string (0 = no, 1 = single quote, 2 = parsed)
  1087. $in_string = 0;
  1088. $is_escape = false;
  1089. for ($i = 0; $i < strlen($string); $i++)
  1090. {
  1091. // Handle ecapes first.
  1092. if ($string{$i} == '\\')
  1093. {
  1094. // Toggle the escape.
  1095. $is_escape = !$is_escape;
  1096. // If we're now escaped don't add this string.
  1097. if ($is_escape)
  1098. continue;
  1099. }
  1100. // Special case - parsed string with line break etc?
  1101. elseif (($string{$i} == 'n' || $string{$i} == 't') && $in_string == 2 && $is_escape)
  1102. {
  1103. // Put the escape back...
  1104. $new_string .= $string{$i} == 'n' ? "\n" : "\t";
  1105. $is_escape = false;
  1106. continue;
  1107. }
  1108. // Have we got a single quote?
  1109. elseif ($string{$i} == '\'')
  1110. {
  1111. // Already in a parsed string, or escaped in a linear string, means we print it - otherwise something special.
  1112. if ($in_string != 2 && ($in_string != 1 || !$is_escape))
  1113. {
  1114. // Is it the end of a single quote string?
  1115. if ($in_string == 1)
  1116. $in_string = 0;
  1117. // Otherwise it's the start!
  1118. else
  1119. $in_string = 1;
  1120. // Don't actually include this character!
  1121. continue;
  1122. }
  1123. }
  1124. // Otherwise a double quote?
  1125. elseif ($string{$i} == '"')
  1126. {
  1127. // Already in a single quote string, or escaped in a parsed string, means we print it - otherwise something special.
  1128. if ($in_string != 1 && ($in_string != 2 || !$is_escape))
  1129. {
  1130. // Is it the end of a double quote string?
  1131. if ($in_string == 2)
  1132. $in_string = 0;
  1133. // Otherwise it's the start!
  1134. else
  1135. $in_string = 2;
  1136. // Don't actually include this character!
  1137. continue;
  1138. }
  1139. }
  1140. // A join/space outside of a string is simply removed.
  1141. elseif ($in_string == 0 && (empty($string{$i}) || $string{$i} == '.'))
  1142. continue;
  1143. // Start of a variable?
  1144. elseif ($in_string == 0 && $string{$i} == '$')
  1145. {
  1146. // Find the whole of it!
  1147. preg_match('~([\$A-Za-z0-9\'\[\]_-]+)~', substr($string, $i), $matches);
  1148. if (!empty($matches[1]))
  1149. {
  1150. // Come up with some pseudo thing to indicate this is a var.
  1151. /**
  1152. * @todo Do better than this, please!
  1153. */
  1154. $new_string .= '{%' . $matches[1] . '%}';
  1155. // We're not going to reparse this.
  1156. $i += strlen($matches[1]) - 1;
  1157. }
  1158. continue;
  1159. }
  1160. // Right, if we're outside of a string we have DANGER, DANGER!
  1161. elseif ($in_string == 0)
  1162. {
  1163. continue;
  1164. }
  1165. // Actually add the character to the string!
  1166. $new_string .= $string{$i};
  1167. // If anything was escaped it ain't any longer!
  1168. $is_escape = false;
  1169. }
  1170. // Unhtml then rehtml the whole thing!
  1171. $new_string = $smcFunc['htmlspecialchars'](un_htmlspecialchars($new_string));
  1172. }
  1173. else
  1174. {
  1175. // Keep track of what we're doing...
  1176. $in_string = 0;
  1177. // This is for deciding whether to HTML a quote.
  1178. $in_html = false;
  1179. for ($i = 0; $i < strlen($string); $i++)
  1180. {
  1181. // We don't do parsed strings apart from for breaks.
  1182. if ($in_string == 2)
  1183. {
  1184. $in_string = 0;
  1185. $new_string .= '"';
  1186. }
  1187. // Not in a string yet?
  1188. if ($in_string != 1)
  1189. {
  1190. $in_string = 1;
  1191. $new_string .= ($new_string ? ' . ' : '') . '\'';
  1192. }
  1193. // Is this a variable?
  1194. if ($string{$i} == '{' && $string{$i + 1} == '%' && $string{$i + 2} == '$')
  1195. {
  1196. // Grab the variable.
  1197. preg_match('~\{%([\$A-Za-z0-9\'\[\]_-]+)%\}~', substr($string, $i), $matches);
  1198. if (!empty($matches[1]))
  1199. {
  1200. if ($in_string == 1)
  1201. $new_string .= '\' . ';
  1202. elseif ($new_string)
  1203. $new_string .= ' . ';
  1204. $new_string .= $matches[1];
  1205. $i += strlen($matches[1]) + 3;
  1206. $in_string = 0;
  1207. }
  1208. continue;
  1209. }
  1210. // Is this a lt sign?
  1211. elseif ($string{$i} == '<')
  1212. {
  1213. // Probably HTML?
  1214. if ($string{$i + 1} != ' ')
  1215. $in_html = true;
  1216. // Assume we need an entity...
  1217. else
  1218. {
  1219. $new_string .= '&lt;';
  1220. continue;
  1221. }
  1222. }
  1223. // What about gt?
  1224. elseif ($string{$i} == '>')
  1225. {
  1226. // Will it be HTML?
  1227. if ($in_html)
  1228. $in_html = false;
  1229. // Otherwise we need an entity...
  1230. else
  1231. {
  1232. $new_string .= '&gt;';
  1233. continue;
  1234. }
  1235. }
  1236. // Is it a slash? If so escape it...
  1237. if ($string{$i} == '\\')
  1238. $new_string .= '\\';
  1239. // The infamous double quote?
  1240. elseif ($string{$i} == '"')
  1241. {
  1242. // If we're in HTML we leave it as a quote - otherwise we entity it.
  1243. if (!$in_html)
  1244. {
  1245. $new_string .= '&quot;';
  1246. continue;
  1247. }
  1248. }
  1249. // A single quote?
  1250. elseif ($string{$i} == '\'')
  1251. {
  1252. // Must be in a string so escape it.
  1253. $new_string .= '\\';
  1254. }
  1255. // Finally add the character to the string!
  1256. $new_string .= $string{$i};
  1257. }
  1258. // If we ended as a string then close it off.
  1259. if ($in_string == 1)
  1260. $new_string .= '\'';
  1261. elseif ($in_string == 2)
  1262. $new_string .= '"';
  1263. }
  1264. return $new_string;
  1265. }
  1266. ?>