/** Récupèration d'informations à propos de la ressource désignée par $this->href. * * La récupération des infos se fait en cascade : * 1. the encoding given in the charset parameter of the Content-Type HTTP header, or * 2. the encoding given in the encoding attribute of the XML declaration within the document, or * 3. utf-8. * * @see http://diveintomark.org/archives/2004/02/13/xml-media-types * @todo Terminer la recherche d'infos */ function fetchUrlInfo() { // Envoi d'une requete vers l'URL require_once 'HTTP/Request.php'; $r = new HTTP_Request($this->href); $r->sendRequest(); // Récupération des informations contenues dans l'entête de la réponse. $url_info = $r->getResponseHeader(); // --- Détermination du Content-Type et du Charset de la page --- // // 1. D'apres un entete http if (isset($url_info['content-type'])) { // eg. text/html; charset=utf8 $pattern = '/^(\\w+\\/\\w+)(;?.\\w+=(.*))?/'; if (preg_match($pattern, $url_info['content-type'], $matches)) { $content_type = isset($matches[1]) ? $matches[1] : null; $charset = isset($matches[3]) ? $matches[3] : null; } } else { $charset = 'utf8'; } // Mise à jour des propriétés de l'objet en fonction des informations obtenues $this->type = $content_type; $this->charset = $charset; return true; }
/** * preprocess Index action. * * @access public * @return string Forward name (null if no errors.) */ function prepare() { if ($this->af->validate() == 0) { /// download file $url = sprintf('%s/repository.sphp', rtrim($this->af->get('repository_url'), '/')); $cache_file = $this->backend->ctl->repositoryURL2CacheFile($url); $repo_data = unserialize(file_get_contents($cache_file)); list($package, $version) = explode('@', $this->af->get('target_package')); $urls = array(); foreach ($repo_data as $package_name => $package_data) { if ($package_name == $package) { foreach ($package_data as $_pdata) { if ($_pdata['version'] == $version) { $urls = $_pdata['urls']; $filesize = $_pdata['size']; } } } } require_once 'HTTP/Request.php'; $req = new HTTP_Request(); $req->setMethod(HTTP_REQUEST_METHOD_HEAD); $command = 'no command'; foreach ($urls as $_url_data) { $_url = $_url_data['url']; $req->setURL($_url); $req->sendRequest(); if ($req->getResponseCode() == "302") { $headers = $req->getResponseHeader(); $req->setURL($headers['location']); } $req->sendRequest(); if ($req->getResponseCode() == '200') { $data_file = $this->backend->ctl->package2dataFile($package, $version); if ($this->fetchTgzFile($data_file, $req->getUrl())) { if (filesize($data_file) == $filesize || !$filesize) { chmod($data_file, 0666); return null; } } } } $this->ae->add('wget failed', _('file download failed.') . '[debug]' . sprintf('SIZE:[datafile,%d => repos,%d]', filesize($data_file), $filesize)); } return 'json_error_reload'; }
/** * @brief rss 주소로 부터 내용을 받아오는 함수 * * tistory 의 경우 원본 주소가 location 헤더를 뿜는다.(내용은 없음) 이를 해결하기 위한 수정 **/ function rss_request($rss_url) { // request rss $rss_url = Context::convertEncodingStr($rss_url); $URL_parsed = parse_url($rss_url); if (strpos($URL_parsed["host"], 'naver.com')) { $rss_url = iconv('UTF-8', 'euc-kr', $rss_url); } $rss_url = str_replace(array('%2F', '%3F', '%3A', '%3D', '%3B', '%26'), array('/', '?', ':', '=', ';', '&'), urlencode($rss_url)); $URL_parsed = parse_url($rss_url); $host = $URL_parsed["host"]; $port = $URL_parsed["port"]; if ($port == 0) { $port = 80; } $path = $URL_parsed["path"]; if ($URL_parsed["query"] != '') { $path .= "?" . $URL_parsed["query"]; } $oReqeust = new HTTP_Request($rss_url); $oReqeust->addHeader('Content-Type', 'application/xml'); $oReqeust->addHeader('User-agent', 'RSS Reader Widget (XE ' . __ZBXE_VERSION__ . ' (http://www.xpressengine.com); PEAR HTTP_Request class (http://pear.php.net))'); $oReqeust->setMethod('GET'); $user = $URL_parsed["user"]; $pass = $URL_parsed["pass"]; if ($user) { $oReqeust->setBasicAuth($user, $pass); } $oResponse = $oReqeust->sendRequest(); if (PEAR::isError($oResponse)) { return; } $header = $oReqeust->getResponseHeader(); if ($header['location']) { return $this->rss_request(trim($header['location'])); } else { return $oReqeust->getResponseBody(); } }
/** * Get the Pingback URL for a given URL * * @param string $url URL to get the Pingback URL for * @return string Pingback URL or empty string * */ function PNB_getPingbackUrl($url) { require_once 'HTTP/Request.php'; $retval = ''; $req = new HTTP_Request($url); $req->setMethod(HTTP_REQUEST_METHOD_HEAD); $req->addHeader('User-Agent', 'glFusion/' . GVERSION); $response = $req->sendRequest(); if (PEAR::isError($response)) { COM_errorLog('Pingback (HEAD): ' . $response->getMessage()); return false; } else { $retval = $req->getResponseHeader('X-Pingback'); } if (empty($retval)) { // search for <link rel="pingback"> $req = new HTTP_Request($url); $req->setMethod(HTTP_REQUEST_METHOD_GET); $req->addHeader('User-Agent', 'glFusion/' . GVERSION); $response = $req->sendRequest(); if (PEAR::isError($response)) { COM_errorLog('Pingback (GET): ' . $response->getMessage()); return false; } elseif ($req->getResponseCode() == 200) { $body = $req->getResponseBody(); // only search for the first match - it doesn't make sense to have // more than one pingback URL $found = preg_match("/<link rel=\"pingback\"[^>]*href=[\"']([^\"']*)[\"'][^>]*>/i", $body, $matches); if ($found === 1 && !empty($matches[1])) { $url = str_replace('&', '&', $matches[1]); $retval = urldecode($url); } } else { COM_errorLog('Pingback (GET): Got HTTP response code ' . $req->getResponseCode() . " when requesting {$url}"); return false; } } return $retval; }
/** * Retrieve Free/Busy data for the specified resource. * * @param string $resource Fetch the Free/Busy data for this resource. * * @return Horde_Icalendar_Vfreebusy The Free/Busy data. */ public function get($resource) { global $conf; $url = self::getUrl($resource); Horde::log(sprintf('Freebusy URL for resource %s is %s', $resource, $url), 'DEBUG'); list($user, $domain) = explode('@', $resource); if (empty($domain)) { $domain = $conf['kolab']['filter']['email_domain']; } /** * This section matches Kronolith_Freebusy and should be merged with it * again in a single Horde_Freebusy module. */ $options = array('method' => 'GET', 'timeout' => 5, 'allowRedirects' => true); if (!empty($conf['http']['proxy']['proxy_host'])) { $options = array_merge($options, $conf['http']['proxy']); } $http = new HTTP_Request($url, $options); $http->setBasicAuth($conf['kolab']['filter']['calendar_id'] . '@' . $domain, $conf['kolab']['filter']['calendar_pass']); @$http->sendRequest(); if ($http->getResponseCode() != 200) { throw new Horde_Kolab_Resource_Exception(sprintf('Unable to retrieve free/busy information for %s', $resource), Horde_Kolab_Resource_Exception::NO_FREEBUSY); } $vfb_text = $http->getResponseBody(); // Detect the charset of the iCalendar data. $contentType = $http->getResponseHeader('Content-Type'); if ($contentType && strpos($contentType, ';') !== false) { list(, $charset, ) = explode(';', $contentType); $vfb_text = Horde_String::convertCharset($vfb_text, trim(str_replace('charset=', '', $charset)), 'UTF-8'); } $iCal = new Horde_Icalendar(); $iCal->parsevCalendar($vfb_text, 'VCALENDAR'); $vfb =& $iCal->findComponent('VFREEBUSY'); if ($vfb === false) { throw new Horde_Kolab_Resource_Exception(sprintf('Invalid or no free/busy information available for %s', $resource), Horde_Kolab_Resource_Exception::NO_FREEBUSY); } $vfb->simplify(); return $vfb; }
function getResponseHeaders() { return $this->HttpRequest->getResponseHeader(); }
function _request($url, $body = null, $content_type = 'text/html', $method = 'GET', $headers = array(), $cookies = array()) { set_include_path(_XE_PATH_ . "libs/PEAR"); require_once 'PEAR.php'; require_once 'HTTP/Request.php'; $url_info = parse_url($url); $host = $url_info['host']; if (__PROXY_SERVER__ !== null) { $oRequest = new HTTP_Request(__PROXY_SERVER__); $oRequest->setMethod('POST'); $oRequest->addPostData('arg', serialize(array('Destination' => $url, 'method' => $method, 'body' => $body, 'content_type' => $content_type, "headers" => $headers))); } else { $oRequest = new HTTP_Request($url); if (count($headers)) { foreach ($headers as $key => $val) { $oRequest->addHeader($key, $val); } } if ($cookies[$host]) { foreach ($cookies[$host] as $key => $val) { $oRequest->addCookie($key, $val); } } if (!$content_type) { $oRequest->addHeader('Content-Type', 'text/html'); } else { $oRequest->addHeader('Content-Type', $content_type); } $oRequest->setMethod($method); if ($body) { $oRequest->setBody($body); } } $oResponse = $oRequest->sendRequest(); $code = $oRequest->getResponseCode(); $header = $oRequest->getResponseHeader(); $response = $oRequest->getResponseBody(); if ($c = $oRequest->getResponseCookies()) { foreach ($c as $k => $v) { $cookies[$host][$v['name']] = $v['value']; } } if ($code > 300 && $code < 399 && $header['location']) { return $this->_request($header['location'], $body, $content_type, $method, $headers, $cookies); } if ($code != 200) { return; } return $response; }
function fetchUrlPear($url, $request_parameters) { if (VERBOSE) { logEvent($url . ' fetching with PEAR'); } if (0 && $GLOBALS['has_pear_http_request'] == 2) { $headreq = new HTTP_Request2($url, $request_parameters); $headreq->setHeader('User-Agent', 'phplist v' . VERSION . 'p (http://www.phplist.com)'); } else { $headreq = new HTTP_Request($url, $request_parameters); $headreq->addHeader('User-Agent', 'phplist v' . VERSION . 'p (http://www.phplist.com)'); } if (!PEAR::isError($headreq->sendRequest(false))) { $code = $headreq->getResponseCode(); if ($code != 200) { logEvent('Fetching ' . $url . ' failed, error code ' . $code); return 0; } $header = $headreq->getResponseHeader(); if (preg_match('/charset=(.*)/i', $header['content-type'], $regs)) { $remote_charset = strtoupper($regs[1]); } $request_parameters['method'] = 'GET'; if (0 && $GLOBALS['has_pear_http_request'] == 2) { $req = new HTTP_Request2($url, $request_parameters); $req->setHeader('User-Agent', 'phplist v' . VERSION . 'p (http://www.phplist.com)'); } else { $req = new HTTP_Request($url, $request_parameters); $req->addHeader('User-Agent', 'phplist v' . VERSION . 'p (http://www.phplist.com)'); } logEvent('Fetching ' . $url); if (VERBOSE && function_exists('output')) { output('Fetching remote: ' . $url); } if (!PEAR::isError($req->sendRequest(true))) { $content = $req->getResponseBody(); if ($remote_charset != 'UTF-8' && function_exists('iconv')) { $content = iconv($remote_charset, 'UTF-8//TRANSLIT', $content); } } else { logEvent('Fetching ' . $url . ' failed on GET ' . $req->getResponseCode()); return 0; } } else { logEvent('Fetching ' . $url . ' failed on HEAD'); return 0; } return $content; }
public function extractPage($url, $match, $replace, $referer, $source, $ident = null) { global $_conf; $ret = array(); $source = @preg_replace('{' . $match . '}', $source, $url); $get_url = $referer; if ($this->extractErrors[$get_url]) { // 今回リクエストでエラーだった場合 return $this->cacheData[$url] && $this->cacheData[$url]['data'] ? $this->cacheData[$url]['data'] : $ret; } $params = array(); $params['timeout'] = $_conf['http_conn_timeout']; $params['readTimeout'] = array($_conf['http_read_timeout'], 0); if ($_conf['proxy_use']) { $params['proxy_host'] = $_conf['proxy_host']; $params['proxy_port'] = $_conf['proxy_port']; } $req = new HTTP_Request($get_url, $params); if ($this->cacheData[$url] && $this->cacheData[$url]['responseHeaders'] && $this->cacheData[$url]['responseHeaders']['last-modified'] && strlen($this->cacheData[$url]['responseHeaders']['last-modified'])) { $req->addHeader("If-Modified-Since", $this->cacheData[$url]['responseHeaders']['last-modified']); } $req->addHeader('User-Agent', !empty($_conf['expack.user_agent']) ? $_conf['expack.user_agent'] : $_SERVER['HTTP_USER_AGENT']); $response = $req->sendRequest(); $code = $req->getResponseCode(); if (PEAR::isError($response) || $code != 200 && $code != 206 && $code != 304) { $errmsg = PEAR::isError($response) ? $response->getMessage() : $code; // 今回リクエストでのエラーをオンラインキャッシュ $this->extractErrors[$get_url] = $errmsg; // サーバエラー以外なら永続キャッシュに保存 if ($code && $code < 500) { // ページが消えている場合 if ($this->_checkLost($url, $ret)) { return $this->cacheData[$url]['data']; } $this->storeCache($url, array('code' => $code, 'errmsg' => $errmsg, 'responseHeaders' => $req->getResponseHeader(), 'data' => $ret)); } return $this->cacheData[$url] && $this->cacheData[$url]['data'] ? $this->cacheData[$url]['data'] : $ret; } if ($code == 304 && $this->cacheData[$url]) { return $this->cacheData[$url]['data']; } $body = $req->getResponseBody(); preg_match_all('{' . $source . '}i', $body, $extracted, PREG_SET_ORDER); foreach ($extracted as $i => $extract) { $_url = $replace; $_referer = $referer; foreach ($extract as $j => $part) { if ($j < 1) { continue; } $_url = str_replace('$EXTRACT' . $j, $part, $_url); $_referer = str_replace('$EXTRACT' . $j, $part, $_referer); } if ($extract[1]) { $_url = str_replace('$EXTRACT', $part, $_url); $_referer = str_replace('$EXTRACT', $part, $_referer); } $ret[$i]['url'] = $_url; $ret[$i]['referer'] = $_referer; } // ページが消えている場合 if ($this->_checkLost($url, $ret)) { return $this->cacheData[$url]['data']; } if ($ident && $this->cacheData[$url] && $this->cacheData[$url]['data']) { $ret = self::_identByCacheData($ret, $this->cacheData[$url]['data'], $ident); } // 結果を永続キャッシュに保存 $this->storeCache($url, array('code' => $code, 'responseHeaders' => $req->getResponseHeader(), 'data' => $ret)); return $ret; }
function mod_time($uri) { if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') >= 0) { require_once S9Y_PEAR_PATH . 'HTTP/Request.php'; serendipity_request_start(); $req = new HTTP_Request($uri); if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') { serendipity_request_end(); return false; } $fHeader = $req->getResponseHeader(); if (isset($fHeader['last-modified'])) { $modtime = $fHeader['last-modified']; } serendipity_request_end(); } else { $parts = parse_url($uri); $host = $parts['host']; $path = $parts['path']; if (!($fp = @fsockopen($host, 80))) { return false; } $req = "HEAD {$path} HTTP/1.1\r\nUser-Agent: PHP/" . phpversion(); $req .= "\r\nHost: {$host}\r\nAccept: */*\r\n\r\n"; fputs($fp, $req); while (!feof($fp)) { $str = fgets($fp, 4096); if (strpos(strtolower($str), 'last-modified') !== false) { $modtime = substr($str, 15); break; } } fclose($fp); } return isset($modtime) ? $modtime : 0; }
/** * Handle a pingback for an entry. * * Also takes care of the speedlimit and spam. Assumes that the caller of this * function has already checked permissions! * * @param string $id ID of entry that got pinged * @param string $type type of that entry ('article' for stories, etc.) * @param string $url URL of the page that pinged us * @param string $oururl URL that got pinged on our site * @return object XML-RPC response * */ function PNB_handlePingback($id, $type, $url, $oururl) { global $_CONF, $_TABLES, $PNB_ERROR; require_once 'HTTP/Request.php'; if (!isset($_CONF['check_trackback_link'])) { $_CONF['check_trackback_link'] = 2; } // handle pingbacks to articles on our own site $skip_speedlimit = false; if ($_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR']) { if (!isset($_CONF['pingback_self'])) { $_CONF['pingback_self'] = 0; // default: skip self-pingbacks } if ($_CONF['pingback_self'] == 0) { return new XML_RPC_Response(new XML_RPC_Value($PNB_ERROR['skipped'])); } else { if ($_CONF['pingback_self'] == 2) { $skip_speedlimit = true; } } } COM_clearSpeedlimit($_CONF['commentspeedlimit'], 'pingback'); if (!$skip_speedlimit) { $last = COM_checkSpeedlimit('pingback'); if ($last > 0) { return new XML_RPC_Response(0, 49, sprintf($PNB_ERROR['speedlimit'], $last, $_CONF['commentspeedlimit'])); } } // update speed limit in any case COM_updateSpeedlimit('pingback'); if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']) { if ($_CONF['check_trackback_link'] & 4) { $parts = parse_url($url); if (empty($parts['host'])) { TRB_logRejected('Pingback: No valid URL', $url); return new XML_RPC_Response(0, 33, $PNB_ERROR['uri_invalid']); } else { $ip = gethostbyname($parts['host']); if ($ip != $_SERVER['REMOTE_ADDR']) { TRB_logRejected('Pingback: IP address mismatch', $url); return new XML_RPC_Response(0, 49, $PNB_ERROR['spam']); } } } } // See if we can read the page linking to us and extract at least // the page's title out of it ... $title = ''; $excerpt = ''; $req = new HTTP_Request($url); $req->addHeader('User-Agent', 'Geeklog/' . VERSION); $response = $req->sendRequest(); if (PEAR::isError($response)) { if ($_CONF['check_trackback_link'] & 3) { // we were supposed to check for backlinks but didn't get the page COM_errorLog("Pingback verification: " . $response->getMessage() . " when requesting {$url}"); return new XML_RPC_Response(0, 33, $PNB_ERROR['uri_invalid']); } // else: silently ignore errors - we'll simply do without the title } else { if ($req->getResponseCode() == 200) { $body = $req->getResponseBody(); if ($_CONF['check_trackback_link'] & 3) { if (!TRB_containsBacklink($body, $oururl)) { TRB_logRejected('Pingback: No link to us', $url); $comment = TRB_formatComment($url); PLG_spamAction($comment, $_CONF['spamx']); return new XML_RPC_Response(0, 49, $PNB_ERROR['spam']); } } preg_match(':<title>(.*)</title>:i', $body, $content); if (empty($content[1])) { $title = ''; // no title found } else { $title = trim(COM_undoSpecialChars($content[1])); } if ($_CONF['pingback_excerpt']) { // Check which character set the site that sent the Pingback // is using $charset = 'ISO-8859-1'; // default, see RFC 2616, 3.7.1 $ctype = $req->getResponseHeader('Content-Type'); if (!empty($ctype)) { // e.g. text/html; charset=utf-8 $c = explode(';', $ctype); foreach ($c as $ct) { $ch = explode('=', trim($ct)); if (count($ch) == 2) { if (trim($ch[0]) == 'charset') { $charset = trim($ch[1]); break; } } } } if (!empty($charset) && strcasecmp($charset, COM_getCharset()) != 0) { if (function_exists('mb_convert_encoding')) { $body = @mb_convert_encoding($body, COM_getCharset(), $charset); } elseif (function_exists('iconv')) { $body = @iconv($charset, COM_getCharset(), $body); } // else: tough luck ... } $excerpt = PNB_makeExcerpt($body, $oururl); } // we could also run the rest of the other site's page // through the spam filter here ... } else { if ($_CONF['check_trackback_link'] & 3) { COM_errorLog("Pingback verification: Got HTTP response code " . $req->getResponseCode() . " when requesting {$url}"); return new XML_RPC_Response(0, 33, $PNB_ERROR['uri_invalid']); } } // else: silently ignore errors - we'll simply do without the title } // check for spam first $saved = TRB_checkForSpam($url, $title, '', $excerpt); if ($saved == TRB_SAVE_SPAM) { return new XML_RPC_Response(0, 49, $PNB_ERROR['spam']); } // save as a trackback comment $saved = TRB_saveTrackbackComment($id, $type, $url, $title, '', $excerpt); if ($saved == TRB_SAVE_REJECT) { return new XML_RPC_Response(0, 49, $PNB_ERROR['multiple']); } if (isset($_CONF['notification']) && in_array('pingback', $_CONF['notification'])) { TRB_sendNotificationEmail($saved, 'pingback'); } return new XML_RPC_Response(new XML_RPC_Value($PNB_ERROR['success'])); }
function fetchUrl($url, $userdata = array()) { require_once "HTTP/Request.php"; # logEvent("Fetching $url"); if (sizeof($userdata)) { foreach ($userdata as $key => $val) { $url = eregi_replace("\\[{$key}\\]", urlencode($val), $url); } } if (!isset($GLOBALS['urlcache'])) { $GLOBALS['urlcache'] = array(); } # keep in memory cache in case we send a page to many emails if (isset($GLOBALS['urlcache'][$url]) && is_array($GLOBALS['urlcache'][$url]) && time() - $GLOBALS['urlcache'][$url]['fetched'] < REMOTE_URL_REFETCH_TIMEOUT) { # logEvent($url . " is cached in memory"); return $GLOBALS['urlcache'][$url]['content']; } $dbcache_lastmodified = getPageCacheLastModified($url); $timeout = time() - $dbcache_lastmodified; if (time() - $dbcache_lastmodified < REMOTE_URL_REFETCH_TIMEOUT) { # logEvent($url.' is cached in database'); return getPageCache($url); } else { # logEvent($url.' is not cached in database '.$timeout.' '. $dbcache_lastmodified." ".time()); } # add a small timeout, although the biggest timeout will exist in doing the DNS lookup, # so it won't make too much of a difference $request_parameters = array('timeout' => 10, 'allowRedirects' => 1, 'method' => 'HEAD'); $headreq = new HTTP_Request($url, $request_parameters); $headreq->addHeader('User-Agent', 'phplist v' . VERSION . ' (http://www.phplist.com)'); if (!PEAR::isError($headreq->sendRequest(false))) { $code = $headreq->getResponseCode(); if ($code != 200) { logEvent('Fetching ' . $url . ' failed, error code ' . $code); return 0; } $header = $headreq->getResponseHeader(); $lastmodified = strtotime($header["last-modified"]); $cache = getPageCache($url, $lastmodified); if (!$cache) { $request_parameters['method'] = 'GET'; $req = new HTTP_Request($url, $request_parameters); $req->addHeader('User-Agent', 'phplist v' . VERSION . ' (http://www.phplist.com)'); logEvent('Fetching ' . $url); if (!PEAR::isError($req->sendRequest(true))) { $content = $req->getResponseBody(); $content = addAbsoluteResources($content, $url); logEvent('Fetching ' . $url . ' success'); setPageCache($url, $lastmodified, $content); } else { logEvent('Fetching ' . $url . ' failed'); return 0; } } else { logEvent($url . ' was cached in database'); $content = $cache; } } else { logEvent('Fetching ' . $url . ' failed'); return 0; } $GLOBALS['urlcache'][$url] = array('fetched' => time(), 'content' => $content); return $content; }
/** * Return array contains the response of the given URL. * array[code] => HTTP status code * array[headers] => HTTP headers * array[headers] => Entity body * Throw exception if error. * * @param string $url * @param array $headers * @param array $post * @return array */ private function getHttpResponse($url, $headers = array(), $post = array()) { $url = str_replace('&', '&', trim($url)); $req = new HTTP_Request($url, array('allowRedirects' => true, 'maxRedirects' => 5)); /* * @see HTTP_Request_Listener_Extended */ $listener = new HTTP_Request_Listener_Extended(); $req->attach($listener); if (!isset($headers['user-agent'])) { $headers['user-agent'] = $this->httpUserAgent; } foreach ($headers as $key => $value) { if (!empty($value)) { $req->addHeader($key, $value); } } if (!empty($post)) { $req->setMethod('POST'); foreach ($post as $key => $value) { $req->addPostData($key, $value); } } $result = $req->sendRequest(); $is_error = false; if (PEAR::isError($result)) { $is_error = true; $error_message = $result->getMessage(); /* * $error_message could be empty if the error was raised * when fsockopen() returns false in Net_Socket::connect() */ if (empty($error_message)) { $error_message = "Failed connecting to the server."; /* * HTTP_Request raises 'Malformed response' error * if request path is empty (e.g. http://www.example.com). * This bug still exists in its automatic redirection mechanism * in CVS rev. 1.55 (latest as of May 18, 2007). */ } elseif ($error_message == 'Malformed response.') { $url = $req->getURL(null); if (false !== ($urls = @parse_url($url)) and !isset($urls['path'])) { $req->setURL($url); $result = $req->sendRequest(); if (PEAR::isError($result)) { $error_message = $result->getMessage(); if (empty($error_message)) { $error_message = "Failed connecting to the server."; } } else { $is_error = false; } } } } if ($is_error) { throw new Exception($error_message); } return array('url' => $req->getUrl(null), 'code' => $req->getResponseCode(), 'headers' => $req->getResponseHeader(), 'body' => $req->getResponseBody()); }
/** * 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_Request */ public 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_Request.'; $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_Request // 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_Request $req = new HTTP_Request($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; }
function callMethod($method, $params = array()) { $this->_err_code = 0; $this->_err_msg = ''; # # create the POST body # $p = $params; $p['method'] = $method; $p['api_key'] = $this->_cfg['api_key']; if ($this->_cfg['api_secret']) { $p['api_sig'] = $this->signArgs($p); } $p2 = array(); foreach ($p as $k => $v) { $p2[] = urlencode($k) . '=' . urlencode($v); } $body = implode('&', $p2); # # create the http request # $req = new HTTP_Request($this->_cfg['endpoint'], array('timeout' => $this->_cfg['conn_timeout'])); $req->_readTimeout = array($this->_cfg['io_timeout'], 0); $req->setMethod(HTTP_REQUEST_METHOD_POST); $req->addRawPostData($body); $req->sendRequest(); $this->_http_code = $req->getResponseCode(); $this->_http_head = $req->getResponseHeader(); $this->_http_body = $req->getResponseBody(); if ($this->_http_code != 200) { $this->_err_code = 0; if ($this->_http_code) { $this->_err_msg = "Bad response from remote server: HTTP status code {$this->_http_code}"; } else { $this->_err_msg = "Couldn't connect to remote server"; } return 0; } # # create xml tree # $dom = new DOMDocument(); $dom->loadXML($this->_http_body); $xp = new DOMXPath($dom); # # check we got an <rsp> element at the root # if (!$xp->query("/rsp")->length) { $this->_err_code = 0; $this->_err_msg = "Bad XML response"; return 0; } # # stat="fail" ? # $stat = $xp->query("/rsp/@stat")->item(0)->value; if ($stat == 'fail') { $n = null; foreach ($xp->query("/rsp/err") as $err) { $this->_err_code = $xp->query("@code", $err)->item(0)->value; $this->_err_msg = $xp->query("@msg", $err)->item(0)->value; } return 0; } # # weird status # if ($stat != 'ok') { $this->_err_code = 0; $this->_err_msg = "Unrecognised REST response status"; return 0; } # # return the tree # return array($dom, $xp, $this->_http_body); }
function verify_s3_etag($object_id, $expected_etag) { $url = s3_signed_object_url($object_id, time() + 300, 'HEAD'); $req = new HTTP_Request($url); $req->setMethod('HEAD'); $res = $req->sendRequest(); if (PEAR::isError($res)) { die_with_code(500, "{$res->message}\n{$q}\n"); } if ($req->getResponseCode() == 200) { return $req->getResponseHeader('etag') == $expected_etag; } return false; }
function fetchImage($url) { $this->log("FETCH : {$url}\n"); if ($url[0] == '/') { $ff = HTML_FlexyFramework::get(); $file = $ff->rootDir . $url; require_once 'File/MimeType.php'; $m = new File_MimeType(); $mt = $m->fromFilename($file); $ext = $m->toExt($mt); return array('mimetype' => $mt, 'ext' => $ext, 'contentid' => md5($file), 'file' => $file); } //print_R($url); exit; if (preg_match('#^file:///#', $url)) { $file = preg_replace('#^file://#', '', $url); require_once 'File/MimeType.php'; $m = new File_MimeType(); $mt = $m->fromFilename($file); $ext = $m->toExt($mt); return array('mimetype' => $mt, 'ext' => $ext, 'contentid' => md5($file), 'file' => $file); } // CACHE??? // 2 files --- the info file.. and the actual file... // add user // unix only... $uinfo = posix_getpwuid(posix_getuid()); $user = $uinfo['name']; $cache = ini_get('session.save_path') . "/Pman_Core_Mailer-{$user}/" . md5($url); if ($this->cache_images && file_exists($cache) && filemtime($cache) > strtotime('NOW - 1 WEEK')) { $ret = json_decode(file_get_contents($cache), true); $this->log("fetched from cache"); $ret['file'] = $cache . '.data'; return $ret; } if (!file_exists(dirname($cache))) { mkdir(dirname($cache), 0700, true); } require_once 'HTTP/Request.php'; $a = new HTTP_Request($this->mapurl($url)); $a->sendRequest(); $data = $a->getResponseBody(); $this->log("got file of size " . strlen($data)); $this->log("save contentid " . md5($url)); file_put_contents($cache . '.data', $data); $mt = $a->getResponseHeader('Content-Type'); require_once 'File/MimeType.php'; $m = new File_MimeType(); $ext = $m->toExt($mt); $ret = array('mimetype' => $mt, 'ext' => $ext, 'contentid' => md5($url)); file_put_contents($cache, json_encode($ret)); $ret['file'] = $cache . '.data'; return $ret; }
/** * Caches a map and streams it back to the browser. */ function saveAndResponseMap($url, $lat, $long, $isArticle) { require_once S9Y_PEAR_PATH . 'HTTP/Request.php'; global $serendipity; $fContent = null; if (function_exists('serendipity_request_start')) { serendipity_request_start(); } $request_pars['allowRedirects'] = TRUE; $req = new HTTP_Request($url, $request_pars); // if the request leads to an error we don't want to have it: return false if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') { $fContent = null; } else { // Allow only images! $mime = $req->getResponseHeader("content-type"); $mimeparts = explode('/', $mime); if (count($mimeparts) == 2 && $mimeparts[0] == 'image') { $fContent = $req->getResponseBody(); } } if (function_exists('serendipity_request_start')) { serendipity_request_end(); } // if no content was fetched, return false if (!isset($fContent)) { return false; } $cache_file = $this->cacheMap($lat, $long, $isArticle, $fContent, $req); if ($cache_file) { $this->showMap($cache_file); } return true; }
function get_url($url, $cache_hard = true) { global $http_cache_timeout; global $api_calls; global $cached_api_calls; # Check whether we have a cached response for this URL # Note there are two cache timestamps: fetched_on_server is tied to the # server (mothership)'s clock and fetched_on is tied to the local clock. # We are careful to compare the local now() against fetched_on and the # server's "Date:" header values against fetched_on_server. if (!$http_cache_timeout) { throw new Exception("\$http_cache_timeout not set"); } # Expire old cache entries. mysql_query('delete from http_cache where fetched_on < now() - ' . $http_cache_timeout); # Load a valid cache element, if any. $sql = 'select content, fetched_on_server from http_cache where url = \'' . mysql_real_escape_string($url) . '\' and fetched_on >= now() - ' . $http_cache_timeout; $q = mysql_query($sql); if (!$q) { throw new Exception("Getting cache, got database error: " . mysql_error()); } require_once 'HTTP/Request.php'; if ($row = mysql_fetch_row($q)) { list($content, $fetched_on) = $row; # Under "hard" caching, return the cached data without talking to server. if ($cache_hard) { message("Hard cache hit at {$url}"); return $content; } # Under "soft" caching, we make a request to ask the server if the resource # has changed since our copy. $fetched_on_http_date = date(DATE_RFC1123, from_mysql_date($fetched_on)); $req = new HTTP_Request($url); $req->addHeader('If-Modified-Since', $fetched_on_http_date); $request_timer -= microtime(true); $ok = $req->sendRequest(); $request_timer += microtime(true); $cached_api_calls = $cached_api_calls + 1; if (!PEAR::isError($ok)) { $respCode = $req->getResponseCode(); if (304 == $respCode) { # 304 Not Modified; we can use the cached copy. message('Cache hit at ' . $url . ' using If-Modified-Since: ' . $fetched_on_http_date . "Request timer: {$request_timer}" . 's'); return $content; } elseif (200 <= $respCode && $respCode < 300) { # Got an OK response, use the data. message('Cache refresh at ' . $url . ' If-Modified-Since: ' . $fetched_on_http_date . '. Request timer: ' . $request_timer . 's'); $content = $req->getResponseBody(); $fetched_on_server = mysql_date(from_http_date($req->getResponseHeader('Date'))); mysql_query('delete from http_cache where url = \'' . mysql_real_escape_string($url) . '\''); if (!insert_into('http_cache', array('url' => $url, 'content' => $content, 'fetched_on_server' => $fetched_on_server))) { throw new Exception("Database error writing to HTTP cache: " . mysql_error()); } return $content; } } else { throw new Exception("Error while GETing {$url} ({$ok})"); } } else { $req = new HTTP_Request($url); $request_timer -= microtime(true); $ok = $req->sendRequest(); $request_timer += microtime(true); $api_calls = $api_calls + 1; message("Cache miss at {$url} Request timer: " . $request_timer . "s"); if (PEAR::isError($ok)) { throw new Exception("Unknown error trying GET {$url}"); } $respCode = $req->getResponseCode(); if (200 <= $respCode && $respCode < 300) { # Got an OK response, use it. $content = $req->getResponseBody(); $fetched_on_server = mysql_date(from_http_date($req->getResponseHeader('Date'))); mysql_query('delete from http_cache where url = \'' . mysql_real_escape_string($url) . '\''); if (!insert_into('http_cache', array('url' => $url, 'content' => $content, 'fetched_on_server' => $fetched_on_server))) { throw new Exception("Database error writing to HTTP cache: " . mysql_error()); } return $content; } else { error("GET {$url} returned {$respCode}"); return null; } } }
private function httpMe($http_cmd, $cloudcache_cmd, $path, $data = '', array $aditional_headers = array()) { $ts = gmdate('Y-m-d\\TH:G:s\\Z'); $userAgent = 'CloudCache PHP Client'; $sig = base64_encode(hash_hmac('sha1', 'CloudCache' . $cloudcache_cmd . $ts, $this->_skey, true)); $req = new HTTP_Request('http://' . $this->_host . '/' . $path); $req->setMethod($http_cmd); $req->addHeader("User-Agent", $userAgent); $req->addHeader("timestamp", $ts); $req->addHeader("signature", $sig); $req->addHeader("akey", $this->_akey); $req->setBody($data); $req->sendRequest(); $ct = $req->getResponseHeader("content-type"); $rspCode = $req->getResponseCode(); return $req->getResponseBody(); }
function getRemoteResource($url, $body = null, $timeout = 3, $method = 'GET', $content_type = null, $headers = array(), $cookies = array(), $post_data = array()) { requirePear(); require_once 'HTTP/Request.php'; $parsed_url = parse_url(__PROXY_SERVER__); if ($parsed_url["host"]) { $oRequest = new HTTP_Request(__PROXY_SERVER__); $oRequest->setMethod('POST'); $oRequest->_timeout = $timeout; $oRequest->addPostData('arg', serialize(array('Destination' => $url, 'method' => $method, 'body' => $body, 'content_type' => $content_type, "headers" => $headers, "post_data" => $post_data))); } else { $oRequest = new HTTP_Request($url); if (count($headers)) { foreach ($headers as $key => $val) { $oRequest->addHeader($key, $val); } } if ($cookies[$host]) { foreach ($cookies[$host] as $key => $val) { $oRequest->addCookie($key, $val); } } if (count($post_data)) { foreach ($post_data as $key => $val) { $oRequest->addPostData($key, $val); } } if (!$content_type) { $oRequest->addHeader('Content-Type', 'text/html'); } else { $oRequest->addHeader('Content-Type', $content_type); } $oRequest->setMethod($method); if ($body) { $oRequest->setBody($body); } $oRequest->_timeout = $timeout; } $oResponse = $oRequest->sendRequest(); $code = $oRequest->getResponseCode(); $header = $oRequest->getResponseHeader(); $response = $oRequest->getResponseBody(); if ($c = $oRequest->getResponseCookies()) { foreach ($c as $k => $v) { $cookies[$host][$v['name']] = $v['value']; } } if ($code > 300 && $code < 399 && $header['location']) { return FileHandler::getRemoteResource($header['location'], $body, $timeout, $method, $content_type, $headers, $cookies, $post_data); } return $response; }
function sendRequest($resource, $verb = NULL, $objectdata = NULL, $acl = NULL, $contentType = NULL, $metadata = NULL) { if ($verb == NULL) { $verb = $this->verb; } if ($acl == NULL) { $aclstring = ""; } else { $aclstring = "x-amz-acl:{$acl}\n"; } $contenttypestring = ""; if ($contentType != NULL && $verb == "PUT" && $objectdata != NULL && $objectdata != "") { $contenttypestring = "{$contentType}"; } // update date / time on each request $this->httpDate = gmdate($this->date_format); $httpDate = $this->httpDate; $paramstring = ""; $delim = "?"; if (strlen($this->prefix)) { $paramstring .= $delim . "prefix=" . urlencode($this->prefix); $delim = "&"; } if (strlen($this->marker)) { $paramstring .= $delim . "marker=" . urlencode($this->marker); $delim = "&"; } if (strlen($this->max_keys)) { $paramstring .= $delim . "max-keys=" . $this->max_keys; $delim = "&"; } if (strlen($this->delimiter)) { $paramstring .= $delim . "delimiter=" . urlencode($this->delimiter); $delim = "&"; } $this->debug_text("HTTP Request sent to: " . $this->S3_URL . $resource . $paramstring); $req = new HTTP_Request($this->S3_URL . $resource . $paramstring); $req->setMethod($verb); if ($objectdata != NULL && $objectdata != "") { $contentMd5 = $this->hex2b64(md5($objectdata)); $req->addHeader("CONTENT-MD5", $contentMd5); $this->debug_text("MD5 HASH OF DATA: " . $contentMd5); $contentmd5string = $contentMd5; } else { $contentmd5string = ""; } if (strlen($contenttypestring)) { $this->debug_text("Setting content type to {$contentType}"); $req->addHeader("CONTENT-TYPE", $contentType); } $req->addHeader("DATE", $httpDate); if (strlen($aclstring)) { $this->debug_text("Setting acl string to {$acl}"); $req->addHeader("x-amz-acl", $acl); } $metadatastring = ""; if (is_array($metadata)) { ksort($metadata); $this->debug_text("Metadata found."); foreach ($metadata as $key => $value) { $metadatastring .= "x-amz-meta-" . $key . ":" . trim($value) . "\n"; $req->addHeader("x-amz-meta-" . $key, trim($value)); $this->debug_text("Setting x-amz-meta-{$key} to '{$value}'"); } } if ($objectdata != NULL && $objectdata != "") { $req->setBody($objectdata); } $stringToSign = "{$verb}\n{$contentmd5string}\n{$contenttypestring}\n{$httpDate}\n{$aclstring}{$metadatastring}/{$resource}"; $this->debug_text("Signing String: {$stringToSign}"); $signature = $this->hex2b64($this->hasher->hash($stringToSign)); $req->addHeader("Authorization", "AWS " . $this->keyId . ":" . $signature); $req->sendRequest(); $this->_responseContentType = $req->getResponseHeader("content-type"); if (strlen($req->getResponseBody())) { $this->debug_text($req->getResponseBody()); return $req->getResponseBody(); } else { $this->debug_text($req->getResponseHeader()); return $req->getResponseHeader(); } }
function geocoder_getdata() { $req = new HTTP_Request("http://rpc.geocoder.us/service/rest"); $req->setMethod(HTTP_REQUEST_METHOD_GET); // assumes $address is *not* urlencoded $geoaddress = $this->Street . ", " . $this->City . ", " . $this->State . ", " . $this->Zip; $req->addQueryString('address', $geoaddress); $req->addHeader("User-Agent", "RadicalDesigns/AMP"); if (!PEAR::isError($req->sendRequest())) { $result = $req->getResponseBody(); } else { // failed $result = $req->getResponseHeader(); //print "there was an error..."; } // echo '<pre><br>geocode result<br>';var_dump($result);echo '</pre><br>'; $xmlparser = new XML_Unserializer(); $parse_result = $xmlparser->unserialize($result, false); /* if ( PEAR::isError( $parse_result )) { print 'yah<BR>'; } else { print 'nah<BR>'; } */ $data = $xmlparser->getUnserializedData(); if (array_key_exists('geo:lat', $data['geo:Point'])) { //return array( $data['geo:Point']['geo:lat'], $data['geo:Point']['geo:long'],$result ); $this->lat = $data['geo:Point']['geo:lat']; $this->long = $data['geo:Point']['geo:long']; } else { #print_r (($data)); //return array( $data['geo:Point'][0]['geo:lat'], $data['geo:Point'][0]['geo:long'],$result ); } }
$v = false; foreach ($_POST as $key => $value) { if ($key == "DESKTOP_XSITE_PARAMS" || $key == "dojo_preventCache") { continue; } if (!$v) { $v = true; $p->setMethod(HTTP_REQUEST_METHOD_POST); } $p->addPostData($key, $value); } foreach ($_GET as $key => $value) { if ($key == "DESKTOP_XSITE_PARAMS") { continue; } $p->addQueryString($key, $value); } $p->sendRequest(); $type = $p->getResponseHeader("Content-Type"); header("Content-Type: {$type}"); foreach (array("400" => "Bad syntax", "401" => "Unauthorized", "402" => "Not Used (Payment Granted)", "403" => "Forbidden", "404" => "Not Found", "500" => "Internal Error", "501" => "Not Implemented", "502" => "Overloaded", "503" => "Gateway Timeout") as $key => $value) { if ($p->getResponseCode() == $key) { header("HTTP/1.0 " . $key . " " . $value); } } $body = $p->getResponseBody(); echo $body; $p->disconnect(); } else { internal_error("permission_denied"); }
/** * Helper function for WebDAV OPTIONS detection * * @access private * @return bool true on success else false */ function _check_options() { // now check OPTIONS reply for WebDAV response headers $req = new HTTP_Request($this->url); $req->setMethod(HTTP_REQUEST_METHOD_OPTIONS); if (is_string($this->user)) { $req->setBasicAuth($this->user, @$this->pass); } $req->sendRequest(); if ($req->getResponseCode() != 200) { return false; } // get the supported DAV levels and extensions $dav = $req->getResponseHeader("DAV"); $this->dav_level = array(); foreach (explode(",", $dav) as $level) { $this->dav_level[trim($level)] = true; } if (!isset($this->dav_level["1"])) { // we need at least DAV Level 1 conformance return false; } // get the supported HTTP methods // TODO these are not checked for WebDAV compliance yet $allow = $req->getResponseHeader("Allow"); $this->dav_allow = array(); foreach (explode(",", $allow) as $method) { $this->dav_allow[trim($method)] = true; } // TODO check for required WebDAV methods return true; }
/** * Opens a url in a file pointer * * @param string $url The URL to open. * @return mixed HTTP response body or boolean false */ protected function _getFeed($url) { $req = new HTTP_Request($url, array('allowRedirects' => true)); if ($this->userAgent != '') { $req->addHeader('User-Agent', $this->userAgent); } if (!empty($this->lastModified) && !empty($this->eTag)) { $req->addHeader('If-Modified-Since', $this->lastModified); $req->addHeader('If-None-Match', $this->eTag); } $response = $req->sendRequest(); if (!PEAR::isError($response)) { if ($req->getResponseCode() == 304) { $this->errorStatus = false; // indicate no error, just unchanged return false; } else { $this->lastModified = $req->getResponseHeader('Last-Modified'); $this->eTag = $req->getResponseHeader('ETag'); return $req->getResponseBody(); } } else { $this->errorStatus = array('HTTP Fetch Failed', $response->getCode(), $response->getMessage()); return false; } }
function saveAndResponseMyBlogAvatar($eventData, $url) { global $serendipity; $request_pars['allowRedirects'] = false; $this->log("saveAndResponseMyBlogAvatar: " . $url); // First a dummy icon is fetched. This is done by fetching a MyBlog Avatar for a not existing domain. // If we have done this before, the dummy_md5 is already set, so we can skip this fetching here. if (!isset($this->mybloglog_dummy_md5)) { $cachefilename = '_mybloglogdummy.md5'; $cache_file = $this->getCacheDirectory() . '/' . $cachefilename; // Look up the cache for the md5 of the MyBlogLog dummy icon saved earlier: if (file_exists($cache_file) && time() - filemtime($cache_file) < $this->cache_seconds) { $fp = fopen($cache_file, 'rb'); $this->mybloglog_dummy_md5 = fread($fp, filesize($cache_file)); fclose($fp); $this->log("Loaded dummy MD5: " . $this->mybloglog_dummy_md5); } else { // dummy MD5 file was not cached or was too old. We have to fetch the dummy icon now $dummyurl = 'http://pub.mybloglog.com/coiserv.php?href=http://grunz.grunz.grunz&n=*'; $this->log("trying dummyUrl: " . $dummyurl); if (function_exists('serendipity_request_start')) { serendipity_request_start(); } $reqdummy = new HTTP_Request($dummyurl, $request_pars); if (PEAR::isError($reqdummy->sendRequest()) || $reqdummy->getResponseCode() != '200') { if (function_exists('serendipity_request_start')) { serendipity_request_end(); } $this->avatarConfiguration["mybloglog_dummy_error!"] = $reqdummy->getResponseCode(); // unable to fetch a dummy picture! $this->log("unable to fetch a dummy picture!" . $dummyurl); return false; // what can we say else.. } else { // Allow only images as Avatar! $mime = $reqdummy->getResponseHeader("content-type"); $this->log("MyBlogLog Avatar fetch mimetype: {$mime}"); $mimeparts = explode('/', $mime); if (count($mimeparts) != 2 || $mimeparts[0] != 'image') { // unable to fetch a dummy picture! $this->log("unable to fetch a dummy picture!" . $dummyurl); if (function_exists('serendipity_request_start')) { serendipity_request_end(); } return false; // what can we say else.. } $fContent = $reqdummy->getResponseBody(); $this->mybloglog_dummy_md5 = md5($fContent); // Save MD5 of dummy icon for later runs $fp = fopen($cache_file, 'wb'); fwrite($fp, $this->mybloglog_dummy_md5); fclose($fp); $this->log("dummy MD5 saved: " . $this->mybloglog_dummy_md5); } if (function_exists('serendipity_request_start')) { serendipity_request_end(); } } } // Fetch the correct icon and compare: if (isset($this->mybloglog_dummy_md5)) { $cachefilename = $this->getCacheFilePath($eventData); // fetch the icon if (function_exists('serendipity_request_start')) { serendipity_request_start(); } $this->log("Fetching mbl: " . $url); $req = new HTTP_Request($url, $request_pars); if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') { if (function_exists('serendipity_request_start')) { serendipity_request_end(); } $this->log("Unable to fetch the correct image!"); // Unable to fetch the correct image! return false; } else { // Test, if this realy is an image! $mime_type = $req->getResponseHeader('content-type'); if (!empty($mime_type)) { $mt_parts = explode('/', $mime_type); } if (isset($mt_parts) && is_array($mt_parts) && $mt_parts[0] == 'image') { $fContent = $req->getResponseBody(); $avtmd5 = md5($fContent); $this->log("mbl image fetched, MD5: " . $avtmd5); if ($this->mybloglog_dummy_md5 != $avtmd5) { $this->log("caching mbl image: " . $cachefilename); $this->cacheAvatar($eventData, $fContent, $req); } } else { $this->log("MyBlogLog did not return an image: " . $mime_type); $avtmd5 = $this->mybloglog_dummy_md5; // Declare it as dummy in order not to save it. } } if (function_exists('serendipity_request_start')) { serendipity_request_end(); } if ($this->mybloglog_dummy_md5 == $avtmd5) { // This seems to be a dummy avatar! return false; } else { $this->show($cachefilename); return true; } } return false; }
/** * Builds appropriate parameters for checking out to google and submits the post params. * * @param array $params * Name value pair of contribution data. * @param string $component * Event/contribution. * @param object $cart * Object of google cart. */ public function submitPostParams($params, $component, $cart) { $url = rtrim($this->_paymentProcessor['url_site'], '/') . '/cws/v2/Merchant/' . $this->_paymentProcessor['user_name'] . '/checkout'; if ($component == "event") { $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}"; } elseif ($component == "contribute") { $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}"; $contributionRecurID = CRM_Utils_Array::value('contributionRecurID', $params); if ($contributionRecurID) { $privateData .= ",contributionRecurID={$contributionRecurID}"; } $membershipID = CRM_Utils_Array::value('membershipID', $params); if ($membershipID) { $privateData .= ",membershipID={$membershipID}"; } $relatedContactID = CRM_Utils_Array::value('related_contact', $params); if ($relatedContactID) { $privateData .= ",relatedContactID={$relatedContactID}"; $onBehalfDupeAlert = CRM_Utils_Array::value('onbehalf_dupe_alert', $params); if ($onBehalfDupeAlert) { $privateData .= ",onBehalfDupeAlert={$onBehalfDupeAlert}"; } } } // Allow further manipulation of the arguments via custom hooks .. CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $privateData); $cart->SetMerchantPrivateData($privateData); if ($component == "event") { $returnURL = CRM_Utils_System::url('civicrm/event/register', "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE); } elseif ($component == "contribute") { $returnURL = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE); } $cart->SetContinueShoppingUrl($returnURL); $cartVal = base64_encode($cart->GetXML()); $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML())); $googleParams = array('cart' => $cartVal, 'signature' => $signatureVal); require_once 'HTTP/Request.php'; $params = array('method' => HTTP_REQUEST_METHOD_POST, 'allowRedirects' => FALSE); $request = new HTTP_Request($url, $params); foreach ($googleParams as $key => $value) { $request->addPostData($key, $value); } $result = $request->sendRequest(); if (PEAR::isError($result)) { CRM_Core_Error::fatal($result->getMessage()); } if ($request->getResponseCode() != 302) { CRM_Core_Error::fatal(ts('Invalid response code received from Google Checkout: %1', array(1 => $request->getResponseCode()))); } CRM_Utils_System::redirect($request->getResponseHeader('location')); CRM_Utils_System::civiExit(); }
/** * Calculates infos on the given file and returns an array containing these infos */ function GetFileInfo($url) { global $serendipity; $this->log("GetFileInfo for {$url}"); $fileInfo = array(); //caching metadata $cacheOptions = array('lifeTime' => '2592000', 'automaticSerialization' => true, 'cacheDir' => $serendipity['serendipityPath'] . 'templates_c/'); if (serendipity_db_bool($this->get_config('use_cache', 'true'))) { $this->log("GetFileInfo: Trying cached infos"); //md5 for not having strange characters in that id.. $cacheId = md5($url) . '.2'; include_once S9Y_PEAR_PATH . "Cache/Lite.php"; $cache = new Cache_Lite($cacheOptions); if ($fileInfo = $cache->get($cacheId)) { $this->log("GetFileInfo: Cached infos found in file {$cacheId}"); //return directly on cache hit return $fileInfo; } } //cache miss! -> get data, store it in cache and return. // translate pontential relative url to absolute url if (preg_match('@https?://@', $url)) { $absolute_url = $url; } else { $absolute_url = $this->GetHostUrl() . $url; } if ($this->debug) { $fileInfo['absolute_url'] = $absolute_url; } // Now remove configured base URL $rel_path = str_replace($serendipity['baseURL'], "", $absolute_url); if ($this->debug) { $fileInfo['rel_path'] = $rel_path; } // do we have a local file here? //$localMediaFile = $serendipity['serendipityPath'] . $urlParts['path']; $localMediaFile = $serendipity['serendipityPath'] . $rel_path; $fileInfo['localMediaFile'] = $localMediaFile; $this->log("Absolute_url: {$absolute_url} - Relative: {$localMediaFile}"); // Remember extension of file list($sName, $fileInfo['extension']) = serendipity_parseFileName($localMediaFile); if (file_exists($localMediaFile)) { $this->log("GetFileInfo: Local file exists"); $fileInfo['length'] = filesize($localMediaFile); $fileInfo['md5'] = md5_file($localMediaFile); $this->GetID3Infos($localMediaFile, $fileInfo); $this->log(print_r($fileInfo, true)); // Set default $fileInfo['mime'] = $this->getFileMime($fileInfo['extension'], $fileInfo['mime']); } elseif (preg_match('@https?://@', $url)) { include_once S9Y_PEAR_PATH . 'HTTP/Request.php'; if (function_exists('serendipity_request_start')) { serendipity_request_start(); } $this->Log("Execute HTTP_Request for {$url}"); $http = new HTTP_Request($url); $http->setMethod(HTTP_REQUEST_METHOD_HEAD); if (!PEAR::isError($http->sendRequest(false))) { $fileInfo['length'] = intval($http->getResponseHeader('content-length')); $fileInfo['md5'] = $http->getResponseHeader('content-md5'); //will return false if not present $fileInfo['mime'] = $http->getResponseHeader('content-type'); $this->Log("Filling MIME with HTTP Header: " . print_r($fileInfo, true)); } if (function_exists('serendipity_request_end')) { serendipity_request_end(); } } else { // Not found locally and no URL $fileInfo['notfound'] = true; } if (serendipity_db_bool($this->get_config('use_cache', 'true'))) { $cache->save($fileInfo, $cacheId); } return $fileInfo; }
/** * Sets appropriate parameters for checking out to google * * @param array $params name value pair of contribution datat * * @return void * @access public * */ function doTransferCheckout(&$params, $component) { $component = strtolower($component); $url = rtrim($this->_paymentProcessor['url_site'], '/') . '/cws/v2/Merchant/' . $this->_paymentProcessor['user_name'] . '/checkout'; //Create a new shopping cart object $merchant_id = $this->_paymentProcessor['user_name']; // Merchant ID $merchant_key = $this->_paymentProcessor['password']; // Merchant Key $server_type = $this->_mode == 'test' ? 'sandbox' : ''; $cart = new GoogleCart($merchant_id, $merchant_key, $server_type); $item1 = new GoogleItem($params['item_name'], '', 1, $params['amount'], $params['currencyID']); $cart->AddItem($item1); if ($component == "event") { $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}"; } elseif ($component == "contribute") { $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}"; $membershipID = CRM_Utils_Array::value('membershipID', $params); if ($membershipID) { $privateData .= ",membershipID={$membershipID}"; } $relatedContactID = CRM_Utils_Array::value('related_contact', $params); if ($relatedContactID) { $privateData .= ",relatedContactID={$relatedContactID}"; $onBehalfDupeAlert = CRM_Utils_Array::value('onbehalf_dupe_alert', $params); if ($onBehalfDupeAlert) { $privateData .= ",onBehalfDupeAlert={$onBehalfDupeAlert}"; } } } // Allow further manipulation of the arguments via custom hooks .. CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $privateData); $cart->SetMerchantPrivateData($privateData); if ($component == "event") { $returnURL = CRM_Utils_System::url('civicrm/event/register', "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", true, null, false); } elseif ($component == "contribute") { $returnURL = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", true, null, false); } $cart->SetContinueShoppingUrl($returnURL); $cartVal = base64_encode($cart->GetXML()); $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML())); $googleParams = array('cart' => $cartVal, 'signature' => $signatureVal); require_once 'HTTP/Request.php'; $params = array('method' => HTTP_REQUEST_METHOD_POST, 'allowRedirects' => false); $request = new HTTP_Request($url, $params); foreach ($googleParams as $key => $value) { $request->addPostData($key, $value); } $result = $request->sendRequest(); if (PEAR::isError($result)) { CRM_Core_Error::fatal($result->getMessage()); } if ($request->getResponseCode() != 302) { CRM_Core_Error::fatal(ts('Invalid response code received from Google Checkout: %1', array(1 => $request->getResponseCode()))); } CRM_Utils_System::redirect($request->getResponseHeader('location')); exit; }