/** * Delete a profile question */ public function delete() { $question = ProfileQuestion::getByName($this->name); if ($question->editable) { $params = json_decode($question->parameters, true); $question->delete(); // Remove the language keys for the label and the options $keysToRemove = array('admin' => array('profile-question-' . $this->name . '-label')); if (!empty($params['options'])) { foreach ($params['options'] as $i => $value) { $keysToRemove['admin'][] = 'profile-question-' . $this->name . '-option-' . $i; } } foreach (Language::getAll() as $language) { $language->removeTranslations($keysToRemove); } } }
/** * Create or edit an user */ public function edit() { $user = App::session()->getUser(); $roles = array_map(function ($role) { return $role->getLabel(); }, Role::getAll('id')); $param = array('id' => 'user-profile-form', 'upload' => true, 'object' => $user, 'fieldsets' => array('general' => array('legend' => Lang::get('admin.user-form-general-legend'), new TextInput(array('name' => 'username', 'required' => true, 'label' => Lang::get('admin.user-form-username-label'), 'disabled' => true)), new EmailInput(array('name' => 'email', 'required' => true, 'label' => Lang::get('admin.user-form-email-label')))), 'profile' => array('legend' => Lang::get('admin.user-form-profile-legend')), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get($this->_plugin . '.valid-button'))))), 'onsuccess' => 'app.dialog("close")'); // Get the user profile questions $questions = ProfileQuestion::getAll('name', array(), array('order' => DB::SORT_ASC)); // Generate the question fields foreach ($questions as $question) { if ($question->displayInProfile && $question->isAllowedForRole($user->roleId)) { $classname = '\\Hawk\\' . ucwords($question->type) . 'Input'; $field = json_decode($question->parameters, true); $field['name'] = $question->name; $field['id'] = 'user-form-' . $question->name . '-input'; $field['independant'] = true; $field['label'] = Lang::get('admin.profile-question-' . $question->name . '-label'); if (isset($field['readonly'])) { if ($field['readonly']) { $field['required'] = false; } } if ($user) { if ($question->type == "file") { $field['after'] = sprintf('<img src="%s" class="profile-image" />', $user->getProfileData($question->name) ? $user->getProfileData($question->name) : ''); } else { $field['default'] = $user->getProfileData($question->name); } } if ($question->name == 'language') { // Get language options $languages = Language::getAllActive(); $options = array(); foreach ($languages as $language) { $options[$language->tag] = $language->label; } $field['options'] = $options; if (!$field['default']) { $field['default'] = Option::get($this->_plugin . '.language'); } } $param['fieldsets']['profile'][] = new $classname($field); } } $form = new Form($param); if (!$form->submitted()) { return NoSidebarTab::make(array('title' => Lang::get('admin.user-form-title'), 'page' => array('content' => $form))); } else { try { foreach ($questions as $question) { if ($question->displayInProfile && $question->isAllowedForRole($user->roleId)) { if ($question->type === 'file') { $upload = Upload::getInstance($question->name); if ($upload) { $file = $upload->getFile(0); $dir = Plugin::current()->getPublicUserfilesDir() . 'img/'; $url = Plugin::current()->getUserfilesUrl() . 'img/'; if (!is_dir($dir)) { mkdir($dir, 0755, true); } $basename = uniqid() . $file->extension; $upload->move($file, $dir, $basename); $user->setProfileData($question->name, $url . $basename); } } else { $user->setProfileData($question->name, $form->inputs[$question->name]->dbvalue()); } } } $user->saveProfile(); if ($form->getData('email') !== $user->email) { // The user asked to reset it email // Check this email is not used by another user on the application $existingUser = User::getByExample(new DBExample(array('id' => array('$ne' => $user->id), 'email' => $form->getData('email')))); if ($existingUser) { return $form->response(Form::STATUS_CHECK_ERROR, Lang::get($this->_plugin . '.reset-email-already-used')); } // Send the email to validate the new email // Create the token to validate the new email $tokenData = array('userId' => $user->id, 'currentEmail' => $user->email, 'newEmail' => $form->getData('email'), 'createTime' => time()); $token = base64_encode(Crypto::aes256Encode(json_encode($tokenData))); // Create the email content $emailContent = View::make($this->getPlugin()->getView('change-email-validation.tpl'), array('sitename' => Option::get($this->_plugin . '.sitename'), 'validationUrl' => App::router()->getUrl('validate-new-email', array('token' => $token)))); $email = new Mail(); $email->to($form->getData('email'))->from(Option::get('main.mailer-from'), Option::get('main.mailer-from-name'))->title(Lang::get($this->_plugin . '.reset-email-title', array('sitename' => Option::get($this->_plugin . '.sitename'))))->content($emailContent)->subject(Lang::get($this->_plugin . '.reset-email-title', array('sitename' => Option::get($this->_plugin . '.sitename'))))->send(); return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.user-profile-update-success-with-email')); } return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.user-profile-update-success')); } catch (Exception $e) { return $form->response(Form::STATUS_ERROR, Lang::get($this->_plugin . '.user-profile-update-error')); } } }
/** * Get the user's profile data * * @param string $prop The property name to get. * If not set, the function will return an array containing all the profile data * * @return mixed */ public function getProfileData($prop = '') { if (!isset($this->profile)) { $sql = 'SELECT Q.name, V.value FROM ' . ProfileQuestionValue::getTable() . ' V INNER JOIN ' . ProfileQuestion::getTable() . ' Q ON V.question = Q.name WHERE V.userId = :id'; $data = App::db()->query($sql, array('id' => $this->id), array('return' => DB::RETURN_ARRAY, 'index' => 'name')); $this->profile = array_map(function ($v) { return $v['value']; }, $data); } return $prop ? isset($this->profile[$prop]) ? $this->profile[$prop] : null : $this->profile; }
/** * Register a new user */ public function register() { $param = array('id' => 'register-form', 'model' => 'User', 'reference' => array('id' => -1), 'fieldsets' => array('global' => array('legend' => Lang::get($this->_plugin . '.register-connection-legend'), new TextInput(array('name' => 'username', 'required' => true, 'unique' => true, 'pattern' => '/^\\w+$/', 'label' => Lang::get($this->_plugin . '.register-username-label'))), new EmailInput(array('name' => 'email', 'required' => true, 'unique' => true, 'label' => Lang::get($this->_plugin . '.register-email-label'))), new PasswordInput(array('name' => 'password', 'required' => true, 'encrypt' => array('\\Hawk\\Crypto', 'saltHash'), 'label' => Lang::get($this->_plugin . '.register-password-label'))), new PasswordInput(array('name' => 'passagain', 'required' => true, 'independant' => true, 'label' => Lang::get($this->_plugin . '.register-passagain-label'), 'compare' => 'password'))), 'profile' => array('legend' => Lang::get($this->_plugin . '.register-profile-legend')), 'terms' => array(Option::get($this->_plugin . '.confirm-register-terms') ? new CheckboxInput(array('name' => 'terms', 'required' => true, 'independant' => true, 'labelWidth' => 'auto', 'label' => Lang::get($this->_plugin . '.register-terms-label', array('uri' => App::router()->getUri('terms'))))) : null), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get($this->_plugin . '.register-button'))), new ButtonInput(array('name' => 'cancel', 'value' => Lang::get($this->_plugin . '.cancel-button'), 'href' => App::router()->getUri('login'), 'target' => 'dialog')))), 'onsuccess' => 'app.dialog(app.getUri("login"))'); $questions = ProfileQuestion::getRegisterQuestions(); foreach ($questions as $question) { $field = json_decode($question->parameters, true); //if(!empty($field->roles) && in_array(Option::get('roles.default-role'), $field->roles)) { if ($question->isAllowedForRole(Option::get('roles.default-role'))) { $classname = 'Hawk\\' . ucwords($question->type) . 'Input'; $field['name'] = $question->name; $field['independant'] = true; $field['label'] = Lang::get('admin.profile-question-' . $question->name . '-label'); // At register, no field is readonly! $field['readonly'] = false; $param['fieldsets']['profile'][] = new $classname($field); } } $form = new Form($param); if (!$form->submitted()) { return Dialogbox::make(array('page' => $form->__toString(), 'icon' => 'sign-in', 'title' => Lang::get($this->_plugin . '.login-form-title'), 'width' => '50rem')); } else { if ($form->check()) { try { $user = new User(array('username' => $form->inputs['username']->dbvalue(), 'email' => $form->inputs['email']->dbvalue(), 'password' => $form->inputs['password']->dbvalue(), 'active' => Option::get($this->_plugin . '.confirm-register-email') ? 0 : 1, 'createTime' => time(), 'createIp' => App::request()->clientIp(), 'roleId' => Option::get('roles.default-role'))); $user->save(); foreach ($questions as $question) { if ($question->type === 'file') { $upload = Upload::getInstance($question->name); if ($upload) { $file = $upload->getFile(0); $dir = Plugin::current()->getUserfilesDir() . 'img/'; $url = Plugin::current()->getUserfilesUrl() . 'img/'; if (!is_dir($dir)) { mkdir($dir, 0755, true); } $upload->move($file, $dir); $user->setProfileData($question->name, $url . $file->basename); } } else { $user->setProfileData($question->name, $form->inputs[$question->name]->dbvalue()); } } $user->saveProfile(); if (Option::get($this->_plugin . '.confirm-register-email')) { // Send an email to validate the registration $tokenData = array('username' => $user->username, 'email' => $user->email, 'createTime' => $user->createTime, 'createIp' => $user->createIp); $token = Crypto::aes256Encode(json_encode($tokenData)); $url = App::router()->getUrl('validate-registration', array('token' => $token)); $data = array('themeBaseCss' => Theme::getSelected()->getBaseCssUrl(), 'themeCustomCss' => Theme::getSelected()->getCustomCssUrl(), 'logoUrl' => Option::get($this->_plugin . '.logo') ? Plugin::current()->getUserfilesUrl(Option::get($this->_plugin . '.logo')) : Plugin::current()->getStaticUrl('img/hawk-logo.png'), 'sitename' => Option::get($this->_plugin . '.sitename'), 'url' => $url); if (Option::get($this->_plugin . '.confirm-email-content')) { $mailContent = View::makeFromString(Option::get($this->_plugin . '.confirm-email-content'), $data); } else { $mailContent = View::make(Plugin::current()->getView('registration-validation-email.tpl'), $data); } $mail = new Mail(); $mail->from(Option::get($this->_plugin . '.mailer-from'))->fromName(Option::get($this->_plugin . '.mailer-from-name'))->to($user->email)->title(Lang::get('main.register-email-title', array('sitename' => Option::get('main.sitename'))))->content($mailContent)->subject(Lang::get($this->_plugin . '.register-email-title', array('sitename' => Option::get($this->_plugin . '.sitename'))))->send(); return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.register-send-email-success')); } else { // validate the registration return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.register-success')); } } catch (Exception $e) { return $form->response(Form::STATUS_ERROR, DEBUG_MODE ? $e->getMessage() : Lang::get($this->_plugin . '.register-error')); } } } }