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 // Try to establish a connection to the WordPress DB 65 // abort in case of failure 66 try { 67 $wp_db = $this->wp_connect(); 68 } 69 catch (Exception $e) { 70 msg(sprintf($this->getLang('error_connect_failed'), $e->getMessage())); 71 $this->success = false; 72 return; 73 } 74 75 // Initialize SQL query with configured prefix 76 $this->sql_wp_user_data = str_replace( 77 '%prefix%', 78 $this->getConf('prefix'), 79 $this->sql_wp_user_data 80 ); 81 82 $this->success = true; 83 } 84 85 86 /** 87 * Check user+password 88 * 89 * @param string $user the user name 90 * @param string $pass the clear text password 91 * @return bool 92 * 93 * @uses PasswordHash::CheckPassword WordPress password hasher 94 */ 95 public function checkPass($user, $pass) { 96 $data = $this->getUserData($user); 97 if ($data === false) { 98 return false; 99 } 100 101 $hasher = new PasswordHash(8, true); 102 $check = $hasher->CheckPassword($pass, $data['pass']); 103 dbglog("Password " . ($check ? 'OK' : 'Invalid')); 104 105 return $check; 106 } 107 108 109 /** 110 * Returns info about the given user 111 * 112 * @param string $user the user name 113 * @return array containing user data or false 114 */ 115 public function getUserData($user, $requireGroups=true) { 116 global $conf; 117 118 $stmt = $this->db->prepare($this->sql_wp_user_data); 119 $stmt->bindParam(':user', $user); 120 dbglog("Retrieving data for user '$user'\n" . $this->sql_wp_user_data); 121 122 if (!$stmt->execute()) { 123 // Query execution failed 124 $err = $stmt->errorInfo(); 125 dbglog("Error $err[1]: $err[2]"); 126 return false; 127 } 128 129 $user = $stmt->fetch(PDO::FETCH_ASSOC); 130 if ($user === false) { 131 // Unknown user 132 dbglog("Unknown user"); 133 return false; 134 } 135 136 // Group membership - add DokuWiki's default group 137 $groups = array_keys(unserialize($user['groups'])); 138 if($this->getConf('usedefaultgroup')) { 139 $groups[] = $conf['defaultgroup']; 140 } 141 142 $info = array( 143 'user' => $user['user_login'], 144 'name' => $user['display_name'], 145 'pass' => $user['user_pass'], 146 'mail' => $user['user_email'], 147 'grps' => $groups, 148 ); 149 return $info; 150 } 151 152 153 /** 154 * Connect to Wordpress database 155 * Initializes $db property as PDO object 156 */ 157 protected function wp_connect() { 158 $dsn = array( 159 'host=' . $this->getConf('hostname'), 160 'dbname=' . $this->getConf('database'), 161 ); 162 $port = $this->getConf('port'); 163 if ($port) { 164 $dsn[] = 'port=' . $port; 165 } 166 $dsn = 'mysql:' . implode(';', $dsn); 167 168 $this->db = new PDO($dsn, $this->getConf('username'), $this->getConf('password')); 169 } 170 171} 172 173// vim:ts=4:sw=4:noet: 174