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