|
1 | 1 | <?php |
| 2 | + |
| 3 | +# https://console.cloud.google.com/apis/credentials |
| 4 | +# https://developers.google.com/identity/protocols/oauth2/ |
| 5 | + |
| 6 | +# https://developer.yahoo.com/oauth2/guide/ |
| 7 | + |
2 | 8 | /** |
3 | 9 | * PHPMailer - PHP email creation and transport class. |
4 | 10 | * PHP Version 5.5 |
|
8 | 14 | * @author Jim Jagielski (jimjag) <jimjag@gmail.com> |
9 | 15 | * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net> |
10 | 16 | * @author Brent R. Matzelle (original founder) |
11 | | - * @copyright 2012 - 2017 Marcus Bointon |
| 17 | + * @copyright 2012 - 2020 Marcus Bointon |
12 | 18 | * @copyright 2010 - 2012 Jim Jagielski |
13 | 19 | * @copyright 2004 - 2009 Andy Prevost |
14 | | - * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 20 | + * @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html GNU Lesser General Public License |
15 | 21 | * @note This program is distributed in the hope that it will be useful - WITHOUT |
16 | 22 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17 | 23 | * FITNESS FOR A PARTICULAR PURPOSE. |
18 | 24 | */ |
| 25 | + |
19 | 26 | /** |
20 | 27 | * Get an OAuth2 token from an OAuth2 provider. |
21 | 28 | * * Install this script on your server so that it's accessible |
|
28 | 35 | * revoke access to your app and run the script again. |
29 | 36 | */ |
30 | 37 |
|
31 | | -namespace PHPMailer\PHPMailer; |
| 38 | +// namespace PHPMailer\PHPMailer; |
32 | 39 |
|
33 | 40 | /** |
34 | 41 | * Aliases for League Provider Classes |
35 | 42 | * Make sure you have added these to your composer.json and run `composer install` |
36 | 43 | * Plenty to choose from here: |
37 | | - * @see http://oauth2-client.thephpleague.com/providers/thirdparty/ |
| 44 | + * @see https://oauth2-client.thephpleague.com/providers/thirdparty/ |
38 | 45 | */ |
39 | | -// @see https://github.com/thephpleague/oauth2-google |
| 46 | +//@see https://github.com/thephpleague/oauth2-google |
40 | 47 | use League\OAuth2\Client\Provider\Google; |
| 48 | +//@see https://packagist.org/packages/hayageek/oauth2-yahoo |
| 49 | +use Hayageek\OAuth2\Client\Provider\Yahoo; |
| 50 | +//@see https://github.com/stevenmaguire/oauth2-microsoft |
| 51 | +use Stevenmaguire\OAuth2\Client\Provider\Microsoft; |
| 52 | +//@see https://github.com/greew/oauth2-azure-provider |
| 53 | +use Greew\OAuth2\Client\Provider\Azure; |
41 | 54 |
|
| 55 | +<<<<<<< HEAD |
42 | 56 | // Include PluXml requirements |
43 | 57 | include 'prepend.php'; |
44 | 58 |
|
|
74 | 88 | //or whichever provider you're using. |
75 | 89 | $clientId = $plxAdmin->aConf['smtpOauth2_clientId']; |
76 | 90 | $clientSecret = $plxAdmin->aConf['smtpOauth2_clientSecret']; |
| 91 | +======= |
| 92 | +include 'prepend.php'; |
| 93 | +>>>>>>> phpmailer-250218 |
77 | 94 |
|
78 | 95 | //If this automatic URL doesn't work, set it yourself manually to the URL of this script |
79 | | -$redirectUri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 96 | +$redirectUri = (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
80 | 97 | //$redirectUri = 'http://localhost/PHPMailer/redirect'; |
81 | 98 |
|
| 99 | +if (!isset($_GET['code']) && !isset($_POST['provider'])) { |
| 100 | + include 'top.php'; |
| 101 | + |
| 102 | + if(isset($_FILES['json-data']) and $_FILES['json-data']['error'] == 0) { |
| 103 | + $filename = $_FILES['json-data']['tmp_name']; |
| 104 | + $dataStr = file_get_contents($filename); |
| 105 | + if(is_string($dataStr)) { |
| 106 | + $data = json_decode($dataStr, true); |
| 107 | + $app = array_values($data)[0]; |
| 108 | + $plxAdmin->aConf['smtpOauth2_provider'] = 'Google'; |
| 109 | + $plxAdmin->aConf['smtpOauth2_clientId'] = $app['client_id']; |
| 110 | + $plxAdmin->aConf['smtpOauth2_clientSecret'] = $app['client_secret']; |
| 111 | + $plxAdmin->editConfiguration($plxAdmin->aConf, array()); |
| 112 | + } |
| 113 | + unlink($filename); |
| 114 | + } |
| 115 | +?> |
| 116 | + <div class="inline-form action-bar"> |
| 117 | + <h2><?= L_CONFIG_ADVANCED_SMTPOAUTH_GETTOKEN ?></h2> |
| 118 | + <div class="grid text-center"> |
| 119 | + <div class="col med-4 text-left"> |
| 120 | + <a class="back" href="parametres_avances.php"><?= L_CONFIG_ADVANCED_DESC ?></a> |
| 121 | + </div> |
| 122 | + <div class="col sml-3 med-2"> |
| 123 | + <a class="button" href="https://console.cloud.google.com/apis/credentials" target="_blank">Google</a> |
| 124 | + </div> |
| 125 | + <div class="col sml-3 med-2"> |
| 126 | + <a class="button" href="https://developer.yahoo.com/oauth2/guide/" target="_blank">Yahoo</a> |
| 127 | + </div> |
| 128 | + <div class="col sml-3 med-2"> |
| 129 | + <a class="button" href="https://learn.microsoft.com/fr-fr/entra/identity-platform/v2-oauth2-auth-code-flow" target="_blank">Microsoft</a> |
| 130 | + </div> |
| 131 | + <div class="col sml-3 med-2"> |
| 132 | + |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + <div class="grid text-left"> |
| 136 | + <div class="col med-3">Redirect Uri :</div> |
| 137 | + <div class="col med-9"><?= $redirectUri ?></div> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + <form method="post" id="form_Oauth2_token"> |
| 141 | + <div class="grid"> |
| 142 | + <div class="col sml-5"> |
| 143 | + <label for="id_provider"><?= L_GET_OAUTH_TOKEN_PROVIDER ?></label> |
| 144 | + </div> |
| 145 | + <div class="col sml-7"> |
| 146 | + <select id="id_provider" name="provider" required> |
| 147 | + <option value="">...</option> |
| 148 | +<?php |
| 149 | + $default = isset($plxAdmin->aConf['smtpOauth2_provider']) ? ucfirst($plxAdmin->aConf['smtpOauth2_provider']) : ''; |
| 150 | + $aClassProviders = array( |
| 151 | + 'League\\OAuth2\\Client\\Provider\\Google', |
| 152 | + 'Hayageek\\OAuth2\\Client\\Provider\\Yahoo', |
| 153 | + 'Stevenmaguire\\OAuth2\\Client\\Provider\\Microsoft', |
| 154 | + 'Greew\\OAuth2\\Client\\Provider\\Azure', |
| 155 | + ); |
| 156 | + foreach($aClassProviders as $aClass) { |
| 157 | + if(!class_exists($aClass)) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + $k = preg_replace('#.*\\\(\w+)$#', '$1', $aClass); |
| 161 | + $selected = ($k == $default) ? ' selected' : ''; |
| 162 | +?> |
| 163 | + <option value="<?= $k ?>"<?= $selected ?>><?= $k ?></option> |
| 164 | +<?php |
| 165 | + } |
| 166 | +?> |
| 167 | + </select> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + <p><?= L_GET_OAUTH_TOKEN_DETAILS ?></p> |
| 171 | +<?php |
| 172 | + foreach(array('clientId'=>'CLIENTID', 'clientSecret' => 'SECRETKEY', 'tenantId'=> 'TENANTID') as $k=>$v) { |
| 173 | + $id = 'smtpOauth2_' . $k; |
| 174 | + $value = !empty($plxAdmin->aConf[$id]) ? $plxAdmin->aConf[$id] : ''; |
| 175 | + $caption = constant('L_CONFIG_ADVANCED_SMTPOAUTH_' . $v); |
| 176 | + $required = ($k != 'tenantId') ? ' required' : ''; |
| 177 | +?> |
| 178 | + <div class="grid" id="container_<?= $k ?>"> |
| 179 | + <div class="col med-5"> |
| 180 | + <label for="id_<?= $k ?>"><?= $caption ?></label> |
| 181 | + </div> |
| 182 | + <div class="col med-7"> |
| 183 | + <input id="id_<?= $k ?>" type="text" name="<?= $k ?>" value="<?= $value ?>"<?= $required ?>> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + |
| 187 | +<?php |
| 188 | + } |
| 189 | + |
| 190 | + if(isset($app['redirect_uris']) and is_array($app['redirect_uris'])) { |
| 191 | +?> |
| 192 | + <ul> |
| 193 | +<?php |
| 194 | + foreach($app['redirect_uris'] as $uri) { |
| 195 | +?> |
| 196 | + <li><em><?= $uri ?></em></li> |
| 197 | +<?php |
| 198 | + } |
| 199 | +?> |
| 200 | + </ul> |
| 201 | +<?php |
| 202 | + } |
| 203 | +?> |
| 204 | + <p><input type="submit"></p> |
| 205 | + </form> |
| 206 | + <form enctype="multipart/form-data" method="post" id="form_get_oauth_credentials"> |
| 207 | + <input type="hidden" name="MAX_FILE_SIZE" value="2000" /> |
| 208 | + <span><?= L_GET_OAUTH_TOKEN_CREDENTIALS ?></span> |
| 209 | + <input type="file" name="json-data" accept=".json, application/json" placeholder="Google"> |
| 210 | + <input type="submit"> |
| 211 | + </form> |
| 212 | + <script> |
| 213 | + (function () { |
| 214 | + 'use strict'; |
| 215 | + setMsg(); |
| 216 | + |
| 217 | + const providerSelect = document.getElementById('id_provider'); |
| 218 | + const credentialsForm = document.getElementById('form_get_oauth_credentials'); |
| 219 | + const tenantId = document.getElementById('container_tenantId'); |
| 220 | + |
| 221 | + function displayCredentials(ev) { |
| 222 | + if(providerSelect.value == 'Google') { |
| 223 | + credentialsForm.classList.add('active'); |
| 224 | + } else { |
| 225 | + credentialsForm.classList.remove('active'); |
| 226 | + } |
| 227 | + |
| 228 | + if(tenantId) { |
| 229 | + if(providerSelect.value == 'Azure') { |
| 230 | + tenantId.classList.add('active'); |
| 231 | + } else { |
| 232 | + tenantId.classList.remove('active'); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + if(providerSelect && credentialsForm) { |
| 238 | + providerSelect.addEventListener('change', displayCredentials); |
| 239 | + displayCredentials(); |
| 240 | + } |
| 241 | + })() |
| 242 | + </script> |
| 243 | + </main> |
| 244 | + </body> |
| 245 | +</html> |
| 246 | +<?php |
| 247 | + exit; |
| 248 | +} |
| 249 | + |
| 250 | +/* ---- traitement du formulaire ---- */ |
| 251 | + |
| 252 | +require '../vendor/autoload.php'; |
| 253 | + |
| 254 | +// session_start(); |
| 255 | + |
| 256 | +$providerName = ''; |
| 257 | +$clientId = ''; |
| 258 | +$clientSecret = ''; |
| 259 | +$tenantId = ''; |
| 260 | + |
| 261 | +if (array_key_exists('provider', $_POST)) { |
| 262 | + $providerName = $_POST['provider']; |
| 263 | + $clientId = $_POST['clientId']; |
| 264 | + $clientSecret = $_POST['clientSecret']; |
| 265 | + $tenantId = $_POST['tenantId']; |
| 266 | + $_SESSION['provider'] = $providerName; |
| 267 | + $_SESSION['clientId'] = $clientId; |
| 268 | + $_SESSION['clientSecret'] = $clientSecret; |
| 269 | + $_SESSION['tenantId'] = $tenantId; |
| 270 | + |
| 271 | + # On sauvegarde les valeurs dans la configuration de PluXml |
| 272 | + foreach(array('provider', 'clientId', 'clientSecret', 'tenantId',) as $k) { |
| 273 | + $content['smtpOauth2_' . $k] = $_SESSION[$k]; |
| 274 | + } |
| 275 | + $plxAdmin->editConfiguration($plxAdmin->aConf, $content); |
| 276 | +} elseif (array_key_exists('provider', $_SESSION)) { |
| 277 | + $providerName = $_SESSION['provider']; |
| 278 | + $clientId = $_SESSION['clientId']; |
| 279 | + $clientSecret = $_SESSION['clientSecret']; |
| 280 | + $tenantId = $_SESSION['tenantId']; |
| 281 | +} |
| 282 | + |
| 283 | +//If you don't want to use the built-in form, set your client id and secret here |
| 284 | +//$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com'; |
| 285 | +//$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP'; |
| 286 | + |
82 | 287 | $params = [ |
83 | 288 | 'clientId' => $clientId, |
84 | 289 | 'clientSecret' => $clientSecret, |
|
89 | 294 | $options = []; |
90 | 295 | $provider = null; |
91 | 296 |
|
92 | | -$provider = new Google($params); |
93 | | -$options = ['scope' => ['https://mail.google.com/']]; |
| 297 | +switch ($providerName) { |
| 298 | + case 'Google': |
| 299 | + $provider = new Google($params); |
| 300 | + $options = [ |
| 301 | + 'scope' => [ |
| 302 | + 'https://mail.google.com/' |
| 303 | + ] |
| 304 | + ]; |
| 305 | + break; |
| 306 | + case 'Yahoo': |
| 307 | + $provider = new Yahoo($params); |
| 308 | + break; |
| 309 | + case 'Microsoft': |
| 310 | + $provider = new Microsoft($params); |
| 311 | + $options = [ |
| 312 | + 'scope' => [ |
| 313 | + 'wl.imap', |
| 314 | + 'wl.offline_access' |
| 315 | + ] |
| 316 | + ]; |
| 317 | + break; |
| 318 | + case 'Azure': |
| 319 | + $params['tenantId'] = $tenantId; |
| 320 | + |
| 321 | + $provider = new Azure($params); |
| 322 | + $options = [ |
| 323 | + 'scope' => [ |
| 324 | + 'https://outlook.office.com/SMTP.Send', |
| 325 | + 'offline_access' |
| 326 | + ] |
| 327 | + ]; |
| 328 | + break; |
| 329 | +} |
94 | 330 |
|
95 | 331 | if (null === $provider) { |
96 | 332 | exit('Provider missing'); |
|
102 | 338 | $_SESSION['oauth2state'] = $provider->getState(); |
103 | 339 | header('Location: ' . $authUrl); |
104 | 340 | exit; |
105 | | -// Check given state against previously stored one to mitigate CSRF attack |
| 341 | + //Check given state against previously stored one to mitigate CSRF attack |
106 | 342 | } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { |
107 | 343 | unset($_SESSION['oauth2state']); |
108 | 344 | unset($_SESSION['provider']); |
|
118 | 354 | ); |
119 | 355 | // Use this to interact with an API on the users behalf |
120 | 356 | // Use this to get a new access token if the old one expires |
121 | | - $tokenToStore = array(); |
122 | | - $tokenToStore['smtpOauth2_refreshToken'] = $token->getRefreshToken(); |
123 | | - // Store the token in the PluXMl configuration and redirect to the administration page |
124 | | - if (!empty($tokenToStore)) { |
125 | | - $plxAdmin->editConfiguration($plxAdmin->aConf, $tokenToStore); |
| 357 | + // echo 'Refresh Token: ', htmlspecialchars($token->getRefreshToken()); |
| 358 | + $resp = htmlspecialchars($token->getRefreshToken()); |
| 359 | + |
| 360 | + if(!empty($resp)) { |
| 361 | + $content = array( |
| 362 | + 'smtpOauth2_refreshToken' => $resp, |
| 363 | + ); |
| 364 | + $plxAdmin->editConfiguration($plxAdmin->aConf, $content); |
| 365 | + header('Location: parametres_avances.php'); |
126 | 366 | } |
127 | | - header('Location: '.htmlentities($plxAdmin->aConf['racine'].'core/admin/parametres_avances.php')); |
128 | 367 | } |
0 commit comments