View file yahoo/includes/classes/news_search_class.php

File size: 4.7Kb
<?

  /*

* Animated Collapsible DIV- © Dynamic Drive DHTML code library (www.pokazuha.tv)

* This notice MUST stay intact for legal use

* Visit Dynamic Drive at http://www.pokazuha.tv/ for this script and 100s more

* Last Update	: Tuesday, November 13 2005


*/

class NewsSearch
{
  var $m_url = 'search?cat=news&cs=utf8&q={keyword}&rys=0&_sb_lang=any';
  var $m_host = "www.alltheweb.com";
  var $m_settings;
  var $m_total_found_string;
  var $m_total_found_int;
  var $m_keywords;
  var $m_pager;
  var $m_is_no_resuls = 0;

  function NewsSearch($settings,$keywords)
  {
    $this->m_settings = $settings;
    $this->m_keywords = $keywords;
  }
  function file_get_contents($url)
  {
    if (check_if_bad_request($this->m_keywords))
    {
      return '';
    }
    $out = "GET /$url HTTP/1.1\r\n";
    $out .= "Host: {$this->m_host}\r\n";
    $out .= "User-Agent: Mozilla 4.0\r\n";
    $out .= "Connection: close\r\n\r\n";

    $h = fsockopen($this->m_host,80);
    fwrite($h,$out);
    for($a = 0,$r = '';!$a;)
    {
       $b = fread($h,8192);
       $r .= $b;
       $a = (($b=='') ? 1 : 0);
    }
    fclose($h);
    return $r;
  }

  function get_url()
  {
    if (_DEBUG == 1)
      return 'www/news.htm';
    else
    {
      $this->m_url = str_replace('{keyword}',$this->m_keywords,$this->m_url);
      $curr_page = isset($_GET['pos']) ? $_GET['pos'] : 1;
      if ($curr_page != 1)
      {
         $curr_page = $curr_page * 10 - 10;
         $this->m_url .= "&o=$curr_page";
       }
       if (isset($_GET['sort']) && ($_GET['sort'] == 'date'))
         $this->m_url .= "&sort=date";
      return $this->m_url;
    }
  }
  function get_total_found_string($content)
  {
    if ($this->m_is_no_resuls == 1)
      return "";
    preg_match('|(<span class="noFound">.*<span class="ofSoMany">(.*)</span>)|Ui',$content,$ret);
    $this->m_total_found_int = (int)str_replace(',','',$ret[2]);
    return $ret[1];
  }

  function get_results()
  {
    $url = $this->get_url();
    $content = $this->file_get_contents($url);
    $content = str_replace("\n"," ",$content);
    //Get total found string
    $this->m_total_found_string = $this->get_total_found_string($content);
    //Init pager
    $this->m_pager = new Pager($this->m_total_found_int,10,'news',$this->m_keywords);
    //Get sort opt for news
    $sort = isset($_GET['sort']) ? $_GET['sort'] : 'relevance';
    if ($sort == 'date')
      $sort_string = '<a href="?type=news&search='.htmlspecialchars($this->m_keywords).'&pos='.$this->m_pager->get_curr_search_page().'">по релевантности</a>&nbsp;-&nbsp;<strong>по дате</strong>';
    else
      $sort_string = '<strong>Relevance</strong>&nbsp;-&nbsp;<a href="?type=news&search='.htmlspecialchars($this->m_keywords).'&pos='.$this->m_pager->get_curr_search_page().'&sort=date">по дате</a>';

    $ret_data = '
    <div align="right">
      <font color="#FF8040"><b>Сортировка:</b></font> '.$sort_string.'
    </div>
    <div style="padding-left:20px;padding-top:20px;">
    ';
    //Get main content
    $content = preg_replace('|.*Web results|Ui','',$content);
    //Get result titles and urls
    if (!preg_match_all('|<span class="resTitle">(.*</a>)</span>|Ui',$content,$ret_titles))
    {
      $this->m_is_no_resuls = 1;
      return $this->m_settings->get_noresults_string();
    }
    //description
    if (!preg_match_all('|<span class="resTeaser">(.*)</span></p>|Ui',$content,$ret_desc))
    {
      $this->m_is_no_resuls = 1;
      return $this->m_settings->get_noresults_string();
    }
    //res date
    if (!preg_match_all('|<span class="resDate">(.*)</span>|Ui',$content,$ret_date))
    {
      $this->m_is_no_resuls = 1;
      return $this->m_settings->get_noresults_string();
    }
    foreach($ret_titles[1] as $index=>$link)
    {
      //Get title and url
      preg_match('|<a.*href="(.*)".*>(.*)</a>|Ui',$ret_titles[1][$index],$data);
      $preview = "<SCRIPT>ppreview('{$data[1]}','$index')</SCRIPT>
        <SCRIPT>potherfunctions('{$data[1]}',$index, '')</SCRIPT>
        ";
      $ret_data .= '
      <span class="resTitle">
      <a class="res" href="'.$data[1].'" target="_new">'.$data[2].'</a>&nbsp;-&nbsp;
      '.$preview.'
      </span>
      <span class="result">
      <br>
      '.$ret_desc[1][$index].'
      <br>
      <font color="green">'.$ret_date[0][$index].'</font>
      </span>
      <br><br>
      ';
    }
    $pager = $this->m_pager->get_pager();
    $ret_data .= '</div>
    <div style="padding-left:20px;">
    <DIV class=rnav><SPAN class=rnavLabel>Страницы:&nbsp;'.$pager.'</span></div>
    </div>
    ';
    return $ret_data;
  }

}
?>