PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/ReaderServer/trunk/sub.inc.php

http://enwikinews.googlecode.com/
PHP | 59 lines | 34 code | 12 blank | 13 comment | 2 complexity | e0cc89fb2d31bc7f5fb9808a62a4bb5a MD5 | raw file
  1. <?php
  2. require_once('config.inc.php');
  3. $IP = dirname( __FILE__ );
  4. /*****************************
  5. * Variables
  6. *****************************/
  7. $GLOBALS['fast'] = true;
  8. DEFINE('API_ROOT','http://en.wikinews.org/w/api.php?');
  9. DEFINE('INDEX_ROOT','http://en.wikinews.org/w/index.php?');
  10. /*****************************
  11. * DB Connect
  12. *****************************/
  13. $GLOBALS['con'] = mysqli_connect(DB_HOST,DB_USER,DB_PASS) or die("Cannot Connect to DB");
  14. mysqli_select_db($GLOBALS['con'], DB_DB);
  15. mysqli_autocommit($GLOBALS['con'], TRUE);
  16. // make sure all communication is in utf-8
  17. mysqli_set_charset($GLOBALS['con'], 'utf8');
  18. mysqli_query($GLOBALS['con'], "SET NAMES 'utf8'");
  19. /*****************************
  20. * Shared Subs
  21. *****************************/
  22. function curlIt($url, $post=0, $poststr=""){
  23. if(!$GLOBALS['fast']){randomSleep();} // Abuse protection
  24. $UA = "Mozilla/4.0 (compatible; WikinewsBot/1.0)";
  25. $ch = curl_init($url);
  26. //curl_setopt($ch, CURLOPT_VERBOSE, 1); //--Debug only
  27. $cookie_file = "/tmp/cookiejar.txt";
  28. curl_setopt($ch,CURLOPT_COOKIEJAR, $cookie_file);
  29. curl_setopt($ch,CURLOPT_COOKIEFILE, $cookie_file);
  30. if($post){
  31. curl_setopt($ch, CURLOPT_POST, 1);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
  33. }
  34. curl_setopt($ch, CURLOPT_USERAGENT, $UA);
  35. ob_start();
  36. curl_exec($ch);
  37. curl_close($ch);
  38. $retHTML = ob_get_contents();
  39. ob_end_clean();
  40. return($retHTML);
  41. }
  42. function randomSleep(){
  43. //Prevent a major hammering from bots
  44. // Rand 1 to 5, with .1 incriments
  45. $x = (double) ((mt_rand(10, 50)) / 10) * 1000000;
  46. usleep($x);
  47. }