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 private $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 private $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 return $hasher->CheckPassword($pass, $data['pass']); 103 } 104 105 106 /** 107 * Returns info about the given user 108 * 109 * @param string $user the user name 110 * @return array containing user data or false 111 */ 112 function getUserData($user, $requireGroups=true) { 113 global $conf; 114 115 $stmt = $this->db->prepare($this->sql_wp_user_data); 116 $stmt->bindParam(':user', $user); 117 118 if (!$stmt->execute()) { 119 // Query execution failed 120 return false; 121 } 122 123 $user = $stmt->fetch(PDO::FETCH_ASSOC); 124 if ($user === false) { 125 // Unknown user 126 return false; 127 } 128 129 // Group membership - add DokuWiki's default group 130 $groups = array_keys(unserialize($user['groups'])); 131 $groups[] = $conf['defaultgroup']; 132 133 $info = array( 134 'user' => $user['user_login'], 135 'name' => $user['display_name'], 136 'pass' => $user['user_pass'], 137 'mail' => $user['user_email'], 138 'grps' => $groups, 139 ); 140 return $info; 141 } 142 143 144 /** 145 * Connect to Wordpress database 146 * Initializes $db property as PDO object 147 */ 148 private function wp_connect() { 149 $dsn = array( 150 'host=' . $this->getConf('hostname'), 151 'dbname=' . $this->getConf('database'), 152 ); 153 $port = $this->getConf('port'); 154 if ($port) { 155 $dsn[] = 'port=' . $port; 156 } 157 $dsn = 'mysql:' . implode(';', $dsn); 158 159 $this->db = new PDO($dsn, $this->getConf('username'), $this->getConf('password')); 160 } 161 162} 163 164// vim:ts=4:sw=4:noet: 165