Dotclear

source: inc/admin/lib.moduleslist.php @ 3874:ab8368569446

Revision 3874:ab8368569446, 70.5 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
10if (!defined('DC_ADMIN_CONTEXT')) {return;}
11
12/**
13 * @brief Helper for admin list of modules.
14 * @since 2.6
15
16 * Provides an object to parse XML feed of modules from a repository.
17 */
18class adminModulesList
19{
20    public $core; /**< @var    object    dcCore instance */
21    public $modules; /**< @var    object    dcModules instance */
22    public $store; /**< @var    object    dcStore instance */
23
24    public static $allow_multi_install = false; /**< @var    boolean    Work with multiple root directories */
25    public static $distributed_modules = []; /**< @var    array    List of modules distributed with Dotclear */
26
27    protected $list_id = 'unknow'; /**< @var    string    Current list ID */
28    protected $data    = []; /**< @var    array    Current modules */
29
30    protected $config_module  = ''; /**< @var    string    Module ID to configure */
31    protected $config_file    = ''; /**< @var    string    Module path to configure */
32    protected $config_content = ''; /**< @var    string    Module configuration page content */
33
34    protected $path          = false; /**< @var    string    Modules root directory */
35    protected $path_writable = false; /**< @var    boolean    Indicate if modules root directory is writable */
36    protected $path_pattern  = false; /**< @var    string    Directory pattern to work on */
37
38    protected $page_url   = ''; /**< @var    string    Page URL */
39    protected $page_qs    = '?'; /**< @var    string    Page query string */
40    protected $page_tab   = ''; /**< @var    string    Page tab */
41    protected $page_redir = ''; /**< @var    string    Page redirection */
42
43    public static $nav_indexes = 'abcdefghijklmnopqrstuvwxyz0123456789'; /**< @var    string    Index list */
44    protected $nav_list        = []; /**< @var    array    Index list with special index */
45    protected $nav_special     = 'other'; /**< @var    string    Text for other special index */
46
47    protected $sort_field = 'sname'; /**< @var    string    Field used to sort modules */
48    protected $sort_asc   = true; /**< @var    boolean    Sort order asc */
49
50    /**
51     * Constructor.
52     *
53     * Note that this creates dcStore instance.
54     *
55     * @param    object    $modules        dcModules instance
56     * @param    string    $modules_root   Modules root directories
57     * @param    string    $xml_url        URL of modules feed from repository
58     * @param    boolean   $force          Force query repository
59     */
60    public function __construct(dcModules $modules, $modules_root, $xml_url, $force = false)
61    {
62        $this->core    = $modules->core;
63        $this->modules = $modules;
64        $this->store   = new dcStore($modules, $xml_url, $force);
65
66        $this->page_url = $this->core->adminurl->get('admin.plugins');
67
68        $this->setPath($modules_root);
69        $this->setIndex(__('other'));
70    }
71
72    /**
73     * Begin a new list.
74     *
75     * @param    string    $id        New list ID
76     * @return    adminModulesList self instance
77     */
78    public function setList($id)
79    {
80        $this->data     = [];
81        $this->page_tab = '';
82        $this->list_id  = $id;
83
84        return $this;
85    }
86
87    /**
88     * Get list ID.
89     *
90     * @return    List ID
91     */
92    public function getList()
93    {
94        return $this->list_id;
95    }
96
97    /// @name Modules root directory methods
98    //@{
99    /**
100     * Set path info.
101     *
102     * @param    string    $root        Modules root directories
103     * @return    adminModulesList self instance
104     */
105    protected function setPath($root)
106    {
107        $paths = explode(PATH_SEPARATOR, $root);
108        $path  = array_pop($paths);
109        unset($paths);
110
111        $this->path = $path;
112        if (is_dir($path) && is_writeable($path)) {
113            $this->path_writable = true;
114            $this->path_pattern  = preg_quote($path, '!');
115        }
116
117        return $this;
118    }
119
120    /**
121     * Get modules root directory.
122     *
123     * @return    Path to work on
124     */
125    public function getPath()
126    {
127        return $this->path;
128    }
129
130    /**
131     * Check if modules root directory is writable.
132     *
133     * @return    True if directory is writable
134     */
135    public function isWritablePath()
136    {
137        return $this->path_writable;
138    }
139
140    /**
141     * Check if root directory of a module is deletable.
142     *
143     * @param    string    $root        Module root directory
144     * @return    True if directory is delatable
145     */
146    public function isDeletablePath($root)
147    {
148        return $this->path_writable
149        && (preg_match('!^' . $this->path_pattern . '!', $root) || defined('DC_DEV') && DC_DEV)
150        && $this->core->auth->isSuperAdmin();
151    }
152    //@}
153
154    /// @name Page methods
155    //@{
156    /**
157     * Set page base URL.
158     *
159     * @param    string    $url        Page base URL
160     * @return    adminModulesList self instance
161     */
162    public function setURL($url)
163    {
164        $this->page_qs  = strpos('?', $url) ? '&amp;' : '?';
165        $this->page_url = $url;
166
167        return $this;
168    }
169
170    /**
171     * Get page URL.
172     *
173     * @param    string|array    $queries    Additionnal query string
174     * @param    booleany    $with_tab        Add current tab to URL end
175     * @return    Clean page URL
176     */
177    public function getURL($queries = '', $with_tab = true)
178    {
179        return $this->page_url .
180            (!empty($queries) ? $this->page_qs : '') .
181            (is_array($queries) ? http_build_query($queries) : $queries) .
182            ($with_tab && !empty($this->page_tab) ? '#' . $this->page_tab : '');
183    }
184
185    /**
186     * Set page tab.
187     *
188     * @param    string    $tab        Page tab
189     * @return    adminModulesList self instance
190     */
191    public function setTab($tab)
192    {
193        $this->page_tab = $tab;
194
195        return $this;
196    }
197
198    /**
199     * Get page tab.
200     *
201     * @return    Page tab
202     */
203    public function getTab()
204    {
205        return $this->page_tab;
206    }
207
208    /**
209     * Set page redirection.
210     *
211     * @param    string    $default        Default redirection
212     * @return    adminModulesList self instance
213     */
214    public function setRedir($default = '')
215    {
216        $this->page_redir = empty($_REQUEST['redir']) ? $default : $_REQUEST['redir'];
217
218        return $this;
219    }
220
221    /**
222     * Get page redirection.
223     *
224     * @return    Page redirection
225     */
226    public function getRedir()
227    {
228        return empty($this->page_redir) ? $this->getURL() : $this->page_redir;
229    }
230    //@}
231
232    /// @name Search methods
233    //@{
234    /**
235     * Get search query.
236     *
237     * @return    Search query
238     */
239    public function getSearch()
240    {
241        $query = !empty($_REQUEST['m_search']) ? trim($_REQUEST['m_search']) : null;
242        return strlen($query) > 2 ? $query : null;
243    }
244
245    /**
246     * Display searh form.
247     *
248     * @return    adminModulesList self instance
249     */
250    public function displaySearch()
251    {
252        $query = $this->getSearch();
253
254        if (empty($this->data) && $query === null) {
255            return $this;
256        }
257
258        echo
259        '<div class="modules-search">' .
260        '<form action="' . $this->getURL() . '" method="get">' .
261        '<p><label for="m_search" class="classic">' . __('Search in repository:') . '&nbsp;</label><br />' .
262        form::field('m_search', 30, 255, html::escapeHTML($query)) .
263        '<input type="submit" value="' . __('OK') . '" /> ';
264
265        if ($query) {
266            echo
267            ' <a href="' . $this->getURL() . '" class="button">' . __('Reset search') . '</a>';
268        }
269
270        echo
271        '</p>' .
272        '<p class="form-note">' .
273        __('Search is allowed on multiple terms longer than 2 chars, terms must be separated by space.') .
274            '</p>' .
275            '</form>';
276
277        if ($query) {
278            echo
279            '<p class="message">' . sprintf(
280                __('Found %d result for search "%s":', 'Found %d results for search "%s":', count($this->data)),
281                count($this->data), html::escapeHTML($query)
282            ) .
283                '</p>';
284        }
285        echo '</div>';
286
287        return $this;
288    }
289    //@}
290
291    /// @name Navigation menu methods
292    //@{
293    /**
294     * Set navigation special index.
295     *
296     * @return    adminModulesList self instance
297     */
298    public function setIndex($str)
299    {
300        $this->nav_special = (string) $str;
301        $this->nav_list    = array_merge(str_split(self::$nav_indexes), [$this->nav_special]);
302
303        return $this;
304    }
305
306    /**
307     * Get index from query.
308     *
309     * @return    Query index or default one
310     */
311    public function getIndex()
312    {
313        return isset($_REQUEST['m_nav']) && in_array($_REQUEST['m_nav'], $this->nav_list) ? $_REQUEST['m_nav'] : $this->nav_list[0];
314    }
315
316    /**
317     * Display navigation by index menu.
318     *
319     * @return    adminModulesList self instance
320     */
321    public function displayIndex()
322    {
323        if (empty($this->data) || $this->getSearch() !== null) {
324            return $this;
325        }
326
327        # Fetch modules required field
328        $indexes = [];
329        foreach ($this->data as $id => $module) {
330            if (!isset($module[$this->sort_field])) {
331                continue;
332            }
333            $char = substr($module[$this->sort_field], 0, 1);
334            if (!in_array($char, $this->nav_list)) {
335                $char = $this->nav_special;
336            }
337            if (!isset($indexes[$char])) {
338                $indexes[$char] = 0;
339            }
340            $indexes[$char]++;
341        }
342
343        $buttons = [];
344        foreach ($this->nav_list as $char) {
345            # Selected letter
346            if ($this->getIndex() == $char) {
347                $buttons[] = '<li class="active" title="' . __('current selection') . '"><strong> ' . $char . ' </strong></li>';
348            }
349            # Letter having modules
350            elseif (!empty($indexes[$char])) {
351                $title     = sprintf(__('%d result', '%d results', $indexes[$char]), $indexes[$char]);
352                $buttons[] = '<li class="btn" title="' . $title . '"><a href="' . $this->getURL('m_nav=' . $char) . '" title="' . $title . '"> ' . $char . ' </a></li>';
353            }
354            # Letter without modules
355            else {
356                $buttons[] = '<li class="btn no-link" title="' . __('no results') . '"> ' . $char . ' </li>';
357            }
358        }
359        # Parse navigation menu
360        echo '<div class="pager">' . __('Browse index:') . ' <ul class="index">' . implode('', $buttons) . '</ul></div>';
361
362        return $this;
363    }
364    //@}
365
366    /// @name Sort methods
367    //@{
368    /**
369     * Set default sort field.
370     *
371     * @return    adminModulesList self instance
372     */
373    public function setSort($field, $asc = true)
374    {
375        $this->sort_field = $field;
376        $this->sort_asc   = (boolean) $asc;
377
378        return $this;
379    }
380
381    /**
382     * Get sort field from query.
383     *
384     * @return    Query sort field or default one
385     */
386    public function getSort()
387    {
388        return !empty($_REQUEST['m_sort']) ? $_REQUEST['m_sort'] : $this->sort_field;
389    }
390
391    /**
392     * Display sort field form.
393     *
394     * @note    This method is not implemented yet
395     * @return    adminModulesList self instance
396     */
397    public function displaySort()
398    {
399        //
400
401        return $this;
402    }
403    //@}
404
405    /// @name Modules methods
406    //@{
407    /**
408     * Set modules and sanitize them.
409     *
410     * @return    adminModulesList self instance
411     */
412    public function setModules($modules)
413    {
414        $this->data = [];
415        if (!empty($modules) && is_array($modules)) {
416            foreach ($modules as $id => $module) {
417                $this->data[$id] = self::sanitizeModule($id, $module);
418            }
419        }
420        return $this;
421    }
422
423    /**
424     * Get modules currently set.
425     *
426     * @return    Array of modules
427     */
428    public function getModules()
429    {
430        return $this->data;
431    }
432
433    /**
434     * Sanitize a module.
435     *
436     * This clean infos of a module by adding default keys
437     * and clean some of them, sanitize module can safely
438     * be used in lists.
439     *
440     * @return    Array of the module informations
441     */
442    public static function sanitizeModule($id, $module)
443    {
444        $label = empty($module['label']) ? $id : $module['label'];
445        $name  = __(empty($module['name']) ? $label : $module['name']);
446
447        return array_merge(
448            # Default values
449            [
450                'desc'              => '',
451                'author'            => '',
452                'version'           => 0,
453                'current_version'   => 0,
454                'root'              => '',
455                'root_writable'     => false,
456                'permissions'       => null,
457                'parent'            => null,
458                'priority'          => 1000,
459                'standalone_config' => false,
460                'support'           => '',
461                'section'           => '',
462                'tags'              => '',
463                'details'           => '',
464                'sshot'             => '',
465                'score'             => 0,
466                'type'              => null,
467                'require'           => [],
468                'settings'          => []
469            ],
470            # Module's values
471            $module,
472            # Clean up values
473            [
474                'id'    => $id,
475                'sid'   => self::sanitizeString($id),
476                'label' => $label,
477                'name'  => $name,
478                'sname' => self::sanitizeString($name)
479            ]
480        );
481    }
482
483    /**
484     * Check if a module is part of the distribution.
485     *
486     * @param    string    $id        Module root directory
487     * @return    True if module is part of the distribution
488     */
489    public static function isDistributedModule($id)
490    {
491        $distributed_modules = self::$distributed_modules;
492
493        return is_array($distributed_modules) && in_array($id, $distributed_modules);
494    }
495
496    /**
497     * Sort modules list by specific field.
498     *
499     * @param    string    $module        Array of modules
500     * @param    string    $field        Field to sort from
501     * @param    bollean    $asc        Sort asc if true, else decs
502     * @return    Array of sorted modules
503     */
504    public static function sortModules($modules, $field, $asc = true)
505    {
506        $origin = $sorter = [];
507
508        foreach ($modules as $id => $module) {
509            $origin[] = $module;
510            $sorter[] = isset($module[$field]) ? $module[$field] : $field;
511        }
512
513        array_multisort($sorter, $asc ? SORT_ASC : SORT_DESC, $origin);
514
515        foreach ($origin as $module) {
516            $final[$module['id']] = $module;
517        }
518
519        return $final;
520    }
521
522    /**
523     * Display list of modules.
524     *
525     * @param    array    $cols        List of colones (module field) to display
526     * @param    array    $actions    List of predefined actions to show on form
527     * @param    boolean    $nav_limit    Limit list to previously selected index
528     * @return    adminModulesList self instance
529     */
530    public function displayModules($cols = ['name', 'version', 'desc'], $actions = [], $nav_limit = false)
531    {
532        echo
533        '<form action="' . $this->getURL() . '" method="post" class="modules-form-actions">' .
534        '<div class="table-outer">' .
535        '<table id="' . html::escapeHTML($this->list_id) . '" class="modules' . (in_array('expander', $cols) ? ' expandable' : '') . '">' .
536        '<caption class="hidden">' . html::escapeHTML(__('Plugins list')) . '</caption><tr>';
537
538        if (in_array('name', $cols)) {
539            $colspan = 1;
540            if (in_array('checkbox', $cols)) {
541                $colspan++;
542            }
543            if (in_array('icon', $cols)) {
544                $colspan++;
545            }
546            echo
547            '<th class="first nowrap"' . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . '>' . __('Name') . '</th>';
548        }
549
550        if (in_array('score', $cols) && $this->getSearch() !== null && defined('DC_DEBUG') && DC_DEBUG) {
551            echo
552            '<th class="nowrap">' . __('Score') . '</th>';
553        }
554
555        if (in_array('version', $cols)) {
556            echo
557            '<th class="nowrap count" scope="col">' . __('Version') . '</th>';
558        }
559
560        if (in_array('current_version', $cols)) {
561            echo
562            '<th class="nowrap count" scope="col">' . __('Current version') . '</th>';
563        }
564
565        if (in_array('desc', $cols)) {
566            echo
567            '<th class="nowrap module-desc" scope="col">' . __('Details') . '</th>';
568        }
569
570        if (in_array('distrib', $cols)) {
571            echo
572                '<th' . (in_array('desc', $cols) ? '' : ' class="maximal"') . '></th>';
573        }
574
575        if (!empty($actions) && $this->core->auth->isSuperAdmin()) {
576            echo
577            '<th class="minimal nowrap">' . __('Action') . '</th>';
578        }
579
580        echo
581            '</tr>';
582
583        $sort_field = $this->getSort();
584
585        # Sort modules by $sort_field (default sname)
586        $modules = $this->getSearch() === null ?
587        self::sortModules($this->data, $sort_field, $this->sort_asc) :
588        $this->data;
589
590        $count = 0;
591        foreach ($modules as $id => $module) {
592            # Show only requested modules
593            if ($nav_limit && $this->getSearch() === null) {
594                $char = substr($module[$sort_field], 0, 1);
595                if (!in_array($char, $this->nav_list)) {
596                    $char = $this->nav_special;
597                }
598                if ($this->getIndex() != $char) {
599                    continue;
600                }
601            }
602
603            echo
604            '<tr class="line" id="' . html::escapeHTML($this->list_id) . '_m_' . html::escapeHTML($id) . '"' .
605                (in_array('desc', $cols) ? ' title="' . html::escapeHTML(__($module['desc'])) . '" ' : '') .
606                '>';
607
608            $tds = 0;
609
610            if (in_array('checkbox', $cols)) {
611                $tds++;
612                echo
613                '<td class="module-icon nowrap">' .
614                form::checkbox(['modules[' . $count . ']', html::escapeHTML($this->list_id) . '_modules_' . html::escapeHTML($id)], html::escapeHTML($id)) .
615                    '</td>';
616            }
617
618            if (in_array('icon', $cols)) {
619                $tds++;
620                echo
621                '<td class="module-icon nowrap">' . sprintf(
622                    '<img alt="%1$s" title="%1$s" src="%2$s" />',
623                    html::escapeHTML($id), file_exists($module['root'] . '/icon.png') ?
624                    dcPage::getPF($id . '/icon.png') : 'images/module.png'
625                ) . '</td>';
626            }
627
628            $tds++;
629            echo
630                '<th class="module-name nowrap" scope="row">';
631            if (in_array('checkbox', $cols)) {
632                if (in_array('expander', $cols)) {
633                    echo
634                    html::escapeHTML($module['name']) . ($id != $module['name'] ? sprintf(__(' (%s)'), $id) : '');
635                } else {
636                    echo
637                    '<label for="' . html::escapeHTML($this->list_id) . '_modules_' . html::escapeHTML($id) . '">' .
638                    html::escapeHTML($module['name']) . ($id != $module['name'] ? sprintf(__(' (%s)'), $id) : '') .
639                        '</label>';
640                }
641            } else {
642                echo
643                html::escapeHTML($module['name']) . ($id != $module['name'] ? sprintf(__(' (%s)'), $id) : '') .
644                form::hidden(['modules[' . $count . ']'], html::escapeHTML($id));
645            }
646            echo
647            $this->core->formNonce() .
648                '</td>';
649
650            # Display score only for debug purpose
651            if (in_array('score', $cols) && $this->getSearch() !== null && defined('DC_DEBUG') && DC_DEBUG) {
652                $tds++;
653                echo
654                    '<td class="module-version nowrap count"><span class="debug">' . $module['score'] . '</span></td>';
655            }
656
657            if (in_array('version', $cols)) {
658                $tds++;
659                echo
660                '<td class="module-version nowrap count">' . html::escapeHTML($module['version']) . '</td>';
661            }
662
663            if (in_array('current_version', $cols)) {
664                $tds++;
665                echo
666                '<td class="module-current-version nowrap count">' . html::escapeHTML($module['current_version']) . '</td>';
667            }
668
669            if (in_array('desc', $cols)) {
670                $tds++;
671                echo
672                '<td class="module-desc maximal">' . html::escapeHTML(__($module['desc']));
673                if (isset($module['cannot_disable']) && $module['enabled']) {
674                    echo
675                    '<br/><span class="info">' .
676                    sprintf(__('This module cannot be disabled nor deleted, since the following modules are also enabled : %s'),
677                        join(',', $module['cannot_disable'])) .
678                        '</span>';
679                }
680                if (isset($module['cannot_enable']) && !$module['enabled']) {
681                    echo
682                    '<br/><span class="info">' .
683                    __('This module cannot be enabled, because of the following reasons :') .
684                        '<ul>';
685                    foreach ($module['cannot_enable'] as $m => $reason) {
686                        echo '<li>' . $reason . '</li>';
687                    }
688                    echo '</ul>' .
689                        '</span>';
690                }
691                echo '</td>';
692
693            }
694
695            if (in_array('distrib', $cols)) {
696                $tds++;
697                echo
698                    '<td class="module-distrib">' . (self::isDistributedModule($id) ?
699                    '<img src="images/dotclear_pw.png" alt="' .
700                    __('Plugin from official distribution') . '" title="' .
701                    __('Plugin from official distribution') . '" />'
702                    : '') . '</td>';
703            }
704
705            if (!empty($actions) && $this->core->auth->isSuperAdmin()) {
706                $buttons = $this->getActions($id, $module, $actions);
707
708                $tds++;
709                echo
710                '<td class="module-actions nowrap">' .
711
712                '<div>' . implode(' ', $buttons) . '</div>' .
713
714                    '</td>';
715            }
716
717            echo
718                '</tr>';
719
720            # Other informations
721            if (in_array('expander', $cols)) {
722                echo
723                    '<tr class="module-more"><td colspan="' . $tds . '" class="expand">';
724
725                if (!empty($module['author']) || !empty($module['details']) || !empty($module['support'])) {
726                    echo
727                        '<div><ul class="mod-more">';
728
729                    if (!empty($module['author'])) {
730                        echo
731                        '<li class="module-author">' . __('Author:') . ' ' . html::escapeHTML($module['author']) . '</li>';
732                    }
733
734                    $more = [];
735                    if (!empty($module['details'])) {
736                        $more[] = '<a class="module-details" href="' . $module['details'] . '">' . __('Details') . '</a>';
737                    }
738
739                    if (!empty($module['support'])) {
740                        $more[] = '<a class="module-support" href="' . $module['support'] . '">' . __('Support') . '</a>';
741                    }
742
743                    if (!empty($more)) {
744                        echo
745                        '<li>' . implode(' - ', $more) . '</li>';
746                    }
747
748                    echo
749                        '</ul></div>';
750                }
751
752                $config = !empty($module['root']) && file_exists(path::real($module['root'] . '/_config.php'));
753                $index  = !empty($module['root']) && file_exists(path::real($module['root'] . '/index.php'));
754
755                if ($config || $index || !empty($module['section']) || !empty($module['tags']) || !empty($module['settings'])) {
756                    echo
757                        '<div><ul class="mod-more">';
758
759                    if ($index && $module['enabled']) {
760                        echo '<li><a href="' . $this->core->adminurl->get('admin.plugin.' . $id) . '">' . __('Manage plugin') . '</a></li>';
761                    }
762
763                    $settings = $this->getSettingsUrls($this->core, $id);
764                    if (!empty($settings) && $module['enabled']) {
765                        echo '<li>' . implode(' - ', $settings) . '</li>';
766                    }
767
768                    if (!empty($module['section'])) {
769                        echo
770                        '<li class="module-section">' . __('Section:') . ' ' . html::escapeHTML($module['section']) . '</li>';
771                    }
772
773                    if (!empty($module['tags'])) {
774                        echo
775                        '<li class="module-tags">' . __('Tags:') . ' ' . html::escapeHTML($module['tags']) . '</li>';
776                    }
777
778                    echo
779                        '</ul></div>';
780                }
781
782                echo
783                    '</td></tr>';
784            }
785
786            $count++;
787        }
788        echo
789            '</table></div>';
790
791        if (!$count && $this->getSearch() === null) {
792            echo
793            '<p class="message">' . __('No plugins matched your search.') . '</p>';
794        } elseif ((in_array('checkbox', $cols) || $count > 1) && !empty($actions) && $this->core->auth->isSuperAdmin()) {
795            $buttons = $this->getGlobalActions($actions, in_array('checkbox', $cols));
796
797            if (!empty($buttons)) {
798                if (in_array('checkbox', $cols)) {
799                    echo
800                        '<p class="checkboxes-helpers"></p>';
801                }
802                echo
803                '<div>' . implode(' ', $buttons) . '</div>';
804            }
805        }
806        echo
807            '</form>';
808
809        return $this;
810    }
811
812    /**
813     * Get settings URLs if any
814     *
815     * @param object $core
816     * @param string $id module ID
817     * @param boolean $check check permission
818     * @param boolean $self include self URL (→ plugin index.php URL)
819     * @return Array of settings URLs
820     */
821    public static function getSettingsUrls($core, $id, $check = false, $self = true)
822    {
823        $st = [];
824
825        $mr       = $core->plugins->moduleRoot($id);
826        $config   = !empty($mr) && file_exists(path::real($mr . '/_config.php'));
827        $settings = $core->plugins->moduleInfo($id, 'settings');
828        if ($config || !empty($settings)) {
829            if ($config) {
830                if (!$check ||
831                    $core->auth->isSuperAdmin() ||
832                    $core->auth->check($core->plugins->moduleInfo($id, 'permissions'), $core->blog->id)) {
833                    $params = ['module' => $id, 'conf' => '1'];
834                    if (!$core->plugins->moduleInfo($id, 'standalone_config') && !$self) {
835                        $params['redir'] = $core->adminurl->get('admin.plugin.' . $id);
836                    }
837                    $st[] = '<a class="module-config" href="' .
838                    $core->adminurl->get('admin.plugins', $params) .
839                    '">' . __('Configure plugin') . '</a>';
840                }
841            }
842            if (is_array($settings)) {
843                foreach ($settings as $sk => $sv) {
844                    switch ($sk) {
845                        case 'blog':
846                            if (!$check ||
847                                $core->auth->isSuperAdmin() ||
848                                $core->auth->check('admin', $core->blog->id)) {
849                                $st[] = '<a class="module-config" href="' .
850                                $core->adminurl->get('admin.blog.pref') . $sv .
851                                '">' . __('Plugin settings (in blog parameters)') . '</a>';
852                            }
853                            break;
854                        case 'pref':
855                            if (!$check ||
856                                $core->auth->isSuperAdmin() ||
857                                $core->auth->check('usage,contentadmin', $core->blog->id)) {
858                                $st[] = '<a class="module-config" href="' .
859                                $core->adminurl->get('admin.user.preferences') . $sv .
860                                '">' . __('Plugin settings (in user preferences)') . '</a>';
861                            }
862                            break;
863                        case 'self':
864                            if ($self) {
865                                if (!$check ||
866                                    $core->auth->isSuperAdmin() ||
867                                    $core->auth->check($core->plugins->moduleInfo($id, 'permissions'), $core->blog->id)) {
868                                    $st[] = '<a class="module-config" href="' .
869                                    $core->adminurl->get('admin.plugin.' . $id) . $sv .
870                                    '">' . __('Plugin settings') . '</a>';
871                                }
872                            }
873                            break;
874                    }
875                }
876            }
877        }
878
879        return $st;
880    }
881
882    /**
883     * Get action buttons to add to modules list.
884     *
885     * @param    string    $id            Module ID
886     * @param    array    $module        Module info
887     * @param    array    $actions    Actions keys
888     * @return    Array of actions buttons
889     */
890    protected function getActions($id, $module, $actions)
891    {
892        $submits = [];
893
894        # Use loop to keep requested order
895        foreach ($actions as $action) {
896            switch ($action) {
897
898                # Deactivate
899                case 'activate':if ($this->core->auth->isSuperAdmin() && $module['root_writable'] && !isset($module['cannot_enable'])) {
900                        $submits[] =
901                        '<input type="submit" name="activate[' . html::escapeHTML($id) . ']" value="' . __('Activate') . '" />';
902                    }break;
903
904                # Activate
905                case 'deactivate':if ($this->core->auth->isSuperAdmin() && $module['root_writable'] && !isset($module['cannot_disable'])) {
906                        $submits[] =
907                        '<input type="submit" name="deactivate[' . html::escapeHTML($id) . ']" value="' . __('Deactivate') . '" class="reset" />';
908                    }break;
909
910                # Delete
911                case 'delete':if ($this->core->auth->isSuperAdmin() && $this->isDeletablePath($module['root']) && !isset($module['cannot_disable'])) {
912                        $dev       = !preg_match('!^' . $this->path_pattern . '!', $module['root']) && defined('DC_DEV') && DC_DEV ? ' debug' : '';
913                        $submits[] =
914                        '<input type="submit" class="delete ' . $dev . '" name="delete[' . html::escapeHTML($id) . ']" value="' . __('Delete') . '" />';
915                    }break;
916
917                # Install (from store)
918                case 'install':if ($this->core->auth->isSuperAdmin() && $this->path_writable) {
919                        $submits[] =
920                        '<input type="submit" name="install[' . html::escapeHTML($id) . ']" value="' . __('Install') . '" />';
921                    }break;
922
923                # Update (from store)
924                case 'update':if ($this->core->auth->isSuperAdmin() && $this->path_writable) {
925                        $submits[] =
926                        '<input type="submit" name="update[' . html::escapeHTML($id) . ']" value="' . __('Update') . '" />';
927                    }break;
928
929                # Behavior
930                case 'behavior':
931
932                    # --BEHAVIOR-- adminModulesListGetActions
933                    $tmp = $this->core->callBehavior('adminModulesListGetActions', $this, $id, $module);
934
935                    if (!empty($tmp)) {
936                        $submits[] = $tmp;
937                    }
938                    break;
939            }
940        }
941
942        return $submits;
943    }
944
945    /**
946     * Get global action buttons to add to modules list.
947     *
948     * @param    array    $actions          Actions keys
949     * @param boolean   $with_selection Limit action to selected modules
950     * @return    Array of actions buttons
951     */
952    protected function getGlobalActions($actions, $with_selection = false)
953    {
954        $submits = [];
955
956        # Use loop to keep requested order
957        foreach ($actions as $action) {
958            switch ($action) {
959
960                # Deactivate
961                case 'activate':if ($this->core->auth->isSuperAdmin() && $this->path_writable) {
962                        $submits[] =
963                            '<input type="submit" name="activate" value="' . ($with_selection ?
964                            __('Activate selected plugins') :
965                            __('Activate all plugins from this list')
966                        ) . '" />';
967                    }break;
968
969                # Activate
970                case 'deactivate':if ($this->core->auth->isSuperAdmin() && $this->path_writable) {
971                        $submits[] =
972                            '<input type="submit" name="deactivate" value="' . ($with_selection ?
973                            __('Deactivate selected plugins') :
974                            __('Deactivate all plugins from this list')
975                        ) . '" />';
976                    }break;
977
978                # Update (from store)
979                case 'update':if ($this->core->auth->isSuperAdmin() && $this->path_writable) {
980                        $submits[] =
981                            '<input type="submit" name="update" value="' . ($with_selection ?
982                            __('Update selected plugins') :
983                            __('Update all plugins from this list')
984                        ) . '" />';
985                    }break;
986
987                # Behavior
988                case 'behavior':
989
990                    # --BEHAVIOR-- adminModulesListGetGlobalActions
991                    $tmp = $this->core->callBehavior('adminModulesListGetGlobalActions', $this, $with_selection);
992
993                    if (!empty($tmp)) {
994                        $submits[] = $tmp;
995                    }
996                    break;
997            }
998        }
999
1000        return $submits;
1001    }
1002
1003    /**
1004     * Execute POST action.
1005     *
1006     * @note    Set a notice on success through dcPage::addSuccessNotice
1007     * @throw    Exception    Module not find or command failed
1008     * @return    Null
1009     */
1010    public function doActions()
1011    {
1012        if (empty($_POST) || !empty($_REQUEST['conf'])
1013            || !$this->isWritablePath()) {
1014            return;
1015        }
1016
1017        $modules = !empty($_POST['modules']) && is_array($_POST['modules']) ? array_values($_POST['modules']) : [];
1018
1019        if ($this->core->auth->isSuperAdmin() && !empty($_POST['delete'])) {
1020
1021            if (is_array($_POST['delete'])) {
1022                $modules = array_keys($_POST['delete']);
1023            }
1024
1025            $list = $this->modules->getDisabledModules();
1026
1027            $failed = false;
1028            $count  = 0;
1029            foreach ($modules as $id) {
1030                if (!isset($list[$id])) {
1031
1032                    if (!$this->modules->moduleExists($id)) {
1033                        throw new Exception(__('No such plugin.'));
1034                    }
1035
1036                    $module       = $this->modules->getModules($id);
1037                    $module['id'] = $id;
1038
1039                    if (!$this->isDeletablePath($module['root'])) {
1040                        $failed = true;
1041                        continue;
1042                    }
1043
1044                    # --BEHAVIOR-- moduleBeforeDelete
1045                    $this->core->callBehavior('pluginBeforeDelete', $module);
1046
1047                    $this->modules->deleteModule($id);
1048
1049                    # --BEHAVIOR-- moduleAfterDelete
1050                    $this->core->callBehavior('pluginAfterDelete', $module);
1051                } else {
1052                    $this->modules->deleteModule($id, true);
1053                }
1054
1055                $count++;
1056            }
1057
1058            if (!$count && $failed) {
1059                throw new Exception(__("You don't have permissions to delete this plugin."));
1060            } elseif ($failed) {
1061                dcPage::addWarningNotice(__('Some plugins have not been delete.'));
1062            } else {
1063                dcPage::addSuccessNotice(
1064                    __('Plugin has been successfully deleted.', 'Plugins have been successuflly deleted.', $count)
1065                );
1066            }
1067            http::redirect($this->getURL());
1068        } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['install'])) {
1069
1070            if (is_array($_POST['install'])) {
1071                $modules = array_keys($_POST['install']);
1072            }
1073
1074            $list = $this->store->get();
1075
1076            if (empty($list)) {
1077                throw new Exception(__('No such plugin.'));
1078            }
1079
1080            $count = 0;
1081            foreach ($list as $id => $module) {
1082
1083                if (!in_array($id, $modules)) {
1084                    continue;
1085                }
1086
1087                $dest = $this->getPath() . '/' . basename($module['file']);
1088
1089                # --BEHAVIOR-- moduleBeforeAdd
1090                $this->core->callBehavior('pluginBeforeAdd', $module);
1091
1092                $this->store->process($module['file'], $dest);
1093
1094                # --BEHAVIOR-- moduleAfterAdd
1095                $this->core->callBehavior('pluginAfterAdd', $module);
1096
1097                $count++;
1098            }
1099
1100            dcPage::addSuccessNotice(
1101                __('Plugin has been successfully installed.', 'Plugins have been successuflly installed.', $count)
1102            );
1103            http::redirect($this->getURL());
1104        } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['activate'])) {
1105
1106            if (is_array($_POST['activate'])) {
1107                $modules = array_keys($_POST['activate']);
1108            }
1109
1110            $list = $this->modules->getDisabledModules();
1111            if (empty($list)) {
1112                throw new Exception(__('No such plugin.'));
1113            }
1114
1115            $count = 0;
1116            foreach ($list as $id => $module) {
1117
1118                if (!in_array($id, $modules)) {
1119                    continue;
1120                }
1121
1122                # --BEHAVIOR-- moduleBeforeActivate
1123                $this->core->callBehavior('pluginBeforeActivate', $id);
1124
1125                $this->modules->activateModule($id);
1126
1127                # --BEHAVIOR-- moduleAfterActivate
1128                $this->core->callBehavior('pluginAfterActivate', $id);
1129
1130                $count++;
1131            }
1132
1133            dcPage::addSuccessNotice(
1134                __('Plugin has been successfully activated.', 'Plugins have been successuflly activated.', $count)
1135            );
1136            http::redirect($this->getURL());
1137        } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['deactivate'])) {
1138
1139            if (is_array($_POST['deactivate'])) {
1140                $modules = array_keys($_POST['deactivate']);
1141            }
1142
1143            $list = $this->modules->getModules();
1144            if (empty($list)) {
1145                throw new Exception(__('No such plugin.'));
1146            }
1147
1148            $failed = false;
1149            $count  = 0;
1150            foreach ($list as $id => $module) {
1151
1152                if (!in_array($id, $modules)) {
1153                    continue;
1154                }
1155
1156                if (!$module['root_writable']) {
1157                    $failed = true;
1158                    continue;
1159                }
1160
1161                $module[$id] = $id;
1162
1163                # --BEHAVIOR-- moduleBeforeDeactivate
1164                $this->core->callBehavior('pluginBeforeDeactivate', $module);
1165
1166                $this->modules->deactivateModule($id);
1167
1168                # --BEHAVIOR-- moduleAfterDeactivate
1169                $this->core->callBehavior('pluginAfterDeactivate', $module);
1170
1171                $count++;
1172            }
1173
1174            if ($failed) {
1175                dcPage::addWarningNotice(__('Some plugins have not been deactivated.'));
1176            } else {
1177                dcPage::addSuccessNotice(
1178                    __('Plugin has been successfully deactivated.', 'Plugins have been successuflly deactivated.', $count)
1179                );
1180            }
1181            http::redirect($this->getURL());
1182        } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['update'])) {
1183
1184            if (is_array($_POST['update'])) {
1185                $modules = array_keys($_POST['update']);
1186            }
1187
1188            $list = $this->store->get(true);
1189            if (empty($list)) {
1190                throw new Exception(__('No such plugin.'));
1191            }
1192
1193            $count = 0;
1194            foreach ($list as $module) {
1195
1196                if (!in_array($module['id'], $modules)) {
1197                    continue;
1198                }
1199
1200                if (!self::$allow_multi_install) {
1201                    $dest = $module['root'] . '/../' . basename($module['file']);
1202                } else {
1203                    $dest = $this->getPath() . '/' . basename($module['file']);
1204                    if ($module['root'] != $dest) {
1205                        @file_put_contents($module['root'] . '/_disabled', '');
1206                    }
1207                }
1208
1209                # --BEHAVIOR-- moduleBeforeUpdate
1210                $this->core->callBehavior('pluginBeforeUpdate', $module);
1211
1212                $this->store->process($module['file'], $dest);
1213
1214                # --BEHAVIOR-- moduleAfterUpdate
1215                $this->core->callBehavior('pluginAfterUpdate', $module);
1216
1217                $count++;
1218            }
1219
1220            $tab = $count && $count == count($list) ? '#plugins' : '#update';
1221
1222            dcPage::addSuccessNotice(
1223                __('Plugin has been successfully updated.', 'Plugins have been successuflly updated.', $count)
1224            );
1225            http::redirect($this->getURL() . $tab);
1226        }
1227
1228        # Manual actions
1229        elseif (!empty($_POST['upload_pkg']) && !empty($_FILES['pkg_file'])
1230            || !empty($_POST['fetch_pkg']) && !empty($_POST['pkg_url'])) {
1231            if (empty($_POST['your_pwd']) || !$this->core->auth->checkPassword($_POST['your_pwd'])) {
1232                throw new Exception(__('Password verification failed'));
1233            }
1234
1235            if (!empty($_POST['upload_pkg'])) {
1236                files::uploadStatus($_FILES['pkg_file']);
1237
1238                $dest = $this->getPath() . '/' . $_FILES['pkg_file']['name'];
1239                if (!move_uploaded_file($_FILES['pkg_file']['tmp_name'], $dest)) {
1240                    throw new Exception(__('Unable to move uploaded file.'));
1241                }
1242            } else {
1243                $url  = urldecode($_POST['pkg_url']);
1244                $dest = $this->getPath() . '/' . basename($url);
1245                $this->store->download($url, $dest);
1246            }
1247
1248            # --BEHAVIOR-- moduleBeforeAdd
1249            $this->core->callBehavior('pluginBeforeAdd', null);
1250
1251            $ret_code = $this->store->install($dest);
1252
1253            # --BEHAVIOR-- moduleAfterAdd
1254            $this->core->callBehavior('pluginAfterAdd', null);
1255
1256            dcPage::addSuccessNotice($ret_code == 2 ?
1257                __('Plugin has been successfully updated.') :
1258                __('Plugin has been successfully installed.')
1259            );
1260            http::redirect($this->getURL() . '#plugins');
1261        } else {
1262
1263            # --BEHAVIOR-- adminModulesListDoActions
1264            $this->core->callBehavior('adminModulesListDoActions', $this, $modules, 'plugin');
1265
1266        }
1267
1268        return;
1269    }
1270
1271    /**
1272     * Display tab for manual installation.
1273     *
1274     * @return    adminModulesList self instance
1275     */
1276    public function displayManualForm()
1277    {
1278        if (!$this->core->auth->isSuperAdmin() || !$this->isWritablePath()) {
1279            return;
1280        }
1281
1282        # 'Upload module' form
1283        echo
1284        '<form method="post" action="' . $this->getURL() . '" id="uploadpkg" enctype="multipart/form-data" class="fieldset">' .
1285        '<h4>' . __('Upload a zip file') . '</h4>' .
1286        '<p class="field"><label for="pkg_file" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Zip file path:') . '</label> ' .
1287        '<input type="file" name="pkg_file" id="pkg_file" required /></p>' .
1288        '<p class="field"><label for="your_pwd1" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Your password:') . '</label> ' .
1289        form::password(['your_pwd', 'your_pwd1'], 20, 255,
1290            [
1291                'extra_html'   => 'required placeholder="' . __('Password') . '"',
1292                'autocomplete' => 'current-password'
1293            ]
1294        ) . '</p>' .
1295        '<p><input type="submit" name="upload_pkg" value="' . __('Upload') . '" />' .
1296        $this->core->formNonce() . '</p>' .
1297            '</form>';
1298
1299        # 'Fetch module' form
1300        echo
1301        '<form method="post" action="' . $this->getURL() . '" id="fetchpkg" class="fieldset">' .
1302        '<h4>' . __('Download a zip file') . '</h4>' .
1303        '<p class="field"><label for="pkg_url" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Zip file URL:') . '</label> ' .
1304        form::field('pkg_url', 40, 255, [
1305            'extra_html' => 'required placeholder="' . __('URL') . '"'
1306        ]) .
1307        '</p>' .
1308        '<p class="field"><label for="your_pwd2" class="classic required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Your password:') . '</label> ' .
1309        form::password(['your_pwd', 'your_pwd2'], 20, 255,
1310            [
1311                'extra_html'   => 'required placeholder="' . __('Password') . '"',
1312                'autocomplete' => 'current-password'
1313            ]
1314        ) . '</p>' .
1315        '<p><input type="submit" name="fetch_pkg" value="' . __('Download') . '" />' .
1316        $this->core->formNonce() . '</p>' .
1317            '</form>';
1318
1319        return $this;
1320    }
1321    //@}
1322
1323    /// @name Module configuration methods
1324    //@{
1325    /**
1326     * Prepare module configuration.
1327     *
1328     * We need to get configuration content in three steps
1329     * and out of this class to keep backward compatibility.
1330     *
1331     * if ($xxx->setConfiguration()) {
1332     *    include $xxx->includeConfiguration();
1333     * }
1334     * $xxx->getConfiguration();
1335     * ... [put here page headers and other stuff]
1336     * $xxx->displayConfiguration();
1337     *
1338     * @param    string    $id        Module to work on or it gather through REQUEST
1339     * @return    True if config set
1340     */
1341    public function setConfiguration($id = null)
1342    {
1343        if (empty($_REQUEST['conf']) || empty($_REQUEST['module']) && !$id) {
1344            return false;
1345        }
1346
1347        if (!empty($_REQUEST['module']) && empty($id)) {
1348            $id = $_REQUEST['module'];
1349        }
1350
1351        if (!$this->modules->moduleExists($id)) {
1352            $this->core->error->add(__('Unknow plugin ID'));
1353            return false;
1354        }
1355
1356        $module = $this->modules->getModules($id);
1357        $module = self::sanitizeModule($id, $module);
1358        $file   = path::real($module['root'] . '/_config.php');
1359
1360        if (!file_exists($file)) {
1361            $this->core->error->add(__('This plugin has no configuration file.'));
1362            return false;
1363        }
1364
1365        $this->config_module  = $module;
1366        $this->config_file    = $file;
1367        $this->config_content = '';
1368
1369        if (!defined('DC_CONTEXT_MODULE')) {
1370            define('DC_CONTEXT_MODULE', true);
1371        }
1372
1373        return true;
1374    }
1375
1376    /**
1377     * Get path of module configuration file.
1378     *
1379     * @note Required previously set file info
1380     * @return Full path of config file or null
1381     */
1382    public function includeConfiguration()
1383    {
1384        if (!$this->config_file) {
1385            return;
1386        }
1387        $this->setRedir($this->getURL() . '#plugins');
1388
1389        ob_start();
1390
1391        return $this->config_file;
1392    }
1393
1394    /**
1395     * Gather module configuration file content.
1396     *
1397     * @note Required previously file inclusion
1398     * @return True if content has been captured
1399     */
1400    public function getConfiguration()
1401    {
1402        if ($this->config_file) {
1403            $this->config_content = ob_get_contents();
1404        }
1405
1406        ob_end_clean();
1407
1408        return !empty($this->file_content);
1409    }
1410
1411    /**
1412     * Display module configuration form.
1413     *
1414     * @note Required previously gathered content
1415     * @return    adminModulesList self instance
1416     */
1417    public function displayConfiguration()
1418    {
1419        if ($this->config_file) {
1420
1421            if (!$this->config_module['standalone_config']) {
1422                echo
1423                '<form id="module_config" action="' . $this->getURL('conf=1') . '" method="post" enctype="multipart/form-data">' .
1424                '<h3>' . sprintf(__('Configure "%s"'), html::escapeHTML($this->config_module['name'])) . '</h3>' .
1425                '<p><a class="back" href="' . $this->getRedir() . '">' . __('Back') . '</a></p>';
1426            }
1427
1428            echo $this->config_content;
1429
1430            if (!$this->config_module['standalone_config']) {
1431                echo
1432                '<p class="clear"><input type="submit" name="save" value="' . __('Save') . '" />' .
1433                form::hidden('module', $this->config_module['id']) .
1434                form::hidden('redir', $this->getRedir()) .
1435                $this->core->formNonce() . '</p>' .
1436                    '</form>';
1437            }
1438        }
1439
1440        return $this;
1441    }
1442    //@}
1443
1444    /**
1445     * Helper to sanitize a string.
1446     *
1447     * Used for search or id.
1448     *
1449     * @param    string    $str        String to sanitize
1450     * @return    Sanitized string
1451     */
1452    public static function sanitizeString($str)
1453    {
1454        return preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str));
1455    }
1456}
1457
1458/**
1459 * @ingroup DC_CORE
1460 * @brief Helper to manage list of themes.
1461 * @since 2.6
1462 */
1463class adminThemesList extends adminModulesList
1464{
1465    /**
1466     * Constructor.
1467     *
1468     * Note that this creates dcStore instance.
1469     *
1470     * @param    object    $modules        dcModules instance
1471     * @param    string    $modules_root   Modules root directories
1472     * @param    string    $xml_url        URL of modules feed from repository
1473     * @param    boolean   $force          Force query repository
1474     */
1475    public function __construct(dcModules $modules, $modules_root, $xml_url, $force = false)
1476    {
1477        parent::__construct($modules, $modules_root, $xml_url, $force);
1478        $this->page_url = $this->core->adminurl->get('admin.blog.theme');
1479    }
1480
1481    public function displayModules($cols = ['name', 'config', 'version', 'desc'], $actions = [], $nav_limit = false)
1482    {
1483        echo
1484        '<form action="' . $this->getURL() . '" method="post" class="modules-form-actions">' .
1485        '<div id="' . html::escapeHTML($this->list_id) . '" class="modules' . (in_array('expander', $cols) ? ' expandable' : '') . ' one-box">';
1486
1487        $sort_field = $this->getSort();
1488
1489        # Sort modules by id
1490        $modules = $this->getSearch() === null ?
1491        self::sortModules($this->data, $sort_field, $this->sort_asc) :
1492        $this->data;
1493
1494        $res   = '';
1495        $count = 0;
1496        foreach ($modules as $id => $module) {
1497            # Show only requested modules
1498            if ($nav_limit && $this->getSearch() === null) {
1499                $char = substr($module[$sort_field], 0, 1);
1500                if (!in_array($char, $this->nav_list)) {
1501                    $char = $this->nav_special;
1502                }
1503                if ($this->getIndex() != $char) {
1504                    continue;
1505                }
1506            }
1507
1508            $current = $this->core->blog->settings->system->theme == $id && $this->modules->moduleExists($id);
1509            $distrib = self::isDistributedModule($id) ? ' dc-box' : '';
1510
1511            $line =
1512                '<div class="box ' . ($current ? 'medium current-theme' : 'theme') . $distrib . '">';
1513
1514            if (in_array('name', $cols) && !$current) {
1515                $line .=
1516                    '<h4 class="module-name">';
1517
1518                if (in_array('checkbox', $cols)) {
1519                    $line .=
1520                    '<label for="' . html::escapeHTML($this->list_id) . '_modules_' . html::escapeHTML($id) . '">' .
1521                    form::checkbox(['modules[' . $count . ']', html::escapeHTML($this->list_id) . '_modules_' . html::escapeHTML($id)], html::escapeHTML($id)) .
1522                    html::escapeHTML($module['name']) .
1523                        '</label>';
1524
1525                } else {
1526                    $line .=
1527                    form::hidden(['modules[' . $count . ']'], html::escapeHTML($id)) .
1528                    html::escapeHTML($module['name']);
1529                }
1530
1531                $line .=
1532                $this->core->formNonce() .
1533                    '</h4>';
1534            }
1535
1536            # Display score only for debug purpose
1537            if (in_array('score', $cols) && $this->getSearch() !== null && defined('DC_DEBUG') && DC_DEBUG) {
1538                $line .=
1539                '<p class="module-score debug">' . sprintf(__('Score: %s'), $module['score']) . '</p>';
1540            }
1541
1542            if (in_array('sshot', $cols)) {
1543                # Screenshot from url
1544                if (preg_match('#^http(s)?://#', $module['sshot'])) {
1545                    $sshot = $module['sshot'];
1546                }
1547                # Screenshot from installed module
1548                elseif (file_exists($this->core->blog->themes_path . '/' . $id . '/screenshot.jpg')) {
1549                    $sshot = $this->getURL('shot=' . rawurlencode($id));
1550                }
1551                # Default screenshot
1552                else {
1553                    $sshot = 'images/noscreenshot.png';
1554                }
1555
1556                $line .=
1557                '<div class="module-sshot"><img src="' . $sshot . '" alt="' .
1558                sprintf(__('%s screenshot.'), html::escapeHTML($module['name'])) . '" /></div>';
1559            }
1560
1561            $line .=
1562                '<div class="module-infos toggle-bloc">';
1563
1564            if (in_array('name', $cols) && $current) {
1565                $line .=
1566                    '<h4 class="module-name">';
1567
1568                if (in_array('checkbox', $cols)) {
1569                    $line .=
1570                    '<label for="' . html::escapeHTML($this->list_id) . '_modules_' . html::escapeHTML($id) . '">' .
1571                    form::checkbox(['modules[' . $count . ']', html::escapeHTML($this->list_id) . '_modules_' . html::escapeHTML($id)], html::escapeHTML($id)) .
1572                    html::escapeHTML($module['name']) .
1573                        '</label>';
1574                } else {
1575                    $line .=
1576                    form::hidden(['modules[' . $count . ']'], html::escapeHTML($id)) .
1577                    html::escapeHTML($module['name']);
1578                }
1579
1580                $line .=
1581                    '</h4>';
1582            }
1583
1584            $line .=
1585                '<p>';
1586
1587            if (in_array('desc', $cols)) {
1588                $line .=
1589                '<span class="module-desc">' . html::escapeHTML(__($module['desc'])) . '</span> ';
1590            }
1591
1592            if (in_array('author', $cols)) {
1593                $line .=
1594                '<span class="module-author">' . sprintf(__('by %s'), html::escapeHTML($module['author'])) . '</span> ';
1595            }
1596
1597            if (in_array('version', $cols)) {
1598                $line .=
1599                '<span class="module-version">' . sprintf(__('version %s'), html::escapeHTML($module['version'])) . '</span> ';
1600            }
1601
1602            if (in_array('current_version', $cols)) {
1603                $line .=
1604                '<span class="module-current-version">' . sprintf(__('(current version %s)'), html::escapeHTML($module['current_version'])) . '</span> ';
1605            }
1606
1607            if (in_array('parent', $cols) && !empty($module['parent'])) {
1608                if ($this->modules->moduleExists($module['parent'])) {
1609                    $line .=
1610                    '<span class="module-parent-ok">' . sprintf(__('(built on "%s")'), html::escapeHTML($module['parent'])) . '</span> ';
1611                } else {
1612                    $line .=
1613                    '<span class="module-parent-missing">' . sprintf(__('(requires "%s")'), html::escapeHTML($module['parent'])) . '</span> ';
1614                }
1615            }
1616
1617            $has_details = in_array('details', $cols) && !empty($module['details']);
1618            $has_support = in_array('support', $cols) && !empty($module['support']);
1619            if ($has_details || $has_support) {
1620                $line .=
1621                    '<span class="mod-more">';
1622
1623                if ($has_details) {
1624                    $line .=
1625                    '<a class="module-details" href="' . $module['details'] . '">' . __('Details') . '</a>';
1626                }
1627
1628                if ($has_support) {
1629                    $line .=
1630                    ' - <a class="module-support" href="' . $module['support'] . '">' . __('Support') . '</a>';
1631                }
1632
1633                $line .=
1634                    '</span>';
1635            }
1636
1637            $line .=
1638                '</p>' .
1639                '</div>';
1640
1641            $line .=
1642                '<div class="module-actions toggle-bloc">';
1643
1644            # Plugins actions
1645            if ($current) {
1646
1647                # _GET actions
1648                if (file_exists(path::real($this->core->blog->themes_path . '/' . $id) . '/style.css')) {
1649                    $theme_url = preg_match('#^http(s)?://#', $this->core->blog->settings->system->themes_url) ?
1650                    http::concatURL($this->core->blog->settings->system->themes_url, '/' . $id) :
1651                    http::concatURL($this->core->blog->url, $this->core->blog->settings->system->themes_url . '/' . $id);
1652                    $line .=
1653                    '<p><a href="' . $theme_url . '/style.css">' . __('View stylesheet') . '</a></p>';
1654                }
1655
1656                $line .= '<div class="current-actions">';
1657
1658                if (file_exists(path::real($this->core->blog->themes_path . '/' . $id) . '/_config.php')) {
1659                    $line .=
1660                    '<p><a href="' . $this->getURL('module=' . $id . '&amp;conf=1', false) . '" class="button submit">' . __('Configure theme') . '</a></p>';
1661                }
1662
1663                # --BEHAVIOR-- adminCurrentThemeDetails
1664                $line .=
1665                $this->core->callBehavior('adminCurrentThemeDetails', $this->core, $id, $module);
1666
1667                $line .= '</div>';
1668            }
1669
1670            # _POST actions
1671            if (!empty($actions)) {
1672                $line .=
1673                '<p>' . implode(' ', $this->getActions($id, $module, $actions)) . '</p>';
1674            }
1675
1676            $line .=
1677                '</div>';
1678
1679            $line .=
1680                '</div>';
1681
1682            $count++;
1683
1684            $res = $current ? $line . $res : $res . $line;
1685        }
1686
1687        echo
1688            $res .
1689            '</div>';
1690
1691        if (!$count && $this->getSearch() === null) {
1692            echo
1693            '<p class="message">' . __('No themes matched your search.') . '</p>';
1694        } elseif ((in_array('checkbox', $cols) || $count > 1) && !empty($actions) && $this->core->auth->isSuperAdmin()) {
1695            $buttons = $this->getGlobalActions($actions, in_array('checkbox', $cols));
1696
1697            if (!empty($buttons)) {
1698                if (in_array('checkbox', $cols)) {
1699                    echo
1700                        '<p class="checkboxes-helpers"></p>';
1701                }
1702                echo '<div>' . implode(' ', $buttons) . '</div>';
1703            }
1704        }
1705
1706        echo
1707            '</form>';
1708
1709        return $this;
1710    }
1711
1712    protected function getActions($id, $module, $actions)
1713    {
1714        $submits = [];
1715
1716        $this->core->blog->settings->addNamespace('system');
1717        if ($id != $this->core->blog->settings->system->theme) {
1718
1719            # Select theme to use on curent blog
1720            if (in_array('select', $actions)) {
1721                $submits[] =
1722                '<input type="submit" name="select[' . html::escapeHTML($id) . ']" value="' . __('Use this one') . '" />';
1723            }
1724        }
1725
1726        return array_merge(
1727            $submits,
1728            parent::getActions($id, $module, $actions)
1729        );
1730    }
1731
1732    protected function getGlobalActions($actions, $with_selection = false)
1733    {
1734        $submits = [];
1735
1736        foreach ($actions as $action) {
1737            switch ($action) {
1738
1739                # Update (from store)
1740                case 'update':if ($this->core->auth->isSuperAdmin() && $this->path_writable) {
1741                        $submits[] =
1742                        '<input type="submit" name="update" value="' . ($with_selection ?
1743                            __('Update selected themes') :
1744                            __('Update all themes from this list')
1745                        ) . '" />' . $this->core->formNonce();
1746                    }break;
1747
1748                # Behavior
1749                case 'behavior':
1750
1751                    # --BEHAVIOR-- adminModulesListGetGlobalActions
1752                    $tmp = $this->core->callBehavior('adminModulesListGetGlobalActions', $this);
1753
1754                    if (!empty($tmp)) {
1755                        $submits[] = $tmp;
1756                    }
1757                    break;
1758            }
1759        }
1760
1761        return $submits;
1762    }
1763
1764    public function doActions()
1765    {
1766        if (empty($_POST) || !empty($_REQUEST['conf'])) {
1767            return;
1768        }
1769
1770        $modules = !empty($_POST['modules']) && is_array($_POST['modules']) ? array_values($_POST['modules']) : [];
1771
1772        if (!empty($_POST['select'])) {
1773
1774            # Can select only one theme at a time!
1775            if (is_array($_POST['select'])) {
1776                $modules = array_keys($_POST['select']);
1777                $id      = $modules[0];
1778
1779                if (!$this->modules->moduleExists($id)) {
1780                    throw new Exception(__('No such theme.'));
1781                }
1782
1783                $this->core->blog->settings->addNamespace('system');
1784                $this->core->blog->settings->system->put('theme', $id);
1785                $this->core->blog->triggerBlog();
1786
1787                dcPage::addSuccessNotice(__('Theme has been successfully selected.'));
1788                http::redirect($this->getURL() . '#themes');
1789            }
1790        } else {
1791            if (!$this->isWritablePath()) {
1792                return;
1793            }
1794
1795            if ($this->core->auth->isSuperAdmin() && !empty($_POST['activate'])) {
1796
1797                if (is_array($_POST['activate'])) {
1798                    $modules = array_keys($_POST['activate']);
1799                }
1800
1801                $list = $this->modules->getDisabledModules();
1802                if (empty($list)) {
1803                    throw new Exception(__('No such theme.'));
1804                }
1805
1806                $count = 0;
1807                foreach ($list as $id => $module) {
1808
1809                    if (!in_array($id, $modules)) {
1810                        continue;
1811                    }
1812
1813                    # --BEHAVIOR-- themeBeforeActivate
1814                    $this->core->callBehavior('themeBeforeActivate', $id);
1815
1816                    $this->modules->activateModule($id);
1817
1818                    # --BEHAVIOR-- themeAfterActivate
1819                    $this->core->callBehavior('themeAfterActivate', $id);
1820
1821                    $count++;
1822                }
1823
1824                dcPage::addSuccessNotice(
1825                    __('Theme has been successfully activated.', 'Themes have been successuflly activated.', $count)
1826                );
1827                http::redirect($this->getURL());
1828            } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['deactivate'])) {
1829
1830                if (is_array($_POST['deactivate'])) {
1831                    $modules = array_keys($_POST['deactivate']);
1832                }
1833
1834                $list = $this->modules->getModules();
1835                if (empty($list)) {
1836                    throw new Exception(__('No such theme.'));
1837                }
1838
1839                $failed = false;
1840                $count  = 0;
1841                foreach ($list as $id => $module) {
1842
1843                    if (!in_array($id, $modules)) {
1844                        continue;
1845                    }
1846
1847                    if (!$module['root_writable']) {
1848                        $failed = true;
1849                        continue;
1850                    }
1851
1852                    $module[$id] = $id;
1853
1854                    # --BEHAVIOR-- themeBeforeDeactivate
1855                    $this->core->callBehavior('themeBeforeDeactivate', $module);
1856
1857                    $this->modules->deactivateModule($id);
1858
1859                    # --BEHAVIOR-- themeAfterDeactivate
1860                    $this->core->callBehavior('themeAfterDeactivate', $module);
1861
1862                    $count++;
1863                }
1864
1865                if ($failed) {
1866                    dcPage::addWarningNotice(__('Some themes have not been deactivated.'));
1867                } else {
1868                    dcPage::addSuccessNotice(
1869                        __('Theme has been successfully deactivated.', 'Themes have been successuflly deactivated.', $count)
1870                    );
1871                }
1872                http::redirect($this->getURL());
1873            } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['delete'])) {
1874
1875                if (is_array($_POST['delete'])) {
1876                    $modules = array_keys($_POST['delete']);
1877                }
1878
1879                $list = $this->modules->getDisabledModules();
1880
1881                $failed = false;
1882                $count  = 0;
1883                foreach ($modules as $id) {
1884                    if (!isset($list[$id])) {
1885
1886                        if (!$this->modules->moduleExists($id)) {
1887                            throw new Exception(__('No such theme.'));
1888                        }
1889
1890                        $module       = $this->modules->getModules($id);
1891                        $module['id'] = $id;
1892
1893                        if (!$this->isDeletablePath($module['root'])) {
1894                            $failed = true;
1895                            continue;
1896                        }
1897
1898                        # --BEHAVIOR-- themeBeforeDelete
1899                        $this->core->callBehavior('themeBeforeDelete', $module);
1900
1901                        $this->modules->deleteModule($id);
1902
1903                        # --BEHAVIOR-- themeAfterDelete
1904                        $this->core->callBehavior('themeAfterDelete', $module);
1905                    } else {
1906                        $this->modules->deleteModule($id, true);
1907                    }
1908
1909                    $count++;
1910                }
1911
1912                if (!$count && $failed) {
1913                    throw new Exception(__("You don't have permissions to delete this theme."));
1914                } elseif ($failed) {
1915                    dcPage::addWarningNotice(__('Some themes have not been delete.'));
1916                } else {
1917                    dcPage::addSuccessNotice(
1918                        __('Theme has been successfully deleted.', 'Themes have been successuflly deleted.', $count)
1919                    );
1920                }
1921                http::redirect($this->getURL());
1922            } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['install'])) {
1923
1924                if (is_array($_POST['install'])) {
1925                    $modules = array_keys($_POST['install']);
1926                }
1927
1928                $list = $this->store->get();
1929
1930                if (empty($list)) {
1931                    throw new Exception(__('No such theme.'));
1932                }
1933
1934                $count = 0;
1935                foreach ($list as $id => $module) {
1936
1937                    if (!in_array($id, $modules)) {
1938                        continue;
1939                    }
1940
1941                    $dest = $this->getPath() . '/' . basename($module['file']);
1942
1943                    # --BEHAVIOR-- themeBeforeAdd
1944                    $this->core->callBehavior('themeBeforeAdd', $module);
1945
1946                    $this->store->process($module['file'], $dest);
1947
1948                    # --BEHAVIOR-- themeAfterAdd
1949                    $this->core->callBehavior('themeAfterAdd', $module);
1950
1951                    $count++;
1952                }
1953
1954                dcPage::addSuccessNotice(
1955                    __('Theme has been successfully installed.', 'Themes have been successuflly installed.', $count)
1956                );
1957                http::redirect($this->getURL());
1958            } elseif ($this->core->auth->isSuperAdmin() && !empty($_POST['update'])) {
1959
1960                if (is_array($_POST['update'])) {
1961                    $modules = array_keys($_POST['update']);
1962                }
1963
1964                $list = $this->store->get(true);
1965                if (empty($list)) {
1966                    throw new Exception(__('No such theme.'));
1967                }
1968
1969                $count = 0;
1970                foreach ($list as $module) {
1971
1972                    if (!in_array($module['id'], $modules)) {
1973                        continue;
1974                    }
1975
1976                    $dest = $module['root'] . '/../' . basename($module['file']);
1977
1978                    # --BEHAVIOR-- themeBeforeUpdate
1979                    $this->core->callBehavior('themeBeforeUpdate', $module);
1980
1981                    $this->store->process($module['file'], $dest);
1982
1983                    # --BEHAVIOR-- themeAfterUpdate
1984                    $this->core->callBehavior('themeAfterUpdate', $module);
1985
1986                    $count++;
1987                }
1988
1989                $tab = $count && $count == count($list) ? '#themes' : '#update';
1990
1991                dcPage::addSuccessNotice(
1992                    __('Theme has been successfully updated.', 'Themes have been successuflly updated.', $count)
1993                );
1994                http::redirect($this->getURL() . $tab);
1995            }
1996
1997            # Manual actions
1998            elseif (!empty($_POST['upload_pkg']) && !empty($_FILES['pkg_file'])
1999                || !empty($_POST['fetch_pkg']) && !empty($_POST['pkg_url'])) {
2000                if (empty($_POST['your_pwd']) || !$this->core->auth->checkPassword($_POST['your_pwd'])) {
2001                    throw new Exception(__('Password verification failed'));
2002                }
2003
2004                if (!empty($_POST['upload_pkg'])) {
2005                    files::uploadStatus($_FILES['pkg_file']);
2006
2007                    $dest = $this->getPath() . '/' . $_FILES['pkg_file']['name'];
2008                    if (!move_uploaded_file($_FILES['pkg_file']['tmp_name'], $dest)) {
2009                        throw new Exception(__('Unable to move uploaded file.'));
2010                    }
2011                } else {
2012                    $url  = urldecode($_POST['pkg_url']);
2013                    $dest = $this->getPath() . '/' . basename($url);
2014                    $this->store->download($url, $dest);
2015                }
2016
2017                # --BEHAVIOR-- themeBeforeAdd
2018                $this->core->callBehavior('themeBeforeAdd', null);
2019
2020                $ret_code = $this->store->install($dest);
2021
2022                # --BEHAVIOR-- themeAfterAdd
2023                $this->core->callBehavior('themeAfterAdd', null);
2024
2025                dcPage::addSuccessNotice($ret_code == 2 ?
2026                    __('Theme has been successfully updated.') :
2027                    __('Theme has been successfully installed.')
2028                );
2029                http::redirect($this->getURL() . '#themes');
2030            } else {
2031
2032                # --BEHAVIOR-- adminModulesListDoActions
2033                $this->core->callBehavior('adminModulesListDoActions', $this, $modules, 'theme');
2034
2035            }
2036        }
2037
2038        return;
2039    }
2040}
Note: See TracBrowser for help on using the repository browser.

Sites map