Exemplo n.º 1
0
 public function __invoke()
 {
     // 验证登录
     $session = $this->getSession();
     if (!empty($session['user'])) {
         $session->addFlash('info', '已经登录过了');
         return new RedirectResponse($this->generateUrl('forum_homepage'));
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         try {
             $email = $posts->get('email');
             $username = $posts->get('username');
             $password = $posts->get('password');
             if (empty($email)) {
                 throw new \Exception("邮箱地址不能为空");
             }
             if (empty($username)) {
                 throw new \Exception('用户名不能为空');
             }
             if (empty($password)) {
                 throw new \Exception("密码不能为空");
             }
             // 查询重复
             $user = UserModel::getUserByUsername($username);
             if ($user) {
                 throw new \Exception("用户名已被占用");
             }
             $user = UserModel::getUserByEmail($email);
             if ($user) {
                 throw new \Exception("邮箱已被注册过");
             }
             $user = new UserModel();
             $user->username = $username;
             $user->password = $password;
             $user->email = $email;
             $now = time();
             $user->createTimestamp = $now;
             $user->loginTimestamp = $now;
             UserModel::createUser($user);
             $session['user'] = $user->toArray();
             if ($request->isXmlHttpRequest()) {
                 $modal_content = $this->render('modal.html.twig', array('title' => '注册成功', 'msg' => '<p class="text-success">恭喜您,注册成功</p>', 'jump_link' => $this->generateUrl('forum_homepage')));
                 return new JsonResponse(array('status' => 1, 'modal' => $modal_content->getContent()));
             } else {
                 $session->addFlash('success', '注册成功');
                 return new RedirectResponse($this->generateUrl('forum_homepage'));
             }
         } catch (\Exception $e) {
             if ($request->isXmlHttpRequest()) {
                 $modal_content = $this->render('modal.html.twig', array('title' => '注册失败', 'msg' => '<p class="text-danger">注册失败:' . $e->getMessage() . '</p>'));
                 return new JsonResponse(array('status' => 0, 'modal' => $modal_content->getContent()));
             } else {
                 $session->addFlash('error', "注册失败:{$e->getMessage()}");
             }
         }
     }
     return $this->render('user/register.html.twig');
 }
Exemplo n.º 2
0
 public function __invoke($post_id)
 {
     // 检查登录
     $session = $this->getSession();
     if (empty($session['user'])) {
         $session->addFlash('error', '请先登录');
         return new RedirectResponse($this->generateUrl('user_login'));
     }
     $user = UserModel::getUser($session['user']['id']);
     // 检查帖子是否存在
     $post = PostModel::getPost($post_id);
     if (!$post) {
         $session->addFlash('error', '帖子不存在');
         return new RedirectResponse($this->getRequest()->server->get('HTTP_REFERER'));
     }
     // 检查主题
     $thread = ThreadModel::getThread($post->threadId);
     if (!$thread) {
         $session->addFlash('error', '主题不存在');
         return new RedirectResponse($this->generateUrl('forum_board', array('forum_id' => $post->forumId)));
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $db = ForumDatabase::getInstance();
         $db->transaction();
         try {
             $content = $posts->get('content');
             $pictures = $posts->get('thumbnails');
             if (!$pictures) {
                 $pictures = array();
             } else {
                 $pictures = explode('<>', $pictures);
             }
             if (empty($content) && empty($pictures)) {
                 throw new \Exception("内容和图片不能都为空");
             }
             $reply = new PostModel();
             $reply->threadId = $post->threadId;
             $reply->forumId = $post->forumId;
             $reply->replyPostId = $post->postId;
             $reply->content = $content;
             $reply->thumbnails = implode('<>', $pictures);
             $reply->createTimestamp = time();
             $reply->authorId = $user->id;
             $reply->authorName = $user->username;
             // 保存
             PostModel::createPost($reply);
             // 增加主题回复数
             $thread->replies += 1;
             ThreadModel::saveThread($thread);
             $db->commit();
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('forum_thread_view', array('thread_id' => $post->threadId)));
     }
     return $this->render('post/reply.html.twig', array('post' => $post));
 }
Exemplo n.º 3
0
 public function __invoke()
 {
     // 检查登录
     $session = $this->getSession();
     if (empty($session['user'])) {
         $session->addFlash('error', '请先登录');
         return new RedirectResponse($this->generateUrl('user_login'));
     }
     $user = UserModel::getUser($session['user']['id']);
     $request = $this->getRequest();
     $type = $request->query->get('type');
     if (!$type) {
         $type = 'post';
     }
     $page = $request->query->get('page');
     if (!$page) {
         $page = 1;
     }
     $size = 10;
     $pager = null;
     if ($type == 'post') {
         $pager = ThreadModel::listForumThreads($page, $size, function (QueryBuilder $qb) use($user) {
             /** @var UserModel $user */
             $qb->andWhere($qb->expr()->eq('author_id', ':author_id'))->setParameter(':author_id', $user->id);
         });
         $data = $pager->getData();
         foreach ($data as $key => $value) {
             $forum_id = $value['forum_id'];
             $forum = ForumModel::getForum($forum_id);
             $value['forum'] = $forum;
             $data[$key] = $value;
         }
         $pager->setData($data);
     } elseif ($type == 'reply') {
         $data = PostModel::getPostsWhere(function (QueryBuilder $qb) use($user) {
             /** @var UserModel $user */
             $qb->andWhere($qb->expr()->eq('author_id', ':author_id'))->setParameter(':author_id', $user->id);
         });
         $thread_ids = array();
         foreach ($data as $key => $value) {
             $thread_ids[] = $value['thread_id'];
         }
         $thread_ids = array_unique($thread_ids);
         $pager = ThreadModel::listForumThreads($page, $size, function (QueryBuilder $qb) use($thread_ids) {
             /** @var UserModel $user */
             $qb->andWhere($qb->expr()->in('thread_id', $thread_ids));
         });
         $data = $pager->getData();
         foreach ($data as $key => $value) {
             $forum_id = $value['forum_id'];
             $forum = ForumModel::getForum($forum_id);
             $value['forum'] = $forum;
             $data[$key] = $value;
         }
         $pager->setData($data);
     }
     $pager->setQuery(array('type' => $type));
     return $this->render('thread/my.html.twig', array('pager' => $pager));
 }
Exemplo n.º 4
0
 public function __invoke($forum_id)
 {
     // 检查登录
     $session = $this->getSession();
     if (empty($session['user'])) {
         $session->addFlash('error', '请先登录');
         return new RedirectResponse($this->generateUrl('user_login'));
     }
     $user = UserModel::getUser($session['user']['id']);
     // 检查版块是否存在
     $forum = ForumModel::getForum($forum_id);
     if (!$forum) {
         $session->addFlash('error', '论坛版块不存在');
         return new RedirectResponse($this->generateUrl('forum_homepage'));
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         // 处理发布数据
         $posts = $request->request;
         $db = ForumDatabase::getInstance();
         try {
             $db->transaction();
             $subject = $posts->get('subject');
             $content = $posts->get('content');
             $pictures = $posts->get('thumbnails');
             if (!$pictures) {
                 $pictures = array();
             } else {
                 $pictures = explode('<>', $pictures);
             }
             if (empty($subject)) {
                 throw new \Exception('帖子标题不能为空');
             }
             $summary = strip_tags($content);
             // 创建主题
             $thread = new ThreadModel();
             $thread->subject = $subject;
             $thread->summary = $summary;
             $thread->thumbnails = implode('<>', $pictures);
             $thread->content = $content;
             $thread->forumId = $forum_id;
             $thread->authorId = $user->id;
             $thread->authorName = $user->username;
             $now = time();
             $thread->createTimestamp = $now;
             $thread->lastPostTimestamp = $now;
             $thread->lastPosterName = $user->username;
             // 保存
             $thread = ThreadModel::createThread($thread);
             $db->commit();
             return new RedirectResponse($this->generateUrl('forum_thread_view', array('thread_id' => $thread->threadId)));
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
             return new RedirectResponse($this->generateUrl('forum_thread_post', array('forum_id' => $forum_id)));
         }
     }
     return $this->render('thread/post.html.twig', array('forum' => $forum));
 }
Exemplo n.º 5
0
 protected function handle()
 {
     $request = $this->getRequest();
     $user_id = $request->query->get('user_id');
     if (empty($user_id)) {
         throw new \Exception('参数错误');
     }
     $user = UserModel::getUser($user_id);
     if (!$user) {
         throw new \Exception('用户不存在');
     }
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         $posts = $request->request;
         try {
             $password = $posts->get('password');
             $repeat_password = $posts->get('repeat_password');
             $nickname = $posts->get('nickname');
             if (empty($nickname) || strlen($nickname) < 2) {
                 throw new \Exception("昵称不能为空,必需至少2个字符");
             }
             if (!empty($password) && strlen($password) < 6) {
                 throw new \Exception("登录密码必需至少6个字符");
             }
             if (!empty($repeat_password) && strlen($repeat_password) < 6) {
                 throw new \Exception("重复密码必需至少6个字符");
             }
             if (!empty($password) && !empty($repeat_password) && $password != $repeat_password) {
                 throw new \Exception("重复密码与登录密码不一致");
             }
             $email = $posts->get('email');
             $mobile = $posts->get('mobile');
             $user->nickname = $nickname;
             if (!empty($password)) {
                 $user->password = $password;
             }
             $user->email = $email;
             $user->mobile = $mobile;
             $user->updateTimestamp = time();
             UserModel::saveUser($user);
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_user_list'));
     }
     return $this->render('user/user-edit.html.twig', array('user' => $user));
 }
Exemplo n.º 6
0
 public function __invoke()
 {
     $request = $this->getRequest();
     $session = $this->getSession();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         try {
             $email = $posts->get('email');
             if (!$email) {
                 throw new \Exception("邮箱不能为空");
             }
             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                 throw new \Exception("错误的邮箱地址");
             }
             $captcha = $posts->get('captcha');
             if (!$captcha) {
                 throw new \Exception("验证码不能为空");
             }
             if ($captcha != $session['captcha']) {
                 throw new \Exception("验证码错误");
             }
             // 检测邮箱是否注册
             $user = UserModel::getUserByEmail($email);
             if (!$user) {
                 throw new \Exception("邮箱没有注册");
             }
             // 发送邮件
             $transport = \Swift_SmtpTransport::newInstance('smtp.163.com')->setUsername('*****@*****.**')->setPassword('llw132255');
             $mailer = \Swift_Mailer::newInstance($transport);
             $message = \Swift_Message::newInstance();
             $message->setSubject('重置密码')->setFrom('*****@*****.**')->setTo($email)->setBody('这是一封测试邮件', 'text/html');
             $mailer->send($message);
             $modal_content = $this->render('modal.html.twig', array('title' => '邮件发送成功', 'msg' => '<p class="text-success">邮件发送成功</p>', 'jump_link' => $this->generateUrl('forum_homepage')));
             return new JsonResponse(array('status' => 1, 'modal' => $modal_content->getContent()));
         } catch (\Exception $e) {
             $modal_content = $this->render('modal.html.twig', array('title' => '邮件发送失败', 'msg' => '<p class="text-danger">发生错误:' . $e->getMessage() . '</p>'));
             return new JsonResponse(array('status' => 0, 'modal' => $modal_content->getContent()));
         }
     }
     return $this->render('user/password.html.twig');
 }
Exemplo n.º 7
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $db = UserModel::getDb();
         $session = $this->getSession();
         try {
             $db->transaction();
             foreach ($ids as $id) {
                 $user = UserModel::getUser($id);
                 if ($user) {
                     UserModel::removeUser($user);
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_user_list'));
     } else {
         $ids = $request->query->get('ids');
         $ids = json_decode($ids);
         $users = array();
         foreach ($ids as $user_id) {
             $user = UserModel::getUser($user_id);
             if (!$user) {
                 throw new \Exception("用户'{$user_id}'不存在");
             } else {
                 $users[] = $user;
             }
         }
         return $this->render('user/user-delete.html.twig', array('users' => $users));
     }
 }
Exemplo n.º 8
0
 protected function handle()
 {
     $request = $this->getRequest();
     if (!$request->isXmlHttpRequest()) {
         return $this->render('user/list.html.twig');
     }
     $posts = $request->request;
     $page_offset = $posts->get('start');
     $page_offset = intval($page_offset);
     $page_size = $posts->get('length');
     $page_size = intval($page_size);
     $is_all = false;
     if ($page_size < 0) {
         $is_all = true;
     }
     $s_echo = $posts->get('draw');
     $s_echo = intval($s_echo);
     $action = $posts->get('action');
     $records = array();
     $records['data'] = array();
     $records['draw'] = $s_echo;
     $records['recordsTotal'] = 0;
     $records['recordsFiltered'] = 0;
     $columns = array(1 => 'id', 2 => 'username', 3 => 'nickname', 4 => 'email', 5 => 'mobile', 6 => 'create_timestamp', 7 => 'login_timestamp');
     $filter_columns = array('id', 'username', 'nickname', 'email', 'mobile', 'create_date_from', 'create_date_to', 'login_date_from', 'login_date_to');
     $orders = $posts->get('order');
     $sorts = array();
     foreach ($orders as $order) {
         $col = $order['column'];
         $dir = $order['dir'];
         $sorts[] = array('field' => $columns[$col], 'direction' => $dir);
     }
     $pager = null;
     if (!$action) {
         // 直接搜索所有
         if ($is_all) {
             $page_size = UserModel::getCount();
         }
         $page = $page_offset / $page_size + 1;
         $pager = UserModel::listForumUsers($page, $page_size, function (QueryBuilder $qb) use($sorts) {
             if (!empty($sorts)) {
                 foreach ($sorts as $sort) {
                     $field = "`{$sort['field']}`";
                     $direction = $sort['direction'];
                     $qb->addOrderBy($field, $direction);
                 }
             }
         });
     } elseif ($action == 'filter') {
         $condition_callback = function (QueryBuilder $qb) use($sorts, $posts, $filter_columns) {
             if (!empty($sorts)) {
                 foreach ($sorts as $sort) {
                     $field = "`{$sort['field']}`";
                     $direction = $sort['direction'];
                     $qb->addOrderBy($field, $direction);
                 }
                 foreach ($filter_columns as $field) {
                     $value = $posts->get($field);
                     if (!empty($value)) {
                         if ($field == 'create_date_from') {
                             $qb->andWhere($qb->expr()->gte("`{$field}`", ":{$field}"))->setParameter(":{$field}", strtotime($value));
                         } elseif ($field == 'create_date_to') {
                             $qb->andWhere($qb->expr()->lte("`{$field}`", ":{$field}"))->setParameter(":{$field}", strtotime($value));
                         } elseif ($field == 'login_date_from') {
                             $qb->andWhere($qb->expr()->gte("`{$field}`", ":{$field}"))->setParameter(":{$field}", strtotime($value));
                         } elseif ($field == 'login_date_to') {
                             $qb->andWhere($qb->expr()->lte("`{$field}`", ":{$field}"))->setParameter(":{$field}", strtotime($value));
                         } else {
                             $qb->andWhere($qb->expr()->eq("`{$field}`", ":{$field}"))->setParameter(":{$field}", $value);
                         }
                     }
                 }
             }
         };
         if ($is_all) {
             $page_size = UserModel::getCount($condition_callback);
         }
         $page = $page_offset / $page_size + 1;
         $pager = UserModel::listForumUsers($page, $page_size, $condition_callback);
     }
     if (isset($pager)) {
         $total = $pager->getCount();
         $records['recordsTotal'] = $total;
         $records['recordsFiltered'] = $total;
         $users = $pager->getData();
         foreach ($users as $user) {
             $records["data"][] = array('<input type="checkbox" name="id[]" value="' . $user['id'] . '">', $user['id'], $user['username'], $user['nickname'], $user['email'], $user['mobile'], date('Y-m-d H:i:s', $user['create_timestamp']), date('Y-m-d H:i:s', $user['login_timestamp']), '<a data-target="#modal"
                     data-toggle="modal"
                     href="' . $this->generateUrl('admin_forum_user_edit', array('user_id' => $user['id'])) . '"
                     class="btn btn-xs default"><i class="fa fa-search"></i> 编辑</a>');
         }
     }
     return new JsonResponse($records);
 }
Exemplo n.º 9
0
 /**
  * 保存
  * @param UserModel $user
  * @return UserModel
  * @throws \Exception
  */
 public static function saveUser(UserModel $user)
 {
     return self::editUser($user->toArray());
 }