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\Adapter; 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|Adapter 25 */ 26 public function loadService($servicename) 27 { 28 $services = $this->listServices(true); 29 if (!isset($services[$servicename])) return null; 30 return $services[$servicename]; 31 } 32 33 /** 34 * The redirect URI used in all oAuth requests 35 * 36 * @return string 37 */ 38 public function redirectURI() 39 { 40 if ($this->getConf('custom-redirectURI') !== '') { 41 return $this->getConf('custom-redirectURI'); 42 } else { 43 return DOKU_URL . DOKU_SCRIPT; 44 } 45 } 46 47 /** 48 * List available Services 49 * 50 * Services returned here, do not have initialized oAuth providers yet! 51 * 52 * @param bool $enabledonly list only services that have been configured 53 * @triggers PLUGIN_OAUTH_BACKEND_REGISTER 54 * @return Adapter[] list of service objects 55 */ 56 public function listServices($enabledonly = true) 57 { 58 $services = []; 59 $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services); 60 $event->advise_before(false); 61 $event->advise_after(); 62 63 // filter out unconfigured services 64 if ($enabledonly) { 65 $services = array_filter($services, function ($service) { 66 /** @var Adapter $service */ 67 return (bool)$service->getKey(); 68 }); 69 } 70 71 return $services; 72 } 73 74 /** 75 * @return array 76 */ 77 public function getValidDomains() 78 { 79 if ($this->getConf('mailRestriction') === '') { 80 return array(); 81 } 82 $validDomains = explode(',', trim($this->getConf('mailRestriction'), ',')); 83 $validDomains = array_map('trim', $validDomains); 84 return $validDomains; 85 } 86 87 /** 88 * @param string $mail 89 * 90 * @return bool 91 */ 92 public function checkMail($mail) 93 { 94 $validDomains = $this->getValidDomains(); 95 if (empty($validDomains)) return true; 96 97 foreach ($validDomains as $validDomain) { 98 if (substr($mail, -strlen($validDomain)) === $validDomain) { 99 return true; 100 } 101 } 102 return false; 103 } 104 105 /** 106 * Display an exception to the user 107 * 108 * @param Exception $e 109 * @param string $friendly - user friendly explanation if available 110 */ 111 public function showException(\Exception $e, $friendly = '') 112 { 113 global $conf; 114 115 $msg = $e->getMessage(); 116 117 // translate the message if possible, using context if available 118 $trans = $this->getLang($msg); 119 if ($trans) { 120 if (is_a($e, \dokuwiki\plugin\oauth\Exception::class)) { 121 $context = $e->getContext(); 122 $trans = sprintf($trans, ...$context); 123 } 124 $msg = $trans; 125 } 126 127 msg('OAuth: ' . $friendly . ' ' . hsc($msg), -1); 128 if ($conf['allowdebug']) { 129 $msg = get_class($e) . ' at ' . $e->getFile() . ':' . $e->getLine() . '<br>'; 130 $msg .= hsc($e->getTraceAsString()); 131 msg("<pre>$msg</pre>", -1); 132 } 133 } 134} 135