Completed
Push — master ( 2ebe1b...b0c184 )
by Michael
01:49
created

ContactContactHandler::contactGetCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 25.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xmf\Request;
0 ignored issues
show
Bug introduced by
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * Contact module
16
 *
17
 * @copyright     XOOPS Project (https://xoops.org)
18
 * @license       http://www.fsf.org/copyleft/gpl.html GNU public license
19
 * @author        Kazumi Ono (aka Onokazu)
20
 * @author        Trabis <[email protected]>
21
 * @author        Hossein Azizabadi (AKA Voltan)
22
 * @author        Mirza (AKA Bleekk)
23
 */
24
25
defined('XOOPS_ROOT_PATH') || die('Restricted access');
26
27
28
/**
29
 * Class ContactContactHandler
30
 */
31
class ContactContactHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsPersistableObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    /**
34
     * ContactContactHandler constructor.
35
     * @param null|\XoopsDatabase $db
36
     */
37
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
    {
39
        parent::__construct($db, 'contact', Contact::class, 'contact_id', 'contact_mail');
40
    }
41
42
    /**
43
     * Get variables passed by GET or POST method
44
     * @param        $global
45
     * @param        $key
46
     * @param string $default
47
     * @param string $type
48
     * @return false|int|mixed|string
49
     */
50
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
    public function contactCleanVars(&$global, $key, $default = '', $type = 'int')
52
    {
53
        switch ($type) {
54
            case 'array':
55
                $ret = (isset($global[$key]) && is_array($global[$key])) ? $global[$key] : $default;
56
                break;
57
            case 'date':
58
                $ret = isset($global[$key]) ? strtotime($global[$key]) : $default;
59
                break;
60
            case 'string':
61
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_MAGIC_QUOTES) : $default;
62
                break;
63
            case 'mail':
64
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_VALIDATE_EMAIL) : $default;
65
                break;
66
            case 'url':
67
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) : $default;
68
                break;
69
            case 'ip':
70
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_VALIDATE_IP) : $default;
71
                break;
72
            case 'amp':
73
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_FLAG_ENCODE_AMP) : $default;
74
                break;
75
            case 'text':
76
                $ret = isset($global[$key]) ? htmlentities($global[$key], ENT_QUOTES, 'UTF-8') : $default;
77
                break;
78
            case 'platform':
79
                $ret = isset($global[$key]) ? $this->contactPlatform($global[$key]) : $this->contactPlatform($default);
80
                break;
81
            case 'type':
82
                $ret = isset($global[$key]) ? $this->contactType($global[$key]) : $this->contactType($default);
83
                break;
84
            case 'int':
85
            default:
86
                $ret = isset($global[$key]) ? filter_var($global[$key], FILTER_SANITIZE_NUMBER_INT) : $default;
87
                break;
88
        }
89
        if ($ret === false) {
90
            return $default;
91
        }
92
93
        return $ret;
94
    }
95
*/
96
97
    /**
98
     * @return array
99
     */
100
    public function contactInfoProcessing()
101
    {
102
        $contact                       = [];
103
        $contact['contact_cid']        = Request::getInt('contact_id', 0, 'POST');
104
        $contact['contact_uid']        = Request::getInt('contact_uid', 0, 'POST');
105
        $contact['contact_name']       = Request::getString('contact_name', '', 'POST');
106
        $contact['contact_nameto']     = Request::getString('contact_nameto', '', 'POST');
107
        $contact['contact_subject']    = Request::getString('contact_subject', '', 'POST');
108
        $contact['contact_mail']       = Request::getString('contact_mail', '', 'POST');
109
        $contact['contact_mailto']     = Request::getEmail('contact_mailto', '', 'POST');
110
        $contact['contact_url']        = Request::getUrl('contact_url', '', 'POST');
111
        $contact['contact_create']     = time();
112
        $contact['contact_icq']        = Request::getString('contact_icq', '', 'POST');
113
        $contact['contact_company']    = Request::getString('contact_company', '', 'POST');
114
        $contact['contact_location']   = Request::getString('contact_location', '', 'POST');
115
        $contact['contact_phone']      = Request::getString('contact_phone', '', 'int');
116
        $contact['contact_department'] = Request::getString('contact_department', _MD_CONTACT_DEFULTDEP, 'POST');
117
        $contact['contact_ip']         = getenv('REMOTE_ADDR');
118
        $contact['contact_message']    = Request::getText('contact_message', '', 'POST');
119
        $contact['contact_address']    = Request::getString('contact_address', '', 'POST');
120
        $contact['contact_platform']   = Request::getString('contact_platform', 'Web', 'POST');
121
        $contact['contact_type']       = Request::getString('contact_type', 'Contact', 'POST');
122
        $contact['contact_reply']      = Request::getInt('contact_reply', 0, 'POST');
123
124
        return $contact;
125
    }
126
127
    /**
128
     * @param $contact
129
     * @return string
130
     */
131
    public function contactSendMail($contact)
132
    {
133
        $xoopsMailer = xoops_getMailer();
0 ignored issues
show
Bug introduced by
The function xoops_getMailer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

133
        $xoopsMailer = /** @scrutinizer ignore-call */ xoops_getMailer();
Loading history...
134
        $xoopsMailer->useMail();
135
        $xoopsMailer->setToEmails($this->contactToEmails($contact['contact_department']));
136
        $xoopsMailer->setFromEmail($contact['contact_mail']);
137
        $xoopsMailer->setFromName(html_entity_decode($contact['contact_name'], ENT_QUOTES, 'UTF-8'));
138
139
        $subjectPrefix = '';
140
        if ($GLOBALS['xoopsModuleConfig']['form_dept'] && $GLOBALS['xoopsModuleConfig']['subject_prefix'] && $GLOBALS['xoopsModuleConfig']['contact_dept']) {
141
            $subjectPrefix = '[' . $GLOBALS['xoopsModuleConfig']['prefix_text'] . ' ' . $contact['contact_department'] . ']: ';
142
        }
143
        $xoopsMailer->setSubject($subjectPrefix . html_entity_decode($contact['contact_subject'], ENT_QUOTES, 'UTF-8'));
144
        $xoopsMailer->setBody(html_entity_decode($contact['contact_message'], ENT_QUOTES, 'UTF-8'));
145
        if ($xoopsMailer->send()) {
146
            $message = _MD_CONTACT_MES_SEND;
147
        } else {
148
            $message = $xoopsMailer->getErrors();
149
        }
150
151
        return $message;
152
    }
153
154
    /**
155
     * @param $contact
156
     * @return string
157
     */
158
    public function contactSendMailConfirm($contact)
159
    {
160
        $xoopsMailer = xoops_getMailer();
0 ignored issues
show
Bug introduced by
The function xoops_getMailer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

160
        $xoopsMailer = /** @scrutinizer ignore-call */ xoops_getMailer();
Loading history...
161
        $xoopsMailer->useMail();
162
        $xoopsMailer->setToEmails($contact['contact_mail']);
163
        $xoopsMailer->setFromEmail($this->contactToEmails($contact['contact_department']));
164
        $xoopsMailer->setFromName(html_entity_decode($GLOBALS['xoopsConfig']['sitename'], ENT_QUOTES, 'UTF-8'));
165
166
        $xoopsMailer->setSubject(_MD_CONTACT_MAILCONFIRM_SUBJECT);
167
        $body = str_replace('{NAME}', html_entity_decode($contact['contact_name'], ENT_QUOTES, 'UTF-8'), _MD_CONTACT_MAILCONFIRM_BODY);
168
        $body = str_replace('{SUBJECT}', html_entity_decode($contact['contact_subject'], ENT_QUOTES, 'UTF-8'), $body);
169
        $body = str_replace('{BODY}', html_entity_decode($contact['contact_message'], ENT_QUOTES, 'UTF-8'), $body);
170
        $xoopsMailer->setBody($body);
171
        if ($xoopsMailer->send()) {
172
            $message = _MD_CONTACT_MES_SEND;
173
        } else {
174
            $message = $xoopsMailer->getErrors();
175
        }
176
177
        return $message;
178
    }
179
180
    /**
181
     * @param $contact
182
     * @return string
183
     */
184
    public function contactReplyMail($contact)
185
    {
186
        $xoopsMailer = xoops_getMailer();
0 ignored issues
show
Bug introduced by
The function xoops_getMailer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

186
        $xoopsMailer = /** @scrutinizer ignore-call */ xoops_getMailer();
Loading history...
187
        $xoopsMailer->useMail();
188
        $xoopsMailer->setToEmails($contact['contact_mailto']);
189
        $xoopsMailer->setFromEmail($contact['contact_mail']);
190
        $xoopsMailer->setFromName($contact['contact_name']);
191
        $xoopsMailer->setSubject($contact['contact_subject']);
192
        $xoopsMailer->setBody($contact['contact_message']);
193
        if ($xoopsMailer->send()) {
194
            $message = _MD_CONTACT_MES_SEND;
195
        } else {
196
            $message = $xoopsMailer->getErrors();
197
        }
198
199
        return $message;
200
    }
201
202
    /**
203
     * @param null $department
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $department is correct as it would always require null to be passed?
Loading history...
204
     * @return array
205
     */
206
    public function contactToEmails($department = null)
207
    {
208
        //        global $xoopsConfig;
209
        $department_mail[] = xoops_getModuleOption('contact_recipient_std', 'contact');
0 ignored issues
show
Bug introduced by
The function xoops_getModuleOption was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

209
        $department_mail[] = /** @scrutinizer ignore-call */ xoops_getModuleOption('contact_recipient_std', 'contact');
Loading history...
Comprehensibility Best Practice introduced by
$department_mail was never initialized. Although not strictly required by PHP, it is generally a good practice to add $department_mail = array(); before regardless.
Loading history...
210
        if (!empty($department)) {
211
            $departments = xoops_getModuleOption('contact_dept', 'contact');
212
            foreach ($departments as $vals) {
213
                $vale = explode(',', $vals);
214
                if ($department == $vale[0]) {
215
                    $department_mail[] = $vale[1];
216
                }
217
            }
218
        }
219
220
        return $department_mail;
221
    }
222
223
    /**
224
     * @param $contact_id
225
     * @return bool
226
     */
227
    public function contactAddReply($contact_id)
228
    {
229
        $obj = $this->get($contact_id);
230
        $obj->setVar('contact_reply', 1);
231
        if (!$this->insert($obj)) {
232
            return false;
233
        }
234
235
        return true;
236
    }
237
238
    /**
239
     * @param $contact_id
240
     * @return array|bool
241
     */
242
    public function contactGetReply($contact_id)
243
    {
244
        $ret      = false;
245
246
        $criteria = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
247
        $criteria->add(new \Criteria('contact_cid', $contact_id));
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
248
        $criteria->add(new \Criteria('contact_type', 'Contact'));
249
        $contacts = $this->getObjects($criteria, false);
250
        if ($contacts) {
251
            $ret = [];
252
            /** @var Contact $root */
253
            foreach ($contacts as $root) {
254
                $tab                   = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tab is dead and can be removed.
Loading history...
255
                $tab                   = $root->toArray();
256
                $tab['contact_owner']  = XoopsUser::getUnameFromId($root->getVar('contact_uid'));
0 ignored issues
show
Bug introduced by
The type XoopsUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
257
                $tab['contact_create'] = formatTimestamp($root->getVar('contact_create'), _MEDIUMDATESTRING);
0 ignored issues
show
Bug introduced by
The constant _MEDIUMDATESTRING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function formatTimestamp was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

257
                $tab['contact_create'] = /** @scrutinizer ignore-call */ formatTimestamp($root->getVar('contact_create'), _MEDIUMDATESTRING);
Loading history...
258
                $ret []                = $tab;
259
            }
260
        }
261
262
        return $ret;
263
    }
264
265
    /**
266
     * @param $contact
267
     * @param $id
268
     * @return array
269
     */
270
    public function contactGetAdminList($contact, $id)
271
    {
272
        $ret      = [];
273
        $criteria = new \CriteriaCompo();
274
        $criteria->add(new \Criteria($id, '0'));
275
        $criteria->add(new \Criteria('contact_type', 'Contact'));
276
        $criteria->setSort($contact['sort']);
277
        $criteria->setOrder($contact['order']);
278
        $criteria->setStart($contact['start']);
279
        $criteria->setLimit($contact['limit']);
280
        $contacts = $this->getObjects($criteria, false);
281
        if ($contacts) {
282
            /** @var Contact $root */
283
            foreach ($contacts as $root) {
284
                $tab                   = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $tab is dead and can be removed.
Loading history...
285
                $tab                   = $root->toArray();
286
                $tab['contact_owner']  = XoopsUser::getUnameFromId($root->getVar('contact_uid'));
287
                $tab['contact_create'] = formatTimestamp($root->getVar('contact_create'), _MEDIUMDATESTRING);
0 ignored issues
show
Bug introduced by
The function formatTimestamp was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

287
                $tab['contact_create'] = /** @scrutinizer ignore-call */ formatTimestamp($root->getVar('contact_create'), _MEDIUMDATESTRING);
Loading history...
Bug introduced by
The constant _MEDIUMDATESTRING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
288
                $ret []                = $tab;
289
            }
290
        }
291
292
        return $ret;
293
    }
294
295
    /**
296
     * Get file Count
297
     * @param $id
298
     * @return int
299
     */
300
    public function contactGetCount($id)
301
    {
302
        $criteria = new \CriteriaCompo();
303
        $criteria->add(new \Criteria($id, '0'));
304
        $criteria->add(new \Criteria('contact_type', 'Contact'));
305
306
        return $this->getCount($criteria);
307
    }
308
309
    /**
310
     * Get Insert ID
311
     */
312
    public function getInsertId()
313
    {
314
        return $this->db->getInsertId();
315
    }
316
317
    /**
318
     * Contact Prune Count
319
     * @param $timestamp
320
     * @param $onlyreply
321
     * @return int
322
     */
323
    public function contactPruneCount($timestamp, $onlyreply)
324
    {
325
        $criteria = new \CriteriaCompo();
326
        $criteria->add(new \Criteria('contact_create', $timestamp, '<='));
327
        if ($onlyreply) {
328
            $criteria->add(new \Criteria('contact_reply', 1));
329
        }
330
331
        return $this->getCount($criteria);
332
    }
333
334
    /**
335
     * Contact Delete Before Date
336
     * @param $timestamp
337
     * @param $onlyreply
338
     */
339
    public function contactDeleteBeforeDate($timestamp, $onlyreply)
340
    {
341
        $criteria = new \CriteriaCompo();
342
        $criteria->add(new \Criteria('contact_create', $timestamp, '<='));
343
        if ($onlyreply) {
344
            $criteria->add(new \Criteria('contact_reply', 1));
345
        }
346
        $this->deleteAll($criteria);
347
    }
348
349
    /**
350
     * Contact Platform
351
     * @param $platform
352
     * @return string
353
     */
354
    public function contactPlatform($platform)
355
    {
356
        $platform = strtolower($platform);
357
        switch ($platform) {
358
            case 'Android':
359
                $ret = 'Android';
360
                break;
361
362
            case 'Ios':
363
                $ret = 'Ios';
364
                break;
365
366
            case 'Web':
367
            default:
368
                $ret = 'Web';
369
                break;
370
        }
371
372
        return $ret;
373
    }
374
375
    /**
376
     * Contact type
377
     * @param $type
378
     * @return string
379
     */
380
    public function contactType($type)
381
    {
382
        $type = strtolower($type);
383
        switch ($type) {
384
            case 'Mail':
385
                $ret = 'Mail';
386
                break;
387
388
            case 'Phone':
389
                $ret = 'Phone';
390
                break;
391
392
            case 'Contact':
393
            default:
394
                $ret = 'Contact';
395
                break;
396
        }
397
398
        return $ret;
399
    }
400
401
    /**
402
     * Contact logs
403
     * @param      $column
404
     * @param null $timestamp
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $timestamp is correct as it would always require null to be passed?
Loading history...
405
     * @return array
406
     */
407
    public function contactLogs($column, $timestamp = null)
408
    {
409
        $ret = [];
410
        if (!in_array($column, ['contact_mail', 'contact_url', 'contact_phone'])) {
411
            return $ret;
412
        }
413
        $criteria = new \CriteriaCompo();
414
        $criteria->add(new \Criteria('contact_cid', '0'));
415
        if (!empty($timestamp)) {
416
            $criteria->add(new \Criteria('contact_create', $timestamp, '<='));
417
        }
418
        $criteria->setSort('contact_create');
419
        $criteria->setOrder('DESC');
420
        $contacts = $this->getObjects($criteria, false);
421
        if ($contacts) {
422
            /** @var Contact $root */
423
            foreach ($contacts as $root) {
424
                $rootColumn = $root->getVar($column);
425
                if (!empty($rootColumn)) {
426
                    $ret[] = $root->getVar($column);
427
                    unset($root);
428
                }
429
            }
430
        }
431
432
        return array_unique($ret);
433
    }
434
}
435