[Mapbender-commits] r1909 - trunk/mapbender/http/classes
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Mon Dec 10 12:20:32 EST 2007
Author: christoph
Date: 2007-12-10 12:20:32 -0500 (Mon, 10 Dec 2007)
New Revision: 1909
Modified:
trunk/mapbender/http/classes/class_connector.php
Log:
extended to http post
Modified: trunk/mapbender/http/classes/class_connector.php
===================================================================
--- trunk/mapbender/http/classes/class_connector.php 2007-12-10 12:53:26 UTC (rev 1908)
+++ trunk/mapbender/http/classes/class_connector.php 2007-12-10 17:20:32 UTC (rev 1909)
@@ -20,23 +20,117 @@
require_once(dirname(__FILE__)."/../../conf/mapbender.conf");
require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
-class connector{
- var $file;
- function connector($url){
- if(CONNECTION == 'curl'){
- $this->file = $this->getCURL($url);
+mb_internal_encoding("UTF-8");
+
+/**
+ * Establishes a connection to a given URL (and loads the content).
+ * Supports HTTP (GET and POST), cURL and socket connections.
+ *
+ * @class
+ */
+class connector {
+
+ var $file;
+ private $connectionType;
+ private $httpType = "get";
+ private $httpPostData;
+ private $httpContentType;
+
+ /**
+ * @constructor
+ * @param String url the URL that will be loaded (optional)
+ */
+ public function __construct($url){
+ $this->set("connectionType", CONNECTION);
+ if ($url) {
+ $this->load($url);
}
- else if(CONNECTION == 'http'){
- $this->file = $this->getHTTP($url);
+ }
+
+ /**
+ * Loads content from the given URL.
+ */
+ public function load($url) {
+ switch ($this->connectionType) {
+ case "curl":
+ $this->file = $this->getCURL($url);
+ break;
+ case "http":
+ $this->file = $this->getHTTP($url);
+ break;
+ case "socket":
+ $this->file = $this->getSOCKET($url);
+ break;
}
- else if(CONNECTION == 'socket'){
- $this->file = $this->getSOCKET($url);
- }
if(!$this->file){
$e = new mb_exception("connector: unable to load: ".$url);
+ return false;
}
+ return $this->file;
+ }
+
+ /**
+ * Sets the environment variables. The following can be set:
+ * - connectionType ("http", "curl", "socket")
+ * - httpType ("get", "post")
+ * - etc.
+ */
+ public function set ($key, $value) {
+ switch ($key) {
+ case "connectionType":
+ if ($this->isValidConnectionType($value)) {
+ $this->connectionType = $value;
+ }
+ break;
+
+ case "httpType":
+ if (in_array(mb_strtoupper($value), array("POST", "GET"))) {
+ $this->httpType = $value;
+ }
+ else {
+ $e = new mb_exception("class_connector.php: invalid http type '" . $value . "'");
+ }
+ break;
+
+ case "httpPostData":
+ $this->httpPostData = $value;
+ break;
+
+ case "httpContentType":
+ if ($this->isValidHttpContentType($value)) {
+ $this->httpContentType = $value;
+ }
+ break;
+ }
}
- function getCURL($url){
+
+ private function isValidConnectionType ($value) {
+ if (in_array(mb_strtoupper($value), array("HTTP", "CURL", "SOCKET"))) {
+ return true;
+ }
+ else {
+ $e = new mb_exception("class_connector.php: invalid connection type '" . $value . "'");
+ return false;
+ }
+ }
+
+ private function isValidHttpContentType ($value) {
+ $validHttpContentTypeArray = array("XML");
+ if (in_array(mb_strtoupper($value), $validHttpContentTypeArray)) {
+ switch ($value) {
+ case "XML":
+ $this->httpContentType = "application/xml";
+ break;
+ }
+ return true;
+ }
+ else {
+ $e = new mb_exception("class_connector.php: invalid HTTP content type '" . $value . "'");
+ return false;
+ }
+ }
+
+ private function getCURL($url){
$ch = curl_init ($url);
// curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
@@ -49,16 +143,42 @@
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec ($ch);
curl_close ($ch);
-// $h = fopen(TMPDIR.md5(microtime()).".png", "w");
-// fputs($h, $file);
-// fclose($h);
return $file;
}
- function getHTTP($url){
- return @file_get_contents($url);
+
+ private function getHTTP($url){
+ if ($this->httpType == "get") {
+ return @file_get_contents($url);
+ }
+ else {
+ $urlComponentArray = parse_url($url);
+ $host = $urlComponentArray["host"];
+ $port = $urlComponentArray["port"];
+ if ($port == "") {
+ $port = 80;
+ }
+ $path = $urlComponentArray["path"];
+
+ $buf = '';
+ $fp = fsockopen($host, $port);
+ fputs($fp, "POST $path HTTP/1.1\r\n");
+ fputs($fp, "Host: $host\r\n");
+ if ($this->isValidHttpContentType($this->httpContentType)) {
+ fputs($fp,"Content-type: " . $this->httpContentType . "\r\n");
+ }
+ fputs($fp, "Content-length: " . strlen($this->httpPostData) . "\r\n");
+ fputs($fp, "Connection: close\r\n\r\n");
+ fputs($fp, $this->httpPostData);
+ while (!feof($fp)) {
+ $buf .= fgets($fp,4096);
+ }
+ fclose($fp);
+ return $buf;
+ }
}
- function getSOCKET($url){
+
+ private function getSOCKET($url){
$r = "";
$fp = fsockopen (CONNECTION_PROXY, CONNECTION_PORT, $errno, $errstr, 30);
if (!$fp) {
More information about the Mapbender_commits
mailing list