Example #1
0
 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)
         // 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
         // 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
         // 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
         // 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;
     }
 }
 function curl_setopt($ch, $option, $value)
 {
     $opt =& $GLOBALS["_CURLEXT_OPT"][$ch];
     if (!isset($opt["args"])) {
         $opt["args"] = array();
     }
     $args =& $opt["args"];
     if (!isset($opt["settings"])) {
         $opt["settings"] = array();
     }
     $settings =& $opt["settings"];
     switch ($option) {
         case CURLOPT_URL:
             $opt["url"] = $value;
             break;
         case CURLOPT_VERBOSE:
             $opt["verbose"] = $value > 0;
             break;
         case CURLOPT_USERPWD:
             if ($value === "") {
                 $value = false;
             }
             $settings["user"] = $value;
             break;
         case CURLOPT_PROXYUSERPWD:
             if ($value === "") {
                 $value = false;
             }
             $settings["proxy-user"] = $value;
             break;
         case CURLOPT_COOKIE:
             if ($value === "") {
                 $value = false;
             }
             if (is_bool($value) || strpos($value, "=") !== false) {
                 $settings["cookie"] = $value;
             }
             break;
         case CURLOPT_COOKIEFILE:
             if ($value === "") {
                 $value = false;
             }
             $settings["cookie"] = $value;
             break;
         case CURLOPT_COOKIEJAR:
             if ($value === "") {
                 $value = false;
             }
             $settings["cookie-jar"] = $value;
             break;
         case CURLOPT_CUSTOMREQUEST:
             if ($value === "") {
                 $value = false;
             }
             $settings["request"] = $value;
             break;
         case CURLOPT_PROXY:
             if ($value === "") {
                 $value = false;
             }
             $settings["proxy"] = $value;
             break;
         case CURLOPT_INTERFACE:
             if ($value === "") {
                 $value = false;
             }
             $settings["interface"] = $value;
             break;
         case CURLOPT_KRB4LEVEL:
             if ($value === "") {
                 $value = false;
             }
             $settings["krb4"] = $value;
             break;
         case CURLOPT_SSLCERT:
             $pass = "";
             if (is_string($settings["cert"])) {
                 list(, $pass) = explode(":", $settings["cert"]);
                 if (strlen($pass)) {
                     $pass = "******";
                 }
             }
             $settings["cert"] = $value . $pass;
             break;
         case CURLOPT_SSLCERTPASSWD:
             $filename = "";
             if (is_string($settings["cert"])) {
                 list($filename, ) = explode(":", $settings["cert"]);
             }
             $settings["cert"] = $filename . ":" . $value;
             break;
         case CURLOPT_RANGE:
             if ($value === "") {
                 $value = false;
             }
             $settings["range"] = $value;
             break;
         case CURLOPT_REFERER:
             if ($value === "") {
                 $value = false;
             }
             $settings["referer"] = $value;
             break;
         case CURLOPT_NOBODY:
             $settings["head"] = $value > 0;
             break;
         case CURLOPT_FAILONERROR:
             $opt["fail_on_error"] = $value > 0;
             break;
         case CURLOPT_USERAGENT:
             $settings["user-agent"] = $value;
             break;
         case CURLOPT_HEADER:
             $settings["include"] = $value > 0;
             break;
         case CURLOPT_RETURNTRANSFER:
             $opt["return_transfer"] = $value > 0;
             break;
         case CURLOPT_TIMEOUT:
             $settings["max-time"] = (int) $value;
             break;
         case CURLOPT_HTTPHEADER:
             reset($value);
             foreach ($value as $k => $header) {
                 $args[] = "header=" . $header;
             }
             break;
         case CURLOPT_POST:
             $settings["data"]["enabled"] = $value > 0;
             break;
         case CURLOPT_POSTFIELDS:
             if ($value === "") {
                 $value = false;
             }
             $settings["data"]["value"] = $value;
             break;
         case CURLOPT_SSL_VERIFYPEER:
             $settings["insecure"] = $value == 0;
             break;
         case CURLOPT_SSL_VERIFYHOST:
             // not supported by the commandline client
             break;
         case CURLOPT_FOLLOWLOCATION:
             $settings["location"] = $value > 0;
             break;
         case CURLOPT_PUT:
             $settings["upload-file"]["enabled"] = $value > 0;
             break;
         case CURLOPT_INFILE:
             if ($value === "") {
                 $value = false;
             }
             if (is_resource($value)) {
                 // Ugh, this is a terrible hack.  The CURL extension accepts a file handle, but
                 // the CURL binary obviously wants a filename.  Since you can't derive a filename
                 // from a file handle, we have to make a copy of the file from the file handle,
                 // then pass the temporary filename to the CURL binary.
                 $tmpfilename = tempnam("/tmp", "cif");
                 $fp = @fopen($tmpfilename, "w");
                 if (!$fp) {
                     trigger_error("CURL emulation library could not create a temporary file for CURLOPT_INFILE; upload aborted", E_USER_WARNING);
                 } else {
                     while (!feof($value)) {
                         $contents = fread($value, 8192);
                         fwrite($fp, $contents);
                     }
                     fclose($fp);
                     // if a temporary file was previously created, unlink it
                     if ($settings["upload-file"]["value"] && file_exists($settings["upload-file"]["value"])) {
                         unlink($settings["upload-file"]["value"]);
                     }
                     // set the new upload-file filename
                     $settings["upload-file"]["value"] = $tmpfilename;
                 }
             } else {
                 trigger_error("CURLOPT_INFILE must specify a valid file resource", E_USER_WARNING);
             }
             break;
         case CURLOPT_MUTE:
             // we're already mute, no?
             break;
         case CURLOPT_LOW_SPEED_LIMIT:
             $settings["speed-limit"] = (int) $value;
             break;
         case CURLOPT_LOW_SPEED_TIME:
             $settings["speed-time"] = (int) $value;
             break;
         case CURLOPT_RESUME_FROM:
             $settings["continue-at"] = (int) $value;
             break;
         case CURLOPT_CAINFO:
             if ($value === "") {
                 $value = false;
             }
             $settings["cacert"] = $value;
             break;
         case CURLOPT_SSLVERSION:
             $value = (int) $value;
             switch ($value) {
                 case 2:
                 case 3:
                     unset($settings["sslv2"]);
                     unset($settings["sslv3"]);
                     $settings["sslv" . $value] = true;
                     break;
             }
             break;
         case CURLOPT_TIMECONDITION:
             // untested - I'm lazy :)
             if (!isset($settings["time-cond"]["enabled"])) {
                 $settings["time-cond"]["enabled"] = false;
             }
             if (!$settings["time-cond"]["value"]) {
                 $settings["time-cond"]["value"] = 1;
             }
             $settings["time-cond"]["value"] = abs($settings["time-cond"]["value"]);
             if ($value == TIMECOND_ISUNMODSINCE) {
                 $settings["time-cond"]["value"] *= -1;
             }
             break;
         case CURLOPT_TIMEVALUE:
             // untested - I'm lazy :)
             if ($settings["time-cond"]["value"]) {
                 $sign = $settings["time-cond"]["value"] / abs($settings["time-cond"]["value"]);
             } else {
                 $sign = 1;
             }
             $settings["time-cond"]["value"] = (int) $value * $sign;
             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;
             // FTP stuff not implemented
         // 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
         // Other stuff not implemented
         case CURLOPT_NETRC:
         default:
             trigger_error("CURL emulation does not implement CURL option " . _curlopt_name($option), E_USER_WARNING);
             break;
     }
 }