Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/lib/ -> webdavlib.php (source)

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

(no description)

File Size: 1754 lines (70 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

webdav_client:: (45 methods):
  __construct()
  __set()
  set_protocol()
  iso8601totime()
  open()
  close()
  check_webdav()
  options()
  mkcol()
  get()
  put()
  put_file()
  get_file()
  copy_file()
  copy_coll()
  move()
  lock()
  unlock()
  delete()
  ls()
  gpi()
  is_file()
  is_dir()
  mput()
  mget()
  _endElement()
  _propfind_startElement()
  _propfind_cData()
  _delete_startElement()
  _delete_cData()
  _lock_startElement()
  _lock_cData()
  header_add()
  header_unset()
  create_basic_request()
  digest_auth()
  digest_signature()
  send_request()
  get_respond()
  update_file_or_buffer()
  process_respond()
  reopen()
  translate_uri()
  utf_decode_path()
  _error_log()


Class: webdav_client  - X-Ref

A Moodle-modified WebDAV client, based on
webdav_client v0.1.5, a php based webdav client class.
class webdav client. a php based nearly RFC 2518 conforming client.

This class implements methods to get access to an webdav server.
Most of the methods are returning boolean false on error, an integer status (http response status) on success
or an array in case of a multistatus response (207) from the webdav server. Look at the code which keys are used in arrays.
It's your responsibility to handle the webdav server responses in an proper manner.
Please notice that all Filenames coming from or going to the webdav server should be UTF-8 encoded (see RFC 2518).
This class tries to convert all you filenames into utf-8 when it's needed.

Moodle modifications:
* Moodle 3.4: Add support for OAuth 2 bearer token-based authentication

__construct($server = '', $user = '', $pass = '', $auth = false, $socket = '', $oauthtoken = '')   X-Ref
Constructor - Initialise class variables

param: string $server Hostname of the server to connect to
param: string $user Username (for basic/digest auth, see $auth)
param: string $pass Password (for basic/digest auth, see $auth)
param: bool $auth Authentication type; one of ['basic', 'digest', 'bearer']
param: string $socket Used protocol for fsockopen, usually: '' (empty) or 'ssl://'
param: string $oauthtoken OAuth 2 bearer token (for bearer auth, see $auth)

__set($key, $value)   X-Ref
No description

set_protocol($version)   X-Ref
Set which HTTP protocol will be used.
Value 1 defines that HTTP/1.1 should be used (Keeps Connection to webdav server alive).
Otherwise HTTP/1.0 will be used.

param: int version

iso8601totime($iso8601)   X-Ref
Convert ISO 8601 Date and Time Profile used in RFC 2518 to an unix timestamp.

param: string iso8601
return: unixtimestamp on sucess. Otherwise false.

open()   X-Ref
Open's a socket to a webdav server

return: bool true on success. Otherwise false.

close()   X-Ref
Closes an open socket.


check_webdav()   X-Ref
Check's if server is a webdav compliant server.
True if server returns a DAV Element in Header and when
schema 1,2 is supported.

return: bool true if server is webdav server. Otherwise false.

options()   X-Ref
Get options from webdav server.

return: array with all header fields returned from webdav server. false if server does not speak http.

mkcol($path)   X-Ref
Public method mkcol

Creates a new collection/directory on a webdav server
param: string path
return: int status code received as response from webdav server (see rfc 2518)

get($path, &$buffer, $fp = null)   X-Ref
Public method get

Gets a file from a webdav collection.
param: string $path the path to the file on the webdav server
param: string &$buffer the buffer to store the data in
param: resource $fp optional if included, the data is written directly to this resource and not to the buffer
return: string|bool status code and &$buffer (by reference) with response data from server on success. False on error.

put($path, $data )   X-Ref
Public method put

Puts a file into a collection.
Data is putted as one chunk!
param: string path, string data
return: int status-code read from webdavserver. False on error.

put_file($path, $filename)   X-Ref
Public method put_file

Read a file as stream and puts it chunk by chunk into webdav server collection.

Look at php documenation for legal filenames with fopen();
The filename will be translated into utf-8 if not allready in utf-8.

param: string targetpath, string filename
return: int status code. False on error.

get_file($srcpath, $localpath)   X-Ref
Public method get_file

Gets a file from a collection into local filesystem.

fopen() is used.
param: string $srcpath
param: string $localpath
return: bool true on success. false on error.

copy_file($src_path, $dst_path, $overwrite)   X-Ref
Public method copy_file

Copies a file on a webdav server

Duplicates a file on the webdav server (serverside).
All work is done on the webdav server. If you set param overwrite as true,
the target will be overwritten.

param: string src_path, string dest_path, bool overwrite
return: int status code (look at rfc 2518). false on error.

copy_coll($src_path, $dst_path, $overwrite)   X-Ref
Public method copy_coll

Copies a collection on a webdav server

Duplicates a collection on the webdav server (serverside).
All work is done on the webdav server. If you set param overwrite as true,
the target will be overwritten.

param: string src_path, string dest_path, bool overwrite
return: int status code (look at rfc 2518). false on error.

move($src_path,$dst_path, $overwrite)   X-Ref
No description

lock($path)   X-Ref
Public method lock

Locks a file or collection.

Lock uses this->_user as lock owner.

param: string path
return: int status code (look at rfc 2518). false on error.

unlock($path, $locktoken)   X-Ref
Public method unlock

Unlocks a file or collection.

param: string path, string locktoken
return: int status code (look at rfc 2518). false on error.

delete($path)   X-Ref
Public method delete

deletes a collection/directory on a webdav server
param: string path
return: int status code (look at rfc 2518). false on error.

ls($path)   X-Ref
Public method ls

Get's directory information from webdav server into flat a array using PROPFIND

All filenames are UTF-8 encoded.
Have a look at _propfind_startElement what keys are used in array returned.
param: string path
return: array dirinfo, false on error

gpi($path)   X-Ref
Public method gpi

Get's path information from webdav server for one element.

param: string path
return: array dirinfo. false on error

is_file($path)   X-Ref
Public method is_file

Gathers whether a path points to a file or not.

param: string path
return: bool true or false

is_dir($path)   X-Ref
Public method is_dir

Gather whether a path points to a directory
param: string path

mput($filelist)   X-Ref
Public method mput

Puts multiple files and/or directories onto a webdav server.

Filenames should be allready UTF-8 encoded.
Param fileList must be in format array("localpath" => "destpath").

param: array filelist
return: bool true on success. otherwise int status code on error

mget($filelist)   X-Ref
Public method mget

Gets multiple files and directories.

FileList must be in format array("remotepath" => "localpath").
Filenames are UTF-8 encoded.

param: array filelist
return: bool true on succes, other int status code on error

_endElement($parser, $name)   X-Ref
Private method _endelement

a generic endElement method  (used for all xml callbacks).

param: resource parser, string name

_propfind_startElement($parser, $name, $attrs)   X-Ref
Private method _propfind_startElement

Is needed by public method ls.

Generic method will called by php xml_parse when a xml start element tag has been detected.
The xml tree will translated into a flat php array for easier access.
param: resource parser, string name, string attrs

_propfind_cData($parser, $cdata)   X-Ref
Private method _propfind_cData

Is needed by public method ls.

Will be called by php xml_set_character_data_handler() when xml data has to be handled.
Stores data found into class var _ls_ref_cdata
param: resource parser, string cdata

_delete_startElement($parser, $name, $attrs)   X-Ref
Private method _delete_startElement

Is used by public method delete.

Will be called by php xml_parse.
param: resource parser, string name, string attrs)

_delete_cData($parser, $cdata)   X-Ref
Private method _delete_cData

Is used by public method delete.

Will be called by php xml_set_character_data_handler() when xml data has to be handled.
Stores data found into class var _delete_ref_cdata
param: resource parser, string cdata

_lock_startElement($parser, $name, $attrs)   X-Ref
Private method _lock_startElement

Is needed by public method lock.

Mmethod will called by php xml_parse when a xml start element tag has been detected.
The xml tree will translated into a flat php array for easier access.
param: resource parser, string name, string attrs

_lock_cData($parser, $cdata)   X-Ref
Private method _lock_cData

Is used by public method lock.

Will be called by php xml_set_character_data_handler() when xml data has to be handled.
Stores data found into class var _lock_ref_cdata
param: resource parser, string cdata

header_add($string)   X-Ref
Private method header_add

extends class var array _req
param: string string

header_unset()   X-Ref
Private method header_unset

unsets class var array _req

create_basic_request($method)   X-Ref
Private method create_basic_request

creates by using private method header_add an general request header.
param: string method

digest_auth()   X-Ref
Reads the header, stores the challenge information

return: void

digest_signature($method)   X-Ref
Generates the digest signature

return: string signature to add to the headers

send_request()   X-Ref
Private method send_request

Sends a ready formed http/webdav request to webdav server.


get_respond($fp = null)   X-Ref
Private method get_respond

Reads the response from the webdav server.

Stores data into class vars _header for the header data and
_body for the rest of the response.
This routine is the weakest part of this class, because it very depends how php does handle a socket stream.
If the stream is blocked for some reason php is blocked as well.
param: resource $fp optional the file handle to write the body content to (stored internally in the '_body' if not set)

update_file_or_buffer($chunk, $fp, &$buffer)   X-Ref
Write the chunk to the file if $fp is set, otherwise append the data to the buffer

param: string $chunk the data to add
param: resource $fp the file handle to write to (or null)
param: string &$buffer the buffer to append to (if $fp is null)

process_respond()   X-Ref
Private method process_respond

Processes the webdav server respond and detects its components (header, body).
and returns data array structure.
return: array ret_struct

reopen()   X-Ref
Private method reopen

Reopens a socket, if 'connection: closed'-header was received from server.

Uses public method open.

translate_uri($uri)   X-Ref
Private method translate_uri

translates an uri to raw url encoded string.
Removes any html entity in uri
param: string uri
return: string translated_uri

utf_decode_path($path)   X-Ref
Private method utf_decode_path

decodes a UTF-8 encoded string
return: string decodedstring

_error_log($err_string)   X-Ref
Private method _error_log

a simple php error_log wrapper.
param: string err_string