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-06-14 23:55:05 -05:00
|
|
|
use Monolog\Logger as Monolog;
|
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;
|
|
|
|
|
|
|
|
/**
|
2021-07-05 16:41:38 -05:00
|
|
|
* The plugin to interact with Baikal and external storages
|
2020-05-03 16:41:59 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Plugin extends ServerPlugin {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to server object.
|
|
|
|
*
|
|
|
|
* @var Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StorageManager
|
|
|
|
*/
|
|
|
|
protected $storageManager;
|
|
|
|
|
2020-10-09 01:12:57 -05:00
|
|
|
/**
|
2021-07-05 16:41:38 -05:00
|
|
|
* @var $rawconfigs
|
2020-10-09 01:12:57 -05:00
|
|
|
*/
|
2020-06-14 23:55:05 -05:00
|
|
|
protected $rawConfigs;
|
2021-07-05 16:41:38 -05:00
|
|
|
|
2020-05-03 16:41:59 -05:00
|
|
|
/**
|
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){
|
2020-06-14 23:55:05 -05:00
|
|
|
$this->rawConfigs = $this->buildConfigurations($configFile);
|
|
|
|
$this->storageManager = new StorageManager($this->rawConfigs);
|
2021-07-05 16:41:38 -05:00
|
|
|
$this->logger = new Logger($this->rawConfigs, 'BaikalStorage');
|
2020-06-14 23:55:05 -05:00
|
|
|
$this->initializeStorages($this->rawConfigs);
|
2020-05-12 23:56:22 -05:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:15:12 -05:00
|
|
|
private function getDisplayName($path) {
|
2021-07-05 16:41:38 -05:00
|
|
|
// Remove filepath
|
2020-09-28 23:48:55 -05:00
|
|
|
$urlParts = explode('/', $path);
|
2020-10-09 01:12:57 -05:00
|
|
|
$calendarUrl = implode('/', array_slice($urlParts, 0, sizeof($urlParts)-1));
|
2020-09-18 22:15:12 -05:00
|
|
|
|
2020-10-09 01:12:57 -05:00
|
|
|
// Get displayname from collection
|
|
|
|
$properties = $this->server->getProperties($calendarUrl, ['{DAV:}displayname']);
|
|
|
|
return $properties['{DAV:}displayname'] ?? '';
|
2020-09-18 22:15:12 -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) {
|
2021-07-05 16:41:38 -05:00
|
|
|
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing',
|
2021-07-02 19:39:08 -05:00
|
|
|
'rc.hooks=off', 'rc.confirmation=no']), $configs, new Logger($configs, 'Taskwarrior'));
|
2021-07-05 16:41:38 -05:00
|
|
|
$this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
|
2020-06-14 23:55:05 -05:00
|
|
|
return 'baikal-storage';
|
2020-05-03 16:41:59 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This method is called before any HTTP method handler.
|
|
|
|
*
|
2021-07-05 16:41:38 -05:00
|
|
|
* This method intercepts any GET, DELETE, PUT and PROPFIND.
|
2020-05-03 16:41:59 -05:00
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function beforeMethod(RequestInterface $request, ResponseInterface $response)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch ($request->getMethod()) {
|
|
|
|
case 'PUT':
|
|
|
|
$this->httpPut($request, $response);
|
2020-06-14 23:55:05 -05:00
|
|
|
break;
|
2021-07-05 16:41:38 -05:00
|
|
|
case 'POST':
|
2020-06-14 23:55:05 -05:00
|
|
|
$this->httpPost($request, $response);
|
2020-06-27 00:56:44 -05:00
|
|
|
break;
|
|
|
|
case 'DELETE':
|
|
|
|
$this->httpDelete($request);
|
2020-06-14 23:55:05 -05:00
|
|
|
return;
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method handles the PUT method.
|
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function httpPut(RequestInterface $request){
|
|
|
|
$body = $request->getBodyAsString();
|
|
|
|
$vCal = \Sabre\VObject\Reader::read($body);
|
2020-09-18 22:15:12 -05:00
|
|
|
$displayname = $this->getDisplayName($request->getPath());
|
|
|
|
try {
|
2020-09-28 23:48:55 -05:00
|
|
|
if (!$this->storageManager->fromStorageSource($vCal)) {
|
2020-09-18 22:15:12 -05:00
|
|
|
$this->storageManager->import($vCal, $displayname);
|
|
|
|
}
|
|
|
|
} catch(BadRequest $e){
|
2020-05-03 16:41:59 -05:00
|
|
|
throw new BadRequest($e->getMessage(), null, $e);
|
2020-09-18 22:15:12 -05:00
|
|
|
} catch(\Exception $e){
|
2020-05-03 16:41:59 -05:00
|
|
|
throw new \Exception($e->getMessage(), null, $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->setBody($body);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:55:05 -05:00
|
|
|
/**
|
|
|
|
* This method handles the POST method.
|
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function httpPost(RequestInterface $request, ResponseInterface $response) {
|
|
|
|
$postVars = $request->getPostData();
|
|
|
|
$body = $request->getBodyAsString();
|
|
|
|
if (isset($postVars['baikalStorage'])) {
|
|
|
|
foreach ($this->storageManager->getStorages() as $storage) {
|
2021-07-05 16:41:38 -05:00
|
|
|
if ($storage::NAME == $postVars['baikalStorage']
|
2020-06-14 23:55:05 -05:00
|
|
|
&& $postVars['baikalStorageAction'] == 'saveConfigs') {
|
|
|
|
$updateStorageConfigs = $storage->updateConfigs($postVars);
|
|
|
|
$this->rawConfigs['storages'][$postVars['baikalStorage']] = $updateStorageConfigs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (isset($postVars['logLevel'])) {
|
|
|
|
$this->rawConfigs['general']['logger']['level'] = $postVars['logLevel'];
|
|
|
|
}
|
|
|
|
if (isset($postVars['logFilePath'])) {
|
|
|
|
$this->rawConfigs['general']['logger']['file'] = $postVars['logFilePath'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config->saveConfigs($this->rawConfigs);
|
|
|
|
|
|
|
|
$response->setHeader('Location', $request->getUrl());
|
|
|
|
$response->setStatus(302);
|
|
|
|
$request->setBody($body);
|
|
|
|
|
2021-07-05 16:41:38 -05:00
|
|
|
}
|
2020-06-27 00:56:44 -05:00
|
|
|
/**
|
|
|
|
* This method handles the DELETE method.
|
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function httpDelete(RequestInterface $request) {
|
|
|
|
try {
|
|
|
|
$body = $request->getBodyAsString();
|
|
|
|
$path = $request->getPath();
|
|
|
|
$paths = explode('/', $path);
|
2020-09-18 22:15:12 -05:00
|
|
|
if (sizeof($paths) > 1) {
|
2020-06-27 00:56:44 -05:00
|
|
|
$uid = str_replace('.ics', '', $paths[sizeof($paths)-1]);
|
2020-09-18 22:15:12 -05:00
|
|
|
// Attempt to delete if we are removing an ics file
|
2020-08-23 17:39:56 -05:00
|
|
|
if ($uid != '') {
|
|
|
|
$this->storageManager->remove($uid);
|
|
|
|
}
|
2020-06-27 00:56:44 -05:00
|
|
|
}
|
|
|
|
} catch(BadRequest $e){
|
|
|
|
throw new BadRequest($e->getMessage(), null, $e);
|
|
|
|
} catch(\Exception $e){
|
|
|
|
throw new \Exception($e->getMessage(), null, $e);
|
|
|
|
}
|
|
|
|
$request->setBody($body);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-05 16:41:38 -05:00
|
|
|
|
2020-06-14 23:55:05 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the 'general' configuration section
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function generateGeneralConfigSection() {
|
|
|
|
$configuredLogLevel = '';
|
|
|
|
$logFilePath = '';
|
|
|
|
if (isset($this->rawConfigs['general'])
|
2021-07-05 16:41:38 -05:00
|
|
|
&& isset($this->rawConfigs['general']['logger'])
|
2020-06-14 23:55:05 -05:00
|
|
|
&& $this->rawConfigs['general']['logger']['enabled']) {
|
|
|
|
$configuredLogLevel = $this->rawConfigs['general']['logger']['level'];
|
|
|
|
$logFilePath = $this->rawConfigs['general']['logger']['file'];
|
|
|
|
}
|
|
|
|
$html = '<form method="post" action="">';
|
|
|
|
$html .= '<section><h1>Configuration - Baikal Storage</h1>';
|
|
|
|
$html .= '<section><h2>general</h2>';
|
|
|
|
$html .= '<table class="propTable">';
|
|
|
|
$html .= '<tr>';
|
|
|
|
$html .= '<th>log level</th>';
|
|
|
|
$html .= '<td>The minimum log level </td>';
|
|
|
|
$html .= '<td>';
|
|
|
|
$html .= '<select name="logLevel">';
|
|
|
|
|
|
|
|
foreach (Monolog::getLevels() as $key => $value) {
|
|
|
|
if ($key == $configuredLogLevel) {
|
|
|
|
$selected = ' selected ';
|
|
|
|
} else {
|
|
|
|
$selected = '';
|
|
|
|
}
|
|
|
|
$html .= '<option value="'. $key .'"' . $selected . '>'. $key .'</option>';
|
|
|
|
}
|
|
|
|
$html .= '</select>';
|
|
|
|
|
|
|
|
$html .= '</tr>';
|
|
|
|
$html .= '<tr>';
|
|
|
|
$html .= '<th>log file path</th>';
|
|
|
|
$html .= '<td>The absolute file path of the log</td>';
|
|
|
|
$html .= '<td><input name="logFilePath" placeholder="/opt/baikal/log" value='. $logFilePath . ' type="text" id="logFilePath"></input></td>';
|
|
|
|
$html .= '</tr>';
|
|
|
|
$html .= '<tr>';
|
|
|
|
$html .= '</table>';
|
|
|
|
$html .= '</section>';
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a html to display an optional configuration page for the plugin
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getConfigBrowser() {
|
|
|
|
$html = $this->generateGeneralConfigSection();
|
|
|
|
|
|
|
|
foreach ($this->storageManager->getStorages() as $storage) {
|
|
|
|
$html .= '<section>';
|
|
|
|
$html .= '<h2>' . $storage::NAME . '</h2>';
|
|
|
|
$html .= '<table class="propTable">';
|
|
|
|
$html .= '<input type="hidden" name="baikalStorageAction" value="saveConfigs"></input>';
|
|
|
|
$html .= '<input type="hidden" name="baikalStorage" value="taskwarrior"></input>';
|
|
|
|
$html .= $storage->getConfigBrowser();
|
|
|
|
$html .= '</table>';
|
|
|
|
$html .= '</section>';
|
|
|
|
$html .= '<input type="submit" value="save"></input>';
|
|
|
|
$html .= '</form>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2020-05-03 16:41:59 -05:00
|
|
|
/**
|
|
|
|
* 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(),
|
2020-10-09 01:12:57 -05:00
|
|
|
'description' => 'The plugin provides synchronization between remote storages and iCal todo events',
|
|
|
|
'link' => 'https://git.aerex.me/Aerex/baikal-storage-plugin',
|
2020-06-14 23:55:05 -05:00
|
|
|
'config' => true
|
2020-05-03 16:41:59 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|