Drupal authentication backend for dokuwiki.
Submitted by msameer on Tue, 13/12/2005 - 12:41pm.
This authentication backend'll enable your DokuWiki to authenticate using the drupal database.
This code is pulled from my svn which means that whenever I update it, It'll reflect the file displayed here ;-)
Or get the file directly from here.
This code is pulled from my svn which means that whenever I update it, It'll reflect the file displayed here ;-)
Or get the file directly from here.
<?php
/**
* Drupal authentication backend
*
* Use drupal as an authentication backend.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Mohammed Sameer <msameer@foolab.org>
*/
// TODO: Currently, Each time we have to connct to the db twice, one to auth the
// user and the 2nd to get the info ??
// Groups with spaces'll screw us.
// ChangeLog:
// 2005-12-23: Use header(); instead of redirect();
// 2006-07-23: Ported to the new dokuwiki auth code.
// use mysql_real_escape_string()
// We now get the groups using a join instead of multiple queries.
// 2006-08-24: in getUserData(): Check that we have results when we try to get
// the user details from the database (Thanks Matthew Robinson of fone-me.com)
// 2006-08-30: Check that the user isn't blocked. Thanks alienbrain of EGLUG for the note.
// 2007-01-29: urldecode the username, password, hostname and path (Thanks Walter G).
// 2007-06-29: $db_url for drupal can be an array, we now account for this.
class auth_drupal extends auth_basic {
var $url = array();
// Constructor.
function auth_drupal() {
global $conf;
// This is a hack because drupal is using ini_set in the $drupal_file
// and php'll complain
$ini = ini_get("error_reporting");
ini_set("error_reporting", 0);
$drupal_file = $conf['auth']['drupal']['file'];
include ($drupal_file);
ini_set("error_reporting", $ini);
// http://drupal.org/node/18429
$this->url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
$this->url['path'] = substr($this->url['path'], 1);
$this->url['db_prefix'] = $db_prefix;
//Adapted from the Drupal database.mysql.inc code
//Decode url-encoded information in the db connection string
$this->url['user'] = urldecode($this->url['user']);
// Test if database url has a password.
if(isset($this->url['pass'])) {
$this->url['pass'] = urldecode($this->url['pass']);
} else {
$this->url['pass'] = '';
}
$this->url['host'] = urldecode($this->url['host']);
$this->url['path'] = urldecode($this->url['path']);
// Establish the connection.
$this->url['link'] = mysql_connect($this->url['host'], $this->url['user'], $this->url['pass']);
if (!$this->url['link']) {
msg('Could not connect: ' . mysql_error());
$this->success = false;
return;
}
if (!mysql_select_db($this->url['path'], $this->url['link'])) {
msg('Can\'t select the database: ' . mysql_error());
$this->success = false;
return;
}
// Note: No capabilities. We only handle authentication.
// Fuck php4. No Destructor.
register_shutdown_function("auth_drupal_disconnect", $this);
}
/**
* Check user+password [required auth function]
*
* Checks if the given user exists and the given
* plaintext password is correct
*
* @author Mohammed Sameer <msameer@foolab.org>
* @return bool
*/
function checkPass($user,$pass)
{
$user = mysql_real_escape_string($user, $this->url['link']);
$password = mysql_real_escape_string(md5($pass), $this->url['link']);
$result = mysql_query("SELECT uid FROM ".$url['db_prefix']."users WHERE name = \"$user\" AND pass = \"$password\" AND status=1");
if (!$result)
{
msg('Invalid query: ' . mysql_error());
$this->disconnect();
return false;
}
$num = mysql_num_rows($result);
//$this->disconnect();
return ($num == 0 ? false : true);
}
/**
* Return user info [required auth function]
* at least these fields:
*
* name string full name of the user
* mail string email addres of the user
* grps array list of groups the user is in
*
* @author Mohammed Sameer <msameer@foolab.org>
*/
function getUserData($user) {
$info['name'] = $user;
$result = mysql_query("SELECT uid, mail FROM ".$url['db_prefix']."users WHERE name = \"".mysql_real_escape_string($user, $this->url['link'])."\"");
if (!$result)
{
msg('Invalid query: ' . mysql_error());
$this->disconnect();
return false;
}
// This shouldn't fail but who knows ?
$tmp = mysql_fetch_row($result);
if (!$tmp)
return false;
$uid = $tmp[0];
// FILL THE EMAIL.
$info['mail'] = $tmp[1];
// Now let's get the groups of the user.
$result = mysql_query("select r.name from role r inner join users_roles u on u.rid=r.rid and u.uid=$uid");
if (!$result)
{
msg('Invalid query: ' . mysql_error());
$this->disconnect();
return false;
}
while($tmp = mysql_fetch_row($result))
$info['grps'][] = $tmp[0];
// $this->disconnect();
return $info;
}
}
/**
* Disconnect from the database.
* @author Mohammed Sameer <msameer@foolab.org>
*/
function auth_drupal_disconnect($obj) {
mysql_close($obj->url['link']);
}
?>
Under:












Ok, how do I get this to work ?
I have the same question like that of Mostafa:
How to make this script work? And where to put it?
Thanks for help.
Are you sure you had a look at the dokuwiki authentication manual ?
I'm out of Cairo ATM, Sorry I can't provide more help.
The first mysql_fetch_row() in getUserData() should be replaced with
<?phpif (! $tmp = mysql_fetch_row($result)) {
return false;
}
?>
You'l also need to change the first query in getUserData() to include
AND pass='$pass'This will provide some error checking, and the function will also double password checker, removing the need for 2 db queries.
Thanks Matthew.
I'm now checking that we are getting results in getUserData()
But do you think that checking the user password is really needed ?
Well it makes the getUserData() double as an authentication function, reducing the amount of database queries and making the code easier to read. I dont have a clue about any of your other code (I accidentily stumbled accross this page), but you could possibly rename the function to login, and do something like:
$_SESSION = login($username,$password)
or similar.
then $_SESSION is either false (not logged in) or is populated with the array(?) passed back from the getUserData function (or login if you decide to rename it)
I can't really rename the function because this is how the dokuwiki authentication works.
Well, I can get all the variables I need using 1 query in the constructor and the other functions will just return them. It just needs a rewrite ;)
In TODO there is "Groups with spaces'll screw us."
For this, I had to change:
$info['grps'][] = $tmp[0];to:
$info['grps'][] = str_replace(' ', '_', $tmp[0]);to be able to use something like @authenticated_user in acl.
Also, I've a suggestion, I think this backend should also check if the user is blocked or not.
I'm now checking that the user is active. Thanks for the note.
I don't like replacing the spaces with underscores. What if I have an identical group but with underscores ?
I have a drupal site, www.theeyes.org and a wiki, creta.theeyes.org
I tried to make the script working, without success.
The question is: where does the backend take the $drupal_file?
I see the lines:
$drupal_file = $conf['auth']['drupal']['file'];
include ($drupal_file);
I imagine $drupal_file is the settings.php, but where does the script look for this file? I have it in www.theeyes.org/sites/theeyes.org/settings.php.
I tried to copy it in creta.theeyes.org/sites/theeyes.org/settings.php or to set the docroot for creta insiede the drupal site, so now i have the wiki in creta.theeyes.org and www.theeyes.org/creta. But again, the wiki can't connect to mysql server. Any ideas?
Sorry for my bad english...
You set $conf['auth']['drupal']['file'] in local.php
$conf['auth']['drupal']['file'] should point to the location of the file on the server. For me I have it set to:
$conf['auth']['drupal']['file'] = "/home/vhosts/foolab.org/www/sites/default/settings.php";And don't forget to set authtype
$conf['authtype'] = 'drupal';Thank you,
now it works :)
Now I try to use the same cookie for dokuwiki and drupal, if I have fortune, I'll post a comment here ;)
Thanks again!
Hi!
In principle the authentification works. But when I login with the drupal admin user, that user is not admin in dokuwiki. How can this be fixed?
Thanks.
Christopher
What you need to do is to set that user as the dokuwiki admin:
$conf['superuser'] = 'msameer'; //The admin can be user or @groupin local.php
I'm planning for more integration between drupal and dokuwiki but I have no time to really work on it.
Hi, I tried to use your auth method, but it didnt work.
I'm using
- dokuwiki-2006-11-06
- drupal 4.7.4
- mysql (I believe its version 4)
- php (both 4 and 5 are installed)
The mysql database and the webserver are in different machines.
I'm using a hosting service, so i can't run commands, but i can ask them whatever you need to know.
The error i have:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in -path_to_wiki-/inc/auth/drupal.class.php on line 43
Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
I have created the drupal.class.php file and filled the $conf['auth']['drupal']['file'] in the local.php file.
I have searched around and it says it has to do with either the mysql service being down (which it isn't, since i can log in to drupal), or a missconfiguration.
I know a bit of programming, but i dont know either php or sql, so i'm not sure where to modify. It may be because of the db being in a sepparate machine? I'm missing something?
Thanks a lot for your time and for contributing this code.
Are you sure that the path to the file is correct ?
Damn, a "t" in settings, I swear i doublechecked it.
I'm really sorry for having such a little thing take.
Thanks a lot.
Congratulations for the site :-)
Cheers.
Hi Mohammed,
I just started using your Drupal auth for dokuwiki, and ran into a problem. Namely, my DB password was in the form "a+password," which Drupal stores as "a%2Bpassword." To fix this, I added the following code before making the MySQL connection.
<?php//Adapted from the Drupal database.mysql.inc code
//Decode url-encoded information in the db connection string
$this->url['user'] = urldecode($this->url['user']);
// Test if database url has a password.
if(isset($this->url['pass'])) {
$this->url['pass'] = urldecode($this->url['pass']);
} else {
$this->url['pass'] = '';
}
$this->url['host'] = urldecode($this->url['host']);$this->url['path'] = urldecode($this->url['path']);
?>
Hope this is useful!
Walter
Thanks. I've added the snippet!
Hi - I'm getting these two error messages in Dokuwiki:
Can't select the database: No database selected
User authentication is temporarily unavailable. If this situation persists, please inform your Wiki Admin.
Here is my local.php (conf directory)
<?php
$conf['title'] = 'mysite';
$conf['lang'] = 'en';
$conf['useacl'] = 1;
$conf['superuser'] = '@admin';
$conf['authtype'] = 'drupal';
$conf['auth']['drupal']['file'] = "/drupal-5.1/sites/default/settings.php";
I created drupal.class.php in the inc\auth directory, using Mohammed's code above.
I must be forgetting something easy!
Drupal can connect but dokuwiki can't ?
Do you have non alphanumeric charcters in the database name or the username or the password ?
Here is my database name from my settings.php file...
$db_url = 'mysql://mysite:db405is@localhost/mysitedrupal';
Could this be the problem?
That shouldn't be a problem.
The only thing that I can think of is the path to the drupal settings.php
I'm planning to use IMAP authentication on Drupal and have Doku authenticate against the Drupal Database.
Hope it works. :)
Username logins will be
+ password
It will work if the usernames and passwords are in the drupal database
Where's the file? cvs is 404 and the node says "can't open the file!". *shrugs*
Sorry. I moved to subversion and forgot to update this. Fixed. Sorry!
The "Edit this page" and "Login" buttons still show up in Dokuwiki after I've followed the above instructions.
I'm at Dokuwiki release 2006-11-06 and Drupal v.5.1 . I've copied the above text as drupal.class.php into inc/auth . I've configured the database string so that I don't get the "Can't select the database: No database selected" message any more.
Can you help me figure out what I'm missing?
In local.php, I have:
/* Authentication Options */$conf['useacl'] = 1;
$conf['openregister']= 0;
$conf['superuser'] = '@admin';
$conf['authtype'] = 'drupal';
$conf['auth']['drupal']['file'] = "/home/web/mywebsite/drupal/sites/default/settings.php";
If I run doku.php?do=check, I get:
DokuWiki version: Release 2006-11-06PHP version 5.2.1
Changelog is writable
Datadir is writable
Attic is writable
Mediadir is writable
Cachedir is writable
Lockdir is writable
conf/users.auth.php is writable
mb_string extension is available and will be used
Debugging support is disabled
Your current permission for this page is 4
The current page is writable by the webserver
The current page is writable by you
I've logged out of Drupal on Firefox and closed the browser, to get the same effect. On IE, there's a message about "Done, with errors on page", but I don't know how to check those errors.
I seem so close ... and yet so far away! Thanks.
What's in your conf/acl.auth.php ?
Mine has:
# acl.auth.php# <?php exit()?>
# Don't modify the lines above
#
# Access Control
#
# none 0
# read 1
# edit 2
# create 4
# upload 8
* @ALL 1
Thanks for telling me about the new dokuwiki release. I updated mine :-)
My acl.auth.php had @ALL 4. I copied yours. The button immediately changed from "Edit this page" to "Show pagesource". That's progress. Thanks for that help.
When I fill in the login screens on Dokuwiki with the Drupal userid and password, my userid shows up at the bottom of the page. However, the "Show pagesource" button remains.
My colleague has pointed out that in dokuwiki.php , we have the following settings:
/* Authentication Options - read http://www.splitbrain.org/dokuwiki/wiki:acl */$conf['useacl'] = 0; //Use Access Control Lists to restrict access?
$conf['autopasswd'] = 1; //autogenerate passwords and email them to user
$conf['authtype'] = 'plain'; //which authentication backend should be used
$conf['passcrypt'] = 'smd5'; //Used crypt method (smd5,md5,sha1,ssha,crypt,mysql,my411)
$conf['defaultgroup']= 'user'; //Default groups new Users are added to
$conf['superuser'] = '!!not set!!'; //The admin can be user or @group
$conf['profileconfirm'] = '1'; //Require current password to confirm changes to user profile
$conf['disableactions'] = ''; //comma separated list of actions to disable
What do you have in your dokuwiki.php ? (I don't know why this content seems redundant. Does local.php take precedence?)
Yup, local.php takes precedence
I guess you need to set the ACLs for group admin and add the user you ar using there.
For me, I have only one user who is allowed to edit thus:
$conf['superuser'] = 'msameer';in local.conf
Thanks for the help. I've changed the superuser from @group to the single admin id. When I log into Dokuwiki now, the "Edit this page" has come back.
When I try with my regular userid, though -- we have multiple userids in Drupal defined with "administrator" privileges -- I don't get the "Edit this page".
How do you give Dokuwiki editing privileges to specific Drupal userids? Is this done in Dokuwiki or in Drupal?
You assign them to a certain drupal group, then you set the dokuwiki proper ACLs for that group.
I didn't really do a setup like this as I didn't need it.
I guess you need to check the documentation for dokuwiki on how to setup ACLs for groups.
Glad it worked for you!
[Thu Jun 28 19:15:11 2007] [error] [client 192.168.1.4] PHP Warning: parse_url() expects parameter 1 to be string, array given in /var/www/wiki/inc/auth/
drupal.class.php on line 40, referer: http://example.com/wiki/doku.php/aimb
ot
Ideas?
is $db_url an array ?
I fixed this situation. Now $db_url['default'] will be used if $db_url is an array. I didn't test it yet.
In this brief blog entry I'll explain how to install dokuwiki with drupal authentication.
So you have Drupal installed and your user account already established. Next install dokuwiki. Once that's done we now append the rewrite prefs for the clean .htaccess style clean-urls. Add to /etc/httpd/conf/httpd.conf (assumes directory = /var/www/html/wiki and Drupal is installed at /var/www/html/*). Read more...
Broken w/ Drupal v5.2? Worked just fine and then *poof*.
What's broken ? I'm running 5.2
My v5.2 is broken also. any news on a fix ?
What do you mean by broken ?