2020-05-03 16:41:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aerex\BaikalStorage;
|
|
|
|
|
|
|
|
use Aerex\BaikalStorage\Storages\Taskwarrior;
|
|
|
|
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
|
|
|
use Aerex\BaikalStorage\Configs\TaskwarriorConfig;
|
|
|
|
use Sabre\DAV\Exception\BadRequest;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
use Sabre\DAV\Server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The plugin to interact with Baikal and external storages
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Plugin extends ServerPlugin {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to server object.
|
|
|
|
*
|
|
|
|
* @var Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StorageManager
|
|
|
|
*/
|
|
|
|
protected $storageManager;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the Taskwarrior plugin
|
|
|
|
*
|
|
|
|
* @param CalendarProcessor $TWCalManager
|
|
|
|
*
|
|
|
|
*/
|
2020-05-12 23:56:22 -05:00
|
|
|
function __construct($configDir){
|
|
|
|
$configs = $this->buildConfigurations($configDir);
|
|
|
|
$this->storageManager = new StorageManager($configs);
|
|
|
|
$this->initializeStorages($configDir, $configs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildConfigurations($configDir) {
|
|
|
|
$this->config = new ConfigBuilder($configDir);
|
|
|
|
$this->config->add(new TaskwarriorConfig());
|
|
|
|
return $this->config->loadYaml();
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure available storages in storage manager
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-05-12 23:56:22 -05:00
|
|
|
public function initializeStorages($configDir, $configs) {
|
|
|
|
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configDir, $configs);
|
2020-05-03 16:41:59 -05:00
|
|
|
$this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the plugin
|
|
|
|
*
|
|
|
|
* @param Server $server
|
|
|
|
* @return void */
|
|
|
|
function initialize(Server $server) {
|
|
|
|
$this->server = $server;
|
|
|
|
$server->on('beforeMethod:*', [$this, 'beforeMethod'], 15);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a plugin name.
|
|
|
|
*
|
|
|
|
* Using this name other plugins will be able to access other plugins
|
|
|
|
* using DAV\Server::getPlugin
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getPluginName() {
|
|
|
|
|
|
|
|
return 'taskwarrior';
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This method is called before any HTTP method handler.
|
|
|
|
*
|
|
|
|
* This method intercepts any GET, DELETE, PUT and PROPFIND calls to
|
|
|
|
* filenames that are known to match the 'temporary file' regex.
|
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function beforeMethod(RequestInterface $request, ResponseInterface $response)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch ($request->getMethod()) {
|
|
|
|
case 'PUT':
|
|
|
|
$this->httpPut($request, $response);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method handles the PUT method.
|
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function httpPut(RequestInterface $request){
|
|
|
|
$body = $request->getBodyAsString();
|
|
|
|
$vCal = \Sabre\VObject\Reader::read($body);
|
|
|
|
try {
|
|
|
|
$this->storageManager->import($vCal);
|
|
|
|
} catch(BadRequest $e){
|
|
|
|
throw new BadRequest($e->getMessage(), null, $e);
|
|
|
|
} catch(\Exception $e){
|
|
|
|
throw new \Exception($e->getMessage(), null, $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->setBody($body);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a bunch of meta-data about the plugin.
|
|
|
|
*
|
|
|
|
* Providing this information is optional, and is mainly displayed by the
|
|
|
|
* Browser plugin.
|
|
|
|
*
|
|
|
|
* The description key in the returned array may contain html and will not
|
|
|
|
* be sanitized.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getPluginInfo() {
|
|
|
|
|
|
|
|
return [
|
|
|
|
'name' => $this->getPluginName(),
|
|
|
|
'description' => 'The plugin provides synchronization between taskwarrior tasks and iCAL events',
|
|
|
|
'link' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|