protected function error($error = null, $code = 200) { $this->builder->createPartial('body', 'error')->getParamManager()->setParams(['error' => $error === null ? _i('We encountered an unexpected error.') : $error]); $this->response->setStatusCode($code); if ($this->response instanceof StreamedResponse) { $this->response->setCallback(function () { $this->builder->stream(); }); } else { $this->response->setContent($this->builder->build()); } return $this->response; }
/** * Renders a view and returns a Response. * * To stream a view, pass an instance of StreamedResponse as a third argument. * * @param string $view The view name * @param array $parameters An array of parameters to pass to the view * @param Response $response A Response instance * * @return Response A Response instance */ public function render($view, array $parameters = array(), Response $response = null) { $twig = $this['twig']; if ($response instanceof StreamedResponse) { $response->setCallback(function () use($twig, $view, $parameters) { $twig->display($view, $parameters); }); } else { if (null === $response) { $response = new Response(); } $response->setContent($twig->render($view, $parameters)); } return $response; }
public function render($view, array $parameters = [], Response $response = null) { /** @var \Twig_Environment $twig */ $twig = $this->app['twig']; if ($response instanceof StreamedResponse) { $response->setCallback(function () use($twig, $view, $parameters) { $twig->display($view, $parameters); }); return $response; } if (null === $response) { $response = new Response(); } $response->setContent($twig->render($view, $parameters)); return $response; }
/** * Renders a view and returns a Response. * * To stream a view, pass an instance of StreamedResponse as a third argument. * * @param string $view The view name * @param array $parameters An array of parameters to pass to the view * @param Response $response A Response instance * * @return Response A Response instance */ public function render($view, array $parameters = [], Response $response = null) { $twig = $this['twig']; if ($response instanceof StreamedResponse) { $response->setCallback(function () use($twig, $view, $parameters) { $twig->display($view, $parameters); }); return $response; } if (null === $response) { $response = new Response(); } if ($this->hasCachingEnabled()) { $response->setTtl($this['http_cache.default_ttl']); } $response->setContent($twig->render($view, $parameters)); return $response; }
/** * Generate a response object for a cached image * * @param String $path Path to the cached image * @return Boolean */ private function serveFromCache($path) { //try and load the image from the cache if ($this->cache->has($path)) { $file = $this->cache->get($path); $lastModified = new \DateTime(); $lastModified->setTimestamp($file->getTimestamp()); $this->setHttpCacheHeaders($lastModified, md5($this->getCachePath() . $lastModified->getTimestamp()), $this->maxAge); // Respond with 304 not modified if ($this->response->isNotModified($this->request)) { return true; } $this->response = new StreamedResponse(); // Set the headers $this->response->headers->set('Content-Type', $file->getMimetype()); $this->response->headers->set('Content-Length', $file->getSize()); $this->setHttpCacheHeaders($lastModified, md5($this->getCachePath() . $lastModified->getTimestamp()), $this->maxAge); $this->response->setCallback(function () use($file) { fpassthru($file->readStream()); }); return true; } return false; }
public function radix_search() { if ($this->getPost('submit_search_global')) { $this->radix = null; } $text = $this->getPost('text'); if ($this->radix !== null && $this->getPost('submit_post')) { return $this->radix_post(str_replace(',', '_', $text)); } $this->response = new StreamedResponse(); // Check all allowed search modifiers and apply only these $modifiers = ['boards', 'subject', 'text', 'username', 'tripcode', 'email', 'filename', 'capcode', 'uid', 'image', 'deleted', 'ghost', 'type', 'filter', 'start', 'end', 'order', 'page']; if ($this->getAuth()->hasAccess('comment.see_ip')) { $modifiers[] = 'poster_ip'; $modifiers[] = 'deletion_mode'; } // GET -> URL Redirection to provide URL presentable for sharing links. if ($this->getPost()) { if ($this->radix !== null) { $redirect_url = [$this->radix->shortname, 'search']; } else { $redirect_url = ['_', 'search']; } foreach ($modifiers as $modifier) { if ($this->getPost($modifier)) { if ($modifier === 'image') { array_push($redirect_url, $modifier); array_push($redirect_url, rawurlencode(Media::urlsafe_b64encode(Media::urlsafe_b64decode($this->getPost($modifier))))); } elseif ($modifier === 'boards') { if ($this->getPost('submit_search_global')) { } elseif (count($this->getPost($modifier)) == 1) { $boards = $this->getPost($modifier); $redirect_url[0] = $boards[0]; } elseif (count($this->getPost($modifier)) > 1) { $redirect_url[0] = '_'; // avoid setting this if we're just searching on all the boards $sphinx_boards = []; foreach ($this->radix_coll->getAll() as $k => $b) { if ($b->sphinx) { $sphinx_boards[] = $b; } } if (count($sphinx_boards) !== count($this->getPost($modifier))) { array_push($redirect_url, $modifier); array_push($redirect_url, rawurlencode(implode('.', $this->getPost($modifier)))); } } } else { array_push($redirect_url, $modifier); array_push($redirect_url, rawurlencode($this->getPost($modifier))); } } } return new RedirectResponse($this->uri->create($redirect_url), 303); } $search = $this->uri->uri_to_assoc($this->request->getPathInfo(), 1, $modifiers); $this->param_manager->setParam('search', $search); // latest searches system if (!is_array($cookie_array = @json_decode($this->getCookie('search_latest_5'), true))) { $cookie_array = []; } // sanitize foreach ($cookie_array as $item) { // all subitems must be array, all must have 'board' if (!is_array($item) || !isset($item['board'])) { $cookie_array = []; break; } } $search_opts = array_filter($search); $search_opts['board'] = $this->radix !== null ? $this->radix->shortname : false; unset($search_opts['page']); // if it's already in the latest searches, remove the previous entry foreach ($cookie_array as $key => $item) { if ($item === $search_opts) { unset($cookie_array[$key]); break; } } // we don't want more than 5 entries for latest searches if (count($cookie_array) > 4) { array_pop($cookie_array); } array_unshift($cookie_array, $search_opts); $this->builder->getPartial('tools_search')->getParamManager()->setParam('latest_searches', $cookie_array); $this->response->headers->setCookie(new Cookie($this->getContext(), 'search_latest_5', json_encode($cookie_array), 60 * 60 * 24 * 30)); foreach ($search as $key => $value) { if ($value !== null) { $search[$key] = trim(rawurldecode($value)); } } if ($search['boards'] !== null) { $search['boards'] = explode('.', $search['boards']); } if ($search['image'] !== null) { $search['image'] = base64_encode(Media::urlsafe_b64decode($search['image'])); } if ($this->getAuth()->hasAccess('comment.see_ip') && $search['poster_ip'] !== null) { if (!filter_var($search['poster_ip'], FILTER_VALIDATE_IP)) { return $this->error(_i('The poster IP you inserted is not a valid IP address.')); } $search['poster_ip'] = Inet::ptod($search['poster_ip']); } try { $board = Search::forge($this->getContext())->getSearch($search)->setRadix($this->radix)->setPage($search['page'] ? $search['page'] : 1); $board->getComments(); } catch (\Foolz\Foolfuuka\Model\SearchException $e) { return $this->error($e->getMessage()); } catch (\Foolz\Foolfuuka\Model\BoardException $e) { return $this->error($e->getMessage()); } catch (\Foolz\SphinxQL\ConnectionException $e) { return $this->error($this->preferences->get('foolfuuka.sphinx.custom_message', 'It appears that the search engine is offline at the moment. Please try again later.')); } // Generate the $title with all search modifiers enabled. $title = []; if ($search['text']) { array_push($title, sprintf(_i('that contain ‘%s’'), e($search['text']))); } if ($search['subject']) { array_push($title, sprintf(_i('with the subject ‘%s’'), e($search['subject']))); } if ($search['username']) { array_push($title, sprintf(_i('with the username ‘%s’'), e($search['username']))); } if ($search['tripcode']) { array_push($title, sprintf(_i('with the tripcode ‘%s’'), e($search['tripcode']))); } if ($search['filename']) { array_push($title, sprintf(_i('with the filename ‘%s’'), e($search['filename']))); } if ($search['image']) { array_push($title, sprintf(_i('with the image hash ‘%s’'), e($search['image']))); } if ($search['deleted'] == 'deleted') { array_push($title, _i('that have been deleted')); } if ($search['deleted'] == 'not-deleted') { array_push($title, _i('that has not been deleted')); } if ($search['ghost'] == 'only') { array_push($title, _i('that are by ghosts')); } if ($search['ghost'] == 'none') { array_push($title, _i('that are not by ghosts')); } if ($search['type'] == 'sticky') { array_push($title, _i('that were stickied')); } if ($search['type'] == 'op') { array_push($title, _i('that are only OP posts')); } if ($search['type'] == 'posts') { array_push($title, _i('that are only non-OP posts')); } if ($search['filter'] == 'image') { array_push($title, _i('that do not contain images')); } if ($search['filter'] == 'text') { array_push($title, _i('that only contain images')); } if ($search['capcode'] == 'user') { array_push($title, _i('that were made by users')); } if ($search['capcode'] == 'mod') { array_push($title, _i('that were made by mods')); } if ($search['capcode'] == 'admin') { array_push($title, _i('that were made by admins')); } if ($search['start']) { array_push($title, sprintf(_i('posts after %s'), e($search['start']))); } if ($search['end']) { array_push($title, sprintf(_i('posts before %s'), e($search['end']))); } if ($search['order'] == 'asc') { array_push($title, _i('in ascending order')); } if (!empty($title)) { $title = sprintf(_i('Searching for posts %s.'), implode(' ' . _i('and') . ' ', $title)); } else { $title = _i('Displaying all posts with no filters applied.'); } if ($this->radix) { $this->builder->getProps()->addTitle($title); } else { $this->builder->getProps()->addTitle('Global Search » ' . $title); } if ($board->getSearchCount() > 5000) { $search_title = sprintf(_i('%s <small>Returning only first %d of %d results found.</small>', $title, $this->preferences->get('foolfuuka.sphinx.max_matches', 5000), $board->getSearchCount())); } else { $search_title = sprintf(_i('%s <small>%d results found.</small>', $title, $board->getSearchCount())); } $this->param_manager->setParam('section_title', $search_title); $main_partial = $this->builder->createPartial('body', 'board'); $main_partial->getParamManager()->setParam('board', $board->getComments()); $pagination = $search; unset($pagination['page']); $pagination_arr = []; $pagination_arr[] = $this->radix !== null ? $this->radix->shortname : '_'; $pagination_arr[] = 'search'; foreach ($pagination as $key => $item) { if ($item || $item === 0) { $pagination_arr[] = rawurlencode($key); if (is_array($item)) { $item = implode('.', $item); } if ($key == 'poster_ip') { $item = Inet::dtop($item); } $pagination_arr[] = rawurlencode($item); } } $pagination_arr[] = 'page'; $this->param_manager->setParam('pagination', ['base_url' => $this->uri->create($pagination_arr), 'current_page' => $search['page'] ?: 1, 'total' => ceil($board->getCount() / 25)]); $this->param_manager->setParam('modifiers', ['post_show_board_name' => $this->radix === null, 'post_show_view_button' => true]); $this->profiler->logMem('Controller Chan $this', $this); $this->profiler->log('Controller Chan::search End'); $this->response->setCallback(function () { $this->builder->stream(); }); return $this->response; }
/** * Makes a JSON response, with either data or an error, never both. * * @param array $array The data to wrap in the response. * @return Symfony\Component\HttpFoundation\Response */ private function response($array) { $json_encodeOptions = isset($this->config['jsonoptions']) ? $this->config['jsonoptions'] : JSON_PRETTY_PRINT; $json = json_encode($array, $json_encodeOptions); if (isset($array['errors'])) { $status = isset($array['errors']['status']) ? $array['errors']['status'] : 400; $response = new Response($json, $status); } else { $response = new Response($json, 201); } if (!empty($this->config['headers']) && is_array($this->config['headers'])) { foreach ($this->config['headers'] as $header => $value) { $response->headers->set($header, $value); } } if ($callback = $this->request->get('callback')) { $response->setCallback($callback); } return $response; }
/** * Renders a view and returns a Response. * * To stream a view, pass an instance of StreamedResponse as a third argument. * * @param string $view The view name * @param array $parameters An array of parameters to pass to the view * @param Response $response A Response instance * * @return Response A Response instance */ public function render($view, array $parameters = array(), Response $response = null) { $twig = $this['twig']; // twigファイルのソースコードを読み込み, 文字列化. $source = $twig->getLoader()->getSource($view); // イベントの実行. // プラグインにはテンプレートファイル名、文字列化されたtwigファイル、パラメータを渡す $event = new TemplateEvent($view, $source, $parameters, $response); $eventName = $view; if ($this->isAdminRequest()) { // 管理画面の場合、event名に「Admin/」を付ける $eventName = 'Admin/' . $view; } $this['monolog']->debug('Template Event Name : ' . $eventName); $this['eccube.event.dispatcher']->dispatch($eventName, $event); if ($response instanceof StreamedResponse) { $response->setCallback(function () use($twig, $view, $parameters) { $twig->display($view, $parameters); }); } else { if (null === $response) { $response = new Response(); } // プラグインで変更された文字列から, テンプレートオブジェクトを生成 $template = $twig->createTemplate($event->getSource()); // レンダリング実行. $content = $template->render($event->getParameters()); $response->setContent($content); } return $response; }