2022-10-10 23:45:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
|
|
|
|
{
|
|
|
|
const
|
|
|
|
NAME = 'Nextcloud',
|
2023-01-04 23:11:10 +08:00
|
|
|
VERSION = '2.14',
|
|
|
|
RELEASE = '2023-01-04',
|
2022-10-10 23:45:48 +08:00
|
|
|
CATEGORY = 'Integrations',
|
2022-10-13 00:45:26 +08:00
|
|
|
DESCRIPTION = 'Integrate with Nextcloud v20+',
|
2022-12-16 16:45:18 +08:00
|
|
|
REQUIRED = '2.23.0';
|
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-11-10 22:26:35 +08:00
|
|
|
$this->addCss('style.css');
|
|
|
|
|
2022-10-19 16:15:03 +08:00
|
|
|
$this->addJs('js/webdav.js');
|
2022-10-21 18:17:49 +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-21 18:17:49 +08:00
|
|
|
|
|
|
|
$this->addJs('js/composer.js');
|
2022-10-24 22:12:35 +08:00
|
|
|
$this->addJsonHook('NextcloudAttachFile', 'NextcloudAttachFile');
|
|
|
|
|
|
|
|
$this->addJs('js/messagelist.js');
|
2022-10-21 18:17:49 +08:00
|
|
|
|
2022-10-19 01:21:27 +08:00
|
|
|
$this->addTemplate('templates/PopupsNextcloudFiles.html');
|
2022-10-25 22:08:28 +08:00
|
|
|
$this->addTemplate('templates/PopupsNextcloudCalendars.html');
|
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-11-02 04:53:39 +08:00
|
|
|
return \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();
|
|
|
|
*/
|
|
|
|
|
2022-10-21 18:17:49 +08:00
|
|
|
public function NextcloudAttachFile() : array
|
|
|
|
{
|
|
|
|
$aResult = [
|
|
|
|
'success' => false,
|
|
|
|
'tempName' => ''
|
|
|
|
];
|
|
|
|
$sFile = $this->jsonParam('file', '');
|
|
|
|
$oFiles = \OCP\Files::getStorage('files');
|
|
|
|
if ($oFiles && $oFiles->is_file($sFile) && $fp = $oFiles->fopen($sFile, 'rb')) {
|
|
|
|
$oActions = \RainLoop\Api::Actions();
|
|
|
|
$oAccount = $oActions->getAccountFromToken();
|
|
|
|
if ($oAccount) {
|
|
|
|
$sSavedName = 'nextcloud-file-' . \sha1($sFile . \microtime());
|
|
|
|
if (!$oActions->FilesProvider()->PutFile($oAccount, $sSavedName, $fp)) {
|
|
|
|
$aResult['error'] = 'failed';
|
|
|
|
} else {
|
|
|
|
$aResult['tempName'] = $sSavedName;
|
|
|
|
$aResult['success'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->jsonResponse(__FUNCTION__, $aResult);
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:07:04 +08:00
|
|
|
public function NextcloudSaveMsg() : array
|
|
|
|
{
|
2022-10-19 16:15:03 +08:00
|
|
|
$sSaveFolder = \ltrim($this->jsonParam('folder', ''), '/');
|
2022-10-13 20:07:04 +08:00
|
|
|
$aValues = \RainLoop\Utils::DecodeKeyValuesQ($this->jsonParam('msgHash', ''));
|
2022-10-19 16:15:03 +08:00
|
|
|
$aResult = [
|
|
|
|
'folder' => '',
|
|
|
|
'filename' => '',
|
|
|
|
'success' => false
|
|
|
|
];
|
|
|
|
if ($sSaveFolder && !empty($aValues['Folder']) && !empty($aValues['Uid'])) {
|
2022-10-13 20:07:04 +08:00
|
|
|
$oActions = \RainLoop\Api::Actions();
|
|
|
|
$oMailClient = $oActions->MailClient();
|
|
|
|
if (!$oMailClient->IsLoggined()) {
|
|
|
|
$oAccount = $oActions->getAccountFromToken();
|
2022-12-16 16:45:18 +08:00
|
|
|
$oAccount->ImapConnectAndLoginHelper($oActions->Plugins(), $oMailClient->ImapClient(), $oActions->Config());
|
2022-10-13 20:07:04 +08:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:15:03 +08:00
|
|
|
$sSaveFolder = $sSaveFolder ?: 'Emails';
|
2022-10-13 20:07:04 +08:00
|
|
|
$oFiles = \OCP\Files::getStorage('files');
|
2022-10-19 16:15:03 +08:00
|
|
|
if ($oFiles) {
|
2022-10-13 20:07:04 +08:00
|
|
|
$oFiles->is_dir($sSaveFolder) || $oFiles->mkdir($sSaveFolder);
|
|
|
|
}
|
2022-10-19 16:15:03 +08:00
|
|
|
$aResult['folder'] = $sSaveFolder;
|
|
|
|
|
2022-10-13 20:07:04 +08:00
|
|
|
$sFilename = $sSaveFolder . '/' . ($this->jsonParam('filename', '') ?: \date('YmdHis')) . '.eml';
|
2022-10-19 16:15:03 +08:00
|
|
|
$aResult['folder'] = $sFilename;
|
2022-10-13 20:07:04 +08:00
|
|
|
|
|
|
|
$oMailClient->MessageMimeStream(
|
2022-10-19 16:15:03 +08:00
|
|
|
function ($rResource) use ($oFiles, $sFilename, $aResult) {
|
2022-10-13 20:07:04 +08:00
|
|
|
if (\is_resource($rResource)) {
|
2022-10-19 16:15:03 +08:00
|
|
|
$aResult['success'] = $oFiles->file_put_contents($sFilename, $rResource);
|
2022-10-13 20:07:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(string) $aValues['Folder'],
|
|
|
|
(int) $aValues['Uid'],
|
|
|
|
isset($aValues['MimeIndex']) ? (string) $aValues['MimeIndex'] : ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-19 16:15:03 +08:00
|
|
|
return $this->jsonResponse(__FUNCTION__, $aResult);
|
2022-10-13 20:07:04 +08:00
|
|
|
}
|
|
|
|
|
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-19 16:15:03 +08:00
|
|
|
$sSaveFolder = \ltrim($this->jsonParam('NcFolder', ''), '/');
|
2022-10-21 18:17:49 +08:00
|
|
|
$sSaveFolder = $sSaveFolder ?: 'Attachments';
|
2022-10-11 04:06:43 +08:00
|
|
|
$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)) {
|
2022-10-19 16:15:03 +08:00
|
|
|
$sSavedFileNameFull = static::SmartFileExists($sSaveFolder.'/'.$sSavedFileName, $oFiles);
|
2022-10-11 04:06:43 +08:00
|
|
|
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-13 02:14:30 +08:00
|
|
|
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
|
2022-11-10 20:57:53 +08:00
|
|
|
$sWebDAV = \OC::$server->getURLGenerator()->linkTo('', 'remote.php') . '/dav';
|
2022-10-13 02:14:30 +08:00
|
|
|
// $sWebDAV = \OCP\Util::linkToRemote('dav');
|
|
|
|
$aResult['Nextcloud'] = [
|
|
|
|
'UID' => $sUID,
|
2022-10-25 23:06:00 +08:00
|
|
|
'WebDAV' => $sWebDAV,
|
|
|
|
'CalDAV' => $this->Config()->Get('plugin', 'calendar', false)
|
2022-10-13 02:14:30 +08:00
|
|
|
// 'WebDAV_files' => $sWebDAV . '/files/' . $sUID
|
|
|
|
];
|
2022-10-14 05:46:37 +08:00
|
|
|
if (empty($aResult['Auth'])) {
|
|
|
|
$config = \OC::$server->getConfig();
|
2022-11-02 04:53:39 +08:00
|
|
|
$sEmail = '';
|
2022-10-14 05:46:37 +08:00
|
|
|
// Only store the user's password in the current session if they have
|
|
|
|
// enabled auto-login using Nextcloud username or email address.
|
|
|
|
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
|
|
|
|
$sEmail = $sUID;
|
|
|
|
} else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
|
|
|
|
$sEmail = $config->getUserValue($sUID, 'settings', 'email', '');
|
|
|
|
}
|
|
|
|
// If the user has set credentials for SnappyMail in their personal
|
|
|
|
// settings, override everything before and use those instead.
|
|
|
|
$sCustomEmail = $config->getUserValue($sUID, 'snappymail', 'snappymail-email', '');
|
|
|
|
if ($sCustomEmail) {
|
|
|
|
$sEmail = $sCustomEmail;
|
|
|
|
}
|
|
|
|
$aResult['DevEmail'] = $sEmail ?: '';
|
|
|
|
}
|
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('suggestions')->SetLabel('Suggestions')
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
|
2022-10-25 23:06:00 +08:00
|
|
|
->SetDefaultValue(true),
|
|
|
|
\RainLoop\Plugins\Property::NewInstance('calendar')->SetLabel('Enable "Put ICS in calendar"')
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
|
|
|
|
->SetDefaultValue(false)
|
2022-10-11 04:06:43 +08:00
|
|
|
);
|
|
|
|
}
|
2022-10-19 16:15:03 +08:00
|
|
|
|
|
|
|
private static function SmartFileExists(string $sFilePath, $oFiles) : string
|
|
|
|
{
|
|
|
|
$sFilePath = \str_replace('\\', '/', \trim($sFilePath));
|
|
|
|
|
|
|
|
if (!$oFiles->file_exists($sFilePath)) {
|
|
|
|
return $sFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
$aFileInfo = \pathinfo($sFilePath);
|
|
|
|
|
|
|
|
$iIndex = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
++$iIndex;
|
|
|
|
$sFilePathNew = $aFileInfo['dirname'].'/'.
|
|
|
|
\preg_replace('/\(\d{1,2}\)$/', '', $aFileInfo['filename']).
|
|
|
|
' ('.$iIndex.')'.
|
|
|
|
(empty($aFileInfo['extension']) ? '' : '.'.$aFileInfo['extension'])
|
|
|
|
;
|
|
|
|
if (!$oFiles->file_exists($sFilePathNew)) {
|
|
|
|
return $sFilePathNew;
|
|
|
|
}
|
|
|
|
if (10 < $iIndex) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sFilePath;
|
|
|
|
}
|
2022-10-10 23:45:48 +08:00
|
|
|
}
|