Dotclear

source: admin/langs.php @ 3699:77a12236e993

Revision 3699:77a12236e993, 10.1 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Add autocomplete attribute to form::password where it's relevant, code formatting (PSR-2)

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

Sites map