In this brief blog entry I'll explain how to install dokuwiki with drupal authentication. First, what is a wiki?
A wiki is a collaborative website whose content can be edited by anyone who has access to it.
Next, what is Dokuwiki?
DokuWiki is a simple to use Wiki aimed at a small companies documentation needs. It works on plain texts files and thus needs no database. It has a simple but powerful Syntax which makes sure the datafiles remain readable outside the Wiki.
The use of just plain text files happens to be the best feature and what I truly imagine a wiki to be; just text.
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/*)
[coolcode]sudo vi /etc/httpd/conf/httpd.conf[/coolcode]
[coolcode download="dokuwiki.rewrite01.txt"]
## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
RewriteEngine on
#
## Not all installations will require the following line. If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
#RewriteBase /
#
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]
RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L]
RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L]
RewriteRule ^$ doku.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) doku.php?id=$1 [QSA,L]
RewriteRule ^index.php$ doku.php
RewriteCond %{QUERY_STRING} \bdo=login\b [NC]
RewriteRule ^.* /user/login [L]
RewriteCond %{QUERY_STRING} \bdo=logout\b [NC]
RewriteRule ^.* /logout [L]
[/coolcode]
Save, Close and restart apache.
[coolcode]:wq
sudo /etc/init.d/httpd restart[/coolcode]
Move to your wiki's auth installation and download Coalface.net's drupal auth script
[coolcode]cd /var/www/html/wiki/inc/auth/
wget http://www.coalface.net/drupal_ext.class.php[/coolcode]
Last edit your local.conf and set drupal auth.
[coolcode]vi /var/www/html/wiki/conf/local.php[/coolcode]
[coolcode]$conf['useacl'] = 1;
$conf['authtype'] = 'drupal_ext';
$conf['auth']['drupal']['file'] = '/var/www/html/sites/default/settings.php';[/coolcode]
And you might want to match the superuser/group with your admin group at Drupal.
[coolcode]$conf['superuser'] = '@admin';[/coolcode]
All done. Visit your drupal site and log-in, then visit your wiki and you will be signed in already (instant). Log out of the wiki and log out of drupal.
NOTE: This drupal auth script is writing 100% by http://www.coalface.net
NOTE 02: I in no way claim credit for this script, just another installation tut regarding my personal CentOS and set-up. The script below is my own personal backup/archive of coalface's script and his DIRECT script SHOULD be used before the one below and only if that site goes missing should this archive/backup be used
[coolcode lang="php" download="drupal_ext.class.php"]
<?php /** * Drupal authentication backend. * * Use Drupal's session and DB backend to authenticate a Dokuwiki user. * All user-handling is delegated to Drupal, including login dialogs, * adding and deleting users etc. All login, logout, register etc. links * need to direct the user to the appropriate Drupal page. * * This only logs a user in if they are already logged into Drupal, i.e. * if the Drupal session cookie is still present. * * Usage: * * 1) Copy this file into your Dokuwiki 'auth' directory--{dokuwiki-base}/inc/auth/. * It must be called 'drupal_ext.class.php'. * 2) In {dokuwiki-base}/conf/local.php, set the following variables: * * $conf['auth']['drupal']['file'] = '{drupal-base}/sites/{inst-dir}/settings.php'; * $conf['authtype'] = 'drupal_ext'; * * where {drupal-base} is your Drupal installation's base directory, and {inst-dir} * refers to the particular sub-directory for this Drupal instance. This might be * 'all' if you only have one Drupal instance, or 'www.example.com' if you have more * than one. * * Original concept inspired by Mohammed Sameer's <msameer@foolab.org> script * for authenticating using Drupal. His script does not provide automatic logging, but * requires the user to log in twice, once into Drupal and next into Dokuwiki. * * @author Walter Gildersleeve <walter@coalface.net> * @todo This assumes that Drupal is using MySQL--any way to alter this? */
class auth_drupal_ext extends auth_basic { var $url; var $db_prefix = ''; var $session_id;
/** c'tor */ function auth_drupal_ext() { global $conf;
$this->cando['external'] = true;
// Drupal is using ini_set in the $drupal_file $ini = ini_get("error_reporting"); ini_set("error_reporting", 0);
//now we can load the file without PHP complaining $drupal_file = $conf['auth']['drupal']['file']; include ($drupal_file);
ini_set("error_reporting", $ini);
//Following Drupal variables are interesting for us: // $db_url, $db_prefix
//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'] = ''; }
/** Strip the leading at-sign from role names. @param $role The original role name. @return string The role name without any preceding at-sign. */ function _tg($role) { return preg_replace("/^@/", "", $role); }
/** * Authenticate the user using Drupal session information. User must login * via the Drupal interface; no login work is done here. * * @param string $user Ignored. * @param string $pass Ignored. * @param bool $sticky Ignored. * @return bool true on successful auth * @todo Drupal roles aren't recognized. Only the super-user in Drupal has * Dokuwiki admin rights. */ function trustExternal($user,$pass,$sticky=false) { global $conf; global $USERINFO;
$logged_in = false;
$link = mysql_connect($this->url['host'], $this->url['user'], $this->url['pass']); if (!$link) { msg('Could not connect: ' . mysql_error()); return; }
if (!mysql_select_db($this->url['path'], $link)) { msg('Can\'t select the database: ' . mysql_error()); mysql_close($link); return; }
$query = 'SELECT u.uid,u.name,u.mail,s.hostname,s.timestamp FROM '.$this->db_prefix. 'users u INNER JOIN '.$this->db_prefix.'sessions s ON u.uid=s.uid WHERE s.sid="'. $_COOKIE['PHPSESSID'].'" && u.status=1';
/* now for the roles */ if ($uid == 1) {/* super user, everything's allowed */ $USERINFO['grps'] = array($this->_tg($conf['superuser']), $this->_tg($conf['defaultgroup'])); } else { $query = 'SELECT r.name FROM '.$this->db_prefix.'users_roles u INNER JOIN '. $this->db_prefix.'role r WHERE u.uid='.$uid.' && u.rid=r.rid'; $result = mysql_query($query);
if ($result) { $tmp = mysql_fetch_row($result); if ($tmp) { $USERINFO['grps'] = array($tmp[0], $this->_tg($conf['defaultgroup'])); } } else { msg('Error querying the database [' . mysql_error() . ']'); } }