[ Index ]
 

Code source de CMS made simple 1.0.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Search/ -> action.dosearch.php (source)

   1  <?php
   2  if (!isset($gCms)) exit;
   3  
   4  if ($params['searchinput'] != '')
   5  {
   6      // Fix to prevent XSS like behaviour. See: http://www.securityfocus.com/archive/1/455417/30/0/threaded
   7      $params['searchinput'] = htmlspecialchars($params['searchinput']);
   8      @$this->SendEvent('SearchInitiated', array(&$params['searchinput']));
   9      
  10      $searchstarttime = microtime();
  11  
  12      $smarty->assign('phrase', $params['searchinput']);
  13  
  14      $words = array_values($this->StemPhrase($params['searchinput']));
  15      $nb_words = count($words);
  16      $max_weight = 1;
  17  
  18      $searchphrase = '';
  19      if ($nb_words > 0)
  20      {
  21          #$searchphrase = implode(' OR ', array_fill(0, $nb_words, 'word = ?'));
  22          $ary = array();
  23          foreach ($words as $word)
  24          {
  25              $word = trim($word);
  26              $ary[] = "word = " . $db->qstr(htmlentities($word, ENT_COMPAT, 'UTF-8'));
  27          }
  28          $searchphrase = implode(' OR ', $ary);
  29      }
  30  
  31      $query = "SELECT DISTINCT i.module_name, i.content_id, i.extra_attr, COUNT(*) AS nb, SUM(idx.count) AS total_weight FROM ".cms_db_prefix()."module_search_items i INNER JOIN ".cms_db_prefix()."module_search_index idx ON idx.item_id = i.id WHERE (".$searchphrase.") AND  (".$db->IfNull('i.expires',$db->DBTimeStamp(100 * 100 * 100 * 100 * 25))." > ".$db->DBTimeStamp(time()).") GROUP BY i.module_name, i.content_id, i.extra_attr";
  32      //This makes it an AND query
  33      $query .= " HAVING count(*) = ".$nb_words;
  34      $query .= " ORDER BY nb DESC, total_weight DESC";
  35      $result =& $db->Execute($query);
  36  
  37      $rawary = array();
  38      $ary = array();
  39  
  40      $hm = &$gCms->GetHierarchyManager();
  41  
  42      $col = new SearchItemCollection();
  43  
  44      while ($result && !$result->EOF)
  45      {
  46          //Handle internal (templates, content, etc) first...
  47          if ($result->fields['module_name'] == $this->GetName())
  48          {
  49              if ($result->fields['extra_attr'] == 'content')
  50              {
  51                  //Content is easy... just grab it out of hierarchy manager and toss the url in
  52                  $node = &$hm->sureGetNodeById($result->fields['content_id']);
  53                  if (isset($node))
  54                  {
  55                      $content =& $node->GetContent();
  56                      if (isset($content))
  57                      {
  58                          $col->AddItem($content->Name(), $content->GetURL(), $content->Name(), $result->fields['total_weight']);
  59                      }
  60                  }
  61              }
  62              else if ($result->fields['extra_attr'] == 'template')
  63              {
  64                  //Templates are more invovled.  We now need to grab every page with said template
  65                  //and toss them into the list
  66                  $query = 'SELECT content_id FROM '.cms_db_prefix().'content WHERE template_id = ?';
  67                  $templateresult =& $db->Execute($query, array($result->fields['content_id']));
  68  
  69                  while ($templateresult && !$templateresult->EOF)
  70                  {
  71                      $node = &$hm->sureGetNodeById($templateresult->fields['content_id']);
  72                      if (isset($node))
  73                      {
  74                          $content =& $node->GetContent();
  75                          if (isset($content))
  76                          {
  77                              $col->AddItem($content->Name(), $content->GetURL(), $content->Name(), $result->fields['total_weight']);
  78                          }
  79                      }
  80                      $templateresult->MoveNext();
  81                  }
  82              }
  83              else if ($result->fields['extra_attr'] == 'global_content')
  84              {
  85                  //This is the most complicated.  Basically, it goes like this...
  86                  //1. Figure out what's cross referenced with this global_content
  87                  //2. If it's content, then we just return that
  88                  //3. If it's a template, then we do the same deal.  Figure out
  89                  //what pages are using that template and then return those
  90                  $query = 'SELECT parent_id, parent_type FROM '.cms_db_prefix().'crossref WHERE child_id = ? AND child_type = \'global_content\'';
  91                  $result2 = &$db->Execute($query, array($result->fields['content_id']));
  92  
  93                  while ($result2 && !$result2->EOF)
  94                  {
  95                      $type = $result2->fields['parent_type'];
  96                      $pid = $result2->fields['parent_id'];
  97  
  98                      if ($type == 'template')
  99                      {
 100                          $query = 'SELECT content_id FROM '.cms_db_prefix().'content WHERE template_id = ?';
 101                          $templateresult =& $db->Execute($query, array($pid));
 102  
 103                          while ($templateresult && !$templateresult->EOF)
 104                          {
 105                              $node = &$hm->sureGetNodeById($templateresult->fields['content_id']);
 106                              if (isset($node))
 107                              {
 108                                  $content =& $node->GetContent();
 109                                  if (isset($content))
 110                                  {
 111                                      $col->AddItem($content->Name(), $content->GetURL(), $content->Name(), $result->fields['total_weight']);
 112                                  }
 113                              }
 114                              $templateresult->MoveNext();
 115                          }
 116                      }
 117                      else if ($type == 'content')
 118                      {
 119                          $node = &$hm->sureGetNodeById($pid);
 120                          if (isset($node))
 121                          {
 122                              $content =& $node->GetContent();
 123                              if (isset($content))
 124                              {
 125                                  $col->AddItem($content->Name(), $content->GetURL(), $content->Name(), $result->fields['total_weight']);
 126                              }
 127                          }
 128                      }
 129  
 130                      $result2->MoveNext();
 131                  }
 132              }
 133          }
 134          else
 135          {
 136              //Start looking at modules...
 137              $modulename = $result->fields['module_name'];
 138              $moduleobj =& $this->GetModuleInstance($modulename);
 139              if ($moduleobj != FALSE)
 140              {
 141                  if (method_exists($moduleobj, 'SearchResult'))
 142                  {
 143                      $searchresult = $moduleobj->SearchResult( $returnid, $result->fields['content_id'], $result->fields['extra_attr']);
 144                      if (count($searchresult) == 3)
 145                      {
 146                          $col->AddItem($searchresult[0], $searchresult[2], $searchresult[1], $result->fields['total_weight']);
 147                      }
 148                  }
 149              }
 150          }
 151  
 152          $result->MoveNext();
 153      }
 154  
 155      $col->CalculateWeights();
 156      
 157      // now we're gonna do some post processing on the results
 158      // and replace the search terms with <span class="searchhilite">term</span>
 159      
 160      $results = $col->_ary;
 161      $words = explode( ' ', $params['searchinput'] );
 162      $newresults = array();
 163      foreach( $results as $result )
 164      {
 165          $title = $result->title;
 166          $txt = $result->urltxt;
 167          foreach( $words as $word )
 168          {
 169              $title = preg_replace('/('.$word.')/i', '<span class="searchhilite">$1</span>', $title);
 170              $txt = preg_replace('/('.$word.')/i', '<span class="searchhilite">$1</span>', $txt);
 171          }
 172          $result->title = $title;
 173          $result->urltxt = $txt;
 174          $newresults[] = $result;
 175      }
 176      $col->_ary = $newresults;
 177      
 178      @$this->SendEvent('SearchCompleted', array(&$params['searchinput'], &$col->_ary));
 179  
 180      $smarty->assign('results', $col->_ary);
 181      $smarty->assign('itemcount', count($col->_ary));
 182  
 183      $searchendtime = microtime();
 184      $smarty->assign('timetook', ($searchendtime - $searchstarttime));
 185  }
 186  else 
 187  {
 188      $smarty->assign('phrase', '');
 189      $smarty->assign('results', 0);
 190      $smarty->assign('itemcount', 0);
 191      $smarty->assign('timetook', 0);
 192  }
 193  
 194  $smarty->assign('searchresultsfor', $this->Lang('searchresultsfor'));
 195  $smarty->assign('noresultsfound', $this->Lang('noresultsfound'));
 196  $smarty->assign('timetaken', $this->Lang('timetaken'));
 197  
 198  echo $this->ProcessTemplateFromDatabase('displayresult');
 199  
 200  ?>


Généré le : Tue Apr 3 18:50:37 2007 par Balluche grâce à PHPXref 0.7