$v) { if ( (substr($k,0,8)=="CURLOPT_") && ($v==$curlopt)) return $k; } return false; } // Initialize a CURL emulation session function curl_init() { $i = $GLOBALS["_CURLNAT_OPT"]["index"]++; $GLOBALS["_CURLNAT_OPT"][$i] = array(); $GLOBALS["_CURLNAT_OPT"][$i]["http"] = &new HTTPRetriever(); $GLOBALS["_CURLNAT_OPT"][$i]["include_body"] = true; return $i; } // Set an option for a CURL emulation transfer function curl_setopt($ch,$option,$value) { $opt = &$GLOBALS["_CURLNAT_OPT"][$ch]; if (!$opt["args"]) $opt["args"] = array(); $args = &$opt["args"]; if (!$opt["settings"]) $opt["settings"] = array(); $settings = &$opt["settings"]; $http = &$opt["http"]; switch($option) { case CURLOPT_URL: $opt["url"] = $value; break; case CURLOPT_CUSTOMREQUEST: $opt["method"] = $value; break; case CURLOPT_REFERER: $http->headers["Referer"] = $value; break; case CURLOPT_NOBODY: $opt["include_body"] = $value==0; break; case CURLOPT_FAILONERROR: $opt["fail_on_error"] = $value>0; break; case CURLOPT_USERAGENT: $http->headers["User-Agent"] = $value; break; case CURLOPT_HEADER: $opt["include_headers"] = $value>0; break; case CURLOPT_RETURNTRANSFER: $opt["return_transfer"] = $value>0; break; case CURLOPT_TIMEOUT: $opt["max-time"] = (int) $value; break; case CURLOPT_HTTPHEADER: reset($value); foreach ($value as $k=>$header) { list($headername,$headervalue) = explode(":",$header); $http->headers[$headername] = ltrim($headervalue); } break; case CURLOPT_POST: $opt["post"] = $value>0; break; case CURLOPT_POSTFIELDS: $opt["postdata"] = $value; break; case CURLOPT_MUTE: // we're already mute, no? break; case CURLOPT_FILE: if (is_resource($value)) { $opt["output_handle"] = $value; } else { trigger_error("CURLOPT_FILE must specify a valid file resource",E_USER_WARNING); } break; case CURLOPT_WRITEHEADER: if (is_resource($value)) { $opt["header_handle"] = $value; } else { trigger_error("CURLOPT_WRITEHEADER must specify a valid file resource",E_USER_WARNING); } break; case CURLOPT_STDERR: // not implemented for now - not really relevant break; case CURLOPT_SSL_VERIFYPEER: case CURLOPT_SSL_VERIFYHOST: // these are automatically disabled using ssl:// anyway break; case CURLOPT_USERPWD: list($curl_user,$curl_pass) = explode(':',$value,2); $http->auth_username = $curl_user; $http->auth_password = $curl_pass; break; // Important stuff not implemented (as it's not yet supported by HTTPRetriever) case CURLOPT_PUT: case CURLOPT_INFILE: case CURLOPT_FOLLOWLOCATION: case CURLOPT_PROXYUSERPWD: case CURLOPT_COOKIE: case CURLOPT_COOKIEFILE: case CURLOPT_PROXY: case CURLOPT_RANGE: case CURLOPT_RESUME_FROM: // Things that cannot (reasonably) be implemented here case CURLOPT_LOW_SPEED_LIMIT: case CURLOPT_LOW_SPEED_TIME: case CURLOPT_KRB4LEVEL: case CURLOPT_SSLCERT: case CURLOPT_SSLCERTPASSWD: case CURLOPT_SSLVERSION: case CURLOPT_INTERFACE: case CURLOPT_CAINFO: case CURLOPT_TIMECONDITION: case CURLOPT_TIMEVALUE: // FTP stuff not implemented case CURLOPT_QUOTE: case CURLOPT_POSTQUOTE: case CURLOPT_UPLOAD: case CURLOPT_FTPLISTONLY: case CURLOPT_FTPAPPEND: case CURLOPT_FTPPORT: // Other stuff not implemented case CURLOPT_VERBOSE: case CURLOPT_NETRC: default: trigger_error("CURL emulation does not implement CURL option "._curlopt_name($option),E_USER_WARNING); break; } } // Perform a CURL emulation session function curl_exec($ch) { $opt = &$GLOBALS["_CURLNAT_OPT"][$ch]; $url = $opt["url"]; $http = &$opt["http"]; $http->disable_curl = true; // avoid problems with recursion, since we *ARE* CURL // set time limits if requested if ($opt["max-time"]) { $http->connect_timeout = $opt["max-time"]; $http->max_time = $opt["max-time"]; } if ($opt["post"]) { $res = $http->post($url,$opt["postdata"]); } elseif ($opt["method"]) { $res = $http->custom($opt["method"],$url,$opt["postdata"]); } else { $res = $http->get($url); } // check for errors $opt["errno"] = (!$res && $http->error) ? 1 : 0; if ($opt["errno"]) $opt["error"] = $http->error; // die if CURLOPT_FAILONERROR is set and the HTTP result code is greater than 300 if ($opt["fail_on_error"]) { if ($http->result_code>300) die; } $opt["stats"] = $http->stats; $headers = ""; foreach ($http->response_headers as $k=>$v) { $headers .= "$k: $v\r\n"; } // if a file handle was provided for header output, extract the headers // and write them to the handle if (isset($opt["header_handle"])) { fwrite($opt["header_handle"],$headers); } $output = ($opt["include_headers"] ? $headers."\r\n" : "") . ($opt["include_body"] ? $http->response : ""); // if a file handle was provided for output, write the output to it if (isset($opt["output_handle"])) { fwrite($opt["output_handle"],$output); // if the caller requested that the response be returned, return it } elseif ($opt["return_transfer"]) { return $output; // otherwise, just echo the output to stdout } else { echo $output; } return true; } function curl_close($ch) { $opt = &$GLOBALS["_CURLNAT_OPT"][$ch]; if ($opt["settings"]) { $settings = &$opt["settings"]; // if the user used CURLOPT_INFILE to specify a file to upload, remove the // temporary file created for the CURL binary if ($settings["upload-file"]["value"] && file_exists($settings["upload-file"]["value"])) unlink($settings["upload-file"]["value"]); } unset($GLOBALS["_CURLNAT_OPT"][$ch]); } function curl_errno($ch) { return (int) $GLOBALS["_CURLNAT_OPT"][$ch]["errno"]; } function curl_error($ch) { return $GLOBALS["_CURLNAT_OPT"][$ch]["error"]; } function curl_getinfo($ch,$opt=NULL) { if ($opt) { $curlinfo_tags = array( CURLINFO_EFFECTIVE_URL=>"url", CURLINFO_CONTENT_TYPE=>"content_type", CURLINFO_HTTP_CODE=>"http_code", CURLINFO_HEADER_SIZE=>"header_size", CURLINFO_REQUEST_SIZE=>"request_size", CURLINFO_FILETIME=>"filetime", CURLINFO_SSL_VERIFYRESULT=>"ssl_verify_result", CURLINFO_REDIRECT_COUNT=>"redirect_count", CURLINFO_TOTAL_TIME=>"total_time", CURLINFO_NAMELOOKUP_TIME=>"namelookup_time", CURLINFO_CONNECT_TIME=>"connect_time", CURLINFO_PRETRANSFER_TIME=>"pretransfer_time", CURLINFO_SIZE_UPLOAD=>"size_upload", CURLINFO_SIZE_DOWNLOAD=>"size_download", CURLINFO_SPEED_DOWNLOAD=>"speed_download", CURLINFO_SPEED_UPLOAD=>"speed_upload", CURLINFO_CONTENT_LENGTH_DOWNLOAD=>"download_content_length", CURLINFO_CONTENT_LENGTH_UPLOAD=>"upload_content_length", CURLINFO_STARTTRANSFER_TIME=>"starttransfer_time", CURLINFO_REDIRECT_TIME=>"redirect_time" ); $key = $curlinfo_tags[$opt]; return $GLOBALS["_CURLNAT_OPT"][$ch]["stats"][$key]; } else { return $GLOBALS["_CURLNAT_OPT"][$ch]["stats"]; } } function curl_version() { return "libcurlemu/".CURLNAT_VERSION."-nat"; } } ?>