| @@ -1,0 +1,225 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers; | |
| 4 | + | |
| 5 | +use AcyMailing\Helpers\MailerHelper; | |
| 6 | +use AcyMailing\Helpers\UpdateHelper; | |
| 7 | +use AcyMailing\Helpers\UpdatemeHelper; | |
| 8 | +use AcyMailing\Core\AcymController; | |
| 9 | + | |
| 10 | +class LanguageController extends AcymController | |
| 11 | +{ | |
| 12 | + public function saveLanguage(bool $fromShare = false): bool | |
| 13 | + { | |
| 14 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 15 | + | |
| 16 | + $code = acym_getVar('cmd', 'code'); | |
| 17 | + acym_setVar('code', $code); | |
| 18 | + | |
| 19 | + // Get content that can be modified when loading the latest version from our site | |
| 20 | + $content = acym_getVar('string', 'content', '', '', ACYM_ALLOWRAW); | |
| 21 | + $content = str_replace('</textarea>', '', $content); | |
| 22 | + | |
| 23 | + if (empty($code) || empty($content)) { | |
| 24 | + $this->displayLanguage(); | |
| 25 | + | |
| 26 | + return true; | |
| 27 | + } | |
| 28 | + | |
| 29 | + // Get the custom translations | |
| 30 | + $customcontent = acym_getVar('string', 'customcontent', '', '', ACYM_ALLOWRAW); | |
| 31 | + $customcontent = str_replace('</textarea>', '', $customcontent); | |
| 32 | + | |
| 33 | + // We have a code, we have a content... so we can simply save the file! | |
| 34 | + $path = acym_getLanguagePath(ACYM_ROOT, $code).DS.$code.'.com_acym.ini'; | |
| 35 | + $result = acym_writeFile($path, $content); | |
| 36 | + if ($result) { | |
| 37 | + acym_enqueueMessage(acym_translation('ACYM_SUCCESSFULLY_SAVED'), 'success'); | |
| 38 | + //We update the picture from "add" to "edit" | |
| 39 | + $js = 'let langIcon = window.top.document.getElementById("image'.$code.'"); langIcon.className = langIcon.className.replace("acymicon-add", "") + " acymicon-edit"'; | |
| 40 | + acym_addScript(true, $js); | |
| 41 | + | |
| 42 | + //Now we will also create a menu language file and save it | |
| 43 | + $updateHelper = new UpdateHelper(); | |
| 44 | + $updateHelper->installBackLanguages($code); | |
| 45 | + } else { | |
| 46 | + acym_enqueueMessage(acym_translationSprintf('ACYM_FAIL_SAVE_FILE', $path), 'error'); | |
| 47 | + } | |
| 48 | + | |
| 49 | + // Let's save the custom language file now... | |
| 50 | + $custompath = acym_getLanguagePath(ACYM_ROOT, $code).DS.$code.'.com_acym_custom.ini'; | |
| 51 | + $customresult = acym_writeFile($custompath, $customcontent); | |
| 52 | + if (!$customresult) { | |
| 53 | + acym_enqueueMessage(acym_translationSprintf('ACYM_FAIL_SAVE_FILE', $custompath), 'error'); | |
| 54 | + } | |
| 55 | + | |
| 56 | + if ($code == acym_getLanguageTag()) { | |
| 57 | + acym_loadLanguage(); | |
| 58 | + } | |
| 59 | + | |
| 60 | + //We add lang to menu | |
| 61 | + $updateHelper = new UpdateHelper(); | |
| 62 | + $updateHelper->installBackLanguages(); | |
| 63 | + | |
| 64 | + if ($fromShare) { | |
| 65 | + return $result; | |
| 66 | + } else { | |
| 67 | + $this->displayLanguage(); | |
| 68 | + | |
| 69 | + return true; | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + public function share(): void | |
| 74 | + { | |
| 75 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 76 | + | |
| 77 | + if ($this->saveLanguage(true)) { | |
| 78 | + acym_setVar('layout', 'share'); | |
| 79 | + | |
| 80 | + $file = new \stdClass(); | |
| 81 | + $file->name = acym_getVar('cmd', 'code'); | |
| 82 | + | |
| 83 | + parent::display(['file' => $file]); | |
| 84 | + } else { | |
| 85 | + $this->displayLanguage(); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function send(): void | |
| 90 | + { | |
| 91 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 92 | + | |
| 93 | + $bodyEmail = acym_getVar('string', 'mailbody'); | |
| 94 | + $code = acym_getVar('cmd', 'code'); | |
| 95 | + acym_setVar('code', $code); | |
| 96 | + | |
| 97 | + if (empty($code)) { | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $mailerHelper = new MailerHelper(); | |
| 102 | + $mailerHelper->Subject = '[ACYMAILING LANGUAGE FILE] '.$code; | |
| 103 | + $mailerHelper->Body = 'The website '.ACYM_LIVE.' using AcyMailing '.$this->config->get('level').' '.$this->config->get('version').' sent a language file : '.$code; | |
| 104 | + $mailerHelper->Body .= "\n\n\n".$bodyEmail; | |
| 105 | + | |
| 106 | + //Include the extra language file.... | |
| 107 | + $file = acym_getLanguagePath(ACYM_ROOT, $code).DS.$code.'.com_acym.ini'; | |
| 108 | + if (!file_exists($file)) { | |
| 109 | + return; | |
| 110 | + } | |
| 111 | + | |
| 112 | + $translation = acym_fileGetContent($file); | |
| 113 | + | |
| 114 | + // Include the custom translations | |
| 115 | + $customFile = acym_getLanguagePath(ACYM_ROOT, $code).DS.$code.'.com_acym_custom.ini'; | |
| 116 | + | |
| 117 | + if (file_exists($customFile)) { | |
| 118 | + $customTranslation = acym_fileGetContent($customFile); | |
| 119 | + | |
| 120 | + if (!empty($customTranslation)) { | |
| 121 | + // Replace translations in the main lang file by the custom ones if they exist | |
| 122 | + $newKeys = []; | |
| 123 | + $customKeys = []; | |
| 124 | + preg_match_all('#([0-9A-Z_]+)="((?:[^"]|"_QQ_")+)"#is', $customTranslation, $customKeys); | |
| 125 | + | |
| 126 | + if (!empty($customKeys)) { | |
| 127 | + $mainKeys = []; | |
| 128 | + preg_match_all('#([0-9A-Z_]+)="((?:[^"]|"_QQ_")+)"#is', $translation, $mainKeys); | |
| 129 | + | |
| 130 | + foreach ($customKeys[1] as $index => $oneKey) { | |
| 131 | + $position = array_search($oneKey, $mainKeys[1]); | |
| 132 | + if ($position !== false) { | |
| 133 | + // Replace the translation in the main file | |
| 134 | + $translation = str_replace($mainKeys[0][$position], $customKeys[0][$index], $translation); | |
| 135 | + } else { | |
| 136 | + // Add the additional translation to the email body, it may be useful somehow | |
| 137 | + $newKeys[] = $customKeys[0][$index]; | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + if (!empty($newKeys)) { | |
| 142 | + $mailerHelper->Body .= "\n\n\nCustom content:\n".implode("\n", $newKeys); | |
| 143 | + } | |
| 144 | + } | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + // Attach the file | |
| 149 | + $mailerHelper->addStringAttachment($translation, $code.'.com_acym.ini'); | |
| 150 | + | |
| 151 | + $mailerHelper->AddAddress(acym_currentUserEmail(), acym_currentUserName()); | |
| 152 | + $mailerHelper->AddAddress('[email protected]', 'Acyba Translation Team'); | |
| 153 | + $mailerHelper->report = false; | |
| 154 | + | |
| 155 | + $result = $mailerHelper->send(); | |
| 156 | + | |
| 157 | + if ($result) { | |
| 158 | + acym_enqueueMessage(acym_translation('ACYM_THANK_YOU_SHARING').'<br>'.acym_translation('ACYM_MESSAGE_SENT'), 'success'); | |
| 159 | + } else { | |
| 160 | + acym_enqueueMessage($mailerHelper->reportMessage, 'error'); | |
| 161 | + } | |
| 162 | + | |
| 163 | + $this->displayLanguage(); | |
| 164 | + } | |
| 165 | + | |
| 166 | + public function displayLanguage(): void | |
| 167 | + { | |
| 168 | + acym_setVar('layout', 'default'); | |
| 169 | + | |
| 170 | + $code = acym_getVar('string', 'code'); | |
| 171 | + if (empty($code)) { | |
| 172 | + acym_display(acym_translation('ACYM_LANGUAGE_CODE_NOT_FOUND'), 'error'); | |
| 173 | + | |
| 174 | + return; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $file = new \stdClass(); | |
| 178 | + $file->name = $code; | |
| 179 | + $path = acym_getLanguagePath(ACYM_ROOT, $code).DS.$code.'.com_acym.ini'; | |
| 180 | + $file->path = $path; | |
| 181 | + $file->customcontent = ''; | |
| 182 | + | |
| 183 | + if (file_exists($path)) { | |
| 184 | + $file->content = acym_fileGetContent($path); | |
| 185 | + if (empty($file->content)) { | |
| 186 | + acym_display(acym_translationSprintf('ACYM_FILE_NOT_FOUND', $path), 'error'); | |
| 187 | + } | |
| 188 | + } else { | |
| 189 | + // Load the default language | |
| 190 | + if (ACYM_CMS === 'joomla') { | |
| 191 | + $message = acym_translation('ACYM_LOAD_ENGLISH_1'); | |
| 192 | + $message .= '<br />'.acym_translation('ACYM_LOAD_ENGLISH_2'); | |
| 193 | + $message .= '<br />'.acym_translation('ACYM_LOAD_ENGLISH_3'); | |
| 194 | + acym_enqueueMessage($message, 'info'); | |
| 195 | + } | |
| 196 | + $file->content = acym_fileGetContent(acym_getLanguagePath(ACYM_ROOT, ACYM_DEFAULT_LANGUAGE).DS.ACYM_DEFAULT_LANGUAGE.'.com_acym.ini'); | |
| 197 | + } | |
| 198 | + | |
| 199 | + $customPath = acym_getLanguagePath(ACYM_ROOT, $code).DS.$code.'.com_acym_custom.ini'; | |
| 200 | + if (file_exists($customPath)) { | |
| 201 | + $file->customcontent = acym_fileGetContent($customPath); | |
| 202 | + } | |
| 203 | + | |
| 204 | + if (!file_exists(acym_getLanguagePath(ACYM_ROOT, $code))) { | |
| 205 | + acym_enqueueMessage(acym_translationSprintf('ACYM_LANGUAGE_NOT_INSTALLED', $code), 'warning'); | |
| 206 | + } | |
| 207 | + | |
| 208 | + $data = [ | |
| 209 | + 'file' => $file, | |
| 210 | + ]; | |
| 211 | + | |
| 212 | + parent::display($data); | |
| 213 | + } | |
| 214 | + | |
| 215 | + public function getLatestTranslationAjax(): void | |
| 216 | + { | |
| 217 | + $languagesContent = UpdatemeHelper::call('public/download/translations?version=latest&codes='.acym_getVar('cmd', 'code')); | |
| 218 | + | |
| 219 | + if (empty($languagesContent['translations'])) { | |
| 220 | + acym_sendAjaxResponse(acym_translation('ACYM_ERROR_LOAD_LATEST_TRANSLATION'), [], false); | |
| 221 | + } | |
| 222 | + | |
| 223 | + acym_sendAjaxResponse('', ['translations' => array_pop($languagesContent['translations'])]); | |
| 224 | + } | |
| 225 | +} | |