flagUpload()   B
last analyzed

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