function &fetchfile($url, $target, $cacheTimeout = 0, $decode_utf8 = false, $sub = 'plugins') { global $serendipity; static $error = false; // Fix double URL strings. $url = preg_replace('@http(s)?:/@i', 'http\\1://', str_replace('//', '/', $url)); // --JAM: Get the URL's IP in the most error-free way possible $url_parts = @parse_url($url); $url_hostname = 'localhost'; if (is_array($url_parts)) { $url_hostname = $url_parts['host']; } $url_ip = gethostbyname($url_hostname); if (is_object($serendipity['logger'])) { $serendipity['logger']->debug(sprintf(PLUGIN_EVENT_SPARTACUS_FETCHING, '<a target="_blank" href="' . $url . '">' . basename($url) . '</a>')); } if (file_exists($target) && filesize($target) > 0 && filemtime($target) >= time() - $cacheTimeout) { $data = file_get_contents($target); if (is_object($serendipity['logger'])) { $serendipity['logger']->debug(sprintf(PLUGIN_EVENT_SPARTACUS_FETCHED_BYTES_CACHE, strlen($data), $target)); } } else { require_once S9Y_PEAR_PATH . 'HTTP/Request2.php'; $options = array('follow_redirects' => true, 'max_redirects' => 5); if (version_compare(PHP_VERSION, '5.6.0', '<')) { // On earlier PHP versions, the certificate validation fails. We deactivate it on them to restore the functionality we had with HTTP/Request1 $options['ssl_verify_peer'] = false; } serendipity_plugin_api::hook_event('backend_http_request', $options, 'spartacus'); serendipity_request_start(); $req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET, $options); try { $response = $req->send(); if ($response->getStatus() != '200') { throw new HTTP_Request2_Exception('Statuscode not 200, Akismet HTTP verification request failed.'); } } catch (HTTP_Request2_Exception $e) { $resolved_url = $url . ' (IP ' . $url_ip . ')'; $this->outputMSG('error', sprintf(PLUGIN_EVENT_SPARTACUS_FETCHERROR, $resolved_url)); //--JAM: START FIREWALL DETECTION if ($response->getStatus()) { $this->outputMSG('error', sprintf(PLUGIN_EVENT_SPARTACUS_REPOSITORY_ERROR, $response->getStatus())); } $check_health = true; if (function_exists('curl_init')) { $this->outputMSG('notice', PLUGIN_EVENT_SPARTACUS_TRYCURL); $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_HEADER, 0); $curl_result = curl_exec($curl_handle); curl_close($curl_handle); if ($curl_result) { $check_health = false; } else { $this->outputMSG('error', PLUGIN_EVENT_SPARTACUS_CURLFAIL . "\n"); } } } if ($check_health) { /*--JAM: Useful for later, when we have a health monitor for SPARTACUS $propbag = new serendipity_property_bag; $this->introspect($propbag); $health_url = 'http://spartacus.s9y.org/spartacus_health.php?version=' . $propbag->get('version'); */ // Garvin: Temporary health. Better than nothing, eh? $health_url = $url; $matches = array(); preg_match('#http://[^/]*/#', $url, $matches); if ($matches[0]) { $health_url = $matches[0]; } $mirrors = $this->getMirrors('files_health', true); $health_url = $mirrors[$health_url]; $this->outputMSG('notice', sprintf(PLUGIN_EVENT_SPARTACUS_HEALTHCHECK, $health_url)); $health_options = $options; serendipity_plugin_api::hook_event('backend_http_request', $health_options, 'spartacus_health'); $health_req = new HTTP_Request2($health_url, HTTP_Request2::METHOD_GET, $health_options); try { $health_result = $health_req->send(); if ($health_result->getStatus() != '200') { $this->outputMSG('error', sprintf(PLUGIN_EVENT_SPARTACUS_HEALTHERROR, $health_req->getResponseCode())); $this->outputMSG('notice', sprintf(PLUGIN_EVENT_SPARTACUS_HEALTHLINK, $health_url)); } else { $this->outputMSG('error', PLUGIN_EVENT_SPARTACUS_HEALTFIREWALLED); } } catch (HTTP_Request2_Exception $e) { $fp = @fsockopen('www.google.com', 80, $errno, $errstr); if (!$fp) { $this->outputMSG('error', sprintf(PLUGIN_EVENT_SPARTACUS_HEALTHBLOCKED, $errno, $errstr)); } else { $this->outputMSG('error', PLUGIN_EVENT_SPARTACUS_HEALTHDOWN); $this->outputMSG('notice', sprintf(PLUGIN_EVENT_SPARTACUS_HEALTHLINK, $health_url)); fclose($fp); } } //--JAM: END FIREWALL DETECTION if (file_exists($target) && filesize($target) > 0) { $data = file_get_contents($target); $this->outputMSG('success', sprintf(PLUGIN_EVENT_SPARTACUS_FETCHED_BYTES_CACHE, strlen($data), $target)); } } else { // Fetch file if (!$data) { $data = $response->getBody(); } if (is_object($serendipity['logger'])) { $serendipity['logger']->debug(sprintf(PLUGIN_EVENT_SPARTACUS_FETCHED_BYTES_URL, strlen($data), $target)); } $tdir = dirname($target); if (!is_dir($tdir) && !$this->rmkdir($tdir, $sub)) { $this->outputMSG('error', sprintf(FILE_WRITE_ERROR, $tdir)); return $error; } $fp = @fopen($target, 'w'); if (!$fp) { $this->outputMSG('error', sprintf(FILE_WRITE_ERROR, $target)); return $error; } if ($decode_utf8) { $data = str_replace('<?xml version="1.0" encoding="UTF-8" ?>', '<?xml version="1.0" encoding="' . LANG_CHARSET . '" ?>', $data); $this->decode($data, true); } fwrite($fp, $data); fclose($fp); $this->fileperm($target, false); $this->purgeCache = true; } serendipity_request_end(); } return $data; }
/** * Perform an interaction of a URL. * * @param string $url A URL. * @param string $pxhost The host name of a proxy. * If it is `null', it is not used. * @param int $pxport The port number of the proxy. * @param int $outsec Timeout in seconds. * If it is negative, it is not used. * @param array $reqheads An array of extension headers. * If it is `null', it is not used. * @param string $reqbody The pointer of the entitiy body of request. * If it is `null', "GET" method is used. * @param object $res EstraierPure_Response * an object into which headers and * the entity body of response are stored. * If it is `null', it is not used. * @return int The status code of the response. * On error, returns PEAR_Error. * @access public * @static * @uses PEAR * @uses HTTP_Request2 */ static function shuttle_url($url, $pxhost = null, $pxport = null, $outsec = -1, $reqheads = null, $reqbody = null, $res = null) { // HTTPS checking disabled. /*$https = preg_match('!^https://!i', $url); if ($https && !extension_loaded('openssl')) { $err = PEAR::raiseError('HTTPS is not supported.'); self::push_error($err); return $err; }*/ if (is_null($reqheads)) { $reqheads = array(); } $reqheads['User-Agent'] = sprintf('EstraierPure/%s (for PHP 5.1)', ESTRAIERPURE_VERSION); if (ESTRAIERPURE_USE_HTTP_STREAM) { // {{{ using stream functions // set request parameters $params = array('http' => array()); if (is_null($reqbody)) { $params['http']['method'] = 'GET'; } else { $params['http']['method'] = 'POST'; $params['http']['content'] = $reqbody; $reqheads['Content-Length'] = strlen($reqbody); } if (!is_null($pxhost)) { /*if ($https && version_compare(phpversion(), '5.1.0', 'lt')) { $err = PEAR::raiseError('HTTPS proxies are not supported.'); self::push_error($err); return $err; }*/ $params['http']['proxy'] = sprintf('tcp://%s:%d', $pxhost, $pxport); } $params['http']['header'] = ''; foreach ($reqheads as $key => $value) { $params['http']['header'] .= sprintf("%s: %s\r\n", $key, $value); } $context = stream_context_create($params); // open a stream and send the request $fp = fopen($url, 'r', false, $context); if (!$fp) { $err = PEAR::raiseError(sprintf('Cannot connect to %s.', $url)); self::push_error($err); return $err; } if ($outsec >= 0) { stream_set_timeout($fp, $outsec); } // process the response $meta_data = stream_get_meta_data($fp); if (strcasecmp($meta_data['wrapper_type'], 'cURL') == 0) { $errmsg = 'EstraierPure does not work with the cURL' . ' HTTP stream wrappers, please use PEAR::HTTP_Request2.'; $err = PEAR::raiseError($errmsg); self::push_error($err); return $err; } if (!empty($meta_data['timed_out'])) { $err = PEAR::raiseError('Connection timed out.'); self::push_error($err); return $err; } $first_header = array_shift($meta_data['wrapper_data']); if (!preg_match('!^HTTP/(.+?) (\\d+) ?(.*)!', $first_header, $matches)) { $err = PEAR::raiseError('Malformed response.'); self::push_error($err); return $err; } $code = intval($matches[2]); if ($res instanceof EstraierPure_Response) { if ($res->save_heads) { foreach ($meta_data['wrapper_data'] as $header) { list($name, $value) = explode(':', $header, 2); $res->add_head(strtolower($name), ltrim($value)); } } if ($res->save_body) { $res->set_body(stream_get_contents($fp)); } } // close the stream fclose($fp); // }}} } else { // {{{{ using PEAR::HTTP_Request2 // set request parameters $params = array(); $params['requestHeaders'] = $reqheads; if (isset($params['requestHeaders']['Content-Type'])) { unset($params['requestHeaders']['Content-Type']); $params['requestHeaders']['content-type'] = $reqheads['Content-Type']; } if (!is_null($pxhost)) { $params['proxy_host'] = $pxhost; $params['proxy_port'] = $pxport; } if ($outsec >= 0) { $params['timeout'] = floatval($outsec); $params['readTimeout'] = array($outsec, 0); } // create an instance of HTTP_Request2 $req = new HTTP_Request2($url, $params); if (is_null($reqbody)) { $req->setMethod('GET'); } else { $req->setMethod('POST'); $req->setBody($reqbody); } // send the request $err = $req->sendRequest(is_object($res) && !empty($res->save_body)); if (PEAR::isError($err)) { self::push_error($err); return $err; } $code = $req->getResponseCode(); // process the response if ($res instanceof EstraierPure_Response) { if ($res->save_heads) { $res->set_heads($req->getResponseHeader()); } if ($res->save_body) { $res->set_body($req->getResponseBody()); } } // }}} } return $code; }
<?php $s = new HTTP_Request2('http://www.smsstriker.com/API/sms.php?username=<username>&password=<password>&from=VVITGT&to=9xxxxxxxx&msg=hi testing&type=1', HTTP_Request2::METH_GET); try { $s->send(); if ($s->getResponseCode() == 200) { echo "sucess"; } } catch (HttpException $ex) { echo $ex; }