Passed
Push — master ( 5538a2...3260fe )
by Michael
34s queued 15s
created

addUsers()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 43
rs 8.8817
cc 6
nc 16
nop 0
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 You may not change or alter any portion of this comment or credits
6
 of supporting developers from this source code or any supporting source code
7
 which is considered copyrighted (c) material of the original comment or credit authors.
8
 
9
 This program is distributed in the hope that it will be useful,
10
 but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
*/
13
14
/**
15
 * @category        Module
16
 * @package         suico
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use Xmf\Database\TableLoad;
23
use Xmf\Module\Helper;
24
use Xmf\Request;
25
use Xmf\Yaml;
26
use XoopsModules\Suico;
27
use XoopsModules\Suico\Common;
28
use XoopsModules\Suico\Utility;
29
30
require_once dirname(__DIR__, 3) . '/include/cp_header.php';
31
require dirname(__DIR__) . '/preloads/autoloader.php';
32
$op                 = Request::getCmd('op', '');
33
$moduleDirName      = basename(dirname(__DIR__));
34
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
35
$helper             = Suico\Helper::getInstance();
36
// Load language files
37
$helper->loadLanguage('common');
38
switch ($op) {
39
    case 'load':
40
        if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0, 'REQUEST')) {
41
            if (!$GLOBALS['xoopsSecurity']->check()) {
42
                redirect_header('../admin/index.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
43
            }
44
            loadSampleData();
45
        } else {
46
            xoops_cp_header();
47
            xoops_confirm(
48
                [
49
                    'ok' => 1,
50
                    'op' => 'load',
51
                ],
52
                'index.php',
53
                sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'ADD_SAMPLEDATA_OK')),
54
                constant(
55
                    'CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'
56
                ),
57
                true
58
            );
59
            xoops_cp_footer();
60
        }
61
        break;
62
    case 'save':
63
        saveSampleData();
64
        break;
65
}
66
// XMF TableLoad for SAMPLE data
67
function loadSampleData()
68
{
69
    global $xoopsConfig;
70
    $moduleDirName      = basename(dirname(__DIR__));
71
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
72
    $utility            = new Suico\Utility();
73
    $configurator       = new Common\Configurator();
74
    $tables             = Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
75
    $language           = 'english/';
76
    if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
77
        $language = $xoopsConfig['language'] . '/';
78
    }
79
    foreach ($tables as $table) {
80
        $tabledata = Yaml::readWrapped($language . $table . '.yml');
81
        if (is_array($tabledata)) {
82
            TableLoad::truncateTable($table);
83
            TableLoad::loadTableFromArray($table, $tabledata);
84
        }
85
    }
86
    //  ---  COPY test folder files ---------------
87
    if (is_array($configurator->copyTestFolders)
88
        && count(
89
               $configurator->copyTestFolders
90
           ) > 0) {
91
        //        $file =  dirname(__DIR__) . '/testdata/images/';
92
        foreach (
93
            array_keys(
94
                $configurator->copyTestFolders
95
            ) as $i
96
        ) {
97
            $src  = $configurator->copyTestFolders[$i][0];
98
            $dest = $configurator->copyTestFolders[$i][1];
99
            $utility::rcopy($src, $dest);
100
        }
101
    }
102
103
    addUsers();
104
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
105
}
106
107
function saveSampleData()
108
{
109
    global $xoopsConfig;
110
    $moduleDirName      = basename(dirname(__DIR__));
111
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
112
    $tables             = Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
113
    $language           = 'english/';
114
    if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
115
        $language = $xoopsConfig['language'] . '/';
116
    }
117
    $languageFolder = __DIR__ . '/' . $language;
118
    if (!file_exists($languageFolder . '/')) {
119
        Utility::createFolder($languageFolder . '/');
120
    }
121
    $exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/';
122
    Utility::createFolder($exportFolder);
123
    foreach ($tables as $table) {
124
        TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml');
125
    }
126
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
127
}
128
129
function exportSchema()
130
{
131
    $moduleDirName      = basename(dirname(__DIR__));
132
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
133
    try {
134
        // TODO set exportSchema
135
        //        $migrate = new Suico\Migrate($moduleDirName);
136
        //        $migrate->saveCurrentSchema();
137
        //
138
        //        redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS'));
139
    } catch (Throwable $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Throwable $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
140
        exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR'));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
141
    }
142
}
143
144
function addUsers()
145
{
146
    $ret = false;
147
    $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
148
149
    $sql = ' INSERT INTO `'
150
           . $xoopsDB->prefix('users')
151
           . '` (`uid`, `name`, `uname`, `email`, `url`, `user_avatar`, `user_regdate`, `user_icq`, `user_from`, `user_sig`, `user_viewemail`, `actkey`, `user_aim`, `user_yim`, `user_msnm`, `pass`, `posts`, `attachsig`, `rank`, `level`, `theme`, `timezone_offset`, `last_login`, `umode`, `uorder`, `notify_method`, `notify_mode`, `user_occ`, `bio`, `user_intrest`, `user_mailok`) VALUES ';
152
153
    $userInfo = [
154
        998  => "998, 'Joe Webmaster', 'webmaster', '[email protected]', 'www.xoops.org', 'avatars/avatar2.jpg', 1587372647, '', '', 'Go XOOPS! ', 0, '', '', '', '', '$2y$10$4NtwDxHimN4uxUya93Egu.VJYkYCKzgX3EtomGyYrf1bkY6rB.DTm', 0, 0, 7, 1, 'xswatch4', -5.0, 1588844591, 'flat', 0, 1, 0, '', '', '', 0",
155
        999  => "999, 'ALL Visitors', 'tester', '0fe6bf283000a9f9376ee72c69322b04', '', 'avatars/avatar4.jpg', 1585429986, '', '', 'User under suspension until 2020/5/3 23:16', 0, '', '', '', '', '$2y$10$tOYz4y.S.g4JYtmDnKfEKOpYh3Vivs8.UNZmdX.DVIBd9G5FZVaUi', 0, 0, 0, 1, '', 0.0, 1588838831, 'flat', 0, 1, 0, '', '', '', 1",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tOYz4y seems to be never defined.
Loading history...
156
        1000 => "1000, 'Members Only', 'tester2', '[email protected]', '', 'avatars/avatar5.jpg', 1588229043, '', '', '', 0, '', '', '', '', '$2y$10$uYjy3xn0WyoDdUK5iXml/.lRhw/51SKeATx/FTEjZmL.2ibo.cF0q', 0, 0, 0, 1, '', 0.0, 1588844747, 'flat', 0, 1, 0, '', '', '', 1",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uYjy3xn0WyoDdUK5iXml seems to be never defined.
Loading history...
157
        1001 => "1001, 'Friends Only', 'tester3', '[email protected]', '', 'avatars/avatar6.jpg', 1588232828, '', '', '', 0, '90853319', '', '', '', '$2y$10$FPO.waxqjq.xXXJggTcSMuXpeXQU5UmuOCpX6Lo33d3o78bCh5w4a', 0, 0, 0, 1, 'xswatch4', 0.0, 1588845446, 'flat', 0, 1, 0, '', '', '', 1",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $FPO seems to be never defined.
Loading history...
158
        1002 => "1002, 'Only Me', 'tester4', '[email protected]', '', 'avatars/avatar7.jpg', 1588800450, '', '', '', 0, '', '', '', '', '$2y$10$QF2FadecKKaSBFOsB9U5ne2o1FtPhbVWTurv3.zjwSpDxPFu9qqhm', 0, 0, 0, 1, '', 0.0, 1588845425, 'flat', 0, 1, 0, '', '', '', 1",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $QF2FadecKKaSBFOsB9U5ne2o1FtPhbVWTurv3 seems to be never defined.
Loading history...
159
    ];
160
    // this is where the magic happens
161
    $it = new ArrayIterator($userInfo);
162
    // a new caching iterator gives us access to hasNext()
163
    $cit = new CachingIterator($it);
164
    // loop over the array
165
    foreach ($cit as $key => $value) {
166
        $criteria    = new \Criteria('uid', $key);
167
        $userHandler = xoops_getHandler('user');
168
        if (0 == $userHandler->getCount($criteria)) {
0 ignored issues
show
Bug introduced by
The method getCount() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsGroupHandler or XoopsConfigCategoryHandler or XoopsRankHandler or XoopsConfigOptionHandler or XoopsBlockHandler or XoopsImagesetHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        if (0 == $userHandler->/** @scrutinizer ignore-call */ getCount($criteria)) {
Loading history...
169
            // add to the query
170
            $sql .= "(" . $cit->current() . ")";
171
            // if there is another array member, add a comma
172
            if ($cit->hasNext()) {
173
                $sql .= ",";
174
            }
175
        }
176
    }
177
178
    if (',' === mb_substr($sql, -1)){
179
        $sql = mb_substr($sql, 0, -1);
180
    }
181
182
    $result = $xoopsDB->queryF($sql);
183
    if (false !== $result) {
184
     $ret = true;
185
    }
186
    return $ret;
187
188
}
189