Completed
Push — antivirus-update ( b6d85d...fcda38 )
by Victor
06:44
created

Notification::sendMail()   B

Complexity

Conditions 6
Paths 65

Size

Total Lines 36
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 36
rs 8.439
cc 6
eloc 30
nc 65
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2014 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus;
10
11
class Notification {
12
	public static function sendMail($path){
13
		if (!\OCP\User::isLoggedIn()){
14
			return;
15
		}
16
		$config = \OC::$server->getConfig();
17
		$user = \OC::$server->getUserSession()->getUser();
18
		$email = $user->getEMailAddress();
19
		$displayName = $user->getDisplayName();
20
		if (\OCP\App::isEnabled('user_ldap')){
21
			$displayName = $config->getUserValue($user->getUID(), 'user_ldap', 'displayName', '');
22
		}
23
		if (empty($displayName)) {
24
			$displayName = $user->getUID();
25
		}
26
		\OCP\Util::writeLog('files_antivirus', 'Email: '.$email, \OCP\Util::DEBUG);
27
		if (!empty($email)) {
28
			try {
29
				$tmpl = new \OCP\Template('files_antivirus', 'notification');
30
				$tmpl->assign('file', $path);
31
				$tmpl->assign('host', \OC::$server->getRequest()->getServerHost());
32
				$tmpl->assign('user', $displayName);
33
				$msg = $tmpl->fetchPage();
34
				$from = \OCP\Util::getDefaultEmailAddress('security-noreply');
35
				$mailer = \OC::$server->getMailer();
36
				$message = $mailer->createMessage();
37
				$message->setSubject(\OCP\Util::getL10N('files_antivirus')->t('Malware detected'));
38
				$message->setFrom([$from => 'ownCloud Notifier']);
39
				$message->setTo([$email => $displayName]);
40
				$message->setPlainBody($msg);
41
				$message->setHtmlBody($msg);
42
				$mailer->send($message);
43
			} catch (\Exception $e){
44
				\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
45
			}
46
		}
47
	}
48
}
49