[0] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @package Dotclear |
---|
| 4 | * @subpackage Backend |
---|
| 5 | * |
---|
| 6 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 7 | * @copyright GPL-2.0-only |
---|
| 8 | */ |
---|
[0] | 9 | |
---|
[3699] | 10 | require dirname(__FILE__) . '/../inc/admin/prepend.php'; |
---|
[0] | 11 | |
---|
| 12 | dcPage::checkSuper(); |
---|
| 13 | |
---|
| 14 | $is_writable = is_dir(DC_L10N_ROOT) && is_writable(DC_L10N_ROOT); |
---|
[3699] | 15 | $iso_codes = l10n::getISOCodes(); |
---|
[0] | 16 | |
---|
| 17 | # Get languages list on Dotclear.net |
---|
[3699] | 18 | $dc_langs = false; |
---|
[0] | 19 | $feed_reader = new feedReader; |
---|
| 20 | $feed_reader->setCacheDir(DC_TPL_CACHE); |
---|
| 21 | $feed_reader->setTimeout(5); |
---|
| 22 | $feed_reader->setUserAgent('Dotclear - http://www.dotclear.org/'); |
---|
| 23 | try { |
---|
[3699] | 24 | $dc_langs = $feed_reader->parse(sprintf(DC_L10N_UPDATE_URL, DC_VERSION)); |
---|
| 25 | if ($dc_langs !== false) { |
---|
| 26 | $dc_langs = $dc_langs->items; |
---|
| 27 | } |
---|
[0] | 28 | } catch (Exception $e) {} |
---|
| 29 | |
---|
[3491] | 30 | # Language installation function |
---|
[3699] | 31 | $lang_install = function ($file) { |
---|
| 32 | $zip = new fileUnzip($file); |
---|
| 33 | $zip->getList(false, '#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#'); |
---|
[3491] | 34 | |
---|
[3699] | 35 | if (!preg_match('/^[a-z]{2,3}(-[a-z]{2})?$/', $zip->getRootDir())) { |
---|
| 36 | throw new Exception(__('Invalid language zip file.')); |
---|
| 37 | } |
---|
[3491] | 38 | |
---|
[3699] | 39 | if ($zip->isEmpty() || !$zip->hasFile($zip->getRootDir() . '/main.po')) { |
---|
| 40 | throw new Exception(__('The zip file does not appear to be a valid Dotclear language pack.')); |
---|
| 41 | } |
---|
[3491] | 42 | |
---|
[3699] | 43 | $target = dirname($file); |
---|
| 44 | $destination = $target . '/' . $zip->getRootDir(); |
---|
| 45 | $res = 1; |
---|
[3491] | 46 | |
---|
[3699] | 47 | if (is_dir($destination)) { |
---|
| 48 | if (!files::deltree($destination)) { |
---|
| 49 | throw new Exception(__('An error occurred during language upgrade.')); |
---|
| 50 | } |
---|
| 51 | $res = 2; |
---|
| 52 | } |
---|
[3491] | 53 | |
---|
[3699] | 54 | $zip->unzipAll($target); |
---|
| 55 | return $res; |
---|
[3491] | 56 | }; |
---|
| 57 | |
---|
[0] | 58 | # Delete a language pack |
---|
[3699] | 59 | if ($is_writable && !empty($_POST['delete']) && !empty($_POST['locale_id'])) { |
---|
| 60 | try |
---|
| 61 | { |
---|
| 62 | $locale_id = $_POST['locale_id']; |
---|
| 63 | if (!isset($iso_codes[$locale_id]) || !is_dir(DC_L10N_ROOT . '/' . $locale_id)) { |
---|
| 64 | throw new Exception(__('No such installed language')); |
---|
| 65 | } |
---|
[2566] | 66 | |
---|
[3699] | 67 | if ($locale_id == 'en') { |
---|
| 68 | throw new Exception(__("You can't remove English language.")); |
---|
| 69 | } |
---|
[2566] | 70 | |
---|
[3699] | 71 | if (!files::deltree(DC_L10N_ROOT . '/' . $locale_id)) { |
---|
| 72 | throw new Exception(__('Permissions to delete language denied.')); |
---|
| 73 | } |
---|
[2566] | 74 | |
---|
[3699] | 75 | dcPage::addSuccessNotice(__('Language has been successfully deleted.')); |
---|
| 76 | $core->adminurl->redirect("admin.langs"); |
---|
| 77 | } catch (Exception $e) { |
---|
| 78 | $core->error->add($e->getMessage()); |
---|
| 79 | } |
---|
[0] | 80 | } |
---|
| 81 | |
---|
| 82 | # Download a language pack |
---|
[3699] | 83 | if ($is_writable && !empty($_POST['pkg_url'])) { |
---|
| 84 | try |
---|
| 85 | { |
---|
| 86 | if (empty($_POST['your_pwd']) || !$core->auth->checkPassword($_POST['your_pwd'])) { |
---|
| 87 | throw new Exception(__('Password verification failed')); |
---|
| 88 | } |
---|
[2566] | 89 | |
---|
[3699] | 90 | $url = html::escapeHTML($_POST['pkg_url']); |
---|
| 91 | $dest = DC_L10N_ROOT . '/' . basename($url); |
---|
| 92 | if (!preg_match('#^http://[^.]+\.dotclear\.(net|org)/.*\.zip$#', $url)) { |
---|
| 93 | throw new Exception(__('Invalid language file URL.')); |
---|
| 94 | } |
---|
[2566] | 95 | |
---|
[3699] | 96 | $client = netHttp::initClient($url, $path); |
---|
| 97 | $client->setUserAgent('Dotclear - http://www.dotclear.org/'); |
---|
| 98 | $client->useGzip(false); |
---|
| 99 | $client->setPersistReferers(false); |
---|
| 100 | $client->setOutput($dest); |
---|
| 101 | $client->get($path); |
---|
[2566] | 102 | |
---|
[3699] | 103 | try { |
---|
| 104 | $ret_code = $lang_install($dest); |
---|
| 105 | } catch (Exception $e) { |
---|
| 106 | @unlink($dest); |
---|
| 107 | throw $e; |
---|
| 108 | } |
---|
[2566] | 109 | |
---|
[3699] | 110 | @unlink($dest); |
---|
| 111 | if ($ret_code == 2) { |
---|
| 112 | dcPage::addSuccessNotice(__('Language has been successfully upgraded')); |
---|
| 113 | } else { |
---|
| 114 | dcPage::addSuccessNotice(__('Language has been successfully installed.')); |
---|
| 115 | } |
---|
| 116 | $core->adminurl->redirect("admin.langs"); |
---|
| 117 | } catch (Exception $e) { |
---|
| 118 | $core->error->add($e->getMessage()); |
---|
| 119 | } |
---|
[0] | 120 | } |
---|
| 121 | |
---|
| 122 | # Upload a language pack |
---|
[3699] | 123 | if ($is_writable && !empty($_POST['upload_pkg'])) { |
---|
| 124 | try |
---|
| 125 | { |
---|
| 126 | if (empty($_POST['your_pwd']) || !$core->auth->checkPassword($_POST['your_pwd'])) { |
---|
| 127 | throw new Exception(__('Password verification failed')); |
---|
| 128 | } |
---|
[2566] | 129 | |
---|
[3699] | 130 | files::uploadStatus($_FILES['pkg_file']); |
---|
| 131 | $dest = DC_L10N_ROOT . '/' . $_FILES['pkg_file']['name']; |
---|
| 132 | if (!move_uploaded_file($_FILES['pkg_file']['tmp_name'], $dest)) { |
---|
| 133 | throw new Exception(__('Unable to move uploaded file.')); |
---|
| 134 | } |
---|
[2566] | 135 | |
---|
[3699] | 136 | try { |
---|
| 137 | $ret_code = $lang_install($dest); |
---|
| 138 | } catch (Exception $e) { |
---|
| 139 | @unlink($dest); |
---|
| 140 | throw $e; |
---|
| 141 | } |
---|
[2566] | 142 | |
---|
[3699] | 143 | @unlink($dest); |
---|
| 144 | if ($ret_code == 2) { |
---|
| 145 | dcPage::addSuccessNotice(__('Language has been successfully upgraded')); |
---|
| 146 | } else { |
---|
| 147 | dcPage::addSuccessNotice(__('Language has been successfully installed.')); |
---|
| 148 | } |
---|
| 149 | $core->adminurl->redirect("admin.langs"); |
---|
| 150 | } catch (Exception $e) { |
---|
| 151 | $core->error->add($e->getMessage()); |
---|
| 152 | } |
---|
[0] | 153 | } |
---|
| 154 | |
---|
| 155 | /* DISPLAY Main page |
---|
| 156 | -------------------------------------------------------- */ |
---|
| 157 | dcPage::open(__('Languages management'), |
---|
[3699] | 158 | dcPage::jsLoad('js/_langs.js'), |
---|
| 159 | dcPage::breadcrumb( |
---|
| 160 | array( |
---|
| 161 | __('System') => '', |
---|
| 162 | __('Languages management') => '' |
---|
| 163 | )) |
---|
[1358] | 164 | ); |
---|
[0] | 165 | |
---|
| 166 | if (!empty($_GET['removed'])) { |
---|
[3699] | 167 | dcPage::success(__('Language has been successfully deleted.')); |
---|
[0] | 168 | } |
---|
| 169 | |
---|
| 170 | if (!empty($_GET['added'])) { |
---|
[3699] | 171 | dcPage::success(($_GET['added'] == 2 ? __('Language has been successfully upgraded') : __('Language has been successfully installed.'))); |
---|
[0] | 172 | } |
---|
| 173 | |
---|
| 174 | echo |
---|
[3699] | 175 | '<p>' . __('Here you can install, upgrade or remove languages for your Dotclear ' . |
---|
| 176 | 'installation.') . '</p>' . |
---|
| 177 | '<p>' . sprintf(__('You can change your user language in your <a href="%1$s">preferences</a> or ' . |
---|
| 178 | 'change your blog\'s main language in your <a href="%2$s">blog settings</a>.'), |
---|
| 179 | $core->adminurl->get("admin.user.preferences"), $core->adminurl->get("admin.blog.pref")) . '</p>'; |
---|
[0] | 180 | |
---|
| 181 | echo |
---|
[3699] | 182 | '<h3>' . __('Installed languages') . '</h3>'; |
---|
[0] | 183 | |
---|
| 184 | $locales_content = scandir(DC_L10N_ROOT); |
---|
[3699] | 185 | $tmp = array(); |
---|
[0] | 186 | foreach ($locales_content as $v) { |
---|
[3699] | 187 | $c = ($v == '.' || $v == '..' || $v == 'en' || !is_dir(DC_L10N_ROOT . '/' . $v) || !isset($iso_codes[$v])); |
---|
[2566] | 188 | |
---|
[3699] | 189 | if (!$c) { |
---|
| 190 | $tmp[$v] = DC_L10N_ROOT . '/' . $v; |
---|
| 191 | } |
---|
[0] | 192 | } |
---|
| 193 | $locales_content = $tmp; |
---|
| 194 | |
---|
[3699] | 195 | if (empty($locales_content)) { |
---|
| 196 | echo '<p><strong>' . __('No additional language is installed.') . '</strong></p>'; |
---|
| 197 | } else { |
---|
| 198 | echo |
---|
| 199 | '<div class="table-outer clear">' . |
---|
| 200 | '<table class="plugins"><tr>' . |
---|
| 201 | '<th>' . __('Language') . '</th>' . |
---|
| 202 | '<th class="nowrap">' . __('Action') . '</th>' . |
---|
| 203 | '</tr>'; |
---|
[2566] | 204 | |
---|
[3699] | 205 | foreach ($locales_content as $k => $v) { |
---|
| 206 | $is_deletable = $is_writable && is_writable($v); |
---|
[2566] | 207 | |
---|
[3699] | 208 | echo |
---|
| 209 | '<tr class="line wide">' . |
---|
| 210 | '<td class="maximal nowrap">(' . $k . ') ' . |
---|
| 211 | '<strong>' . html::escapeHTML($iso_codes[$k]) . '</strong></td>' . |
---|
| 212 | '<td class="nowrap action">'; |
---|
[2566] | 213 | |
---|
[3699] | 214 | if ($is_deletable) { |
---|
| 215 | echo |
---|
| 216 | '<form action="' . $core->adminurl->get("admin.langs") . '" method="post">' . |
---|
| 217 | '<div>' . |
---|
| 218 | $core->formNonce() . |
---|
| 219 | form::hidden(array('locale_id'), html::escapeHTML($k)) . |
---|
| 220 | '<input type="submit" class="delete" name="delete" value="' . __('Delete') . '" /> ' . |
---|
| 221 | '</div>' . |
---|
| 222 | '</form>'; |
---|
| 223 | } |
---|
[2566] | 224 | |
---|
[3699] | 225 | echo '</td></tr>'; |
---|
| 226 | } |
---|
| 227 | echo '</table></div>'; |
---|
[0] | 228 | } |
---|
| 229 | |
---|
[3699] | 230 | echo '<h3>' . __('Install or upgrade languages') . '</h3>'; |
---|
[0] | 231 | |
---|
| 232 | if (!$is_writable) { |
---|
[3699] | 233 | echo '<p>' . sprintf(__('You can install or remove a language by adding or ' . |
---|
| 234 | 'removing the relevant directory in your %s folder.'), '<strong>locales</strong>') . '</p>'; |
---|
[0] | 235 | } |
---|
| 236 | |
---|
[3699] | 237 | if (!empty($dc_langs) && $is_writable) { |
---|
| 238 | $dc_langs_combo = array(); |
---|
| 239 | foreach ($dc_langs as $k => $v) { |
---|
| 240 | if ($v->link && isset($iso_codes[$v->title])) { |
---|
| 241 | $dc_langs_combo[html::escapeHTML('(' . $v->title . ') ' . $iso_codes[$v->title])] = html::escapeHTML($v->link); |
---|
| 242 | } |
---|
| 243 | } |
---|
[2566] | 244 | |
---|
[3699] | 245 | echo |
---|
| 246 | '<form method="post" action="' . $core->adminurl->get("admin.langs") . '" enctype="multipart/form-data" class="fieldset">' . |
---|
| 247 | '<h4>' . __('Available languages') . '</h4>' . |
---|
| 248 | '<p>' . sprintf(__('You can download and install a additional language directly from Dotclear.net. ' . |
---|
| 249 | 'Proposed languages are based on your version: %s.'), '<strong>' . DC_VERSION . '</strong>') . '</p>' . |
---|
| 250 | '<p class="field"><label for="pkg_url" class="classic">' . __('Language:') . '</label> ' . |
---|
| 251 | form::combo(array('pkg_url'), $dc_langs_combo) . '</p>' . |
---|
| 252 | '<p class="field"><label for="your_pwd1" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Your password:') . '</label> ' . |
---|
| 253 | form::password(array('your_pwd', 'your_pwd1'), 20, 255, |
---|
| 254 | array( |
---|
| 255 | 'extra_html' => 'required placeholder="' . __('Password') . '"', |
---|
| 256 | 'autocomplete' => 'current-password') |
---|
| 257 | ) . '</p>' . |
---|
| 258 | '<p><input type="submit" value="' . __('Install language') . '" />' . |
---|
| 259 | $core->formNonce() . |
---|
| 260 | '</p>' . |
---|
| 261 | '</form>'; |
---|
[0] | 262 | } |
---|
| 263 | |
---|
[3699] | 264 | if ($is_writable) { |
---|
| 265 | # 'Upload language pack' form |
---|
| 266 | echo |
---|
| 267 | '<form method="post" action="' . $core->adminurl->get("admin.langs") . '" enctype="multipart/form-data" class="fieldset">' . |
---|
| 268 | '<h4>' . __('Upload a zip file') . '</h4>' . |
---|
| 269 | '<p>' . __('You can install languages by uploading zip files.') . '</p>' . |
---|
| 270 | '<p class="field"><label for="pkg_file" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Language zip file:') . '</label> ' . |
---|
| 271 | '<input type="file" id="pkg_file" name="pkg_file" required /></p>' . |
---|
| 272 | '<p class="field"><label for="your_pwd2" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Your password:') . '</label> ' . |
---|
| 273 | form::password(array('your_pwd', 'your_pwd2'), 20, 255, |
---|
| 274 | array( |
---|
| 275 | 'extra_html' => 'required placeholder="' . __('Password') . '"', |
---|
| 276 | 'autocomplete' => 'current-password') |
---|
| 277 | ) . '</p>' . |
---|
| 278 | '<p><input type="submit" name="upload_pkg" value="' . __('Upload language') . '" />' . |
---|
| 279 | $core->formNonce() . |
---|
| 280 | '</p>' . |
---|
| 281 | '</form>'; |
---|
[0] | 282 | } |
---|
[2314] | 283 | dcPage::helpBlock('core_langs'); |
---|
[0] | 284 | dcPage::close(); |
---|