/** * @Route("/{id}", name="@page/id", requirements={"id"="\d+"}) * @Response("extension://page/views/index.razr") */ public function indexAction($id = 0) { if (!($page = $this->pages->where(compact('id'))->where(['status' => Page::STATUS_PUBLISHED])->first())) { throw new NotFoundHttpException(__('Page not found!')); } if (!$page->hasAccess($this['user'])) { if (!$this['user']->isAuthenticated()) { return $this->redirect('@system/auth/login', ['redirect' => $this['url']->current()]); } throw new AccessDeniedHttpException(__('Unable to access this page!')); } $page->setContent($this['content']->applyPlugins($page->getContent(), ['page' => $page, 'markdown' => $page->get('markdown')])); return ['head.title' => __($page->getTitle()), 'page' => $page]; }
/** * @Request({"id": "int", "name"}, csrf=true) */ public function saveAction($id, $name) { try { if (!$name) { throw new Exception(__('Invalid menu name.')); } if (!($menu = $this->menus->find($id))) { $menu = new Menu(); } if ($this->menus->where(['name = ?', 'id <> ?'], [$name, $id])->first()) { throw new Exception(__('Invalid menu name. "%name%" is already in use.', ['%name%' => $name])); } $this->menus->save($menu, compact('name')); } catch (Exception $e) { $this['message']->error($e->getMessage()); } return $this->redirect('@system/menu', ['id' => isset($menu) ? $menu->getId() : 0]); }
/** * Gets the user roles. * * @param User $user * @return array */ protected function getRoles(User $user = null) { $roles = $this->roles->where(['id <> ?'], [Role::ROLE_ANONYMOUS])->orderBy('priority')->get(); foreach ($roles as $role) { if ($role->isAuthenticated()) { $role->disabled = true; } if ($user && $user->getId() == $this['user']->getId() && $user->isAdministrator() && $role->isAdministrator()) { $role->disabled = true; } } return $roles; }
/** * @Request({"id": "int", "alias", "source"}, csrf=true) */ public function saveAction($id, $alias, $source) { try { if (!($obj = $this->aliases->find($id))) { $obj = new Alias(); } if (!($alias = trim($alias, '/'))) { throw new Exception(__('Invalid alias.')); } if (!($source = trim($source, '/')) or strpos($source, '@') !== 0) { throw new Exception(__('Invalid source.')); } if ($this->aliases->where(['alias = ?', 'id <> ?'], [$alias, $id])->first()) { throw new Exception(__('The alias "%alias%" is already in use.', ['%alias%' => $alias])); } $this->aliases->save($obj, compact('alias', 'source')); $id = $obj->getId(); $this['message']->success($id ? __('Alias saved.') : __('Alias created.')); } catch (Exception $e) { $this['message']->error($e->getMessage()); } return $this->redirect($id ? '@system/alias/edit' : '@system/alias/add', compact('id')); }
/** * @Request({"id": "int", "page": "array"}, csrf=true) */ public function saveAction($id, $data) { try { if (!($page = $this->pages->find($id))) { $page = new Page(); } if ($this->pages->where(['url = ?', 'id <> ?'], [$data['url'], $page->getId()])->first()) { throw new Exception(__('Page Url not available.')); } $data['data'] = array_merge(['title' => 0, 'markdown' => 0], isset($data['data']) ? $data['data'] : []); $this->pages->save($page, $data); $response = ['message' => $id ? __('Page saved.') : __('Page created.'), 'id' => $page->getId()]; } catch (Exception $e) { $response = ['message' => $e->getMessage(), 'error' => true]; } return $this['response']->json($response); }
/** * @Route("/{id}", name="@blog/id") * @Response("extension://blog/views/post/post.razr") */ public function postAction($id = 0) { if (!($post = $this->posts->where(['id = ?', 'status = ?', 'date < ?'], [$id, Post::STATUS_PUBLISHED, new \DateTime()])->related('user')->first())) { return $this['response']->create(__('Post not found!'), 404); } if (!$post->hasAccess($this['user'])) { return $this['response']->create(__('Unable to access this post!'), 403); } $user = $this['user']; $query = $this->comments->query()->where(['status = ?'], [Comment::STATUS_APPROVED])->orderBy('created'); if ($user->isAuthenticated()) { $query->orWhere(function ($query) use($user) { $query->where(['status = ?', 'user_id = ?'], [Comment::STATUS_PENDING, $user->getId()]); }); } $this['db.em']->related($post, 'comments', $query); $post->setContent($this['content']->applyPlugins($post->getContent(), ['post' => $post, 'markdown' => $post->get('markdown')])); foreach ($post->getComments() as $comment) { $comment->setContent($this['content']->applyPlugins($comment->getContent(), ['comment' => true])); } return ['head.title' => __($post->getTitle()), 'post' => $post, 'params' => $this->extension->getParams()]; }
/** * @Request({"user": "******"}) * @Response("json") */ public function registerAction($data) { $response = ['success' => false]; $errors = []; try { if ($this['user']->isAuthenticated() || $this['option']->get('system:user.registration', 'admin') == 'admin') { return $this->redirect('/'); } if (!$this['csrf']->validate($this['request']->request->get('_csrf'))) { throw new Exception(__('Invalid token. Please try again.')); } $name = trim(@$data['name']); $username = trim(@$data['username']); $email = trim(@$data['email']); $password = @$data['password']; if (empty($name)) { $errors[] = ['field' => 'name', 'message' => __('Name required.')]; } if (empty($password)) { $errors[] = ['field' => 'password', 'message' => __('Password required.')]; } if (strlen($username) < 3 || !preg_match('/^[a-zA-Z0-9_\\-]+$/', $username)) { $errors[] = ['field' => 'username', 'message' => __('Username is invalid.')]; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors[] = ['field' => 'email', 'message' => __('Email is invalid.')]; } if ($this->users->query()->orWhere(['username = :username', 'email = :username'], ['username' => $username])->first()) { $errors[] = ['field' => 'username', 'message' => __('Username not available.'), 'dynamic' => true]; } if ($this->users->query()->orWhere(['username = :email', 'email = :email'], ['email' => $email])->first()) { $errors[] = ['field' => 'email', 'message' => __('Email not available.'), 'dynamic' => true]; } if (count($errors)) { throw new Exception(__('Signup failed')); } $user = new User(); $user->setRegistered(new \DateTime()); $user->setName($name); $user->setUsername($username); $user->setEmail($email); $user->setPassword($this['auth.password']->hash($password)); $user->setStatus(UserInterface::STATUS_BLOCKED); $user->setRoles($this->roles->where(['id' => RoleInterface::ROLE_AUTHENTICATED])->get()); $token = $this['auth.random']->generateString(32); $admin = $this['option']->get('system:user.registration') == 'approval'; if ($verify = $this['option']->get('system:user.require_verification')) { $user->setActivation($token); } elseif ($admin) { $user->setActivation($token); $user->set('verified', true); } else { $user->setStatus(UserInterface::STATUS_ACTIVE); } $this->users->save($user); if ($verify) { $this->sendVerificationMail($user); $response['success'] = __('Your user account has been created. Complete your registration, by clicking the link provided in the mail that has been sent to you.'); } elseif ($admin) { $this->sendApproveMail($user); $response['success'] = __('Your user account has been created and is pending approval by the site administrator.'); } else { $this->sendWelcomeEmail($user); $response['success'] = __('Your user account has been created.'); } if (!$response['success']) { $response['success'] = true; } if (!$this['request']->isXmlHttpRequest()) { $this['message']->success($response['success']); return $this->redirect('@system/auth/login'); } } catch (Exception $e) { if (!$this['request']->isXmlHttpRequest()) { foreach ($errors as $error) { $this['message']->error($error['message']); } } else { $response['errors'] = $errors; } } return $this['request']->isXmlHttpRequest() ? $response : $this->redirect(count($errors) ? '@system/registration' : '@system/auth/login'); }