PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/admin/ManageLanguages.php

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