From 6e26c767c31b6edb51a6393155db13a3f20c66f9 Mon Sep 17 00:00:00 2001 From: djmaze Date: Fri, 16 Jul 2021 16:27:19 +0200 Subject: [PATCH] Changes for base OwnCloud/NextCloud See https://github.com/the-djmaze/snappymail/issues/96 --- plugins/README.md | 4 + plugins/owncloud/OwnCloudSuggestions.php | 114 +++++++++++ plugins/owncloud/index.php | 180 ++++++++++++++++++ .../app/libraries/RainLoop/Actions/User.php | 10 + .../snappymail/attachmentsaction.php | 13 ++ 5 files changed, 321 insertions(+) create mode 100644 plugins/owncloud/OwnCloudSuggestions.php create mode 100644 plugins/owncloud/index.php create mode 100644 snappymail/v/0.0.0/app/libraries/snappymail/attachmentsaction.php diff --git a/plugins/README.md b/plugins/README.md index 57279fb75..f42178f31 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -278,6 +278,10 @@ $Plugin->addHook('hook.name', 'functionName'); params: string $sAction +### json.attachments + params: + \SnappyMail\AttachmentsAction $oData + ### json.suggestions-input-parameters params: string &$sQuery diff --git a/plugins/owncloud/OwnCloudSuggestions.php b/plugins/owncloud/OwnCloudSuggestions.php new file mode 100644 index 000000000..cb873c485 --- /dev/null +++ b/plugins/owncloud/OwnCloudSuggestions.php @@ -0,0 +1,114 @@ +getContactsManager(); + if (!$cm && !$cm->isEnabled()) + { + return $aResult; + } + + $aSearchResult = $cm->search($sQuery, $aParams); + } + else if (\class_exists('OCP\Contacts') && \OCP\Contacts::isEnabled()) + { + $aSearchResult = \OCP\Contacts::search($sQuery, $aParams); + } + else + { + return $aResult; + } + + //$this->oLogger->WriteDump($aSearchResult); + + $aHashes = array(); + if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) + { + foreach ($aSearchResult as $aContact) + { + if (0 >= $iLimit) + { + break; + } + + $sUid = empty($aContact['UID']) ? '' : $aContact['UID']; + if (!empty($sUid)) + { + $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : ''; + + $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : ''; + if (empty($sFullName)) + { + $sFullName = isset($aContact['NICKNAME']) ? \trim($aContact['NICKNAME']) : ''; + } + + if (!\is_array($mEmails)) + { + $mEmails = array($mEmails); + } + + foreach ($mEmails as $sEmail) + { + $sHash = '"'.$sFullName.'" <'.$sEmail.'>'; + if (!isset($aHashes[$sHash])) + { + $aHashes[$sHash] = true; + $aResult[] = array($sEmail, $sFullName); + $iLimit--; + } + } + } + } + + $aResult = \array_slice($aResult, 0, $iInputLimit); + } + + unset($aSearchResult, $aHashes); + } + catch (\Throwable $oException) + { + if ($this->oLogger) + { + $this->oLogger->WriteException($oException); + } + } + + return $aResult; + } + + /** + * @param \MailSo\Log\Logger $oLogger + */ + public function SetLogger($oLogger) + { + $this->oLogger = $oLogger instanceof \MailSo\Log\Logger ? $oLogger : null; + } +} diff --git a/plugins/owncloud/index.php b/plugins/owncloud/index.php new file mode 100644 index 000000000..7fb6a2fcd --- /dev/null +++ b/plugins/owncloud/index.php @@ -0,0 +1,180 @@ +addHook('main.fabrica', 'MainFabrica'); + $this->addHook('filter.app-data', 'FilterAppData'); + $this->addHook('json.attachments', 'DoAttachmentsActions'); + + $sAppPath = ''; + if (\class_exists('OC_App')) { + $sAppPath = \rtrim(\trim(\OC_App::getAppWebPath('snappymail')), '\\/').'/app/'; + } + if (!$sAppPath) { + $sUrl = \MailSo\Base\Http::SingletonInstance()->GetUrl(); + if ($sUrl && \preg_match('/\/index\.php\/apps\/snappymail/', $sUrl)) { + $sAppPath = \preg_replace('/\/index\.php\/apps\/snappymail.+$/', + '/apps/snappymail/app/', $sUrl); + } + } + $_SERVER['SCRIPT_NAME'] = $sAppPath; + } + } + + public function Supported() : string + { + if (!static::IsOwnCloud()) { + return 'OwnCloud not found to use this plugin'; + } + return ''; + } + + // DoAttachmentsActions + public function DoAttachmentsActions(\SnappyMail\AttachmentsAction $data) + { + if ('owncloud' === $data->action) { + if (static::IsOwnCloudLoggedIn() && \class_exists('OCP\Files')) { + $oFiles = \OCP\Files::getStorage('files'); + if ($oFiles && $data->filesProvider->IsActive() && \method_exists($oFiles, 'file_put_contents')) { + $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); + } + } + } + } + + /** + * TODO: create pre-login auth hook + */ + public function ServiceOwnCloudAuth() + { +/* + $this->oHttp->ServerNoCache(); + + if (!static::IsOwnCloud() || + !isset($_ENV['___snappymail_owncloud_email']) || + !isset($_ENV['___snappymail_owncloud_password']) || + empty($_ENV['___snappymail_owncloud_email']) + ) + { + $this->oActions->SetAuthLogoutToken(); + $this->oActions->Location('./'); + return ''; + } + + $bLogout = true; + + $sEmail = $_ENV['___snappymail_owncloud_email']; + $sPassword = $_ENV['___snappymail_owncloud_password']; + + try + { + $oAccount = $this->oActions->LoginProcess($sEmail, $sPassword); + $this->oActions->AuthToken($oAccount); + + $bLogout = !($oAccount instanceof \snappymail\Model\Account); + } + catch (\Exception $oException) + { + $this->oActions->Logger()->WriteException($oException); + } + + if ($bLogout) + { + $this->oActions->SetAuthLogoutToken(); + } + + $this->oActions->Location('./'); + return ''; +*/ + } + + /** + * @return void + */ + public function FilterAppData($bAdmin, &$aResult) + { + if (!$bAdmin && \is_array($aResult) && static::IsOwnCloud()) { + $key = \array_search(\RainLoop\Enumerations\Capa::AUTOLOGOUT, $aResult['Capa']); + if (false !== $key) { + unset($aResult['Capa'][$key]); + } + if (static::IsOwnCloudLoggedIn() && \class_exists('OCP\Files')) { + $aResult['System']['attachmentsActions'][] = 'owncloud'; + } + } + } + + /** + * @param string $sName + * @param mixed $mResult + */ + public function MainFabrica($sName, &$mResult) + { + if ('suggestions' === $sName && static::IsOwnCloud() && $this->Config()->Get('plugin', 'suggestions', true)) { + include_once __DIR__.'/OwnCloudSuggestions.php'; + if (!\is_array($mResult)) { + $mResult = array(); + } + $mResult[] = new OwnCloudSuggestions(); + } + } + + 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) + ); + } +} diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php index 6952bbdc6..dfcd1bd5c 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php @@ -186,6 +186,16 @@ trait User } } break; + + default: + $data = new \SnappyMail\AttachmentsAction; + $data->action = $sAction; + $data->items = $aData; + $data->filesProvider = $oFilesProvider; + $data->account = $oAccount; + $this->Plugins()->RunHook('json.attachments', array($data)); + $mResult = $data->result; + break; } } else diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/attachmentsaction.php b/snappymail/v/0.0.0/app/libraries/snappymail/attachmentsaction.php new file mode 100644 index 000000000..5638950d2 --- /dev/null +++ b/snappymail/v/0.0.0/app/libraries/snappymail/attachmentsaction.php @@ -0,0 +1,13 @@ +