/** * Send forgot password email. * * @param $Email * @return bool * @throws Exception */ public function passwordRequest($Email) { if (!$Email) { return false; } $Users = $this->getWhere(array('Email' => $Email))->resultObject(); if (count($Users) == 0) { // Check for the username. $Users = $this->getWhere(array('Name' => $Email))->resultObject(); } $this->EventArguments['Users'] =& $Users; $this->EventArguments['Email'] = $Email; $this->fireEvent('BeforePasswordRequest'); if (count($Users) == 0) { $this->Validation->addValidationResult('Name', "Couldn't find an account associated with that email/username."); return false; } $NoEmail = true; foreach ($Users as $User) { if (!$User->Email) { continue; } $Email = new Gdn_Email(); // Instantiate in loop to clear previous settings $PasswordResetKey = BetterRandomString(20, 'Aa0'); $PasswordResetExpires = strtotime('+1 hour'); $this->saveAttribute($User->UserID, 'PasswordResetKey', $PasswordResetKey); $this->saveAttribute($User->UserID, 'PasswordResetExpires', $PasswordResetExpires); $AppTitle = c('Garden.Title'); $Email->subject(sprintf(t('[%s] Password Reset Request'), $AppTitle)); $Email->to($User->Email); $Email->message(sprintf(t('PasswordRequest'), $User->Name, $AppTitle, ExternalUrl('/entry/passwordreset/' . $User->UserID . '/' . $PasswordResetKey))); $Email->send(); $NoEmail = false; } if ($NoEmail) { $this->Validation->addValidationResult('Name', 'There is no email address associated with that account.'); return false; } return true; }
/** * Send forgot password email. * * @param string $Email * @return bool */ public function passwordRequest($Email) { if (!$Email) { return false; } $Users = $this->getWhere(['Email' => $Email])->resultObject(); if (count($Users) == 0) { // Check for the username. $Users = $this->getWhere(['Name' => $Email])->resultObject(); } $this->EventArguments['Users'] =& $Users; $this->EventArguments['Email'] = $Email; $this->fireEvent('BeforePasswordRequest'); if (count($Users) == 0) { $this->Validation->addValidationResult('Name', "Couldn't find an account associated with that email/username."); return false; } $NoEmail = true; foreach ($Users as $User) { if (!$User->Email) { continue; } $Email = new Gdn_Email(); // Instantiate in loop to clear previous settings $PasswordResetKey = betterRandomString(20, 'Aa0'); $PasswordResetExpires = strtotime('+1 hour'); $this->saveAttribute($User->UserID, 'PasswordResetKey', $PasswordResetKey); $this->saveAttribute($User->UserID, 'PasswordResetExpires', $PasswordResetExpires); $AppTitle = c('Garden.Title'); $Email->subject('[' . $AppTitle . '] ' . t('Reset Your Password')); $Email->to($User->Email); $emailTemplate = $Email->getEmailTemplate()->setTitle(t('Reset Your Password'))->setMessage(sprintf(t('We\'ve received a request to change your password.'), $AppTitle))->setButton(externalUrl('/entry/passwordreset/' . $User->UserID . '/' . $PasswordResetKey), t('Change My Password')); $Email->setEmailTemplate($emailTemplate); try { $Email->send(); } catch (Exception $e) { if (debug()) { throw $e; } } $NoEmail = false; } if ($NoEmail) { $this->Validation->addValidationResult('Name', 'There is no email address associated with that account.'); return false; } return true; }
/** * Prompts new admins how to get started using new install. * * @since 2.0.0 * @access public */ public function gettingStarted() { $this->permission('Garden.Settings.Manage'); $this->setData('Title', t('Getting Started')); $this->addSideMenu('dashboard/settings/gettingstarted'); $this->TextEnterEmails = t('TextEnterEmails', 'Type email addresses separated by commas here'); if ($this->Form->authenticatedPostBack()) { // Do invitations to new members. $Message = $this->Form->getFormValue('InvitationMessage'); $Message .= "\n\n" . Gdn::request()->Url('/', true); $Message = trim($Message); $Recipients = $this->Form->getFormValue('Recipients'); if ($Recipients == $this->TextEnterEmails) { $Recipients = ''; } $Recipients = explode(',', $Recipients); $CountRecipients = 0; foreach ($Recipients as $Recipient) { if (trim($Recipient) != '') { $CountRecipients++; if (!validateEmail($Recipient)) { $this->Form->addError(sprintf(t('%s is not a valid email address'), $Recipient)); } } } if ($CountRecipients == 0) { $this->Form->addError(t('You must provide at least one recipient')); } if ($this->Form->errorCount() == 0) { $Email = new Gdn_Email(); $Email->subject(t('Check out my new community!')); $Email->message($Message); foreach ($Recipients as $Recipient) { if (trim($Recipient) != '') { $Email->to($Recipient); try { $Email->send(); } catch (Exception $ex) { $this->Form->addError($ex); } } } } if ($this->Form->errorCount() == 0) { $this->informMessage(t('Your invitations were sent successfully.')); } } $this->render(); }
/** * * * @param $InvitationID * @throws Exception */ public function send($InvitationID) { $Invitation = $this->GetByInvitationID($InvitationID); $Session = Gdn::session(); if ($Invitation === false) { throw new Exception(t('ErrorRecordNotFound')); } elseif ($Session->UserID != $Invitation->SenderUserID) { throw new Exception(t('InviteErrorPermission', t('ErrorPermission'))); } else { // Some information for the email $RegistrationUrl = ExternalUrl("entry/registerinvitation/{$Invitation->Code}"); $AppTitle = Gdn::config('Garden.Title'); $Email = new Gdn_Email(); $Email->subject(sprintf(t('[%s] Invitation'), $AppTitle)); $Email->to($Invitation->Email); $Email->message(sprintf(t('EmailInvitation'), $Invitation->SenderName, $AppTitle, $RegistrationUrl)); $Email->send(); } }
/** * * * @param $InvitationID * @throws Exception */ public function send($InvitationID) { $Invitation = $this->GetByInvitationID($InvitationID); $Session = Gdn::session(); if ($Invitation === false) { throw new Exception(t('ErrorRecordNotFound')); } elseif ($Session->UserID != $Invitation->SenderUserID) { throw new Exception(t('InviteErrorPermission', t('ErrorPermission'))); } else { // Some information for the email $RegistrationUrl = ExternalUrl("entry/registerinvitation/{$Invitation->Code}"); $AppTitle = Gdn::config('Garden.Title'); $Email = new Gdn_Email(); $Email->subject(sprintf(t('[%s] Invitation'), $AppTitle)); $Email->to($Invitation->Email); $emailTemplate = $Email->getEmailTemplate(); $message = t('Hello!') . ' ' . sprintf(t('%s has invited you to join %s.'), $Invitation->SenderName, $AppTitle); $emailTemplate->setButton($RegistrationUrl, t('Join this Community Now'))->setMessage($message)->setTitle(sprintf(t('Join %s'), $AppTitle)); $Email->setEmailTemplate($emailTemplate); try { $Email->send(); } catch (Exception $e) { if (debug()) { throw $e; } } } }
/** * Handle flagging process in a discussion. */ public function discussionController_flag_create($Sender) { // Signed in users only. if (!($UserID = Gdn::session()->UserID)) { return; } $UserName = Gdn::session()->User->Name; $Arguments = $Sender->RequestArgs; if (sizeof($Arguments) != 5) { return; } list($Context, $ElementID, $ElementAuthorID, $ElementAuthor, $EncodedURL) = $Arguments; $URL = htmlspecialchars(base64_decode(str_replace('-', '=', $EncodedURL))); $Sender->setData('Plugin.Flagging.Data', array('Context' => $Context, 'ElementID' => $ElementID, 'ElementAuthorID' => $ElementAuthorID, 'ElementAuthor' => $ElementAuthor, 'URL' => $URL, 'UserID' => $UserID, 'UserName' => $UserName)); if ($Sender->Form->authenticatedPostBack()) { $SQL = Gdn::sql(); $Comment = $Sender->Form->getValue('Plugin.Flagging.Reason'); $Sender->setData('Plugin.Flagging.Reason', $Comment); $CreateDiscussion = c('Plugins.Flagging.UseDiscussions'); if ($CreateDiscussion) { // Category $CategoryID = c('Plugins.Flagging.CategoryID'); // New discussion name if ($Context == 'comment') { $Result = $SQL->select('d.Name')->select('c.Body')->from('Comment c')->join('Discussion d', 'd.DiscussionID = c.DiscussionID', 'left')->where('c.CommentID', $ElementID)->get()->firstRow(); } elseif ($Context == 'discussion') { $DiscussionModel = new DiscussionModel(); $Result = $DiscussionModel->getID($ElementID); } $DiscussionName = val('Name', $Result); $PrefixedDiscussionName = t('FlagPrefix', 'FLAG: ') . $DiscussionName; // Prep data for the template $Sender->setData('Plugin.Flagging.Report', array('DiscussionName' => $DiscussionName, 'FlaggedContent' => val('Body', $Result))); // Assume no discussion exists $this->DiscussionID = null; // Get discussion ID if already flagged $FlagResult = Gdn::sql()->select('DiscussionID')->from('Flag fl')->where('ForeignType', $Context)->where('ForeignID', $ElementID)->get()->firstRow(); if ($FlagResult) { // New comment in existing discussion $DiscussionID = $FlagResult->DiscussionID; $ReportBody = $Sender->fetchView($this->getView('reportcomment.php')); $SQL->insert('Comment', array('DiscussionID' => $DiscussionID, 'InsertUserID' => $UserID, 'Body' => $ReportBody, 'Format' => 'Html', 'DateInserted' => date('Y-m-d H:i:s'))); $CommentModel = new CommentModel(); $CommentModel->updateCommentCount($DiscussionID); } else { // New discussion body $ReportBody = $Sender->fetchView($this->getView('report.php')); $DiscussionID = $SQL->insert('Discussion', array('InsertUserID' => $UserID, 'UpdateUserID' => $UserID, 'CategoryID' => $CategoryID, 'Name' => $PrefixedDiscussionName, 'Body' => $ReportBody, 'Format' => 'Html', 'CountComments' => 1, 'DateInserted' => date('Y-m-d H:i:s'), 'DateUpdated' => date('Y-m-d H:i:s'), 'DateLastComment' => date('Y-m-d H:i:s'))); // Update discussion count $DiscussionModel = new DiscussionModel(); $DiscussionModel->updateDiscussionCount($CategoryID); } } try { // Insert the flag $SQL->insert('Flag', array('DiscussionID' => $DiscussionID, 'InsertUserID' => $UserID, 'InsertName' => $UserName, 'AuthorID' => $ElementAuthorID, 'AuthorName' => $ElementAuthor, 'ForeignURL' => $URL, 'ForeignID' => $ElementID, 'ForeignType' => $Context, 'Comment' => $Comment, 'DateInserted' => date('Y-m-d H:i:s'))); } catch (Exception $e) { } // Notify users with permission who've chosen to be notified if (!$FlagResult) { // Only send if this is first time it's being flagged. $Sender->setData('Plugin.Flagging.DiscussionID', $DiscussionID); $Subject = isset($PrefixedDiscussionName) ? $PrefixedDiscussionName : t('FlagDiscussion', 'A discussion was flagged'); $EmailBody = $Sender->fetchView($this->getView('reportemail.php')); $NotifyUsers = c('Plugins.Flagging.NotifyUsers', array()); // Send emails $UserModel = new UserModel(); foreach ($NotifyUsers as $UserID) { $User = $UserModel->getID($UserID); $Email = new Gdn_Email(); $Email->to($User->Email)->subject(sprintf(t('[%1$s] %2$s'), Gdn::config('Garden.Title'), $Subject))->message($EmailBody); try { $Email->send(); } catch (Exception $e) { if (debug()) { throw $e; } } } } $Sender->informMessage(t('FlagSent', "Your complaint has been registered.")); } $Sender->render($this->getView('flag.php')); }
/** * * * @param $Activity * @param bool $NoDelete * @return bool * @throws Exception */ public function email(&$Activity, $NoDelete = false) { if (is_numeric($Activity)) { $ActivityID = $Activity; $Activity = $this->getID($ActivityID); } else { $ActivityID = val('ActivityID', $Activity); } if (!$Activity) { return false; } $Activity = (array) $Activity; $User = Gdn::userModel()->getID($Activity['NotifyUserID'], DATASET_TYPE_ARRAY); if (!$User) { return false; } // Format the activity headline based on the user being emailed. if (val('HeadlineFormat', $Activity)) { $SessionUserID = Gdn::session()->UserID; Gdn::session()->UserID = $User['UserID']; $Activity['Headline'] = formatString($Activity['HeadlineFormat'], $Activity); Gdn::session()->UserID = $SessionUserID; } else { if (!isset($Activity['ActivityGender'])) { $AT = self::getActivityType($Activity['ActivityType']); $Data = array($Activity); self::joinUsers($Data); $Activity = $Data[0]; $Activity['RouteCode'] = val('RouteCode', $AT); $Activity['FullHeadline'] = val('FullHeadline', $AT); $Activity['ProfileHeadline'] = val('ProfileHeadline', $AT); } $Activity['Headline'] = Gdn_Format::activityHeadline($Activity, '', $User['UserID']); } // Build the email to send. $Email = new Gdn_Email(); $Email->subject(sprintf(t('[%1$s] %2$s'), c('Garden.Title'), Gdn_Format::plainText($Activity['Headline']))); $Email->to($User); $url = externalUrl(val('Route', $Activity) == '' ? '/' : val('Route', $Activity)); $emailTemplate = $Email->getEmailTemplate()->setButton($url, t('Check it out'))->setTitle(Gdn_Format::plainText(val('Headline', $Activity))); if ($message = val('Story', $Activity)) { $emailTemplate->setMessage($message, true); } $Email->setEmailTemplate($emailTemplate); // Fire an event for the notification. $Notification = array('ActivityID' => $ActivityID, 'User' => $User, 'Email' => $Email, 'Route' => $Activity['Route'], 'Story' => $Activity['Story'], 'Headline' => $Activity['Headline'], 'Activity' => $Activity); $this->EventArguments = $Notification; $this->fireEvent('BeforeSendNotification'); // Send the email. try { // Only send if the user is not banned if (!val('Banned', $User)) { $Email->send(); } $Emailed = self::SENT_OK; // Delete the activity now that it has been emailed. if (!$NoDelete && !$Activity['Notified']) { if (val('ActivityID', $Activity)) { $this->delete($Activity['ActivityID']); } else { $Activity['_Delete'] = true; } } } catch (phpmailerException $pex) { if ($pex->getCode() == PHPMailer::STOP_CRITICAL) { $Emailed = self::SENT_FAIL; } else { $Emailed = self::SENT_ERROR; } } catch (Exception $ex) { $Emailed = self::SENT_FAIL; // similar to http 5xx } $Activity['Emailed'] = $Emailed; if ($ActivityID) { // Save the emailed flag back to the activity. $this->SQL->put('Activity', array('Emailed' => $Emailed), array('ActivityID' => $ActivityID)); } }