Conditions | 137 |
Paths | > 20000 |
Total Lines | 740 |
Code Lines | 325 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
793 | function ModifyLanguage() |
||
794 | { |
||
795 | global $settings, $context, $smcFunc, $txt, $modSettings, $boarddir, $sourcedir, $language, $cache_enable; |
||
796 | |||
797 | loadLanguage('ManageSettings'); |
||
798 | |||
799 | // Select the languages tab. |
||
800 | $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'edit'; |
||
801 | $context['page_title'] = $txt['edit_languages']; |
||
802 | $context['sub_template'] = 'modify_language_entries'; |
||
803 | |||
804 | $context['lang_id'] = $_GET['lid']; |
||
805 | list($theme_id, $file_id) = empty($_REQUEST['tfid']) || strpos($_REQUEST['tfid'], '+') === false ? array(1, '') : explode('+', $_REQUEST['tfid']); |
||
806 | |||
807 | // Clean the ID - just in case. |
||
808 | preg_match('~([A-Za-z0-9_-]+)~', $context['lang_id'], $matches); |
||
809 | $context['lang_id'] = $matches[1]; |
||
810 | |||
811 | // Get all the theme data. |
||
812 | $request = $smcFunc['db_query']('', ' |
||
813 | SELECT id_theme, variable, value |
||
814 | FROM {db_prefix}themes |
||
815 | WHERE id_theme != {int:default_theme} |
||
816 | AND id_member = {int:no_member} |
||
817 | AND variable IN ({string:name}, {string:theme_dir})', |
||
818 | array( |
||
819 | 'default_theme' => 1, |
||
820 | 'no_member' => 0, |
||
821 | 'name' => 'name', |
||
822 | 'theme_dir' => 'theme_dir', |
||
823 | ) |
||
824 | ); |
||
825 | $themes = array( |
||
826 | 1 => array( |
||
827 | 'name' => $txt['dvc_default'], |
||
828 | 'theme_dir' => $settings['default_theme_dir'], |
||
829 | ), |
||
830 | ); |
||
831 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
832 | $themes[$row['id_theme']][$row['variable']] = $row['value']; |
||
833 | $smcFunc['db_free_result']($request); |
||
834 | |||
835 | // This will be where we look |
||
836 | $lang_dirs = array(); |
||
837 | |||
838 | // There are different kinds of strings |
||
839 | $string_types = array('txt', 'helptxt', 'editortxt', 'tztxt', 'txtBirthdayEmails'); |
||
840 | $additional_string_types = array(); |
||
841 | |||
842 | // Some files allow the admin to add and/or remove certain types of strings |
||
843 | $allows_add_remove = array( |
||
844 | 'Timezones' => array( |
||
845 | 'add' => array('tztxt', 'txt'), |
||
846 | 'remove' => array('tztxt', 'txt'), |
||
847 | ), |
||
848 | 'Modifications' => array( |
||
849 | 'add' => array('txt'), |
||
850 | 'remove' => array('txt'), |
||
851 | ), |
||
852 | 'ThemeStrings' => array( |
||
853 | 'add' => array('txt'), |
||
854 | ), |
||
855 | ); |
||
856 | |||
857 | // Does a hook need to add in some additional places to look for languages or info about how to handle them? |
||
858 | call_integration_hook('integrate_modifylanguages', array(&$themes, &$lang_dirs, &$allows_add_remove, &$additional_string_types)); |
||
859 | |||
860 | $string_types = array_unique(array_merge($string_types, $additional_string_types)); |
||
861 | |||
862 | // Check we have themes with a path and a name - just in case - and add the path. |
||
863 | foreach ($themes as $id => $data) |
||
864 | { |
||
865 | if (count($data) != 2) |
||
866 | unset($themes[$id]); |
||
867 | elseif (is_dir($data['theme_dir'] . '/languages')) |
||
868 | $lang_dirs[$id] = $data['theme_dir'] . '/languages'; |
||
869 | |||
870 | // How about image directories? |
||
871 | if (is_dir($data['theme_dir'] . '/images/' . $context['lang_id'])) |
||
872 | $images_dirs[$id] = $data['theme_dir'] . '/images/' . $context['lang_id']; |
||
873 | } |
||
874 | |||
875 | $current_file = $file_id ? $lang_dirs[$theme_id] . '/' . $file_id . '.' . $context['lang_id'] . '.php' : ''; |
||
876 | |||
877 | // Now for every theme get all the files and stick them in context! |
||
878 | $context['possible_files'] = array(); |
||
879 | foreach ($lang_dirs as $theme => $theme_dir) |
||
880 | { |
||
881 | // Open it up. |
||
882 | $dir = dir($theme_dir); |
||
883 | while ($entry = $dir->read()) |
||
884 | { |
||
885 | // We're only after the files for this language. |
||
886 | if (preg_match('~^([A-Za-z]+)\.' . $context['lang_id'] . '\.php$~', $entry, $matches) == 0) |
||
887 | continue; |
||
888 | |||
889 | if (!isset($context['possible_files'][$theme])) |
||
890 | $context['possible_files'][$theme] = array( |
||
891 | 'id' => $theme, |
||
892 | 'name' => $themes[$theme]['name'], |
||
893 | 'files' => array(), |
||
894 | ); |
||
895 | |||
896 | $context['possible_files'][$theme]['files'][] = array( |
||
897 | 'id' => $matches[1], |
||
898 | 'name' => isset($txt['lang_file_desc_' . $matches[1]]) ? $txt['lang_file_desc_' . $matches[1]] : $matches[1], |
||
899 | 'selected' => $theme_id == $theme && $file_id == $matches[1], |
||
900 | ); |
||
901 | } |
||
902 | $dir->close(); |
||
903 | usort($context['possible_files'][$theme]['files'], function($val1, $val2) |
||
904 | { |
||
905 | return strcmp($val1['name'], $val2['name']); |
||
906 | }); |
||
907 | } |
||
908 | |||
909 | // We no longer wish to speak this language. |
||
910 | if (!empty($_POST['delete_main']) && $context['lang_id'] != 'english') |
||
911 | { |
||
912 | checkSession(); |
||
913 | validateToken('admin-mlang'); |
||
914 | |||
915 | // @todo Todo: FTP Controls? |
||
916 | require_once($sourcedir . '/Subs-Package.php'); |
||
917 | |||
918 | // First, Make a backup? |
||
919 | if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['lang_id'] . '$$$')) |
||
920 | { |
||
921 | $_SESSION['last_backup_for'] = $context['lang_id'] . '$$$'; |
||
922 | $result = package_create_backup('backup_lang_' . $context['lang_id']); |
||
923 | if (!$result) |
||
924 | fatal_lang_error('could_not_language_backup', false); |
||
925 | } |
||
926 | |||
927 | // Second, loop through the array to remove the files. |
||
928 | foreach ($lang_dirs as $curPath) |
||
929 | { |
||
930 | foreach ($context['possible_files'][1]['files'] as $lang) |
||
931 | if (file_exists($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php')) |
||
932 | unlink($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php'); |
||
933 | |||
934 | // Check for the email template. |
||
935 | if (file_exists($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php')) |
||
936 | unlink($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php'); |
||
937 | } |
||
938 | |||
939 | // Third, the agreement file. |
||
940 | if (file_exists($boarddir . '/agreement.' . $context['lang_id'] . '.txt')) |
||
941 | unlink($boarddir . '/agreement.' . $context['lang_id'] . '.txt'); |
||
942 | |||
943 | // Fourth, a related images folder, if it exists... |
||
944 | if (!empty($images_dirs)) |
||
945 | foreach ($images_dirs as $curPath) |
||
946 | if (is_dir($curPath)) |
||
947 | deltree($curPath); |
||
948 | |||
949 | // Members can no longer use this language. |
||
950 | $smcFunc['db_query']('', ' |
||
951 | UPDATE {db_prefix}members |
||
952 | SET lngfile = {empty} |
||
953 | WHERE lngfile = {string:current_language}', |
||
954 | array( |
||
955 | 'empty_string' => '', |
||
956 | 'current_language' => $context['lang_id'], |
||
957 | ) |
||
958 | ); |
||
959 | |||
960 | // Fifth, update getLanguages() cache. |
||
961 | if (!empty($cache_enable)) |
||
962 | { |
||
963 | cache_put_data('known_languages', null, !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600); |
||
964 | } |
||
965 | |||
966 | // Sixth, if we deleted the default language, set us back to english? |
||
967 | if ($context['lang_id'] == $language) |
||
968 | { |
||
969 | require_once($sourcedir . '/Subs-Admin.php'); |
||
970 | $language = 'english'; |
||
971 | updateSettingsFile(array('language' => '\'' . $language . '\'')); |
||
972 | } |
||
973 | |||
974 | // Seventh, get out of here. |
||
975 | redirectexit('action=admin;area=languages;sa=edit;' . $context['session_var'] . '=' . $context['session_id']); |
||
976 | } |
||
977 | |||
978 | // Saving primary settings? |
||
979 | $primary_settings = array('native_name' => 'string', 'lang_character_set' => 'string', 'lang_locale' => 'string', 'lang_rtl' => 'bool', 'lang_dictionary' => 'string', 'lang_spelling' => 'string', 'lang_recaptcha' => 'string'); |
||
980 | $madeSave = false; |
||
981 | if (!empty($_POST['save_main']) && !$current_file) |
||
982 | { |
||
983 | checkSession(); |
||
984 | validateToken('admin-mlang'); |
||
985 | |||
986 | // Read in the current file. |
||
987 | $current_data = implode('', file($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php')); |
||
988 | |||
989 | // Build the replacements. old => new |
||
990 | $replace_array = array(); |
||
991 | foreach ($primary_settings as $setting => $type) |
||
992 | $replace_array['~\$txt\[\'' . $setting . '\'\]\s*=\s*[^\r\n]+~'] = '$txt[\'' . $setting . '\'] = ' . ($type === 'bool' ? (!empty($_POST[$setting]) ? 'true' : 'false') : '\'' . ($setting = 'native_name' ? htmlentities(un_htmlspecialchars($_POST[$setting]), ENT_QUOTES, $context['character_set']) : preg_replace('~[^\w-]~i', '', $_POST[$setting])) . '\'') . ';'; |
||
993 | |||
994 | $current_data = preg_replace(array_keys($replace_array), array_values($replace_array), $current_data); |
||
995 | $fp = fopen($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php', 'w+'); |
||
996 | fwrite($fp, $current_data); |
||
997 | fclose($fp); |
||
998 | |||
999 | $madeSave = true; |
||
1000 | } |
||
1001 | |||
1002 | // Quickly load index language entries. |
||
1003 | $old_txt = $txt; |
||
1004 | require($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'); |
||
1005 | $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'); |
||
1006 | // Setup the primary settings context. |
||
1007 | $context['primary_settings']['name'] = $smcFunc['ucwords'](strtr($context['lang_id'], array('_' => ' ', '-utf8' => ''))); |
||
1008 | foreach ($primary_settings as $setting => $type) |
||
1009 | { |
||
1010 | $context['primary_settings'][$setting] = array( |
||
1011 | 'label' => str_replace('lang_', '', $setting), |
||
1012 | 'value' => $txt[$setting], |
||
1013 | ); |
||
1014 | } |
||
1015 | |||
1016 | // Restore normal service. |
||
1017 | $txt = $old_txt; |
||
1018 | |||
1019 | // Are we saving? |
||
1020 | $save_strings = array(); |
||
1021 | $remove_strings = array(); |
||
1022 | $add_strings = array(); |
||
1023 | if (isset($_POST['save_entries'])) |
||
1024 | { |
||
1025 | checkSession(); |
||
1026 | validateToken('admin-mlang'); |
||
1027 | |||
1028 | if (!empty($_POST['edit'])) |
||
1029 | { |
||
1030 | foreach ($_POST['edit'] as $k => $v) |
||
1031 | { |
||
1032 | if (is_string($v)) |
||
1033 | { |
||
1034 | // Only try to save if 'edit' was specified and if the string has changed |
||
1035 | if ($v == 'edit' && isset($_POST['entry'][$k]) && isset($_POST['comp'][$k]) && $_POST['entry'][$k] != $_POST['comp'][$k]) |
||
1036 | $save_strings[$k] = cleanLangString($_POST['entry'][$k], false); |
||
1037 | |||
1038 | // Record any add or remove requests. We'll decide on them later. |
||
1039 | elseif ($v == 'remove') |
||
1040 | $remove_strings[] = $k; |
||
1041 | elseif ($v == 'add' && isset($_POST['entry'][$k])) |
||
1042 | { |
||
1043 | $add_strings[$k] = array( |
||
1044 | 'group' => isset($_POST['grp'][$k]) ? $_POST['grp'][$k] : 'txt', |
||
1045 | 'string' => cleanLangString($_POST['entry'][$k], false), |
||
1046 | ); |
||
1047 | } |
||
1048 | } |
||
1049 | elseif (is_array($v)) |
||
1050 | { |
||
1051 | foreach ($v as $subk => $subv) |
||
1052 | { |
||
1053 | if ($subv == 'edit' && isset($_POST['entry'][$k][$subk]) && isset($_POST['comp'][$k][$subk]) && $_POST['entry'][$k][$subk] != $_POST['comp'][$k][$subk]) |
||
1054 | $save_strings[$k][$subk] = cleanLangString($_POST['entry'][$k][$subk], false); |
||
1055 | |||
1056 | elseif ($subv == 'remove') |
||
1057 | $remove_strings[$k][] = $subk; |
||
1058 | elseif ($subv == 'add' && isset($_POST['entry'][$k][$subk])) |
||
1059 | { |
||
1060 | $add_strings[$k][$subk] = array( |
||
1061 | 'group' => isset($_POST['grp'][$k]) ? $_POST['grp'][$k] : 'txt', |
||
1062 | 'string' => cleanLangString($_POST['entry'][$k][$subk], false), |
||
1063 | ); |
||
1064 | } |
||
1065 | |||
1066 | } |
||
1067 | } |
||
1068 | } |
||
1069 | } |
||
1070 | } |
||
1071 | |||
1072 | // If we are editing a file work away at that. |
||
1073 | $context['can_add_lang_entry'] = array(); |
||
1074 | if ($current_file) |
||
1075 | { |
||
1076 | $context['entries_not_writable_message'] = is_writable($current_file) ? '' : sprintf($txt['lang_entries_not_writable'], $current_file); |
||
1077 | |||
1078 | // How many strings will PHP let us edit at once? |
||
1079 | // Each string needs 3 inputs, and there are 5 others in the form. |
||
1080 | $context['max_inputs'] = floor(ini_get('max_input_vars') / 3) - 5; |
||
1081 | |||
1082 | // Do we want to override the helptxt for certain types of text variables? |
||
1083 | $special_groups = array( |
||
1084 | 'Timezones' => array('txt' => 'txt_for_timezones'), |
||
1085 | 'EmailTemplates' => array('txt' => 'txt_for_email_templates', 'txtBirthdayEmails' => 'txt_for_email_templates'), |
||
1086 | ); |
||
1087 | call_integration_hook('integrate_language_edit_helptext', array(&$special_groups)); |
||
1088 | |||
1089 | // Determine which groups of strings (if any) allow adding new entries |
||
1090 | if (isset($allows_add_remove[$file_id]['add'])) |
||
1091 | { |
||
1092 | foreach ($allows_add_remove[$file_id]['add'] as $var_group) |
||
1093 | { |
||
1094 | $group = !empty($special_groups[$file_id][$var_group]) ? $special_groups[$file_id][$var_group] : $var_group; |
||
1095 | if (in_array($var_group, $allows_add_remove[$file_id]['add'])) |
||
1096 | $context['can_add_lang_entry'][$group] = true; |
||
1097 | } |
||
1098 | } |
||
1099 | |||
1100 | // Read in the file's contents and process it into entries. |
||
1101 | // Also, remove any lines for uneditable variables like $forum_copyright from the working data. |
||
1102 | $entries = array(); |
||
1103 | foreach (preg_split('~^(?=\$(?:' . implode('|', $string_types) . ')\[\'([^\n]+?)\'\])~m' . ($context['utf8'] ? 'u' : ''), preg_replace('~\s*\n(\$(?!(?:' . implode('|', $string_types) . '))[^\n]*)~', '', file_get_contents($current_file))) as $blob) |
||
1104 | { |
||
1105 | // Comment lines at the end of the blob can make terrible messes |
||
1106 | $blob = preg_replace('~(\n[ \t]*//[^\n]*)*$~' . ($context['utf8'] ? 'u' : ''), '', $blob); |
||
1107 | |||
1108 | // Extract the variable |
||
1109 | if (preg_match('~^\$(' . implode('|', $string_types) . ')\[\'([^\n]+?)\'\](?:\[\'?([^\n]+?)\'?\])?\s?=\s?(.+);([ \t]*(?://[^\n]*)?)$~ms' . ($context['utf8'] ? 'u' : ''), strtr($blob, array("\r" => '')), $matches)) |
||
1110 | { |
||
1111 | // If no valid subkey was found, we need it to be explicitly null |
||
1112 | $matches[3] = isset($matches[3]) && $matches[3] !== '' ? $matches[3] : null; |
||
1113 | |||
1114 | // The point of this exercise |
||
1115 | $entries[$matches[2] . (isset($matches[3]) ? '[' . $matches[3] . ']' : '')] = array( |
||
1116 | 'type' => $matches[1], |
||
1117 | 'group' => !empty($special_groups[$file_id][$matches[1]]) ? $special_groups[$file_id][$matches[1]] : $matches[1], |
||
1118 | 'can_remove' => isset($allows_add_remove[$file_id]['remove']) && in_array($matches[1], $allows_add_remove[$file_id]['remove']), |
||
1119 | 'key' => $matches[2], |
||
1120 | 'subkey' => $matches[3], |
||
1121 | 'full' => $matches[0], |
||
1122 | 'entry' => $matches[4], |
||
1123 | 'cruft' => $matches[5], |
||
1124 | ); |
||
1125 | } |
||
1126 | } |
||
1127 | |||
1128 | // These will be the entries we can definitely save. |
||
1129 | $final_saves = array(); |
||
1130 | |||
1131 | $context['file_entries'] = array(); |
||
1132 | foreach ($entries as $entryKey => $entryValue) |
||
1133 | { |
||
1134 | // Ignore some things we set separately. |
||
1135 | if (in_array($entryKey, array_keys($primary_settings))) |
||
1136 | continue; |
||
1137 | |||
1138 | // These are arrays that need breaking out. |
||
1139 | if (strpos($entryValue['entry'], 'array(') === 0 && strpos($entryValue['entry'], ')', -1) === strlen($entryValue['entry']) - 1) |
||
1140 | { |
||
1141 | // No, you may not use multidimensional arrays of $txt strings. Madness stalks that path. |
||
1142 | if (isset($entryValue['subkey'])) |
||
1143 | continue; |
||
1144 | |||
1145 | // Trim off the array construct bits. |
||
1146 | $entryValue['entry'] = substr($entryValue['entry'], strpos($entryValue['entry'], 'array(') + 6, -1); |
||
1147 | |||
1148 | // This crazy regex extracts each array element, even if the value contains commas or escaped quotes |
||
1149 | // The keys can be either integers or strings |
||
1150 | // The values must be strings, or the regex will fail |
||
1151 | $m = preg_match_all('/ |
||
1152 | # Optional explicit key assignment |
||
1153 | (?: |
||
1154 | (?: |
||
1155 | \d+ |
||
1156 | | |
||
1157 | (?: |
||
1158 | (?: |
||
1159 | \'(?:[^\']|(?<=\\\)\')*\' |
||
1160 | ) |
||
1161 | | |
||
1162 | (?: |
||
1163 | "(?:[^"]|(?<=\\\)")*" |
||
1164 | ) |
||
1165 | ) |
||
1166 | ) |
||
1167 | \s*=>\s* |
||
1168 | )? |
||
1169 | |||
1170 | # String value in single or double quotes |
||
1171 | (?: |
||
1172 | (?: |
||
1173 | \'(?:[^\']|(?<=\\\)\')*\' |
||
1174 | ) |
||
1175 | | |
||
1176 | (?: |
||
1177 | "(?:[^"]|(?<=\\\)")*" |
||
1178 | ) |
||
1179 | ) |
||
1180 | |||
1181 | # Followed by a comma or the end of the string |
||
1182 | (?=,|$) |
||
1183 | |||
1184 | /x' . ($context['utf8'] ? 'u' : ''), $entryValue['entry'], $matches); |
||
1185 | |||
1186 | if (empty($m)) |
||
1187 | continue; |
||
1188 | |||
1189 | $entryValue['entry'] = $matches[0]; |
||
1190 | |||
1191 | // Now create an entry for each item. |
||
1192 | $cur_index = -1; |
||
1193 | $save_cache = array( |
||
1194 | 'enabled' => false, |
||
1195 | 'entries' => array(), |
||
1196 | ); |
||
1197 | foreach ($entryValue['entry'] as $id => $subValue) |
||
1198 | { |
||
1199 | // Is this a new index? |
||
1200 | if (preg_match('~^(\d+|(?:(?:\'(?:[^\']|(?<=\\\)\')*\')|(?:"(?:[^"]|(?<=\\\)")*")))\s*=>~', $subValue, $matches)) |
||
1201 | { |
||
1202 | $subKey = trim($matches[1], '\'"'); |
||
1203 | |||
1204 | if (ctype_digit($subKey)) |
||
1205 | $cur_index = $subKey; |
||
1206 | |||
1207 | $subValue = trim(substr($subValue, strpos($subValue, '=>') + 2)); |
||
1208 | } |
||
1209 | else |
||
1210 | $subKey = ++$cur_index; |
||
1211 | |||
1212 | // Clean up some bits. |
||
1213 | if (strpos($subValue, '\'') === 0) |
||
1214 | $subValue = trim($subValue, '\''); |
||
1215 | elseif (strpos($subValue, '"') === 0) |
||
1216 | $subValue = trim($subValue, '"'); |
||
1217 | |||
1218 | // Can we save? |
||
1219 | if (isset($save_strings[$entryKey][$subKey])) |
||
1220 | { |
||
1221 | $save_cache['entries'][$subKey] = strtr($save_strings[$entryKey][$subKey], array('\'' => '')); |
||
1222 | $save_cache['enabled'] = true; |
||
1223 | } |
||
1224 | // Should we remove this one? |
||
1225 | elseif (isset($remove_strings[$entryKey]) && in_array($subKey, $remove_strings[$entryKey]) && $entryValue['can_remove']) |
||
1226 | $save_cache['enabled'] = true; |
||
1227 | // Just keep this one as it is |
||
1228 | else |
||
1229 | $save_cache['entries'][$subKey] = $subValue; |
||
1230 | |||
1231 | $context['file_entries'][$entryValue['group']][] = array( |
||
1232 | 'key' => $entryKey, |
||
1233 | 'subkey' => $subKey, |
||
1234 | 'value' => $subValue, |
||
1235 | 'rows' => 1, |
||
1236 | 'can_remove' => $entryValue['can_remove'], |
||
1237 | ); |
||
1238 | } |
||
1239 | |||
1240 | // Should we add a new string to this array? |
||
1241 | if (!empty($context['can_add_lang_entry'][$entryValue['type']]) && isset($add_strings[$entryKey])) |
||
1242 | { |
||
1243 | foreach ($add_strings[$entryKey] as $string_key => $string_val) |
||
1244 | $save_cache['entries'][$string_key] = strtr($string_val['string'], array('\'' => '')); |
||
1245 | |||
1246 | $save_cache['enabled'] = true; |
||
1247 | |||
1248 | // Make sure we don't add this again as an independent line |
||
1249 | unset($add_strings[$entryKey][$string_key]); |
||
1250 | if (empty($add_strings[$entryKey])) |
||
1251 | unset($add_strings[$entryKey]); |
||
1252 | } |
||
1253 | |||
1254 | // Do we need to save? |
||
1255 | if ($save_cache['enabled']) |
||
1256 | { |
||
1257 | // Format the string, checking the indexes first. |
||
1258 | $items = array(); |
||
1259 | $cur_index = 0; |
||
1260 | foreach ($save_cache['entries'] as $k2 => $v2) |
||
1261 | { |
||
1262 | // Manually show the custom index. |
||
1263 | if ($k2 != $cur_index) |
||
1264 | { |
||
1265 | $items[] = $k2 . ' => \'' . $v2 . '\''; |
||
1266 | $cur_index = $k2; |
||
1267 | } |
||
1268 | else |
||
1269 | $items[] = '\'' . $v2 . '\''; |
||
1270 | |||
1271 | $cur_index++; |
||
1272 | } |
||
1273 | // Now create the string! |
||
1274 | $final_saves[$entryKey] = array( |
||
1275 | 'find' => $entryValue['full'], |
||
1276 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = array(' . implode(', ', $items) . ');' . $entryValue['cruft'], |
||
1277 | ); |
||
1278 | } |
||
1279 | } |
||
1280 | // A single array element, like: $txt['foo']['bar'] = 'baz'; |
||
1281 | elseif (isset($entryValue['subkey'])) |
||
1282 | { |
||
1283 | // Saving? |
||
1284 | if (isset($save_strings[$entryValue['key']][$entryValue['subkey']]) && $save_strings[$entryValue['key']][$entryValue['subkey']] != $entryValue['entry']) |
||
1285 | { |
||
1286 | if ($save_strings[$entryValue['key']][$entryValue['subkey']] == '') |
||
1287 | $save_strings[$entryValue['key']][$entryValue['subkey']] = '\'\''; |
||
1288 | |||
1289 | // Preserve subkey as either digit or string |
||
1290 | $subKey = ctype_digit($entryValue['subkey']) ? $entryValue['subkey'] : '\'' . $entryValue['subkey'] . '\''; |
||
1291 | |||
1292 | // We have a new value, so we should use it |
||
1293 | $entryValue['entry'] = $save_strings[$entryValue['key']][$entryValue['subkey']]; |
||
1294 | |||
1295 | // And save it |
||
1296 | $final_saves[$entryKey] = array( |
||
1297 | 'find' => $entryValue['full'], |
||
1298 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryValue['key'] . '\'][' . $subKey . '] = ' . $save_strings[$entryValue['key']][$entryValue['subkey']] . ';' . $entryValue['cruft'], |
||
1299 | ); |
||
1300 | } |
||
1301 | |||
1302 | // Remove this entry only if it is allowed |
||
1303 | if (isset($remove_strings[$entryValue['key']]) && in_array($entryValue['subkey'], $remove_strings[$entryValue['key']]) && $entryValue['can_remove']) |
||
1304 | { |
||
1305 | $entryValue['entry'] = '\'\''; |
||
1306 | $final_saves[$entryKey] = array( |
||
1307 | 'find' => $entryValue['full'], |
||
1308 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n", |
||
1309 | ); |
||
1310 | } |
||
1311 | |||
1312 | $editing_string = cleanLangString($entryValue['entry'], true); |
||
1313 | $context['file_entries'][$entryValue['group']][] = array( |
||
1314 | 'key' => $entryValue['key'], |
||
1315 | 'subkey' => $entryValue['subkey'], |
||
1316 | 'value' => $editing_string, |
||
1317 | 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, |
||
1318 | 'can_remove' => $entryValue['can_remove'], |
||
1319 | ); |
||
1320 | } |
||
1321 | // A simple string entry |
||
1322 | else |
||
1323 | { |
||
1324 | // Saving? |
||
1325 | if (isset($save_strings[$entryValue['key']]) && $save_strings[$entryValue['key']] != $entryValue['entry']) |
||
1326 | { |
||
1327 | // @todo Fix this properly. |
||
1328 | if ($save_strings[$entryValue['key']] == '') |
||
1329 | $save_strings[$entryValue['key']] = '\'\''; |
||
1330 | |||
1331 | // Set the new value. |
||
1332 | $entryValue['entry'] = $save_strings[$entryValue['key']]; |
||
1333 | // And we know what to save now! |
||
1334 | $final_saves[$entryKey] = array( |
||
1335 | 'find' => $entryValue['full'], |
||
1336 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryValue['key'] . '\'] = ' . $save_strings[$entryValue['key']] . ';' . $entryValue['cruft'], |
||
1337 | ); |
||
1338 | } |
||
1339 | // Remove this entry only if it is allowed |
||
1340 | if (in_array($entryValue['key'], $remove_strings) && $entryValue['can_remove']) |
||
1341 | { |
||
1342 | $entryValue['entry'] = '\'\''; |
||
1343 | $final_saves[$entryKey] = array( |
||
1344 | 'find' => $entryValue['full'], |
||
1345 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n", |
||
1346 | ); |
||
1347 | } |
||
1348 | |||
1349 | $editing_string = cleanLangString($entryValue['entry'], true); |
||
1350 | $context['file_entries'][$entryValue['group']][] = array( |
||
1351 | 'key' => $entryValue['key'], |
||
1352 | 'subkey' => null, |
||
1353 | 'value' => $editing_string, |
||
1354 | 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, |
||
1355 | 'can_remove' => $entryValue['can_remove'], |
||
1356 | ); |
||
1357 | } |
||
1358 | } |
||
1359 | |||
1360 | // Do they want to add some brand new strings? Does this file allow that? |
||
1361 | if (!empty($add_strings) && !empty($allows_add_remove[$file_id]['add'])) |
||
1362 | { |
||
1363 | $special_types = isset($special_groups[$file_id]) ? array_flip($special_groups[$file_id]) : array(); |
||
1364 | |||
1365 | foreach ($add_strings as $string_key => $string_val) |
||
1366 | { |
||
1367 | // Adding a normal string |
||
1368 | if (isset($string_val['string']) && is_string($string_val['string'])) |
||
1369 | { |
||
1370 | $type = isset($special_types[$string_val['group']]) ? $special_types[$string_val['group']] : $string_val['group']; |
||
1371 | |||
1372 | if (empty($context['can_add_lang_entry'][$type])) |
||
1373 | continue; |
||
1374 | |||
1375 | $final_saves[$string_key] = array( |
||
1376 | 'find' => "\s*\?" . '>$', |
||
1377 | 'replace' => "\n\$" . $type . '[\'' . $string_key . '\'] = ' . $string_val['string'] . ';' . "\n\n?" . '>', |
||
1378 | 'is_regex' => true, |
||
1379 | ); |
||
1380 | } |
||
1381 | // Adding an array element |
||
1382 | else |
||
1383 | { |
||
1384 | foreach ($string_val as $substring_key => $substring_val) |
||
1385 | { |
||
1386 | $type = isset($special_types[$substring_val['group']]) ? $special_types[$substring_val['group']] : $substring_val['group']; |
||
1387 | |||
1388 | if (empty($context['can_add_lang_entry'][$type])) |
||
1389 | continue; |
||
1390 | |||
1391 | $subKey = ctype_digit(trim($substring_key, '\'')) ? trim($substring_key, '\'') : '\'' . $substring_key . '\''; |
||
1392 | |||
1393 | $final_saves[$string_key . '[' . $substring_key . ']'] = array( |
||
1394 | 'find' => "\s*\?" . '>$', |
||
1395 | 'replace' => "\n\$" . $type . '[\'' . $string_key . '\'][' . $subKey . '] = ' . $substring_val['string'] . ';' . "\n\n?" . '>', |
||
1396 | 'is_regex' => true, |
||
1397 | ); |
||
1398 | } |
||
1399 | } |
||
1400 | } |
||
1401 | } |
||
1402 | |||
1403 | // Any saves to make? |
||
1404 | if (!empty($final_saves)) |
||
1405 | { |
||
1406 | checkSession(); |
||
1407 | |||
1408 | // Get a fresh copy of the file's current content. |
||
1409 | $file_contents = file_get_contents($current_file); |
||
1410 | |||
1411 | // Apply our changes. |
||
1412 | foreach ($final_saves as $save) |
||
1413 | { |
||
1414 | if (!empty($save['is_regex'])) |
||
1415 | $file_contents = preg_replace('~' . $save['find'] . '~' . ($context['utf8'] ? 'u' : ''), $save['replace'], $file_contents); |
||
1416 | else |
||
1417 | $file_contents = str_replace($save['find'], $save['replace'], $file_contents); |
||
1418 | } |
||
1419 | |||
1420 | // Save the result back to the file. |
||
1421 | file_put_contents($current_file, $file_contents); |
||
1422 | |||
1423 | $madeSave = true; |
||
1424 | } |
||
1425 | |||
1426 | // Another restore. |
||
1427 | $txt = $old_txt; |
||
1428 | |||
1429 | // If they can add new language entries, make sure the UI is set up for that. |
||
1430 | if (!empty($context['can_add_lang_entry'])) |
||
1431 | { |
||
1432 | // Make sure the Add button has a place to show up. |
||
1433 | foreach ($context['can_add_lang_entry'] as $group => $value) |
||
1434 | { |
||
1435 | if (!isset($context['file_entries'][$group])) |
||
1436 | $context['file_entries'][$group] = array(); |
||
1437 | } |
||
1438 | |||
1439 | addInlineJavaScript(' |
||
1440 | function add_lang_entry(group) { |
||
1441 | var key = prompt("' . $txt['languages_enter_key'] . '"); |
||
1442 | |||
1443 | if (key !== null) { |
||
1444 | ++entry_num; |
||
1445 | |||
1446 | var array_regex = /^(.*)(\[[^\[\]]*\])$/ |
||
1447 | var result = array_regex.exec(key); |
||
1448 | if (result != null) { |
||
1449 | key = result[1]; |
||
1450 | var subkey = result[2]; |
||
1451 | } else { |
||
1452 | var subkey = ""; |
||
1453 | } |
||
1454 | |||
1455 | var bracket_regex = /[\[\]]/ |
||
1456 | if (bracket_regex.test(key)) { |
||
1457 | alert("' . $txt['languages_invalid_key'] . '" + key + subkey); |
||
1458 | return; |
||
1459 | } |
||
1460 | |||
1461 | $("#language_" + group).append("<dt><span>" + key + subkey + "</span></dt> <dd id=\"entry_" + entry_num + "\"><input id=\"entry_" + entry_num + "_edit\" class=\"entry_toggle\" type=\"checkbox\" name=\"edit[" + key + "]" + subkey + "\" value=\"add\" data-target=\"#entry_" + entry_num + "\" checked> <label for=\"entry_" + entry_num + "_edit\">' . $txt['edit'] . '</label> <input type=\"hidden\" class=\"entry_oldvalue\" name=\"grp[" + key + "]\" value=\"" + group + "\"> <textarea name=\"entry[" + key + "]" + subkey + "\" class=\"entry_textfield\" cols=\"40\" rows=\"1\" style=\"width: 96%; margin-bottom: 2em;\"></textarea></dd>"); |
||
1462 | } |
||
1463 | };'); |
||
1464 | |||
1465 | addInlineJavaScript(' |
||
1466 | $(".add_lang_entry_button").show();', true); |
||
1467 | } |
||
1468 | |||
1469 | // Warn them if they try to submit more changes than the server can accept in a single request. |
||
1470 | // Also make it obvious that they cannot submit changes to both the primary settings and the entries at the same time. |
||
1471 | if (!empty($context['file_entries'])) |
||
1472 | { |
||
1473 | addInlineJavaScript(' |
||
1474 | max_inputs = ' . $context['max_inputs'] . '; |
||
1475 | num_inputs = 0; |
||
1476 | |||
1477 | $(".entry_textfield").prop("disabled", true); |
||
1478 | $(".entry_oldvalue").prop("disabled", true); |
||
1479 | |||
1480 | $(".entry_toggle").click(function() { |
||
1481 | var target_dd = $( $(this).data("target") ); |
||
1482 | |||
1483 | if ($(this).prop("checked") === true && $(this).val() === "edit") { |
||
1484 | if (++num_inputs <= max_inputs) { |
||
1485 | target_dd.find(".entry_oldvalue, .entry_textfield").prop("disabled", false); |
||
1486 | } else { |
||
1487 | alert("' . sprintf($txt['languages_max_inputs_warning'], $context['max_inputs']) . '"); |
||
1488 | $(this).prop("checked", false); |
||
1489 | } |
||
1490 | } else { |
||
1491 | --num_inputs; |
||
1492 | target_dd.find(".entry_oldvalue, .entry_textfield").prop("disabled", true); |
||
1493 | } |
||
1494 | |||
1495 | if (num_inputs > 0) { |
||
1496 | $("#primary_settings").trigger("reset"); |
||
1497 | $("#primary_settings input").prop("disabled", true); |
||
1498 | } else { |
||
1499 | $("#primary_settings input").prop("disabled", false); |
||
1500 | } |
||
1501 | }); |
||
1502 | |||
1503 | $("#primary_settings input").change(function() { |
||
1504 | num_changed = 0; |
||
1505 | $("#primary_settings input:text").each(function(i, e) { |
||
1506 | if ($(e).data("orig") != $(e).val()) |
||
1507 | num_changed++; |
||
1508 | }); |
||
1509 | $("#primary_settings input:checkbox").each(function(i, e) { |
||
1510 | cur_val = $(e).is(":checked"); |
||
1511 | orig_val = $(e).val == "true"; |
||
1512 | if (cur_val != orig_val) |
||
1513 | num_changed++; |
||
1514 | }); |
||
1515 | |||
1516 | if (num_changed > 0) { |
||
1517 | $("#entry_fields").fadeOut(); |
||
1518 | } else { |
||
1519 | $("#entry_fields").fadeIn(); |
||
1520 | } |
||
1521 | }); |
||
1522 | $("#reset_main").click(function() { |
||
1523 | $("#entry_fields").fadeIn(); |
||
1524 | });', true); |
||
1525 | } |
||
1526 | } |
||
1527 | |||
1528 | // If we saved, redirect. |
||
1529 | if ($madeSave) |
||
1530 | redirectexit('action=admin;area=languages;sa=editlang;lid=' . $context['lang_id'] . (!empty($file_id) ? ';entries;tfid=' . $theme_id . rawurlencode('+') . $file_id : '')); |
||
1531 | |||
1532 | createToken('admin-mlang'); |
||
1533 | } |
||
1746 | ?> |