silverstripe-fulltextsearch/src/Utils/WebDAV.php

85 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2017-04-21 02:27:01 +02:00
namespace SilverStripe\FullTextSearch\Utils;
2015-11-21 07:19:20 +01:00
class WebDAV
{
public static function curl_init($url, $method)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
return $ch;
}
public static function exists($url)
{
// WebDAV expects that checking a directory exists has a trailing slash
2022-04-13 01:24:03 +02:00
if (substr($url ?? '', -1) != '/') {
2015-11-21 07:19:20 +01:00
$url .= '/';
}
$ch = self::curl_init($url, 'PROPFIND');
2022-01-07 11:40:31 +01:00
curl_exec($ch);
2015-11-21 07:19:20 +01:00
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2022-01-07 11:40:31 +01:00
$err = curl_error($ch);
curl_close($ch);
2015-11-21 07:19:20 +01:00
if ($code == 404) {
return false;
}
if ($code == 200 || $code == 207) {
return true;
}
user_error("Got error from webdav server - " . $err, E_USER_ERROR);
2015-11-21 07:19:20 +01:00
}
public static function mkdir($url)
{
2022-04-13 01:24:03 +02:00
$ch = self::curl_init(rtrim($url ?? '', '/') . '/', 'MKCOL');
2015-11-21 07:19:20 +01:00
2022-01-07 11:40:31 +01:00
curl_exec($ch);
2015-11-21 07:19:20 +01:00
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
2022-01-07 11:40:31 +01:00
curl_close($ch);
2015-11-21 07:19:20 +01:00
return $code == 201;
}
public static function put($handle, $url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $handle);
2022-01-07 11:40:31 +01:00
curl_exec($ch);
2015-11-21 07:19:20 +01:00
fclose($handle);
2022-01-07 11:40:31 +01:00
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
2015-11-21 07:19:20 +01:00
2022-01-07 11:40:31 +01:00
return $code;
2015-11-21 07:19:20 +01:00
}
public static function upload_from_string($string, $url)
{
$fh = tmpfile();
2022-04-13 01:24:03 +02:00
fwrite($fh, $string ?? '');
2015-11-21 07:19:20 +01:00
fseek($fh, 0);
return self::put($fh, $url);
}
public static function upload_from_file($string, $url)
{
2022-04-13 01:24:03 +02:00
return self::put(fopen($string ?? '', 'rb'), $url);
2015-11-21 07:19:20 +01:00
}
}