CMSimple_XH 開発者ドキュメント
Search.php
1 <?php
2 
16 /*
17  ======================================
18  CMSimple_XH 1.7.0
19  2017-07-02
20  based on CMSimple version 3.3 - December 31. 2009
21  For changelog, downloads and information please see http://www.cmsimple-xh.com
22  ======================================
23  -- COPYRIGHT INFORMATION START --
24  Based on CMSimple version 3.3 - December 31. 2009
25  Small - simple - smart
26  (c) 1999-2009 Peter Andreas Harteg - peter@harteg.dk
27 
28  This file is part of CMSimple_XH
29  For licence see notice in /cmsimple/cms.php
30  -- COPYRIGHT INFORMATION END --
31  ======================================
32  */
33 
34 namespace XH;
35 
46 class Search
47 {
53  private $searchString;
54 
60  private $words;
61 
67  public function __construct($searchString)
68  {
69  $this->searchString = $searchString;
70  }
71 
77  private function getWords()
78  {
79  if (!isset($this->words)) {
80  $words = explode(' ', $this->searchString);
81  $this->words = array();
82  foreach ($words as $word) {
83  $word = trim($word);
84  if ($word != '') {
85  if (class_exists('\Normalizer', false)
86  && method_exists('\Normalizer', 'normalize')
87  ) {
88  $word = \Normalizer::normalize($word);
89  }
90  $this->words[] = $word;
91  }
92  }
93  }
94  return $this->words;
95  }
96 
106  public function search()
107  {
108  global $c, $cf;
109 
110  $result = array();
111  $words = $this->getWords();
112  if (empty($words)) {
113  return $result;
114  }
115  foreach ($c as $i => $content) {
116  if (!hide($i) || $cf['show_hidden']['pages_search'] == 'true') {
117  $found = true;
118  $content = $this->prepareContent($content, $i);
119  foreach ($words as $word) {
120  if (utf8_stripos($content, $word) === false) {
121  $found = false;
122  break;
123  }
124  }
125  if ($found) {
126  $result[] = $i;
127  }
128  }
129  }
130  return $result;
131  }
132 
141  private function prepareContent($content, $pageIndex)
142  {
143  global $s;
144 
145  $vars = array('s', 'o', 'hjs', 'bjs', 'e', 'onload');
146  foreach ($vars as $var) {
147  $old[$var] = $GLOBALS[$var];
148  }
149  $s = $pageIndex;
150  $content = strip_tags(evaluate_plugincall($content));
151  foreach ($vars as $var) {
152  $GLOBALS[$var] = $old[$var];
153  }
154  if (method_exists('\Normalizer', 'normalize')) {
155  $content = \Normalizer::normalize($content);
156  }
157  return html_entity_decode($content, ENT_QUOTES, 'UTF-8');
158  }
159 
169  private function foundMessage($count)
170  {
171  global $tx;
172 
173  if ($count == 0) {
174  $key = 'notfound';
175  } elseif ($count == 1) {
176  $key = 'found_1';
177  } elseif (2 <= $count && $count <= 4) {
178  $key = 'found_2-4';
179  } else {
180  $key = 'found_5';
181  }
182  $message = sprintf($tx['search'][$key], $this->searchString, $count);
183  $message = XH_hsc($message);
184  $message = '<p>' . $message . '</p>';
185  return $message;
186  }
187 
200  public function render()
201  {
202  global $h, $u, $sn, $cf, $tx, $pd_router;
203 
204  $cf['meta']['robots'] = 'noindex, nofollow';
205  $o = '<h1>' . $tx['search']['result'] . '</h1>';
206  $words = $this->getWords();
207  $pages = $this->search();
208  $count = count($pages);
209  $o .= $this->foundMessage($count) . PHP_EOL;
210  if ($count > 0) {
211  $o .= '<ul>' . PHP_EOL;
212  $words = implode(' ', $words);
213  foreach ($pages as $i) {
214  $pageData = $pd_router->find_page($i);
215  $site = isset($pageData['title']) ? $pageData['title'] : '';
216  $title = XH_title($site, $h[$i]);
217  $url = $sn . '?' . $u[$i] . '&amp;search=' . urlencode($words);
218  $o .= ' <li><a href="' . $url . '">' . $title . '</a>';
219  $description = isset($pageData['description'])
220  ? $pageData['description'] : '';
221  if ($description != '') {
222  $o .= '<div>' . XH_hsc($description) . '</div>';
223  }
224  $o .= '</li>' . PHP_EOL;
225  }
226  $o .= '</ul>' . PHP_EOL;
227  }
228  return $o;
229  }
230 }
search()
Definition: Search.php:106
$s
Definition: cms.php:953
__construct($searchString)
Definition: Search.php:67
$i
Definition: cms.php:193
$title
Definition: cms.php:100
$cf
Definition: cms.php:272
hide($i)
Definition: functions.php:1100
$c
Definition: cms.php:964
$u
Definition: cms.php:990
$o
Definition: cms.php:113
$h
Definition: cms.php:977
$sn
Definition: cms.php:434
$tx
Definition: cms.php:363
XH_hsc($string)
Definition: functions.php:2204
render()
Definition: Search.php:200
utf8_stripos($haystack, $needle, $offset=0)
Definition: utf8.php:106
$pd_router
Definition: cms.php:929
evaluate_plugincall($text)
Definition: functions.php:186
XH_title($site, $subtitle)
Definition: functions.php:1869