Ejemplo n.º 1
0
 public function get_search()
 {
     // 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';
     }
     $search = [];
     foreach ($modifiers as $modifier) {
         $search[$modifier] = $this->getQuery($modifier, null);
     }
     foreach ($search as $key => $value) {
         if (in_array($key, $modifiers) && $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->response->setData(['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);
         foreach ($board->getCommentsUnsorted() as $comment) {
             $this->apify($comment);
         }
         $comments = $board->getComments();
         $this->response->setData($comments);
     } catch (\Foolz\Foolslide\Model\SearchException $e) {
         return $this->response->setData(['error' => $e->getMessage()]);
     } catch (\Foolz\Foolslide\Model\BoardException $e) {
         return $this->response->setData(['error' => $e->getMessage()])->setStatusCode(500);
     }
     return $this->response;
 }
Ejemplo n.º 2
0
 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\Foolslide\Model\SearchException $e) {
         return $this->error($e->getMessage());
     } catch (\Foolz\Foolslide\Model\BoardException $e) {
         return $this->error($e->getMessage());
     } catch (\Foolz\SphinxQL\ConnectionException $e) {
         return $this->error($this->preferences->get('foolslide.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('foolslide.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;
 }