Completed
Push — master ( 9a4ef0...878baf )
by
unknown
01:47
created

smallworld_mail   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 196
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 196
loc 196
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C Mail::sendMails() 4 155 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace XoopsModules\Smallworld;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * SmallWorld
17
 *
18
 * @package      \XoopsModules\Smallworld
19
 * @license      GNU GPL (https://www.gnu.org/licenses/gpl-2.0.html/)
20
 * @copyright    The XOOPS Project (https://xoops.org)
21
 * @copyright    2011 Culex
22
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
23
 * @link         https://github.com/XoopsModules25x/smallworld
24
 * @since        1.0
25
 */
26
require_once XOOPS_ROOT_PATH . '/class/mail/xoopsmultimailer.php';
27
28
/**
29
 * Class Mail
30
 */
31
class Mail
32
{
33
    /** Function to send mails to users based on certain events
34
     *
35
     * $fromUserId = uid, $toUserId = uid
36
     * $event : 'register' = New user registration,
37
     * 	'complatint' = Complaint agains a wall message,
38
     *  'newavatar' = User has opload new avatar, 'commentToWM' = New comment to your update
39
     * Register, complaint, newavatar is sent only to site admin, commentToWM to owner user of wall update
40
     * Link is optional, defaul null. Could be a link to Userprofile, or singlepage wall update.
41
     * Itemtext is text from comments or complaints to be sent by mail..
42
     * Result: send mail, return true or false
43
     *
44
     * @param int         $fromUserId
45
     * @param int         $toUserId
46
     * @param string      $event
47
     * @param null|string $link
48
     * @param array       $data
49
     * @throws \phpmailerException
50
     * @return bool  true on success, false on failure
51
     */
52
    public function sendMails($fromUserId, $toUserId, $event, $link, array $data)
53
    {
54
        $date    = date('m-d-Y H:i:s', time());
55
        $mail    = new \XoopsMultiMailer();
56
        $wall    = new WallUpdates();
0 ignored issues
show
Unused Code introduced by
$wall is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
        $tpl     = new \XoopsTpl();
0 ignored issues
show
Unused Code introduced by
$tpl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
        $message = '';
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
        /**
60
         * \XoopsModules\Smallworld\Helper $helper
61
         * \XoopsModules\Smallworld\SwUserHandler $swUserHandler
62
         */
63
        $helper        = Helper::getInstance();
64
        $swUserHandler = $helper->getHandler('SwUser');
65
66
        // From and To user ids
67
        $fromXuser       = new \XoopsUser($fromUserId);
68
        $fromAvatar      = $swUserHandler->gravatar($fromUserId);
69
        $fromAvatarlink  = "<img class='left' src='" . $swUserHandler->getAvatarLink($fromUserId, $fromAvatar) . "' height='90px' width='90px'>";
70
        $toXuser         = new \XoopsUser($toUserId);
71
        $toAvatar        = $swUserHandler->gravatar($toUserId);
72
        $toAvatarlink    = "<img class='left' src='" . $swUserHandler->getAvatarLink($toUserId, $toAvatar) . "' height='90px' width='90px'>";
0 ignored issues
show
Unused Code introduced by
$toAvatarlink is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
        // Sender's XOOPS username
74
        $sendName        = $fromXuser->getVar('uname');
75
        $sendNameUrl     = "<a href='" . $helper->url("userprofile.php?username={$sendName}") . "'>{$sendName}</a>";
76
        // Receiver's XOOPS username and email
77
        $receiveName     = $toXuser->getVar('uname');
78
        $receiveNameUrl  = "<a href='" . $helper->url("userprofile.php?username={$receiveName}") . "'>{$receiveName}</a>";
0 ignored issues
show
Unused Code introduced by
$receiveNameUrl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
80
        // Checking content of 'event' to send right message
81
        switch ($event) {
82
            case ('register'):
83
                $subject = _SMALLWORLD_MAIL_REGISTERSUBJECT . $GLOBALS['xoopsConfig']['sitename'];
84
85
                $registername  = $sendName;
86
                $toAvatarlink = "<img class='left' src='" . $swUserHandler->getAvatarLink($fromUserId, $toAvatar) . "' height='90px' width='90px'>";
87
88
                $tpl = new \XoopsTpl();
89
                $tpl->assign([
90
                    'registername' => $registername,
91
                    'sitename'     => $GLOBALS['xoopsConfig']['sitename'],
92
                    'registerurl'  => $sendNameUrl,
93
                    'registerlink' => $toAvatarlink
94
                ]);
95
96
                $lnk        = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_register.tpl');
97
                $message    = $tpl->fetch($lnk);
98
                $mail->Body = $message;
99
                $toMail     = $GLOBALS['xoopsConfig']['adminmail'];
100
                break;
101
            // Send email to admin if red/yellow card has been pressed indicating a "bad" thread has been found.
102
            case ('complaint'):
103
                $subject = _SMALLWORLD_MAIL_COMPLAINT . $GLOBALS['xoopsConfig']['sitename'];
104
105
                $senders_id  = $fromUserId;
0 ignored issues
show
Unused Code introduced by
$senders_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
                $sendersName = stripslashes($data['byuser']);
107
                $againstUser = stripslashes($data['a_user']);
108
                $time        = date('d-m-Y H:i:s', $data['time']);
109
                $link        = stripslashes($data['link']);
110
111
                $tpl = new \XoopsTpl();
112
                $tpl->assign('sendername', $sendersName);
113
                $tpl->assign('against', $againstUser);
114
                $tpl->assign('time', $time);
115
                $tpl->assign('link', $link);
116
                $tpl->assign('sitename', $GLOBALS['xoopsConfig']['sitename']);
117
118
                $lnk        = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_complaint.tpl');
119
                $message    = $tpl->fetch($lnk);
120
                $mail->Body = $message;
121
                $toMail     = $GLOBALS['xoopsConfig']['adminmail'];
122
                break;
123
            case ('commentToWM'):
124
                $subject = _SMALLWORLD_MAIL_NEWCOMMENT . $GLOBALS['xoopsConfig']['sitename'];
125
126
                $ownermessage = stripslashes($this->getOwnerUpdateFromMsgID($data['msg_id_fk']));
127 View Code Duplication
                if (preg_match('/UPLIMAGE/', $ownermessage)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
                    $ownmsg       = str_replace('UPLIMAGE ', '', $ownermessage);
129
                    $ownermessage = "<img width='300px' src='" . $ownmsg . "' style='margin: 5px 0px;' >";
130
                }
131
132
                $owner           = smallworld_getOwnerFromComment($data['msg_id_fk']);
133
                $ownerXuser      = new \XoopsUser($owner);
134
                $ownerAvatar     = $swUserHandler->gravatar($owner);
135
                $ownerAvatarlink = "<img class='left' src='" . $swUserHandler->getAvatarLink($owner, $ownerAvatar) . "' height='90px' width='90px'>";
136
                $ownerXname      = $ownerXuser->uname();
137
                $ownerXnameUrl   = "<a href='" . $helper->url("userprofile.php?username='{$ownerXname}") . "'>{$ownerXname}</a>";
138
                $replylink       = "<a href='" . $helper->url("permalink.php?ownerid={$owner}&updid={$data['msg_id_fk']}") . "'>" . _SMALLWORLD_SEEANDREPLYHERE . '</a>';
139
140
                $tpl = new \XoopsTpl();
141
                $tpl->assign([
142
                    'receivename'     => $receiveName,
143
                    'ownername'       => $ownerXname,
144
                    'ownernameurl'    => $ownerXnameUrl,
145
                    'sendname'        => $sendName,
146
                    'sendnameurl'     => $sendNameUrl,
147
                    'sitename'        => $GLOBALS['xoopsConfig']['sitename'],
148
                    'ownermessage'    => $ownermessage,
149
                    'from_avatarlink' => $fromAvatarlink,
150
                    'to_avatarlink'   => $ownerAvatarlink,
151
                    'itemtext'        => stripslashes($data['comment']),
152
                    'itemtextdate'    => $date,
153
                    'replylink'       => $replylink
154
                ]);
155
                $lnk        = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_newcomment.tpl');
156
                $message    = $tpl->fetch($lnk);
157
                $mail->Body = $message;
158
                $toMail     = $toXuser->getVar('email');
159
                break;
160
            case ('friendshipfollow'):
161
                $subject = _SMALLWORLD_MAIL_NEWFRIENDFOLLOWER . $GLOBALS['xoopsConfig']['sitename'];
162
                $link    = "<a href='" . $helper->url('index.php') . "'>" . _SMALLWORLD_GOTOSMALLWORLDHERE . "</a>";
163
164
                $tpl = new \XoopsTpl();
165
                $tpl->assign([
166
                    'toUser'   => $receiveName,
167
                    'date'     => $date,
168
                    'link'     => $link,
169
                    'sitename' => $GLOBALS['xoopsConfig']['sitename']
170
                ]);
171
172
                $lnk        = $helper->url('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_attencionneeded.tpl');
173
                $message    = $tpl->fetch($lnk);
174
                $mail->Body = $message;
175
                $toMail     = $toXuser->getVar('email');
176
                break;
177
            case ('tag'):
178
                $subject = _SMALLWORLD_MAIL_FRIENDTAGGEDYOU . $GLOBALS['xoopsConfig']['sitename'];
179
                $tpl     = new \XoopsTpl();
180
                $tpl->assign([
181
                    'toUser'   => $receiveName,
182
                    'fromUser' => $sendName,
183
                    'date'     => $date,
184
                    'link'     => $link,
185
                    'sitename'  => $GLOBALS['xoopsConfig']['sitename']
186
                ]);
187
188
                $lnk        = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_tag.tpl');
189
                $message    = $tpl->fetch($lnk);
190
                $mail->Body = $message;
191
                $toMail     = $toXuser->getVar('email');
192
                break;
193
        }
194
195
        $mail->isMail();
196
        $mail->isHTML(true);
197
        $mail->addAddress($toMail);
0 ignored issues
show
Bug introduced by
The variable $toMail does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
198
        $mail->Subject = $subject;
0 ignored issues
show
Bug introduced by
The variable $subject does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
199
200
        $retVal = true;
201
        if (!$mail->send()) {
202
            //@todo figure out what to do if failure, if anything
203
            $retVal = false;
204
        }
205
        return $retVal;
206
    }
207
208
    /*
209
     From msg_id_fk get userids in the thread and return unique array
210
    */
211
212
    /**
213
     * @param $msg_id_fk
214
     * @return array
215
     */
216
    public function getPartsFromComment($msg_id_fk)
217
    {
218
        $parts  = [];
219
        $sql    = 'SELECT uid_fk FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE msg_id_fk = '" . $msg_id_fk . "'";
220
        $result = $GLOBALS['xoopsDB']->queryF($sql);
221
        while (false !== ($r = $GLOBALS['xoopsDB']->fetchArray($result))) {
222
            $parts[] = $r['uid_fk'];
223
        }
224
225
        return array_unique($parts);
226
    }
227
228
    /**
229
     * @param $msgid
230
     * @return mixed
231
     */
232
    public function getOwnerUpdateFromMsgID($msgid)
233
    {
234
        $sql    = 'SELECT message FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " WHERE msg_id = '" . $msgid . "'";
235
        $result = $GLOBALS['xoopsDB']->queryF($sql);
236
        while (false !== ($r = $GLOBALS['xoopsDB']->fetchArray($result))) {
237
            $message = $r['message'];
238
        }
239
240
        return $message;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
241
    }
242
}
243