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