Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

Components::change_url_access()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 18

Duplication

Lines 19
Ratio 73.08 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 26
rs 8.5806
cc 4
eloc 18
nc 4
nop 0
1
<?php
2
3
use CMSFactory\Events;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Image CMS
11
 * Components Class
12
 * @property Cms_hooks $cms_hooks
13
 * @property Lib_admin $lib_admin
14
 */
15
class Components extends BaseAdminController
16
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
17
18
    /**
19
     * array of installed modules
20
     * @var array
21
     */
22
    private $installed = [];
23
24
    private $permited = [];
25
26 View Code Duplication
    public function __construct() {
27
        parent::__construct();
28
29
        $this->load->library('DX_Auth');
30
31
        admin_or_redirect();
32
33
        $this->load->library('lib_admin');
34
        $this->lib_admin->init_settings();
35
        $this->setInstalled();
36
        $this->setPermited();
37
    }
38
39
    public function index() {
40
        $this->modules_table();
41
    }
42
43
    public function modules_table() {
44
        $not_installed = [];
45
46
        $fs_modules = $this->find_components();
47
        $db_modules = $this->db->order_by('position', 'asc')->not_like('identif', 'payment_method_')->get('components')->result_array();
48
49
        // Find not installed modules
50
        $count = count($fs_modules);
51
        for ($i = 0; $i < $count; $i++) {
52
            if ($this->is_installed($fs_modules[$i]['com_name']) == 0) {
53
                $info = $this->get_module_info($fs_modules[$i]['com_name']);
54
55
                $fs_modules[$i]['name'] = $info['menu_name'];
56
                $fs_modules[$i]['version'] = $info['version'];
57
                $fs_modules[$i]['description'] = $info['description'];
58
                $fs_modules[$i]['icon_class'] = $info['icon_class'];
59
60
                array_push($not_installed, $fs_modules[$i]);
61
            }
62
        }
63
64
        // process modules info
65
        $count = count($db_modules);
66
        for ($i = 0; $i < $count; $i++) {
67
            $module_name = $db_modules[$i]['name'];
68
            if ($this->module_exists($module_name)) {
69
70
                $info = $this->get_module_info($module_name);
71
                $db_modules[$i]['menu_name'] = $info['menu_name'];
72
                $db_modules[$i]['version'] = $info['version'];
73
                $db_modules[$i]['description'] = $info['description'];
74
                $db_modules[$i]['icon_class'] = $info['icon_class'];
75
                $db_modules[$i]['identif'] = $db_modules[$i]['identif'];
76
                $modulePath = getModulePath($module_name);
77
                if (file_exists($modulePath . 'admin.php')) {
78
                    $db_modules[$i]['admin_file'] = 1;
79
                } else {
80
                    $db_modules[$i]['admin_file'] = 0;
81
                }
82
            } else {
83
                unset($db_modules[$i]);
84
            }
85
        }
86
87
        if (MAINSITE != '') {
88
            list($db_modules, $not_installed) = $this->isPermitedModules($db_modules, $not_installed);
89
        }
90
91
        Events::create()->registerEvent(
92
            [
93
                'installed' => $db_modules,
94
                'not_installed' => $not_installed
95
            ],
96
            'Components:modules_table'
97
        )->runFactory();
98
99
        $frozen_autoload = ['template_manager', 'admin_menu', 'xbanners', 'menu', 'cmsemail', 'shop'];
100
        $frozen_delete = ['template_manager', 'admin_menu', 'xbanners', 'menu', 'cmsemail', 'shop', 'mod_discount', 'auth'];
101
102
        $this->template->assign('frozen_autoload', $frozen_autoload);
103
        $this->template->assign('frozen_delete', $frozen_delete);
104
        $this->template->assign('installed', $db_modules);
105
        $this->template->assign('not_installed', $not_installed);
106
        $this->template->show('module_table', FALSE);
107
    }
108
109
    private function isNotPermited($moduleName) {
110
        if (MAINSITE != '') {
111
            return !in_array($moduleName, $this->permited);
112
        } else {
113
            return FALSE;
114
        }
115
    }
116
117
    private function isPermitedModules($db_modules, $not_installed) {
118
        foreach ($db_modules as $key => $db_module) {
119
            if ($this->isNotPermited($db_module['name'])) {
120
                unset($db_modules[$key]);
121
            }
122
        }
123
        foreach ($not_installed as $key => $db_module) {
124
            if ($this->isNotPermited($db_module['com_name'])) {
125
                unset($not_installed[$key]);
126
            }
127
        }
128
        return [$db_modules, $not_installed];
129
    }
130
131
    private function setInstalled() {
132
        $installed = $this->db->select('name')->get('components')->result_array();
133
        $this->installed = array_column($installed, 'name');
134
    }
135
136
    private function setPermited() {
137
        if (MAINSITE != '' and $this->load->module('mainsaas')) {
138
            $this->permited = $this->load->module('mainsaas')->getNotPermited();
139
            $this->permited = array_map('trim', $this->permited);
140
        }
141
    }
142
143
    public function is_installed($mod_name) {
144
        return in_array($mod_name, $this->installed);
145
    }
146
147
    public function install($module = '') {
148
        //cp_check_perm('module_install');
149
150
        $module = strtolower($module);
151
152
        ($hook = get_hook('admin_install_module')) ? eval($hook) : NULL;
0 ignored issues
show
Unused Code introduced by
The call to get_hook() has too many arguments starting with 'admin_install_module'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
153
154
        $modulePath = getModulePath($module);
155
156
        if (file_exists($modulePath . $module . '.php') AND $this->is_installed($module) == 0) {
157
            // Make module install
158
            $data = [
159
                'name' => $module,
160
                'identif' => $module
161
            ];
162
163
            $this->db->insert('components', $data);
164
            $this->load->module($module);
165
166
            if (method_exists($module, '_install') === TRUE) {
167
                $this->$module->_install();
168
            }
169
170
            // Update hooks
171
            $this->load->library('cms_hooks');
172
            $this->cms_hooks->build_hooks();
173
174
            $this->lib_admin->log(lang('Installed a module', 'admin') . ' ' . $data['name']);
175
176 View Code Duplication
            if ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') {
177
                $result = true;
178
                echo json_encode(['result' => $result]);
179
            } else {
180
                return TRUE;
181
            }
182 View Code Duplication
        } else {
183
            if ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') {
184
                $result = true;
185
                echo json_encode(['result' => $result]);
186
            } else {
187
                return FALSE;
188
            }
189
        }
190
    }
191
192
    /**
193
     * @param string $moduleName
194
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
195
     */
196
    public function deinstall($moduleName) {
197
        $modules = $this->input->post('ids') ?: [$moduleName];
198
199
        foreach ($modules as $module) {
200
            $module = strtolower($module);
201
202
            ($hook = get_hook('admin_deinstall_module')) ? eval($hook) : NULL;
0 ignored issues
show
Unused Code introduced by
The call to get_hook() has too many arguments starting with 'admin_deinstall_module'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
203
204
            $modulePath = getModulePath($module);
205
206
            if (file_exists($modulePath . $module . '.php') AND $this->is_installed($module) == 1) {
207
                $this->load->module($module);
208
209
                if (method_exists($module, '_deinstall') === TRUE) {
210
                    $this->$module->_deinstall();
211
                }
212
213
                $this->db->limit(1);
214
                $this->db->delete('components', ['name' => $module]);
215
                $this->lib_admin->log(lang('Deleted a module', 'admin') . ' ' . $module);
216
                if (PHP_SAPI == 'cli') {
217
                    return true;
218
                }
219
                showMessage(lang('The module successfully uninstall', 'admin'));
220
                pjax('/admin/components/modules_table');
221
            } else {
222
                if (PHP_SAPI == 'cli') {
223
                    return false;
224
                }
225
                showMessage(lang('Module deletion error', 'admin'), false, 'r');
226
                pjax('/admin/components/modules_table');
227
            }
228
229
            // Update hooks
230
            $this->load->library('cms_hooks');
231
            $this->cms_hooks->build_hooks();
232
        }
233
    }
234
235
    /**
236
     * Check is module exists
237
     * @param string $module_name module name
238
     * @return boolean
239
     */
240
    public function module_exists($module_name) {
241
        return moduleExists($module_name);
242
    }
243
244
    public function find_components($in_menu = FALSE) {
245
        $components = [];
246
        if ($in_menu == TRUE) {
247
            $this->db->where('in_menu', 1);
248
        }
249
        $this->db->not_like('identif', 'payment_method_');
250
        $installed = $this->db->get('components')->result_array();
251
252
        $modulesPaths = getModulesPaths();
253
        foreach ($modulesPaths as $moduleName => $modulePath) {
254
255
            $info_file = $modulePath . 'module_info.php';
256
            $com_file_admin = $modulePath . 'admin.php';
257
258
            $lang = new MY_Lang();
259
            $lang->load($moduleName);
260
261
            if (file_exists($info_file)) {
262
                include $info_file;
263
264
                if (file_exists($com_file_admin)) {
265
                    $admin_file = 1;
266
                } else {
267
                    $admin_file = 0;
268
                }
269
270
                $ins = FALSE;
271
272
                foreach ($installed as $k) {
273
                    if ($k['name'] == $moduleName) {
274
                        $ins = TRUE;
275
                    }
276
                }
277
278
                $new_com = [
279
                    'menu_name' => $com_info['menu_name'],
0 ignored issues
show
Bug introduced by
The variable $com_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
280
                    'com_name' => $moduleName,
281
                    'admin_file' => $admin_file,
282
                    'installed' => $ins,
283
                    'type' => $com_info['type'],
284
                ];
285
286
                array_push($components, $new_com);
287
            }
288
        }
289
        return $components;
290
    }
291
292
    /**
293
     * Get components which show in menu and have admin.php
294
     * @return array|boolean
295
     */
296
    public function find_components_for_menu_list() {
297
        /** Get all components which show in menu */
298
        $components = $this->db->where('in_menu', 1);
299
300
        if (MAINSITE) {
301
            $components = $components->order_by('name', 'asc');
302
        } else {
303
            $components = $components->order_by('position', 'asc');
304
        }
305
306
        $components = $components->get('components')
307
            ->result_array();
308
309
        if (MAINSITE != '') {
310
            $components = $this->isPermitedModules($components, []);
311
            $components = $components[0];
312
        }
313
        /*         * If not components for show in menu */
314
        if (!$components) {
315
            return false;
316
        } else {
317
            /** Delete components which not have admin.php */
318
            foreach ($components as $key => $value) {
319
                if (!file_exists(getModulePath($value['name']) . 'admin.php')) {
320
                    unset($components[$key]);
321
                } else {
322
                    $info_file = getModulePath($value['name']) . 'module_info.php';
323
                    $lang = new MY_Lang();
324
                    $lang->load($value['name']);
325
326
                    if (file_exists($info_file)) {
327
                        include $info_file;
328
                        $components[$key]['type'] = $com_info['type'];
0 ignored issues
show
Bug introduced by
The variable $com_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
329
                        $components[$key] = array_merge($components[$key], $com_info);
330
                    }
331
                }
332
            }
333
        }
334
        return $components;
335
    }
336
337
    /**
338
     * @param string $component
339
     */
340
    public function component_settings($component) {
341
342
        $this->db->where('name', $component);
343
        $query = $this->db->get('components', 1);
344
345
        if ($query->num_rows() == 1) {
346
            $com = $query->row_array();
347
            $this->template->add_array($com);
348
        } else {
349
            $this->template->assign('com_name', $component);
350
            $this->template->assign('identif', $component);
351
            $this->template->assign('status', 0);
352
        }
353
354
        $this->template->show('component_settings', FALSE);
355
    }
356
357
    /**
358
     * Save component settings
359
     * @param string $component
360
     */
361
    public function save_settings($component) {
362
        //cp_check_perm('module_admin');
363
364
        $this->db->where('name', $component);
365
        $query = $this->db->get('components', 1);
366
367
        $com = $query->row_array();
368
369
        ($hook = get_hook('admin_component_save_settings')) ? eval($hook) : NULL;
0 ignored issues
show
Unused Code introduced by
The call to get_hook() has too many arguments starting with 'admin_component_save_settings'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
370
371
        if ($query->num_rows() >= 1) {
372
            $data = [
373
                'enabled' => (int) $this->input->post('status'),
374
                //'identif' => $this->input->post('identif'),
375
                'identif' => $com['name'],
376
                'autoload' => (int) $this->input->post('autoload'),
377
                'in_menu' => (int) $this->input->post('in_menu')
378
            ];
379
380
            $this->db->where('name', $component);
381
            $this->db->update('components', $data);
382
383
            $this->lib_admin->log(lang('Changed the module settings', 'admin') . ' ' . $com['name']);
384
        }
385
386
        jsCode("ajax_div('modules_table',base_url + 'admin/components/modules_table/');");
387
    }
388
389
    /**
390
     * @param string $module
391
     */
392
    private function checkPerm($module) {
393
        if ($this->isNotPermited($module)) {
394
            $msg = lang('Error checking permissions');
395
            die($msg);
396
        }
397
    }
398
399
    /**
400
     * Load component admin class in iframe/xhr
401
     * @param string $module
402
     */
403
    public function init_window($module) {
404
        $this->checkPerm($module);
405
        $lang = new MY_Lang();
406
        $lang->load($module);
407
408
        // buildWindow($id,$title,$contentURL,$width,$height,$method = 'iframe')
409
        //$module = $this->input->post('component');
410
        $info_file = getModulePath($module) . 'module_info.php';
411
412
        if (file_exists($info_file)) {
413
            include_once $info_file;
414
415
            switch ($com_info['admin_type']) {
0 ignored issues
show
Bug introduced by
The variable $com_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
416
                case 'window':
417
                    //buildWindow($module . '_window', lang('Module','admin') . ': ' . $com_info['menu_name'], site_url('admin/components/cp/' . $module), $com_info['w'], $com_info['h'], $com_info['window_type']);
418
                    //pjax('/admin/components/cp/'.$module, '.row-fluid');
419
                    $this->cp($module);
420
                    break;
421
422
                case 'inside':
423
                    //pjax('/admin/components/cp/'.$module, '.row-fluid');
424
                    $this->cp($module);
425
                    //updateDiv('page', site_url('admin/components/cp/' . $module));
426
                    break;
427
            }
428
        }
429
    }
430
431
    /**
432
     * @param string $module
433
     */
434
    public function cp($module) {
435
        $this->checkPerm($module);
436
        $func = $this->uri->segment(5);
437
438
        if ($func == FALSE) {
439
            $func = 'index';
440
        }
441
442
        $this->load->module('core/core');
443
        $args = $this->core->grab_variables(6);
444
445
        $this->template->assign('SELF_URL', site_url('admin/components/cp/' . $module));
446
447
        //echo '<div id="' . $module . '_module_block">' . modules::run($module . '/admin/' . $func, $args) . '</div>';
448
        echo modules::run($module . '/admin/' . $func, $args);
449
450
        //ajax_links($module);
451
    }
452
453
    /**
454
     * @param string $module
455
     */
456
    public function run($module) {
457
        $this->checkPerm($module);
458
459
        $func = $this->uri->segment(5);
460
        if ($func == FALSE) {
461
            $func = 'index';
462
        }
463
464
        ($hook = get_hook('admin_run_module_admin')) ? eval($hook) : NULL;
0 ignored issues
show
Unused Code introduced by
The call to get_hook() has too many arguments starting with 'admin_run_module_admin'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
465
466
        $this->load->module('core/core');
467
        $args = $this->core->grab_variables(6);
468
469
        $this->template->assign('SELF_URL', site_url('admin/components/cp/' . $module));
470
471
        echo modules::run($module . '/admin/' . $func, $args);
472
    }
473
474
    public function com_info() {
475
        $com_info = $this->get_module_info($this->input->post('component'));
476
477
        if ($com_info != FALSE) {
478
            $info_text = '<h1>' . $com_info['menu_name'] . '</h1><p>' . $com_info['description'] . '</p><p><b>' . lang('Author', 'admin') . '</b> ' . $com_info['author'] . '<br/><b>' . lang('Version ', 'admin') . '</b> ' . $com_info['version'] . '</p>';
479
480
            jsCode("alertBox.info('" . $info_text . "');");
481
        } else {
482
            showMessage(lang("Can't load module info file", 'admin'), false . 'r');
483
        }
484
    }
485
486
    /**
487
     * @param $mod_name
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
488
     * @return bool|string
489
     */
490
    public function get_module_info($mod_name) {
491
        $info_file = getModulePath($mod_name) . 'module_info.php';
492
        $lang = new MY_Lang();
493
        $lang->load($mod_name);
494
        if (file_exists($info_file)) {
495
            include $info_file;
496
            return $com_info;
0 ignored issues
show
Bug introduced by
The variable $com_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
497
        } else {
498
            return FALSE;
499
        }
500
    }
501
502
    public function change_autoload() {
503
        if ($this->input->post('mid')) {
504
            $mid = $this->input->post('mid');
505
            $row = $this->db->where('id', $mid)->get('components')->row();
506
            if (count($row) > 0) {
507
                $autoload = $row->autoload;
508
                if ($autoload) {
509
                    $autoload = 0;
510
                    $status = lang('Disable', 'admin');
511
                } else {
512
                    $autoload = 1;
513
                    $status = lang('Enable', 'admin');
514
                }
515
                $this->db->where('id', $mid)->set('autoload', $autoload)->update('components');
516
                $row->autoload = $autoload;
517
518
                $nameModule = $this->get_module_info($row->identif)['menu_name'];
519
520
                $message = lang('Change Autoload. Module : ', 'admin') . ' '
521
                    . $nameModule . '. ' . lang('Status', 'admin') . ' : ' . $status . '.';
522
                $this->lib_admin->log($message);
523
                echo json_encode(['result' => $row]);
524
            } else {
525
                $result = false;
526
                echo json_encode(['result' => $result]);
527
            }
528
        }
529
    }
530
531
    public function change_url_access() {
532
        if ($this->input->post('mid')) {
533
            $mid = $this->input->post('mid');
534
            $row = $this->db->where('id', $mid)->get('components')->row();
535
536 View Code Duplication
            if (count($row) > 0) {
537
                $enabled = $row->enabled;
538
                if ($enabled) {
539
                    $enabled = 0;
540
                    $status = lang('Disable', 'admin');
541
                } else {
542
                    $enabled = 1;
543
                    $status = lang('Enable', 'admin');
544
                }
545
546
                $this->db->where('id', $mid)->set('enabled', $enabled)->update('components');
547
548
                $row->enabled = $enabled;
549
                $nameModule = $this->get_module_info($row->identif)['menu_name'];
550
551
                $message = lang('Change URL access. Module : ', 'admin') . ' '
552
                    . $nameModule . '. ' . lang('Status', 'admin') . ' : ' . $status . '.';
553
                $this->lib_admin->log($message);
554
            }
555
        }
556
    }
557
558
    public function change_show_in_menu() {
559
        $id = $this->input->post('id');
560
        $row = $this->db->where('id', (int) $id)->get('components')->row();
561 View Code Duplication
        if (count($row) > 0) {
562
            $in_menu = $row->in_menu;
563
            if ($in_menu == 1) {
564
                $in_menu = 0;
565
                $status = lang('Disable', 'admin');
566
            } else {
567
                $in_menu = 1;
568
                $status = lang('Enable', 'admin');
569
            }
570
            $this->db->where('id', (int) $id)->set('in_menu', $in_menu)->update('components');
571
572
            $nameModule = $this->get_module_info($row->identif)['menu_name'];
573
574
            $message = lang('Change Show in menu. Module : ', 'admin') . ' '
575
                . $nameModule . '. ' . lang('Status', 'admin') . ' : ' . $status . '.';
576
            $this->lib_admin->log($message);
577
        }
578
    }
579
580
    public function save_components_positions() {
581
        $positions = $this->input->post('positions');
582
        if (is_array($positions)) {
583
            foreach ($positions as $key => $value) {
584
                if ($this->db->where('name', $value)->set('position', $key)->update('components')) {
585
                    $result = true;
586
                } else {
587
                    $result = false;
588
                }
589
            }
590
            if ($result) {
591
                showMessage(lang('Positions updated', 'admin'));
592
            } else {
593
                showMessage(lang('Fail', 'admin'));
594
            }
595
        }
596
    }
597
598
}
599
600
/* End of components.php */