Issues (29)

Security Analysis    no request data  

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/Newsletter.php (8 issues)

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
namespace Spatie\Newsletter;
4
5
use DrewM\MailChimp\MailChimp;
6
7
class Newsletter
8
{
9
    /** @var \DrewM\MailChimp\MailChimp */
10
    protected $mailChimp;
11
12
    /** @var \Spatie\Newsletter\NewsletterListCollection */
13
    protected $lists;
14
15
    public function __construct(MailChimp $mailChimp, NewsletterListCollection $lists)
16
    {
17
        $this->mailChimp = $mailChimp;
18
19
        $this->lists = $lists;
20
    }
21
22 View Code Duplication
    public function subscribe(string $email, array $mergeFields = [], string $listName = '', array $options = [])
0 ignored issues
show
This method seems to be duplicated in 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...
23
    {
24
        $list = $this->lists->findByName($listName);
25
26
        $options = $this->getSubscriptionOptions($email, $mergeFields, $options);
27
28
        $response = $this->mailChimp->post("lists/{$list->getId()}/members", $options);
29
30
        if (! $this->lastActionSucceeded()) {
31
            return false;
32
        }
33
34
        return $response;
35
    }
36
37
    public function subscribePending(string $email, array $mergeFields = [], string $listName = '', array $options = [])
38
    {
39
        $options = array_merge($options, ['status' => 'pending']);
40
41
        return $this->subscribe($email, $mergeFields, $listName, $options);
42
    }
43
44 View Code Duplication
    public function subscribeOrUpdate(string $email, array $mergeFields = [], string $listName = '', array $options = [])
0 ignored issues
show
This method seems to be duplicated in 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...
45
    {
46
        $list = $this->lists->findByName($listName);
47
48
        $options = $this->getSubscriptionOptions($email, $mergeFields, $options);
49
50
        $response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", $options);
51
52
        if (! $this->lastActionSucceeded()) {
53
            return false;
54
        }
55
56
        return $response;
57
    }
58
59
    public function getMembers(string $listName = '', array $parameters = [])
60
    {
61
        $list = $this->lists->findByName($listName);
62
63
        return $this->mailChimp->get("lists/{$list->getId()}/members", $parameters);
64
    }
65
66
    public function getMember(string $email, string $listName = '')
67
    {
68
        $list = $this->lists->findByName($listName);
69
70
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
71
    }
72
73
    public function getMemberActivity(string $email, string $listName = '')
74
    {
75
        $list = $this->lists->findByName($listName);
76
77
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/activity");
78
    }
79
80
    public function hasMember(string $email, string $listName = ''): bool
81
    {
82
        $response = $this->getMember($email, $listName);
83
84
        if (! isset($response['email_address'])) {
85
            return false;
86
        }
87
88
        if (strtolower($response['email_address']) != strtolower($email)) {
89
            return false;
90
        }
91
92
        return true;
93
    }
94
95
    public function isSubscribed(string $email, string $listName = ''): bool
96
    {
97
        $response = $this->getMember($email, $listName);
98
99
        if (! isset($response)) {
100
            return false;
101
        }
102
103
        if ($response['status'] != 'subscribed') {
104
            return false;
105
        }
106
107
        return true;
108
    }
109
110 View Code Duplication
    public function unsubscribe(string $email, string $listName = '')
0 ignored issues
show
This method seems to be duplicated in 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...
111
    {
112
        $list = $this->lists->findByName($listName);
113
114
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [
115
            'status' => 'unsubscribed',
116
        ]);
117
118
        if (! $this->lastActionSucceeded()) {
119
            return false;
120
        }
121
122
        return $response;
123
    }
124
125 View Code Duplication
    public function updateEmailAddress(string $currentEmailAddress, string $newEmailAddress, string $listName = '')
0 ignored issues
show
This method seems to be duplicated in 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...
126
    {
127
        $list = $this->lists->findByName($listName);
128
129
        $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}", [
130
            'email_address' => $newEmailAddress,
131
        ]);
132
133
        return $response;
134
    }
135
136 View Code Duplication
    public function delete(string $email, string $listName = '')
0 ignored issues
show
This method seems to be duplicated in 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...
137
    {
138
        $list = $this->lists->findByName($listName);
139
140
        $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}");
141
142
        return $response;
143
    }
144
145 View Code Duplication
    public function deletePermanently(string $email, string $listName = '')
0 ignored issues
show
This method seems to be duplicated in 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...
146
    {
147
        $list = $this->lists->findByName($listName);
148
149
        $response = $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/actions/delete-permanent");
150
151
        return $response;
152
    }
153
154
    public function getTags(string $email, string $listName = '')
155
    {
156
        $list = $this->lists->findByName($listName);
157
158
        return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags");
159
    }
160
161 View Code Duplication
    public function addTags(array $tags, string $email, string $listName = '')
0 ignored issues
show
This method seems to be duplicated in 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...
162
    {
163
        $list = $this->lists->findByName($listName);
164
165
        $payload = collect($tags)->map(function ($tag) {
166
            return ['name' => $tag, 'status' => 'active'];
167
        })->toArray();
168
169
        return $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags", [
170
            'tags' => $payload,
171
        ]);
172
    }
173
174 View Code Duplication
    public function removeTags(array $tags, string $email, string $listName = '')
0 ignored issues
show
This method seems to be duplicated in 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...
175
    {
176
        $list = $this->lists->findByName($listName);
177
178
        $payload = collect($tags)->map(function ($tag) {
179
            return ['name' => $tag, 'status' => 'inactive'];
180
        })->toArray();
181
182
        return $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags", [
183
            'tags' => $payload,
184
        ]);
185
    }
186
187
    public function createCampaign(
188
        string $fromName,
189
        string $replyTo,
190
        string $subject,
191
        string $html = '',
192
        string $listName = '',
193
        array $options = [],
194
        array $contentOptions = [])
195
    {
196
        $list = $this->lists->findByName($listName);
197
198
        $defaultOptions = [
199
            'type' => 'regular',
200
            'recipients' => [
201
                'list_id' => $list->getId(),
202
            ],
203
            'settings' => [
204
                'subject_line' => $subject,
205
                'from_name' => $fromName,
206
                'reply_to' => $replyTo,
207
            ],
208
        ];
209
210
        $options = array_merge($defaultOptions, $options);
211
212
        $response = $this->mailChimp->post('campaigns', $options);
213
214
        if (! $this->lastActionSucceeded()) {
215
            return false;
216
        }
217
218
        if ($html === '') {
219
            return $response;
220
        }
221
222
        if (! $this->updateContent($response['id'], $html, $contentOptions)) {
223
            return false;
224
        }
225
226
        return $response;
227
    }
228
229
    public function updateContent(string $campaignId, string $html, array $options = [])
230
    {
231
        $defaultOptions = compact('html');
232
233
        $options = array_merge($defaultOptions, $options);
234
235
        $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options);
236
237
        if (! $this->lastActionSucceeded()) {
238
            return false;
239
        }
240
241
        return $response;
242
    }
243
244
    public function getApi(): MailChimp
245
    {
246
        return $this->mailChimp;
247
    }
248
249
    /**
250
     * @return string|false
251
     */
252
    public function getLastError()
253
    {
254
        return $this->mailChimp->getLastError();
255
    }
256
257
    public function lastActionSucceeded(): bool
258
    {
259
        return $this->mailChimp->success();
260
    }
261
262
    protected function getSubscriberHash(string $email): string
263
    {
264
        return $this->mailChimp->subscriberHash($email);
265
    }
266
267
    protected function getSubscriptionOptions(string $email, array $mergeFields, array $options): array
268
    {
269
        $defaultOptions = [
270
            'email_address' => $email,
271
            'status' => 'subscribed',
272
            'email_type' => 'html',
273
        ];
274
275
        if (count($mergeFields)) {
276
            $defaultOptions['merge_fields'] = $mergeFields;
277
        }
278
279
        $options = array_merge($defaultOptions, $options);
280
281
        return $options;
282
    }
283
}
284