Issues (56)

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.

code/SmartChimpSignupPage.php (4 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
class SmartChimpSignupPage extends Page
4
{
5
    public static $icon = "smartchimp/images/treeicons/SmartChimpSignupPage";
6
7
    public static $db = array(
8
        //	@todo:	provide optional dropdown for entering username/password??
9
        'MCApiKey' => 'Varchar(50)',    //	api_key
10
        'MCListKey' => 'Varchar(50)',    //	list_unique_id
11
        'MCSuccessContent'    => 'HTMLText',
12
        "DoubleOptin" => "Boolean",
13
        "SendWelcomeMail" => "Boolean",
14
        "SendGoodbey" => "Boolean",
15
        "SendDeleteNotification" => "Boolean",
16
        "IsDefaultList" => "Boolean",
17
        "FirstFieldRequired" => "Boolean",
18
        "LastFieldRequired" => "Boolean"
19
    );
20
21
    public static $has_many = array(
22
        "SmartChimpNewsletters" => "SmartChimpNewsletter"
23
    );
24
25
    public static $defaults = array(
26
        "DoubleOptin" => 1,
27
        "SendWelcomeMail" => 0,
28
        "SendGoodbey" => 0,
29
        "IsDefaultList" => 1
30
    );
31
32
    public static $mc_api_version = '1.2.1';
33
34
    protected static $api = null;
35
36
    public function getAPI()
37
    {
38
        if ($this->MCApiKey && $this->MCListKey) {
39
            if (!(self::$api instanceof MCAPI)) {
40
                require_once(Director::baseFolder().'/smartchimp/thirdparty/mcapi/'.self::$mc_api_version.'/MCAPI.class.php');
41
                self::$api = new MCAPI("$this->MCApiKey");
42
            }
43
            return self::$api;
44
        }
45
    }
46
47
48
    public function getCMSFields()
49
    {
50
        $fields = parent::getCMSFields();
51
52
        $fields->addFieldsToTab('Root.Content.SentNewsletters', array(
53
            new LiteralField('HowToRetrieve', '<p>To retrieve sent newsletters, simply save this page or <a href="'.$this->Link("update").'?flush=1">click here</a>.</p>'),
54
            $this->SmartChimpNewslettersTable()
55
        ));
56
        $fields->addFieldsToTab('Root.Content.MailChimpConfig', array(
57
            new CheckboxField('IsDefaultList', 'This is the default newsletter'),
58
            new TextField('MCApiKey', _t('SmartChimp.MCAPIKEY', 'API Key')),
59
            new TextField('MCListKey', _t('SmartChimp.MCLISTKEY', 'Unique ID for List')),
60
            new HTMLEditorField('MCSuccessContent', _t('SmartChimp.MCSuccessContent', 'Signup Success Content'))
61
        ));
62
        $fields->addFieldsToTab('Root.Content.Subscribe', array(
63
            new CheckboxField('DoubleOptin', "Double Opt-In Process (send email to confirm registration)"),
64
            new CheckboxField('SendWelcomeMail', "Send Welcome Mail")
65
        ));
66
        $fields->addFieldsToTab('Root.Content.Unsubscribe', array(
67
            new CheckboxField('SendGoodbey', "Send Goodbey Email"),
68
            new CheckboxField('SendDeleteNotification', "Send notification of unsubscribe")
69
        ));
70
        $fields->addFieldsToTab("Root.Content.RequiredFields", array(
71
            new CheckboxField('FirstRequired', "First name is required"),
72
            new CheckboxField('LastRequired', "Last name is required")
73
        ));
74
        $this->extend('updateSmartChimpCMSFields');
75
76
        return $fields;
77
    }
78
79
    public function SmartChimpNewslettersTable()
80
    {
81
        $table = new HasManyComplexTableField(
82
            $controller = $this,
83
            $name = "SmartChimpNewsletters",
84
            $sourceClass = "SmartChimpNewsletter",
85
            $fieldList = null,
86
            $detailFormFields = null,
87
            $sourceFilter = "ParentID = ".$this->ID
88
        );
89
        $table->setPageSize(100);
90
        $table->setPermissions(array('export', 'show', 'edit', 'delete'));
91
        return $table;
92
    }
93
94
    public function SmartChimpNewslettersShow()
95
    {
96
        return DataObject::get("SmartChimpNewsletter", "\"ParentID\" = ".$this->ID." AND \"Hide\" <> 1");
97
    }
98
99
    public function RetrieveCampaigns()
100
    {
101
        $dos = new DataObjectSet();
0 ignored issues
show
$dos 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...
102
        SmartChimpNewsletter::clean_up_characters();
103
        $api = $this->getAPI();
104
        if ($api && $this->ID) {
105
            $campaignArray = $api->campaigns(array("list_id" => $this->MCListKey));
106
            if (is_array($campaignArray) && count($campaignArray)) {
107
                foreach ($campaignArray as $key => $campaign) {
108
                    if ($campaign["status"] == "sent") {
109
                        $obj = DataObject::get_one("SmartChimpNewsletter", "`ParentID` = ".$this->ID." AND `CampaignID` = '".$campaign["id"]."'");
110
                        if ($obj) {
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
111
                            //do nothing
112
                        } else {
113
                            $content =  $api->campaignContent($campaign["id"]);
114
                            if ($content) {
115
                                $obj = new SmartChimpNewsletter();
116
                                $obj->ParentID = $this->ID;
117
                                $obj->Date = $campaign["send_time"];//
118
                                $obj->Title = $campaign["title"];//
119
                                $obj->Subject = $campaign["subject"];//
120
                                $obj->PermaLink = $campaign["archive_url"];//
121
                                $obj->CampaignID = $campaign["id"];//
122
                                $obj->WebID = $campaign["web_id"];//
123
                                $obj->Status = $campaign["status"];//should be sent!
124
                                //$obj->TextContent = $content["text"] ;//html
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
125
                                //$obj->HTMLContent = $content["html"];//text
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...
126
                            } else {
127
                                user_error(" could not retrieve content for newsletter with subject: ".$campaign["subject"]." AND ID".$campaign["id"], E_USER_NOTICE);
128
                            }
129
                            $obj->write();
130
                        }
131
                    }
132
                }
133
            }
134
        }
135
    }
136
137
138
    public function subscribe($email, $firstname, $lastname)
139
    {
140
        $api = $this->getAPI();
141
        if ($api) {
142
            $mergeVars = array(
143
                'FNAME'    => $firstname,
144
                'LNAME'    => $lastname
145
            );
146
            //NOTE: update existing is set to false to not accidentally resubscribe someone.
147
            if (true === $api->listSubscribe($id = $this->MCListKey, $email, $mergeVars, $email_type='html', $this->DoubleOptin, $update_existing=false, $replace_interests=true, $this->SendWelcomeMail)) {
148
                return true;
149
            } else {
150
                return $api->errorMessage;
151
            }
152
        }
153
    }
154
155
    public function unsubscribe($email)
156
    {
157
        $api = $this->getAPI();
158
        if ($api) {
159
            if (true === $api->listUnsubscribe($id = $this->MCListKey, $email, $delete_member=false, $this->SendGoodbey, $this->SendDeleteNotification)) {
160
                return true;
161
            } else {
162
                return $api->errorMessage;
163
            }
164
        }
165
    }
166
167
    public function onBeforeWrite()
168
    {
169
        parent::onBeforeWrite();
170
        if ($this->IsDefaultList && $this->ID) {
171
            $others = DataObject::get("SmartChimpSignupPage", "`SmartChimpSignupPage`.`ID` <> ".intval($this->ID)." AND `SmartChimpSignupPage`.`IsDefaultList` = 1");
172
            if ($others) {
173
                foreach ($others as $other) {
174
                    $other->IsDefaultList = 0;
175
                    $other->writeToStage('Stage');
176
                    $other->publish('Stage', 'Live');
177
                }
178
            }
179
        }
180
    }
181
182
    public function onAfterWrite()
183
    {
184
        parent::onAfterWrite();
185
        $this->RetrieveCampaigns();
186
    }
187
188
189
190
    public function requireDefaultRecords()
191
    {
192
        parent::requireDefaultRecords();
193
        $pages = DataObject::get("SmartChimpSignupPage");
194
        if ($pages) {
195
            if ($pages->count() == 1) {
196
                foreach ($pages as $page) {
197
                    if (!$page->IsDefaultList) {
198
                        $page->IsDefaultList = 1;
199
                        $page->writeToStage('Stage');
200
                        $page->publish('Stage', 'Live');
201
                        Database::alteration_message($page->ClassName.' created/updated: added IsDefaultList = true setting as there is only one SmartChimpSignupPage', 'edited');
202
                    }
203
                }
204
            }
205
        }
206
    }
207
}
208
209
210
class SmartChimpSignupPage_Controller extends Page_Controller
211
{
212
    public static $allowed_actions = array("Form", "update");
213
214
    public function init()
215
    {
216
        parent::init();
217
    }
218
219
220
    public function update()
221
    {
222
        $this->RetrieveCampaigns();
223
        return array();
224
    }
225
226
    public function Form()
227
    {
228
        if (Session::get('SmartChimp.SUCCESS')) {
229
            Session::clear('SmartChimp.SUCCESS');
230
            return false;
231
        }
232
        $requiredFields = new RequiredFields('email');
233
        if ($this->FirstFieldRequired) {
234
            $requiredFields->appendRequiredFields(array('fname'));
235
        }
236
        if ($this->LastFieldRequired) {
237
            $requiredFields->appendRequiredFields(array('lname'));
238
        }
239
        $form = new Form($this, 'Form',
240
            new FieldSet(
241
                new TextField('fname', 'First name'),
242
                new TextField('lname', 'Last name'),
243
                new TextField('email', 'Email address')
244
            ),
245
            new FieldSet(
246
                new FormAction('SignupAction', 'Sign up')
247
            ),
248
            $requiredFields
249
        );
250
        $this->extend('updateSmartChimpForm', $form);
251
        return $form;
252
    }
253
254
    public function ShortForm()
255
    {
256
        if (Session::get('SmartChimp.SUCCESS')) {
257
            Session::clear('SmartChimp.SUCCESS');
258
            return false;
259
        }
260
        $form = new Form($this, 'Form',
261
            new FieldSet(
262
                new TextField('email', 'Email Address')
263
            ),
264
            new FieldSet(
265
                new FormAction('SignupAction', 'Sign up')
266
            ),
267
            new RequiredFields('email')
268
        );
269
270
        $this->extend('updateSmartChimpForm', $form);
271
272
        return $form;
273
    }
274
275
    public function mcsuccess()
276
    {
277
        if (Session::get('SmartChimp.SUCCESS')) {
278
            $this->Content = $this->MCSuccessContent;
279
        }
280
        return array();
281
    }
282
283
    public function SignupAction($raw_data, $form)
284
    {
285
        $data = Convert::raw2sql($raw_data);
286
        $outcome = $this->subscribe($data['email'], $data['fname'],  $data['lname']);
287
        if (true === $outcome) {
288
            Session::set('SmartChimp.SUCCESS', true);
289
            return $this->mcsuccess();
290
        } else {
291
            $form->sessionMessage($outcome, 'warning');
292
            Director::redirectBack();
293
        }
294
    }
295
}
296