1<?php 2/** 3 * DokuWiki Plugin oauth (Helper Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9use dokuwiki\Extension\Event; 10use dokuwiki\plugin\oauth\Service; 11 12require_once(__DIR__ . '/vendor/autoload.php'); 13 14/** 15 * Basic helper methods for the oauth flow 16 */ 17class helper_plugin_oauth extends DokuWiki_Plugin 18{ 19 20 /** 21 * Load the needed libraries and initialize the named oAuth service 22 * 23 * @param string $servicename 24 * @return null|Service 25 */ 26 public function loadService($servicename) 27 { 28 $services = $this->listServices(true); 29 if (!isset($services[$servicename])) return null; 30 $service = $services[$servicename]; 31 return $service; 32 } 33 34 /** 35 * The redirect URI used in all oAuth requests 36 * 37 * @return string 38 */ 39 public function redirectURI() 40 { 41 if ($this->getConf('custom-redirectURI') !== '') { 42 return $this->getConf('custom-redirectURI'); 43 } else { 44 return DOKU_URL . DOKU_SCRIPT; 45 } 46 } 47 48 /** 49 * List available Services 50 * 51 * @param bool $enabledonly list only services that have been configured 52 * @triggers PLUGIN_OAUTH_BACKEND_REGISTER 53 * @return Service[] list of service objects 54 */ 55 public function listServices($enabledonly = true) 56 { 57 $services = []; 58 $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services); 59 $event->advise_before(false); 60 $event->advise_after(); 61 62 // filter out unconfigured services 63 if ($enabledonly) { 64 $services = array_filter($services, function ($service) { 65 /** @var Service $service */ 66 return (bool)$service->getKey(); 67 }); 68 } 69 70 return $services; 71 } 72 73 /** 74 * @return array 75 */ 76 public function getValidDomains() 77 { 78 if ($this->getConf('mailRestriction') === '') { 79 return array(); 80 } 81 $validDomains = explode(',', trim($this->getConf('mailRestriction'), ',')); 82 $validDomains = array_map('trim', $validDomains); 83 return $validDomains; 84 } 85 86 /** 87 * @param string $mail 88 * 89 * @return bool 90 */ 91 public function checkMail($mail) 92 { 93 $hostedDomains = $this->getValidDomains(); 94 95 foreach ($hostedDomains as $validDomain) { 96 if (substr($mail, -strlen($validDomain)) === $validDomain) { 97 return true; 98 } 99 } 100 return false; 101 } 102 103 /** 104 * @return bool 105 */ 106 public function isGETRequest() 107 { 108 global $INPUT; 109 return $INPUT->server->str('REQUEST_METHOD') === 'GET'; 110 } 111 112 /** 113 * check if we are handling a request to doku.php. Only doku.php defines $updateVersion 114 * 115 * @return bool 116 */ 117 public function isDokuPHP() 118 { 119 global $updateVersion; 120 return isset($updateVersion); 121 } 122 123 /** 124 * Display an exception to the user 125 * 126 * @param Exception $e 127 * @param string $prefix - user friendly explanation if available 128 */ 129 public function showException(\Exception $e, $prefix = '') 130 { 131 global $conf; 132 msg('OAuth: ' . $prefix . ' ' . hsc($e->getMessage()), -1); 133 if ($conf['allowdebug']) { 134 msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1); 135 } 136 } 137} 138