123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- if (!defined('SMF'))
- die('Hacking attempt...');
- if (!defined('SMF'))
- die('Hacking attempt...');
- class curl_fetch_web_data
- {
- private $default_options = array(
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_HEADER => 1,
- CURLOPT_FOLLOWLOCATION => 0,
- CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko Firefox/11.0',
- CURLOPT_CONNECTTIMEOUT => 15,
- CURLOPT_TIMEOUT => 90,
- CURLOPT_MAXREDIRS => 5,
- CURLOPT_ENCODING => 'gzip,deflate',
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_POST => 0,
- );
-
- public function __construct($options = array(), $max_redirect = 3)
- {
-
- $this->max_redirect = intval($max_redirect);
- $this->user_options = $options;
- }
-
- public function get_url_data($url, $post_data = array())
- {
-
- if (!empty($post_data) && is_array($post_data))
- $this->post_data = $this->build_post_data($post_data);
- elseif (!empty($post_data))
- $this->post_data = trim($post_data);
-
- $this->set_options();
- $this->curl_request(str_replace(' ', '%20', $url));
- return $this;
- }
-
- private function curl_request($url, $redirect = false)
- {
-
- if ($url == '')
- return false;
- else
- $this->options[CURLOPT_URL] = $url;
-
- if (!$redirect)
- {
- $this->current_redirect = 1;
- $this->response = array();
- }
-
- $cr = curl_init();
- curl_setopt_array($cr, $this->options);
- curl_exec($cr);
-
- $curl_info = curl_getinfo($cr);
- $curl_content = curl_multi_getcontent($cr);
- $url = $curl_info['url'];
- $http_code = $curl_info['http_code'];
- $body = (!curl_error($cr)) ? substr($curl_content, $curl_info['header_size']) : false;
- $error = (curl_error($cr)) ? curl_error($cr) : false;
-
- curl_close($cr);
-
- $this->response[] = array(
- 'url' => $url,
- 'code' => $http_code,
- 'error' => $error,
- 'headers' => isset($this->headers) ? $this->headers : false,
- 'body' => $body,
- );
-
- if (preg_match('~30[127]~i', $http_code) === 1 && $this->headers['location'] != '' && $this->current_redirect <= $this->max_redirect)
- {
- $this->current_redirect++;
- $header_location = $this->get_redirect_url($url, $this->headers['location']);
- $this->redirect($header_location, $url);
- }
- }
-
- private function get_redirect_url($last_url = '', $new_url = '')
- {
-
- $last_url_parse = parse_url($last_url);
- $new_url_parse = parse_url($new_url);
-
- $new_url_parse['scheme'] = isset($new_url_parse['scheme']) ? $new_url_parse['scheme'] : $last_url_parse['scheme'];
- $new_url_parse['host'] = isset($new_url_parse['host']) ? $new_url_parse['host'] : $last_url_parse['host'];
- $new_url_parse['path'] = isset($new_url_parse['path']) ? $new_url_parse['path'] : $last_url_parse['path'];
- $new_url_parse['query'] = isset($new_url_parse['query']) ? $new_url_parse['query'] : '';
-
- return $new_url_parse['scheme'] . '://' . $new_url_parse['host'] . $new_url_parse['path'] . (!empty($new_url_parse['query']) ? '?' . $new_url_parse['query'] : '');
- }
-
- public function result($area = '')
- {
- $max_result = count($this->response) - 1;
-
- if ($area == '')
- return $this->response[$max_result];
- else
- return isset($this->response[$max_result][$area]) ? $this->response[$max_result][$area] : $this->response[$max_result];
- }
-
- public function result_raw($response_number = '')
- {
- if (!is_numeric($response_number))
- return $this->response;
- else
- {
- $response_number = min($response_number, count($this->response) - 1);
- return $this->response[$response_number];
- }
- }
-
- private function build_post_data($post_data)
- {
- if (is_array($post_data))
- {
- $postvars = array();
-
- foreach ($post_data as $name => $value)
- $postvars[] = $name . '=' . urlencode($value[0] == '@' ? '' : $value);
- return implode('&', $postvars);
- }
- else
- return $post_data;
- }
-
- private function set_options()
- {
-
- $this->default_options[CURLOPT_HEADERFUNCTION] = array($this, 'header_callback');
-
- if (is_array($this->user_options))
- {
- $keys = array_merge(array_keys($this->default_options), array_keys($this->user_options));
- $vals = array_merge($this->default_options, $this->user_options);
- $this->options = array_combine($keys, $vals);
- }
- else
- $this->options = $this->default_options;
-
- if (isset($this->post_data))
- {
- $this->options[CURLOPT_POST] = 1;
- $this->options[CURLOPT_POSTFIELDS] = $this->post_data;
- }
- }
-
- private function redirect($target_url, $referer_url)
- {
-
- $this->set_options();
- $this->options[CURLOPT_REFERER] = $referer_url;
- $this->curl_request($target_url, true);
- }
-
- private function header_callback($cr, $header)
- {
- $_header = trim($header);
- $temp = explode(': ', $_header, 2);
-
- if (isset($temp[0]) && isset($temp[1]))
- $this->headers[strtolower($temp[0])] = strtolower(trim($temp[1]));
-
- return strlen($header);
- }
- }
|