xref: /dokuwiki/inc/auth.php (revision d2dde4eb797d69646f31f2b86b686db7707427d3)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * Authentication library
415fae107Sandi *
515fae107Sandi * Including this file will automatically try to login
615fae107Sandi * a user by calling auth_login()
715fae107Sandi *
815fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
915fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
1015fae107Sandi */
1115fae107Sandi
12ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
13ed7b5f09Sandi  require_once(DOKU_INC.'inc/common.php');
14ed7b5f09Sandi  require_once(DOKU_INC.'inc/io.php');
15ed7b5f09Sandi  require_once(DOKU_INC.'inc/blowfish.php');
16ed7b5f09Sandi  require_once(DOKU_INC.'inc/mail.php');
178b06d178Schris
188b06d178Schris  // load the the backend auth functions and instantiate the auth object
198b06d178Schris  if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) {
208b06d178Schris      require_once(DOKU_INC.'inc/auth/basic.class.php');
218b06d178Schris      require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php');
228b06d178Schris
238b06d178Schris      $auth_class = "auth_".$conf['authtype'];
248b06d178Schris      if (!class_exists($auth_class)) $auth_class = "auth_basic";
258b06d178Schris      $auth = new $auth_class();
26*d2dde4ebSMatthias Grimm      if ($auth->success == false) {
27*d2dde4ebSMatthias Grimm          msg($lang['authmodfailed'],-1);
28*d2dde4ebSMatthias Grimm          unset($auth);
29*d2dde4ebSMatthias Grimm      }
308b06d178Schris
318b06d178Schris      // interface between current dokuwiki/old auth system and new style auth object
328b06d178Schris      function auth_canDo($fn) {
338b06d178Schris        global $auth;
348b06d178Schris        return method_exists($auth, $fn);
358b06d178Schris      }
368b06d178Schris
378b06d178Schris      // mandatory functions - these should exist
388b06d178Schris      function auth_checkPass($user,$pass) {
398b06d178Schris        global $auth;
408b06d178Schris        return method_exists($auth,'checkPass') ? $auth->checkPass($user, $pass) : false;
418b06d178Schris      }
428b06d178Schris
438b06d178Schris      function auth_getUserData($user) {
448b06d178Schris        global $auth;
458b06d178Schris        return method_exists($auth, 'getUserData') ? $auth->getUserData($user) : false;
468b06d178Schris      }
478b06d178Schris
488b06d178Schris      // optional functions, behave gracefully if these don't exist;
498b06d178Schris      // potential calling code should query whether these exist in advance
508b06d178Schris      function auth_createUser($user,$pass,$name,$mail) {
518b06d178Schris        global $auth;
528b06d178Schris        return method_exists($auth, 'createUser') ? $auth->createUser($user,$pass,$name,$mail) : null;
538b06d178Schris      }
548b06d178Schris
558b06d178Schris      function auth_modifyUser($user, $changes) {
568b06d178Schris        global $auth;
578b06d178Schris        return method_exists($auth, 'modifyUser') ? $auth->modifyUser($user,$changes) : false;
588b06d178Schris      }
598b06d178Schris
608b06d178Schris      function auth_deleteUsers($users) {
618b06d178Schris        global $auth;
628b06d178Schris        return method_exists($auth, 'deleteUsers') ? $auth->deleteUsers($users) : 0;
638b06d178Schris      }
648b06d178Schris
658b06d178Schris      // other functions, will only be accessed by new code
668b06d178Schris      //- these must query auth_canDo() or test method existence themselves.
678b06d178Schris
688b06d178Schris  } else {
698b06d178Schris    // old style auth functions
70f62ea8a1Sandi    require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.php');
718b06d178Schris    $auth = null;
728b06d178Schris
738b06d178Schris    // new function, allows other parts of dokuwiki to know what they can and can't do
748b06d178Schris    function auth_canDo($fn) { return function_exists("auth_$fn"); }
758b06d178Schris  }
76f3f0262cSandi
771e866646Sandi  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5($conf['title']));
78e65afed4SSameer D. Sahasrabuddhe
7915fae107Sandi  // some ACL level defines
80f3f0262cSandi  define('AUTH_NONE',0);
81f3f0262cSandi  define('AUTH_READ',1);
82f3f0262cSandi  define('AUTH_EDIT',2);
83f3f0262cSandi  define('AUTH_CREATE',4);
84f3f0262cSandi  define('AUTH_UPLOAD',8);
858ef6b7caSandi  define('AUTH_DELETE',16);
8610a76f6fSfrank  define('AUTH_ADMIN',255);
87f3f0262cSandi
88f3f0262cSandi  if($conf['useacl']){
89132bdbfeSandi    auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
9015fae107Sandi    //load ACL into a global array
91e7cb32dcSAndreas Gohr    if(is_readable(DOKU_CONF.'acl.auth.php')){
92e7cb32dcSAndreas Gohr      $AUTH_ACL = file(DOKU_CONF.'acl.auth.php');
9311799630Sandi    }else{
9411799630Sandi      $AUTH_ACL = array();
9511799630Sandi    }
96f3f0262cSandi  }
97f3f0262cSandi
98f3f0262cSandi/**
99f3f0262cSandi * This tries to login the user based on the sent auth credentials
100f3f0262cSandi *
101f3f0262cSandi * The authentication works like this: if a username was given
10215fae107Sandi * a new login is assumed and user/password are checked. If they
10315fae107Sandi * are correct the password is encrypted with blowfish and stored
10415fae107Sandi * together with the username in a cookie - the same info is stored
10515fae107Sandi * in the session, too. Additonally a browserID is stored in the
10615fae107Sandi * session.
10715fae107Sandi *
10815fae107Sandi * If no username was given the cookie is checked: if the username,
10915fae107Sandi * crypted password and browserID match between session and cookie
11015fae107Sandi * no further testing is done and the user is accepted
11115fae107Sandi *
11215fae107Sandi * If a cookie was found but no session info was availabe the
113136ce040Sandi * blowfish encrypted password from the cookie is decrypted and
11415fae107Sandi * together with username rechecked by calling this function again.
115f3f0262cSandi *
116f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO
117f3f0262cSandi * are set.
11815fae107Sandi *
11915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
12015fae107Sandi *
12115fae107Sandi * @param   string  $user    Username
12215fae107Sandi * @param   string  $pass    Cleartext Password
12315fae107Sandi * @param   bool    $sticky  Cookie should not expire
12415fae107Sandi * @return  bool             true on successful auth
125f3f0262cSandi*/
126132bdbfeSandifunction auth_login($user,$pass,$sticky=false){
127f3f0262cSandi  global $USERINFO;
128f3f0262cSandi  global $conf;
129f3f0262cSandi  global $lang;
130132bdbfeSandi  $sticky ? $sticky = true : $sticky = false; //sanity check
131f3f0262cSandi
132f3f0262cSandi  if(isset($user)){
133132bdbfeSandi    //usual login
134f3f0262cSandi    if (auth_checkPass($user,$pass)){
135132bdbfeSandi      // make logininfo globally available
136f3f0262cSandi      $_SERVER['REMOTE_USER'] = $user;
137132bdbfeSandi      $USERINFO = auth_getUserData($user); //FIXME move all references to session
138132bdbfeSandi
139132bdbfeSandi      // set cookie
140132bdbfeSandi      $pass   = PMA_blowfish_encrypt($pass,auth_cookiesalt());
141132bdbfeSandi      $cookie = base64_encode("$user|$sticky|$pass");
142132bdbfeSandi      if($sticky) $time = time()+60*60*24*365; //one year
143e65afed4SSameer D. Sahasrabuddhe      setcookie(DOKU_COOKIE,$cookie,$time,'/');
144132bdbfeSandi
145132bdbfeSandi      // set session
146132bdbfeSandi      $_SESSION[$conf['title']]['auth']['user'] = $user;
147132bdbfeSandi      $_SESSION[$conf['title']]['auth']['pass'] = $pass;
148132bdbfeSandi      $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid();
149132bdbfeSandi      $_SESSION[$conf['title']]['auth']['info'] = $USERINFO;
150132bdbfeSandi      return true;
151f3f0262cSandi    }else{
152f3f0262cSandi      //invalid credentials - log off
153f3f0262cSandi      msg($lang['badlogin'],-1);
154f3f0262cSandi      auth_logoff();
155132bdbfeSandi      return false;
156f3f0262cSandi    }
157f3f0262cSandi  }else{
158132bdbfeSandi    // read cookie information
159e65afed4SSameer D. Sahasrabuddhe    $cookie = base64_decode($_COOKIE[DOKU_COOKIE]);
160132bdbfeSandi    list($user,$sticky,$pass) = split('\|',$cookie,3);
161132bdbfeSandi    // get session info
162132bdbfeSandi    $session = $_SESSION[$conf['title']]['auth'];
163132bdbfeSandi
164132bdbfeSandi    if($user && $pass){
165132bdbfeSandi      // we got a cookie - see if we can trust it
166132bdbfeSandi      if(isset($session) &&
167132bdbfeSandi        ($session['user'] == $user) &&
168132bdbfeSandi        ($session['pass'] == $pass) &&  //still crypted
169132bdbfeSandi        ($session['buid'] == auth_browseruid()) ){
170132bdbfeSandi        // he has session, cookie and browser right - let him in
171132bdbfeSandi        $_SERVER['REMOTE_USER'] = $user;
172132bdbfeSandi        $USERINFO = $session['info']; //FIXME move all references to session
173132bdbfeSandi        return true;
174132bdbfeSandi      }
175132bdbfeSandi      // no we don't trust it yet - recheck pass
176132bdbfeSandi      $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt());
177132bdbfeSandi      return auth_login($user,$pass,$sticky);
178132bdbfeSandi    }
179132bdbfeSandi  }
180f3f0262cSandi  //just to be sure
181f3f0262cSandi  auth_logoff();
182132bdbfeSandi  return false;
183f3f0262cSandi}
184132bdbfeSandi
185132bdbfeSandi/**
186136ce040Sandi * Builds a pseudo UID from browser and IP data
187132bdbfeSandi *
188132bdbfeSandi * This is neither unique nor unfakable - still it adds some
189136ce040Sandi * security. Using the first part of the IP makes sure
190136ce040Sandi * proxy farms like AOLs are stil okay.
19115fae107Sandi *
19215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
19315fae107Sandi *
19415fae107Sandi * @return  string  a MD5 sum of various browser headers
195132bdbfeSandi */
196132bdbfeSandifunction auth_browseruid(){
197132bdbfeSandi  $uid  = '';
198132bdbfeSandi  $uid .= $_SERVER['HTTP_USER_AGENT'];
199132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_ENCODING'];
200132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
201132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_CHARSET'];
202136ce040Sandi  $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.'));
203132bdbfeSandi  return md5($uid);
204132bdbfeSandi}
205132bdbfeSandi
206132bdbfeSandi/**
207132bdbfeSandi * Creates a random key to encrypt the password in cookies
20815fae107Sandi *
20915fae107Sandi * This function tries to read the password for encrypting
21098407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt'
21115fae107Sandi * if no such file is found a random key is created and
21215fae107Sandi * and stored in this file.
21315fae107Sandi *
21415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
21515fae107Sandi *
21615fae107Sandi * @return  string
217132bdbfeSandi */
218132bdbfeSandifunction auth_cookiesalt(){
219132bdbfeSandi  global $conf;
22098407a7aSandi  $file = $conf['metadir'].'/_htcookiesalt';
221132bdbfeSandi  $salt = io_readFile($file);
222132bdbfeSandi  if(empty($salt)){
223132bdbfeSandi    $salt = uniqid(rand(),true);
224132bdbfeSandi    io_saveFile($file,$salt);
225132bdbfeSandi  }
226132bdbfeSandi  return $salt;
227f3f0262cSandi}
228f3f0262cSandi
229f3f0262cSandi/**
230f3f0262cSandi * This clears all authenticationdata and thus log the user
231f3f0262cSandi * off
23215fae107Sandi *
23315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
234f3f0262cSandi */
235f3f0262cSandifunction auth_logoff(){
236f3f0262cSandi  global $conf;
237f3f0262cSandi  global $USERINFO;
2388b06d178Schris  global $INFO, $ID;
23937065e65Sandi
24037065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['user']))
241132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['user']);
24237065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['pass']))
243132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['pass']);
24437065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['info']))
245132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['info']);
24637065e65Sandi  if(isset($_SERVER['REMOTE_USER']))
247f3f0262cSandi    unset($_SERVER['REMOTE_USER']);
248132bdbfeSandi  $USERINFO=null; //FIXME
2491e866646Sandi  setcookie(DOKU_COOKIE,'',time()-600000,'/');
250f3f0262cSandi}
251f3f0262cSandi
252f3f0262cSandi/**
25315fae107Sandi * Convinience function for auth_aclcheck()
25415fae107Sandi *
25515fae107Sandi * This checks the permissions for the current user
25615fae107Sandi *
25715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
25815fae107Sandi *
25915fae107Sandi * @param  string  $id  page ID
26015fae107Sandi * @return int          permission level
261f3f0262cSandi */
262f3f0262cSandifunction auth_quickaclcheck($id){
263f3f0262cSandi  global $conf;
264f3f0262cSandi  global $USERINFO;
265f3f0262cSandi  # if no ACL is used always return upload rights
266f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
267f3f0262cSandi  return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']);
268f3f0262cSandi}
269f3f0262cSandi
270f3f0262cSandi/**
271f3f0262cSandi * Returns the maximum rights a user has for
272f3f0262cSandi * the given ID or its namespace
27315fae107Sandi *
27415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
27515fae107Sandi *
27615fae107Sandi * @param  string  $id     page ID
27715fae107Sandi * @param  string  $user   Username
27815fae107Sandi * @param  array   $groups Array of groups the user is in
27915fae107Sandi * @return int             permission level
280f3f0262cSandi */
281f3f0262cSandifunction auth_aclcheck($id,$user,$groups){
282f3f0262cSandi  global $conf;
283f3f0262cSandi  global $AUTH_ACL;
284f3f0262cSandi
285f3f0262cSandi  # if no ACL is used always return upload rights
286f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
287f3f0262cSandi
28810a76f6fSfrank  //if user is superuser return 255 (acl_admin)
28910a76f6fSfrank  if($conf['superuser'] == $user) { return AUTH_ADMIN; }
29010a76f6fSfrank
291074cf26bSandi  //make sure groups is an array
292074cf26bSandi  if(!is_array($groups)) $groups = array();
293074cf26bSandi
29410a76f6fSfrank  //prepend groups with @
2952cd2db38Sandi  $cnt = count($groups);
2962cd2db38Sandi  for($i=0; $i<$cnt; $i++){
29710a76f6fSfrank    $groups[$i] = '@'.$groups[$i];
29810a76f6fSfrank  }
29910a76f6fSfrank  //if user is in superuser group return 255 (acl_admin)
30010a76f6fSfrank  if(in_array($conf['superuser'], $groups)) { return AUTH_ADMIN; }
30110a76f6fSfrank
302f3f0262cSandi  $ns    = getNS($id);
303f3f0262cSandi  $perm  = -1;
304f3f0262cSandi
305f3f0262cSandi  if($user){
306f3f0262cSandi    //add ALL group
307f3f0262cSandi    $groups[] = '@ALL';
308f3f0262cSandi    //add User
309f3f0262cSandi    $groups[] = $user;
310f3f0262cSandi    //build regexp
311f3f0262cSandi    $regexp   = join('|',$groups);
312f3f0262cSandi  }else{
313f3f0262cSandi    $regexp = '@ALL';
314f3f0262cSandi  }
315f3f0262cSandi
316f3f0262cSandi  //check exact match first
31742905504SAndreas Gohr  $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL);
318f3f0262cSandi  if(count($matches)){
319f3f0262cSandi    foreach($matches as $match){
320f3f0262cSandi      $match = preg_replace('/#.*$/','',$match); //ignore comments
321f3f0262cSandi      $acl   = preg_split('/\s+/',$match);
3228ef6b7caSandi      if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
323f3f0262cSandi      if($acl[2] > $perm){
324f3f0262cSandi        $perm = $acl[2];
325f3f0262cSandi      }
326f3f0262cSandi    }
327f3f0262cSandi    if($perm > -1){
328f3f0262cSandi      //we had a match - return it
329f3f0262cSandi      return $perm;
330f3f0262cSandi    }
331f3f0262cSandi  }
332f3f0262cSandi
333f3f0262cSandi  //still here? do the namespace checks
334f3f0262cSandi  if($ns){
335f3f0262cSandi    $path = $ns.':\*';
336f3f0262cSandi  }else{
337f3f0262cSandi    $path = '\*'; //root document
338f3f0262cSandi  }
339f3f0262cSandi
340f3f0262cSandi  do{
341f3f0262cSandi    $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL);
342f3f0262cSandi    if(count($matches)){
343f3f0262cSandi      foreach($matches as $match){
344f3f0262cSandi        $match = preg_replace('/#.*$/','',$match); //ignore comments
345f3f0262cSandi        $acl   = preg_split('/\s+/',$match);
3468ef6b7caSandi        if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
347f3f0262cSandi        if($acl[2] > $perm){
348f3f0262cSandi          $perm = $acl[2];
349f3f0262cSandi        }
350f3f0262cSandi      }
351f3f0262cSandi      //we had a match - return it
352f3f0262cSandi      return $perm;
353f3f0262cSandi    }
354f3f0262cSandi
355f3f0262cSandi    //get next higher namespace
356f3f0262cSandi    $ns   = getNS($ns);
357f3f0262cSandi
358f3f0262cSandi    if($path != '\*'){
359f3f0262cSandi      $path = $ns.':\*';
360f3f0262cSandi      if($path == ':\*') $path = '\*';
361f3f0262cSandi    }else{
362f3f0262cSandi      //we did this already
363f3f0262cSandi      //looks like there is something wrong with the ACL
364f3f0262cSandi      //break here
365d5ce66f6SAndreas Gohr      msg('No ACL setup yet! Denying access to everyone.');
366d5ce66f6SAndreas Gohr      return AUTH_NONE;
367f3f0262cSandi    }
368f3f0262cSandi  }while(1); //this should never loop endless
36952a5af8dSandi
37052a5af8dSandi  //still here? return no permissions
37152a5af8dSandi  return AUTH_NONE;
372f3f0262cSandi}
373f3f0262cSandi
374f3f0262cSandi/**
375f3f0262cSandi * Create a pronouncable password
376f3f0262cSandi *
37715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
37815fae107Sandi * @link    http://www.phpbuilder.com/annotate/message.php3?id=1014451
37915fae107Sandi *
38015fae107Sandi * @return string  pronouncable password
381f3f0262cSandi */
382f3f0262cSandifunction auth_pwgen(){
383f3f0262cSandi  $pw = '';
384f3f0262cSandi  $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
385f3f0262cSandi  $v  = 'aeiou';              //vowels
386f3f0262cSandi  $a  = $c.$v;                //both
387f3f0262cSandi
388f3f0262cSandi  //use two syllables...
389f3f0262cSandi  for($i=0;$i < 2; $i++){
390f3f0262cSandi    $pw .= $c[rand(0, strlen($c)-1)];
391f3f0262cSandi    $pw .= $v[rand(0, strlen($v)-1)];
392f3f0262cSandi    $pw .= $a[rand(0, strlen($a)-1)];
393f3f0262cSandi  }
394f3f0262cSandi  //... and add a nice number
395f3f0262cSandi  $pw .= rand(10,99);
396f3f0262cSandi
397f3f0262cSandi  return $pw;
398f3f0262cSandi}
399f3f0262cSandi
400f3f0262cSandi/**
401f3f0262cSandi * Sends a password to the given user
402f3f0262cSandi *
40315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
40415fae107Sandi *
40515fae107Sandi * @return bool  true on success
406f3f0262cSandi */
407f3f0262cSandifunction auth_sendPassword($user,$password){
408f3f0262cSandi  global $conf;
409f3f0262cSandi  global $lang;
410f3f0262cSandi  $hdrs  = '';
41187ddda95Sandi  $userinfo = auth_getUserData($user);
412f3f0262cSandi
41387ddda95Sandi  if(!$userinfo['mail']) return false;
414f3f0262cSandi
415f3f0262cSandi  $text = rawLocale('password');
416ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
41787ddda95Sandi  $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
418f3f0262cSandi  $text = str_replace('@LOGIN@',$user,$text);
419f3f0262cSandi  $text = str_replace('@PASSWORD@',$password,$text);
420f3f0262cSandi  $text = str_replace('@TITLE@',$conf['title'],$text);
421f3f0262cSandi
42244f669e9Sandi  return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>',
42344f669e9Sandi                   $lang['regpwmail'],
42444f669e9Sandi                   $text,
42544f669e9Sandi                   $conf['mailfrom']);
426f3f0262cSandi}
427f3f0262cSandi
428f3f0262cSandi/**
42915fae107Sandi * Register a new user
430f3f0262cSandi *
43115fae107Sandi * This registers a new user - Data is read directly from $_POST
43215fae107Sandi *
43315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
43415fae107Sandi *
43515fae107Sandi * @return bool  true on success, false on any error
436f3f0262cSandi */
437f3f0262cSandifunction register(){
438f3f0262cSandi  global $lang;
439eb5d07e4Sjan  global $conf;
440f3f0262cSandi
441f3f0262cSandi  if(!$_POST['save']) return false;
442640145a5Sandi
443f3f0262cSandi  //clean username
444f3f0262cSandi  $_POST['login'] = preg_replace('/.*:/','',$_POST['login']);
445f3f0262cSandi  $_POST['login'] = cleanID($_POST['login']);
446f3f0262cSandi  //clean fullname and email
447f3f0262cSandi  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
448f3f0262cSandi  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
449f3f0262cSandi
450f3f0262cSandi  if( empty($_POST['login']) ||
451f3f0262cSandi      empty($_POST['fullname']) ||
452f3f0262cSandi      empty($_POST['email']) ){
453f3f0262cSandi    msg($lang['regmissing'],-1);
454f3f0262cSandi    return false;
455f3f0262cSandi  }
456f3f0262cSandi
457cab2716aSmatthias.grimm  if ($conf['autopasswd']) {
458cab2716aSmatthias.grimm    $pass = auth_pwgen();                // automatically generate password
459cab2716aSmatthias.grimm  } elseif (empty($_POST['pass']) ||
460cab2716aSmatthias.grimm            empty($_POST['passchk'])) {
461bf12ec81Sjan    msg($lang['regmissing'], -1);        // complain about missing passwords
462cab2716aSmatthias.grimm    return false;
463cab2716aSmatthias.grimm  } elseif ($_POST['pass'] != $_POST['passchk']) {
464bf12ec81Sjan    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
465cab2716aSmatthias.grimm    return false;
466cab2716aSmatthias.grimm  } else {
467cab2716aSmatthias.grimm    $pass = $_POST['pass'];              // accept checked and valid password
468cab2716aSmatthias.grimm  }
469cab2716aSmatthias.grimm
470f3f0262cSandi  //check mail
47144f669e9Sandi  if(!mail_isvalid($_POST['email'])){
472f3f0262cSandi    msg($lang['regbadmail'],-1);
473f3f0262cSandi    return false;
474f3f0262cSandi  }
475f3f0262cSandi
476f3f0262cSandi  //okay try to create the user
4777c37db8aSmatthias.grimm  $pass = auth_createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email']);
478f3f0262cSandi  if(empty($pass)){
479f3f0262cSandi    msg($lang['reguexists'],-1);
480f3f0262cSandi    return false;
481f3f0262cSandi  }
482f3f0262cSandi
483cab2716aSmatthias.grimm  if (!$conf['autopasswd']) {
484cab2716aSmatthias.grimm    msg($lang['regsuccess2'],1);
485cab2716aSmatthias.grimm    return true;
486cab2716aSmatthias.grimm  }
487cab2716aSmatthias.grimm
488cab2716aSmatthias.grimm  // autogenerated password? then send him the password
489f3f0262cSandi  if (auth_sendPassword($_POST['login'],$pass)){
490f3f0262cSandi    msg($lang['regsuccess'],1);
491f3f0262cSandi    return true;
492f3f0262cSandi  }else{
493f3f0262cSandi    msg($lang['regmailfail'],-1);
494f3f0262cSandi    return false;
495f3f0262cSandi  }
496f3f0262cSandi}
497f3f0262cSandi
49810a76f6fSfrank/**
4998b06d178Schris * Update user profile
5008b06d178Schris *
5018b06d178Schris * @author    Christopher Smith <chris@jalakai.co.uk>
5028b06d178Schris */
5038b06d178Schrisfunction updateprofile() {
5048b06d178Schris  global $conf;
5058b06d178Schris  global $INFO;
5068b06d178Schris  global $lang;
5078b06d178Schris
5088b06d178Schris  if(!$_POST['save']) return false;
5098b06d178Schris
5108b06d178Schris  // should not be able to get here without modifyUser being possible...
5118b06d178Schris  if(!auth_canDo('modifyUser')) {
5128b06d178Schris    msg($lang['profna'],-1);
5138b06d178Schris    return false;
5148b06d178Schris  }
5158b06d178Schris
5168b06d178Schris  if ($_POST['newpass'] != $_POST['passchk']) {
5178b06d178Schris    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
5188b06d178Schris    return false;
5198b06d178Schris  }
5208b06d178Schris
5218b06d178Schris  //clean fullname and email
5228b06d178Schris  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
5238b06d178Schris  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
5248b06d178Schris
5258b06d178Schris  if (empty($_POST['fullname']) || empty($_POST['email'])) {
5268b06d178Schris    msg($lang['profnoempty'],-1);
5278b06d178Schris    return false;
5288b06d178Schris  }
5298b06d178Schris
5308b06d178Schris  if (!mail_isvalid($_POST['email'])){
5318b06d178Schris    msg($lang['regbadmail'],-1);
5328b06d178Schris    return false;
5338b06d178Schris  }
5348b06d178Schris
5358b06d178Schris  if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname'];
5368b06d178Schris  if ($_POST['email']    != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email'];
5378b06d178Schris  if (!empty($_POST['newpass']))  $changes['pass'] = $_POST['newpass'];
5388b06d178Schris
5398b06d178Schris  if (!count($changes)) {
5408b06d178Schris    msg($lang['profnochange'], -1);
5418b06d178Schris    return false;
5428b06d178Schris  }
5438b06d178Schris
5448b06d178Schris  if ($conf['profileconfirm']) {
5458b06d178Schris      if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) {
5468b06d178Schris      msg($lang['badlogin'],-1);
5478b06d178Schris      return false;
5488b06d178Schris    }
5498b06d178Schris  }
5508b06d178Schris
5518b06d178Schris  return auth_modifyUser($_SERVER['REMOTE_USER'], $changes);
5528b06d178Schris}
5538b06d178Schris
5548b06d178Schris/**
5558b06d178Schris * Send a  new password
5568b06d178Schris *
5578b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info>
5588b06d178Schris * @author Chris Smith <chris@jalakai.co.uk>
5598b06d178Schris *
5608b06d178Schris * @return bool true on success, false on any error
5618b06d178Schris*/
5628b06d178Schrisfunction act_resendpwd(){
5638b06d178Schris    global $lang;
5648b06d178Schris    global $conf;
5658b06d178Schris
5668b06d178Schris    if(!$_POST['save']) return false;
5678b06d178Schris
5688b06d178Schris    // should not be able to get here without modifyUser being possible...
5698b06d178Schris	if(!auth_canDo('modifyUser')) {
5708b06d178Schris      msg($lang['resendna'],-1);
5718b06d178Schris      return false;
5728b06d178Schris	}
5738b06d178Schris
5748b06d178Schris    if (empty($_POST['login'])) {
5758b06d178Schris      msg($lang['resendpwdmissing'], -1);
5768b06d178Schris      return false;
5778b06d178Schris    } else {
5788b06d178Schris      $user = $_POST['login'];
5798b06d178Schris    }
5808b06d178Schris
5818b06d178Schris    $userinfo = auth_getUserData($user);
5828b06d178Schris    if(!$userinfo['mail']) {
5838b06d178Schris      msg($lang['resendpwdnouser'], -1);
5848b06d178Schris      return false;
5858b06d178Schris    }
5868b06d178Schris
5878b06d178Schris    $pass = auth_pwgen();
5888b06d178Schris    if (!auth_modifyUser($user,array('pass' => $pass))) {
5898b06d178Schris      msg('error modifying user data',-1);
5908b06d178Schris      return false;
5918b06d178Schris    }
5928b06d178Schris
5938b06d178Schris    if (auth_sendPassword($user,$pass)) {
5948b06d178Schris      msg($lang['resendpwdsuccess'],1);
5958b06d178Schris    } else {
5968b06d178Schris      msg($lang['regmailfail'],-1);
5978b06d178Schris    }
5988b06d178Schris    return true;
5998b06d178Schris}
6008b06d178Schris
6018b06d178Schris/**
60210a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid
60310a76f6fSfrank *
60410a76f6fSfrank * May not be completly RFC conform!
60510a76f6fSfrank *
60610a76f6fSfrank * @link    http://www.webmasterworld.com/forum88/135.htm
60710a76f6fSfrank *
60810a76f6fSfrank * @param   string $email the address to check
60910a76f6fSfrank * @return  bool          true if address is valid
61010a76f6fSfrank */
61110a76f6fSfrankfunction isvalidemail($email){
61210a76f6fSfrank  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
61310a76f6fSfrank}
61410a76f6fSfrank
615b0855b11Sandi/**
616b0855b11Sandi * Encrypts a password using the given method and salt
617b0855b11Sandi *
618b0855b11Sandi * If the selected method needs a salt and none was given, a random one
619b0855b11Sandi * is chosen.
620b0855b11Sandi *
621b0855b11Sandi * The following methods are understood:
622b0855b11Sandi *
623b0855b11Sandi *   smd5  - Salted MD5 hashing
624b0855b11Sandi *   md5   - Simple MD5 hashing
625b0855b11Sandi *   sha1  - SHA1 hashing
626b0855b11Sandi *   ssha  - Salted SHA1 hashing
627d7be6245Sandi *   crypt - Unix crypt
628d7be6245Sandi *   mysql - MySQL password (old method)
629d7be6245Sandi *   my411 - MySQL 4.1.1 password
630b0855b11Sandi *
631b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
632b0855b11Sandi * @return  string  The crypted password
633b0855b11Sandi */
634b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){
635b0855b11Sandi  global $conf;
636b0855b11Sandi  if(empty($method)) $method = $conf['passcrypt'];
63710a76f6fSfrank
638b0855b11Sandi  //prepare a salt
639b0855b11Sandi  if(empty($salt)) $salt = md5(uniqid(rand(), true));
640b0855b11Sandi
641b0855b11Sandi  switch(strtolower($method)){
642b0855b11Sandi    case 'smd5':
643b0855b11Sandi        return crypt($clear,'$1$'.substr($salt,0,8).'$');
644b0855b11Sandi    case 'md5':
645b0855b11Sandi      return md5($clear);
646b0855b11Sandi    case 'sha1':
647b0855b11Sandi      return sha1($clear);
648b0855b11Sandi    case 'ssha':
649b0855b11Sandi      $salt=substr($salt,0,4);
650d6e54e02Smatthiasgrimm      return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
651b0855b11Sandi    case 'crypt':
652b0855b11Sandi      return crypt($clear,substr($salt,0,2));
653d7be6245Sandi    case 'mysql':
654d7be6245Sandi      //from http://www.php.net/mysql comment by <soren at byu dot edu>
655d7be6245Sandi      $nr=0x50305735;
656d7be6245Sandi      $nr2=0x12345671;
657d7be6245Sandi      $add=7;
658d7be6245Sandi      $charArr = preg_split("//", $clear);
659d7be6245Sandi      foreach ($charArr as $char) {
660d7be6245Sandi        if (($char == '') || ($char == ' ') || ($char == '\t')) continue;
661d7be6245Sandi        $charVal = ord($char);
662d7be6245Sandi        $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
663d7be6245Sandi        $nr2 += ($nr2 << 8) ^ $nr;
664d7be6245Sandi        $add += $charVal;
665d7be6245Sandi      }
666d7be6245Sandi      return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
667d7be6245Sandi    case 'my411':
668d7be6245Sandi      return '*'.sha1(pack("H*", sha1($clear)));
669b0855b11Sandi    default:
670b0855b11Sandi      msg("Unsupported crypt method $method",-1);
671b0855b11Sandi  }
672b0855b11Sandi}
673b0855b11Sandi
674b0855b11Sandi/**
675b0855b11Sandi * Verifies a cleartext password against a crypted hash
676b0855b11Sandi *
677b0855b11Sandi * The method and salt used for the crypted hash is determined automatically
678b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs
679b0855b11Sandi * match true is is returned else false
680b0855b11Sandi *
681b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
682b0855b11Sandi * @return  bool
683b0855b11Sandi */
684b0855b11Sandifunction auth_verifyPassword($clear,$crypt){
685b0855b11Sandi  $method='';
686b0855b11Sandi  $salt='';
687b0855b11Sandi
688b0855b11Sandi  //determine the used method and salt
689d7be6245Sandi  $len = strlen($crypt);
690b0855b11Sandi  if(substr($crypt,0,3) == '$1$'){
691b0855b11Sandi    $method = 'smd5';
692b0855b11Sandi    $salt   = substr($crypt,3,8);
693b0855b11Sandi  }elseif(substr($crypt,0,6) == '{SSHA}'){
694b0855b11Sandi    $method = 'ssha';
695b0855b11Sandi    $salt   = substr(base64_decode(substr($crypt, 6)),20);
696d7be6245Sandi  }elseif($len == 32){
697b0855b11Sandi    $method = 'md5';
698d7be6245Sandi  }elseif($len == 40){
699b0855b11Sandi    $method = 'sha1';
700d7be6245Sandi  }elseif($len == 16){
701d7be6245Sandi    $method = 'mysql';
702d7be6245Sandi  }elseif($len == 41 && $crypt[0] == '*'){
703d7be6245Sandi    $method = 'my411';
704b0855b11Sandi  }else{
705b0855b11Sandi    $method = 'crypt';
706b0855b11Sandi    $salt   = substr($crypt,0,2);
707b0855b11Sandi  }
708b0855b11Sandi
709b0855b11Sandi  //crypt and compare
710b0855b11Sandi  if(auth_cryptPassword($clear,$method,$salt) === $crypt){
711b0855b11Sandi    return true;
712b0855b11Sandi  }
713b0855b11Sandi  return false;
714b0855b11Sandi}
715340756e4Sandi
716340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
717