/** * 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; }
/** * Returns a unique 8 character invitation code. */ protected function getInvitationCode() { // Generate a new invitation code. $Code = BetterRandomString(16, 'Aa0'); // Make sure the string doesn't already exist in the invitation table $CodeData = $this->getWhere(array('Code' => $Code)); if ($CodeData->numRows() > 0) { return $this->GetInvitationCode(); } else { return $Code; } }
public function Ping() { $start = microtime(true); $this->SetData('pong', TRUE); $this->MasterView = 'empty'; $this->CssClass = 'Home'; $valid = true; // Test the cache. if (Gdn::Cache()->ActiveEnabled()) { $k = BetterRandomString(20); Gdn::Cache()->Store($k, 1); Gdn::Cache()->Increment($k, 1); $v = Gdn::Cache()->Get($k); if ($v !== 2) { $valid = false; $this->SetData('cache', false); } else { $this->SetData('cache', true); } } else { $this->SetData('cache', 'disabled'); } // Test the db. try { $users = Gdn::SQL()->Get('User', 'UserID', 'asc', 1); $this->SetData('database', true); } catch (Exception $ex) { $this->SetData('database', false); $valid = false; } $this->EventArguments['Valid'] =& $valid; $this->FireEvent('Ping'); if (!$valid) { $this->StatusCode(500); } $time = microtime(true) - $start; $this->SetData('time', Gdn_Format::Timespan($time)); $this->SetData('time_s', $time); $this->SetData('valid', $valid); $this->Title('Ping'); $this->Render(); }
/** * Used by $this->Stash() to create & manage sessions for users & guests. * This is a stop-gap solution until full session management for users & * guests can be imlemented. */ private function _GetStashSession($ValueToStash) { $CookieName = C('Garden.Cookie.Name', 'Vanilla'); $Name = $CookieName . '-sid'; // Grab the entire session record $SessionID = GetValue($Name, $_COOKIE, ''); // If there is no session, and no value for saving, return; if ($SessionID == '' && $ValueToStash == '') { return FALSE; } $Session = Gdn::SQL()->Select()->From('Session')->Where('SessionID', $SessionID)->Get()->FirstRow(); if (!$Session) { $SessionID = BetterRandomString(32); $TransientKey = substr(md5(mt_rand()), 0, 11) . '!'; // Save the session information to the database. Gdn::SQL()->Insert('Session', array('SessionID' => $SessionID, 'UserID' => Gdn::Session()->UserID, 'TransientKey' => $TransientKey, 'DateInserted' => Gdn_Format::ToDateTime(), 'DateUpdated' => Gdn_Format::ToDateTime())); Trace("Inserting session stash {$SessionID}"); $Session = Gdn::SQL()->Select()->From('Session')->Where('SessionID', $SessionID)->Get()->FirstRow(); // Save a session cookie $Path = C('Garden.Cookie.Path', '/'); $Domain = C('Garden.Cookie.Domain', ''); $Expire = 0; // If the domain being set is completely incompatible with the current domain then make the domain work. $CurrentHost = Gdn::Request()->Host(); if (!StringEndsWith($CurrentHost, trim($Domain, '.'))) { $Domain = ''; } setcookie($Name, $SessionID, $Expire, $Path, $Domain); $_COOKIE[$Name] = $SessionID; } $Session->Attributes = @unserialize($Session->Attributes); if (!$Session->Attributes) { $Session->Attributes = array(); } return $Session; }