Dotclear

source: admin/langs.php @ 3036:7ed4286c8013

Revision 3036:7ed4286c8013, 8.7 KB checked in by franck <carnet.franck.paul@…>, 10 years ago (diff)

Centralizes crypt function of pwd in class.Dotclear.auth.php, closes #1923

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

Sites map