-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathossn_com.php
185 lines (177 loc) · 6.41 KB
/
ossn_com.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Open Source Social Network
*
* @package Open Source Social Network
* @author Open Social Website Core Team <[email protected]>
* @copyright (C) OpenTeknik LLC
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/
define('__SMTP__', ossn_route()->com . 'SMTP/');
/**
* Initialize SMTP Component
*
* @return void
* @access private
*/
function ossn_com_smtp_init() {
ossn_add_hook('email', 'config', 'ossn_smtp', 1);
ossn_register_com_panel('SMTP', 'settings');
if(ossn_isAdminLoggedin()) {
ossn_register_action('admin/smtp/settings/save', __SMTP__ . 'actions/admin/settings/save.php');
ossn_register_action('smtp/tokengmail', __SMTP__ . 'actions/admin/settings/gmail.php');
ossn_register_page('smtp_oauth', 'smtp_oauth_token_page_handler');
}
}
function smtp_oauth_token_page_handler($pages) {
switch($pages[0]) {
case 'google':
require_once __SMTP__ . 'vendor/google/vendor/autoload.php';
$smtp = new OssnComponents();
$settings = $smtp->getSettings('SMTP');
$provider = new League\OAuth2\Client\Provider\Google(array(
'clientId' => $settings->clientId,
'clientSecret' => $settings->clientSecret,
'redirectUri' => ossn_site_url('smtp_oauth/google'),
'accessType' => 'offline',
));
$token = $provider->getAccessToken('authorization_code', array(
'code' => input('code'),
));
$vars = array(
'oauth_token_google' => $token->getRefreshToken(),
);
$com = new OssnComponents();
if($com->setSettings('SMTP', $vars)) {
ossn_trigger_message(ossn_print('smtp:oauth:token:received'));
} else {
ossn_trigger_message(ossn_print('smtp:oauth:token:notreceived'));
}
redirect('administrator/component/SMTP');
break;
}
}
/**
* Ossn SMTP
*
* Send notification emails using your smtp server
* Few hosting providers didn't have php mail() enabled,
* Users belong to this category can try this component.
*
* @return void|boolean
*/
function ossn_smtp($hook, $type, $mail, $return) {
require_once __SMTP__ . 'vendor/google/vendor/autoload.php';
require_once __SMTP__ . 'vendor/phpmailer/OAuth.php';
$smtp = new OssnComponents();
$settings = $smtp->getSettings('SMTP');
if(isset($settings->oauth_token_google) && !empty($settings->oauth_token_google) && $settings->oauth_type == "gmail") {
$settings->password = '';
}
if(!empty($settings->host) && !empty($settings->port) && !empty($settings->username)) {
$mail->IsSMTP();
$mail->SMTPAuth = true;
//$mail->SMTPDebug = false;
$mail->Host = $settings->host;
$mail->Port = $settings->port;
$mail->Username = $settings->username;
$mail->Password = $settings->password;
$mail->SMTPSecure = true;
if($settings->port == "465"){
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
}
if($settings->port == "587"){
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
if(isset($settings->oauth_token_google) && !empty($settings->oauth_token_google) && $settings->oauth_type == "gmail") {
unset($mail->SMTPSecure);
$mail->SMTPSecure = 'ssl';
$mail->AuthType = 'XOAUTH2';
$provider = new League\OAuth2\Client\Provider\Google(array(
'clientId' => $settings->clientId,
'clientSecret' => $settings->clientSecret,
));
$oauth = new OAuthTokenProvider(array(
'provider' => $provider,
'clientId' => $settings->clientId,
'clientSecret' => $settings->clientSecret,
'refreshToken' => $settings->oauth_token_google,
'userName' => $settings->username,
));
$mail->setOAuth($oauth);
}
//from ossn v5.6 allow to send email from default class
return $mail;
}
}
/**
* Check if is connected to server or not
*
* @return array
*/
function ossn_smtp_connected() {
require_once __SMTP__ . 'vendor/google/vendor/autoload.php';
require_once __SMTP__ . 'vendor/phpmailer/OAuth.php';
$return = array();
$mail = new OssnMail();
$smtp = new OssnComponents();
$settings = $smtp->getSettings('SMTP');
$return['status'] = ossn_print('smtp:connectio:failed');
if(isset($settings->oauth_token_google) && !empty($settings->oauth_token_google) && $settings->oauth_type == "gmail") {
$settings->password = '';
}
if(!empty($settings->host) && !empty($settings->port) && !empty($settings->username)) {
$mail->IsSMTP();
$mail->Timeout = 5; //timeout after 10 seconds
$mail->SMTPAuth = true;
//$mail->SMTPDebug = true;
$mail->Host = $settings->host;
$mail->Port = $settings->port;
$mail->Username = $settings->username;
$mail->Password = $settings->password;
$mail->SMTPSecure = true;
if($settings->port == "465"){
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
}
if($settings->port == "587"){
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
if(isset($settings->oauth_token_google) && !empty($settings->oauth_token_google) && $settings->oauth_type == "gmail") {
unset($mail->SMTPSecure);
$mail->SMTPSecure = 'ssl';
$mail->AuthType = 'XOAUTH2';
$provider = new League\OAuth2\Client\Provider\Google(array(
'clientId' => $settings->clientId,
'clientSecret' => $settings->clientSecret,
));
try {
$grant = new League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, array(
'refresh_token' => $settings->oauth_token_google,
));
try {
$ownerDetails = $provider->getResourceOwner($token);
$name = $ownerDetails->getFirstName();
if(!empty($name)) {
$return['status'] = ossn_print('smtp:connection:connected');
}
} catch (Exception $e) {
$return['status'] = ossn_print('smtp:connectio:failed');
}
} catch (Exception $e) {
$return['status'] = ossn_print('smtp:connectio:failed');
}
} else {
if($mail->smtpConnect()) {
$mail->smtpClose();
$return['status'] = ossn_print('smtp:connection:connected');
} else {
$return['status'] = ossn_print('smtp:connectio:failed');
}
}
}
return $return;
}
//initilize ossn wall
ossn_register_callback('ossn', 'init', 'ossn_com_smtp_init');