/** * 获取用户统计信息 * @param User $user * @return array|false */ public function getUserCount($user) { if (!isset($this->count_info[$user->getId()])) { $rt = $this->db->get("user_count", ['picture_count', 'gallery_count', 'comment_count', 'user_follow_count', 'user_fans_count', 'like_gallery_count', 'like_picture_count', 'like_comment_count', 'follow_gallery_count', 'unread_message_count'], ['users_id' => $user->getId()]); if ($rt === false) { Log::write(_("Get user count info error.") . "USER ID:" . $user->getId(), Log::SQL); return false; } $this->count_info[$user->getId()] =& $rt; } return $this->count_info[$user->getId()]; }
/** * 添加上传一张图片 * @param array $names * @param array $tags * @param array $desc * @param array $files * @param array $server * @param User $user * @return array */ public function add($names, $tags, $desc, $files, $server, $user) { $this->checkServer($server); $n_c = count($names); $t_c = count($tags); $d_c = count($desc); $f_c = isset($files['name']) ? count($files['name']) : 0; if ($t_c > 20) { $this->throwMsg(-3); } if ($tags == 0 || $t_c !== $d_c || $d_c !== $f_c || $t_c !== $f_c || $n_c != $t_c) { $this->throwMsg(-2); } unset($n_c, $t_c, $d_c, $f_c); c_lib()->load('upload'); $lib = $server['meta']['_Lib'] . ""; $upload = new Upload(['exts' => ['jpg', 'png', 'gif'], 'sub_status' => true, 'replace' => false, 'max_size' => 5 * 1024 * 1024, 'image_info' => true], $lib, $server['meta']); $info = $upload->upload([$files]); for ($i = 0; $i < count($info); $i++) { $info[$i]['key'] = $this->getIndexOfArray($info[$i]['name'], $files['name']); } $rt = ['list' => [], 'error' => []]; foreach ($info as $v) { $tmp = $this->insert_data($v['save_path'] . $v['save_name'], $server, $names[$v['key']], $tags[$v['key']], $desc[$v['key']], $user->getId(), $v['image']['width'], $v['image']['height']); if (is_int($tmp)) { $rt['list'][] = $tmp; } else { $rt['error'][] = $tmp; } } return $rt; }
/** * 登录限制 * @param null $rt * @param User $user * @return null */ public function message_login_restrictions($rt, $user) { try { if (!$this->notice($user->getId(), 'message', 'login_restrictions')) { return $rt; } $mt = new MailTemplate("message_notice/login_restrictions.md"); $mt->setUserInfo($user->getInfo()); c_lib()->load('input'); $input = new Input(); $mt->setValues(['login_ip' => $input->getRealIP(), 'login_ua' => $input->getUA(), 'login_count' => $user->getErrorLoginCount()]); $this->message->addNoticeMsg($mt->getTitle(), $mt->getContent(), $user->getId()); } catch (\Exception $ex) { Log::write(_("NoticeApply message_login_restrictions create a Exception.") . "EX:[" . $ex->getCode() . "]:" . $ex->getMessage(), Log::NOTICE); } return $rt; }
/** * @param User $user */ public function upload_avatar($user) { c_lib()->load('upload'); $upload = new Upload(['root_path' => 'avatar', 'exts' => ['jpg', 'png'], 'sub_status' => false, 'name_callback' => [function ($param) { return $param; }, $user->getId()], 'replace' => true, 'max_size' => 500 * 1024, 'save_ext' => 'jpg'], 'Local'); if (!isset($_FILES['avatar'])) { $this->throwMsg(-16); } $info = $upload->uploadOne($_FILES['avatar']); lib()->load('Avatar'); $avatar = new Avatar(); $avatar->process_avatar($info['save_name'], 400, 400); }
/** * 解析的时候子返回顶级评论,回复需单独解析 * @param array $data * @return Comment[] */ private function parse($data) { //echo "PARSE:" . c()->getTimer()->get_second() . "<br>"; $this->__lib('Comment'); /** * @var Comment[] $rt */ $rt = []; for ($i = 0, $l = count($data); $i < $l; $i++) { $user_info = []; $comment = []; foreach ($data[$i] as $k => $v) { $index = strpos($k, '_'); switch ($index) { case 4: $user_info[substr($k, 5)] = $v; break; case 7: $comment[$k] = $v; break; } } //从堆栈获取用户 $user = User::UserStack(+$user_info['id']); if (!is_object($user)) { $user = new User($user_info, true); } $rt[$comment['comment_id']] = new Comment($comment, $user->getId()); } $top = []; foreach (array_keys($rt) as $v) { $parent = $rt[$v]->getCommentParent(); if ($parent == 0) { $top[] =& $rt[$v]; } else { $rt[$parent]->createSubNode(intval($v)); } } // echo "PARSE END:" . c()->getTimer()->get_second() . "<br>"; return $top; }
/** * @param User $user * @return string */ public static function upload_avatar($user) { $path = _BasePath_ . "/avatar/"; if (is_file($path . $user->getId() . ".jpg")) { return self::local_avatar($user->getId()); } return ""; }
/** * POST登录 * @param string $account * @param string $password * @param string $captcha * @param bool $save_status */ public function PostLogin($account, $password, $captcha, $save_status) { if (empty($account) || empty($password)) { $this->throwMsg(-10); } $save_status = !empty($save_status); if (!$this->Captcha($captcha)) { //验证码检测 $this->throwMsg(-5); } $account = strtolower($account); $password = strtolower($password); $this->GetAccountUser($account); lib()->load('UserCheck'); if (!UserCheck::CheckPasswordChar($password)) { $this->throwMsg(-3); } $ip = Ip::getInstance(); $max_error_count = hook()->apply("UserLogin_max_error_count", 6); $now_ip = $ip->realip(); if ($max_error_count <= $this->user->getErrorLoginCount() && $ip->fill($now_ip) === $ip->fill($this->user->getErrorLoginIp()) && explode(" ", $this->user->getErrorLoginTime())[0] == date("Y-m-d")) { //登录被限制 $this->throwMsg(-8); } else { if (UserCheck::CreatePassword($password, $this->user->getSalt()) !== $this->user->getPassword()) { //错误登录记录 $this->user->set(array("error_login_count" => 1 + $this->user->getErrorLoginCount(), 'error_login_time' => date("Y-m-d H:i:s"), 'error_login_ip' => $now_ip)); if ($this->user->getErrorLoginCount() >= $max_error_count) { hook()->apply("UserLogin_PostLogin_restrictions", NULL, $this->user); } $this->throwMsg(-4); } else { if (in_array($this->user->getStatus(), [0, 1, 2])) { if ($this->user->getErrorLoginCount() > 0) { //错误登录清零 $this->user->set(array("error_login_count" => 0)); } } else { //登录受限制,无法登录 $this->throwMsg(-9); } } } try { //登录成功后的COOKIE设置 if (strlen($this->user->getCookieLogin()) < 10) { $this->user->set(array("cookie_login" => salt_hash(time() . $this->user->getEmail(), salt(20)))); } if ($save_status) { cookie()->set("UserLogin", $this->user->getId() . "\t" . $this->user->getCookieLogin(), hook()->apply("UserLogin_PostLogin_CookieTime", time() + 60 * 60 * 24 * 7)); } else { cookie()->set("UserLogin", $this->user->getId() . "\t" . $this->user->getCookieLogin()); } } catch (\Exception $ex) { $this->throwMsg(-6); } try { //最后登录信息 self::setLastLoginInfo($this->user); } catch (\Exception $ex) { $this->code = -7; } hook()->apply('UserLogin_PostLogin_Success', NULL, $this->user); }