2020-05-03 16:41:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aerex\BaikalStorage;
|
|
|
|
|
2020-06-11 11:43:56 -05:00
|
|
|
use Aerex\BaikalStorage\Logger;
|
2020-05-03 16:41:59 -05:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-06-11 11:43:56 -05:00
|
|
|
* Creates the Storage plugin
|
2020-05-03 16:41:59 -05:00
|
|
|
*
|
|
|
|
* @param CalendarProcessor $TWCalManager
|
|
|
|
*
|
|
|
|
*/
|
2020-05-28 11:54:21 -05:00
|
|
|
function __construct($configFile){
|
|
|
|
$configs = $this->buildConfigurations($configFile);
|
2020-05-12 23:56:22 -05:00
|
|
|
$this->storageManager = new StorageManager($configs);
|
2020-05-28 11:54:21 -05:00
|
|
|
$this->initializeStorages($configs);
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
|
2020-05-28 11:54:21 -05:00
|
|
|
public function buildConfigurations($configFile) {
|
|
|
|
$this->config = new ConfigBuilder($configFile);
|
2020-05-12 23:56:22 -05:00
|
|
|
$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-28 11:54:21 -05:00
|
|
|
public function initializeStorages($configs) {
|
2020-06-11 11:43:56 -05:00
|
|
|
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configs, new Logger($configs, 'Taskwarrior'););
|
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,
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|