Utility   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 88
dl 0
loc 188
rs 9.52
c 1
b 0
f 0
wmc 36

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_badips() 0 17 4
A getCountry() 0 15 4
A email_exist() 0 11 3
A getAllCountry() 0 19 4
B upload() 0 28 8
B get_user_data() 0 28 8
A clear_tmp_files() 0 18 5
1
<?php
2
3
namespace XoopsModules\Xfguestbook;
4
5
use XoopsModules\Xfguestbook;
6
use XoopsModules\Xfguestbook\Common;
7
use XoopsModules\Xfguestbook\Constants;
8
9
/**
10
 * Class Utility
11
 */
12
class Utility extends Common\SysUtility
13
{
14
    //--------------- Custom module methods -----------------------------
15
16
    public static function upload()
17
    {
18
        global $xoopsModule, $preview_name, $msgstop;
19
        /** @var Helper $helper */
20
        $helper = Helper::getInstance();
21
22
        $created = \time();
23
        $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...
24
        require_once XOOPS_ROOT_PATH . '/class/uploader.php';
25
        $field = $_POST['xoops_upload_file'][0];
26
        if (!empty($field) || '' !== $field) {
27
            // Check if file uploaded
28
            if ('' === $_FILES[$field]['tmp_name'] || !\is_readable($_FILES[$field]['tmp_name'])) {
29
                $msgstop .= \sprintf(MD_XFGUESTBOOK_FILEERROR, $helper->getConfig('photo_maxsize'));
30
            } else {
31
                $photos_dir              = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname');
32
                $array_allowed_mimetypes = ['image/gif', 'image/pjpeg', 'image/jpeg', 'image/x-png'];
33
                $uploader                = new \XoopsMediaUploader($photos_dir, $array_allowed_mimetypes, $helper->getConfig('photo_maxsize'), $helper->getConfig('photo_maxwidth'), $helper->getConfig('photo_maxheight'));
34
                if ($uploader->fetchMedia($field) && $uploader->upload()) {
35
                    if (null !== $preview_name) {
36
                        @\unlink("$photos_dir/" . $preview_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). 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

36
                        /** @scrutinizer ignore-unhandled */ @\unlink("$photos_dir/" . $preview_name);

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...
37
                    }
38
                    $tmp_name     = $uploader->getSavedFileName();
39
                    $ext          = \preg_replace("/^.+\.([^.]+)$/sU", '\\1', $tmp_name);
40
                    $preview_name = 'tmp_' . $created . '.' . $ext;
41
                    \rename("$photos_dir/$tmp_name", "$photos_dir/$preview_name");
42
                } else {
43
                    $msgstop .= $uploader->getErrors();
44
                }
45
            }
46
        }
47
    }
48
49
    /**
50
     * @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...
51
     * @param int  $limit
52
     * @param int  $start
53
     * @return array
54
     */
55
    public static function getCountry($criteria = null, $limit = 0, $start = 0)
56
    {
57
        global $xoopsDB, $action;
58
        $ret = [];
59
        $sql = 'SELECT * FROM ' . $xoopsDB->prefix('xfguestbook_country');
60
        if (null !== $criteria && '' !== $criteria) {
0 ignored issues
show
introduced by
The condition null !== $criteria is always false.
Loading history...
61
            $sql .= ' WHERE ' . $criteria;
62
        }
63
        $sql    .= ' ORDER BY country_code ASC';
64
        $result = $xoopsDB->query($sql, $limit, $start);
65
        while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
66
            $ret[] = $myrow;
67
        }
68
69
        return $ret;
70
    }
71
72
    /**
73
     * @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...
74
     * @param int  $limit
75
     * @param int  $start
76
     * @return array
77
     */
78
    public static function getAllCountry($criteria = null, $limit = 0, $start = 0)
79
    {
80
        global $xoopsDB, $action;
81
        /** @var Helper $helper */
82
        $helper = Helper::getInstance();
83
84
        $ret = [];
85
        $sql = 'SELECT country_code, country_name FROM ' . $xoopsDB->prefix('xfguestbook_country');
86
        if (null !== $criteria && '' !== $criteria) {
0 ignored issues
show
introduced by
The condition null !== $criteria is always false.
Loading history...
87
            $sql .= ' WHERE ' . $criteria;
88
        }
89
        $sql    .= ' ORDER BY country_code ASC';
90
        $result = $xoopsDB->query($sql, $limit, $start);
91
        while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
92
            //      $ret[$myrow['country_code']] = $myrow['country_name'];
93
            $ret[$helper->getConfig('flagdir') . '/' . $myrow['country_code']] = $myrow['country_name'];
94
        }
95
96
        return $ret;
97
    }
98
99
    /**
100
     * @param $user_id
101
     * @return bool
102
     */
103
    public static function get_user_data($user_id)
104
    {
105
        global $xoopsUser;
106
        /** @var Helper $helper */
107
        $helper = Helper::getInstance();
108
109
        if (!(int)$user_id) {
110
            return false;
111
        }
112
113
        $poster = new \XoopsUser($user_id);
114
        if ($poster->isActive()) {
115
            $xoopsUser ? $a_poster['poster'] = "<a href='../../userinfo.php?uid=$user_id'>" . $poster->uname() . '</a>' : $a_poster['poster'] = $poster->uname();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$a_poster was never initialized. Although not strictly required by PHP, it is generally a good practice to add $a_poster = array(); before regardless.
Loading history...
116
            if ($helper->getConfig('display_avatar')) {
117
                $rank = $poster->rank();
118
                $rank['title'] ? $a_poster['rank'] = $rank['title'] : $a_poster['rank'] = '';
119
                $rank['image'] ? $a_poster['rank_img'] = "<img src='" . XOOPS_URL . '/uploads/' . $rank['image'] . '\' alt=\'\'>' : $a_poster['rank_img'] = '';
120
                $poster->user_avatar() ? $a_poster['avatar'] = "<img src='" . XOOPS_URL . '/uploads/' . $poster->user_avatar() . '\' alt=\'\'>' : $a_poster['avatar'] = '';
121
            } else {
122
                $a_poster['rank']     = '';
123
                $a_poster['avatar']   = '';
124
                $a_poster['rank_img'] = '';
125
            }
126
127
            return $a_poster;
128
        }
129
130
        return false;
131
    }
132
133
    // Effacement fichiers temporaires
134
135
    /**
136
     * @param         $dir_path
137
     * @param string  $prefix
138
     * @return int
139
     */
140
    public static function clear_tmp_files($dir_path, $prefix = 'tmp_')
141
    {
142
        if (!($dir = @\opendir($dir_path))) {
143
            return 0;
144
        }
145
        $ret        = 0;
146
        $prefix_len = mb_strlen($prefix);
0 ignored issues
show
Unused Code introduced by
The assignment to $prefix_len is dead and can be removed.
Loading history...
147
        while (false !== ($file = \readdir($dir))) {
148
            //        if (strncmp($file, $prefix, $prefix_len) === 0) {
149
            if (0 === mb_strpos($file, $prefix)) {
150
                if (@\unlink("$dir_path/$file")) {
151
                    $ret++;
152
                }
153
            }
154
        }
155
        \closedir($dir);
156
157
        return $ret;
158
    }
159
160
    // IP bannies (modérés automatiquement)
161
162
    /**
163
     * @param null $all
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $all is correct as it would always require null to be passed?
Loading history...
164
     * @return array
165
     */
166
    public static function get_badips($all = null)
167
    {
168
        global $xoopsDB;
169
        $ret    = [];
170
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('xfguestbook_badips');
171
        $result = $xoopsDB->query($sql);
172
        if ($all) {
0 ignored issues
show
introduced by
$all is of type null, thus it always evaluated to false.
Loading history...
173
            while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
174
                $ret[] = $myrow;
175
            }
176
        } else {
177
            while (list($ip_id, $ip_value) = $xoopsDB->fetchRow($result)) {
178
                $ret[] = $ip_value;
179
            }
180
        }
181
182
        return $ret;
183
    }
184
185
    /**
186
     * @param $email
187
     * @return bool
188
     */
189
    public static function email_exist($email)
190
    {
191
        if (!\filter_var($email, \FILTER_VALIDATE_EMAIL)) {
192
            return false;
193
        }
194
195
        if (!\checkdnsrr(\array_pop(\explode('@', $email)), 'MX')) {
0 ignored issues
show
Bug introduced by
explode('@', $email) cannot be passed to array_pop() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

195
        if (!\checkdnsrr(\array_pop(/** @scrutinizer ignore-type */ \explode('@', $email)), 'MX')) {
Loading history...
196
            return false;
197
        }
198
199
        return true;
200
    }
201
}
202