/** * Set the user data to placeholders * * @return array */ public function setToPlaceholders() { $placeholders = array_merge($this->profile->toArray(), $this->user->toArray()); $extended = $this->getExtended(); $placeholders = array_merge($extended, $placeholders); $placeholders = $this->removePasswordPlaceholders($placeholders); $this->modx->toPlaceholders($placeholders, $this->getProperty('prefix', '', 'isset'), ''); return $placeholders; }
/** * Get the Profile of the active User * @return modUserProfile */ public function getProfile() { $this->profile = $this->modx->user->getOne('Profile'); if (empty($this->profile)) { $this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not find profile for user: '******'username')); } else { $placeholders = array_merge($this->profile->toArray(), array('username' => $this->modx->user->get('username'), 'id' => $this->modx->user->get('id'))); $this->modx->setPlaceholders($placeholders, $this->getProperty('placeholderPrefix', 'logcp.')); } return $this->profile; }
/** * {@inheritDoc} * * @return array|string */ public function process() { $this->prepare(); /* save profile */ if ($this->profile->save() == false) { return $this->failure($this->modx->lexicon('user_profile_err_save')); } else { /* log manager action */ $this->modx->logManagerAction('save_profile', 'modUser', $this->modx->user->get('id')); } return $this->success($this->modx->lexicon('success'), $this->profile->toArray()); }
/** * Set the user data to placeholders * * @return array */ public function setToPlaceholders() { $placeholders = array_merge($this->profile->toArray(), $this->user->toArray()); $placeholderPrefix = rtrim($this->getProperty('prefix', ''), '.'); $extended = $this->getExtended(); $placeholders = array_merge($extended, $placeholders); $placeholders = $this->removePasswordPlaceholders($placeholders); $this->modx->toPlaceholders($placeholders, $placeholderPrefix); foreach ($placeholders as $k => $v) { if (is_array($v)) { $this->modx->setPlaceholder($placeholderPrefix . '.' . $k, json_encode($v)); } } return $placeholders; }
/** * Fetch the user to update, also allowing external user updating * @return modUser */ public function fetchUser() { $fields = $this->dictionary->toArray(); $this->usernameField = 'username'; $alias = 'modUser'; if (empty($fields['username']) && !empty($fields['email'])) { $this->usernameField = 'email'; $alias = 'Profile'; } /* if the preHook didn't set the user info, find it by email/username */ if (empty($fields[Login::FORGOT_PASSWORD_EXTERNAL_USER])) { /* get the user dependent on the retrieval method */ $this->user = $this->login->getUserByField($this->usernameField, $fields[$this->usernameField], $alias); if ($this->user) { $fields = array_merge($fields, $this->user->toArray()); $this->profile = $this->user->getOne('Profile'); if ($this->profile) { /* merge in profile */ $fields = array_merge($this->profile->toArray(), $fields); } } } $this->dictionary->fromArray($fields); return $this->user; }
/** * If desired, set any extended fields * @return void */ public function setExtended() { if ($this->controller->getProperty('useExtended', true, 'isset')) { $allowedExtendedFields = $this->controller->getProperty('allowedExtendedFields', ''); $allowedExtendedFields = !empty($allowedExtendedFields) ? explode(',', $allowedExtendedFields) : array(); /* first cut out regular fields */ $excludeExtended = $this->controller->getProperty('excludeExtended', ''); $excludeExtended = explode(',', $excludeExtended); $profileFields = $this->profile->toArray(); $userFields = $this->controller->user->toArray(); $newExtended = array(); $fields = $this->controller->dictionary->toArray(); foreach ($fields as $field => $value) { $isValidExtended = true; if (!empty($allowedExtendedFields)) { if (!in_array($field, $allowedExtendedFields)) { $isValidExtended = false; } } if (isset($profileFields[$field]) || isset($userFields[$field]) || $field == 'password_confirm' || $field == 'passwordconfirm' || in_array($field, $excludeExtended) || $field == 'nospam' || $field == 'nospam:blank') { $isValidExtended = false; } if ($isValidExtended) { $newExtended[$field] = $value; } } /* now merge with existing extended data */ $extended = $this->profile->get('extended'); $extended = is_array($extended) ? array_merge($extended, $newExtended) : $newExtended; $this->profile->set('extended', $extended); } }
/** * Set the user data as placeholders * @return void */ public function setFieldPlaceholders() { $placeholders = $this->profile->toArray(); /* add extended fields to placeholders */ if ($this->getProperty('useExtended', true, 'isset')) { $extended = $this->profile->get('extended'); if (!empty($extended) && is_array($extended)) { $placeholders = array_merge($extended, $placeholders); } } $this->modx->toPlaceholders($placeholders, $this->getProperty('placeholderPrefix')); }
/** * Set the user data as placeholders * @return void */ public function setFieldPlaceholders() { $placeholders = $this->profile->toArray(); $placeholderPrefix = rtrim($this->getProperty('placeholderPrefix'), '.'); /* add extended fields to placeholders */ if ($this->getProperty('useExtended', true)) { $extended = $this->profile->get('extended'); if (!empty($extended) && is_array($extended)) { $placeholders = array_merge($extended, $placeholders); } } $this->modx->toPlaceholders($placeholders, $placeholderPrefix); foreach ($placeholders as $k => $v) { if (is_array($v)) { $this->modx->setPlaceholder($placeholderPrefix . '.' . $k, json_encode($v)); } } }
/** * If wanted, set extra values in the form to profile extended field * @return void */ public function setExtended() { /* first cut out regular and unwanted fields */ $excludeExtended = $this->controller->getProperty('excludeExtended', ''); $excludeExtended = explode(',', $excludeExtended); $profileFields = $this->profile->toArray(); $userFields = $this->user->toArray(); $extended = array(); $fields = $this->dictionary->toArray(); $fields = $this->filterAllowedFields($fields); $userGroupField = $this->controller->getProperty('usergroupsField', ''); foreach ($fields as $field => $value) { if (!isset($profileFields[$field]) && !isset($userFields[$field]) && $field != 'password_confirm' && $field != 'passwordconfirm' && $field != $userGroupField && !in_array($field, $excludeExtended)) { $extended[$field] = $value; } } /* now set extended data */ $this->profile->set('extended', $extended); }
/** * @param array $data */ public function OnUserSave(array $data) { if (!defined('SMF') || SMF != 'API') { return; } /** @var modUser $user */ $user = $data['user']; if (!$user || !$user instanceof modUser) { return; } /** @var modUserProfile $profile */ $profile = $user->getOne('Profile'); $password = !empty($_REQUEST['specifiedpassword']) && !empty($_REQUEST['confirmpassword']) && $_REQUEST['specifiedpassword'] == $_REQUEST['confirmpassword'] ? $_REQUEST['specifiedpassword'] : ''; $username = !empty($this->_user) ? $this->_user->username : $user->username; if (!smfapi_getUserByUsername($username)) { $this->addUserToSMF($user->username); } else { $update = array('member_name' => 'username', 'email_address' => 'email', 'real_name' => 'fullname', 'date_registered' => 'createdon', 'birthdate' => 'dob', 'website_url' => 'website', 'location' => 'city', 'gender' => 'gender'); // New MODX user if (empty($this->_user)) { /* if (!$this->modx->getOption('smf_forced_sync')) { $this->modx->log(modX::LOG_LEVEL_ERROR, "[SMF] Could not update existing SMF user \"{$username}\" because of \"smf_forced_sync\" is disabled"); return; } */ $new = array_merge($user->toArray(), $profile->toArray()); foreach ($update as $k => $v) { if (!empty($new[$v])) { if ($k == 'birthdate') { $update[$k] = date('Y-m-d', $new[$v]); } else { $update[$k] = $new[$v]; } } else { unset($update[$k]); } } $update['is_activated'] = $user->active && !$profile->blocked ? 1 : 3; } else { $current = array_merge($this->_user->toArray(), $this->_profile->toArray()); $new = array_merge($user->toArray(), $profile->toArray()); foreach ($update as $k => $v) { if ($new[$v] != $current[$v]) { if ($k == 'birthdate') { $update[$k] = date('Y-m-d', $new[$v]); } else { $update[$k] = $new[$v]; } } else { unset($update[$k]); } } if ($this->_user->active != $user->active || $this->_profile->blocked != $profile->blocked) { $update['is_activated'] = $user->active && !$profile->blocked ? 1 : 3; } } if (!empty($password)) { $update['passwd'] = sha1(strtolower($username) . smfapi_unHtmlspecialchars($password)); } if (!empty($update)) { $response = smfapi_updateMemberData($username, $update); if (is_array($response)) { $this->modx->log(modX::LOG_LEVEL_ERROR, "[SMF] Could not update user \"{$username}\" {$this->modx->event->name} in SMF: " . print_r($response, true)); } elseif (!empty($update['passwd'])) { $contexts = $this->smfGetContexts(); if (in_array($this->modx->context->key, $contexts) && $this->modx->user->username == $user->username) { smfapi_logout($username); smfapi_login($user->username); } } } } }