Esempio n. 1
0
 /**
  * Uses an instance of `MD5`, `SHA1` or `PBKDF2` to create a hash
  *
  * @see cryptography.MD5#hash()
  * @see cryptography.SHA1#hash()
  * @see cryptography.PBKDF2#hash()
  *
  * @param string $input
  * the string to be hashed
  * @param string $algorithm
  * a handle for the algorithm to be used
  * @deprecated This parameter will be removed in a future release. The use of i.e. `SHA1::hash()` is recommended instead when not using the default algorithm.
  * @return string
  * the hashed string
  */
 public static function hash($input, $algorithm = 'pbkdf2')
 {
     switch ($algorithm) {
         case 'md5':
             return MD5::hash($input);
             break;
         case 'sha1':
             return SHA1::hash($input);
             break;
         case 'pbkdf2':
         default:
             return PBKDF2::hash($input);
             break;
     }
 }
 /**
  * Creates an author token using the `Cryptography::hash` function and the
  * current Author's username and password. The default hash function
  * is SHA1
  *
  * @see toolkit.Cryptography#hash()
  * @see toolkit.General#substrmin()
  *
  * @return string
  */
 public function createAuthToken()
 {
     return General::substrmin(SHA1::hash($this->get('username') . $this->get('password')), 8);
 }
Esempio n. 3
0
 public function action()
 {
     if (isset($_POST['action'])) {
         $actionParts = array_keys($_POST['action']);
         $action = end($actionParts);
         // Login Attempted
         if ($action == 'login') {
             if (empty($_POST['username']) || empty($_POST['password']) || !Administration::instance()->login($_POST['username'], $_POST['password'])) {
                 /**
                  * A failed login attempt into the Symphony backend
                  *
                  * @delegate AuthorLoginFailure
                  * @since Symphony 2.2
                  * @param string $context
                  * '/login/'
                  * @param string $username
                  *  The username of the Author who attempted to login.
                  */
                 Symphony::ExtensionManager()->notifyMembers('AuthorLoginFailure', '/login/', array('username' => Symphony::Database()->cleanValue($_POST['username'])));
                 $this->failedLoginAttempt = true;
             } else {
                 /**
                  * A successful login attempt into the Symphony backend
                  *
                  * @delegate AuthorLoginSuccess
                  * @since Symphony 2.2
                  * @param string $context
                  * '/login/'
                  * @param string $username
                  *  The username of the Author who logged in.
                  */
                 Symphony::ExtensionManager()->notifyMembers('AuthorLoginSuccess', '/login/', array('username' => Symphony::Database()->cleanValue($_POST['username'])));
                 isset($_POST['redirect']) ? redirect($_POST['redirect']) : redirect(SYMPHONY_URL . '/');
             }
             // Reset of password requested
         } elseif ($action == 'reset') {
             $author = Symphony::Database()->fetchRow(0, sprintf("\n                        SELECT `id`, `email`, `first_name`\n                        FROM `tbl_authors`\n                        WHERE `email` = '%1\$s' OR `username` = '%1\$s'\n                    ", Symphony::Database()->cleanValue($_POST['email'])));
             if (!empty($author)) {
                 Symphony::Database()->delete('tbl_forgotpass', sprintf("\n                        `expiry` < %d", DateTimeObj::getGMT('c')));
                 if (!($token = Symphony::Database()->fetchVar('token', 0, "SELECT `token` FROM `tbl_forgotpass` WHERE `expiry` > '" . DateTimeObj::getGMT('c') . "' AND `author_id` = " . $author['id']))) {
                     // More secure password token generation
                     if (function_exists('openssl_random_pseudo_bytes')) {
                         $seed = openssl_random_pseudo_bytes(16);
                     } else {
                         $seed = mt_rand();
                     }
                     $token = substr(SHA1::hash($seed), 0, 16);
                     Symphony::Database()->insert(array('author_id' => $author['id'], 'token' => $token, 'expiry' => DateTimeObj::getGMT('c', time() + 120 * 60)), 'tbl_forgotpass');
                 }
                 try {
                     $email = Email::create();
                     $email->recipients = $author['email'];
                     $email->subject = __('New Symphony Account Password');
                     $email->text_plain = __('Hi %s,', array($author['first_name'])) . PHP_EOL . __('A new password has been requested for your account. Login using the following link, and change your password via the Authors area:') . PHP_EOL . PHP_EOL . '    ' . SYMPHONY_URL . "/login/{$token}/" . PHP_EOL . PHP_EOL . __('It will expire in 2 hours. If you did not ask for a new password, please disregard this email.') . PHP_EOL . PHP_EOL . __('Best Regards,') . PHP_EOL . __('The Symphony Team');
                     $email->send();
                     $this->_email_sent = true;
                     $this->_email_sent_to = $author['email'];
                     // Set this so we can display a customised message
                 } catch (Exception $e) {
                     $this->_email_error = General::unwrapCDATA($e->getMessage());
                     Symphony::Log()->pushExceptionToLog($e, true);
                 }
                 /**
                  * When a password reset has occurred and after the Password
                  * Reset email has been sent.
                  *
                  * @delegate AuthorPostPasswordResetSuccess
                  * @since Symphony 2.2
                  * @param string $context
                  * '/login/'
                  * @param integer $author_id
                  *  The ID of the Author who requested the password reset
                  */
                 Symphony::ExtensionManager()->notifyMembers('AuthorPostPasswordResetSuccess', '/login/', array('author_id' => $author['id']));
             } else {
                 /**
                  * When a password reset has been attempted, but Symphony doesn't
                  * recognise the credentials the user has given.
                  *
                  * @delegate AuthorPostPasswordResetFailure
                  * @since Symphony 2.2
                  * @param string $context
                  * '/login/'
                  * @param string $email
                  *  The sanitised Email of the Author who tried to request the password reset
                  */
                 Symphony::ExtensionManager()->notifyMembers('AuthorPostPasswordResetFailure', '/login/', array('email' => Symphony::Database()->cleanValue($_POST['email'])));
                 $this->_email_sent = false;
             }
         }
     }
 }
 /**
  * Uses `SHA1` or `MD5` to create a hash based on some input
  * This function is currently very basic, but would allow
  * future expansion. Salting the hash comes to mind.
  *
  * @param string $input
  *  the string to be hashed
  * @param string $algorithm
  *  This function supports 'md5', 'sha1' and 'pbkdf2'. Any
  *  other algorithm will default to 'pbkdf2'.
  * @return string
  *  the hashed string
  */
 public static function hash($input, $algorithm = 'sha1')
 {
     switch ($algorithm) {
         case 'sha1':
             return SHA1::hash($input);
         case 'md5':
             return MD5::hash($input);
         case 'pbkdf2':
         default:
             return Crytography::hash($input, $algorithm);
     }
 }
 protected function __trigger()
 {
     $result = new XMLElement(self::ROOTELEMENT);
     $fields = $_POST['fields'];
     $this->driver = Symphony::ExtensionManager()->create('members');
     // Add POST values to the Event XML
     $post_values = new XMLElement('post-values');
     // Create the post data cookie element
     if (is_array($fields) && !empty($fields)) {
         General::array_to_xml($post_values, $fields, true);
     }
     // Set the section ID
     $result = $this->setMembersSection($result, $_REQUEST['members-section-id']);
     if ($result->getAttribute('result') === 'error') {
         $result->appendChild($post_values);
         return $result;
     }
     // If a member is logged in, return early with an error
     if ($this->driver->getMemberDriver()->isLoggedIn()) {
         $result->setAttribute('result', 'error');
         $result->appendChild(new XMLElement('message', __('You cannot generate a recovery code while being logged in.'), array('message-id' => MemberEventMessages::ALREADY_LOGGED_IN)));
         $result->appendChild($post_values);
         return $result;
     }
     // Trigger the EventPreSaveFilter delegate. We are using this to make
     // use of the XSS Filter extension that will ensure our data is ok to use
     $this->notifyEventPreSaveFilter($result, $fields, $post_values);
     if ($result->getAttribute('result') == 'error') {
         return $result;
     }
     // Add any Email Templates for this event
     $this->addEmailTemplates('generate-recovery-code-template');
     // Check that either a Member: Username or Member: Password field
     // has been detected
     $identity = $this->driver->getMemberDriver()->setIdentityField($fields, false);
     if (!$identity instanceof Identity) {
         $result->setAttribute('result', 'error');
         $result->appendChild(new XMLElement('message', __('No Identity field found.'), array('message-id' => MemberEventMessages::MEMBER_ERRORS)));
         $result->appendChild($post_values);
         return $result;
     }
     // Check that a member exists first before proceeding.
     if (!isset($fields[$identity->get('element_name')]) or empty($fields[$identity->get('element_name')])) {
         $result->setAttribute('result', 'error');
         $result->appendChild(new XMLElement('message', __('Member event encountered errors when processing.'), array('message-id' => MemberEventMessages::MEMBER_ERRORS)));
         $result->appendChild(new XMLElement($identity->get('element_name'), null, array('label' => $identity->get('label'), 'type' => 'missing', 'message-id' => EventMessages::FIELD_MISSING, 'message' => __('%s is a required field.', array($identity->get('label'))))));
         $result->appendChild($post_values);
         return $result;
     }
     $member_id = $identity->fetchMemberIDBy($fields[$identity->get('element_name')]);
     if (is_null($member_id)) {
         $result->setAttribute('result', 'error');
         $result->appendChild(new XMLElement('message', __('Member event encountered errors when processing.'), array('message-id' => MemberEventMessages::MEMBER_ERRORS)));
         $result->appendChild(new XMLElement($identity->get('element_name'), null, extension_Members::$_errors[$identity->get('element_name')]));
         $result->appendChild($post_values);
         return $result;
     }
     // Find the Authentication fiedl
     $auth = $this->driver->getMemberDriver()->section->getField('authentication');
     $status = Field::__OK__;
     // Generate new password
     $newPassword = $auth->generatePassword();
     $entry = $this->driver->getMemberDriver()->fetchMemberFromID($member_id);
     $entry_data = $entry->getData();
     // Generate a Recovery Code with the same logic as a normal password
     $data = $auth->processRawFieldData(array('password' => $auth->encodePassword($newPassword . $member_id)), $status);
     // Set the Entry password to be reset and the current timestamp
     $data['recovery-code'] = SHA1::hash($newPassword . $member_id);
     $data['reset'] = 'yes';
     $data['expires'] = DateTimeObj::get('Y-m-d H:i:s', time());
     // If the Member has entry data for the Authentication field, update it
     if (array_key_exists((int) $auth->get('id'), $entry_data)) {
         // Overwrite the password with the old password data. This prevents
         // a users account from being locked out if it it just reset by a random
         // member of the public
         $data['password'] = $entry_data[$auth->get('id')]['password'];
         $data['length'] = $entry_data[$auth->get('id')]['length'];
         $data['strength'] = $entry_data[$auth->get('id')]['strength'];
         Symphony::Database()->update($data, 'tbl_entries_data_' . $auth->get('id'), ' `entry_id` = ' . $member_id);
     } else {
         $data['entry_id'] = $member_id;
         Symphony::Database()->insert($data, 'tbl_entries_data_' . $auth->get('id'));
     }
     /**
      * Fired just after a Member has requested a recovery code so they
      * can reset their password.
      *
      * @delegate MembersPostForgotPassword
      * @param string $context
      *  '/frontend/'
      * @param integer $member_id
      *  The Member ID of the member who just requested a recovery
      *  code.
      * @param string $recovery_code
      *  The recovery code that was generated for this Member
      */
     Symphony::ExtensionManager()->notifyMembers('MembersPostForgotPassword', '/frontend/', array('member_id' => $member_id, 'recovery_code' => $data['recovery-code']));
     // Trigger the EventFinalSaveFilter delegate. The Email Template Filter
     // and Email Template Manager extensions use this delegate to send any
     // emails attached to this event
     $this->notifyEventFinalSaveFilter($result, $fields, $post_values, $entry);
     // If a redirect is set, redirect, the page won't be able to receive
     // the Event XML anyway
     if (isset($_REQUEST['redirect'])) {
         redirect($_REQUEST['redirect']);
     }
     $result->setAttribute('result', 'success');
     $result->appendChild(new XMLElement('recovery-code', $data['recovery-code']));
     $result->appendChild($post_values);
     return $result;
 }