Move the NextCloud plugin to final destination

This commit is contained in:
djmaze 2021-07-17 08:53:50 +02:00
parent 65200af210
commit b0d7bab1b0
2 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,107 @@
<?php
class OwnCloudSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions
{
/**
* @var \MailSo\Log\Logger
*/
protected $oLogger;
public function Process(\RainLoop\Model\Account $oAccount, string $sQuery, int $iLimit = 20): array
{
$iInputLimit = $iLimit;
$aResult = array();
$sQuery = \trim($sQuery);
try
{
if ('' === $sQuery || !$oAccount || !OwnCloudPlugin::IsOwnCloudLoggedIn())
{
return $aResult;
}
$aParams = array('FN', 'NICKNAME', 'TITLE', 'EMAIL');
if (\class_exists('OC') && isset(\OC::$server) && \method_exists(\OC::$server, 'getContactsManager'))
{
$cm = \OC::$server->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;
}
}

View file

@ -0,0 +1,180 @@
<?php
class NextCloudPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'NextCloud',
VERSION = '2.0',
CATEGORY = 'Integrations',
DESCRIPTION = 'Plugin that adds functionality to integrate with NextCloud.';
private static function IsNextCloud() : bool
{
return !empty($_ENV['SNAPPYMAIL_NEXTCLOUD']) && \class_exists('OC');
}
private static function IsNextCloudLoggedIn() : bool
{
return static::IsNextCloud() && \class_exists('OCP\User') && \OCP\User::isLoggedIn();
}
public function Init() : void
{
if (static::IsNextCloud()) {
$this->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::IsNextCloud()) {
return 'NextCloud not found to use this plugin';
}
return '';
}
// DoAttachmentsActions
public function DoAttachmentsActions(\SnappyMail\AttachmentsAction $data)
{
if ('nextcloud' === $data->action) {
if (static::IsNextCloudLoggedIn() && \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 ServiceNextCloudAuth()
{
/*
$this->oHttp->ServerNoCache();
if (!static::IsNextCloud() ||
!isset($_ENV['___snappymail_nextcloud_email']) ||
!isset($_ENV['___snappymail_nextcloud_password']) ||
empty($_ENV['___snappymail_nextcloud_email'])
)
{
$this->oActions->SetAuthLogoutToken();
$this->oActions->Location('./');
return '';
}
$bLogout = true;
$sEmail = $_ENV['___snappymail_nextcloud_email'];
$sPassword = $_ENV['___snappymail_nextcloud_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::IsNextCloud()) {
$key = \array_search(\RainLoop\Enumerations\Capa::AUTOLOGOUT, $aResult['Capa']);
if (false !== $key) {
unset($aResult['Capa'][$key]);
}
if (static::IsNextCloudLoggedIn() && \class_exists('OCP\Files')) {
$aResult['System']['attachmentsActions'][] = 'nextcloud';
}
}
}
/**
* @param string $sName
* @param mixed $mResult
*/
public function MainFabrica($sName, &$mResult)
{
if ('suggestions' === $sName && static::IsNextCloud() && $this->Config()->Get('plugin', 'suggestions', true)) {
include_once __DIR__.'/NextCloudSuggestions.php';
if (!\is_array($mResult)) {
$mResult = array();
}
$mResult[] = new NextCloudSuggestions();
}
}
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)
);
}
}