Completed
Push — master ( 8dbcbc...f734af )
by Nazar
07:14
created

mail::admin_mail_settings_common()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 3
eloc 27
nc 2
nop 0
1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @copyright  Copyright (c) 2015, Nazar Mokrynskyi
8
 * @license    MIT License, see license.txt
9
 */
10
namespace cs\modules\System\api\Controller\admin;
11
use
12
	cs\Config,
13
	cs\ExitException,
14
	cs\Mail as System_mail,
15
	cs\Page;
16
trait mail {
17
	/**
18
	 * Get mail settings
19
	 */
20
	static function admin_mail_get_settings () {
21
		$Config = Config::instance();
22
		Page::instance()->json(
23
			[
24
				'smtp'              => $Config->core['smtp'],
25
				'smtp_host'         => $Config->core['smtp_host'],
26
				'smtp_port'         => $Config->core['smtp_port'],
27
				'smtp_secure'       => $Config->core['smtp_secure'],
28
				'smtp_auth'         => $Config->core['smtp_auth'],
29
				'smtp_user'         => $Config->core['smtp_user'],
30
				'smtp_password'     => $Config->core['smtp_password'],
31
				'mail_from'         => $Config->core['mail_from'],
32
				'mail_from_name'    => get_core_ml_text('mail_from_name'),
33
				'mail_signature'    => get_core_ml_text('mail_signature'),
34
				'simple_admin_mode' => $Config->core['simple_admin_mode'],
35
				'applied'           => $Config->cancel_available()
36
			]
37
		);
38
	}
39
	/**
40
	 * Send test email to check if setup is correct
41
	 *
42
	 * @throws ExitException
43
	 */
44
	static function admin_mail_send_test_email () {
45
		if (!isset($_POST['email'])) {
46
			throw new ExitException(400);
47
		}
48
		if (!System_mail::instance()->send_to($_GET['email'], 'Email testing on '.get_core_ml_text('name'), 'Test email')) {
49
			throw new ExitException(500);
50
		}
51
	}
52
	/**
53
	 * Apply mail settings
54
	 *
55
	 * @throws ExitException
56
	 */
57
	static function admin_mail_apply_settings () {
58
		static::admin_mail_settings_common();
59
		if (!Config::instance()->apply()) {
60
			throw new ExitException(500);
61
		}
62
	}
63
	/**
64
	 * @throws ExitException
65
	 */
66
	protected static function admin_mail_settings_common () {
67
		if (
68
			!isset(
69
				$_POST['smtp'],
70
				$_POST['smtp_host'],
71
				$_POST['smtp_port'],
72
				$_POST['smtp_secure'],
73
				$_POST['smtp_auth'],
74
				$_POST['smtp_user'],
75
				$_POST['smtp_password'],
76
				$_POST['mail_from'],
77
				$_POST['mail_from_name'],
78
				$_POST['mail_signature']
79
			) ||
80
			!in_array($_POST['smtp_secure'], ['', 'ssl', 'tls'], true)
81
		) {
82
			throw new ExitException(400);
83
		}
84
		$Config                         = Config::instance();
85
		$Config->core['smtp']           = (int)(bool)$_POST['smtp'];
86
		$Config->core['smtp_host']      = $_POST['smtp_host'];
87
		$Config->core['smtp_port']      = (int)$_POST['smtp_port'];
88
		$Config->core['smtp_secure']    = $_POST['smtp_secure'];
89
		$Config->core['smtp_auth']      = (int)(bool)$_POST['smtp_auth'];
90
		$Config->core['smtp_user']      = $_POST['smtp_user'];
91
		$Config->core['smtp_password']  = $_POST['smtp_password'];
92
		$Config->core['mail_from']      = $_POST['mail_from'];
93
		$Config->core['mail_from_name'] = set_core_ml_text('mail_from_name', xap($_POST['mail_from_name']));
94
		$Config->core['mail_signature'] = set_core_ml_text('mail_signature', xap($_POST['mail_signature'], true));
95
	}
96
	/**
97
	 * Save mail settings
98
	 *
99
	 * @throws ExitException
100
	 */
101
	static function admin_mail_save_settings () {
102
		static::admin_mail_settings_common();
103
		if (!Config::instance()->save()) {
104
			throw new ExitException(500);
105
		}
106
	}
107
	/**
108
	 * Cancel mail settings
109
	 *
110
	 * @throws ExitException
111
	 */
112
	static function admin_mail_cancel_settings () {
113
		if (!Config::instance()->cancel()) {
114
			throw new ExitException(500);
115
		}
116
	}
117
}
118