180852c15SAndreas Gohr<?php 280852c15SAndreas Gohr/** 380852c15SAndreas Gohr * DokuWiki Plugin oauth (Helper Component) 480852c15SAndreas Gohr * 580852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 680852c15SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 780852c15SAndreas Gohr */ 880852c15SAndreas Gohr 998a36116SAndreas Gohruse dokuwiki\Extension\Event; 1004a78b87SAndreas Gohruse dokuwiki\plugin\oauth\Adapter; 1180852c15SAndreas Gohr 1298a36116SAndreas Gohrrequire_once(__DIR__ . '/vendor/autoload.php'); 1398a36116SAndreas Gohr 1498a36116SAndreas Gohr/** 1598a36116SAndreas Gohr * Basic helper methods for the oauth flow 1698a36116SAndreas Gohr */ 1798a36116SAndreas Gohrclass helper_plugin_oauth extends DokuWiki_Plugin 1898a36116SAndreas Gohr{ 1980852c15SAndreas Gohr 2080852c15SAndreas Gohr /** 21f10e09e2SAndreas Gohr * Load the needed libraries and initialize the named oAuth service 2280852c15SAndreas Gohr * 23f10e09e2SAndreas Gohr * @param string $servicename 2404a78b87SAndreas Gohr * @return null|Adapter 2580852c15SAndreas Gohr */ 2698a36116SAndreas Gohr public function loadService($servicename) 2798a36116SAndreas Gohr { 2898a36116SAndreas Gohr $services = $this->listServices(true); 2998a36116SAndreas Gohr if (!isset($services[$servicename])) return null; 306d9a8a49SAndreas Gohr return $services[$servicename]; 31f10e09e2SAndreas Gohr } 32f10e09e2SAndreas Gohr 33a90c044eSAndreas Gohr /** 34a90c044eSAndreas Gohr * The redirect URI used in all oAuth requests 35a90c044eSAndreas Gohr * 36a90c044eSAndreas Gohr * @return string 37a90c044eSAndreas Gohr */ 3898a36116SAndreas Gohr public function redirectURI() 3998a36116SAndreas Gohr { 409683193cSMichael Große if ($this->getConf('custom-redirectURI') !== '') { 419683193cSMichael Große return $this->getConf('custom-redirectURI'); 429683193cSMichael Große } else { 432e94f0b8SAndreas Gohr return DOKU_URL . DOKU_SCRIPT; 442e94f0b8SAndreas Gohr } 459683193cSMichael Große } 462e94f0b8SAndreas Gohr 47dfbdd519SAndreas Gohr /** 48dfbdd519SAndreas Gohr * List available Services 49dfbdd519SAndreas Gohr * 506d9a8a49SAndreas Gohr * Services returned here, do not have initialized oAuth providers yet! 516d9a8a49SAndreas Gohr * 5298a36116SAndreas Gohr * @param bool $enabledonly list only services that have been configured 5398a36116SAndreas Gohr * @triggers PLUGIN_OAUTH_BACKEND_REGISTER 5404a78b87SAndreas Gohr * @return Adapter[] list of service objects 55dfbdd519SAndreas Gohr */ 5698a36116SAndreas Gohr public function listServices($enabledonly = true) 5798a36116SAndreas Gohr { 5898a36116SAndreas Gohr $services = []; 5998a36116SAndreas Gohr $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services); 6098a36116SAndreas Gohr $event->advise_before(false); 6198a36116SAndreas Gohr $event->advise_after(); 62dfbdd519SAndreas Gohr 6398a36116SAndreas Gohr // filter out unconfigured services 6498a36116SAndreas Gohr if ($enabledonly) { 6598a36116SAndreas Gohr $services = array_filter($services, function ($service) { 6604a78b87SAndreas Gohr /** @var Adapter $service */ 6798a36116SAndreas Gohr return (bool)$service->getKey(); 6898a36116SAndreas Gohr }); 69dfbdd519SAndreas Gohr } 70dfbdd519SAndreas Gohr 71dfbdd519SAndreas Gohr return $services; 72dfbdd519SAndreas Gohr } 73f10e09e2SAndreas Gohr 74f10e09e2SAndreas Gohr /** 75ebf681d1SMichael Große * @return array 76d9818adbSMichael Große */ 7798a36116SAndreas Gohr public function getValidDomains() 7898a36116SAndreas Gohr { 793c0c1b14SMichael Große if ($this->getConf('mailRestriction') === '') { 803c0c1b14SMichael Große return array(); 813c0c1b14SMichael Große } 82d9818adbSMichael Große $validDomains = explode(',', trim($this->getConf('mailRestriction'), ',')); 83ebf681d1SMichael Große $validDomains = array_map('trim', $validDomains); 84d9818adbSMichael Große return $validDomains; 85d9818adbSMichael Große } 86d9818adbSMichael Große 87d9818adbSMichael Große /** 88d9818adbSMichael Große * @param string $mail 89d9818adbSMichael Große * 90d9818adbSMichael Große * @return bool 91d9818adbSMichael Große */ 9298a36116SAndreas Gohr public function checkMail($mail) 9398a36116SAndreas Gohr { 94*39730c7eSAnna Dabrowska $validDomains = $this->getValidDomains(); 95*39730c7eSAnna Dabrowska if (empty($validDomains)) return true; 96d9818adbSMichael Große 97*39730c7eSAnna Dabrowska foreach ($validDomains as $validDomain) { 98d9818adbSMichael Große if (substr($mail, -strlen($validDomain)) === $validDomain) { 99d9818adbSMichael Große return true; 100d9818adbSMichael Große } 101d9818adbSMichael Große } 102d9818adbSMichael Große return false; 103d9818adbSMichael Große } 104f2e164b0SMichael Große 105f2e164b0SMichael Große /** 1063e7ac5b1SAndreas Gohr * Display an exception to the user 1073e7ac5b1SAndreas Gohr * 1083e7ac5b1SAndreas Gohr * @param Exception $e 1093e7ac5b1SAndreas Gohr * @param string $prefix - user friendly explanation if available 1103e7ac5b1SAndreas Gohr */ 1113e7ac5b1SAndreas Gohr public function showException(\Exception $e, $prefix = '') 1123e7ac5b1SAndreas Gohr { 1133e7ac5b1SAndreas Gohr global $conf; 114c82ad624SAndreas Gohr 115c82ad624SAndreas Gohr msg('OAuth: ' . $prefix . ' ' . get_class($e) . ' ' . hsc($e->getMessage()), -1); 1163e7ac5b1SAndreas Gohr if ($conf['allowdebug']) { 1173e7ac5b1SAndreas Gohr msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 1183e7ac5b1SAndreas Gohr } 1193e7ac5b1SAndreas Gohr } 12080852c15SAndreas Gohr} 121