public function do_set()
 {
     $rules = ['sitename' => 'required|max:255', 'sender_name' => 'required|max:510', 'sender_email' => 'required|email', 'reply_to_email' => 'required|email'];
     $validation = Jamesy\MyValidations::validate(Input::all(), $rules);
     if ($validation != NULL) {
         return Response::json(['validation' => $validation]);
     } else {
         $setting = Setting::getSiteSettings();
         $setting->sitename = Input::get('sitename');
         $setting->save();
         $user = Sentry::getUser();
         Setting::setFromName($user, Input::get('sender_name'));
         Setting::setFromEmail($user, Input::get('sender_email'));
         Setting::setReplyToEmail($user, Input::get('reply_to_email'));
         return Response::json(['success' => 'Settings successfully saved.']);
     }
 }
 public function update($id)
 {
     $addressbook = Addressbook::find($id);
     $inputs = [];
     foreach (Input::all() as $key => $input) {
         $inputs[$key] = Jamesy\Sanitiser::trimInput($input);
     }
     if ($inputs['name'] == $addressbook->name) {
         $validation = NULL;
     } else {
         $validation = Jamesy\MyValidations::validate($inputs, $this->rules);
     }
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $addressbook->name = $inputs['name'];
         $addressbook->active = $inputs['active'];
         $addressbook->save();
         if ($addressbook->active == 0) {
             $subscribers = Subscriber::with(['addressbooks' => function ($query) use($id) {
                 $query->where('addressbook_id', $id);
             }])->get();
             foreach ($subscribers as $subscriber) {
                 $subscriber->active = 0;
                 $subscriber->save();
             }
         }
         return Redirect::to('dashboard/lists')->withSuccess($addressbook->active == 0 ? 'List updated. NOTE: All subscribers in the list are now inactive.' : 'List updated.');
     }
 }
 public function update($id)
 {
     $maillist = Maillist::getMaillistWithSubs($id);
     $name = Jamesy\Sanitiser::trimInput(Input::get('name'));
     if ($name == $maillist->name) {
         $validation = NULL;
     } else {
         $validation = Jamesy\MyValidations::validate(['name' => $name], $this->rules);
     }
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $maillist->name = $name;
         $maillist->active = Input::get('active');
         $maillist->save();
         if ($maillist->active == 0) {
             $subscribers = $maillist->subscribers;
             foreach ($subscribers as $subscriber) {
                 $subscriber->active = 0;
                 $subscriber->save();
             }
         }
         if (Input::file('file')) {
             $fileValidation = Jamesy\MyValidations::validate(['file' => Input::file('file')], $this->fileSizeRules);
             if ($fileValidation != NULL) {
                 return Redirect::back()->withErrors($fileValidation)->withInput();
             } else {
                 $input = Input::file('file');
                 $ext = pathinfo($input->getClientOriginalName(), PATHINFO_EXTENSION);
                 if ($ext != 'xlt' && $ext != 'xls' && $ext != 'csv') {
                     return Redirect::back()->withIssues('You attempted the import with an invalid file. File must be Excel or CSV')->withInput();
                 } else {
                     $import = new Jamesy\Imports($input, $this->subscriberRules);
                     $importResult = $import->getInsertArray();
                     if (is_array($importResult)) {
                         $totalNum = $importResult[0];
                         $duplicatesNum = $importResult[1];
                         $passedArr = $importResult[2];
                         $passedNum = count($passedArr);
                         $failedNum = (int) $totalNum - (int) $duplicatesNum - (int) $passedNum;
                         $timestamp = $importResult[3];
                         $existingEmails = $importResult[4];
                         $newPassedArr = [];
                         if ($maillist->active == 0) {
                             foreach ($passedArr as $subscriber) {
                                 $subscriber['active'] = 0;
                                 $newPassedArr[] = $subscriber;
                             }
                         } else {
                             $newPassedArr = $passedArr;
                         }
                         if (count($newPassedArr)) {
                             Subscriber::insert($newPassedArr);
                             $generalList = Maillist::find(1);
                             $generalList->touch();
                             $maillist->touch();
                             $subscribers = Subscriber::where('created_at', $timestamp)->where('updated_at', $timestamp)->get();
                             foreach ($subscribers as $key => $subscriber) {
                                 $generalList->subscribers()->attach($subscriber->id);
                                 $maillist->subscribers()->attach($subscriber->id);
                             }
                         }
                         if (count($existingEmails)) {
                             $alreadySubs = Subscriber::with('maillists')->whereIn('email', $existingEmails)->get();
                             if (count($alreadySubs)) {
                                 foreach ($alreadySubs as $subscriber) {
                                     if (!$subscriber->maillists->contains($maillist->id)) {
                                         $maillist->subscribers()->attach($subscriber->id);
                                     }
                                     if ($maillist->active == 0) {
                                         $subscriber->active = 0;
                                         $subscriber->save();
                                     }
                                 }
                             }
                         }
                         $message = "List updated.";
                         $message .= "<br /><b>{$totalNum}</b> " . str_plural('row', $totalNum) . " found in excel file.";
                         if ($duplicatesNum) {
                             $message .= "<br /><b>{$duplicatesNum}</b> had duplicate email addresses.";
                         }
                         if ($passedNum == 1) {
                             $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and was stored.";
                         } else {
                             $message .= "<br /><b>{$passedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and were stored.";
                         }
                         if ($failedNum == 1) {
                             $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and was NOT stored.";
                         } elseif ($failedNum > 1) {
                             $message .= "<br /><b>{$failedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and were NOT stored.";
                         }
                         return Redirect::to('dashboard/lists')->withSuccess($message);
                     } else {
                         return Redirect::back()->withIssues($importResult);
                     }
                 }
             }
         } else {
             return Redirect::to('dashboard/lists')->withSuccess($maillist->active == 0 ? 'List updated. NOTE: All subscribers in the list are now inactive.' : 'List updated.');
         }
     }
 }
 public function update($id)
 {
     $tag = Tag::find($id);
     $inputs = [];
     foreach (Input::all() as $key => $input) {
         $inputs[$key] = Jamesy\Sanitiser::trimInput($input);
     }
     if ($inputs['name'] == $tag->name) {
         $validation = NULL;
     } else {
         $validation = Jamesy\MyValidations::validate($inputs, $this->rules);
     }
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $tag->name = $inputs['name'];
         $tag->save();
         return Redirect::to('dashboard/tags')->withSuccess('Tag updated.');
     }
 }
 public function update_password()
 {
     $theUser = User::find(Input::get('id'));
     if (!Hash::check(Input::get('existing_password'), $this->user->password)) {
         return Redirect::back()->withExistingPassError('Wrong password.');
     }
     $validation = Jamesy\MyValidations::validate(Input::all(), User::$passwordRules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation);
     } else {
         $theUser->password = Hash::make(Input::get('new_password'));
         $theUser->save();
         return Redirect::back()->withSuccess('Password updated.');
     }
 }
 public function update($id)
 {
     $inputs['title'] = Jamesy\Sanitiser::trimInput(Input::get('title'));
     $inputs['content'] = Jamesy\Sanitiser::trimInput(Input::get('content'));
     $validation = Jamesy\MyValidations::validate($inputs, $this->rules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         $anchors = [];
         $anchor_string = '';
         if (Input::has('anchors')) {
             $anchors = array_unique(explode(",", Str::lower(Input::get('anchors'))));
             $anchors = array_filter($anchors, function ($i) {
                 return $i != 'type in your anchors separated by a space or comma';
             });
             foreach ($anchors as $key => $value) {
                 $anchor_string .= $value;
                 $anchor_string .= $key == count($anchors) - 1 ? '' : ',';
             }
         }
         $page = Page::find($id);
         $slug = $page->slug;
         if (Str::slug($inputs['title']) != $slug) {
             $existingSlugs = Page::lists('slug');
             $slug = Jamesy\MyValidations::makeSlug($existingSlugs, Str::slug($inputs['title']));
         }
         $page->user_id = $this->user->id;
         $page->title = $inputs['title'];
         $page->slug = $slug;
         $page->content = $inputs['content'];
         $page->anchors = Str::length($anchor_string) ? $anchor_string : NULL;
         $page->save();
         return Redirect::to('dashboard/pages')->withSuccess('Page updated');
     }
 }
 public function import()
 {
     $validation = Jamesy\MyValidations::validate(Input::all(), $this->fileRules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation);
     } else {
         $input = Input::file('file');
         $ext = pathinfo($input->getClientOriginalName(), PATHINFO_EXTENSION);
         if ($ext != 'xlt' && $ext != 'xls' && $ext != 'csv') {
             return Redirect::back()->withIssues('You attempted the import with an invalid file. File must be Excel or CSV');
         } else {
             $import = new Jamesy\Imports($input, $this->rules);
             $importResult = $import->getInsertArray();
             if (is_array($importResult)) {
                 $totalNum = $importResult[0];
                 $duplicatesNum = $importResult[1];
                 $passedArr = $importResult[2];
                 $passedNum = count($passedArr);
                 $failedNum = (int) $totalNum - (int) $duplicatesNum - (int) $passedNum;
                 $timestamp = $importResult[3];
                 if (count($passedArr)) {
                     Subscriber::insert($passedArr);
                     $list = Maillist::find(1);
                     $list->touch();
                     $subscribers = Subscriber::where('created_at', $timestamp)->where('updated_at', $timestamp)->get();
                     foreach ($subscribers as $key => $subscriber) {
                         $list->subscribers()->attach($subscriber->id);
                     }
                 }
                 $message = "<b>{$totalNum}</b> " . str_plural('row', $totalNum) . " found in excel file.";
                 if ($duplicatesNum) {
                     $message .= "<br /><b>{$duplicatesNum}</b> had duplicate email addresses.";
                 }
                 if ($passedNum == 1) {
                     $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and was stored.";
                 } else {
                     $message .= "<br /><b>{$passedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and were stored.";
                 }
                 if ($failedNum == 1) {
                     $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and was NOT stored.";
                 } elseif ($failedNum > 1) {
                     $message .= "<br /><b>{$failedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and were NOT stored.";
                 }
                 return Redirect::back()->withSuccess($message);
             } else {
                 return Redirect::back()->withIssues($importResult);
             }
         }
     }
 }
 public function send()
 {
     $inputs = [];
     foreach (Input::all() as $key => $input) {
         $inputs[$key] = Jamesy\Sanitiser::trimInput($input);
     }
     $rules = Input::has('save_draft') ? $this->draftRules : $this->rules;
     $validation = Jamesy\MyValidations::validate($inputs, $rules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         if (Input::has('save_draft')) {
             $draft = Input::has('was_draft') ? Draft::find(Input::get('was_draft')) : new Draft();
             $draft->user_id = $this->user->id;
             $draft->tag_id = $inputs['tag_id'];
             $draft->subject = $inputs['subject'];
             $draft->email_body = $inputs['email_body'];
             $draft->save();
             return Redirect::to('dashboard/emails/drafts');
         } else {
             $maxSendNum = 999;
             $email = new Email();
             $email->user_id = $this->user->id;
             $email->tag_id = $inputs['tag_id'];
             $email->from = Setting::getFromName($this->user) . " (" . Setting::getFromEmail($this->user) . ")";
             $email->reply_to = Setting::getFromName($this->user) . " (" . Setting::getReplyToEmail($this->user) . ")";
             $email->subject = $inputs['subject'];
             $email->email_body = $inputs['email_body'];
             $email->save();
             $maillists = Input::has('mail_lists') ? $inputs['mail_lists'] : [];
             $emails = Input::has('subscribers') ? $inputs['subscribers'] : [];
             $selectedSubs = Subscriber::getSelectedSubscribersForEmail($maillists, $emails);
             while (count($selectedSubs)) {
                 $data = ['email_body' => $inputs['email_body'], 'email_id' => \Jamesy\Miscellaneous::encryptId($email->id)];
                 $variables = [];
                 $tag = Tag::find($inputs['tag_id']);
                 $emailId = $email->id;
                 $numToSlice = count($selectedSubs) > $maxSendNum ? $maxSendNum : count($selectedSubs);
                 $subscribers = $numToSlice < count($selectedSubs) ? array_slice($selectedSubs, 0, $numToSlice) : $selectedSubs;
                 foreach ($subscribers as $subscriber) {
                     $variables[$subscriber['email']] = ['id' => $subscriber['id'], 'first_name' => $subscriber['first_name'], 'last_name' => $subscriber['last_name']];
                 }
                 $result = Mailgun::send('backend.emails.send_templates.main', $data, function ($message) use($subscribers, $inputs, $variables, $emailId, $tag) {
                     $message->from(Setting::getFromEmail($this->user), Setting::getFromName($this->user))->replyTo(Setting::getReplyToEmail($this->user), Setting::getFromName($this->user))->subject($inputs['subject']);
                     foreach ($subscribers as $subscriber) {
                         $message->to($subscriber['email'], $subscriber['first_name'] . ' ' . $subscriber['last_name']);
                     }
                     $message->recipientVariables($variables);
                     $message->tag($tag->name);
                 });
                 if (is_object($result)) {
                     if ($result->http_response_code == 200) {
                         foreach ($subscribers as $subscriber) {
                             $apicall_id = explode('<', explode('@', $result->http_response_body->id)[0])[1];
                             $analytic = new Analytic();
                             $analytic->subscriber_id = $subscriber['id'];
                             $analytic->email_id = $email->id;
                             $analytic->recipient = $subscriber['email'];
                             $analytic->apicall_id = $apicall_id;
                             $analytic->status = 'queued';
                             $analytic->save();
                         }
                     }
                 }
                 $selectedSubs = array_slice((array) $selectedSubs, $numToSlice);
             }
             if (Input::has('was_draft')) {
                 Draft::find(Input::get('was_draft'))->delete();
             }
             return Redirect::to('dashboard/emails/sent');
         }
     }
 }