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