Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // ToDo: change message type
- // ToDo: registred?
- class Profile extends HModule {
- public function fetch() {
- if ('login' == $this->param(0)) {
- return $this->fetchLogin();
- }
- if ('register' == $this->param(0)) {
- return $this->fetchRegister();
- }
- if ('logout' == $this->param(0)) {
- return $this->fetchLogout();
- }
- if('edit' == $this->param(0)) {
- return $this->fetchEdit();
- }
- if ('remind' == $this->param(0)) {
- return $this->fetchRemind();
- }
- if ('new_password' == $this->param(0)) {
- return $this->fetchNewPassword();
- }
- if('list' == $this->param(0)) {
- return $this->fetchList();
- }
- if (!$this->param(0)) { // my profile
- Auth::registred();
- return $this->fetchProfile(Auth::user()->id);
- }
- if (is_numeric($this->param(0))) { // user's profile
- return $this->fetchProfile(intval($this->param(0)));
- }
- if ('add_friend' == $this->param(0) and is_numeric($this->param(1))) { // add friend
- return $this->fetchFriendAdd(intval($this->param(1)));
- }
- if ('remove_friend' == $this->param(0) and is_numeric($this->param(1))) { // remove friend
- return $this->fetchFriendRemove(intval($this->param(1)));
- }
- if ('validate' == $this->param(0)) {
- return $this->fetchValidateEmail($this->param(1));
- }
- redirect('/');
- }
- public function fetchLogin() {
- if (Auth::login()) {
- redirect('/');
- } else {
- return HEngine::getInstance()->message('Wrong password');
- }
- }
- public function fetchLogout() {
- Auth::logout();
- redirect('/');
- }
- public function fetchValidateEmail($code) {
- $updated = db()->update('users', array('email_validate' => ''), db()->quoteInto('email_validate=?', $code));
- if ($updated) {
- HEngine::getInstance()->message(t("You can login now"));
- } else {
- HEngine::getInstance()->message(t("Wrong code or already activated"));
- }
- redirect('');
- }
- public function fetchRegister() {
- if(Auth::user()) {
- redirect('profile/edit');
- }
- $nick = post('nick');
- $password = post('password');
- $password2 = post('password2');
- $email = post('email');
- if ($result = PluginManager::slot('profileRegister')) {
- return $result;
- }
- if ($_POST) {
- $valid = true;
- if (mb_strlen($nick, 'UTF-8') < 3) {
- HEngine::getInstance()->message(t("Username should be 3 characters at minimum"));
- $valid = false;
- }
- if (Zend_Validate::is($nick, 'Db_RecordExists', array('table' => 'users', 'field' => 'nick'))) {
- HEngine::getInstance()->message(t("This username already exists"));
- $valid = false;
- }
- if (mb_strlen($password, 'UTF-8') < 5) {
- HEngine::getInstance()->message(t("Password should be 5 characters at minimum"));
- $valid = false;
- }
- if ($password != $password2) {
- HEngine::getInstance()->message(t("Passwords should be the same"));
- $valid = false;
- }
- if (!Zend_Validate::is($email, 'EmailAddress')) {
- HEngine::getInstance()->message(t("Incorrect email"));
- $valid = false;
- }
- if (Zend_Validate::is($email, 'Db_RecordExists', array('table' => 'users', 'field' => 'email'))) {
- HEngine::getInstance()->message(t("This email already exists"));
- $valid = false;
- }
- if ($valid) {
- $user = new User();
- $user->nick = $nick;
- $user->setPassword($password);
- $user->email = $email;
- $user->share_email = post('share_email');
- $user->email_news = post('email_news');
- $user->email_validate = md5(time() . rand());
- $user->points_free = config('pointsForRegistration');
- phpmailer_mail($user->email, t('Email verification'), php_template('profile/email_validate', array(
- 'code' => $user->email_validate,
- 'nick' => $user->nick,
- )));
- $user = PluginManager::slot('profileRegisterSave', $user);
- $user->save();
- HEngine::getInstance()->message(t('Check your email please'));
- redirect('/');
- }
- }
- return php_template('profile/register.php', array(
- 'nick' => $nick,
- 'email' => $email,
- ));
- }
- public function fetchEdit() {
- if(!Auth::user()) {
- redirect('profile/register');
- }
- if ($result = PluginManager::slot('profileEdit')) {
- return $result;
- }
- $user = Auth::user();
- $name = post('name', $user->name);
- $location = post('location', $user->location);
- $web = post('web', $user->web);
- $bio = post('bio', $user->bio);
- $email_news = $_POST ? post('email_news'): $user->email_news;
- $email_new_friends = $_POST ? post('email_new_friends') : $user->email_new_friends;
- $email_new_karma = $_POST ? post('email_new_karma') : $user->email_new_karma;
- $share_email = $_POST ? post('share_email') : $user->share_email;
- if (post('submit')) {
- $valid = true;
- if (!$name) {
- HEngine::getInstance()->message(t('Name is required'));
- $valid = false;
- }
- if (mb_strlen($bio, 'UTF-8') > 160) {
- HEngine::getInstance()->message(t('Bio must be lesser than 160 chars'));
- $valid = false;
- }
- if (post('old_password')) {
- if(Auth::user()->hash != Auth::getHash(post('old_password'))) {
- HEngine::getInstance()->message(t('Wrong password'));
- $valid = false;
- }
- $hasPassword = true;
- } else {
- $hasPassword = false;
- }
- if (post('password') and !$hasPassword) {
- HEngine::getInstance()->message(t('Supply old password if you want to change it'));
- $valid = false;
- }
- if (post('password') != post('password2')) {
- HEngine::getInstance()->message(t('The passwords do not match'));
- $valid = false;
- }
- if (post('password') and mb_strlen(post('password')) < 5) {
- HEngine::getInstance()->message(t('The password is too short'));
- $valid = false;
- }
- $uploader = new ImageUploader('avatar');
- if ($uploader->isUploaded()) {
- if (!$uploader->isCorrectType()) {
- HEngine::getInstance()->message(t("Use png or jpeg image"));
- $valid = false;
- } elseif ($uploader->getFileSize() > config('maxFileSize')) {
- HEngine::getInstance()->message(t("Image is too large"));
- $valid = false;
- } else {
- $ext = $uploader->getImageType();
- $imageFile = $user->id . '.' . $ext;
- $resizer = new ImageResizer(config('thumbWidth'), config('thumbHeight'));
- $resizer->setSourceUploader($uploader);
- $result = $resizer->resizeToFile('uploads/users/' . $imageFile);
- if (!$result) {
- HEngine::getInstance()->message(t('Incorrect image'));
- $valid = false;
- }
- }
- }
- if (!PluginManager::slot('profileEditCheck', true)) {
- $valid = false;
- }
- if ($valid) {
- $user->name = $name;
- $user->location = $location;
- $user->web = $web;
- $user->bio = $bio;
- $user->share_email = (bool)$share_email;
- if (isset($imageFile)) {
- $user->avatar = $imageFile;
- }
- if (post('password') and post('old_password')) {
- $user->setPassword(post('password'));
- $time = time() + 30 * 24 * 60 * 60;
- setcookie('nick', $user->nick, $time, '/', DOMAIN);
- setcookie('hash', Auth::getHash(post('password')), $time, '/', DOMAIN);
- }
- HEngine::getInstance()->message(t("The data was changed"));
- PluginManager::slot('profileEditUpdate', $user);
- $user->save();
- redirect();
- }
- }
- $data = array(
- 'name' => $name,
- 'location' => $location,
- 'web' => $web,
- 'bio' => $bio,
- 'avatar' => $user->avatar,
- 'email_news' => $email_news,
- 'email_new_friends' => $email_new_friends,
- 'email_new_karma' => $email_new_karma,
- 'share_email' => $share_email,
- );
- $data = PluginManager::slot('profileEditTemplate', $data);
- return php_template('profile/edit.php', $data);
- }
- public function fetchRemind() {
- if (Auth::user()) {
- redirect('');
- }
- if (post('submit')) {
- if (post('nick') xor post('email')) {
- $model = HOrmModel::factory('User');
- if (post('nick')) {
- $criteria = $model->criteria()->where('nick = ?', post('nick'));
- } else {
- $criteria = $model->criteria()->where('email = ?', post('email'));
- }
- $user = $model->getOne($criteria);
- if ($user) {
- $email = $user->email;
- $code = md5(time() . rand() . rand());
- $user->remind_code = $code;
- phpmailer_mail($user->email, t('Password remind'), php_template('profile/remind_message', array('code' => $code)));
- $user->save();
- HEngine::getInstance()->message(t("Check your email please"));
- redirect('');
- } else {
- HEngine::getInstance()->message(t("There is no user with this data"));
- }
- } else {
- HEngine::getInstance()->message(t("Fill e-mail OR nick"));
- }
- }
- return php_template('profile/remind.php');
- }
- public function fetchNewPassword() {
- if (Auth::user()) {
- redirect('');
- }
- $code = $this->param(1);
- if (!$code) {
- HEngine::getInstance()->message(t("Code cannot be empty"));
- redirect('');
- }
- $model = HOrmModel::factory('User');
- $criteria = $model->criteria()->where('remind_code = ?', $code);
- $user = $model->getOne($criteria);
- if (!$user) {
- HEngine::getInstance()->message(t("Wrong or used code"));
- redirect('');
- }
- $password = substr(md5(time() . rand()), 0, 10);
- $user->remind_code = '';
- $user->setPassword($password);
- $user->save();
- return php_template('profile/change_password', array(
- 'password' => $password,
- ));
- }
- public function fetchProfile($userId) {
- $db = registry('db');
- $user = HOrmModel::factory('User')->get($userId);
- if (!$user) {
- HEngine::getInstance()->message(sprintf(t("User with id %s doesn't exist"), $userId));
- redirect('');
- }
- $currrentUser = Auth::user() && Auth::user()->id == $user->id;
- $friends = $user->getFriends();
- $friendOf = $user->getFriendOf();
- $mutual = array_intersect($friends, $friendOf);
- $gifts = $user->getGifts();
- $hasFriend = Auth::user() && in_array(Auth::user()->id, array_column($friendOf, 'id'));
- return php_template('profile/profile.php', array(
- 'user' => $user,
- 'currentUser' => $currrentUser,
- 'friends' => $friends,
- 'friendOf' => $friendOf,
- 'mutual' => $mutual,
- 'hasFriend' => $hasFriend,
- 'gifts' => $gifts,
- ));
- }
- public function fetchFriendAdd($friendId) {
- Auth::registred();
- $db = registry('db');
- $friend = HOrmModel::factory('User')->get($friendId);
- if (!$friend) {
- HEngine::getInstance()->message(sprintf(t("No user with this ID (%s)"), $friendId));
- redirect('');
- }
- PluginManager::slot('friendAdd', $friendId);
- $db->insert('friends', array(
- 'user_id' => Auth::user()->id,
- 'friend_id' => $friendId,
- ));
- HEngine::getInstance()->message(t("You have added friend ") . escape($friend->nick));
- redirect("profile/$friendId");
- }
- public function fetchFriendRemove($friendId) {
- Auth::registred();
- $db = registry('db');
- $friends = array_column(Auth::user()->getFriends(), 'id');
- $userId = Auth::user()->id;
- PluginManager::slot('friendRemove', $friendId);
- $db->delete('friends', "user_id = $userId AND friend_id = $friendId");
- HEngine::getInstance()->message(t("You deleted this friend"));
- redirect("profile/$friendId");
- }
- public function fetchList() {
- $limit = intval(config('usersPerPage'));
- $offset = paginator_offset(null, $limit);
- $users = db()->fetchAll("SELECT SQL_CALC_FOUND_ROWS * FROM users LIMIT $limit OFFSET $offset");
- $total = db()->fetchOne("SELECT FOUND_ROWS()");
- if (Auth::user()) {
- $friends = array_column(Auth::user()->getFriends(), 'id');
- $friendOf = array_column(Auth::user()->getFriendOf(), 'id');
- } else {
- $friends = $friendOf = array();
- }
- return php_template('profile/list', array(
- 'users' => $users,
- 'total' => $total,
- 'limit' => $limit,
- 'friends' => $friends,
- 'friendOf' => $friendOf,
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement