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

Utility::email_exist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Xfguestbook;
2
3
use Xmf\Request;
4
use XoopsModules\Xfguestbook;
5
use XoopsModules\Xfguestbook\Common;
6
7
8
/**
9
 * Class Utility
10
 */
11
class Utility
12
{
13
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
14
15
    use Common\ServerStats; // getServerStats Trait
16
17
    use Common\FilesManagement; // Files Management Trait
18
19
    //--------------- Custom module methods -----------------------------
20
21
22
    public static function upload()
23
    {
24
        global $xoopsModule,  $preview_name, $msgstop;
25
        /** @var Xfguestbook\Helper $helper */
26
        $helper = Xfguestbook\Helper::getInstance();
27
28
        $created = time();
29
        $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...
30
        require_once XOOPS_ROOT_PATH . '/class/uploader.php';
31
        $field = $_POST['xoops_upload_file'][0];
32
        if (!empty($field) || '' !== $field) {
33
            // Check if file uploaded
34
            if ('' === $_FILES[$field]['tmp_name'] || !is_readable($_FILES[$field]['tmp_name'])) {
35
                $msgstop .= sprintf(MD_XFGUESTBOOK_FILEERROR, $helper->getConfig('photo_maxsize'));
36
            } else {
37
                $photos_dir              = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname');
38
                $array_allowed_mimetypes = ['image/gif', 'image/pjpeg', 'image/jpeg', 'image/x-png'];
39
                $uploader                = new \XoopsMediaUploader($photos_dir, $array_allowed_mimetypes, $helper->getConfig('photo_maxsize'), $helper->getConfig('photo_maxwidth'), $helper->getConfig('photo_maxheight'));
40
                if ($uploader->fetchMedia($field) && $uploader->upload()) {
41
                    if (null !== $preview_name) {
42
                        @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

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

199
        } elseif (!checkdnsrr(array_pop(/** @scrutinizer ignore-type */ explode('@', $email)), 'MX')) {
Loading history...
200
            return false;
201
        } else {
202
            return true;
203
        }
204
    }
205
}
206