summaryrefslogtreecommitdiff
path: root/inc/auth_plain.php
blob: 4213b8dcc7df7afc921b2bc582faa064d10a1905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
 * Plaintext authentication backend
 *
 * If you want to authenticate against something
 * else then the builtin flatfile auth system
 * you have to reimplement the "required auth
 * functions"
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */


/**
 * Check user+password [required auth function]
 *
 * Checks if the given user exists and the given
 * plaintext password is correct
 *
 * @author  Andreas Gohr <andi@splitbrain.org>
 * @return  bool
 */
function auth_checkPass($user,$pass){
  $users = auth_plain_loadUserData();
  $pass = md5($pass); //encode pass

  if($users[$user]['pass'] == $pass){
    return true;
  }else{
    return false;
  }
}

/**
 * Return user info [required auth function]
 *
 * Returns info about the given user needs to contain
 * 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  Andreas Gohr <andi@splitbrain.org>
 */
function auth_getUserData($user){
  $users = auth_plain_loadUserData();
  return $users[$user];
}

/**
 * Create a new User [required auth function]
 *
 * Returns false if the user already exists, null when an error
 * occured and the cleartext password of the new user if
 * everything went well.
 * 
 * The new user HAS TO be added to the default group by this
 * function!
 *
 * @author  Andreas Gohr <andi@splitbrain.org>
 */
function auth_createUser($user,$name,$mail){
  global $conf;

  $users = auth_plain_loadUserData();
  if(isset($users[$user])) return false;

  $pass = auth_pwgen();
  $userline = join(':',array($user,
                             md5($pass),
                             $name,
                             $mail,
                             $conf['defaultgroup']));
  $userline .= "\n";
  $fh = fopen('conf/users.auth','a');
  if($fh){
    fwrite($fh,$userline);
    fclose($fh);
    return $pass;
  }
  msg('The users.auth file is not writable. Please inform the Wiki-Admin',-1);
  return null;
}

/**
 * Load all user data
 *
 * Used by the plaintext auth functions
 * loads the user file into a datastructure
 *
 * @author  Andreas Gohr <andi@splitbrain.org>
 */
function auth_plain_loadUserData(){
  $data = array();
  $lines = file('conf/users.auth');
  foreach($lines as $line){
    $line = preg_replace('/#.*$/','',$line); //ignore comments
    $line = trim($line);
    if(empty($line)) continue;

    $row    = split(":",$line,5);
    $groups = split(",",$row[4]);
    $data[$row[0]]['pass'] = $row[1];
    $data[$row[0]]['name'] = urldecode($row[2]);
    $data[$row[0]]['mail'] = $row[3];
    $data[$row[0]]['grps'] = $groups;
  }
  return $data;
}

?>