Added NextCloud

Taken from https://github.com/pierre-alain-b/snappymail-nextcloud
This commit is contained in:
djmaze 2021-07-17 08:38:21 +02:00
parent 2dde6c59eb
commit 65200af210
32 changed files with 1713 additions and 1 deletions

View file

@ -0,0 +1,87 @@
<?php
namespace OCA\SnappyMail\AppInfo;
use OCA\SnappyMail\Util\SnappyMailHelper;
use OCA\SnappyMail\Controller\AjaxController;
use OCA\SnappyMail\Controller\PageController;
use OCP\AppFramework\App;
use OCP\IL10N;
use OCP\IUser;
class Application extends App {
public function __construct(array $urlParams = []) {
parent::__construct('snappymail', $urlParams);
$container = $this->getContainer();
$server = $container->getServer();
$config = $server->getConfig();
/**
* Controllers
*/
$container->registerService(
'PageController', function($c) {
return new PageController(
$c->query('AppName'),
$c->query('Request'),
$c->getServer()->getAppManager(),
$c->query('ServerContainer')->getConfig(),
$c->getServer()->getSession()
);
}
);
$container->registerService(
'AjaxController', function($c) {
return new AjaxController(
$c->query('AppName'),
$c->query('Request'),
$c->getServer()->getAppManager(),
$c->query('ServerContainer')->getConfig(),
$c->query(IL10N::class)
);
}
);
/**
* Utils
*/
$container->registerService(
'SnappyMailHelper', function($c) {
return new SnappyMailHelper(
$c->getServer()->getConfig(),
$c->getServer()->getUserSession(),
$c->getServer()->getAppManager(),
$c->getServer()->getSession()
);
}
);
// Add script js/snappymail.js
\OCP\Util::addScript('snappymail', 'snappymail');
}
public function registerNavigation() {
$container = $this->getContainer();
$container->query('OCP\INavigationManager')->add(function () use ($container) {
$urlGenerator = $container->query('OCP\IURLGenerator');
return [
'id' => 'snappymail',
'order' => 10,
'href' => $urlGenerator->linkToRoute('snappymail.page.index'),
'icon' => $urlGenerator->imagePath('snappymail', 'snappymail.svg'),
'name' => \OCP\Util::getL10N('snappymail')->t('Email')
];
});
}
public function registerPersonalSettings() {
\OCP\App::registerPersonal('snappymail', 'templates/personal');
}
}

View file

@ -0,0 +1,98 @@
<?php
namespace OCA\SnappyMail\Controller;
use OCA\SnappyMail\Util\SnappyMailHelper;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
class AjaxController extends Controller {
private $config;
private $appManager;
public function __construct(string $appName, IRequest $request, IAppManager $appManager, IConfig $config, IL10N $l) {
parent::__construct($appName, $request);
$this->config = $config;
$this->appManager = $appManager;
$this->l = $l;
}
public function setAdmin(): JSONResponse {
try {
$sUrl = '';
$sPath = '';
$bAutologin = false;
if (isset($_POST['appname']) && 'snappymail' === $_POST['appname']) {
$this->config->setAppValue('snappymail', 'snappymail-autologin',
isset($_POST['snappymail-autologin']) ? '1' === $_POST['snappymail-autologin'] : false);
$this->config->setAppValue('snappymail', 'snappymail-autologin-with-email',
isset($_POST['snappymail-autologin']) ? '2' === $_POST['snappymail-autologin'] : false);
$bAutologin = $this->config->getAppValue('snappymail', 'snappymail-autologin', false);
} else {
return new JSONResponse([
'status' => 'error',
'Message' => $this->l->t('Invalid argument(s)')
]);
}
return new JSONResponse([
'status' => 'success',
'Message' => $this->l->t('Saved successfully')
]);
} catch (Exception $e) {
return new JSONResponse([
'status' => 'error',
'Message' => $e->getMessage()
]);
}
}
/**
* @NoAdminRequired
*/
public function setPersonal(): JSONResponse {
try {
if (isset($_POST['appname'], $_POST['snappymail-password'], $_POST['snappymail-email']) && 'snappymail' === $_POST['appname']) {
$sUser = \OC::$server->getUserSession()->getUser()->getUID();
$sPostEmail = $_POST['snappymail-email'];
$this->config->setUserValue($sUser, 'snappymail', 'snappymail-email', $sPostEmail);
$sPass = $_POST['snappymail-password'];
if ('******' !== $sPass && '' !== $sPass) {
include_once $this->appManager->getAppPath('snappymail').'/lib/Util/SnappyMailHelper.php';
$this->config->setUserValue($sUser, 'snappymail', 'snappymail-password',
SnappyMailHelper::encodePassword($sPass, md5($sPostEmail)));
}
$sEmail = $this->config->getUserValue($sUser, 'snappymail', 'snappymail-email', '');
} else {
return new JSONResponse([
'status' => 'error',
'Message' => $this->l->t('Invalid argument(s)'),
'Email' => $sEmail
]);
}
return new JSONResponse([
'status' => 'success',
'Message' => $this->l->t('Saved successfully'),
'Email' => $sEmail
]);
} catch (Exception $e) {
return new JSONResponse([
'status' => 'error',
'Message' => $e->getMessage()
]);
}
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace OCA\SnappyMail\Controller;
use OCA\SnappyMail\Util\SnappyMailHelper;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
class PageController extends Controller {
private $userId;
private $config;
private $appManager;
private $appPath;
private $session;
public function __construct($AppName, IRequest $request, IAppManager $appManager, IConfig $config, ISession $session) {
parent::__construct($AppName, $request);
$this->appPath = $appManager->getAppPath('snappymail');
$this->config = $config;
$this->appManager = $appManager;
$this->session = $session;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index() {
\OC::$server->getNavigationManager()->setActiveEntry('snappymail');
\OCP\Util::addStyle('snappymail', 'style');
$sUrl = SnappyMailHelper::normalizeUrl(SnappyMailHelper::getAppUrl());
$params = [
'snappymail-iframe-url' => SnappyMailHelper::normalizeUrl($sUrl).'?OwnCloudAuth'
];
$response = new TemplateResponse('snappymail', 'index', $params);
$csp = new ContentSecurityPolicy();
$csp->addAllowedFrameDomain("'self'");
$response->setContentSecurityPolicy($csp);
return $response;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function appGet() {
$this->app();
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function appPost() {
$this->app();
}
public function app() {
SnappyMailHelper::regSnappyMailDataFunction();
if (isset($_GET['OwnCloudAuth'])) {
$sEmail = '';
$sEncodedPassword = '';
$sUser = \OC::$server->getUserSession()->getUser()->getUID();
if ($this->config->getAppValue('snappymail', 'snappymail-autologin', false)) {
$sEmail = $sUser;
$sPasswordSalt = $sUser;
$sEncodedPassword = $this->session['snappymail-autologin-password'];
} else if ($this->config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sEmail = $this->config->getUserValue($sUser, 'settings', 'email', '');
$sPasswordSalt = $sUser;
$sEncodedPassword = $this->session['snappymail-autologin-password'];
}
// If the user has set credentials for SnappyMail in their personal
// settings, override everything before and use those instead.
$sIndividualEmail = $this->config->getUserValue($sUser, 'snappymail', 'snappymail-email', '');
if ($sIndividualEmail) {
$sEmail = $sIndividualEmail;
$sPasswordSalt = $sEmail;
$sEncodedPassword = $this->config->getUserValue($sUser, 'snappymail', 'snappymail-password', '');
}
$sDecodedPassword = SnappyMailHelper::decodePassword($sEncodedPassword, md5($sPasswordSalt));
$_ENV['___snappymail_owncloud_email'] = $sEmail;
$_ENV['___snappymail_owncloud_password'] = $sDecodedPassword;
}
include $this->appPath . '/app/index.php';
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace OCA\SnappyMail\Settings;
use OCA\SnappyMail\Util\SnappyMailHelper;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\Settings\ISettings;
class AdminSettings implements ISettings {
private $config;
public function __construct(IConfig $config) {
$this->config = $config;
}
public function getForm() {
$keys = [
'snappymail-autologin',
'snappymail-autologin-with-email'
];
$parameters = [];
foreach ($keys as $k) {
$v = $this->config->getAppValue('snappymail', $k);
$parameters[$k] = $v;
}
$uid = \OC::$server->getUserSession()->getUser()->getUID();
if (\OC_User::isAdminUser($uid)) {
$parameters['snappymail-admin-panel-link'] = SnappyMailHelper::getAppUrl().'?admin';
}
return new TemplateResponse('snappymail', 'admin-local', $parameters);
}
public function getSection() {
return 'additional';
}
public function getPriority() {
return 50;
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace OCA\SnappyMail\Settings;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
class PersonalSettings {
private $config;
public function __construct(IConfig $config) {
$this->config = $config;
}
public function getForm() {
$uid = \OC::$server->getUserSession()->getUser()->getUID();
$keys = [
'snappymail-email',
'snappymail-password'
];
$parameters = [];
foreach ($keys as $k) {
$v = $this->config->getUserValue($uid, 'snappymail', $k);
$parameters[$k] = $v;
}
return new TemplateResponse('snappymail', 'personal_settings', $parameters, '');
}
}

View file

@ -0,0 +1,341 @@
<?php
namespace OCA\SnappyMail\Util;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\ISession;
use OCP\IUserSession;
class SnappyMailHelper {
private $appManager;
private $config;
private $session;
private $userSession;
public function __construct(IConfig $config, IUserSession $userSession, IAppManager $appManager, ISession $session) {
$this->appManager = $appManager;
$this->config = $config;
$this->userSession = $userSession;
$this->session = $session;
}
public function registerHooks() {
$this->userSession->listen('\OC\User', 'postLogin', function($user, $loginName, $password, $isTokenLogin) {
$this->login($user, $loginName, $password, $isTokenLogin, $this->config, $this->session);
});
$this->userSession->listen('\OC\User', 'logout', function() {
$this->logout($this->appManager, $this->config, $this->session);
});
}
/**
* @return string
*/
public static function getAppUrl()
{
$sRequestUri = \OC::$server->getURLGenerator()->linkToRoute('snappymail.page.appGet');
if ($sRequestUri)
{
return $sRequestUri;
}
$sRequestUri = empty($_SERVER['REQUEST_URI']) ? '': trim($_SERVER['REQUEST_URI']);
$sRequestUri = preg_replace('/index.php\/.+$/', 'index.php/', $sRequestUri);
$sRequestUri = $sRequestUri.'apps/snappymail/app/';
return '/'.ltrim($sRequestUri, '/\\');
}
/**
* @param string $sPath
* @param string $sEmail
* @param string $sPassword
*
* @return string
*/
public static function getSsoHash($sPath, $sEmail, $sPassword)
{
$SsoHash = '';
$sPath = rtrim(trim($sPath), '\\/').'/index.php';
if (file_exists($sPath))
{
self::regSnappyMailDataFunction();
$_ENV['RAINLOOP_INCLUDE_AS_API'] = true;
include $sPath;
if (class_exists('\\SnappyMail\\Api'))
{
$SsoHash = \SnappyMail\Api::GetUserSsoHash($sEmail, $sPassword);
}
}
return $SsoHash;
}
/**
* @param string $sUrl
*
* @return string
*/
public static function normalizeUrl($sUrl)
{
$sUrl = rtrim(trim($sUrl), '/\\');
if ('.php' !== strtolower(substr($sUrl, -4)))
{
$sUrl .= '/';
}
return $sUrl;
}
/**
* @return boolean
*/
public static function mcryptSupported()
{
return function_exists('mcrypt_encrypt') &&
function_exists('mcrypt_decrypt') &&
defined('MCRYPT_RIJNDAEL_256') &&
defined('MCRYPT_MODE_ECB');
}
/**
* @return string
*/
public static function openSslSupportedMethod()
{
$method = 'AES-256-CBC';
return function_exists('openssl_encrypt') &&
function_exists('openssl_decrypt') &&
function_exists('openssl_random_pseudo_bytes') &&
function_exists('openssl_cipher_iv_length') &&
function_exists('openssl_get_cipher_methods') &&
defined('OPENSSL_RAW_DATA') && defined('OPENSSL_ZERO_PADDING') &&
in_array($method, openssl_get_cipher_methods()) ? $method : '';
}
/**
* @param string $sMethod
* @param string $sPassword
* @param string $sSalt
*
* @return string
*/
public static function encodePasswordSsl($sMethod, $sPassword, $sSalt)
{
$sData = base64_encode($sPassword);
$iv = @openssl_random_pseudo_bytes(openssl_cipher_iv_length($sMethod));
$r = @openssl_encrypt($sData, $sMethod, md5($sSalt), OPENSSL_RAW_DATA, $iv);
return @base64_encode(base64_encode($r).'|'.base64_encode($iv));
}
/**
* @param string $sMethod
* @param string $sPassword
* @param string $sSalt
*
* @return string
*/
public static function decodePasswordSsl($sMethod, $sPassword, $sSalt)
{
$sLine = base64_decode(trim($sPassword));
$aParts = explode('|', $sLine, 2);
if (is_array($aParts) && !empty($aParts[0]) && !empty($aParts[1])) {
$sData = @base64_decode($aParts[0]);
$iv = @base64_decode($aParts[1]);
return @base64_decode(trim(
@openssl_decrypt($sData, $sMethod, md5($sSalt), OPENSSL_RAW_DATA, $iv)
));
}
return '';
}
/**
* @param string $sPassword
* @param string $sSalt
*
* @return string
*/
public static function encodePassword($sPassword, $sSalt)
{
$method = self::openSslSupportedMethod();
if ($method)
{
return self::encodePasswordSsl($method, $sPassword, $sSalt);
}
else if (self::mcryptSupported())
{
return @trim(base64_encode(
@mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($sSalt), base64_encode($sPassword), MCRYPT_MODE_ECB)
));
}
return @trim(base64_encode($sPassword));
}
/**
* @param string $sPassword
* @param string $sSalt
*
* @return string
*/
public static function decodePassword($sPassword, $sSalt)
{
$method = self::openSslSupportedMethod();
if ($method)
{
return self::decodePasswordSsl($method, $sPassword, $sSalt);
}
else if (self::mcryptSupported())
{
return @base64_decode(trim(
@mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($sSalt), base64_decode(trim($sPassword)), MCRYPT_MODE_ECB)
));
}
return @base64_decode(trim($sPassword));
}
/**
* @param array $aParams
* @return boolean
* @UseSession
*/
public static function login($user, $loginName, $password, $isTokenLogin, $config, &$session)
{
$sUser = $user->getUID();
$sEmail = $sUser;
$sPassword = $password;
$sEncodedPassword = self::encodePassword($sPassword, md5($sEmail));
// 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') || $config->getAppValue('snappymail', 'snappymail-autologin-with-email')) {
$session['snappymail-autologin-password'] = $sEncodedPassword;
}
}
/**
* @UseSession
*/
public static function logout($appManager, $config, &$session)
{
$session['snappymail-autologin-password'] = '';
$sApiPath = $appManager->getAppPath('snappymail') . '/app/index.php';
self::regSnappyMailDataFunction();
$_ENV['RAINLOOP_INCLUDE_AS_API'] = true;
include $sApiPath;
\SnappyMail\Api::LogoutCurrentLogginedUser();
return true;
}
public static function regSnappyMailDataFunction()
{
if (!@function_exists('__get_custom_data_full_path'))
{
$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true;
function __get_custom_data_full_path()
{
$sData = rtrim(trim(OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/';
return @is_dir($sData) ? $sData.'snappymail-storage' : '';
}
}
}
public static function mimeContentType($filename) {
$mime_types = array(
'woff' => 'application/font-woff',
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
if (0 < strpos($filename, '.'))
{
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types))
{
return $mime_types[$ext];
}
else if (function_exists('finfo_open'))
{
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
}
return 'application/octet-stream';
}
}