PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/game_server/include/lib/curl.php

http://github.com/ericmuyser/mmo
PHP | 356 lines | 269 code | 79 blank | 8 comment | 35 complexity | cfe865293fded84be59b05f356a19417 MD5 | raw file
  1. <?php
  2. function reconstruct_url($url)
  3. {
  4. if(!$url)
  5. return NULL;
  6. if(!stristr($url, "http"))
  7. $url = "http://" . $url;
  8. $scheme = parse_url($url, PHP_URL_SCHEME);
  9. $host = parse_url($url, PHP_URL_HOST);
  10. $path = parse_url($url, PHP_URL_PATH);
  11. $q = parse_url($url, PHP_URL_QUERY);
  12. if($q)
  13. return $scheme . "://" . $host . $path . "?" . $q;
  14. else
  15. return $scheme . "://" . $host . $path;
  16. }
  17. class curl_request
  18. {
  19. public $options;
  20. public function __construct($default = true)
  21. {
  22. if($default)
  23. $this->set_default();
  24. }
  25. public function set_default()
  26. {
  27. $this->options[CURLOPT_RETURNTRANSFER] = true;
  28. $this->options[CURLOPT_FOLLOWLOCATION] = false;
  29. $this->options[CURLOPT_HEADER] = false;
  30. $this->options[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
  31. $this->options[CURLOPT_CONNECTTIMEOUT] = 60;
  32. $this->options[CURLOPT_TIMEOUT] = 60;
  33. $this->options[CURLOPT_CUSTOMREQUEST] = "GET";
  34. $this->options[CURLOPT_MAXREDIRS] = 4;
  35. }
  36. public function set_timeout($timeout)
  37. {
  38. $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
  39. $this->options[CURLOPT_TIMEOUT] = $timeout;
  40. }
  41. public function set_options($options)
  42. {
  43. foreach($options as $key => $value)
  44. $this->options[$key] = $value;
  45. }
  46. public function set_option($key, $value)
  47. {
  48. $this->options[$key] = $value;
  49. }
  50. public function get_option($key)
  51. {
  52. if(!isset($this->options[$key]))
  53. return NULL;
  54. return $this->options[$key];
  55. }
  56. public function get_options()
  57. {
  58. return $this->options;
  59. }
  60. public function set_url($url)
  61. {
  62. $this->options[CURLOPT_URL] = $url;
  63. if(strpos($url, "https") === 0)
  64. $this->options[CURLOPT_SSL_VERIFYPEER] = false;
  65. }
  66. public function set_referer($url)
  67. {
  68. $this->options[CURLOPT_REFERER] = $url;
  69. }
  70. public function enable_redirects()
  71. {
  72. $this->options[CURLOPT_FOLLOWLOCATION] = true;
  73. }
  74. public function set_authentication($username, $password)
  75. {
  76. $this->options[CURLOPT_USERPWD] = $username . ':' . $password;
  77. $this->options[CURLOPT_HTTPAUTH] = CURLAUTH_ANY;
  78. }
  79. public function set_cookies($file_path)
  80. {
  81. // clear the cookies
  82. //fclose(fopen($file_path, 'w'));
  83. $this->options[CURLOPT_COOKIEJAR] = $file_path;
  84. $this->options[CURLOPT_COOKIEFILE] = $file_path;
  85. }
  86. public function set_proxy($type, $host, $port, $username = NULL, $password = NULL)
  87. {
  88. if($type == "http")
  89. $this->options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
  90. else if($type == "socks4")
  91. $this->options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5;
  92. else if($type == "socks5")
  93. $this->options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5;
  94. $this->options[CURLOPT_PROXY] = $host . ":" . $port;
  95. if($username && $password)
  96. $this->options[CURLOPT_PROXYUSERPWD] = $username . ":" . $password;
  97. }
  98. public function set_post($data)
  99. {
  100. $this->options[CURLOPT_CUSTOMREQUEST] = "POST";
  101. $this->options[CURLOPT_POST] = true;
  102. $this->options[CURLOPT_POSTFIELDS] = $data;
  103. }
  104. public function set_get()
  105. {
  106. $this->options[CURLOPT_CUSTOMREQUEST] = "GET";
  107. $this->options[CURLOPT_POST] = false;
  108. $this->options[CURLOPT_POSTFIELDS] = '';
  109. }
  110. public function set_header($data)
  111. {
  112. $this->options[CURLOPT_HEADER] = true;
  113. $this->options[CURLOPT_HTTPHEADER] = $data;
  114. }
  115. };
  116. class curl_response
  117. {
  118. public $data;
  119. public $request;
  120. public $info;
  121. public $status_code;
  122. public $header_list;
  123. public function __construct()
  124. {
  125. $this->data = '';
  126. $this->info = array();
  127. $this->status_code = 0;
  128. $this->request = NULL;
  129. $this->header_list = array();
  130. }
  131. }
  132. class curl
  133. {
  134. public function __construct()
  135. {
  136. $this->setting_list = array("active" => true, "max_connections" => 10, "debug" => false);
  137. $this->connection_list = array();
  138. $this->job_list = array();
  139. $this->mc = curl_multi_init();
  140. }
  141. public function run($request, $callback = NULL)
  142. {
  143. $c = curl_init();
  144. foreach($request->get_options() as $key => $value)
  145. curl_setopt($c, $key, $value);
  146. // we've got a callback so let's go asynchronous
  147. if($callback)
  148. {
  149. $this->job_list[] = array("request" => $request, "handle" => $c, "callback" => $callback);
  150. }
  151. // nope, no asio for us today
  152. else
  153. {
  154. $r = new curl_response();
  155. $header_list = array();
  156. if(phpversion() >= 5.3)
  157. curl_setopt($c, CURLOPT_HEADERFUNCTION, function($c, $header) use(&$r)
  158. {
  159. if(strstr($header, ":"))
  160. {
  161. $h = explode(":", $header);
  162. $key = $h[0];
  163. array_shift($h);
  164. $r->header_list[$key] = implode(":", $h);
  165. }
  166. return strlen($header);
  167. });
  168. ob_start();
  169. $r->data = curl_exec($c);
  170. ob_end_clean();
  171. $r->request = $request;
  172. $r->info = curl_getinfo($c);
  173. $r->status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  174. curl_close($c);
  175. return $r;
  176. }
  177. $this->last_request = $request;
  178. }
  179. public function update()
  180. {
  181. if(!$this->setting_list['active'])
  182. return;
  183. while(count($this->connection_list) < $this->setting_list['max_connections'] && count($this->job_list) > 0)
  184. {
  185. $job = array_shift($this->job_list);
  186. $url = $job['request']->get_option(CURLOPT_URL);
  187. // rebuild the URL
  188. $url = reconstruct_url($url);
  189. $host = parse_url($url, PHP_URL_HOST);
  190. $job['request']->set_option(CURLOPT_URL, $url);
  191. // check if the domain is bad and will block multicurl
  192. if(!$this->is_host_active($host))
  193. {
  194. $response = new curl_response();
  195. $response->request = $job['request'];
  196. $response->status_code = 666;
  197. curl_close($job['handle']);
  198. if($job['callback'] != NULL)
  199. if(phpversion() >= 5.3)
  200. $job['callback']($response);
  201. else
  202. call_user_func_array($job['callback'], array($response));
  203. continue;
  204. }
  205. $this->connection_list[$job['handle']] = array("request" => $job['request'], "handle" => $job['handle'], "callback" => $job['callback']);
  206. curl_multi_add_handle($this->mc, $job['handle']);
  207. }
  208. while(($status = curl_multi_exec($this->mc, $running)) == CURLM_CALL_MULTI_PERFORM)
  209. usleep(20000);
  210. if($status != CURLM_OK)
  211. return;
  212. while($item = curl_multi_info_read($this->mc))
  213. {
  214. $handle = $item['handle'];
  215. $connection = $this->connection_list[$handle];
  216. $info = curl_getinfo($handle);
  217. $data = curl_multi_getcontent($handle);
  218. curl_multi_remove_handle($this->mc, $handle);
  219. unset($this->connection_list[$handle]);
  220. $response = new curl_response();
  221. $response->request = $connection['request'];
  222. $response->data = $data;
  223. $response->info = $info;
  224. $response->status_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  225. $this->last_response = $response;
  226. curl_close($handle);
  227. var_dump(count($this->connection_list));
  228. if($connection['callback'] != NULL)
  229. if(phpversion() >= 5.3)
  230. $connection['callback']($response);
  231. else
  232. call_user_func_array($connection['callback'], array($response));
  233. usleep(5000);
  234. }
  235. }
  236. public function is_host_active($host)
  237. {
  238. // if this isn't linux don't check it
  239. if(!stristr(PHP_OS, "linux"))
  240. return true;
  241. // if this is an IP don't check it
  242. if(long2ip(ip2long($host)) == $host)
  243. return true;
  244. if($this->setting_list['debug'])
  245. echo $host . "\n";
  246. $x1 = shell_exec("nslookup " . $host);
  247. return !stristr($x1, " find");
  248. }
  249. public function get_last_request()
  250. {
  251. return $this->last_request;
  252. }
  253. public function get_last_response()
  254. {
  255. return $this->last_response;
  256. }
  257. public function set_setting_list($setting_list)
  258. {
  259. foreach($setting_list as $name => $value)
  260. $this->setting_list[$name] = $value;
  261. }
  262. public function set_setting($name, $value)
  263. {
  264. $this->setting_list[$name] = $value;
  265. }
  266. public function get()
  267. {
  268. return $this->mc;
  269. }
  270. protected $setting_list;
  271. protected $mc;
  272. public $connection_list;
  273. public $job_list;
  274. protected $last_request;
  275. protected $last_response;
  276. }
  277. ?>