/**
* addHeader
* set a unique request header
* @param headerName the header name
* @param headerValue the header value, ( unencoded)
**/
function addHeader( $headerName, $headerValue )
{
if( $this->debug & DBGTRACE ) echo "addHeader( $headerName, $headerValue )\n";
$this->requestHeaders[$headerName] = $headerValue;
} /**
* removeHeader
* unset a request header
* @param headerName the header name
**/
function removeHeader( $headerName )
{
if( $this->debug & DBGTRACE ) echo "removeHeader( $headerName) \n";
unset( $this->requestHeaders[$headerName] );
} /**
* addCookie
* set a session cookie, that will be used in the next requests.
* this is a hack as cookie are usually set by the server, but you may need it
* it is your responsabilty to unset the cookie if you request another host
* to keep a session on the server
* @param string the name of the cookie
* @param string the value for the cookie
**/
function addCookie( $cookiename, $cookievalue )
{
if( $this->debug & DBGTRACE ) echo "addCookie( $cookiename, $cookievalue ) \n";
$cookie = $cookiename . "=" . $cookievalue;
$this->requestHeaders["Cookie"] = $cookie;
} /**
* removeCookie
* unset cookies currently in use
**/
function removeCookies()
{
if( $this->debug & DBGTRACE ) echo "removeCookies() \n";
unset( $this->requestHeaders["Cookie"] );
} /**
* Connect
* open the connection to the server
* @param host string server address (or IP)
* @param port string server listening port - defaults to 80
* @return boolean false is connection failed, true otherwise
**/
function Connect( $host, $port = NULL )
{
if( $this->debug & DBGTRACE ) echo "Connect( $host, $port ) \n"; $this->url['scheme'] = "http";
$this->url['host'] = $host;
if( $port != NULL )
$this->url['port'] = $port;
return true;
} /**
* Disconnect
* close the connection to the server
**/
function Disconnect()
{
if( $this->debug & DBGTRACE ) echo "Disconnect()\n";
if( $this->socket )
fclose( $this->socket );
} /**
* head
* issue a HEAD request
* @param uri string URI of the document
* @return string response status code (200 if ok)
* @seeAlso getHeaders()
**/
function Head( $uri )
{
if( $this->debug & DBGTRACE ) echo "Head( $uri )\n";
$this->responseHeaders = $this->responseBody = "";
$uri = $this->makeUri( $uri );
if( $this->sendCommand( "HEAD $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
}
/**
* get
* issue a GET http request
* @param uri URI (path on server) or full URL of the document
* @return string response status code (200 if ok)
* @seeAlso getHeaders(), getBody()
**/
function Get( $url )
{
if( $this->debug & DBGTRACE ) echo "Get( $url )\n";
$this->responseHeaders = $this->responseBody = "";
$uri = $this->makeUri( $url ); if( $this->sendCommand( "GET $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
} /**
* Options
* issue a OPTIONS http request
* @param uri URI (path on server) or full URL of the document
* @return array list of options supported by the server or NULL in case of error
**/
function Options( $url )
{
if( $this->debug & DBGTRACE ) echo "Options( $url )\n";
$this->responseHeaders = $this->responseBody = "";
$uri = $this->makeUri( $url ); if( $this->sendCommand( "OPTIONS $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
if( @$this->responseHeaders["Allow"] == NULL )
return NULL;
else
return explode( ",", $this->responseHeaders["Allow"] );
} /**
* Post
* issue a POST http request
* @param uri string URI of the document
* @param query_params array parameters to send in the form "parameter name" => value
* @return string response status code (200 if ok)
* @example
* $params = array( "login" => "tiger", "password" => "secret" );
* $http->post( "/login.php", $params );
**/
function Post( $uri, $query_params="" )
{
if( $this->debug & DBGTRACE ) echo "Post( $uri, $query_params )\n";
$uri = $this->makeUri( $uri );
if( is_array($query_params) ) {
$postArray = array();
foreach( $query_params as $k=>$v ) {
$postArray[] = urlencode($k) . "=" . urlencode($v);
}
$this->requestBody = implode( "&", $postArray);
}
// set the content type for post parameters
$this->addHeader( 'Content-Type', "application/x-www-form-urlencoded" );
// done in sendCommand() $this->addHeader( 'Content-Length', strlen($this->requestBody) ); if( $this->sendCommand( "POST $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
$this->removeHeader('Content-Type');
$this->removeHeader('Content-Length');
$this->requestBody = "";
return $this->reply;
} /**
* Put
* Send a PUT request
* PUT is the method to sending a file on the server. it is *not* widely supported
* @param uri the location of the file on the server. dont forget the heading "/"
* @param filecontent the content of the file. binary content accepted
* @return string response status code 201 (Created) if ok
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
* addHeader
* set a unique request header
* @param headerName the header name
* @param headerValue the header value, ( unencoded)
**/
function addHeader( $headerName, $headerValue )
{
if( $this->debug & DBGTRACE ) echo "addHeader( $headerName, $headerValue )\n";
$this->requestHeaders[$headerName] = $headerValue;
} /**
* removeHeader
* unset a request header
* @param headerName the header name
**/
function removeHeader( $headerName )
{
if( $this->debug & DBGTRACE ) echo "removeHeader( $headerName) \n";
unset( $this->requestHeaders[$headerName] );
} /**
* addCookie
* set a session cookie, that will be used in the next requests.
* this is a hack as cookie are usually set by the server, but you may need it
* it is your responsabilty to unset the cookie if you request another host
* to keep a session on the server
* @param string the name of the cookie
* @param string the value for the cookie
**/
function addCookie( $cookiename, $cookievalue )
{
if( $this->debug & DBGTRACE ) echo "addCookie( $cookiename, $cookievalue ) \n";
$cookie = $cookiename . "=" . $cookievalue;
$this->requestHeaders["Cookie"] = $cookie;
} /**
* removeCookie
* unset cookies currently in use
**/
function removeCookies()
{
if( $this->debug & DBGTRACE ) echo "removeCookies() \n";
unset( $this->requestHeaders["Cookie"] );
} /**
* Connect
* open the connection to the server
* @param host string server address (or IP)
* @param port string server listening port - defaults to 80
* @return boolean false is connection failed, true otherwise
**/
function Connect( $host, $port = NULL )
{
if( $this->debug & DBGTRACE ) echo "Connect( $host, $port ) \n"; $this->url['scheme'] = "http";
$this->url['host'] = $host;
if( $port != NULL )
$this->url['port'] = $port;
return true;
} /**
* Disconnect
* close the connection to the server
**/
function Disconnect()
{
if( $this->debug & DBGTRACE ) echo "Disconnect()\n";
if( $this->socket )
fclose( $this->socket );
} /**
* head
* issue a HEAD request
* @param uri string URI of the document
* @return string response status code (200 if ok)
* @seeAlso getHeaders()
**/
function Head( $uri )
{
if( $this->debug & DBGTRACE ) echo "Head( $uri )\n";
$this->responseHeaders = $this->responseBody = "";
$uri = $this->makeUri( $uri );
if( $this->sendCommand( "HEAD $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
}
/**
* get
* issue a GET http request
* @param uri URI (path on server) or full URL of the document
* @return string response status code (200 if ok)
* @seeAlso getHeaders(), getBody()
**/
function Get( $url )
{
if( $this->debug & DBGTRACE ) echo "Get( $url )\n";
$this->responseHeaders = $this->responseBody = "";
$uri = $this->makeUri( $url ); if( $this->sendCommand( "GET $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
} /**
* Options
* issue a OPTIONS http request
* @param uri URI (path on server) or full URL of the document
* @return array list of options supported by the server or NULL in case of error
**/
function Options( $url )
{
if( $this->debug & DBGTRACE ) echo "Options( $url )\n";
$this->responseHeaders = $this->responseBody = "";
$uri = $this->makeUri( $url ); if( $this->sendCommand( "OPTIONS $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
if( @$this->responseHeaders["Allow"] == NULL )
return NULL;
else
return explode( ",", $this->responseHeaders["Allow"] );
} /**
* Post
* issue a POST http request
* @param uri string URI of the document
* @param query_params array parameters to send in the form "parameter name" => value
* @return string response status code (200 if ok)
* @example
* $params = array( "login" => "tiger", "password" => "secret" );
* $http->post( "/login.php", $params );
**/
function Post( $uri, $query_params="" )
{
if( $this->debug & DBGTRACE ) echo "Post( $uri, $query_params )\n";
$uri = $this->makeUri( $uri );
if( is_array($query_params) ) {
$postArray = array();
foreach( $query_params as $k=>$v ) {
$postArray[] = urlencode($k) . "=" . urlencode($v);
}
$this->requestBody = implode( "&", $postArray);
}
// set the content type for post parameters
$this->addHeader( 'Content-Type', "application/x-www-form-urlencoded" );
// done in sendCommand() $this->addHeader( 'Content-Length', strlen($this->requestBody) ); if( $this->sendCommand( "POST $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
$this->removeHeader('Content-Type');
$this->removeHeader('Content-Length');
$this->requestBody = "";
return $this->reply;
} /**
* Put
* Send a PUT request
* PUT is the method to sending a file on the server. it is *not* widely supported
* @param uri the location of the file on the server. dont forget the heading "/"
* @param filecontent the content of the file. binary content accepted
* @return string response status code 201 (Created) if ok
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
{
if( $this->debug & DBGTRACE ) echo "Put( $uri, [filecontent not displayed )\n";
$uri = $this->makeUri( $uri );
$this->requestBody = $filecontent;
if( $this->sendCommand( "PUT $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
} /**
* Send a MOVE HTTP-DAV request
* Move (rename) a file on the server
* @param srcUri the current file location on the server. dont forget the heading "/"
* @param destUri the destination location on the server. this is *not* a full URL
* @param overwrite boolean - true to overwrite an existing destinationn default if yes
* @return string response status code 204 (Unchanged) if ok
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
function Move( $srcUri, $destUri, $overwrite=true )
{
if( $this->debug & DBGTRACE ) echo "Move( $srcUri, $destUri, $overwrite )\n";
if( $overwrite )
$this->requestHeaders['Overwrite'] = "T";
else
$this->requestHeaders['Overwrite'] = "F"; $destUrl = $this->url['scheme'] . "://" . $this->url['host'];
if( $this->url['port'] != "" )
$destUrl .= ":" . $this->url['port'];
$destUrl .= $destUri;
$this->requestHeaders['Destination'] = $destUrl; if( $this->sendCommand( "MOVE $srcUri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
} /**
* Send a COPY HTTP-DAV request
* Copy a file -allready on the server- into a new location
* @param srcUri the current file location on the server. dont forget the heading "/"
* @param destUri the destination location on the server. this is *not* a full URL
* @param overwrite boolean - true to overwrite an existing destination - overwrite by default
* @return string response status code 204 (Unchanged) if ok
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
function Copy( $srcUri, $destUri, $overwrite=true )
{
if( $this->debug & DBGTRACE ) echo "Copy( $srcUri, $destUri, $overwrite )\n";
if( $overwrite )
$this->requestHeaders['Overwrite'] = "T";
else
$this->requestHeaders['Overwrite'] = "F"; $destUrl = $this->url['scheme'] . "://" . $this->url['host'];
if( $this->url['port'] != "" )
$destUrl .= ":" . $this->url['port'];
$destUrl .= $destUri;
$this->requestHeaders['Destination'] = $destUrl; if( $this->sendCommand( "COPY $srcUri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
}
/**
* Send a MKCOL HTTP-DAV request
* Create a collection (directory) on the server
* @param uri the directory location on the server. dont forget the heading "/"
* @return string response status code 201 (Created) if ok
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
function MkCol( $uri )
{
if( $this->debug & DBGTRACE ) echo "Mkcol( $uri )\n";
// $this->requestHeaders['Overwrite'] = "F";
if( $this->sendCommand( "MKCOL $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
} /**
* Delete a file on the server using the "DELETE" HTTP-DAV request
* This HTTP method is *not* widely supported
* Only partially supports "collection" deletion, as the XML response is not parsed
* @param uri the location of the file on the server. dont forget the heading "/"
* @return string response status code 204 (Unchanged) if ok
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
function Delete( $uri )
{
if( $this->debug & DBGTRACE ) echo "Delete( $uri )\n";
if( $this->sendCommand( "DELETE $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
}
/**
* PropFind
* implements the PROPFIND method
* PROPFIND retrieves meta informations about a resource on the server
* XML reply is not parsed, you'll need to do it
* @param uri the location of the file on the server. dont forget the heading "/"
* @param scope set the scope of the request.
* O : infos about the node only
* 1 : infos for the node and its direct children ( one level)
* Infinity : infos for the node and all its children nodes (recursive)
* @return string response status code - 207 (Multi-Status) if OK
* @see RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"
**/
function PropFind( $uri, $scope=0 )
{
if( $this->debug & DBGTRACE ) echo "Propfind( $uri, $scope )\n";
$this->requestHeaders['Depth'] = $scope;
if( $this->sendCommand( "PROPFIND $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
}
/**
* Lock - WARNING: EXPERIMENTAL
* Lock a ressource on the server. XML reply is not parsed, you'll need to do it
* @param $uri URL (relative) of the resource to lock
* @param $lockScope - use "exclusive" for an eclusive lock, "inclusive" for a shared lock
* @param $lockType - acces type of the lock : "write"
* @param $lockScope - use "exclusive" for an eclusive lock, "inclusive" for a shared lock
* @param $lockOwner - an url representing the owner for this lock
* @return server reply code, 200 if ok
**/
function Lock( $uri, $lockScope, $lockType, $lockOwner )
{
$body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<D:lockinfo xmlns:D='DAV:'>
<D:lockscope><D:$lockScope/></D:lockscope>\n<D:locktype><D:$lockType/></D:locktype>
<D:owner><D:href>$lockOwner</D:href></D:owner>
</D:lockinfo>\n"; $this->requestBody = utf8_encode( $body );
if( $this->sendCommand( "LOCK $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
}
/**
* Unlock - WARNING: EXPERIMENTAL
* unlock a ressource on the server
* @param $uri URL (relative) of the resource to unlock
* @param $lockToken the lock token given at lock time, eg: opaquelocktoken:e71d4fae-5dec-22d6-fea5-00a0c91e6be4
* @return server reply code, 204 if ok
**/
function Unlock( $uri, $lockToken )
{
$this->addHeader( "Lock-Token", "<$lockToken>" );
if( $this->sendCommand( "UNLOCK $uri HTTP/$this->protocolVersion" ) )
$this->processReply();
return $this->reply;
} /**
* getHeaders
* return the response headers
* to be called after a Get() or Head() call
* @return array headers received from server in the form headername => value
* @seeAlso get, head
**/
function getHeaders()
{
if( $this->debug & DBGTRACE ) echo "getHeaders()\n";
if( $this->debug & DBGINDATA ) {
echo "DBG.INDATA responseHeaders="; print_r( $this->responseHeaders );
}
return $this->responseHeaders;
} /**
* getHeader
* return the response header "headername"
* @param headername the name of the header
* @return header value or NULL if no such header is defined
**/
function getHeader( $headername )
{
if( $this->debug & DBGTRACE ) echo "getHeaderName( $headername )\n";
return $this->responseHeaders[$headername];
} /**
* getBody
* return the response body
* invoke it after a Get() call for instance, to retrieve the response
* @return string body content
* @seeAlso get, head
**/