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']; 24cd52f92dSchris if (class_exists($auth_class)) { 258b06d178Schris $auth = new $auth_class(); 26d2dde4ebSMatthias Grimm if ($auth->success == false) { 27d2dde4ebSMatthias Grimm unset($auth); 28cd52f92dSchris msg($lang['authtempfail'], -1); 29cd52f92dSchris 30cd52f92dSchris // turn acl config setting off for the rest of this page 31cd52f92dSchris $conf['useacl'] = 0; 32d2dde4ebSMatthias Grimm } 338b06d178Schris } else { 34cd52f92dSchris die($lang['authmodfailed']); 35cd52f92dSchris } 36cd52f92dSchris } else { 37cd52f92dSchris die($lang['authmodfailed']); 388b06d178Schris } 39f3f0262cSandi 401e866646Sandi if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5($conf['title'])); 41e65afed4SSameer D. Sahasrabuddhe 4215fae107Sandi // some ACL level defines 43f3f0262cSandi define('AUTH_NONE',0); 44f3f0262cSandi define('AUTH_READ',1); 45f3f0262cSandi define('AUTH_EDIT',2); 46f3f0262cSandi define('AUTH_CREATE',4); 47f3f0262cSandi define('AUTH_UPLOAD',8); 488ef6b7caSandi define('AUTH_DELETE',16); 4910a76f6fSfrank define('AUTH_ADMIN',255); 50f3f0262cSandi 51f5cb575dSAndreas Gohr // do the login either by cookie or provided credentials 52f3f0262cSandi if($conf['useacl']){ 53f5cb575dSAndreas Gohr // external trust mechanism in place? 5482fd59b6SAndreas Gohr if(!is_null($auth) && $auth->canDo('external')){ 55f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 56f5cb575dSAndreas Gohr }else{ 57132bdbfeSandi auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 58f5cb575dSAndreas Gohr } 59f5cb575dSAndreas Gohr 6015fae107Sandi //load ACL into a global array 61e7cb32dcSAndreas Gohr if(is_readable(DOKU_CONF.'acl.auth.php')){ 62e7cb32dcSAndreas Gohr $AUTH_ACL = file(DOKU_CONF.'acl.auth.php'); 6311799630Sandi }else{ 6411799630Sandi $AUTH_ACL = array(); 6511799630Sandi } 66f3f0262cSandi } 67f3f0262cSandi 68f3f0262cSandi/** 69f3f0262cSandi * This tries to login the user based on the sent auth credentials 70f3f0262cSandi * 71f3f0262cSandi * The authentication works like this: if a username was given 7215fae107Sandi * a new login is assumed and user/password are checked. If they 7315fae107Sandi * are correct the password is encrypted with blowfish and stored 7415fae107Sandi * together with the username in a cookie - the same info is stored 7515fae107Sandi * in the session, too. Additonally a browserID is stored in the 7615fae107Sandi * session. 7715fae107Sandi * 7815fae107Sandi * If no username was given the cookie is checked: if the username, 7915fae107Sandi * crypted password and browserID match between session and cookie 8015fae107Sandi * no further testing is done and the user is accepted 8115fae107Sandi * 8215fae107Sandi * If a cookie was found but no session info was availabe the 83136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 8415fae107Sandi * together with username rechecked by calling this function again. 85f3f0262cSandi * 86f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 87f3f0262cSandi * are set. 8815fae107Sandi * 8915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 9015fae107Sandi * 9115fae107Sandi * @param string $user Username 9215fae107Sandi * @param string $pass Cleartext Password 9315fae107Sandi * @param bool $sticky Cookie should not expire 9415fae107Sandi * @return bool true on successful auth 95f3f0262cSandi*/ 96132bdbfeSandifunction auth_login($user,$pass,$sticky=false){ 97f3f0262cSandi global $USERINFO; 98f3f0262cSandi global $conf; 99f3f0262cSandi global $lang; 100cd52f92dSchris global $auth; 101132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 102f3f0262cSandi 103f3f0262cSandi if(isset($user)){ 104132bdbfeSandi //usual login 105cd52f92dSchris if ($auth->checkPass($user,$pass)){ 106132bdbfeSandi // make logininfo globally available 107f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 108cd52f92dSchris $USERINFO = $auth->getUserData($user); //FIXME move all references to session 109132bdbfeSandi 110132bdbfeSandi // set cookie 111132bdbfeSandi $pass = PMA_blowfish_encrypt($pass,auth_cookiesalt()); 112132bdbfeSandi $cookie = base64_encode("$user|$sticky|$pass"); 113132bdbfeSandi if($sticky) $time = time()+60*60*24*365; //one year 114e65afed4SSameer D. Sahasrabuddhe setcookie(DOKU_COOKIE,$cookie,$time,'/'); 115132bdbfeSandi 116132bdbfeSandi // set session 117132bdbfeSandi $_SESSION[$conf['title']]['auth']['user'] = $user; 118132bdbfeSandi $_SESSION[$conf['title']]['auth']['pass'] = $pass; 119132bdbfeSandi $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid(); 120132bdbfeSandi $_SESSION[$conf['title']]['auth']['info'] = $USERINFO; 121132bdbfeSandi return true; 122f3f0262cSandi }else{ 123f3f0262cSandi //invalid credentials - log off 124f3f0262cSandi msg($lang['badlogin'],-1); 125f3f0262cSandi auth_logoff(); 126132bdbfeSandi return false; 127f3f0262cSandi } 128f3f0262cSandi }else{ 129132bdbfeSandi // read cookie information 130e65afed4SSameer D. Sahasrabuddhe $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 131132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 132132bdbfeSandi // get session info 133132bdbfeSandi $session = $_SESSION[$conf['title']]['auth']; 134132bdbfeSandi 135132bdbfeSandi if($user && $pass){ 136132bdbfeSandi // we got a cookie - see if we can trust it 137132bdbfeSandi if(isset($session) && 138132bdbfeSandi ($session['user'] == $user) && 139132bdbfeSandi ($session['pass'] == $pass) && //still crypted 140132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 141132bdbfeSandi // he has session, cookie and browser right - let him in 142132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 143132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 144132bdbfeSandi return true; 145132bdbfeSandi } 146132bdbfeSandi // no we don't trust it yet - recheck pass 147132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 148132bdbfeSandi return auth_login($user,$pass,$sticky); 149132bdbfeSandi } 150132bdbfeSandi } 151f3f0262cSandi //just to be sure 152f3f0262cSandi auth_logoff(); 153132bdbfeSandi return false; 154f3f0262cSandi} 155132bdbfeSandi 156132bdbfeSandi/** 157136ce040Sandi * Builds a pseudo UID from browser and IP data 158132bdbfeSandi * 159132bdbfeSandi * This is neither unique nor unfakable - still it adds some 160136ce040Sandi * security. Using the first part of the IP makes sure 161136ce040Sandi * proxy farms like AOLs are stil okay. 16215fae107Sandi * 16315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 16415fae107Sandi * 16515fae107Sandi * @return string a MD5 sum of various browser headers 166132bdbfeSandi */ 167132bdbfeSandifunction auth_browseruid(){ 168132bdbfeSandi $uid = ''; 169132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 170132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 171132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 172132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 173136ce040Sandi $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.')); 174132bdbfeSandi return md5($uid); 175132bdbfeSandi} 176132bdbfeSandi 177132bdbfeSandi/** 178132bdbfeSandi * Creates a random key to encrypt the password in cookies 17915fae107Sandi * 18015fae107Sandi * This function tries to read the password for encrypting 18198407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 18215fae107Sandi * if no such file is found a random key is created and 18315fae107Sandi * and stored in this file. 18415fae107Sandi * 18515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 18615fae107Sandi * 18715fae107Sandi * @return string 188132bdbfeSandi */ 189132bdbfeSandifunction auth_cookiesalt(){ 190132bdbfeSandi global $conf; 19198407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 192132bdbfeSandi $salt = io_readFile($file); 193132bdbfeSandi if(empty($salt)){ 194132bdbfeSandi $salt = uniqid(rand(),true); 195132bdbfeSandi io_saveFile($file,$salt); 196132bdbfeSandi } 197132bdbfeSandi return $salt; 198f3f0262cSandi} 199f3f0262cSandi 200f3f0262cSandi/** 201f3f0262cSandi * This clears all authenticationdata and thus log the user 202f3f0262cSandi * off 20315fae107Sandi * 20415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 205f3f0262cSandi */ 206f3f0262cSandifunction auth_logoff(){ 207f3f0262cSandi global $conf; 208f3f0262cSandi global $USERINFO; 2098b06d178Schris global $INFO, $ID; 2105298a619SAndreas Gohr global $auth; 21137065e65Sandi 21237065e65Sandi if(isset($_SESSION[$conf['title']]['auth']['user'])) 213132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['user']); 21437065e65Sandi if(isset($_SESSION[$conf['title']]['auth']['pass'])) 215132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['pass']); 21637065e65Sandi if(isset($_SESSION[$conf['title']]['auth']['info'])) 217132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['info']); 21837065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 219f3f0262cSandi unset($_SERVER['REMOTE_USER']); 220132bdbfeSandi $USERINFO=null; //FIXME 2211e866646Sandi setcookie(DOKU_COOKIE,'',time()-600000,'/'); 2225298a619SAndreas Gohr 2235298a619SAndreas Gohr if($auth && $auth->canDo('logoff')){ 2245298a619SAndreas Gohr $auth->logOff(); 2255298a619SAndreas Gohr } 226f3f0262cSandi} 227f3f0262cSandi 228f3f0262cSandi/** 22915fae107Sandi * Convinience function for auth_aclcheck() 23015fae107Sandi * 23115fae107Sandi * This checks the permissions for the current user 23215fae107Sandi * 23315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 23415fae107Sandi * 23515fae107Sandi * @param string $id page ID 23615fae107Sandi * @return int permission level 237f3f0262cSandi */ 238f3f0262cSandifunction auth_quickaclcheck($id){ 239f3f0262cSandi global $conf; 240f3f0262cSandi global $USERINFO; 241f3f0262cSandi # if no ACL is used always return upload rights 242f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 243f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 244f3f0262cSandi} 245f3f0262cSandi 246f3f0262cSandi/** 247f3f0262cSandi * Returns the maximum rights a user has for 248f3f0262cSandi * the given ID or its namespace 24915fae107Sandi * 25015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 25115fae107Sandi * 25215fae107Sandi * @param string $id page ID 25315fae107Sandi * @param string $user Username 25415fae107Sandi * @param array $groups Array of groups the user is in 25515fae107Sandi * @return int permission level 256f3f0262cSandi */ 257f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 258f3f0262cSandi global $conf; 259f3f0262cSandi global $AUTH_ACL; 260f3f0262cSandi 261f3f0262cSandi # if no ACL is used always return upload rights 262f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 263f3f0262cSandi 26410a76f6fSfrank //if user is superuser return 255 (acl_admin) 26510a76f6fSfrank if($conf['superuser'] == $user) { return AUTH_ADMIN; } 26610a76f6fSfrank 267074cf26bSandi //make sure groups is an array 268074cf26bSandi if(!is_array($groups)) $groups = array(); 269074cf26bSandi 27010a76f6fSfrank //prepend groups with @ 2712cd2db38Sandi $cnt = count($groups); 2722cd2db38Sandi for($i=0; $i<$cnt; $i++){ 27310a76f6fSfrank $groups[$i] = '@'.$groups[$i]; 27410a76f6fSfrank } 27510a76f6fSfrank //if user is in superuser group return 255 (acl_admin) 27610a76f6fSfrank if(in_array($conf['superuser'], $groups)) { return AUTH_ADMIN; } 27710a76f6fSfrank 278f3f0262cSandi $ns = getNS($id); 279f3f0262cSandi $perm = -1; 280f3f0262cSandi 281f3f0262cSandi if($user){ 282f3f0262cSandi //add ALL group 283f3f0262cSandi $groups[] = '@ALL'; 284f3f0262cSandi //add User 285f3f0262cSandi $groups[] = $user; 286f3f0262cSandi //build regexp 287f3f0262cSandi $regexp = join('|',$groups); 288f3f0262cSandi }else{ 289f3f0262cSandi $regexp = '@ALL'; 290f3f0262cSandi } 291f3f0262cSandi 292f3f0262cSandi //check exact match first 29342905504SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL); 294f3f0262cSandi if(count($matches)){ 295f3f0262cSandi foreach($matches as $match){ 296f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 297f3f0262cSandi $acl = preg_split('/\s+/',$match); 2988ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 299f3f0262cSandi if($acl[2] > $perm){ 300f3f0262cSandi $perm = $acl[2]; 301f3f0262cSandi } 302f3f0262cSandi } 303f3f0262cSandi if($perm > -1){ 304f3f0262cSandi //we had a match - return it 305f3f0262cSandi return $perm; 306f3f0262cSandi } 307f3f0262cSandi } 308f3f0262cSandi 309f3f0262cSandi //still here? do the namespace checks 310f3f0262cSandi if($ns){ 311f3f0262cSandi $path = $ns.':\*'; 312f3f0262cSandi }else{ 313f3f0262cSandi $path = '\*'; //root document 314f3f0262cSandi } 315f3f0262cSandi 316f3f0262cSandi do{ 317f3f0262cSandi $matches = preg_grep('/^'.$path.'\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 //we had a match - return it 328f3f0262cSandi return $perm; 329f3f0262cSandi } 330f3f0262cSandi 331f3f0262cSandi //get next higher namespace 332f3f0262cSandi $ns = getNS($ns); 333f3f0262cSandi 334f3f0262cSandi if($path != '\*'){ 335f3f0262cSandi $path = $ns.':\*'; 336f3f0262cSandi if($path == ':\*') $path = '\*'; 337f3f0262cSandi }else{ 338f3f0262cSandi //we did this already 339f3f0262cSandi //looks like there is something wrong with the ACL 340f3f0262cSandi //break here 341d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 342d5ce66f6SAndreas Gohr return AUTH_NONE; 343f3f0262cSandi } 344f3f0262cSandi }while(1); //this should never loop endless 34552a5af8dSandi 34652a5af8dSandi //still here? return no permissions 34752a5af8dSandi return AUTH_NONE; 348f3f0262cSandi} 349f3f0262cSandi 350f3f0262cSandi/** 351f3f0262cSandi * Create a pronouncable password 352f3f0262cSandi * 35315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 35415fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 35515fae107Sandi * 35615fae107Sandi * @return string pronouncable password 357f3f0262cSandi */ 358f3f0262cSandifunction auth_pwgen(){ 359f3f0262cSandi $pw = ''; 360f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 361f3f0262cSandi $v = 'aeiou'; //vowels 362f3f0262cSandi $a = $c.$v; //both 363f3f0262cSandi 364f3f0262cSandi //use two syllables... 365f3f0262cSandi for($i=0;$i < 2; $i++){ 366f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 367f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 368f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 369f3f0262cSandi } 370f3f0262cSandi //... and add a nice number 371f3f0262cSandi $pw .= rand(10,99); 372f3f0262cSandi 373f3f0262cSandi return $pw; 374f3f0262cSandi} 375f3f0262cSandi 376f3f0262cSandi/** 377f3f0262cSandi * Sends a password to the given user 378f3f0262cSandi * 37915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 38015fae107Sandi * 38115fae107Sandi * @return bool true on success 382f3f0262cSandi */ 383f3f0262cSandifunction auth_sendPassword($user,$password){ 384f3f0262cSandi global $conf; 385f3f0262cSandi global $lang; 386cd52f92dSchris global $auth; 387cd52f92dSchris 388f3f0262cSandi $hdrs = ''; 389cd52f92dSchris $userinfo = $auth->getUserData($user); 390f3f0262cSandi 39187ddda95Sandi if(!$userinfo['mail']) return false; 392f3f0262cSandi 393f3f0262cSandi $text = rawLocale('password'); 394ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 39587ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 396f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 397f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 398f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 399f3f0262cSandi 40044f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 40144f669e9Sandi $lang['regpwmail'], 40244f669e9Sandi $text, 40344f669e9Sandi $conf['mailfrom']); 404f3f0262cSandi} 405f3f0262cSandi 406f3f0262cSandi/** 40715fae107Sandi * Register a new user 408f3f0262cSandi * 40915fae107Sandi * This registers a new user - Data is read directly from $_POST 41015fae107Sandi * 41115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 41215fae107Sandi * 41315fae107Sandi * @return bool true on success, false on any error 414f3f0262cSandi */ 415f3f0262cSandifunction register(){ 416f3f0262cSandi global $lang; 417eb5d07e4Sjan global $conf; 418cd52f92dSchris global $auth; 419f3f0262cSandi 420f3f0262cSandi if(!$_POST['save']) return false; 42182fd59b6SAndreas Gohr if(!$auth->canDo('addUser')) return false; 422640145a5Sandi 423f3f0262cSandi //clean username 424f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 425f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 426f3f0262cSandi //clean fullname and email 427f3f0262cSandi $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname'])); 428f3f0262cSandi $_POST['email'] = trim(str_replace(':','',$_POST['email'])); 429f3f0262cSandi 430f3f0262cSandi if( empty($_POST['login']) || 431f3f0262cSandi empty($_POST['fullname']) || 432f3f0262cSandi empty($_POST['email']) ){ 433f3f0262cSandi msg($lang['regmissing'],-1); 434f3f0262cSandi return false; 435f3f0262cSandi } 436f3f0262cSandi 437cab2716aSmatthias.grimm if ($conf['autopasswd']) { 438cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 439cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 440cab2716aSmatthias.grimm empty($_POST['passchk'])) { 441bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 442cab2716aSmatthias.grimm return false; 443cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 444bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 445cab2716aSmatthias.grimm return false; 446cab2716aSmatthias.grimm } else { 447cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 448cab2716aSmatthias.grimm } 449cab2716aSmatthias.grimm 450f3f0262cSandi //check mail 45144f669e9Sandi if(!mail_isvalid($_POST['email'])){ 452f3f0262cSandi msg($lang['regbadmail'],-1); 453f3f0262cSandi return false; 454f3f0262cSandi } 455f3f0262cSandi 456f3f0262cSandi //okay try to create the user 457*1d096a10SAndreas Gohr if(!$auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email'])){ 458f3f0262cSandi msg($lang['reguexists'],-1); 459f3f0262cSandi return false; 460f3f0262cSandi } 461f3f0262cSandi 462cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 463cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 464cab2716aSmatthias.grimm return true; 465cab2716aSmatthias.grimm } 466cab2716aSmatthias.grimm 467cab2716aSmatthias.grimm // autogenerated password? then send him the password 468f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 469f3f0262cSandi msg($lang['regsuccess'],1); 470f3f0262cSandi return true; 471f3f0262cSandi }else{ 472f3f0262cSandi msg($lang['regmailfail'],-1); 473f3f0262cSandi return false; 474f3f0262cSandi } 475f3f0262cSandi} 476f3f0262cSandi 47710a76f6fSfrank/** 4788b06d178Schris * Update user profile 4798b06d178Schris * 4808b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 4818b06d178Schris */ 4828b06d178Schrisfunction updateprofile() { 4838b06d178Schris global $conf; 4848b06d178Schris global $INFO; 4858b06d178Schris global $lang; 486cd52f92dSchris global $auth; 4878b06d178Schris 4888b06d178Schris if(!$_POST['save']) return false; 4898b06d178Schris 49082fd59b6SAndreas Gohr // should not be able to get here without Profile being possible... 49182fd59b6SAndreas Gohr if(!$auth->canDo('Profile')) { 4928b06d178Schris msg($lang['profna'],-1); 4938b06d178Schris return false; 4948b06d178Schris } 4958b06d178Schris 4968b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 4978b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 4988b06d178Schris return false; 4998b06d178Schris } 5008b06d178Schris 5018b06d178Schris //clean fullname and email 5028b06d178Schris $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname'])); 5038b06d178Schris $_POST['email'] = trim(str_replace(':','',$_POST['email'])); 5048b06d178Schris 5058b06d178Schris if (empty($_POST['fullname']) || empty($_POST['email'])) { 5068b06d178Schris msg($lang['profnoempty'],-1); 5078b06d178Schris return false; 5088b06d178Schris } 5098b06d178Schris 5108b06d178Schris if (!mail_isvalid($_POST['email'])){ 5118b06d178Schris msg($lang['regbadmail'],-1); 5128b06d178Schris return false; 5138b06d178Schris } 5148b06d178Schris 5158b06d178Schris if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname']; 5168b06d178Schris if ($_POST['email'] != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email']; 5178b06d178Schris if (!empty($_POST['newpass'])) $changes['pass'] = $_POST['newpass']; 5188b06d178Schris 5198b06d178Schris if (!count($changes)) { 5208b06d178Schris msg($lang['profnochange'], -1); 5218b06d178Schris return false; 5228b06d178Schris } 5238b06d178Schris 5248b06d178Schris if ($conf['profileconfirm']) { 5258b06d178Schris if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) { 5268b06d178Schris msg($lang['badlogin'],-1); 5278b06d178Schris return false; 5288b06d178Schris } 5298b06d178Schris } 5308b06d178Schris 531cd52f92dSchris return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes); 5328b06d178Schris} 5338b06d178Schris 5348b06d178Schris/** 5358b06d178Schris * Send a new password 5368b06d178Schris * 5378b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 5388b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 5398b06d178Schris * 5408b06d178Schris * @return bool true on success, false on any error 5418b06d178Schris*/ 5428b06d178Schrisfunction act_resendpwd(){ 5438b06d178Schris global $lang; 5448b06d178Schris global $conf; 545cd52f92dSchris global $auth; 5468b06d178Schris 5478b06d178Schris if(!$_POST['save']) return false; 5488fd2f03aSAndreas Gohr if(!$conf['resendpasswd']) return false; 5498b06d178Schris 55082fd59b6SAndreas Gohr // should not be able to get here without modPass being possible... 55182fd59b6SAndreas Gohr if(!$auth->canDo('modPass')) { 5528b06d178Schris msg($lang['resendna'],-1); 5538b06d178Schris return false; 5548b06d178Schris } 5558b06d178Schris 5568b06d178Schris if (empty($_POST['login'])) { 5578b06d178Schris msg($lang['resendpwdmissing'], -1); 5588b06d178Schris return false; 5598b06d178Schris } else { 5608b06d178Schris $user = $_POST['login']; 5618b06d178Schris } 5628b06d178Schris 563cd52f92dSchris $userinfo = $auth->getUserData($user); 5648b06d178Schris if(!$userinfo['mail']) { 5658b06d178Schris msg($lang['resendpwdnouser'], -1); 5668b06d178Schris return false; 5678b06d178Schris } 5688b06d178Schris 5698b06d178Schris $pass = auth_pwgen(); 570cd52f92dSchris if (!$auth->modifyUser($user,array('pass' => $pass))) { 5718b06d178Schris msg('error modifying user data',-1); 5728b06d178Schris return false; 5738b06d178Schris } 5748b06d178Schris 5758b06d178Schris if (auth_sendPassword($user,$pass)) { 5768b06d178Schris msg($lang['resendpwdsuccess'],1); 5778b06d178Schris } else { 5788b06d178Schris msg($lang['regmailfail'],-1); 5798b06d178Schris } 5808b06d178Schris return true; 5818b06d178Schris} 5828b06d178Schris 5838b06d178Schris/** 58410a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid 58510a76f6fSfrank * 58610a76f6fSfrank * May not be completly RFC conform! 58710a76f6fSfrank * 58810a76f6fSfrank * @link http://www.webmasterworld.com/forum88/135.htm 58910a76f6fSfrank * 59010a76f6fSfrank * @param string $email the address to check 59110a76f6fSfrank * @return bool true if address is valid 59210a76f6fSfrank */ 59310a76f6fSfrankfunction isvalidemail($email){ 59410a76f6fSfrank return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 59510a76f6fSfrank} 59610a76f6fSfrank 597b0855b11Sandi/** 598b0855b11Sandi * Encrypts a password using the given method and salt 599b0855b11Sandi * 600b0855b11Sandi * If the selected method needs a salt and none was given, a random one 601b0855b11Sandi * is chosen. 602b0855b11Sandi * 603b0855b11Sandi * The following methods are understood: 604b0855b11Sandi * 605b0855b11Sandi * smd5 - Salted MD5 hashing 606b0855b11Sandi * md5 - Simple MD5 hashing 607b0855b11Sandi * sha1 - SHA1 hashing 608b0855b11Sandi * ssha - Salted SHA1 hashing 609d7be6245Sandi * crypt - Unix crypt 610d7be6245Sandi * mysql - MySQL password (old method) 611d7be6245Sandi * my411 - MySQL 4.1.1 password 612b0855b11Sandi * 613b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 614b0855b11Sandi * @return string The crypted password 615b0855b11Sandi */ 616b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){ 617b0855b11Sandi global $conf; 618b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 61910a76f6fSfrank 620b0855b11Sandi //prepare a salt 621b0855b11Sandi if(empty($salt)) $salt = md5(uniqid(rand(), true)); 622b0855b11Sandi 623b0855b11Sandi switch(strtolower($method)){ 624b0855b11Sandi case 'smd5': 625b0855b11Sandi return crypt($clear,'$1$'.substr($salt,0,8).'$'); 626b0855b11Sandi case 'md5': 627b0855b11Sandi return md5($clear); 628b0855b11Sandi case 'sha1': 629b0855b11Sandi return sha1($clear); 630b0855b11Sandi case 'ssha': 631b0855b11Sandi $salt=substr($salt,0,4); 632d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 633b0855b11Sandi case 'crypt': 634b0855b11Sandi return crypt($clear,substr($salt,0,2)); 635d7be6245Sandi case 'mysql': 636d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 637d7be6245Sandi $nr=0x50305735; 638d7be6245Sandi $nr2=0x12345671; 639d7be6245Sandi $add=7; 640d7be6245Sandi $charArr = preg_split("//", $clear); 641d7be6245Sandi foreach ($charArr as $char) { 642d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 643d7be6245Sandi $charVal = ord($char); 644d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 645d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 646d7be6245Sandi $add += $charVal; 647d7be6245Sandi } 648d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 649d7be6245Sandi case 'my411': 650d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 651b0855b11Sandi default: 652b0855b11Sandi msg("Unsupported crypt method $method",-1); 653b0855b11Sandi } 654b0855b11Sandi} 655b0855b11Sandi 656b0855b11Sandi/** 657b0855b11Sandi * Verifies a cleartext password against a crypted hash 658b0855b11Sandi * 659b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 660b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 661b0855b11Sandi * match true is is returned else false 662b0855b11Sandi * 663b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 664b0855b11Sandi * @return bool 665b0855b11Sandi */ 666b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 667b0855b11Sandi $method=''; 668b0855b11Sandi $salt=''; 669b0855b11Sandi 670b0855b11Sandi //determine the used method and salt 671d7be6245Sandi $len = strlen($crypt); 672b0855b11Sandi if(substr($crypt,0,3) == '$1$'){ 673b0855b11Sandi $method = 'smd5'; 674b0855b11Sandi $salt = substr($crypt,3,8); 675b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 676b0855b11Sandi $method = 'ssha'; 677b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 678d7be6245Sandi }elseif($len == 32){ 679b0855b11Sandi $method = 'md5'; 680d7be6245Sandi }elseif($len == 40){ 681b0855b11Sandi $method = 'sha1'; 682d7be6245Sandi }elseif($len == 16){ 683d7be6245Sandi $method = 'mysql'; 684d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 685d7be6245Sandi $method = 'my411'; 686b0855b11Sandi }else{ 687b0855b11Sandi $method = 'crypt'; 688b0855b11Sandi $salt = substr($crypt,0,2); 689b0855b11Sandi } 690b0855b11Sandi 691b0855b11Sandi //crypt and compare 692b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 693b0855b11Sandi return true; 694b0855b11Sandi } 695b0855b11Sandi return false; 696b0855b11Sandi} 697340756e4Sandi 698340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 699