Dotclear

source: admin/langs.php @ 456:f55f02550402

Revision 456:f55f02550402, 8.2 KB checked in by Tomtom33 <tbouron@…>, 14 years ago (diff)

Fixed tables in admin pages - step 1, addresses #1068,#1069

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2010 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
13require dirname(__FILE__).'/../inc/admin/prepend.php';
14
15dcPage::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/');
26try {
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
34if ($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
60if ($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
98if ($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-------------------------------------------------------- */
130dcPage::open(__('Languages management'),
131     dcPage::jsLoad('js/_langs.js')
132);
133
134echo
135'<h2>'.__('Languages management').'</h2>';
136
137if (!empty($_GET['removed'])) {
138     echo '<p class="message">'.__('Language has been successfully deleted.').'</p>';
139}
140
141if (!empty($_GET['added'])) {
142     echo '<p class="message">'.
143     ($_GET['added'] == 2 ? __('Language has been successfully upgraded') : __('Language has been successfully installed.')).
144     '</p>';
145}
146
147echo
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
154echo
155'<h3>'.__('Installed languages').'</h3>';
156
157$locales_content = scandir(DC_L10N_ROOT);
158$tmp = array();
159foreach ($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
168if (empty($locales_content))
169{
170     echo '<p><strong>'.__('No additional language is installed.').'</strong></p>';
171}
172else
173{
174     echo
175     '<table class="clear plugins">'.
176     '<thead><tr>'.
177     '<th scope="col">'.__('Language').'</th>'.
178     '<th scope="col" class="nowrap">'.__('Action').'</th>'.
179     '</tr></thead>'.
180     '<tbody>';
181     
182     foreach ($locales_content as $k => $v)
183     {
184          $is_deletable = $is_writable && is_writable($v);
185         
186          echo
187          '<tr class="line wide">'.
188          '<th scope="row" class="maximal nowrap">('.$k.') '.
189          '<strong>'.html::escapeHTML($iso_codes[$k]).'</strong></th>'.
190          '<td class="nowrap action">';
191         
192          if ($is_deletable)
193          {
194               echo
195               '<form action="langs.php" method="post">'.
196               '<div>'.
197               $core->formNonce().
198               form::hidden(array('locale_id'),html::escapeHTML($k)).
199               '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" /> '.
200               '</div>'.
201               '</form>';
202          }
203         
204          echo '</td></tr>';
205     }
206     echo
207     '</tbody>'.
208     '</table>';
209}
210
211echo '<h3>'.__('Install or upgrade languages').'</h3>';
212
213if (!$is_writable) {
214     echo '<p>'.sprintf(__('You can install or remove a language by adding or '.
215          'removing the relevant directory in your %s folder.'),'<strong>locales</strong>').'</p>';
216}
217
218if (!empty($dc_langs) && $is_writable)
219{
220     $dc_langs_combo = array();
221     foreach ($dc_langs as $k => $v) {
222          if ($v->link && isset($iso_codes[$v->title])) {
223               $dc_langs_combo[html::escapeHTML('('.$v->title.') '.$iso_codes[$v->title])] = html::escapeHTML($v->link);
224          }
225     }
226     
227     echo
228     '<form method="post" action="langs.php" enctype="multipart/form-data">'.
229     '<fieldset>'.
230     '<legend>'.__('Available languages').'</legend>'.
231     '<p>'.sprintf(__('You can download and install a additional language directly from Dotclear.net. '.
232     'Proposed languages are based on your version: %s.'),'<strong>'.DC_VERSION.'</strong>').'</p>'.
233     '<p class="field"><label for="pkg_url" class="classic">'.__('Language:').' '.
234     form::combo('pkg_url',$dc_langs_combo).'</label></p>'.
235     '<p class="field"><label for="your_pwd1" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Your password:').' '.
236     form::password(array('your_pwd','your_pwd1'),20,255).'</label></p>'.
237     '<input type="submit" value="'.__('Install language').'" />'.
238     $core->formNonce().
239     '</fieldset>'.
240     '</form>';
241}
242
243if ($is_writable)
244{
245     # 'Upload language pack' form
246     echo
247     '<form method="post" action="langs.php" enctype="multipart/form-data">'.
248     '<fieldset>'.
249     '<legend>'.__('Upload a zip file').'</legend>'.
250     '<p>'.__('You can install languages by uploading zip files.').'</p>'.
251     '<p class="field"><label for="pkg_file" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Language zip file:').' '.
252     '<input type="file" id="pkg_file" name="pkg_file" /></label></p>'.
253     '<p class="field"><label for="your_pwd2" class="classic required"><abbr title="'.__('Required field').'">*</abbr> '.__('Your password:').' '.
254     form::password(array('your_pwd','your_pwd2'),20,255).'</label></p>'.
255     '<input type="submit" name="upload_pkg" value="'.__('Upload language').'" />'.
256     $core->formNonce().
257     '</fieldset>'.
258     '</form>';
259}
260
261dcPage::close();
262
263
264# Language installation function
265function dc_lang_install($file)
266{
267     $zip = new fileUnzip($file);
268     $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#');
269     
270     if (!preg_match('/^[a-z]{2,3}(-[a-z]{2})?$/',$zip->getRootDir())) {
271          throw new Exception(__('Invalid language zip file.'));
272     }
273     
274     if ($zip->isEmpty() || !$zip->hasFile($zip->getRootDir().'/main.po')) {
275          throw new Exception(__('The zip file does not appear to be a valid Dotclear language pack.'));
276     }
277     
278     
279     $target = dirname($file);
280     $destination = $target.'/'.$zip->getRootDir();
281     $res = 1;
282     
283     if (is_dir($destination)) {
284          if (!files::deltree($destination)) {
285               throw new Exception(__('An error occurred during language upgrade.'));
286          }
287          $res = 2;
288     }
289     
290     $zip->unzipAll($target);
291     return $res;
292}
293?>
Note: See TracBrowser for help on using the repository browser.

Sites map