1<?php 2/** 3 * DokuWiki Plugin joomla3 (Auth Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Jury Verrigni <jury.verrigni@skayahack.com> 7 */ 8 9if (!defined('DOKU_INC')) { 10 die(); 11} 12 13/** 14 * Class auth_plugin_authjoomla3 15 */ 16class auth_plugin_authjoomla3 extends auth_plugin_authpdo 17{ 18 protected $joomlaPath = ''; 19 protected $joomlaConfig = array(); 20 21 /** 22 * Before calling AuthPDO's construct we want to override database's 23 * settings with Joomla's ones 24 **/ 25 public function __construct() 26 { 27 $this->joomlaPath = $this->getConf('joomlaPath'); 28 29 if ($this->joomlaPath == '' 30 || !is_dir($this->joomlaPath) 31 || !file_exists($this->joinPaths($this->joomlaPath, 'configuration.php'))) { 32 $this->_debug('Joomla not found at the specified path.', -1, __LINE__); 33 $this->success = false; 34 return; 35 } 36 37 $this->setupPdoConfig(); 38 39 parent::__construct(); 40 } 41 42 /** 43 * After fetching user's groups we want to rename Joomla's default 44 * Administrator and Super Users to admin in order to make them automatically 45 * admin on DokuWiki 46 **/ 47 protected function _selectUserGroups($userdata) 48 { 49 $groups = parent::_selectUserGroups($userdata); 50 foreach ($groups as &$group) { 51 if (in_array($group, array('Administrator', 'Super Users'))) { 52 $group = 'admin'; 53 } 54 } 55 return $groups; 56 } 57 58 /** 59 * Here we override PDO's config with Joomla's one. 60 * Called from the constructor 61 */ 62 protected function setupPdoConfig() 63 { 64 require_once $this->joinPaths($this->joomlaPath, 'configuration.php'); 65 66 $this->joomlaConfig = new JConfig; 67 $this->joomlaConfig->dbtype = str_replace('mysqli', 'mysql', $this->joomlaConfig->dbtype); 68 $this->conf['dsn'] = sprintf('%s:dbname=%s;host=%s', $this->joomlaConfig->dbtype, $this->joomlaConfig->db, $this->joomlaConfig->host); 69 $this->conf['user'] = $this->joomlaConfig->user; 70 $this->conf['pass'] = $this->joomlaConfig->password; 71 72 $this->setupPdoQueries(); 73 } 74 75 /** 76 * These are the queries required from authPDO. 77 * Creating/deleting/updating users and groups is done from 78 * Joomla itself so we only need to fetch data from the DB. 79 */ 80 protected function setupPdoQueries() 81 { 82 $this->conf['select-user'] = sprintf(' 83 SELECT username as user, name, email as mail, password as hash, id as uid 84 FROM %s WHERE username = :user', 85 $this->getTableName('users')); 86 87 $this->conf['select-user-groups'] = sprintf(' 88 SELECT title as `group` FROM %s as groups 89 LEFT JOIN %s as groupmap ON groups.id = groupmap.group_id 90 LEFT JOIN %s as user ON groupmap.user_id = user.id 91 WHERE user.username = :user OR user.id = :uid ', 92 $this->getTableName('usergroups'), 93 $this->getTableName('user_usergroup_map'), 94 $this->getTableName('users')); 95 96 $this->conf['select-groups'] = sprintf(' 97 SELECT title as `group`, id as gid FROM %s', 98 $this->getTableName('usergroups')); 99 } 100 101 protected function getTableName($name) 102 { 103 return $this->joomlaConfig->dbprefix . $name; 104 } 105 106 protected function joinPaths() 107 { 108 $args = func_get_args(); 109 $paths = array(); 110 foreach ($args as $arg) { 111 $paths = array_merge($paths, (array)$arg); 112 } 113 $paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths); 114 $paths = array_filter($paths); 115 if (substr($args[0], 0, 1) == '/') { 116 $paths[0] = '/' . $paths[0]; 117 } 118 return join('/', $paths); 119 } 120} 121