2022-10-10 23:45:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
|
|
|
{
|
|
|
|
const
|
|
|
|
NAME = 'Nextcloud',
|
2022-10-13 20:07:04 +08:00
|
|
|
VERSION = '2.2',
|
|
|
|
RELEASE = '2022-10-13',
|
2022-10-10 23:45:48 +08:00
|
|
|
CATEGORY = 'Integrations',
|
2022-10-13 00:45:26 +08:00
|
|
|
DESCRIPTION = 'Integrate with Nextcloud v20+',
|
|
|
|
REQUIRED = '2.18.6';
|
2022-10-10 23:45:48 +08:00
|
|
|
|
|
|
|
public function Init() : void
|
|
|
|
{
|
2022-10-11 04:06:43 +08:00
|
|
|
if (static::IsIntegrated()) {
|
2022-10-13 00:45:26 +08:00
|
|
|
$this->UseLangs(true);
|
|
|
|
|
2022-10-11 04:06:43 +08:00
|
|
|
$this->addHook('main.fabrica', 'MainFabrica');
|
|
|
|
$this->addHook('filter.app-data', 'FilterAppData');
|
|
|
|
|
2022-10-13 20:07:04 +08:00
|
|
|
$this->addJs('js/message.js');
|
2022-10-13 00:45:26 +08:00
|
|
|
$this->addHook('json.attachments', 'DoAttachmentsActions');
|
2022-10-13 20:07:04 +08:00
|
|
|
$this->addJsonHook('NextcloudSaveMsg', 'NextcloudSaveMsg');
|
2022-10-11 04:06:43 +08:00
|
|
|
}
|
2022-10-10 23:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function Supported() : string
|
|
|
|
{
|
2022-10-11 04:06:43 +08:00
|
|
|
return static::IsIntegrated() ? '' : 'Nextcloud not found to use this plugin';
|
2022-10-10 23:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function IsIntegrated()
|
|
|
|
{
|
2022-10-11 04:06:43 +08:00
|
|
|
return !empty($_ENV['SNAPPYMAIL_NEXTCLOUD']) && \class_exists('OC') && isset(\OC::$server);
|
2022-10-10 23:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function IsLoggedIn()
|
|
|
|
{
|
|
|
|
return static::IsIntegrated() && \OC::$server->getUserSession()->isLoggedIn();
|
|
|
|
}
|
|
|
|
|
2022-10-13 00:45:26 +08:00
|
|
|
/*
|
|
|
|
\OC::$server->getCalendarManager();
|
|
|
|
\OC::$server->getLDAPProvider();
|
|
|
|
|
|
|
|
$oFiles = \OCP\Files::getStorage('files');
|
|
|
|
if ($oFiles and $oFiles->is_dir('/')) {
|
|
|
|
$dh = $oFiles->opendir('/');
|
|
|
|
if (\is_resource($dh)) {
|
|
|
|
while (($file = \readdir($dh)) !== false) {
|
|
|
|
if ($file != '.' && $file != '..') {
|
|
|
|
// DO THINGS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2022-10-13 20:07:04 +08:00
|
|
|
public function NextcloudSaveMsg() : array
|
|
|
|
{
|
|
|
|
$aValues = \RainLoop\Utils::DecodeKeyValuesQ($this->jsonParam('msgHash', ''));
|
|
|
|
if (!empty($aValues['Folder']) && !empty($aValues['Uid'])) {
|
|
|
|
$oActions = \RainLoop\Api::Actions();
|
|
|
|
$oMailClient = $oActions->MailClient();
|
|
|
|
if (!$oMailClient->IsLoggined()) {
|
|
|
|
$oAccount = $oActions->getAccountFromToken();
|
|
|
|
$oAccount->ImapConnectAndLoginHelper($oActions->Plugins(), $oMailClient, $oActions->Config());
|
|
|
|
}
|
|
|
|
|
|
|
|
$sSaveFolder = $this->Config()->Get('plugin', 'save_folder', '') ?: 'Emails';
|
|
|
|
$oFiles = \OCP\Files::getStorage('files');
|
|
|
|
if ($oFiles && \method_exists($oFiles, 'file_put_contents')) {
|
|
|
|
$oFiles->is_dir($sSaveFolder) || $oFiles->mkdir($sSaveFolder);
|
|
|
|
}
|
|
|
|
$sFilename = $sSaveFolder . '/' . ($this->jsonParam('filename', '') ?: \date('YmdHis')) . '.eml';
|
|
|
|
|
|
|
|
$oMailClient->MessageMimeStream(
|
|
|
|
function ($rResource) use ($oFiles, $sFilename) {
|
|
|
|
if (\is_resource($rResource)) {
|
|
|
|
$oFiles->file_put_contents($sFilename, $rResource);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(string) $aValues['Folder'],
|
|
|
|
(int) $aValues['Uid'],
|
|
|
|
isset($aValues['MimeIndex']) ? (string) $aValues['MimeIndex'] : ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->jsonResponse(__FUNCTION__, true);
|
|
|
|
}
|
|
|
|
|
2022-10-12 20:06:11 +08:00
|
|
|
public function DoAttachmentsActions(\SnappyMail\AttachmentsAction $data)
|
2022-10-11 04:06:43 +08:00
|
|
|
{
|
2022-10-13 00:45:26 +08:00
|
|
|
if (static::isLoggedIn() && 'nextcloud' === $data->action) {
|
2022-10-11 04:06:43 +08:00
|
|
|
$oFiles = \OCP\Files::getStorage('files');
|
2022-10-13 00:45:26 +08:00
|
|
|
if ($oFiles && \method_exists($oFiles, 'file_put_contents')) {
|
2022-10-11 04:06:43 +08:00
|
|
|
$sSaveFolder = $this->Config()->Get('plugin', 'save_folder', '') ?: 'Attachments';
|
|
|
|
$oFiles->is_dir($sSaveFolder) || $oFiles->mkdir($sSaveFolder);
|
|
|
|
$data->result = true;
|
|
|
|
foreach ($data->items as $aItem) {
|
|
|
|
$sSavedFileName = isset($aItem['FileName']) ? $aItem['FileName'] : 'file.dat';
|
|
|
|
$sSavedFileHash = !empty($aItem['FileHash']) ? $aItem['FileHash'] : '';
|
|
|
|
if (!empty($sSavedFileHash)) {
|
|
|
|
$fFile = $data->filesProvider->GetFile($data->account, $sSavedFileHash, 'rb');
|
|
|
|
if (\is_resource($fFile)) {
|
|
|
|
$sSavedFileNameFull = \MailSo\Base\Utils::SmartFileExists($sSaveFolder.'/'.$sSavedFileName, function ($sPath) use ($oFiles) {
|
|
|
|
return $oFiles->file_exists($sPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!$oFiles->file_put_contents($sSavedFileNameFull, $fFile)) {
|
|
|
|
$data->result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\is_resource($fFile)) {
|
|
|
|
\fclose($fFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($data->items as $aItem) {
|
|
|
|
$sFileHash = (string) (isset($aItem['FileHash']) ? $aItem['FileHash'] : '');
|
|
|
|
if (!empty($sFileHash)) {
|
|
|
|
$data->filesProvider->Clear($data->account, $sFileHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function FilterAppData($bAdmin, &$aResult) : void
|
|
|
|
{
|
2022-10-13 00:45:26 +08:00
|
|
|
if (!$bAdmin && \is_array($aResult)) {
|
2022-10-11 04:06:43 +08:00
|
|
|
$key = \array_search(\RainLoop\Enumerations\Capa::AUTOLOGOUT, $aResult['Capa']);
|
|
|
|
if (false !== $key) {
|
|
|
|
unset($aResult['Capa'][$key]);
|
|
|
|
}
|
2022-10-13 02:14:30 +08:00
|
|
|
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
|
|
|
|
$sWebDAV = \OC::$server->getURLGenerator()->linkTo('', 'remote.php') . '/dav/';
|
|
|
|
// $sWebDAV = \OCP\Util::linkToRemote('dav');
|
|
|
|
$aResult['Nextcloud'] = [
|
|
|
|
'UID' => $sUID,
|
|
|
|
'WebDAV' => $sWebDAV
|
|
|
|
// 'WebDAV_files' => $sWebDAV . '/files/' . $sUID
|
|
|
|
];
|
2022-10-11 04:06:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 23:45:48 +08:00
|
|
|
/**
|
|
|
|
* @param mixed $mResult
|
|
|
|
*/
|
|
|
|
public function MainFabrica(string $sName, &$mResult)
|
|
|
|
{
|
2022-10-11 00:41:15 +08:00
|
|
|
if (static::isLoggedIn()) {
|
2022-10-11 04:06:43 +08:00
|
|
|
if ('suggestions' === $sName && $this->Config()->Get('plugin', 'suggestions', true)) {
|
2022-10-11 00:41:15 +08:00
|
|
|
if (!\is_array($mResult)) {
|
|
|
|
$mResult = array();
|
|
|
|
}
|
|
|
|
include_once __DIR__ . '/NextcloudContactsSuggestions.php';
|
|
|
|
$mResult[] = new NextcloudContactsSuggestions();
|
2022-10-10 23:45:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 04:06:43 +08:00
|
|
|
|
|
|
|
protected function configMapping() : array
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
\RainLoop\Plugins\Property::NewInstance('save_folder')->SetLabel('Save Folder')
|
|
|
|
->SetDefaultValue('Attachments'),
|
|
|
|
\RainLoop\Plugins\Property::NewInstance('suggestions')->SetLabel('Suggestions')
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
|
|
|
|
->SetDefaultValue(true)
|
|
|
|
);
|
|
|
|
}
|
2022-10-10 23:45:48 +08:00
|
|
|
}
|