00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 class GyroHttpRequest {
00013 const NONE = 0;
00014 const SSL_NO_VERIFY = 1;
00015 const NO_ERROR_ON_4XX_5XX = 2;
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 public static function get_content ($url, $err = null, $timeout = 30, $policy = self::NONE, &$info = false) {
00028 $options = self::get_default_opts($policy);
00029 $ret = self::execute_curl($url, $options, $timeout, $err, $info);
00030 return $ret;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 public static function get_content_with_auth($url, $user, $pwd, $err = null, $timeout = 30, $policy = self::NONE, &$info = false) {
00046 $options = self::get_default_opts($policy);
00047 $options[CURLOPT_USERPWD] = "$user:$pwd";
00048 $ret = self::execute_curl($url, $options, $timeout, $err, $info);
00049 return $ret;
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 public static function get_head($url, $err = null, $timeout = 30, $policy = self::NONE) {
00061 $options = self::get_default_opts($policy);
00062
00063 $options[CURLOPT_HEADER] = 1;
00064
00065
00066 $options[CURLOPT_NOBODY] = 1;
00067 $ret = self::execute_curl($url, $options, $timeout, $err);
00068 return $ret;
00069 }
00070
00071
00072
00073
00074
00075
00076 public static function site_exists($url) {
00077 $ret = self::get_head($url);
00078 return !empty($ret);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 private static function get_default_opts($policy) {
00088 $ret = array(
00089 CURLOPT_RETURNTRANSFER => 1,
00090 CURLOPT_FAILONERROR => 1,
00091 CURLOPT_REFERER => Config::get_url(Config::URL_SERVER),
00092 CURLOPT_USERAGENT => Config::get_value(Config::TITLE). ' bot',
00093 CURLOPT_CONNECTTIMEOUT => 5,
00094 CURLOPT_FRESH_CONNECT => 1
00095 );
00096
00097 if (!ini_get('safe_mode')) {
00098 $ret[CURLOPT_FOLLOWLOCATION] = 1;
00099 }
00100
00101 if (Common::flag_is_set($policy, self::NO_ERROR_ON_4XX_5XX)) {
00102 $ret[CURLOPT_FAILONERROR] = 0;
00103 }
00104 if (Common::flag_is_set($policy, self::SSL_NO_VERIFY)) {
00105 $ret[CURLOPT_SSL_VERIFYHOST] = 0;
00106 $ret[CURLOPT_SSL_VERIFYPEER] = 0;
00107 }
00108
00109 return $ret;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 private static function execute_curl($url, $options, $timeout, $err, &$info = false) {
00122 $address = $url;
00123 if ($url instanceof Url) {
00124 $address = $url->build();
00125 }
00126 $options[CURLOPT_URL] = $address;
00127 if ($timeout > 0) {
00128 $options[CURLOPT_TIMEOUT] = $timeout;
00129 }
00130
00131 $ret = false;
00132 $ch = NULL;
00133
00134 $status = ($err) ? $err : new Status();
00135 $err_no = 0;
00136 try {
00137 $ch = curl_init();
00138 if (curl_setopt_array($ch, $options)) {
00139 $ret = curl_exec ($ch);
00140 $info = curl_getinfo($ch);
00141 if ($ret === false) {
00142 $status->append($address . ': ' . curl_error($ch));
00143 $err_no = curl_errno($ch);
00144 }
00145 }
00146 }
00147 catch (Exception $e) {
00148
00149 if ($ch) {
00150 $status->append($address . ': ' . curl_error($ch));
00151 $err_no = curl_errno($ch);
00152 $info = curl_getinfo($ch);
00153 }
00154 else {
00155 $status->append($address . ': ' . $e->getMessage());
00156 $err_no = 999;
00157 }
00158 $ret = false;
00159 }
00160 if ($ch) {
00161 @curl_close ($ch);
00162 }
00163 if (Config::has_feature(Config::LOG_HTTPREQUESTS)) {
00164 $log = array($address, $err_no, $status->to_string(Status::OUTPUT_PLAIN));
00165 Load::components('logger');
00166 Logger::log('httprequests', $log);
00167 }
00168 return $ret;
00169 }
00170 }
00171
00172 if (!class_exists('HttpRequest')) {
00173 class HttpRequest extends GyroHttpRequest {}
00174 }