Dotclear

source: admin/langs.php @ 3874:ab8368569446

Revision 3874:ab8368569446, 9.9 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

short notation for array (array() → [])

Line 
1<?php
2/**
3 * @package Dotclear
4 * @subpackage Backend
5 *
6 * @copyright Olivier Meunier & Association Dotclear
7 * @copyright GPL-2.0-only
8 */
9
10require dirname(__FILE__) . '/../inc/admin/prepend.php';
11
12dcPage::checkSuper();
13
14$is_writable = is_dir(DC_L10N_ROOT) && is_writable(DC_L10N_ROOT);
15$iso_codes   = l10n::getISOCodes();
16
17# Get languages list on Dotclear.net
18$dc_langs    = false;
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/');
23try {
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    }
28} catch (Exception $e) {}
29
30# Language installation function
31$lang_install = function ($file) {
32    $zip = new fileUnzip($file);
33    $zip->getList(false, '#(^|/)(__MACOSX|\.svn|\.DS_Store|\.directory|Thumbs\.db)(/|$)#');
34
35    if (!preg_match('/^[a-z]{2,3}(-[a-z]{2})?$/', $zip->getRootDir())) {
36        throw new Exception(__('Invalid language zip file.'));
37    }
38
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    }
42
43    $target      = dirname($file);
44    $destination = $target . '/' . $zip->getRootDir();
45    $res         = 1;
46
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    }
53
54    $zip->unzipAll($target);
55    return $res;
56};
57
58# Delete a language pack
59if ($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        }
66
67        if ($locale_id == 'en') {
68            throw new Exception(__("You can't remove English language."));
69        }
70
71        if (!files::deltree(DC_L10N_ROOT . '/' . $locale_id)) {
72            throw new Exception(__('Permissions to delete language denied.'));
73        }
74
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    }
80}
81
82# Download a language pack
83if ($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        }
89
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        }
95
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);
102
103        try {
104            $ret_code = $lang_install($dest);
105        } catch (Exception $e) {
106            @unlink($dest);
107            throw $e;
108        }
109
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    }
120}
121
122# Upload a language pack
123if ($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        }
129
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        }
135
136        try {
137            $ret_code = $lang_install($dest);
138        } catch (Exception $e) {
139            @unlink($dest);
140            throw $e;
141        }
142
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    }
153}
154
155/* DISPLAY Main page
156-------------------------------------------------------- */
157dcPage::open(__('Languages management'),
158    dcPage::jsLoad('js/_langs.js'),
159    dcPage::breadcrumb(
160        [
161            __('System')               => '',
162            __('Languages management') => ''
163        ])
164);
165
166if (!empty($_GET['removed'])) {
167    dcPage::success(__('Language has been successfully deleted.'));
168}
169
170if (!empty($_GET['added'])) {
171    dcPage::success(($_GET['added'] == 2 ? __('Language has been successfully upgraded') : __('Language has been successfully installed.')));
172}
173
174echo
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>';
180
181echo
182'<h3>' . __('Installed languages') . '</h3>';
183
184$locales_content = scandir(DC_L10N_ROOT);
185$tmp             = [];
186foreach ($locales_content as $v) {
187    $c = ($v == '.' || $v == '..' || $v == 'en' || !is_dir(DC_L10N_ROOT . '/' . $v) || !isset($iso_codes[$v]));
188
189    if (!$c) {
190        $tmp[$v] = DC_L10N_ROOT . '/' . $v;
191    }
192}
193$locales_content = $tmp;
194
195if (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>';
204
205    foreach ($locales_content as $k => $v) {
206        $is_deletable = $is_writable && is_writable($v);
207
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">';
213
214        if ($is_deletable) {
215            echo
216            '<form action="' . $core->adminurl->get("admin.langs") . '" method="post">' .
217            '<div>' .
218            $core->formNonce() .
219            form::hidden(['locale_id'], html::escapeHTML($k)) .
220            '<input type="submit" class="delete" name="delete" value="' . __('Delete') . '" /> ' .
221                '</div>' .
222                '</form>';
223        }
224
225        echo '</td></tr>';
226    }
227    echo '</table></div>';
228}
229
230echo '<h3>' . __('Install or upgrade languages') . '</h3>';
231
232if (!$is_writable) {
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>';
235}
236
237if (!empty($dc_langs) && $is_writable) {
238    $dc_langs_combo = [];
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    }
244
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(['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(['your_pwd', 'your_pwd1'], 20, 255,
254        [
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>';
262}
263
264if ($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(['your_pwd', 'your_pwd2'], 20, 255,
274        [
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>';
282}
283dcPage::helpBlock('core_langs');
284dcPage::close();
Note: See TracBrowser for help on using the repository browser.

Sites map