1<?php 2/** 3 * DokuWiki Plugin authwordpress (Auth Component) 4 * 5 * Provides authentication against a WordPress MySQL database backend 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; version 2 of the License 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * See the COPYING file in your DokuWiki folder for details 17 * 18 * @author Damien Regad <dregad@mantisbt.org> 19 * @copyright 2015 Damien Regad 20 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 21 * @version 1.1 22 * @link https://github.com/dregad/dokuwiki-authwordpress 23 */ 24 25 26// must be run within Dokuwiki 27if(!defined('DOKU_INC')) die(); 28 29/** 30 * WordPress password hashing framework 31 */ 32require_once('class-phpass.php'); 33 34/** 35 * Authentication class 36 */ 37class auth_plugin_authwordpress extends DokuWiki_Auth_Plugin { 38 39 /** 40 * SQL statement to retrieve User data from WordPress DB 41 * (including group memberships) 42 * '%prefix%' will be replaced by the actual prefix (from plugin config) 43 */ 44 protected $sql_wp_user_data = "SELECT 45 id, user_login, user_pass, user_email, display_name, 46 meta_value AS groups 47 FROM %prefix%users u 48 JOIN %prefix%usermeta m ON u.id = m.user_id 49 WHERE meta_key = '%prefix%capabilities' 50 AND user_login = :user"; 51 52 /** 53 * Wordpress database connection 54 */ 55 protected $db; 56 57 58 /** 59 * Constructor. 60 */ 61 public function __construct() { 62 parent::__construct(); 63 64 $this->cando['getUsers'] = true; 65 66 // Try to establish a connection to the WordPress DB 67 // abort in case of failure 68 try { 69 $this->wp_connect(); 70 } 71 catch (Exception $e) { 72 msg(sprintf($this->getLang('error_connect_failed'), $e->getMessage())); 73 $this->success = false; 74 return; 75 } 76 77 // Initialize SQL query with configured prefix 78 $this->sql_wp_user_data = str_replace( 79 '%prefix%', 80 $this->getConf('prefix'), 81 $this->sql_wp_user_data 82 ); 83 84 $this->success = true; 85 } 86 87 88 /** 89 * Check user+password 90 * 91 * @param string $user the user name 92 * @param string $pass the clear text password 93 * @return bool 94 * 95 * @uses PasswordHash::CheckPassword WordPress password hasher 96 */ 97 public function checkPass($user, $pass) { 98 $data = $this->getUserData($user); 99 if ($data === false) { 100 return false; 101 } 102 103 $hasher = new PasswordHash(8, true); 104 $check = $hasher->CheckPassword($pass, $data['pass']); 105 dbglog("Password " . ($check ? 'OK' : 'Invalid')); 106 107 return $check; 108 } 109 110 /** 111 * Bulk retrieval of user data 112 * 113 * @param int $start index of first user to be returned 114 * @param int $limit max number of users to be returned 115 * @param array $filter array of field/pattern pairs 116 * @return array userinfo (refer getUserData for internal userinfo details) 117 */ 118 public function retrieveUsers($start = 0, $limit = 0, $filter = array()) { 119 msg($this->getLang('user_list_use_wordpress')); 120 return array(); 121 } 122 123 124 /** 125 * Returns info about the given user 126 * 127 * @param string $user the user name 128 * @return array containing user data or false 129 */ 130 public function getUserData($user, $requireGroups=true) { 131 global $conf; 132 133 $stmt = $this->db->prepare($this->sql_wp_user_data); 134 $stmt->bindParam(':user', $user); 135 dbglog("Retrieving data for user '$user'\n" . $this->sql_wp_user_data); 136 137 if (!$stmt->execute()) { 138 // Query execution failed 139 $err = $stmt->errorInfo(); 140 dbglog("Error $err[1]: $err[2]"); 141 return false; 142 } 143 144 $user = $stmt->fetch(PDO::FETCH_ASSOC); 145 if ($user === false) { 146 // Unknown user 147 dbglog("Unknown user"); 148 return false; 149 } 150 151 // Group membership - add DokuWiki's default group 152 $groups = array_keys(unserialize($user['groups'])); 153 if($this->getConf('usedefaultgroup')) { 154 $groups[] = $conf['defaultgroup']; 155 } 156 157 $info = array( 158 'user' => $user['user_login'], 159 'name' => $user['display_name'], 160 'pass' => $user['user_pass'], 161 'mail' => $user['user_email'], 162 'grps' => $groups, 163 ); 164 return $info; 165 } 166 167 168 /** 169 * Connect to Wordpress database 170 * Initializes $db property as PDO object 171 */ 172 protected function wp_connect() { 173 if($this->db) { 174 // Already connected 175 return; 176 } 177 178 // Build connection string 179 $dsn = array( 180 'host=' . $this->getConf('hostname'), 181 'dbname=' . $this->getConf('database'), 182 ); 183 $port = $this->getConf('port'); 184 if ($port) { 185 $dsn[] = 'port=' . $port; 186 } 187 $dsn = 'mysql:' . implode(';', $dsn); 188 189 $this->db = new PDO($dsn, $this->getConf('username'), $this->getConf('password')); 190 } 191 192} 193 194// vim:ts=4:sw=4:noet: 195