Passed
Pull Request — master (#4)
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
include 'admin_header.php';
18
xoops_cp_header();
19
if (TDMDownloads_checkModuleAdmin()) {
20
    $import_admin = new ModuleAdmin();
21
    echo $import_admin->addNavigation('import.php');
22
}
23
//Action dans switch
24
if (isset($_REQUEST['op'])) {
25
    $op = $_REQUEST['op'];
26
} else {
27
    $op = 'index';
28
}
29
30
// import depuis mydownloads
31
function Import_mydownloads($path='', $imgurl='')
32
{
33
    $ok =  isset($_POST['ok']) ? intval($_POST['ok']) : 0;
34
    global $xoopsDB;
35
    if ($ok == 1) {
36
        //Vider les tables
37
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_broken"));
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
38
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_cat"));
39
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_downloads"));
40
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_fielddata"));
41
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_modfielddata"));
42
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_votedata"));
43
        //Inserer les donn�es des cat�gories
44
        $query_topic = $xoopsDB->query("SELECT cid, pid, title, imgurl FROM ".$xoopsDB->prefix("mydownloads_cat"));
45
        while ($donnees = $xoopsDB->fetchArray($query_topic)) {
46
            if ($donnees['imgurl'] == "") {
47
                $img = "blank.gif";
48
            } else {
49
                $img = substr_replace($donnees['imgurl'],'',0,strlen($imgurl));
50
                @copy($path . $img, XOOPS_ROOT_PATH . "/uploads/TDMDownloads/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

50
                /** @scrutinizer ignore-unhandled */ @copy($path . $img, XOOPS_ROOT_PATH . "/uploads/TDMDownloads/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...
51
            }
52
53
            $title = $donnees['title'];
54
            $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')");
55
            if (!$insert) {
56
                echo "<font color='red'>" . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA .": </font> " . $donnees['title'] . "<br>";
57
            }
58
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_CAT_IMP . '<br/>', $donnees['title']);
59
        }
60
        echo '<br/>';
61
62
        //Inserer les donnees des t�l�chargemnts
63
        $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"));
64
        while ($donnees = $xoopsDB->fetchArray($query_links)) {
65
            //On recupere la description
66
            $requete = $xoopsDB->queryF("SELECT description FROM ".$xoopsDB->prefix("mydownloads_text")." WHERE lid = '".$donnees['lid']."'");
67
            list ($description) = $xoopsDB -> fetchRow($requete);
68
            $insert = $xoopsDB->queryF("INSERT INTO ".$xoopsDB->prefix("tdmdownloads_downloads")." (
69
            lid, cid, title, url, homepage, version, size, platform, description, logourl, submitter, status, date, hits, rating, votes, comments, top) VALUES
70
            ('".$donnees['lid']."', '".$donnees['cid']."', '".$donnees['title']."', '".$donnees['url']."', '".$donnees['homepage']."', '".$donnees['version']."', '".$donnees['size']."', '".$donnees['platform']."', '".$description."',  '".$donnees['logourl']."', '".$donnees['submitter']."', '".$donnees['status']."', '".$donnees['date']."', '".$donnees['hits']."', '".$donnees['rating']."', '".$donnees['votes']."', '0', '0' )");
71
            if (!$insert) {
72
                echo "<font color='red'>" . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA .": </font> " . $donnees['title'] . "<br>";
73
            }
74
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_DOWNLOADS_IMP . '<br/>', $donnees['title']);
75
            @copy($path . $donnees['logourl'], XOOPS_ROOT_PATH . "/uploads/TDMDownloads/images/shots/" . $donnees['logourl']);
76
        }
77
        echo '<br/>';
78
        //Inserer les donnees des votes
79
        $query_vote = $xoopsDB->query("SELECT ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp FROM ".$xoopsDB->prefix("mydownloads_votedata"));
80
        while ($donnees = $xoopsDB->fetchArray($query_vote)) {
81
            $insert = $xoopsDB->queryF("INSERT INTO ".$xoopsDB->prefix("tdmdownloads_votedata")." (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp ) VALUES ('".$donnees['ratingid']."', '".$donnees['lid']."', '".$donnees['ratinguser']."', '".$donnees['rating']."', '".$donnees['ratinghostname']."', '". $donnees['ratingtimestamp']."')");
82
            if (!$insert) {
83
                echo "<font color='red'>" . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA .": </font> " . $donnees['ratingid'] . "<br>";
84
            }
85
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_VOTE_IMP . '<br/>', $donnees['ratingid']);
86
        }
87
        echo "<br><br>";
88
        echo "<div class='errorMsg'>";
89
        echo _AM_TDMDOWNLOADS_IMPORT_OK;
90
        echo "</div>";
91
    } else {
92
        xoops_confirm(array('op' => 'import_mydownloads', 'ok' => 1, 'path' => $path, 'imgurl' => $imgurl), 'import.php', _AM_TDMDOWNLOADS_IMPORT_CONF_MYDOWNLOADS . '<br>');
93
    }
94
}
95
96
// import depuis WF-Downloads
97
function Import_wfdownloads($shots='', $catimg='')
98
{
99
    $ok =  isset($_POST['ok']) ? intval($_POST['ok']) : 0;
100
    global $xoopsDB;
101
    if ($ok == 1) {
102
        //Vider les tables
103
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_broken"));
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
104
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_cat"));
105
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_downloads"));
106
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_fielddata"));
107
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_modfielddata"));
108
        $query = $xoopsDB->queryF("truncate table ".$xoopsDB->prefix("tdmdownloads_votedata"));
109
        //Inserer les donn�es des cat�gories
110
        $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"));
111
        while ($donnees = $xoopsDB->fetchArray($query_topic)) {
112
            if ($donnees['imgurl'] == "") {
113
                $img = "blank.gif";
114
            } else {
115
                $img = $donnees['imgurl'];
116
                @copy($catimg . $img, XOOPS_ROOT_PATH . "/uploads/TDMDownloads/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

116
                /** @scrutinizer ignore-unhandled */ @copy($catimg . $img, XOOPS_ROOT_PATH . "/uploads/TDMDownloads/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...
117
            }
118
            $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']."', '".addcslashes($donnees['title'],"'")."', '".$img."', '".addcslashes($donnees['description'],"'")."', '".$donnees['weight']."')");
119
            if (!$insert) {
120
                echo "<font color='red'>" . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA .": </font> " . $donnees['title'] . "<br>";
121
            }
122
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_CAT_IMP . '<br/>', $donnees['title']);
123
        }
124
        echo '<br/>';
125
126
        //Inserer les donnees des t�l�chargemnts
127
        $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 ".$xoopsDB->prefix("wfdownloads_downloads"));
128
        while ($donnees = $xoopsDB->fetchArray($query_links)) {
129
            if ($donnees['url']=='') {
130
                $newurl = XOOPS_URL . '/uploads/' . $donnees['filename'];
131
            } else {
132
                $newurl = $donnees['url'];
133
            }
134
            $insert = $xoopsDB->queryF("INSERT INTO ".$xoopsDB->prefix("tdmdownloads_downloads")." (
135
            lid, cid, title, url, homepage, version, size, platform, description, logourl, submitter, status, date, hits, rating, votes, comments, top) VALUES
136
            ('".$donnees['lid']."', '".$donnees['cid']."', '".addcslashes($donnees['title'],"'")."', '".$newurl."', '".$donnees['homepage']."', '".$donnees['version']."', '".$donnees['size']."', '".$donnees['platform']."', '".addcslashes($donnees['description'],"'")."',  '".$donnees['screenshot']."', '".$donnees['submitter']."', '".$donnees['status']."', '".$donnees['date']."', '".$donnees['hits']."', '".$donnees['rating']."', '".$donnees['votes']."', '0', '0' )");
137
138
            if (!$insert) {
139
                echo "<font color='red'>" . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA .": </font> " . $donnees['title'] . "<br>";
140
            }
141
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_DOWNLOADS_IMP . '<br/>', $donnees['title']);
142
            @copy($shots . $donnees['screenshot'], XOOPS_ROOT_PATH . "/uploads/TDMDownloads/images/shots/" . $donnees['screenshot']);
143
        }
144
        echo '<br/>';
145
146
        //Inserer les donnees des votes
147
        $query_vote = $xoopsDB->query("SELECT ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp FROM ".$xoopsDB->prefix("wfdownloads_votedata"));
148
        while ($donnees = $xoopsDB->fetchArray($query_vote)) {
149
            $insert = $xoopsDB->queryF("INSERT INTO ".$xoopsDB->prefix("tdmdownloads_votedata")." (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp ) VALUES ('".$donnees['ratingid']."', '".$donnees['lid']."', '".$donnees['ratinguser']."', '".$donnees['rating']."', '".$donnees['ratinghostname']."', '". $donnees['ratingtimestamp']."')");
150
            if (!$insert) {
151
                echo "<font color='red'>" . _AM_TDMDOWNLOADS_IMPORT_ERROR_DATA .": </font> " . $donnees['ratingid'] . "<br>";
152
            }
153
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_VOTE_IMP . '<br/>', $donnees['ratingid']);
154
        }
155
        echo "<br><br>";
156
        echo "<div class='errorMsg'>";
157
        echo _AM_TDMDOWNLOADS_IMPORT_OK;
158
        echo "</div>";
159
    } else {
160
        xoops_confirm(array('op' => 'import_wfdownloads', 'ok' => 1, 'shots' => $shots, 'catimg' => $catimg), 'import.php', _AM_TDMDOWNLOADS_IMPORT_CONF_WFDOWNLOADS . '<br>');
161
    }
162
}
163
164
switch ($op) {
165
    case "index":
166
        default:
167
        echo "<br><br>";
168
        echo "<div class='errorMsg'>";
169
        echo _AM_TDMDOWNLOADS_IMPORT_WARNING;
170
        echo "</div>";
171
        echo "<br><br>";
172
        if (TDMDownloads_checkModuleAdmin()) {
173
            $a_admin = new ModuleAdmin();
174
            $a_admin->addItemButton(_AM_TDMDOWNLOADS_IMPORT_MYDOWNLOADS, 'import.php?op=form_mydownloads', 'add');
175
            $a_admin->addItemButton(_AM_TDMDOWNLOADS_IMPORT_WFDOWNLOADS, 'import.php?op=form_wfdownloads', 'add');
176
            echo $a_admin->renderButton("center");
177
        }
178
    break;
179
180
    // import Mydownloads
181
    case "import_mydownloads":
182
        if ($_REQUEST['path'] == '' || $_REQUEST['imgurl'] == '') {
183
            redirect_header('import.php?op=form_mydownloads', 3, _AM_TDMDOWNLOADS_IMPORT_ERREUR);
184
        } else {
185
            Import_mydownloads($_REQUEST['path'],$_REQUEST['imgurl']);
186
        }
187
    break;
188
189
    case "form_mydownloads":
190
        echo "<br><br>";
191
        echo "<fieldset><legend style='font-weight: bold; color: #900;'>" . _AM_TDMDOWNLOADS_IMPORT_NUMBER . "</legend>";
192
        global $xoopsDB;
193
        $query = $xoopsDB->query("SELECT COUNT(lid) as count FROM ".$xoopsDB->prefix("mydownloads_downloads"));
194
        list( $count_downloads ) = $xoopsDB->fetchRow( $query ) ;
195
        if ($count_downloads < 1) {
196
            echo _AM_TDMDOWNLOADS_IMPORT_DONT_DOWNLOADS . "<br />";
197
        } else {
198
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_NB_DOWNLOADS, $count_downloads);
199
        }
200
        $query = $xoopsDB->query("SELECT COUNT(cid) as count FROM ".$xoopsDB->prefix("mydownloads_cat"));
201
        list( $count_topic ) = $xoopsDB->fetchRow( $query ) ;
202
        if ($count_topic < 1) {
203
            echo ""._AM_TDMDOWNLOADS_IMPORT_DONT_TOPIC."<br>";
204
        } else {
205
            echo sprintf('<br/>' . _AM_TDMDOWNLOADS_IMPORT_NB_CAT, $count_topic);
206
        }
207
        echo "</fieldset>";
208
        echo "<br><br>";
209
        echo "<table width='100%' border='0'>
210
                <form action='import.php?op=import_mydownloads' method=POST>
211
                <tr>
212
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_MYDOWNLOADS_PATH . "</td>
213
                    <td  class='odd'><input type='text' name='path' id='import_data' size='100' value='" . XOOPS_ROOT_PATH . "/modules/mydownloads/images/shots/' /></td>
214
                </tr>
215
                <tr>
216
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_MYDOWNLOADS_URL . "</td>
217
                    <td  class='odd'><input type='text' name='imgurl' id='import_data' size='100' value='" . XOOPS_URL . "/modules/mydownloads/images/shots/' /></td>
218
                </tr>
219
                <tr>
220
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_DOWNLOADS . "</td>
221
                    <td  class='odd'><input type='submit' name='button' id='import_data' value='" . _AM_TDMDOWNLOADS_IMPORT1 . "'></td>
222
                </tr>
223
                </form>
224
            </table>";
225
    break;
226
227
    // import WF-Downloads
228
    case "import_wfdownloads":
229
        if ($_REQUEST['shots'] == '' || $_REQUEST['catimg'] == '') {
230
            redirect_header('import.php?op=form_wfdownloads', 3, _AM_TDMDOWNLOADS_IMPORT_ERREUR);
231
        } else {
232
            Import_wfdownloads($_REQUEST['shots'],$_REQUEST['catimg']);
233
        }
234
    break;
235
236
    case "form_wfdownloads":
237
        echo "<br><br>";
238
        echo "<fieldset><legend style='font-weight: bold; color: #900;'>" . _AM_TDMDOWNLOADS_IMPORT_NUMBER . "</legend>";
239
        global $xoopsDB;
240
        $query = $xoopsDB->query("SELECT COUNT(lid) as count FROM ".$xoopsDB->prefix("wfdownloads_downloads"));
241
        list( $count_downloads ) = $xoopsDB->fetchRow( $query ) ;
242
        if ($count_downloads < 1) {
243
            echo _AM_TDMDOWNLOADS_IMPORT_DONT_DOWNLOADS . "<br />";
244
        } else {
245
            echo sprintf(_AM_TDMDOWNLOADS_IMPORT_NB_DOWNLOADS, $count_downloads);
246
        }
247
        $query = $xoopsDB->query("SELECT COUNT(cid) as count FROM ".$xoopsDB->prefix("wfdownloads_cat"));
248
        list( $count_topic ) = $xoopsDB->fetchRow( $query ) ;
249
        if ($count_topic < 1) {
250
            echo ""._AM_TDMDOWNLOADS_IMPORT_DONT_TOPIC."<br>";
251
        } else {
252
            echo sprintf('<br/>' . _AM_TDMDOWNLOADS_IMPORT_NB_CAT, $count_topic);
253
        }
254
        echo "</fieldset>";
255
        echo "<br><br>";
256
        echo "<table width='100%' border='0'>
257
                <form action='import.php?op=import_wfdownloads' method=POST>
258
                <tr>
259
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_WFDOWNLOADS_SHOTS . "</td>
260
                    <td  class='odd'><input type='text' name='shots' id='import_data' size='100' value='" . XOOPS_ROOT_PATH . "/modules/wfdownloads/images/screenshots/' /></td>
261
                </tr>
262
                <tr>
263
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_WFDOWNLOADS_CATIMG . "</td>
264
                    <td  class='odd'><input type='text' name='catimg' id='import_data' size='100' value='" . XOOPS_ROOT_PATH . "/modules/wfdownloads/images/category/' /></td>
265
                </tr>
266
                <tr>
267
                    <td  class='even'>" . _AM_TDMDOWNLOADS_IMPORT_DOWNLOADS . "</td>
268
                    <td  class='odd'><input type='submit' name='button' id='import_data' value='" . _AM_TDMDOWNLOADS_IMPORT1 . "'></td>
269
                </tr>
270
                </form>
271
            </table>";
272
    break;
273
}
274
275
xoops_cp_footer();
276