Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.59 KB | None | 0 0
  1. <?php
  2.  
  3. // ToDo: change message type
  4. // ToDo: registred?
  5.  
  6.  
  7. class Profile extends HModule {
  8.     public function fetch() {
  9.         if ('login' == $this->param(0)) {
  10.             return $this->fetchLogin();
  11.         }
  12.         if ('register' == $this->param(0)) {
  13.             return $this->fetchRegister();
  14.         }
  15.         if ('logout' == $this->param(0)) {
  16.             return $this->fetchLogout();
  17.         }
  18.         if('edit' == $this->param(0)) {
  19.             return $this->fetchEdit();
  20.         }
  21.         if ('remind' == $this->param(0)) {
  22.             return $this->fetchRemind();
  23.         }
  24.         if ('new_password' == $this->param(0)) {
  25.             return $this->fetchNewPassword();
  26.         }
  27.         if('list' == $this->param(0)) {
  28.             return $this->fetchList();
  29.         }
  30.         if (!$this->param(0)) { // my profile
  31.             Auth::registred();
  32.             return $this->fetchProfile(Auth::user()->id);
  33.         }
  34.         if (is_numeric($this->param(0))) { // user's profile
  35.             return $this->fetchProfile(intval($this->param(0)));
  36.         }
  37.         if ('add_friend' == $this->param(0) and is_numeric($this->param(1))) { // add friend
  38.             return $this->fetchFriendAdd(intval($this->param(1)));
  39.         }
  40.         if ('remove_friend' == $this->param(0) and is_numeric($this->param(1))) { // remove friend
  41.             return $this->fetchFriendRemove(intval($this->param(1)));
  42.         }
  43.         if ('validate' == $this->param(0)) {
  44.             return $this->fetchValidateEmail($this->param(1));
  45.         }
  46.         redirect('/');
  47.     }
  48.  
  49.     public function fetchLogin() {
  50.         if (Auth::login()) {
  51.             redirect('/');
  52.         } else {
  53.             return HEngine::getInstance()->message('Wrong password');
  54.         }
  55.     }
  56.  
  57.     public function fetchLogout() {
  58.         Auth::logout();
  59.         redirect('/');
  60.     }
  61.  
  62.     public function fetchValidateEmail($code) {
  63.         $updated = db()->update('users', array('email_validate' => ''), db()->quoteInto('email_validate=?', $code));
  64.         if ($updated) {
  65.             HEngine::getInstance()->message(t("You can login now"));
  66.         } else {
  67.             HEngine::getInstance()->message(t("Wrong code or already activated"));
  68.         }
  69.         redirect('');
  70.     }
  71.  
  72.     public function fetchRegister() {
  73.         if(Auth::user()) {
  74.             redirect('profile/edit');
  75.         }
  76.         $nick = post('nick');
  77.         $password = post('password');
  78.         $password2 = post('password2');
  79.         $email = post('email');
  80.         if ($result = PluginManager::slot('profileRegister')) {
  81.             return $result;
  82.         }
  83.         if ($_POST) {
  84.             $valid = true;
  85.             if (mb_strlen($nick, 'UTF-8') < 3) {
  86.                 HEngine::getInstance()->message(t("Username should be 3 characters at minimum"));
  87.                 $valid = false;
  88.             }
  89.             if (Zend_Validate::is($nick, 'Db_RecordExists', array('table' => 'users', 'field' => 'nick'))) {
  90.                 HEngine::getInstance()->message(t("This username already exists"));
  91.                 $valid = false;
  92.             }
  93.             if (mb_strlen($password, 'UTF-8') < 5) {
  94.                 HEngine::getInstance()->message(t("Password should be 5 characters at minimum"));
  95.                 $valid = false;
  96.             }
  97.             if ($password != $password2) {
  98.                 HEngine::getInstance()->message(t("Passwords should be the same"));
  99.                 $valid = false;
  100.             }
  101.             if (!Zend_Validate::is($email, 'EmailAddress')) {
  102.                 HEngine::getInstance()->message(t("Incorrect email"));
  103.                 $valid = false;
  104.             }
  105.  
  106.             if (Zend_Validate::is($email, 'Db_RecordExists', array('table' => 'users', 'field' => 'email'))) {
  107.                 HEngine::getInstance()->message(t("This email already exists"));
  108.                 $valid = false;
  109.             }
  110.             if ($valid) {
  111.                 $user = new User();
  112.                 $user->nick = $nick;
  113.                 $user->setPassword($password);
  114.                 $user->email = $email;
  115.                 $user->share_email = post('share_email');
  116.                 $user->email_news = post('email_news');
  117.                 $user->email_validate = md5(time() . rand());
  118.                 $user->points_free = config('pointsForRegistration');
  119.                 phpmailer_mail($user->email, t('Email verification'), php_template('profile/email_validate', array(
  120.                     'code' => $user->email_validate,
  121.                     'nick' => $user->nick,
  122.                 )));
  123.                 $user = PluginManager::slot('profileRegisterSave', $user);
  124.                 $user->save();
  125.                 HEngine::getInstance()->message(t('Check your email please'));
  126.                 redirect('/');
  127.             }
  128.         }
  129.         return php_template('profile/register.php', array(
  130.             'nick' => $nick,
  131.             'email' => $email,
  132.         ));
  133.     }
  134.  
  135.     public function fetchEdit() {
  136.         if(!Auth::user()) {
  137.             redirect('profile/register');
  138.         }
  139.         if ($result = PluginManager::slot('profileEdit')) {
  140.             return $result;
  141.         }
  142.         $user = Auth::user();
  143.         $name = post('name', $user->name);
  144.         $location = post('location', $user->location);
  145.         $web = post('web', $user->web);
  146.         $bio = post('bio', $user->bio);
  147.         $email_news        = $_POST ? post('email_news'): $user->email_news;
  148.         $email_new_friends = $_POST ? post('email_new_friends') : $user->email_new_friends;
  149.         $email_new_karma   = $_POST ? post('email_new_karma') : $user->email_new_karma;
  150.         $share_email       = $_POST ? post('share_email') : $user->share_email;
  151.  
  152.         if (post('submit')) {
  153.             $valid = true;
  154.             if (!$name) {
  155.                 HEngine::getInstance()->message(t('Name is required'));
  156.                 $valid = false;
  157.             }
  158.             if (mb_strlen($bio, 'UTF-8') > 160) {
  159.                 HEngine::getInstance()->message(t('Bio must be lesser than 160 chars'));
  160.                 $valid = false;
  161.             }
  162.             if (post('old_password')) {
  163.                 if(Auth::user()->hash != Auth::getHash(post('old_password'))) {
  164.                     HEngine::getInstance()->message(t('Wrong password'));
  165.                     $valid = false;
  166.                 }
  167.                 $hasPassword = true;
  168.             } else {
  169.                 $hasPassword = false;
  170.             }
  171.             if (post('password') and !$hasPassword) {
  172.                 HEngine::getInstance()->message(t('Supply old password if you want to change it'));
  173.                 $valid = false;
  174.             }
  175.             if (post('password') != post('password2')) {
  176.                 HEngine::getInstance()->message(t('The passwords do not match'));
  177.                 $valid = false;
  178.             }
  179.             if (post('password') and mb_strlen(post('password')) < 5) {
  180.                 HEngine::getInstance()->message(t('The password is too short'));
  181.                 $valid = false;
  182.             }
  183.             $uploader = new ImageUploader('avatar');
  184.             if ($uploader->isUploaded()) {
  185.                 if (!$uploader->isCorrectType()) {
  186.                     HEngine::getInstance()->message(t("Use png or jpeg image"));
  187.                     $valid = false;
  188.                 } elseif ($uploader->getFileSize() > config('maxFileSize')) {
  189.                     HEngine::getInstance()->message(t("Image is too large"));
  190.                     $valid = false;
  191.                 } else {
  192.                     $ext = $uploader->getImageType();
  193.                     $imageFile = $user->id . '.' . $ext;
  194.                     $resizer = new ImageResizer(config('thumbWidth'), config('thumbHeight'));
  195.                     $resizer->setSourceUploader($uploader);
  196.                     $result = $resizer->resizeToFile('uploads/users/' . $imageFile);
  197.                     if (!$result) {
  198.                         HEngine::getInstance()->message(t('Incorrect image'));
  199.                         $valid = false;
  200.                     }
  201.                 }
  202.             }
  203.             if (!PluginManager::slot('profileEditCheck', true)) {
  204.                 $valid = false;
  205.             }
  206.  
  207.             if ($valid) {
  208.                 $user->name = $name;
  209.                 $user->location = $location;
  210.                 $user->web = $web;
  211.                 $user->bio = $bio;
  212.                 $user->share_email = (bool)$share_email;
  213.                 if (isset($imageFile)) {
  214.                     $user->avatar = $imageFile;
  215.                 }
  216.                 if (post('password') and post('old_password')) {
  217.                     $user->setPassword(post('password'));
  218.                     $time = time() + 30 * 24 * 60 * 60;
  219.                     setcookie('nick', $user->nick, $time, '/', DOMAIN);
  220.                     setcookie('hash', Auth::getHash(post('password')), $time, '/', DOMAIN);
  221.                 }
  222.                 HEngine::getInstance()->message(t("The data was changed"));
  223.                 PluginManager::slot('profileEditUpdate', $user);
  224.                 $user->save();
  225.                 redirect();
  226.             }
  227.         }
  228.  
  229.         $data = array(
  230.             'name' => $name,
  231.             'location' => $location,
  232.             'web' => $web,
  233.             'bio' => $bio,
  234.             'avatar' => $user->avatar,
  235.             'email_news' => $email_news,
  236.             'email_new_friends' => $email_new_friends,
  237.             'email_new_karma' => $email_new_karma,
  238.             'share_email' => $share_email,
  239.         );
  240.         $data = PluginManager::slot('profileEditTemplate', $data);
  241.         return php_template('profile/edit.php', $data);
  242.     }
  243.  
  244.     public function fetchRemind() {
  245.         if (Auth::user()) {
  246.             redirect('');
  247.         }
  248.         if (post('submit')) {
  249.             if (post('nick') xor post('email')) {
  250.                 $model = HOrmModel::factory('User');
  251.                 if (post('nick')) {
  252.                     $criteria = $model->criteria()->where('nick = ?', post('nick'));
  253.                 } else {
  254.                     $criteria = $model->criteria()->where('email = ?', post('email'));
  255.                 }
  256.                 $user = $model->getOne($criteria);
  257.                 if ($user) {
  258.                     $email = $user->email;
  259.                     $code = md5(time() . rand() . rand());
  260.                     $user->remind_code = $code;
  261.                     phpmailer_mail($user->email, t('Password remind'), php_template('profile/remind_message', array('code' => $code)));
  262.                     $user->save();
  263.                     HEngine::getInstance()->message(t("Check your email please"));
  264.                     redirect('');
  265.                 } else {
  266.                     HEngine::getInstance()->message(t("There is no user with this data"));
  267.                 }
  268.             } else {
  269.                 HEngine::getInstance()->message(t("Fill e-mail OR nick"));
  270.             }
  271.         }
  272.         return php_template('profile/remind.php');
  273.     }
  274.  
  275.     public function fetchNewPassword() {
  276.         if (Auth::user()) {
  277.             redirect('');
  278.         }
  279.         $code = $this->param(1);
  280.         if (!$code) {
  281.             HEngine::getInstance()->message(t("Code cannot be empty"));
  282.             redirect('');
  283.         }
  284.         $model = HOrmModel::factory('User');
  285.         $criteria = $model->criteria()->where('remind_code = ?', $code);
  286.         $user = $model->getOne($criteria);
  287.         if (!$user) {
  288.             HEngine::getInstance()->message(t("Wrong or used code"));
  289.             redirect('');
  290.         }
  291.         $password = substr(md5(time() . rand()), 0, 10);
  292.         $user->remind_code = '';
  293.         $user->setPassword($password);
  294.         $user->save();
  295.  
  296.         return php_template('profile/change_password', array(
  297.             'password' => $password,
  298.         ));
  299.     }
  300.  
  301.     public function fetchProfile($userId) {
  302.         $db = registry('db');
  303.         $user = HOrmModel::factory('User')->get($userId);
  304.         if (!$user) {
  305.             HEngine::getInstance()->message(sprintf(t("User with id %s doesn't exist"), $userId));
  306.             redirect('');
  307.         }
  308.         $currrentUser = Auth::user() && Auth::user()->id == $user->id;
  309.         $friends = $user->getFriends();
  310.         $friendOf = $user->getFriendOf();
  311.         $mutual = array_intersect($friends, $friendOf);
  312.         $gifts = $user->getGifts();
  313.         $hasFriend = Auth::user() && in_array(Auth::user()->id, array_column($friendOf, 'id'));
  314.         return php_template('profile/profile.php', array(
  315.             'user' => $user,
  316.             'currentUser' => $currrentUser,
  317.             'friends' => $friends,
  318.             'friendOf' => $friendOf,
  319.             'mutual' => $mutual,
  320.             'hasFriend' => $hasFriend,
  321.             'gifts' => $gifts,
  322.         ));
  323.     }
  324.  
  325.     public function fetchFriendAdd($friendId) {
  326.         Auth::registred();
  327.         $db = registry('db');
  328.         $friend = HOrmModel::factory('User')->get($friendId);
  329.         if (!$friend) {
  330.             HEngine::getInstance()->message(sprintf(t("No user with this ID (%s)"), $friendId));
  331.             redirect('');
  332.         }
  333.         PluginManager::slot('friendAdd', $friendId);
  334.         $db->insert('friends', array(
  335.             'user_id' => Auth::user()->id,
  336.             'friend_id' => $friendId,
  337.         ));
  338.         HEngine::getInstance()->message(t("You have added friend ") . escape($friend->nick));
  339.         redirect("profile/$friendId");
  340.     }
  341.  
  342.     public function fetchFriendRemove($friendId) {
  343.         Auth::registred();
  344.         $db = registry('db');
  345.         $friends = array_column(Auth::user()->getFriends(), 'id');
  346.         $userId = Auth::user()->id;
  347.         PluginManager::slot('friendRemove', $friendId);
  348.         $db->delete('friends', "user_id = $userId AND friend_id = $friendId");
  349.         HEngine::getInstance()->message(t("You deleted this friend"));
  350.         redirect("profile/$friendId");
  351.     }
  352.  
  353.     public function fetchList() {
  354.         $limit = intval(config('usersPerPage'));
  355.         $offset = paginator_offset(null, $limit);
  356.         $users = db()->fetchAll("SELECT SQL_CALC_FOUND_ROWS * FROM users LIMIT $limit OFFSET $offset");
  357.         $total = db()->fetchOne("SELECT FOUND_ROWS()");
  358.         if (Auth::user()) {
  359.             $friends = array_column(Auth::user()->getFriends(), 'id');
  360.             $friendOf = array_column(Auth::user()->getFriendOf(), 'id');
  361.         } else {
  362.             $friends = $friendOf = array();
  363.         }
  364.         return php_template('profile/list', array(
  365.             'users' => $users,
  366.             'total' => $total,
  367.             'limit' => $limit,
  368.             'friends' => $friends,
  369.             'friendOf' => $friendOf,
  370.         ));
  371.     }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement