Passed
Push — master ( 37a2f2...290aa0 )
by Michael
02:35
created

flagUpload()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 8
nop 1
dl 0
loc 37
rs 8.0555
c 0
b 0
f 0
1
<?php
2
//
3
//  ------------------------------------------------------------------------ //
4
//             XF Guestbook                                                  //
5
// ------------------------------------------------------------------------- //
6
//  This program is free software; you can redistribute it and/or modify     //
7
//  it under the terms of the GNU General Public License as published by     //
8
//  the Free Software Foundation; either version 2 of the License, or        //
9
//  (at your option) any later version.                                      //
10
//                                                                           //
11
//  You may not change or alter any portion of this comment or credits       //
12
//  of supporting developers from this source code or any supporting         //
13
//  source code which is considered copyrighted (c) material of the          //
14
//  original comment or credit authors.                                      //
15
//                                                                           //
16
//  This program is distributed in the hope that it will be useful,          //
17
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
18
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
19
//  GNU General Public License for more details.                             //
20
//                                                                           //
21
//  You should have received a copy of the GNU General Public License        //
22
//  along with this program; if not, write to the Free Software              //
23
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
24
//  ------------------------------------------------------------------------ //
25
26
use XoopsModules\Xfguestbook;
27
28
//require_once dirname(dirname(dirname(__DIR__))) . '/include/cp_header.php';
29
//require_once  dirname(__DIR__) . '/include/cp_functions.php';
30
require_once __DIR__ . '/admin_header.php';
31
32
/** @var Xfguestbook\Helper $helper */
33
$helper = Xfguestbook\Helper::getInstance();
34
35
// Flag
36
$maxsize   = 2000;
37
$maxheight = 50;
38
$maxwidth  = 80;
39
$format    = 'gif';
40
41
if (\Xmf\Request::hasVar('op', 'GET')) {
42
    $op = $_GET['op'];
43
} elseif (\Xmf\Request::hasVar('op', 'POST')) {
44
    $op = $_POST['op'];
45
} else {
46
    $op = 'countryShow';
47
}
48
49
if (\Xmf\Request::hasVar('country_id', 'GET')) {
50
 $country_id = \Xmf\Request::getInt('country_id', 0, 'GET');
51
} else {
52
    $country_id = \Xmf\Request::getInt('country_id', 0, 'POST');
53
}
54
55
if (\Xmf\Request::hasVar('country_code', 'GET')) {
56
    $country_code = $_GET['country_code'];
57
} elseif (\Xmf\Request::hasVar('country_code', 'POST')) {
58
    $country_code = $_POST['country_code'];
59
} else {
60
    $country_code = '';
61
}
62
63
$start        = \Xmf\Request::getInt('start', 0, 'GET');
64
$country_name = \Xmf\Request::getString('country_name', '', 'POST');
65
66
/**
67
 * @param $country_code
68
 */
69
function flagUpload($country_code)
70
{
71
    global $xoopsModule,  $maxsize, $maxwidth, $maxheight, $format;
72
    /** @var Xfguestbook\Helper $helper */
73
    $helper = Xfguestbook\Helper::getInstance();
74
75
    $array_allowed_mimetypes = ['image/' . $format];
76
    // photos
77
    if (!empty($_FILES['photo']['name'])) {
78
        $ext = preg_replace("/^.+\.([^.]+)$/sU", "\\1", $_FILES['photo']['name']);
0 ignored issues
show
Unused Code introduced by
The assignment to $ext is dead and can be removed.
Loading history...
79
        require_once XOOPS_ROOT_PATH . '/class/uploader.php';
80
        $field = $_POST['xoops_upload_file'][0];
81
        if (!empty($field) || '' !== $field) {
82
            // Check if file uploaded
83
            if ('' === $_FILES[$field]['tmp_name'] || !is_readable($_FILES[$field]['tmp_name'])) {
84
                redirect_header('country_manager.php', 2, MD_XFGUESTBOOK_FILEERROR);
85
            }
86
            $photos_dir = XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $helper->getConfig('flagdir');
87
            $uploader   = new \XoopsMediaUploader($photos_dir, $array_allowed_mimetypes, $maxsize, $maxwidth, $maxheight);
88
            $uploader->setPrefix('tmp');
89
            if ($uploader->fetchMedia($field) && $uploader->upload()) {
90
                $tmp_name = $uploader->getSavedFileName();
91
                $ext      = preg_replace("/^.+\.([^.]+)$/sU", "\\1", $tmp_name);
92
                $photo    = $country_code . '.' . $ext;
93
                if (file_exists($photos_dir . '/' . $photo)) {
94
                    unlink($photos_dir . '/' . $photo);
95
                }
96
                rename("$photos_dir/$tmp_name", "$photos_dir/$photo");
97
            } else {
98
                redirect_header('country_manager.php', 2, $uploader->getErrors());
99
            }
100
        }
101
        redirect_header('country_manager.php', 2, AM_XFGUESTBOOK_FILEUPLOADED);
102
    } else {
103
        redirect_header('country_manager.php?op=flagForm&amp;country_code=' . $country_code, 2, MD_XFGUESTBOOK_NOIMGSELECTED);
104
    }
105
    exit();
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...
106
}
107
108
/**
109
 * @param $country_code
110
 */
111
function flagForm($country_code)
112
{
113
    global $xoopsModule,  $maxsize, $maxwidth, $maxheight, $format;
114
    /** @var Xfguestbook\Helper $helper */
115
    $helper = Xfguestbook\Helper::getInstance();
116
117
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
118
119
    $flagform = new \XoopsThemeForm(AM_XFGUESTBOOK_SUBMITFLAG, 'op', xoops_getenv('PHP_SELF'), 'post', true);
120
    $flagform->setExtra("enctype='multipart/form-data'");
121
122
    $flag = '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $helper->getConfig('flagdir') . '/' . $country_code . '.gif';
123
    if (file_exists(XOOPS_ROOT_PATH . $flag)) {
124
        $flag_img = "<img src='" . XOOPS_URL . $flag . '\'>';
125
        $img_flag = new \XoopsFormLabel('', '<br>' . $flag_img . '<br>');
126
        $flagform->addElement($img_flag);
127
    }
128
    $flag_desc = sprintf(AM_XFGUESTBOOK_FLAGDSC, $maxsize, $maxwidth, $maxheight, $format);
129
    $flagform->addElement(new \XoopsFormLabel('', $flag_desc));
130
131
    $img_text = new \XoopsFormFile(AM_XFGUESTBOOK_ADDIMG, 'photo', 30000);
132
    $img_text->setExtra("size ='60'");
133
    $flagform->addElement($img_text);
134
135
    $button_tray = new \XoopsFormElementTray('', '');
136
    $button_tray->addElement(new \XoopsFormButton('', 'post', _SUBMIT, 'submit'));
137
    $button_tray->addElement(new \XoopsFormHidden('country_code', $country_code));
138
    $button_tray->addElement(new \XoopsFormHidden('op', 'flagUpload'));
139
    $flagform->addElement($button_tray);
140
141
    $flagform->display();
142
}
143
144
/**
145
 * @param $country_code
146
 */
147
function flagDel($country_code)
148
{
149
    global $xoopsModule;
150
    /** @var Xfguestbook\Helper $helper */
151
    $helper = Xfguestbook\Helper::getInstance();
152
153
    $ok = \Xmf\Request::getInt('ok', 0, 'POST');
154
    if (1 == $ok) {
155
        $flag = '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $helper->getConfig('flagdir') . '/' . $country_code . '.gif';
156
        if (file_exists(XOOPS_ROOT_PATH . $flag)) {
157
            unlink(XOOPS_ROOT_PATH . $flag);
158
        }
159
        redirect_header('country_manager.php', 2, AM_XFGUESTBOOK_FLAGDELETED);
160
    } else {
161
        xoops_cp_header();
162
        $adminObject = \Xmf\Module\Admin::getInstance();
163
        $adminObject->displayNavigation(basename(__FILE__));
164
        xoops_confirm(['op' => 'flagDel', 'country_code' => $country_code, 'ok' => 1], 'country_manager.php', AM_XFGUESTBOOK_CONFDELFLAG);
165
        require_once __DIR__   . '/admin_footer.php';
166
        //xoops_cp_footer();
167
    }
168
}
169
170
/**
171
 * @param null $country_id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $country_id is correct as it would always require null to be passed?
Loading history...
172
 */
173
function countryForm($country_id = null)
174
{
175
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
176
177
    if ($country_id) {
0 ignored issues
show
introduced by
$country_id is of type null, thus it always evaluated to false.
Loading history...
178
        $sform        = new \XoopsThemeForm(AM_XFGUESTBOOK_MODCOUNTRY, 'op', xoops_getenv('PHP_SELF'), 'post', true);
179
        $arr_country  = Xfguestbook\Utility::getCountry('country_id=' . $country_id, 0, 0);
180
        $country_code = $arr_country[0]['country_code'];
181
        $country_name = $arr_country[0]['country_name'];
182
    } else {
183
        $sform        = new \XoopsThemeForm(AM_XFGUESTBOOK_ADDCOUNTRY, 'op', xoops_getenv('PHP_SELF'), 'post', true);
184
        $country_code = '';
185
        $country_name = '';
186
    }
187
188
    $text_code = new \XoopsFormText(AM_XFGUESTBOOK_FLAGCODE, 'country_code', 5, 5, $country_code);
189
    if ($country_id) {
0 ignored issues
show
introduced by
$country_id is of type null, thus it always evaluated to false.
Loading history...
190
        $text_code->setExtra("readonly = 'readonly'");
191
    }
192
    $sform->addElement($text_code, true);
193
    $sform->addElement(new \XoopsFormText(AM_XFGUESTBOOK_FLAGNAME, 'country_name', 50, 50, $country_name), true);
194
195
    $button_tray = new \XoopsFormElementTray('', '');
196
    $button_tray->addElement(new \XoopsFormButton('', 'save', _SUBMIT, 'submit'));
197
    if ($country_id) {
0 ignored issues
show
introduced by
$country_id is of type null, thus it always evaluated to false.
Loading history...
198
        $button_tray->addElement(new \XoopsFormHidden('country_id', $country_id));
199
    }
200
    $button_tray->addElement(new \XoopsFormHidden('op', 'countrySave'));
201
    $sform->addElement($button_tray);
202
    $sform->display();
203
}
204
205
/**
206
 * @param  null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
207
 * @param  int  $limit
208
 * @param  int  $start
209
 * @return array
210
 */
211
function xfgb_getCountry($criteria = null, $limit = 0, $start = 0)
212
{
213
    global $xoopsDB, $action;
214
    $ret = [];
215
216
    $sql = 'SELECT * FROM ' . $xoopsDB->prefix('xfguestbook_country');
217
    if (null !== $criteria && '' !== $criteria) {
0 ignored issues
show
introduced by
The condition null !== $criteria is always false.
Loading history...
218
        $sql .= ' WHERE ' . $criteria;
219
    }
220
    $sql    .= ' ORDER BY country_name ASC';
221
    $result = $xoopsDB->query($sql, $limit, $start);
222
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
223
        $ret[] = $myrow;
224
    }
225
226
    return $ret;
227
}
228
229
/**
230
 * @param $country_id
231
 */
232
function countryDel($country_id)
233
{
234
    global $xoopsDB, $xoopsModule;
235
    /** @var Xfguestbook\Helper $helper */
236
    $helper = Xfguestbook\Helper::getInstance();
237
238
    $ok = \Xmf\Request::getInt('ok', 0, 'POST');
239
    if (1 == $ok) {
240
        $arr_country = Xfguestbook\Utility::getCountry('country_id=' . $country_id, 0, 0);
241
        $flag        = '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $helper->getConfig('flagdir') . '/' . $arr_country[0]['country_code'] . '.gif';
242
        $sql         = 'DELETE FROM ' . $xoopsDB->prefix('xfguestbook_country') . " WHERE country_id=$country_id";
243
        $result      = $xoopsDB->query($sql);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
244
        if (file_exists(XOOPS_ROOT_PATH . $flag)) {
245
            unlink(XOOPS_ROOT_PATH . $flag);
246
        }
247
        redirect_header('country_manager.php', 1, AM_XFGUESTBOOK_COUNTRYDELETED);
248
    } else {
249
        xoops_cp_header();
250
        $adminObject = \Xmf\Module\Admin::getInstance();
251
        $adminObject->displayNavigation(basename(__FILE__));
252
        xoops_confirm(['op' => 'countryDel', 'country_id' => $country_id, 'ok' => 1], 'country_manager.php', AM_XFGUESTBOOK_CONFDELCOUNTRY);
253
        require_once __DIR__   . '/admin_footer.php';
254
        //xoops_cp_footer();
255
    }
256
}
257
258
/**
259
 * @param $country_id
260
 * @param $country_code
261
 * @param $country_name
262
 */
263
function countrySave($country_id, $country_code, $country_name)
264
{
265
    global $xoopsDB;
266
267
    $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
268
    //$country_code=$myts->makeTboxData4Save::$country_code;
269
    //$country_name=$myts->makeTboxData4Save::$country_name;
270
    echo $country_code;
271
    if (!empty($country_id)) {
272
        $sql = 'UPDATE ' . $xoopsDB->prefix('xfguestbook_country') . " SET country_code='$country_code', country_name='$country_name'";
273
        $sql .= " WHERE country_id=$country_id";
274
        $xoopsDB->query($sql);
275
        $messagesent = AM_XFGUESTBOOK_COUNTRY_UPDATED;
276
    } else {
277
        $sql = sprintf("SELECT COUNT(*) FROM  %s WHERE country_code = '%s'", $xoopsDB->prefix('xfguestbook_country'), $country_code);
278
        list($count) = $xoopsDB->fetchRow($xoopsDB->query($sql));
279
        if ($count > 0) {
280
            $messagesent = '<span style="color: #FF0000; ">' . AM_XFGUESTBOOK_COUNTRY_EXIST . '</span>';
281
        } else {
282
            $country_id = $xoopsDB->genId('country_id_seq');
283
            $sql        = sprintf("INSERT INTO `%s` (country_id, country_code, country_name) VALUES (%s, '%s', '%s')", $xoopsDB->prefix('xfguestbook_country'), $country_id, $country_code, $country_name);
284
            $xoopsDB->query($sql);
285
            $messagesent = AM_XFGUESTBOOK_COUNTRY_ADDED;
286
        }
287
    }
288
    redirect_header('country_manager.php', 2, $messagesent);
289
}
290
291
function countryShow()
292
{
293
    global $action, $start, $xoopsModule,  $pathIcon16;
294
    /** @var Xfguestbook\Helper $helper */
295
    $helper = Xfguestbook\Helper::getInstance();
296
297
    $myts        = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
298
    $limit       = 15;
299
    $arr_country = Xfguestbook\Utility::getCountry('', $limit, $start);
300
    $scount      = count(Xfguestbook\Utility::getCountry('', $limit, 0));
301
    $totalcount  = count(Xfguestbook\Utility::getCountry('', 0, 0));
302
303
    echo "
304
    <table width='100%' cellspacing='1' cellpadding='2' border='0' style='border-left: 1px solid #c0c0c0; border-top: 1px solid #c0c0c0; border-right: 1px solid #c0c0c0;'>
305
        <tr>
306
            <td><span style='font-weight: bold; font-size: 12px; font-variant: small-caps;'>" . AM_XFGUESTBOOK_DISPCOUNTRY . ' : ' . $totalcount . "</span></td>
307
            <td align='right'>
308
            </td>
309
        </tr>
310
    </table>";
311
312
    echo "<table border='1' width='100%' cellpadding ='2' cellspacing='1'>";
313
    echo "<tr class='bg3'>";
314
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_FLAGIMG . '</td>';
315
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_FLAGCODE . '</td>';
316
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_FLAGNAME . '</td>';
317
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_COUNTRY . '</td></b>';
318
    echo "<td align='center'><b>" . AM_XFGUESTBOOK_FLAGIMG . '</td></b>';
319
    echo '</tr>';
320
321
    if ('0' == count($arr_country)) {
322
        echo "<tr ><td align='center' colspan ='10' class = 'head'><b>" . AM_XFGUESTBOOK_NOFLAG . '</b></td></tr>';
323
    }
324
325
    for ($i = 0, $iMax = count($arr_country); $i < $iMax; ++$i) {
326
        $all_country = [];
327
        $flag        = '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $helper->getConfig('flagdir') . '/' . $arr_country[$i]['country_code'] . '.gif';
328
        if (file_exists(XOOPS_ROOT_PATH . $flag)) {
329
            $all_country['flag_img'] = "<img src='" . XOOPS_URL . $flag . '\'>';
330
        } else {
331
            $all_country['flag_img'] = "<img src='" . XOOPS_URL . "/images/blank.gif'>";
332
        }
333
334
        $all_country['country_id']   = $arr_country[$i]['country_id'];
335
        $all_country['country_code'] = $arr_country[$i]['country_code'];
336
        $all_country['country_name'] = $arr_country[$i]['country_name'];
337
        $all_country['msg_action']   = "<a href='country_manager.php?op=countryEdit&amp;country_id=" . $arr_country[$i]['country_id'] . '\'><img src=\'' . $pathIcon16 . "/edit.png'></a>";
338
        $all_country['msg_action']   .= "&nbsp;<a href='country_manager.php?op=countryDel&amp;country_id=" . $arr_country[$i]['country_id'] . '\'><img src=\'' . $pathIcon16 . "/delete.png'></a>";
339
        $all_country['flag_action']  = "<a href='country_manager.php?op=flagForm&amp;country_code=" . $arr_country[$i]['country_code'] . '\'><img src=\'' . $pathIcon16 . "/add.png'></a>";
340
        $all_country['flag_action']  .= "&nbsp;<a href='country_manager.php?op=flagDel&amp;country_code=" . $arr_country[$i]['country_code'] . '\'><img src=\'' . $pathIcon16 . "/delete.png'></a>";
341
        echo "<tr><td align='center' class = 'head'><b>" . $all_country['flag_img'] . '</b>';
342
        echo "</td><td class = 'even'>" . $all_country['country_code'] . '';
343
        echo "</td><td class = 'odd'>" . $all_country['country_name'] . '';
344
        echo "</td><td align='center' class='even'>" . $all_country['msg_action'] . '';
345
        echo "</td><td align='center' class='even'>" . $all_country['flag_action'] . '';
346
        echo '</td></tr>';
347
        unset($all_country);
348
    }
349
350
    echo '</table><br>';
351
352
    if ($totalcount > $scount) {
353
        require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
354
        $pagenav = new \XoopsPageNav($totalcount, $limit, $start, 'start', 'action=' . $action);
355
        echo "<div class='center;' class = 'head'>" . $pagenav->renderNav() . '</div><br>';
356
    } else {
357
        echo '';
358
    }
359
    echo '<br>';
360
}
361
362
switch ($op) {
363
    case 'flagForm':
364
        xoops_cp_header();
365
        $adminObject = \Xmf\Module\Admin::getInstance();
366
        $adminObject->displayNavigation(basename(__FILE__));
367
        //xfguestbook_admin_menu(2);
368
        flagForm($country_code);
369
        require_once __DIR__   . '/admin_footer.php';
370
        //xoops_cp_footer();
371
        break;
372
    case 'flagUpload':
373
        flagUpload($country_code);
374
        break;
375
    case 'flagDel':
376
        flagDel($country_code);
377
        break;
378
    case 'countryDel':
379
        countryDel($country_id);
380
        break;
381
    case 'countryEdit':
382
        xoops_cp_header();
383
        $adminObject = \Xmf\Module\Admin::getInstance();
384
        $adminObject->displayNavigation(basename(__FILE__));
385
        //xfguestbook_admin_menu(2);
386
        countryForm($country_id);
387
        require_once __DIR__   . '/admin_footer.php';
388
        //xoops_cp_footer();
389
        break;
390
    case 'countrySave':
391
        countrySave($country_id, $country_code, $country_name);
392
        break;
393
    case 'countryAdd':
394
        xoops_cp_header();
395
        $adminObject = \Xmf\Module\Admin::getInstance();
396
        $adminObject->displayNavigation(basename(__FILE__));
397
        //xfguestbook_admin_menu(2);
398
        countryForm();
399
        require_once __DIR__   . '/admin_footer.php';
400
        //xoops_cp_footer();
401
        break;
402
    case 'countryShow':
403
    default:
404
        xoops_cp_header();
405
        $adminObject = \Xmf\Module\Admin::getInstance();
406
        $adminObject->displayNavigation(basename(__FILE__));
407
        //xfguestbook_admin_menu(2);
408
        countryShow();
409
        countryForm();
410
        require_once __DIR__   . '/admin_footer.php';
411
        //xoops_cp_footer();
412
        break;
413
}
414