Passed
Pull Request — master (#5)
by Michael
02:50
created

Import_wfdownloads()   C

Complexity

Conditions 11
Paths 152

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 64
rs 6.292
cc 11
nc 152

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * TDMDownload
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
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   Gregory Mage (Aka Mage)
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Gregory Mage (Aka Mage)
15
 */
16
17
require __DIR__ . '/admin_header.php';
18
xoops_cp_header();
19
$import_admin = \Xmf\Module\Admin::getInstance();
20
echo $import_admin->displayNavigation(basename(__FILE__));
21
22
//Action dans switch
23
$op = 'index';
24
if (\Xmf\Request::hasVar('op', 'REQUEST')) {
25
    $op = $_REQUEST['op'];
26
}
27
28
// import depuis mydownloads
29
/**
30
 * @param string $path
31
 * @param string $imgurl
32
 */
33
function importMydownloads($path = '', $imgurl = '')
34
{
35
    $ok = \Xmf\Request::getInt('ok', 0, 'POST');
36
    global $xoopsDB;
37
    if (1 === $ok) {
38
        //Vider les tables
39
        $myTables = ['tdmdownloads_broken', 'tdmdownloads_cat', 'tdmdownloads_downloads', 'tdmdownloads_fielddata', 'tdmdownloads_modfielddata', 'tdmdownloads_votedata'];
40
        $table    = new \Xmf\Database\TableLoad();
41
        $tables   = new \Xmf\Database\Tables();
42
        foreach ($myTables as $myTable) {
43
            if ($tables->useTable($myTable)) { // if this returns false, there is no table
44
                $table::truncateTable($myTable);
45
            }
46
        }
47
48
        //Inserer les données des catégories
49
        $query_topic = $xoopsDB->query('SELECT cid, pid, title, imgurl FROM ' . $xoopsDB->prefix('mydownloads_cat'));
50
        while (false !== ($donnees = $xoopsDB->fetchArray($query_topic))) {
51
            if ('' === $donnees['imgurl']) {
52
                $img = 'blank.gif';
53
            } else {
54
                $img = substr_replace($donnees['imgurl'], '', 0, mb_strlen($imgurl));
55
                @copy($path . $img, XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/cats/' . $img);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for copy(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

55
                /** @scrutinizer ignore-unhandled */ @copy($path . $img, XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/cats/' . $img);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Comprehensibility Best Practice introduced by
The variable $moduleDirName seems to be never defined.
Loading history...
56
            }
57
58
            $title  = $donnees['title'];
59
            $insert = $xoopsDB->queryF('INSERT INTO ' . $xoopsDB->prefix('tdmdownloads_cat') . " (cat_cid, cat_pid, cat_title, cat_imgurl, cat_description_main, cat_weight ) VALUES ('" . $donnees['cid'] . "', '" . $donnees['pid'] . "', '" . $title . "', '" . $img . "', '', '0')");
60
            if (!$insert) {
61
                echo '<span style="color: #ff0000; ">' . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA . ': </span> ' . $donnees['title'] . '<br>';
62
            }
63
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_CAT_IMP . '<br>', $donnees['title']);
64
        }
65
        echo '<br>';
66
67
        //Inserer les données des téléchargemnts
68
        $query_links = $xoopsDB->query('SELECT lid, cid, title, url, homepage, version, size, platform, logourl, submitter, status, date, hits, rating, votes, comments FROM ' . $xoopsDB->prefix('mydownloads_downloads'));
69
        while (false !== ($donnees = $xoopsDB->fetchArray($query_links))) {
70
            //On recupere la description
71
            $requete = $xoopsDB->queryF('SELECT description FROM ' . $xoopsDB->prefix('mydownloads_text') . " WHERE lid = '" . $donnees['lid'] . "'");
72
            list($description) = $xoopsDB->fetchRow($requete);
73
            $insert = $xoopsDB->queryF('INSERT INTO '
74
                                       . $xoopsDB->prefix('tdmdownloads_downloads')
75
                                       . " (
76
            lid, cid, title, url, homepage, version, size, platform, description, logourl, submitter, status, date, hits, rating, votes, comments, top) VALUES
77
            ('"
78
                                       . $donnees['lid']
79
                                       . "', '"
80
                                       . $donnees['cid']
81
                                       . "', '"
82
                                       . $donnees['title']
83
                                       . "', '"
84
                                       . $donnees['url']
85
                                       . "', '"
86
                                       . $donnees['homepage']
87
                                       . "', '"
88
                                       . $donnees['version']
89
                                       . "', '"
90
                                       . $donnees['size']
91
                                       . "', '"
92
                                       . $donnees['platform']
93
                                       . "', '"
94
                                       . $description
95
                                       . "',  '"
96
                                       . $donnees['logourl']
97
                                       . "', '"
98
                                       . $donnees['submitter']
99
                                       . "', '"
100
                                       . $donnees['status']
101
                                       . "', '"
102
                                       . $donnees['date']
103
                                       . "', '"
104
                                       . $donnees['hits']
105
                                       . "', '"
106
                                       . $donnees['rating']
107
                                       . "', '"
108
                                       . $donnees['votes']
109
                                       . "', '0', '0' )");
110
            if (!$insert) {
111
                echo '<span style="color: #ff0000; ">' . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA . ': </span> ' . $donnees['title'] . '<br>';
112
            }
113
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_DOWNLOADS_IMP . '<br>', $donnees['title']);
114
            @copy($path . $donnees['logourl'], XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/shots/' . $donnees['logourl']);
115
        }
116
        echo '<br>';
117
        //Inserer les données des votes
118
        $query_vote = $xoopsDB->query('SELECT ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp FROM ' . $xoopsDB->prefix('mydownloads_votedata'));
119
        while (false !== ($donnees = $xoopsDB->fetchArray($query_vote))) {
120
            $insert = $xoopsDB->queryF('INSERT INTO '
121
                                       . $xoopsDB->prefix('tdmdownloads_votedata')
122
                                       . " (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp ) VALUES ('"
123
                                       . $donnees['ratingid']
124
                                       . "', '"
125
                                       . $donnees['lid']
126
                                       . "', '"
127
                                       . $donnees['ratinguser']
128
                                       . "', '"
129
                                       . $donnees['rating']
130
                                       . "', '"
131
                                       . $donnees['ratinghostname']
132
                                       . "', '"
133
                                       . $donnees['ratingtimestamp']
134
                                       . "')");
135
            if (!$insert) {
136
                echo '<span style="color: #ff0000; ">' . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA . ': </span> ' . $donnees['ratingid'] . '<br>';
137
            }
138
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_VOTE_IMP . '<br>', $donnees['ratingid']);
139
        }
140
        echo '<br><br>';
141
        echo "<div class='errorMsg'>";
142
        echo _AM_TDMDOWNLOADS_IMPORT_OK;
143
        echo '</div>';
144
    } else {
145
        xoops_confirm(['op' => 'importMydownloads', 'ok' => 1, 'path' => $path, 'imgurl' => $imgurl], 'import.php', _AM_TDMDOWNLOADS_IMPORT_CONF_MYDOWNLOADS . '<br>');
146
    }
147
}
148
149
// import depuis WF-Downloads
150
/**
151
 * @param string $shots
152
 * @param string $catimg
153
 */
154
function importWfdownloads($shots = '', $catimg = '')
155
{
156
    $ok = \Xmf\Request::getInt('ok', 0, 'POST');
157
    global $xoopsDB;
158
    if (1 === $ok) {
159
        //Vider les tables
160
        $myTables = ['tdmdownloads_broken', 'tdmdownloads_cat', 'tdmdownloads_downloads', 'tdmdownloads_fielddata', 'tdmdownloads_modfielddata', 'tdmdownloads_votedata'];
161
        $table    = new \Xmf\Database\TableLoad();
162
        $tables   = new \Xmf\Database\Tables();
163
        foreach ($myTables as $myTable) {
164
            if ($tables->useTable($myTable)) { // if this returns false, there is no table
165
                $table::truncateTable($myTable);
166
            }
167
        }
168
169
        //Inserer les données des catégories
170
        $query_topic = $xoopsDB->query('SELECT cid, pid, title, imgurl, description, total, summary, spotlighttop, spotlighthis, dohtml, dosmiley, doxcode, doimage, dobr, weight, formulize_fid FROM ' . $xoopsDB->prefix('wfdownloads_cat'));
171
        while (false !== ($donnees = $xoopsDB->fetchArray($query_topic))) {
172
            if ('' === $donnees['imgurl']) {
173
                $img = 'blank.gif';
174
            } else {
175
                $img = $donnees['imgurl'];
176
                @copy($catimg . $img, XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/cats/' . $img);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $moduleDirName seems to be never defined.
Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for copy(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

176
                /** @scrutinizer ignore-unhandled */ @copy($catimg . $img, XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/cats/' . $img);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
177
            }
178
            $insert = $xoopsDB->queryF('INSERT INTO '
179
                                       . $xoopsDB->prefix('tdmdownloads_cat')
180
                                       . " (cat_cid, cat_pid, cat_title, cat_imgurl, cat_description_main, cat_weight ) VALUES ('"
181
                                       . $donnees['cid']
182
                                       . "', '"
183
                                       . $donnees['pid']
184
                                       . "', '"
185
                                       . addcslashes($donnees['title'], "'")
186
                                       . "', '"
187
                                       . $img
188
                                       . "', '"
189
                                       . addcslashes($donnees['description'], "'")
190
                                       . "', '"
191
                                       . $donnees['weight']
192
                                       . "')");
193
            if (!$insert) {
194
                echo '<span style="color: #ff0000; ">' . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA . ': </span> ' . $donnees['title'] . '<br>';
195
            }
196
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_CAT_IMP . '<br>', $donnees['title']);
197
        }
198
        echo '<br>';
199
200
        //Inserer les données des téléchargemnts
201
        $query_links = $xoopsDB->query('SELECT lid, cid, title, url, filename, filetype, homepage, version, size, platform, screenshot, screenshot2, screenshot3, screenshot4, submitter, publisher, status, date, hits, rating, votes, comments, license, mirror, price, paypalemail, features, requirements, homepagetitle, forumid, limitations, versiontypes, dhistory, published, expired, updated, offline, summary, description, ipaddress, notifypub, formulize_idreq  FROM '
202
                                       . $xoopsDB->prefix('wfdownloads_downloads'));
203
        while (false !== ($donnees = $xoopsDB->fetchArray($query_links))) {
204
            if ('' === $donnees['url']) {
205
                $newurl = XOOPS_URL . '/uploads/' . $donnees['filename'];
206
            } else {
207
                $newurl = $donnees['url'];
208
            }
209
            $insert = $xoopsDB->queryF('INSERT INTO '
210
                                       . $xoopsDB->prefix('tdmdownloads_downloads')
211
                                       . " (
212
            lid, cid, title, url, homepage, version, size, platform, description, logourl, submitter, status, date, hits, rating, votes, comments, top) VALUES
213
            ('"
214
                                       . $donnees['lid']
215
                                       . "', '"
216
                                       . $donnees['cid']
217
                                       . "', '"
218
                                       . addcslashes($donnees['title'], "'")
219
                                       . "', '"
220
                                       . $newurl
221
                                       . "', '"
222
                                       . $donnees['homepage']
223
                                       . "', '"
224
                                       . $donnees['version']
225
                                       . "', '"
226
                                       . $donnees['size']
227
                                       . "', '"
228
                                       . $donnees['platform']
229
                                       . "', '"
230
                                       . addcslashes($donnees['description'], "'")
231
                                       . "',  '"
232
                                       . $donnees['screenshot']
233
                                       . "', '"
234
                                       . $donnees['submitter']
235
                                       . "', '"
236
                                       . $donnees['status']
237
                                       . "', '"
238
                                       . $donnees['date']
239
                                       . "', '"
240
                                       . $donnees['hits']
241
                                       . "', '"
242
                                       . $donnees['rating']
243
                                       . "', '"
244
                                       . $donnees['votes']
245
                                       . "', '0', '0' )");
246
247
            if (!$insert) {
248
                echo '<span style="color: #ff0000; ">' . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA . ': </span> ' . $donnees['title'] . '<br>';
249
            }
250
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_DOWNLOADS_IMP . '<br>', $donnees['title']);
251
            @copy($shots . $donnees['screenshot'], XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/shots/' . $donnees['screenshot']);
252
        }
253
        echo '<br>';
254
255
        //Inserer les données des votes
256
        $query_vote = $xoopsDB->query('SELECT ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp FROM ' . $xoopsDB->prefix('wfdownloads_votedata'));
257
        while (false !== ($donnees = $xoopsDB->fetchArray($query_vote))) {
258
            $insert = $xoopsDB->queryF('INSERT INTO '
259
                                       . $xoopsDB->prefix('tdmdownloads_votedata')
260
                                       . " (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp ) VALUES ('"
261
                                       . $donnees['ratingid']
262
                                       . "', '"
263
                                       . $donnees['lid']
264
                                       . "', '"
265
                                       . $donnees['ratinguser']
266
                                       . "', '"
267
                                       . $donnees['rating']
268
                                       . "', '"
269
                                       . $donnees['ratinghostname']
270
                                       . "', '"
271
                                       . $donnees['ratingtimestamp']
272
                                       . "')");
273
            if (!$insert) {
274
                echo '<span style="color: #ff0000; ">' . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA . ': </span> ' . $donnees['ratingid'] . '<br>';
275
            }
276
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_VOTE_IMP . '<br>', $donnees['ratingid']);
277
        }
278
        echo '<br><br>';
279
        echo "<div class='errorMsg'>";
280
        echo _AM_TDMDOWNLOADS_IMPORT_OK;
281
        echo '</div>';
282
    } else {
283
        xoops_confirm(['op' => 'importWfdownloads', 'ok' => 1, 'shots' => $shots, 'catimg' => $catimg], 'import.php', _AM_TDMDOWNLOADS_IMPORT_CONF_WFDOWNLOADS . '<br>');
284
    }
285
}
286
287
switch ($op) {
288
    case 'index':
289
    default:
290
        echo '<br><br>';
291
        echo "<div class='errorMsg'>";
292
        echo _AM_TDMDOWNLOADS_IMPORT_WARNING;
293
        echo '</div>';
294
        echo '<br><br>';
295
        $adminObject = \Xmf\Module\Admin::getInstance();
296
        $adminObject->addItemButton(_AM_TDMDOWNLOADS_IMPORT_MYDOWNLOADS, 'import.php?op=form_mydownloads', 'add');
297
        $adminObject->addItemButton(_AM_TDMDOWNLOADS_IMPORT_WFDOWNLOADS, 'import.php?op=form_wfdownloads', 'add');
298
        $adminObject->displayButton('center');
299
        break;
300
    // import Mydownloads
301
    case 'importMydownloads':
302
        if ('' == $_REQUEST['path'] || '' == $_REQUEST['imgurl']) {
303
            redirect_header('import.php?op=form_mydownloads', 3, _AM_TDMDOWNLOADS_IMPORT_ERREUR);
304
        } else {
305
            importMydownloads($_REQUEST['path'], $_REQUEST['imgurl']);
306
        }
307
        break;
308
    case 'form_mydownloads':
309
        echo '<br><br>';
310
        echo "<fieldset><legend style='font-weight: bold; color: #900;'>" . _AM_TDMDOWNLOADS_IMPORT_NUMBER . '</legend>';
311
        global $xoopsDB;
312
        $sql = $xoopsDB->query('SELECT COUNT(lid) AS count FROM ' . $xoopsDB->prefix('mydownloads_downloads'));
313
        list($count_downloads) = $xoopsDB->fetchRow($sql);
314
        if ($count_downloads < 1) {
315
            echo _AM_TDMDOWNLOADS_IMPORT_DONT_DOWNLOADS . '<br>';
316
        } else {
317
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_NB_DOWNLOADS, $count_downloads);
318
        }
319
        $sql = $xoopsDB->query('SELECT COUNT(cid) AS count FROM ' . $xoopsDB->prefix('mydownloads_cat'));
320
        list($count_topic) = $xoopsDB->fetchRow($sql);
321
        if ($count_topic < 1) {
322
            echo '' . _AM_TDMDOWNLOADS_IMPORT_DONT_TOPIC . '<br>';
323
        } else {
324
            echo sprintf('<br>' . _AM_TDMDOWNLOADS_IMPORT_NB_CAT, $count_topic);
325
        }
326
        echo '</fieldset>';
327
        echo '<br><br>';
328
        echo "<table width='100%' border='0'>
329
                <form action='import.php?op=importMydownloads' method=POST>
330
                <tr>
331
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_MYDOWNLOADS_PATH . "</td>
332
                    <td  class='odd'><input type='text' name='path' id='import_data' size='100' value='" . XOOPS_ROOT_PATH . "/modules/mydownloads/assets/images/shots/'></td>
333
                </tr>
334
                <tr>
335
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_MYDOWNLOADS_URL . "</td>
336
                    <td  class='odd'><input type='text' name='imgurl' id='import_data' size='100' value='" . XOOPS_URL . "/modules/mydownloads/assets/images/shots/'></td>
337
                </tr>
338
                <tr>
339
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_DOWNLOADS . "</td>
340
                    <td  class='odd'><input type='submit' name='button' id='import_data' value='" . _AM_TDMDOWNLOADS_IMPORT1 . "'></td>
341
                </tr>
342
                </form>
343
            </table>";
344
        break;
345
    // import WF-Downloads
346
    case 'importWfdownloads':
347
        if ('' === $_REQUEST['shots'] || '' === $_REQUEST['catimg']) {
348
            redirect_header('import.php?op=form_wfdownloads', 3, _AM_TDMDOWNLOADS_IMPORT_ERREUR);
349
        } else {
350
            importWfdownloads($_REQUEST['shots'], $_REQUEST['catimg']);
351
        }
352
        break;
353
    case 'form_wfdownloads':
354
        echo '<br><br>';
355
        echo "<fieldset><legend style='font-weight: bold; color: #900;'>" . _AM_TDMDOWNLOADS_IMPORT_NUMBER . '</legend>';
356
        global $xoopsDB;
357
        $sql = $xoopsDB->query('SELECT COUNT(lid) AS count FROM ' . $xoopsDB->prefix('wfdownloads_downloads'));
358
        list($count_downloads) = $xoopsDB->fetchRow($sql);
359
        if ($count_downloads < 1) {
360
            echo _AM_TDMDOWNLOADS_IMPORT_DONT_DOWNLOADS . '<br>';
361
        } else {
362
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_NB_DOWNLOADS, $count_downloads);
363
        }
364
        $sql = $xoopsDB->query('SELECT COUNT(cid) AS count FROM ' . $xoopsDB->prefix('wfdownloads_cat'));
365
        list($count_topic) = $xoopsDB->fetchRow($sql);
366
        if ($count_topic < 1) {
367
            echo '' . _AM_TDMDOWNLOADS_IMPORT_DONT_TOPIC . '<br>';
368
        } else {
369
            echo sprintf('<br>' . _AM_TDMDOWNLOADS_IMPORT_NB_CAT, $count_topic);
370
        }
371
        echo '</fieldset>';
372
        echo '<br><br>';
373
        echo "<table width='100%' border='0'>
374
                <form action='import.php?op=importWfdownloads' method=POST>
375
                <tr>
376
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_WFDOWNLOADS_SHOTS . "</td>
377
                    <td  class='odd'><input type='text' name='shots' id='import_data' size='100' value='" . XOOPS_ROOT_PATH . "/modules/wfdownloads/assets/images/screenshots/'></td>
378
                </tr>
379
                <tr>
380
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_WFDOWNLOADS_CATIMG . "</td>
381
                    <td  class='odd'><input type='text' name='catimg' id='import_data' size='100' value='" . XOOPS_ROOT_PATH . "/modules/wfdownloads/assets/images/category/'></td>
382
                </tr>
383
                <tr>
384
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_DOWNLOADS . "</td>
385
                    <td  class='odd'><input type='submit' name='button' id='import_data' value='" . _AM_TDMDOWNLOADS_IMPORT1 . "'></td>
386
                </tr>
387
                </form>
388
            </table>";
389
        break;
390
}
391
392
require __DIR__ . '/admin_footer.php';
393