Issues (11)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/queue/AbstractEmailByKey.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\queue;
10
11
use craft\queue\BaseJob;
12
use Craft;
13
use craft\elements\User;
14
use craft\helpers\StringHelper;
15
use yii\helpers\Json;
16
use yii\mail\MessageInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.3.2
21
 */
22
abstract class AbstractEmailByKey extends BaseJob
23
{
24
    /**
25
     * The email key
26
     *
27
     * @return string
28
     */
29
    abstract public function getKey(): string;
30
31
    /**
32
     * The recipients
33
     *
34
     * @return User[]
35
     */
36
    abstract public function getRecipients(): array;
37
38
    /**
39
     * The email params
40
     *
41
     * @return array
42
     */
43
    abstract public function getParams(): array;
44
45
    /**
46
     * Returns a default description for [[getDescription()]].
47
     *
48
     * @return string|null
49
     */
50
    protected function defaultDescription()
51
    {
52
        return 'Sending an email by key.';
53
    }
54
55
    /**
56
     * @param \craft\queue\QueueInterface|\yii\queue\Queue $queue
57
     * @throws \Exception
58
     */
59
    public function execute($queue)
60
    {
61
        // A random tracking string
62
        $id = StringHelper::randomString();
63
64
        try {
65
            Craft::info(
66
                sprintf(
67
                    "[Sending email via job '%s'.] %s",
68
                    (string)$id,
69
                    (string)Json::encode(
70
                        [
71
                            'tracking' => $id,
72
                            'key' => $this->getKey(),
73
                            'recipients' => count($this->getRecipients())
74
                        ]
75
                    )
76
                ),
77
                __METHOD__
78
            );
79
80
            foreach ($this->getRecipients() as $recipient) {
81
                if (!$this->composeMessage($recipient, $this->getParams())->send()) {
82
                    Craft::warning(
83
                        sprintf(
84
                            "Failed to send email via job '%s'",
85
                            (string)$id
86
                        ),
87
                        __METHOD__
88
                    );
89
                    continue;
90
                }
91
92
                Craft::info(
93
                    sprintf(
94
                        "Successfully sent email via job '%s'",
95
                        (string)$id
96
                    ),
97
                    __METHOD__
98
                );
99
            }
100
        } catch (\Exception $e) {
101
            Craft::warning(
102
                sprintf(
103
                    "Exception caught while trying to run '%s' (Id: %s) job. Exception: [%s].",
104
                    (string)get_class($this),
105
                    $id,
106
                    (string)Json::encode([
107
                        'Trace' => $e->getTraceAsString(),
108
                        'File' => $e->getFile(),
109
                        'Line' => $e->getLine(),
110
                        'Code' => $e->getCode(),
111
                        'Message' => $e->getMessage()
112
                    ])
113
                ),
114
                __METHOD__
115
            );
116
117
            throw $e;
118
        }
119
    }
120
121
    /**
122
     * @param $recipient
123
     * @param array $params
124
     * @return MessageInterface
125
     * @throws \yii\base\InvalidConfigException
126
     */
127
    protected function composeMessage($recipient, array $params): MessageInterface
128
    {
129
        // Attempt to convert a $recipient to a User element
130
        $recipient = $this->resolveUser($recipient);
131
132
        return Craft::$app->getMailer()
133
            ->composeFromKey(
134
                $this->getKey(),
135
                array_merge(
136
                    ['recipient' => $recipient],
137
                    $params
138
                )
139
            )->setTo($recipient);
140
    }
141
142
    /**
143
     * @param User|int|string|array $user
144
     * @return User
145
     */
146
    protected function resolveUser($user)
147
    {
148
        if ($user instanceof User) {
149
            return $user;
150
        }
151
152
        // An Id
153
        if (is_numeric($user)) {
154
            return Craft::$app->getUsers()->getUserById($user);
155
        }
156
157
        // An email
158
        if (is_string($user)) {
159
            if (!$element = Craft::$app->getUsers()->getUserByUsernameOrEmail($user)) {
160
                $element = new User([
161
                    'email' => $user
162
                ]);
163
            }
164
165
            return $element;
166
        }
167
168
        if (is_array($user)) {
169
            $email = key($user);
170
            $user = reset($user);
171
172
            // $user was an array [email => name]
173
            if (is_string($email)) {
174
                @list($firstName, $lastName) = explode(' ', $user);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
175
176
                // Resolve user (and set name)
177
                if ($element = $this->resolveUser($email)) {
178
                    $element->firstName = $firstName ?: $element->firstName;
179
                    $element->lastName = $lastName ?: $element->lastName;
180
                    return $element;
181
                }
182
183
                return new User([
184
                    'email' => $email,
185
                    'firstName' => $firstName,
186
                    'lastName' => $lastName
187
                ]);
188
            }
189
190
            // An array of [$user]
191
            if (!$element = $this->resolveUser($user)) {
192
                return new User([
193
                    'email' => $user
194
                ]);
195
            }
196
197
            return $element;
198
        }
199
200
        return null;
201
    }
202
}
203