1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\FullTextSearch\Utils; |
3
|
|
|
|
4
|
|
|
class WebDAV |
5
|
|
|
{ |
6
|
|
|
public static function curl_init($url, $method) |
7
|
|
|
{ |
8
|
|
|
$ch = curl_init($url); |
9
|
|
|
|
10
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
11
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
12
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
13
|
|
|
|
14
|
|
|
return $ch; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function exists($url) |
18
|
|
|
{ |
19
|
|
|
// WebDAV expects that checking a directory exists has a trailing slash |
20
|
|
|
if (substr($url, -1) != '/') { |
21
|
|
|
$url .= '/'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$ch = self::curl_init($url, 'PROPFIND'); |
25
|
|
|
|
26
|
|
|
$res = curl_exec($ch); |
|
|
|
|
27
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
28
|
|
|
|
29
|
|
|
if ($code == 404) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
if ($code == 200 || $code == 207) { |
33
|
|
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
user_error("Got error from webdav server - " . $code, E_USER_ERROR); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function mkdir($url) |
40
|
|
|
{ |
41
|
|
|
$ch = self::curl_init(rtrim($url, '/') . '/', 'MKCOL'); |
42
|
|
|
|
43
|
|
|
$res = curl_exec($ch); |
|
|
|
|
44
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
45
|
|
|
|
46
|
|
|
return $code == 201; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function put($handle, $url) |
50
|
|
|
{ |
51
|
|
|
$ch = curl_init($url); |
52
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
53
|
|
|
|
54
|
|
|
curl_setopt($ch, CURLOPT_PUT, true); |
55
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); |
|
|
|
|
56
|
|
|
|
57
|
|
|
curl_setopt($ch, CURLOPT_INFILE, $handle); |
58
|
|
|
|
59
|
|
|
$res = curl_exec($ch); |
|
|
|
|
60
|
|
|
fclose($handle); |
61
|
|
|
|
62
|
|
|
return curl_getinfo($ch, CURLINFO_HTTP_CODE); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function upload_from_string($string, $url) |
66
|
|
|
{ |
67
|
|
|
$fh = tmpfile(); |
68
|
|
|
fwrite($fh, $string); |
69
|
|
|
fseek($fh, 0); |
70
|
|
|
return self::put($fh, $url); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function upload_from_file($string, $url) |
74
|
|
|
{ |
75
|
|
|
return self::put(fopen($string, 'rb'), $url); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|