Passed
Pull Request — master (#1467)
by Michael
05:54
created

XoopsGuiDefault::getComposerData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9666
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
xoops_load('gui', 'system');
13
14
/*
15
 * Xoops Cpanel default GUI class
16
 *
17
 * @copyright   (c) 2000-2016 XOOPS Project (www.xoops.org)
18
 * @license     GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @package     system
20
 * @usbpackage  GUI
21
 * @since       2.4
22
 * @author      Mamba       <[email protected]>
23
 * @author      Mojtabajml  <[email protected]>
24
 * @author      Voltan      <[email protected]>
25
 * @author      BitC3R0     <[email protected]>
26
 * @author      trabis      <[email protected]>
27
 */
28
29
/**
30
 * Class XoopsGuiDefault
31
 */
32
class XoopsGuiDefault extends XoopsSystemGui
33
{
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public static function validate()
45
    {
46
        return true;
47
    }
48
49
    public function header()
50
    {
51
        parent::header();
52
53
        global $xoopsConfig, $xoopsUser, $xoopsModule, $xoTheme, $xoopsTpl, $xoopsDB;
54
        $tpl =& $this->template;
55
56
        $xoTheme->addScript('browse.php?Frameworks/jquery/jquery.js');
57
        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/styleswitch.js');
58
        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/formenu.js');
59
        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/menu.js');
60
        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/tooltip.js');
61
//        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/tabs.jquery.tools.min.js');
62
        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/tabs.js');
63
        $xoTheme->addScript(XOOPS_ADMINTHEME_URL . '/default/js/tabs.slideshow.js');
64
65
        $xoTheme->addStylesheet(XOOPS_ADMINTHEME_URL . '/default/css/style.css');
66
        $xoTheme->addStylesheet(XOOPS_ADMINTHEME_URL . '/default/css/dark.css', array('title' => 'dark', 'media' => 'screen'));
67
        $xoTheme->addStylesheet(XOOPS_ADMINTHEME_URL . '/default/css/silver.css', array('title' => 'silver', 'media' => 'screen'));
68
        $xoTheme->addStylesheet(XOOPS_ADMINTHEME_URL . '/default/css/orange.css', array('title' => 'orange', 'media' => 'screen'));
69
70
        $tpl->assign('lang_cp', _CPHOME);
71
        //start system overview
72
        //$tpl->assign('lang_xoops_version', XOOPS_VERSION);
73
        $tpl->assign('lang_php_version', PHP_VERSION);
74
        $tpl->assign('lang_smarty_version', $tpl::SMARTY_VERSION);
75
        $tpl->assign('lang_mysql_version', mysqli_get_server_info($xoopsDB->conn));
76
        $tpl->assign('lang_server_api', PHP_SAPI);
77
        $tpl->assign('lang_os_name', PHP_OS);
78
//        $tpl->assign('safe_mode', ini_get('safe_mode') ? 'On' : 'Off');
79
//        $tpl->assign('register_globals', ini_get('register_globals') ? 'On' : 'Off');
80
//        $tpl->assign('magic_quotes_gpc', ini_get('magic_quotes_gpc') ? 'On' : 'Off');
81
        $tpl->assign('allow_url_fopen', ini_get('allow_url_fopen') ? 'On' : 'Off');
82
        $tpl->assign('fsockopen', function_exists('fsockopen') ? 'On' : 'Off');
83
//        $tpl->assign('allow_call_time_pass_reference', ini_get('allow_call_time_pass_reference') ? 'On' : 'Off');
84
        $tpl->assign('post_max_size', ini_get('post_max_size'));
85
        $tpl->assign('max_input_time', ini_get('max_input_time'));
86
        $tpl->assign('output_buffering', ini_get('output_buffering'));
87
        $tpl->assign('max_execution_time', ini_get('max_execution_time'));
88
        $tpl->assign('memory_limit', ini_get('memory_limit'));
89
        $tpl->assign('file_uploads', ini_get('file_uploads') ? 'On' : 'Off');
90
        $tpl->assign('upload_max_filesize', ini_get('upload_max_filesize'));
91
        $tpl->assign('xoops_sitename', $xoopsConfig['sitename']);
92
93
        // COMPOSER PACKAGES VERSION INFO *******************
94
95
        try {
96
            // Define the path to the composer.lock file
97
            $composerLockPath = XOOPS_ROOT_PATH . '/class/libraries/composer.lock';
98
            // Get the packages data from composer.lock file
99
            $packages = $this->getComposerData($composerLockPath);
100
            // Extract package name and version
101
            $composerPackages = $this->extractPackages($packages);
102
            // Assign the $composerPackages array to the Smarty template
103
            $tpl->assign('composerPackages', $composerPackages);
104
        } catch (Exception $e) {
105
            // Handle any exception and log the error using XOOPS Logger
106
            global $xoopsLogger;
107
            $xoopsLogger->handleError(E_USER_ERROR, $e->getMessage(), __FILE__, __LINE__);
108
            echo "An error occurred. Please try again later.";
109
        }
110
111
        // ADD MENU *****************************************
112
113
        //Add  CONTROL PANEL  Menu  items
114
        $menu                = array();
115
        $menu[0]['link']     = XOOPS_URL;
116
        $menu[0]['title']    = _YOURHOME;
117
        $menu[0]['absolute'] = 1;
118
        $menu[1]['link']     = XOOPS_URL . '/admin.php?xoopsorgnews=1';
119
        $menu[1]['title']    = _OXYGEN_NEWS;
120
        $menu[1]['absolute'] = 1;
121
        $menu[1]['icon']     = XOOPS_ADMINTHEME_URL . '/default/images/xoops.png';
122
        $menu[2]['link']     = XOOPS_URL . '/user.php?op=logout';
123
        $menu[2]['title']    = _LOGOUT;
124
        $menu[2]['absolute'] = 1;
125
        $menu[2]['icon']     = XOOPS_ADMINTHEME_URL . '/default/images/logout.png';
126
        $tpl->append('navitems', array('link' => XOOPS_URL . '/admin.php', 'text' => _CPHOME, 'menu' => $menu));
127
128
        //add SYSTEM  Menu items
129
        include __DIR__ . '/menu.php';
130
        if (empty($xoopsModule) || 'system' === $xoopsModule->getVar('dirname', 'n')) {
131
            $modpath = XOOPS_URL . '/admin.php';
132
            $modname = _OXYGEN_SYSOPTIONS;
133
            $modid   = 1;
134
            $moddir  = 'system';
135
136
            $mod_options = $adminmenu;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $adminmenu seems to be never defined.
Loading history...
137
            foreach (array_keys($mod_options) as $item) {
138
                $mod_options[$item]['link'] = empty($mod_options[$item]['absolute']) ? XOOPS_URL . '/modules/' . $moddir . '/' . $mod_options[$item]['link'] : $mod_options[$item]['link'];
139
                $mod_options[$item]['icon'] = empty($mod_options[$item]['icon']) ? '' : XOOPS_ADMINTHEME_URL . '/default/' . $mod_options[$item]['icon'];
140
                unset($mod_options[$item]['icon_small']);
141
            }
142
        } else {
143
            $moddir  = $xoopsModule->getVar('dirname', 'n');
144
            $modpath = XOOPS_URL . '/modules/' . $moddir;
145
            $modname = $xoopsModule->getInfo('name') . '  (' . $xoopsModule->getInfo('version') . ')';
146
            $modid   = $xoopsModule->getVar('mid');
147
148
            $mod_options = $xoopsModule->getAdminMenu();
149
            foreach (array_keys($mod_options) as $item) {
150
                $mod_options[$item]['link'] = empty($mod_options[$item]['absolute']) ? XOOPS_URL . "/modules/{$moddir}/" . $mod_options[$item]['link'] : $mod_options[$item]['link'];
151
                //                $mod_options[$item]['icon'] = empty($mod_options[$item]['icon']) ? '' : XOOPS_URL . "/modules/{$moddir}/" . $mod_options[$item]['icon'];
152
                //mb for direct URL access to icons in modules Admin
153
                $mod_options[$item]['icon'] = empty($mod_options[$item]['icon']) ? '' : (filter_var($mod_options[$item]['icon'], FILTER_VALIDATE_URL) ? $mod_options[$item]['icon'] : (XOOPS_URL . "/modules/{$moddir}/" . $mod_options[$item]['icon']));
154
            }
155
        }
156
157
        $tpl->assign('mod_options', $mod_options);
158
        $tpl->assign('modpath', $modpath);
159
        $tpl->assign('modname', $modname);
160
        $tpl->assign('modid', $modid);
161
        $tpl->assign('moddir', $moddir);
162
163
        // add MODULES  Menu items
164
        /** @var XoopsModuleHandler $module_handler */
165
        $module_handler = xoops_getHandler('module');
166
        $criteria       = new CriteriaCompo();
167
        $criteria->add(new Criteria('hasadmin', 1));
168
        $criteria->add(new Criteria('isactive', 1));
169
        $criteria->setSort('mid');
170
        $mods = $module_handler->getObjects($criteria);
171
172
        $menu               = array();
173
        /** @var XoopsGroupPermHandler $moduleperm_handler */
174
        $moduleperm_handler = xoops_getHandler('groupperm');
175
        foreach ($mods as $mod) {
176
            $rtn        = array();
177
            $modOptions = array();                                                         //add for sub menus
178
            $sadmin     = $moduleperm_handler->checkRight('module_admin', $mod->getVar('mid'), $xoopsUser->getGroups());
179
            if ($sadmin) {
180
                $info = $mod->getInfo();
181
                if (!empty($info['adminindex'])) {
182
                    $rtn['link'] = XOOPS_URL . '/modules/' . $mod->getVar('dirname', 'n') . '/' . $info['adminindex'];
183
                } else {
184
                    $rtn['link'] = XOOPS_URL . '/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod=' . $mod->getVar('mid');
185
                }
186
                $rtn['title']    = htmlspecialchars($mod->name(), ENT_QUOTES | ENT_HTML5);
187
                $rtn['absolute'] = 1;
188
                $rtn['url']      = XOOPS_URL . '/modules/' . $mod->getVar('dirname', 'n') . '/'; //add for sub menus
189
                $modOptions      = $mod->getAdminMenu();                                        //add for sub menus
190
                $rtn['options']  = $modOptions;                                             //add for sub menus
191
192
                if (isset($info['icon']) && $info['icon'] !== '') {
193
                    $rtn['icon'] = XOOPS_URL . '/modules/' . $mod->getVar('dirname', 'n') . '/' . $info['icon'];
194
                }
195
                $menu[] = $rtn;
196
            }
197
        }
198
        $tpl->append('navitems', array(
199
            'link' => XOOPS_URL . '/modules/system/admin.php?fct=modulesadmin',
200
            'text' => _AM_SYSTEM_MODULES,
201
            'dir'  => $mod->getVar('dirname', 'n'),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mod seems to be defined by a foreach iteration on line 175. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
202
            'menu' => $menu));
203
204
        // add preferences menu
205
        $menu = array();
206
207
        $OPT   = array();
208
        $OPT[] = array(
209
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=1',
210
            'title'    => _OXYGEN_GENERAL,
211
            'absolute' => 1,
212
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
213
        $OPT[] = array(
214
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=2',
215
            'title'    => _OXYGEN_USERSETTINGS,
216
            'absolute' => 1,
217
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
218
        $OPT[] = array(
219
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=3',
220
            'title'    => _OXYGEN_METAFOOTER,
221
            'absolute' => 1,
222
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
223
        $OPT[] = array(
224
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=4',
225
            'title'    => _OXYGEN_CENSOR,
226
            'absolute' => 1,
227
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
228
        $OPT[] = array(
229
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=5',
230
            'title'    => _OXYGEN_SEARCH,
231
            'absolute' => 1,
232
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
233
        $OPT[] = array(
234
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=6',
235
            'title'    => _OXYGEN_MAILER,
236
            'absolute' => 1,
237
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
238
        $OPT[] = array(
239
            'link'     => 'admin.php?fct=preferences&amp;op=show&amp;confcat_id=7',
240
            'title'    => _OXYGEN_AUTHENTICATION,
241
            'absolute' => 1,
242
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
243
        $OPT[] = array(
244
            'link'     => 'admin.php?fct=preferences&amp;op=showmod&amp;mod=1',
245
            'title'    => _OXYGEN_MODULESETTINGS,
246
            'absolute' => 1,
247
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/icons/prefs_small.png');
248
249
        $menu[] = array(
250
            'link'     => XOOPS_URL . '/modules/system/admin.php?fct=preferences',
251
            'title'    => _OXYGEN_SYSOPTIONS,
252
            'absolute' => 1,
253
            'url'      => XOOPS_URL . '/modules/system/',
254
            'options'  => $OPT);
255
256
        foreach ($mods as $mod) {
257
            $rtn    = array();
258
            $sadmin = $moduleperm_handler->checkRight('module_admin', $mod->getVar('mid'), $xoopsUser->getGroups());
259
            if ($sadmin && ($mod->getVar('hasnotification') || \is_array($mod->getInfo('config')) || \is_array($mod->getInfo('comments')))) {
260
                $rtn['link']     = XOOPS_URL . '/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod=' . $mod->getVar('mid');
261
                $rtn['title']    = htmlspecialchars($mod->name(), ENT_QUOTES | ENT_HTML5);
262
                $rtn['absolute'] = 1;
263
                $rtn['icon']     = XOOPS_ADMINTHEME_URL . '/gui/oxygen/icons/prefs_small.png';
264
                $menu[]          = $rtn;
265
            }
266
        }
267
        $tpl->append('navitems', array(
268
            'link' => XOOPS_URL . '/modules/system/admin.php?fct=preferences',
269
            'text' => _OXYGEN_SITEPREF,
270
            'dir'  => $mod->getVar('dirname', 'n'),
271
            'menu' => $menu));
272
273
        //add OPTIONS/Links Menu Items
274
        $menu   = array();
275
        $menu[] = array(
276
            'link'     => 'https://xoops.org',
277
            'title'    => _OXYGEN_XOOPSPROJECT,
278
            'absolute' => 1);
279
        $menu[] = array(
280
            'link'     => 'https://xoops.org',
281
            'title'    => _OXYGEN_WEBSITE,
282
            'absolute' => 1,
283
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/images/xoops.png');
284
        $menu[] = array(
285
            'link'     => 'https://xoops.org/modules/repository/',
286
            'title'    => _OXYGEN_XOOPSMODULES,
287
            'absolute' => 1,
288
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/images/xoops.png');
289
        $menu[] = array(
290
            'link'     => 'https://xoops.org/modules/extgallery/',
291
            'title'    => _OXYGEN_XOOPSTHEMES,
292
            'absolute' => 1,
293
            'icon'     => XOOPS_ADMINTHEME_URL . '/default/images/tweb.png');
294
295
        $tpl->append('navitems', array('link' => XOOPS_URL . '/admin.php', 'text' => _OXYGEN_INTERESTSITES, 'menu' => $menu));
296
297
        //add OPTIONS/links for local support
298
        if (file_exists($file = XOOPS_ADMINTHEME_PATH . '/default/language/' . $xoopsConfig['language'] . '/localsupport.php')) {
299
            $links = include XOOPS_ADMINTHEME_PATH . '/default/language/' . $xoopsConfig['language'] . '/localsupport.php';
300
            if (count($links) > 0) {
301
                $tpl->append('navitems', array('link' => XOOPS_URL . '/admin.php', 'text' => _OXYGEN_LOCALSUPPORT, 'menu' => $links));
302
            }
303
        }
304
305
        if (is_object($xoopsModule) || !empty($_GET['xoopsorgnews'])) {
306
            if (is_object($xoopsModule) && file_exists($file = XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/' . $xoopsModule->getInfo('adminmenu'))) {
307
                include $file;
308
            }
309
310
            return null;
311
        }
312
313
        foreach ($mods as $mod) {
314
            $sadmin = $moduleperm_handler->checkRight('module_admin', $mod->getVar('mid'), $xoopsUser->getGroups());
315
            if ($sadmin) {
316
                $rtn  = array();
317
                $info = $mod->getInfo();
318
                if (!empty($info ['adminindex'])) {
319
                    $rtn ['link'] = XOOPS_URL . '/modules/' . $mod->getVar('dirname', 'n') . '/' . $info ['adminindex'];
320
                } else {
321
                    $rtn ['link'] = XOOPS_URL . '/modules/system/admin.php?fct=preferences&amp;op=showmod&amp;mod=' . $mod->getVar('mid');
322
                }
323
                $rtn ['title']       = htmlspecialchars($mod->getVar('name'), ENT_QUOTES | ENT_HTML5);
324
                $rtn ['description'] = $mod->getInfo('description');
325
                $rtn ['absolute']    = 1;
326
                if (isset($info ['icon_big'])) {
327
                    $rtn ['icon'] = XOOPS_URL . '/modules/' . $mod->getVar('dirname', 'n') . '/' . $info ['icon_big'];
328
                } elseif (isset($info ['image'])) {
329
                    $rtn ['icon'] = XOOPS_URL . '/modules/' . $mod->getVar('dirname', 'n') . '/' . $info ['image'];
330
                }
331
332
                $tpl->append('modules', $rtn);
333
            }
334
        }
335
    }
336
337
    // Function to read and parse composer.lock file
338
    private function getComposerData(string $composerLockPath): array
339
    {
340
        if (!file_exists($composerLockPath)) {
341
            throw new InvalidArgumentException("File not found at: " . $composerLockPath);
342
        }
343
344
        $composerLockData = file_get_contents($composerLockPath);
345
346
        if ($composerLockData === false) {
347
            throw new RuntimeException("Failed to read the file: " . $composerLockPath);
348
        }
349
350
        $composerData = json_decode($composerLockData, true);
351
352
        if (json_last_error() !== JSON_ERROR_NONE) {
353
            throw new JsonException("Failed to decode JSON data: " . json_last_error_msg());
354
        }
355
356
        return $composerData['packages'] ?? [];
357
    }
358
359
360
    // Function to extract package name and version (using array_map for optimization)
361
    private function extractPackages(array $packages): array
362
    {
363
        return array_map(
364
            static fn($package) => [
365
                'name'    => $package['name'],
366
                'version' => $package['version']
367
            ], $packages
368
        );
369
    }
370
}
371