1<?php
2
3use dokuwiki\Extension\AuthPlugin;
4
5/**
6 * DokuWiki Plugin autham (Auth Component)
7 *
8 * @license GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
9 * @author Mr_Fang <klxf@vip.qq.com>
10 */
11class auth_plugin_autham extends AuthPlugin
12{
13    /** @inheritDoc */
14    public function __construct()
15    {
16        parent::__construct(); // for compatibility
17
18        // FIXME set capabilities accordingly
19        //$this->cando['addUser']     = false; // can Users be created?
20        //$this->cando['delUser']     = false; // can Users be deleted?
21        //$this->cando['modLogin']    = false; // can login names be changed?
22        //$this->cando['modPass']     = false; // can passwords be changed?
23        //$this->cando['modName']     = false; // can real names be changed?
24        //$this->cando['modMail']     = false; // can emails be changed?
25        //$this->cando['modGroups']   = false; // can groups be changed?
26        //$this->cando['getUsers']    = false; // can a (filtered) list of users be retrieved?
27        //$this->cando['getUserCount']= false; // can the number of users be retrieved?
28        //$this->cando['getGroups']   = false; // can a list of available groups be retrieved?
29        //$this->cando['external']    = false; // does the module do external auth checking?
30        //$this->cando['logout']      = true; // can the user logout again? (eg. not possible with HTTP auth)
31
32        // FIXME intialize your auth system and set success to true, if successful
33        $this->success = true;
34    }
35
36    /** @inheritDoc */
37    // public function logOff()
38    // {
39    // }
40
41    /** @inheritDoc */
42    //public function trustExternal($user, $pass, $sticky = false)
43    //{
44        /* some example:
45
46        global $USERINFO;
47        global $conf;
48        $sticky ? $sticky = true : $sticky = false; //sanity check
49
50        // do the checking here
51
52        // set the globals if authed
53        $USERINFO['name'] = 'FIXME';
54        $USERINFO['mail'] = 'FIXME';
55        $USERINFO['grps'] = array('FIXME');
56        $_SERVER['REMOTE_USER'] = $user;
57        $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
58        $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
59        $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
60        return true;
61
62        */
63    //}
64
65    /** @inheritDoc */
66    public function checkPass($user, $pass)
67    {
68        $sql_host = $this->getConf('sql_host');
69        $sql_user = $this->getConf('sql_user');
70        $sql_pass = $this->getConf('sql_pass');
71        $database = $this->getConf('database');
72        $table = $this->getConf('table');
73
74        $conn = new mysqli($sql_host, $sql_user, $sql_pass, $database);
75        if ($conn->connect_error) {
76            die("连接数据库失败: " . $conn->connect_error);
77        }
78        $user = strtolower(mysqli_real_escape_string($conn, $user));
79        $query = "SELECT * FROM $table WHERE username = '$user'";
80        $result = $conn->query($query);
81
82        if ($result->num_rows > 0) {
83            $row = $result->fetch_assoc();
84            $password = $row["password"];
85            $conn->close();
86
87            $password = explode("$", $password);
88            $salt = $password[2];
89            $pass_md5 = $password[3];
90            if($pass_md5 == hash("sha256", hash("sha256", $pass).$salt)) {
91                return true;
92            } else {
93                return false;
94            }
95        } else {
96            return false;
97        }
98
99
100        return false; // return true if okay
101    }
102
103    /** @inheritDoc */
104    public function getUserData($user, $requireGroups = true)
105    {
106        $sql_host = $this->getConf('sql_host');
107        $sql_user = $this->getConf('sql_user');
108        $sql_pass = $this->getConf('sql_pass');
109        $database = $this->getConf('database');
110        $table = $this->getConf('table');
111        $admin = $this->getConf('admin');
112
113        $conn = new mysqli($sql_host, $sql_user, $sql_pass, $database);
114        if ($conn->connect_error) {
115            die("连接数据库失败: " . $conn->connect_error);
116        }
117        $user = strtolower(mysqli_real_escape_string($conn, $user));
118        $query = "SELECT * FROM $table WHERE username = '$user'";
119        $result = $conn->query($query);
120
121        if ($result->num_rows > 0) {
122            $row = $result->fetch_assoc();
123            $realName = $row["realname"];
124            $email = $row["email"];
125            $conn->close();
126
127            $admin = explode(",", $admin);
128
129            if(in_array($realName, $admin)) {
130                $group = ['admin'];
131            } else {
132                $group = ['user'];
133            }
134
135            $userinfo = [
136                'name' => $realName,
137                'mail' => $email,
138                'grps' => $group
139            ];
140        } else {
141            return false;
142        }
143
144        return $userinfo;
145    }
146
147    /** @inheritDoc */
148    //public function createUser($user, $pass, $name, $mail, $grps = null)
149    //{
150        // FIXME implement
151    //    return null;
152    //}
153
154    /** @inheritDoc */
155    //public function modifyUser($user, $changes)
156    //{
157        // FIXME implement
158    //    return false;
159    //}
160
161    /** @inheritDoc */
162    //public function deleteUsers($users)
163    //{
164        // FIXME implement
165    //    return false;
166    //}
167
168    /** @inheritDoc */
169    //public function retrieveUsers($start = 0, $limit = 0, $filter = null)
170    //{
171        // FIXME implement
172    //    return array();
173    //}
174
175    /** @inheritDoc */
176    //public function getUserCount($filter = array())
177    //{
178        // FIXME implement
179    //    return 0;
180    //}
181
182    /** @inheritDoc */
183    //public function addGroup($group)
184    //{
185        // FIXME implement
186    //    return false;
187    //}
188
189    /** @inheritDoc */
190    //public function retrieveGroups($start = 0, $limit = 0)
191    //{
192        // FIXME implement
193    //    return array();
194    //}
195
196    /** @inheritDoc */
197    public function isCaseSensitive()
198    {
199        return true;
200    }
201
202    /** @inheritDoc */
203    public function cleanUser($user)
204    {
205        return $user;
206    }
207
208    /** @inheritDoc */
209    public function cleanGroup($group)
210    {
211        return $group;
212    }
213
214    /** @inheritDoc */
215    //public function useSessionCache($user)
216    //{
217      // FIXME implement
218    //}
219}
220