isDepartmentEmail()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 17
rs 9.9332
1
<?php declare(strict_types=1);
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @author       Brian Wahoff <[email protected]>
17
 * @author       Eric Juden <[email protected]>
18
 * @author       XOOPS Development Team
19
 */
20
21
use XoopsModules\Xhelp;
22
23
require __DIR__ . '/servicemain.php';
24
25
//Include xhelp Related Includes
26
//require_once XHELP_INCLUDE_PATH . '/events.php';
27
//require_once XHELP_CLASS_PATH . '/mailboxPOP3.php';
28
//require_once XHELP_CLASS_PATH . '/EmailParser.php';
29
//require_once XHELP_CLASS_PATH . '/EmailStore.php';
30
//require_once XHELP_CLASS_PATH . '/validator.php';
31
32
$helper       = Xhelp\Helper::getInstance();
33
$eventService = Xhelp\EventService::getInstance();
34
35
//Initialize xhelp objects
36
$msgParser = new Xhelp\EmailParser();
37
$msgStore  = new Xhelp\EmailStore();
38
/** @var \XoopsModules\Xhelp\DepartmentMailBoxHandler $departmentMailBoxHandler */
39
$departmentMailBoxHandler = $helper->getHandler('DepartmentMailBox');
40
/** @var \XoopsModules\Xhelp\MailEventHandler $mailEventHandler */
41
$mailEventHandler = $helper->getHandler('MailEvent');
42
/** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */
43
$ticketHandler = $helper->getHandler('Ticket');
44
//$notificationService =  Xhelp\NotificationService::getInstance();
45
46
//$eventService->advise('new_user_by_email', Xhelp\NotificationService::getInstance(), 'new_user_activation' . $xoopsConfigUser['activation_type']);
47
$eventService->advise('new_user_by_email', 'new_user_activation' . $xoopsConfigUser['activation_type']);
48
49
//Get All Department Mailboxes
50
$departmentMailBoxes = $departmentMailBoxHandler->getActiveMailboxes();
51
52
//Loop Through All Department Mailboxes
53
foreach ($departmentMailBoxes as $mbox) {
54
    $deptid = $mbox->getVar('departmentid');
55
    //Connect to the mailbox
56
    if ($mbox->connect()) {
57
        //Check for new messages
58
        if ($mbox->hasMessages()) {
59
            //Retrieve / Store each message
60
            while ($msg = $mbox->getMessage()) {
61
                $msg_logs = [];
62
                $skip_msg = false;
63
64
                //Check if there are any errors parsing msg
65
                $parsed = $msgParser->parseMessage($msg);
66
                if ($parsed) {
67
                    //Sanity Check: Disallow emails from other department mailboxes
68
                    if (isDepartmentEmail($parsed->getEmail())) {
69
                        $msg_logs[_XHELP_MAIL_CLASS3][] = sprintf(_XHELP_MESSAGE_EMAIL_DEPT_MBOX, $parsed->getEmail());
70
                    } else {
71
                        //Create new user account if necessary
72
                        if (!$xoopsUser = Xhelp\Utility::emailIsXoopsUser($parsed->getEmail())) {
73
                            if ($helper->getConfig('xhelp_allowAnonymous')) {
74
                                switch ($xoopsConfigUser['activation_type']) {
75
                                    case 1:
76
                                        $level = 1;
77
                                        break;
78
                                    case 0:
79
                                    case 2:
80
                                    default:
81
                                        $level = 0;
82
                                }
83
                                $xoopsUser = Xhelp\Utility::getXoopsAccountFromEmail($parsed->getEmail(), $parsed->getName(), $password, $level);
84
                                $eventService->trigger('new_user_by_email', [$password, $xoopsUser]);
85
                            } else {
86
                                $msg_logs[_XHELP_MAIL_CLASS3][] = sprintf(_XHELP_MESSAGE_NO_ANON, $parsed->getEmail());
87
                                $skip_msg                       = true;
88
                            }
89
                        }
90
91
                        if (false === $skip_msg) {
92
                            //Store Message In Server
93
                            $obj = $msgStore->storeMsg($parsed, $xoopsUser, $mbox, $errors);
0 ignored issues
show
Bug introduced by
It seems like $xoopsUser can also be of type boolean; however, parameter $user of XoopsModules\Xhelp\EmailStore::storeMsg() does only seem to accept XoopsUser, maybe add an additional type check? ( Ignorable by Annotation )

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

93
                            $obj = $msgStore->storeMsg($parsed, /** @scrutinizer ignore-type */ $xoopsUser, $mbox, $errors);
Loading history...
94
                            if ($obj) {
95
                                switch ($parsed->getMsgType()) {
96
                                    case _XHELP_MSGTYPE_TICKET:
97
                                        //Trigger New Ticket Events
98
                                        $eventService->trigger('new_ticket', $obj);
99
                                        break;
100
                                    case _XHELP_MSGTYPE_RESPONSE:
101
                                        //Trigger New Response Events
102
                                        $eventService->trigger('new_response', $obj);
103
                                        break;
104
                                }
105
                                //} else {        // If message not stored properly, log event
106
                                //    $storeEvent = $mailEventHandler->newEvent($mbox->getVar('id'), _XHELP_MAILEVENT_DESC2, _XHELP_MAILEVENT_CLASS2);
107
                            } else {
108
                                $msg_logs[_XHELP_MAILEVENT_CLASS2] = &$errors;
109
                            }
110
                        }
111
                    }
112
                } else {
113
                    $msg_logs[_XHELP_MAILEVENT_CLASS1][] = _XHELP_MAILEVENT_DESC1;
114
                }
115
                //Remove Message From Server
116
                $mbox->deleteMessage($msg);
117
118
                //Log Any Messages
119
                logMessages($mbox->getVar('id'), $msg_logs);
120
            }
121
        }
122
        //Disconnect from Server
123
        $mbox->disconnect();
124
    } else {                        // If mailbox not connected properly, log event
125
        $connEvent = $mailEventHandler->newEvent($mbox->getVar('id'), _XHELP_MAILEVENT_DESC0, (string)_XHELP_MAILEVENT_CLASS0);
126
    }
127
}
128
129
/**
130
 * @param int   $mbox
131
 * @param array $arr
132
 */
133
function logMessages(int $mbox, array $arr)
134
{
135
    global $mailEventHandler;
136
    foreach ($arr as $class => $msg) {
137
        if (is_array($msg)) {
138
            $msg = implode("\r\n", $msg);
139
        }
140
        $event = $mailEventHandler->newEvent($mbox, $msg, $class);
0 ignored issues
show
Unused Code introduced by
The assignment to $event is dead and can be removed.
Loading history...
141
    }
142
}
143
144
/**
145
 * @param string $email
146
 * @return bool
147
 */
148
function isDepartmentEmail(string $email): bool
149
{
150
    static $email_arr;
151
152
    if (null === $email_arr) {
153
        global $departmentMailBoxHandler;
154
        $departmentMailBoxes = $departmentMailBoxHandler->getObjects();
155
        $email_arr           = [];
156
        foreach ($departmentMailBoxes as $mbox) {
157
            $email_arr[] = $mbox->getVar('emailaddress');
158
        }
159
        unset($departmentMailBoxes);
160
    }
161
162
    $ret = in_array($email, $email_arr);
163
164
    return $ret;
165
}
166