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 980852c15SAndreas Gohr// must be run within Dokuwiki 1080852c15SAndreas Gohrif(!defined('DOKU_INC')) die(); 1180852c15SAndreas Gohr 1280852c15SAndreas Gohrclass helper_plugin_oauth extends DokuWiki_Plugin { 1380852c15SAndreas Gohr 1480852c15SAndreas Gohr /** 15f10e09e2SAndreas Gohr * Load the needed libraries and initialize the named oAuth service 1680852c15SAndreas Gohr * 17f10e09e2SAndreas Gohr * @param string $servicename 18f10e09e2SAndreas Gohr * @return null|\OAuth\Plugin\AbstractAuthService 1980852c15SAndreas Gohr */ 20a7a8f46aSAndreas Gohr public function loadService(&$servicename) { 21f10e09e2SAndreas Gohr global $ID; 22f10e09e2SAndreas Gohr 23f10e09e2SAndreas Gohr $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename); 24f10e09e2SAndreas Gohr if(!$servicename) return null; 25f10e09e2SAndreas Gohr 26f10e09e2SAndreas Gohr require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php'); 27f10e09e2SAndreas Gohr require_once(__DIR__.'/classes/AbstractAuthService.php'); 28f10e09e2SAndreas Gohr require_once(__DIR__.'/classes/oAuthHTTPClient.php'); 29f10e09e2SAndreas Gohr 30f10e09e2SAndreas Gohr $file = __DIR__.'/classes/'.$servicename.'AuthService.php'; 31f10e09e2SAndreas Gohr if(!file_exists($file)) return null; 32f10e09e2SAndreas Gohr require_once($file); 33f10e09e2SAndreas Gohr $class = '\\OAuth\\Plugin\\'.$servicename.'AuthService'; 34f10e09e2SAndreas Gohr 35f10e09e2SAndreas Gohr /** @var \OAuth\Plugin\AbstractAuthService $service */ 36f10e09e2SAndreas Gohr $service = new $class(wl($ID, array('oa' => $servicename), true, '&')); 37f10e09e2SAndreas Gohr if(!$service->isInitialized()) { 38f10e09e2SAndreas Gohr msg("Failed to initialize $service authentication service. Check credentials", -1); 39f10e09e2SAndreas Gohr return null; 40f10e09e2SAndreas Gohr } 41f10e09e2SAndreas Gohr 42f10e09e2SAndreas Gohr return $service; 43f10e09e2SAndreas Gohr } 44f10e09e2SAndreas Gohr 45*dfbdd519SAndreas Gohr /** 46*dfbdd519SAndreas Gohr * List available Services 47*dfbdd519SAndreas Gohr * 48*dfbdd519SAndreas Gohr * @return array 49*dfbdd519SAndreas Gohr */ 50*dfbdd519SAndreas Gohr public function listServices() { 51*dfbdd519SAndreas Gohr $services = array(); 52*dfbdd519SAndreas Gohr $files = glob(__DIR__.'/classes/*AuthService.php'); 53*dfbdd519SAndreas Gohr 54*dfbdd519SAndreas Gohr foreach($files as $file) { 55*dfbdd519SAndreas Gohr $file = basename($file, 'AuthService.php'); 56*dfbdd519SAndreas Gohr if($file == 'Abstract') continue; 57*dfbdd519SAndreas Gohr $services[] = $file; 58*dfbdd519SAndreas Gohr } 59*dfbdd519SAndreas Gohr 60*dfbdd519SAndreas Gohr return $services; 61*dfbdd519SAndreas Gohr } 62f10e09e2SAndreas Gohr 63f10e09e2SAndreas Gohr /** 64f10e09e2SAndreas Gohr * Return the configured key for the given service 65f10e09e2SAndreas Gohr * 66f10e09e2SAndreas Gohr * @param $service 67f10e09e2SAndreas Gohr * @return string 68f10e09e2SAndreas Gohr */ 69f10e09e2SAndreas Gohr public function getKey($service) { 70f10e09e2SAndreas Gohr $service = strtolower($service); 71f10e09e2SAndreas Gohr return $this->getConf($service.'-key'); 72f10e09e2SAndreas Gohr } 73f10e09e2SAndreas Gohr 74f10e09e2SAndreas Gohr /** 75f10e09e2SAndreas Gohr * Return the configured secret for the given service 76f10e09e2SAndreas Gohr * 77f10e09e2SAndreas Gohr * @param $service 78f10e09e2SAndreas Gohr * @return string 79f10e09e2SAndreas Gohr */ 80f10e09e2SAndreas Gohr public function getSecret($service) { 81f10e09e2SAndreas Gohr $service = strtolower($service); 82f10e09e2SAndreas Gohr return $this->getConf($service.'-secret'); 8380852c15SAndreas Gohr } 8480852c15SAndreas Gohr 8580852c15SAndreas Gohr} 8680852c15SAndreas Gohr 8780852c15SAndreas Gohr// vim:ts=4:sw=4:et: 88