1<?php 2/** 3 * django auth backend 4 * 5 * @author Andreas Gohr <andi@splitbrain.org> 6 * @author Michael Luggen <michael.luggen at unifr.ch> 7 * @author Robert Czechowski <zgtm at zgtm.de> 8 */ 9 10define('DOKU_AUTH', dirname(__FILE__)); 11define('AUTH_USERFILE',DOKU_CONF.'users.auth.php'); 12 13class auth_plugin_authdjango extends DokuWiki_Auth_Plugin { 14 15 var $dbh = null; // db handle 16 17 /** 18 * Constructor. 19 * 20 * Sets additional capabilities and config strings 21 * @author Michael Luggen <michael.luggen at rhone.ch> 22 * @author Robert Czechowski <zgtm at zgtm.de> 23 */ 24 public function __construct() 25 { 26 parent::__construct(); 27 28 global $config_cascade; 29 global $dbh; 30 31 $this->cando['external'] = true; 32 $this->cando['getGroups'] = true; 33 34 $this->cando['logout'] = !empty($this->getConf('logoff_uri')); 35 36 try { 37 // Connecting, selecting database 38 if ($this->getConf('protocol') == 'sqlite') { 39 $this->dbh = new PDO('sqlite:' . $this->getConf('server')); 40 } 41 else { 42 $this->dbh = new PDO($this->getConf('protocol') . ':host=' . $this->getConf('server') . ';dbname=' . $this->getConf('db'), $this->getConf('user'), $this->getConf('password')); 43 } 44 } catch (PDOException $e) { 45 msg("Can not connect to database!", -1); 46 dbg($e); 47 $this->success = false; 48 } 49 $this->success = true; 50 } 51 52 function trustExternal($user,$pass,$sticky=false){ 53 global $USERINFO; 54 global $conf; 55 global $dbh; 56 57 $sticky ? $sticky = true : $sticky = false; //sanity check 58 59 /** 60 * Just checks against the django sessionid variable, 61 * gets user info from django-database 62 */ 63 64 if (isset($_COOKIE['sessionid']) && $this->dbh) { 65 66 $s_id = $_COOKIE['sessionid']; 67 68 // Look the cookie up in the db 69 $query = 'SELECT session_data FROM django_session WHERE session_key=' . $this->dbh->quote($s_id) . ' LIMIT 1;'; 70 $result = $this->dbh->query($query) or die('Query failed1: ' . $this->dbh->errorInfo()); 71 $ar = $result->fetch(PDO::FETCH_ASSOC); 72 $session_data = $ar['session_data']; 73 74 // TODO: $session_data can now be empty if the session does not exist in database, handle correctly instead of just dying 75 if (strlen($session_data) == 0) { 76 return false; 77 } 78 79 $compressed = false; 80 81 if (str_contains($session_data, ":")) { 82 // New django session encoding since django 4 83 if ($session_data[0] == '.') { 84 $compressed = true; 85 $session_data = substr($session_data, 1); 86 } 87 88 $session_json = base64_decode(strtr(preg_split('/:/', $session_data, 2)[0], "-_", "+/"), true); 89 90 if ($compressed) { 91 $session_json = zlib_decode($session_json); 92 } 93 94 } else { 95 // Old django session enconding until django 3 96 // Decoding the session data: 97 98 $session_json = preg_split('/:/', base64_decode($session_data), 2)[1]; 99 } 100 $userid = json_decode($session_json, true)['_auth_user_id']; 101 $query2 = 'SELECT username, first_name, last_name, email, is_superuser, is_staff FROM auth_user WHERE id=' . $this->dbh->quote($userid) . ' LIMIT 1;'; 102 103 $result2 = $this->dbh->query($query2) or die('Query failed2: ' . print_r($this->dbh->errorInfo())); 104 $user = $result2->fetch(PDO::FETCH_ASSOC); 105 106 $username = $user['username']; 107 $userfullname = $user['first_name'] . " " . $user['last_name']; 108 $useremail = $user['email']; 109 110 // okay we're logged in - set the globals 111 $groups = $this->_getUserGroups($username); 112 113 $USERINFO['name'] = $username; 114 $USERINFO['pass'] = ''; 115 $USERINFO['mail'] = $useremail; 116 117 if (($user['is_superuser'] && $this->getConf('admin_admin') == 1) 118 || ($user['is_staff'] && $this->getConf('staff_admin') == 1)) 119 { 120 $groups[] = 'admin'; 121 } 122 $USERINFO['grps'] = $groups; 123 124 $_SERVER['REMOTE_USER'] = $username; 125 126 $_SESSION[DOKU_COOKIE]['auth']['user'] = $username; 127 $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 128 129 return true; 130 } 131 return false; 132 } 133 134 function _getUserGroups($user){ 135 $query = 'SELECT auth_group.name FROM auth_user, auth_user_groups, auth_group where auth_user.username = ' . $this->dbh->quote($user) . ' AND auth_user.id = auth_user_groups.user_id AND auth_user_groups.group_id = auth_group.id;'; 136 137 $result = $this->dbh->query($query) or die('Query failed3: ' . $this->dbh->errorInfo()); 138 139 $groups = []; 140 foreach ($result as $row) { 141 $groups[] = $row[0]; 142 }; 143 144 if (!in_array("user", $groups)) { 145 $groups[] = "user"; 146 } 147 148 return $groups; 149 } 150 151 function retrieveGroups($start=0,$limit=0){ 152 $query = 'SELECT auth_group.name FROM auth_group'; 153 154 $result = $this->dbh->query($query) or die('Query failed4: ' . $this->dbh->errorInfo()); 155 156 $groups = []; 157 foreach ($result as $row) { 158 $groups[] = $row[0]; 159 }; 160 161 if (!in_array("user", $groups)) { 162 $groups[] = "user"; 163 } 164 165 if (!in_array("admin", $groups)) { 166 $groups[] = "admin"; 167 } 168 169 return $groups; 170 } 171 172 function logOff() { 173 header("Location: " . $this->getConf('logoff_uri')); 174 die(); 175 } 176 177 178 function __destruct() { 179 $this->dbh = null; 180 } 181} 182