| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | /**
 | 
        
           |  |  | 18 |  * Moodle Customised version of the PHPMailer OAuth class
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    core
 | 
        
           |  |  | 21 |  * @copyright  2022 Huong Nguyen <huongnv13@gmail.com>
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 | class moodle_phpmailer_oauth extends \PHPMailer\PHPMailer\OAuth {
 | 
        
           |  |  | 25 |   | 
        
           | 1441 | ariadna | 26 |     #[\Override]
 | 
        
           | 1 | efrain | 27 |     protected function getToken() {
 | 
        
           |  |  | 28 |         return $this->provider->get_accesstoken()->token;
 | 
        
           |  |  | 29 |     }
 | 
        
           |  |  | 30 |   | 
        
           | 1441 | ariadna | 31 |     #[\Override]
 | 
        
           |  |  | 32 |     public function getOauth64() {
 | 
        
           |  |  | 33 |         // Get a new token if it's not available.
 | 
        
           |  |  | 34 |         if ($this->oauthToken === null) {
 | 
        
           |  |  | 35 |             $this->oauthToken = $this->getToken();
 | 
        
           |  |  | 36 |         }
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |         $accesstoken = $this->provider->get_accesstoken();
 | 
        
           |  |  | 39 |         // Renew the token if it's expired.
 | 
        
           |  |  | 40 |         if (isset($accesstoken->expires) && time() >= $accesstoken->expires) {
 | 
        
           |  |  | 41 |             if (isset($accesstoken->refreshtoken)) {
 | 
        
           |  |  | 42 |                 $this->provider->upgrade_token($accesstoken->refreshtoken, 'refresh_token');
 | 
        
           |  |  | 43 |             }
 | 
        
           |  |  | 44 |             $this->oauthToken = $this->getToken();
 | 
        
           |  |  | 45 |         }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |         return base64_encode(
 | 
        
           |  |  | 48 |             'user=' .
 | 
        
           |  |  | 49 |             $this->oauthUserEmail .
 | 
        
           |  |  | 50 |             "\001auth=Bearer " .
 | 
        
           |  |  | 51 |             $this->oauthToken .
 | 
        
           |  |  | 52 |             "\001\001"
 | 
        
           |  |  | 53 |         );
 | 
        
           |  |  | 54 |     }
 | 
        
           | 1 | efrain | 55 | }
 |