Advertisement
Guest User

Stifler found this!!

a guest
Aug 10th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.51 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. +---------------------------------------------------------------------------
  5. |   PHP-IRC Google Search
  6. |   ========================================
  7. |     by Mad Clog
  8. |   (c) 2007-2009 by http://www.madclog.nl
  9. |   Contact:
  10. |    email: phpirc@madclog.nl
  11. |    msn:   gertjuhh@hotmail.com
  12. |    irc:   #madclog@irc.quakenet.org
  13. |   ========================================
  14. |   Changelog:
  15. |   0.1
  16. |    - Initial release
  17. |   0.2
  18. |    - Added image support
  19. |   0.3
  20. |    - Improved main search preg pattern
  21. |    - Added calculator support (also includes currency conversions)
  22. |    - Added config option for google extention
  23. |   0.4
  24. |    - Added video support
  25. |   0.4.1
  26. |    - Fixed calculated results
  27. |   0.4.2
  28. |    - Fixed video searches
  29. |   0.4.3
  30. |    - Fixed video searches (again...)
  31. |   0.4.4
  32. |    - Fixed all searches (google had a nice little update...)
  33. |   0.5.0
  34. |    - Added !define functionality
  35. |    - Did some MINOR code cleanup
  36. |   0.5.1
  37. |    - Fixed images searches (again...)
  38. +---------------------------------------------------------------------------
  39. |   > This program is free software; you can redistribute it and/or
  40. |   > modify it under the terms of the GNU General Public License
  41. |   > as published by the Free Software Foundation; either version 2
  42. |   > of the License, or (at your option) any later version.
  43. |   >
  44. |   > This program is distributed in the hope that it will be useful,
  45. |   > but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. |   > MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  47. |   > GNU General Public License for more details.
  48. |   >
  49. |   > You should have received a copy of the GNU General Public License
  50. |   > along with this program; if not, write to the Free Software
  51. |   > Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  52. +---------------------------------------------------------------------------
  53. */
  54.  
  55. class google_search extends module {
  56.    
  57.     public $title = 'Google Search';
  58.     public $author = 'Mad_Clog';
  59.     public $version = '0.5.1';
  60.    
  61.     public $max_results = 2;
  62.     public $max_image_results = 2;
  63.     public $max_video_results = 2;
  64.     public $response_type = 0; // 0 = channel; 1 = query/pm; 2 = notice
  65.     public $extension = 'com'; // Which google to search, eg 'com' 'nl' 'co.uk'
  66.    
  67.  
  68.     public function init() {
  69.         // we can't have more then 10 results
  70.         if ($this->max_results > 10)
  71.             $this->max_results = 10;
  72.         if ($this->max_image_results > 10)
  73.             $this->max_image_results = 10;
  74.         if ($this->max_video_results > 10)
  75.             $this->max_video_results = 10;
  76.     }
  77.    
  78.     public function destroy() {
  79.     }
  80.    
  81.     public function priv_google($line, $args) {
  82.         if ($args ['nargs'] < 1) {
  83.             $this->sendMsg ( $line, $args, 'You need to supply a search string' );
  84.             return;
  85.         }
  86.        
  87.         $getQuery = socket::generateGetQuery ( 'q=' . urlencode ( $args ['query'] ), 'www.google.' . $this->extension, '/search' );
  88.         if (strtolower ( substr ( $args ['cmd'], 1 ) ) == 'calc') {
  89.             $this->ircClass->addQuery ( 'www.google.' . $this->extension, 80, $getQuery, $line, $this, 'sendCalcResults' );
  90.         } else {
  91.             $this->ircClass->addQuery ( 'www.google.' . $this->extension, 80, $getQuery, $line, $this, 'sendSearchResults' );
  92.         }
  93.     }
  94.    
  95.     public function priv_define($line, $args) {
  96.         if ($args ['nargs'] < 1) {
  97.             $this->sendMsg ( $line, $args, 'You need to supply a search string' );
  98.             return;
  99.         }
  100.        
  101.         $getQuery = socket::generateGetQuery ( 'q=define:' . urlencode ( $args ['query'] ), 'www.google.' . $this->extension, '/search' );
  102.         $this->ircClass->addQuery ( 'www.google.' . $this->extension, 80, $getQuery, $line, $this, 'sendSearchResults' );
  103.     }
  104.    
  105.     public function sendSearchResults($line, $args, $result, $response) {
  106.         if ($result == QUERY_SUCCESS) {
  107.             if (strtolower ( substr ( $args ['cmd'], 1 ) ) == 'define') {
  108.                 $pattern = '#<li>([^<]+)#i';
  109.             } else {
  110.                 $pattern = '#<h3 class=r><a href="([^"]*)" class=l>(([^<]|<[^a][^ ])*)</a></h3>#i';
  111.             }
  112.            
  113.             $count = preg_match_all ( $pattern, $response, $matches, PREG_SET_ORDER );
  114.             if ($count == 0) {
  115.                 $this->sendMsg ( $line, $args, 'Your search - ' . BOLD . $args ['query'] . BOLD . ' - did not match any documents.' );
  116.                 return;
  117.             }
  118.            
  119.             $numResults = ($count < $this->max_results) ? $count : $this->max_results;
  120.             for($i = 0; $i < $numResults; $i ++) {
  121.                 $this->sendMsg ( $line, $args, strip_tags ( html_entity_decode ( $matches [$i] [2], ENT_QUOTES, 'utf-8' ) ) . ' - ' . $matches [$i] [1] );
  122.             }
  123.         } else {
  124.             $this->sendMsg ( $line, $args, 'Google says NO! (server didn\'t respond)' );
  125.         }
  126.     }
  127.    
  128.     public function sendCalcResults($line, $args, $result, $response) {
  129.         if ($result == QUERY_SUCCESS) {
  130.             $pattern = '#<h2 class=r><font size=\+1><b>(.*)</b></h2>#Ui';
  131.             $res = preg_match ( $pattern, $response, $match );
  132.             if ($res === 1) {
  133.                 $this->sendMsg ( $line, $args, 'Google calculator: ' . strip_tags ( html_entity_decode ( $match [1] ) ) );
  134.             } else {
  135.                 $this->sendMsg ( $line, $args, 'Your search - ' . BOLD . $args ['query'] . BOLD . ' - did not return a calculated result' );
  136.             }
  137.         } else {
  138.             $this->sendMsg ( $line, $args, 'Google says NO! (server didn\'t respond)' );
  139.         }
  140.     }
  141.    
  142.     public function priv_image($line, $args) {
  143.         if ($args ['nargs'] < 1) {
  144.             $this->sendMsg ( $line, $args, 'You need to supply a search string' );
  145.             return;
  146.         }
  147.        
  148.         $getQuery = socket::generateGetQuery ( 'q=' . urlencode ( $args ['query'] ), 'images.google.' . $this->extension, '/images' );
  149.         $this->ircClass->addQuery ( 'images.google.' . $this->extension, 80, $getQuery, $line, $this, 'sendImageResults' );
  150.     }
  151.    
  152.     public function sendImageResults($line, $args, $result, $response) {
  153.         if ($result == QUERY_SUCCESS) {
  154.             $pattern = '#\["([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)",\[([^\]]*)\],"([^"]*)"#i';
  155.             $count = preg_match_all ( $pattern, $response, $matches, PREG_SET_ORDER );
  156.             if ($count == 0) {
  157.                 $this->sendMsg ( $line, $args, 'Your search - ' . BOLD . $args ['query'] . BOLD . ' - did not match any documents.' );
  158.                 return;
  159.             }
  160.            
  161.             $numResults = ($count < $this->max_image_results) ? $count : $this->max_image_results;
  162.             for($i = 0; $i < $numResults; $i ++) {
  163.                 $this->sendMsg ( $line, $args, strip_tags ( html_entity_decode ( $matches [$i] [4] ) . ' (' . $matches [$i] [10] . ' - ' . $matches [$i] [12] . ')' ) );
  164.             }
  165.         } else {
  166.             $this->sendMsg ( $line, $args, 'Google says NO! (server didn\'t respond)' );
  167.         }
  168.     }
  169.    
  170.     public function priv_video($line, $args) {
  171.         if ($args ['nargs'] < 1) {
  172.             $this->sendMsg ( $line, $args, 'You need to supply a search string' );
  173.             return;
  174.         }
  175.        
  176.         $getQuery = socket::generateGetQuery ( 'q=' . urlencode ( $args ['query'] ) . '&hl=en', 'video.google.' . $this->extension, '/videosearch' );
  177.         $this->ircClass->addQuery ( 'video.google.' . $this->extension, 80, $getQuery, $line, $this, 'sendVideoResults' );
  178.     }
  179.    
  180.     public function sendVideoResults($line, $args, $result, $response) {
  181.         /* Array mapping
  182.         1: link
  183.         2: title
  184.         3: length
  185.         4: date
  186.         */
  187.         if ($result == QUERY_SUCCESS) {
  188.             $pattern = '#srcurl="(.*)".*<div class="rl-title".*><a.*>(.*)</a></div>.*<div class="rl-details">(?:<div .*</div>)?(\d.*) \-<span class="rl-date">(.*) - </span>#iU';
  189.            
  190.             $response = str_replace ( array ("\r", "\n" ), '', $response );
  191.             $count = preg_match_all ( $pattern, $response, $matches, PREG_SET_ORDER );
  192.             if ($count == 0) {
  193.                 $this->sendMsg ( $line, $args, 'Your search - ' . BOLD . $args ['query'] . BOLD . ' - did not match any documents.' );
  194.                 return;
  195.             }
  196.            
  197.             $numResults = ($count < $this->max_video_results) ? $count : $this->max_video_results;
  198.             for($i = 0; $i < $numResults; $i ++) {
  199.                 $this->sendMsg ( $line, $args, trim ( $matches [$i] [1] ) . ' - ' . BOLD . trim ( strip_tags ( html_entity_decode ( $matches [$i] [2] ) ) ) . BOLD . ' ( ' . trim ( $matches [$i] [3] ) . ' - ' . trim ( $matches [$i] [4] ) . ' )' );
  200.             }
  201.         } else {
  202.             $this->sendMsg ( $line, $args, 'Google says NO! (server didn\'t respond)' );
  203.         }
  204.     }
  205.    
  206.     public function sendMsg($line, $args, $message) {
  207.         switch ($this->response_type) {
  208.             case 0 :
  209.                 $this->ircClass->privMsg ( $line ['to'], $message );
  210.                 break;
  211.            
  212.             case 1 :
  213.                 $this->ircClass->privMsg ( $line ['fromNick'], $message );
  214.                 break;
  215.            
  216.             case 2 :
  217.                 $this->ircClass->notice ( $line ['fromNick'], $message );
  218.                 break;
  219.            
  220.             default :
  221.                 $this->ircClass->privMsg ( $line ['to'], $message );
  222.         }
  223.     }
  224.  
  225. }
  226.  
  227. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement