Skip to content

Commit

Permalink
Changes for base OwnCloud/NextCloud
Browse files Browse the repository at this point in the history
See #96
  • Loading branch information
djmaze committed Jul 16, 2021
1 parent 5213b80 commit 6e26c76
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 114 additions & 0 deletions plugins/owncloud/OwnCloudSuggestions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

class OwnCloudSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions
{
/**
* @var \MailSo\Log\Logger
*/
protected $oLogger;

/**
* @param \RainLoop\Model\Account $oAccount
* @param string $sQuery
* @param int $iLimit = 20
*
* @return array
*/
public function Process(\RainLoop\Model\Account $oAccount, string $sQuery, int $iLimit = 20): array
{
$iInputLimit = $iLimit;
$aResult = array();
$sQuery = \trim($sQuery);

try
{
if ('' === $sQuery || !$oAccount || !\RainLoop\Utils::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;
}
}
180 changes: 180 additions & 0 deletions plugins/owncloud/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

class OwnCloudPlugin extends \RainLoop\Plugins\AbstractPlugin
{
const
NAME = 'OwnCloud',
VERSION = '2.0',
CATEGORY = 'Security',
DESCRIPTION = 'Plugin that adds functionality to integrate with OwnCloud.';

private static function IsOwnCloud() : bool
{
return !empty($_ENV['SNAPPYMAIL_OWNCLOUD']) && \class_exists('OC');
}

private static function IsOwnCloudLoggedIn() : bool
{
return static::IsOwnCloud() && \class_exists('OCP\User') && \OCP\User::isLoggedIn();
}

public function Init() : void
{
if (static::IsOwnCloud()) {
$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::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)
);
}
}
10 changes: 10 additions & 0 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ public function DoAttachmentsActions() : array
}
}
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
Expand Down
13 changes: 13 additions & 0 deletions snappymail/v/0.0.0/app/libraries/snappymail/attachmentsaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SnappyMail;

class AttachmentsAction
{
public
$account = null, // \RainLoop\Model\Account
$action = '',
$items = [],
$filesProvider = null,
$result = false;
}

0 comments on commit 6e26c76

Please sign in to comment.