xref: /plugin/oauth/auth.php (revision d313403c1ae2bd9fb3dd1e1d64a2d337d8c7c83e)
180852c15SAndreas Gohr<?php
280852c15SAndreas Gohr/**
380852c15SAndreas Gohr * DokuWiki Plugin oauth (Auth Component)
480852c15SAndreas Gohr *
580852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
680852c15SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
780852c15SAndreas Gohr */
880852c15SAndreas Gohr
980852c15SAndreas Gohr// must be run within Dokuwiki
1080852c15SAndreas Gohrif(!defined('DOKU_INC')) die();
1180852c15SAndreas Gohr
12f10e09e2SAndreas Gohrclass auth_plugin_oauth extends auth_plugin_authplain {
1380852c15SAndreas Gohr
14f866280eSAndreas Gohr    /**
15f866280eSAndreas Gohr     * Constructor
16f866280eSAndreas Gohr     *
17f866280eSAndreas Gohr     * Sets capabilities.
18f866280eSAndreas Gohr     */
1980852c15SAndreas Gohr    public function __construct() {
20f10e09e2SAndreas Gohr        parent::__construct();
2180852c15SAndreas Gohr
22f10e09e2SAndreas Gohr        $this->cando['external'] = true;
2380852c15SAndreas Gohr    }
2480852c15SAndreas Gohr
25438dcc52SMichael Grosse    private function handleState($state) {
26438dcc52SMichael Grosse        /** @var \helper_plugin_farmer $farmer */
27438dcc52SMichael Grosse        $farmer = plugin_load('helper', 'farmer', false, true);
28438dcc52SMichael Grosse        $data = json_decode(base64_decode(urldecode($state)));
29438dcc52SMichael Grosse        if (empty($data->animal) || $farmer->getAnimal() == $data->animal) {
30438dcc52SMichael Grosse            return;
31438dcc52SMichael Grosse        }
32438dcc52SMichael Grosse        $animal = $data->animal;
33438dcc52SMichael Grosse        $allAnimals = $farmer->getAllAnimals();
34438dcc52SMichael Grosse        if (!in_array($animal, $allAnimals)) {
35438dcc52SMichael Grosse            msg('Animal ' . $animal . ' does not exist!');
36438dcc52SMichael Grosse            return;
37438dcc52SMichael Grosse        }
38438dcc52SMichael Grosse        global $INPUT;
39518d0e6eSMichael Große        $url = $farmer->getAnimalURL($animal) . '/doku.php?' . $INPUT->server->str('QUERY_STRING');
40438dcc52SMichael Grosse        send_redirect($url);
41438dcc52SMichael Grosse    }
42438dcc52SMichael Grosse
43f866280eSAndreas Gohr    /**
44f866280eSAndreas Gohr     * Handle the login
45f866280eSAndreas Gohr     *
46f866280eSAndreas Gohr     * This either trusts the session data (if any), processes the second oAuth step or simply
47f866280eSAndreas Gohr     * executes a normal plugin against local users.
48f866280eSAndreas Gohr     *
49f866280eSAndreas Gohr     * @param string $user
50f866280eSAndreas Gohr     * @param string $pass
51f866280eSAndreas Gohr     * @param bool   $sticky
52f866280eSAndreas Gohr     * @return bool
53f866280eSAndreas Gohr     */
54f10e09e2SAndreas Gohr    function trustExternal($user, $pass, $sticky = false) {
55438dcc52SMichael Grosse        global $USERINFO, $INPUT;
56438dcc52SMichael Grosse
57438dcc52SMichael Grosse        if ($INPUT->has('state') && plugin_load('helper', 'farmer', false, true)) {
58438dcc52SMichael Grosse            $this->handleState($INPUT->str('state'));
59438dcc52SMichael Grosse        }
6080852c15SAndreas Gohr
61a7a8f46aSAndreas Gohr        // check session for existing oAuth login data
62a7a8f46aSAndreas Gohr        $session = $_SESSION[DOKU_COOKIE]['auth'];
63523e6571SMichael Große        if(isset($session['oauth'])) {
64a7a8f46aSAndreas Gohr            $servicename = $session['oauth'];
65a7a8f46aSAndreas Gohr            // check if session data is still considered valid
66f2e164b0SMichael Große            if ($this->isSessionValid($session)) {
67a7a8f46aSAndreas Gohr                $_SERVER['REMOTE_USER'] = $session['user'];
68a7a8f46aSAndreas Gohr                $USERINFO               = $session['info'];
6980852c15SAndreas Gohr                return true;
70f10e09e2SAndreas Gohr            }
7180852c15SAndreas Gohr        }
7280852c15SAndreas Gohr
73523e6571SMichael Große        $existingLoginProcess = false;
74523e6571SMichael Große        // are we in login progress?
75523e6571SMichael Große        if(isset($_SESSION[DOKU_COOKIE]['oauth-inprogress'])) {
76523e6571SMichael Große            $servicename = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'];
77523e6571SMichael Große            $page        = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['id'];
78188ba446SMichael Große            $params      = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['params'];
79523e6571SMichael Große
80523e6571SMichael Große            unset($_SESSION[DOKU_COOKIE]['oauth-inprogress']);
81523e6571SMichael Große            $existingLoginProcess = true;
82523e6571SMichael Große        }
83523e6571SMichael Große
84a7a8f46aSAndreas Gohr        // either we're in oauth login or a previous log needs to be rechecked
852e94f0b8SAndreas Gohr        if(isset($servicename)) {
86a7a8f46aSAndreas Gohr            /** @var helper_plugin_oauth $hlp */
87a7a8f46aSAndreas Gohr            $hlp     = plugin_load('helper', 'oauth');
88827232fcSMichael Große
89827232fcSMichael Große            /** @var OAuth\Plugin\AbstractAdapter $service */
90a7a8f46aSAndreas Gohr            $service = $hlp->loadService($servicename);
91523e6571SMichael Große            if(is_null($service)) {
92523e6571SMichael Große                $this->cleanLogout();
93523e6571SMichael Große                return false;
94a7a8f46aSAndreas Gohr            }
95a7a8f46aSAndreas Gohr
96523e6571SMichael Große            if($service->checkToken()) {
97188ba446SMichael Große                $ok = $this->processLogin($sticky, $service, $servicename, $page, $params);
98523e6571SMichael Große                if (!$ok) {
99523e6571SMichael Große                    $this->cleanLogout();
100523e6571SMichael Große                    return false;
101523e6571SMichael Große                }
102523e6571SMichael Große                return true;
103523e6571SMichael Große            } else {
104523e6571SMichael Große                if ($existingLoginProcess) {
105523e6571SMichael Große                    msg($this->getLang('oauth login failed'),0);
106523e6571SMichael Große                    $this->cleanLogout();
107523e6571SMichael Große                    return false;
108523e6571SMichael Große                } else {
109523e6571SMichael Große                    // first time here
110523e6571SMichael Große                    $this->relogin($servicename);
111523e6571SMichael Große                }
112523e6571SMichael Große            }
113523e6571SMichael Große
114523e6571SMichael Große            $this->cleanLogout();
115a7a8f46aSAndreas Gohr            return false; // something went wrong during oAuth login
116213f4618SMichael Große        } elseif (isset($_COOKIE[DOKU_COOKIE])) {
117213f4618SMichael Große            global $INPUT;
118213f4618SMichael Große            //try cookie
119213f4618SMichael Große            list($cookieuser, $cookiesticky, $auth, $servicename) = explode('|', $_COOKIE[DOKU_COOKIE]);
120213f4618SMichael Große            $cookieuser = base64_decode($cookieuser, true);
121213f4618SMichael Große            $auth = base64_decode($auth, true);
122213f4618SMichael Große            $servicename = base64_decode($servicename, true);
123213f4618SMichael Große            if ($auth === 'oauth') {
124213f4618SMichael Große                $this->relogin($servicename);
125213f4618SMichael Große            }
12680852c15SAndreas Gohr        }
12780852c15SAndreas Gohr
128a7a8f46aSAndreas Gohr        // do the "normal" plain auth login via form
129a7a8f46aSAndreas Gohr        return auth_login($user, $pass, $sticky);
130a7a8f46aSAndreas Gohr    }
13180852c15SAndreas Gohr
132f2e164b0SMichael Große    /**
133f2e164b0SMichael Große     * @param array $session cookie auth session
134f2e164b0SMichael Große     *
135f2e164b0SMichael Große     * @return bool
136f2e164b0SMichael Große     */
137f2e164b0SMichael Große    protected function isSessionValid ($session) {
138f2e164b0SMichael Große        /** @var helper_plugin_oauth $hlp */
139f2e164b0SMichael Große        $hlp     = plugin_load('helper', 'oauth');
140f2e164b0SMichael Große        if ($hlp->validBrowserID($session)) {
141f2e164b0SMichael Große            if (!$hlp->isSessionTimedOut($session)) {
142f2e164b0SMichael Große                return true;
143f2e164b0SMichael Große            } elseif (!($hlp->isGETRequest() && $hlp->isDokuPHP())) {
144f2e164b0SMichael Große                // only force a recheck on a timed-out session during a GET request on the main script doku.php
145f2e164b0SMichael Große                return true;
146f2e164b0SMichael Große            }
147f2e164b0SMichael Große        }
148f2e164b0SMichael Große        return false;
149f2e164b0SMichael Große    }
150f2e164b0SMichael Große
151213f4618SMichael Große    protected function relogin($servicename) {
152213f4618SMichael Große        global $INPUT;
153213f4618SMichael Große
154213f4618SMichael Große        /** @var helper_plugin_oauth $hlp */
155213f4618SMichael Große        $hlp     = plugin_load('helper', 'oauth');
156213f4618SMichael Große        $service     = $hlp->loadService($servicename);
157213f4618SMichael Große        if(is_null($service)) return false;
158213f4618SMichael Große
159213f4618SMichael Große        // remember service in session
160213f4618SMichael Große        session_start();
161213f4618SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'] = $servicename;
162213f4618SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['id']      = $INPUT->str('id');
163188ba446SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['params']  = $_GET;
164213f4618SMichael Große
16509623faaSMichael Große        $_SESSION[DOKU_COOKIE]['oauth-done']['$_REQUEST'] = $_REQUEST;
166213f4618SMichael Große
167213f4618SMichael Große        if (is_array($INPUT->post->param('do'))) {
168213f4618SMichael Große            $doPost = key($INPUT->post->arr('do'));
169213f4618SMichael Große        } else {
170213f4618SMichael Große            $doPost = $INPUT->post->str('do');
171213f4618SMichael Große        }
172213f4618SMichael Große        $doGet = $INPUT->get->str('do');
173213f4618SMichael Große        if (!empty($doPost)) {
174213f4618SMichael Große            $_SESSION[DOKU_COOKIE]['oauth-done']['do'] = $doPost;
175213f4618SMichael Große        } elseif (!empty($doGet)) {
176213f4618SMichael Große            $_SESSION[DOKU_COOKIE]['oauth-done']['do'] = $doGet;
177213f4618SMichael Große        }
178213f4618SMichael Große
179213f4618SMichael Große        session_write_close();
180213f4618SMichael Große
181213f4618SMichael Große        $service->login();
182213f4618SMichael Große    }
183213f4618SMichael Große
184a7a8f46aSAndreas Gohr    /**
185b2b9fbc7SMichael Große     * @param                              $sticky
186b2b9fbc7SMichael Große     * @param OAuth\Plugin\AbstractAdapter $service
1879928f5efSMichael Große     * @param string                       $servicename
188b2b9fbc7SMichael Große     * @param string                       $page
189188ba446SMichael Große     * @param array                        $params
190f07c7607SMichael Große     *
191f07c7607SMichael Große     * @return bool
192f07c7607SMichael Große     */
193188ba446SMichael Große    protected function processLogin($sticky, $service, $servicename, $page, $params = array()) {
194b2b9fbc7SMichael Große        $uinfo = $service->getUser();
195b2b9fbc7SMichael Große        $ok = $this->processUser($uinfo, $servicename);
196f07c7607SMichael Große        if(!$ok) {
197f07c7607SMichael Große            return false;
198f07c7607SMichael Große        }
199b2b9fbc7SMichael Große        $this->setUserSession($uinfo, $servicename);
200b2b9fbc7SMichael Große        $this->setUserCookie($uinfo['user'], $sticky, $servicename);
201b2b9fbc7SMichael Große        if(isset($page)) {
202188ba446SMichael Große            if(!empty($params['id'])) unset($params['id']);
203188ba446SMichael Große            send_redirect(wl($page, $params, false, '&'));
204b2b9fbc7SMichael Große        }
205f07c7607SMichael Große        return true;
206f07c7607SMichael Große    }
207f07c7607SMichael Große
2089928f5efSMichael Große    /**
2099928f5efSMichael Große     * process the user and update the $uinfo array
2109928f5efSMichael Große     *
2119928f5efSMichael Große     * @param $uinfo
2129928f5efSMichael Große     * @param $servicename
2139928f5efSMichael Große     *
2149928f5efSMichael Große     * @return bool
2159928f5efSMichael Große     */
2169928f5efSMichael Große    protected function processUser(&$uinfo, $servicename) {
2179928f5efSMichael Große        $uinfo['user'] = $this->cleanUser((string) $uinfo['user']);
2189928f5efSMichael Große        if(!$uinfo['name']) $uinfo['name'] = $uinfo['user'];
2199928f5efSMichael Große
2209928f5efSMichael Große        if(!$uinfo['user'] || !$uinfo['mail']) {
2219928f5efSMichael Große            msg("$servicename did not provide the needed user info. Can't log you in", -1);
2229928f5efSMichael Große            return false;
2239928f5efSMichael Große        }
2249928f5efSMichael Große
2259928f5efSMichael Große        // see if the user is known already
2269928f5efSMichael Große        $user = $this->getUserByEmail($uinfo['mail']);
2279928f5efSMichael Große        if($user) {
2289928f5efSMichael Große            $sinfo = $this->getUserData($user);
2299928f5efSMichael Große            // check if the user allowed access via this service
2309928f5efSMichael Große            if(!in_array($this->cleanGroup($servicename), $sinfo['grps'])) {
2319928f5efSMichael Große                msg(sprintf($this->getLang('authnotenabled'), $servicename), -1);
2329928f5efSMichael Große                return false;
2339928f5efSMichael Große            }
2349928f5efSMichael Große            $uinfo['user'] = $user;
2359928f5efSMichael Große            $uinfo['name'] = $sinfo['name'];
2369928f5efSMichael Große            $uinfo['grps'] = array_merge((array) $uinfo['grps'], $sinfo['grps']);
237*d313403cSAnna Dabrowska        } elseif(actionOK('register') || $this->getConf('register-on-auth')) {
2389928f5efSMichael Große            $ok = $this->addUser($uinfo, $servicename);
2399928f5efSMichael Große            if(!$ok) {
2409928f5efSMichael Große                msg('something went wrong creating your user account. please try again later.', -1);
2419928f5efSMichael Große                return false;
2429928f5efSMichael Große            }
2439928f5efSMichael Große        } else {
2449928f5efSMichael Große            msg($this->getLang('addUser not possible'), -1);
2459928f5efSMichael Große            return false;
2469928f5efSMichael Große        }
2479928f5efSMichael Große        return true;
2489928f5efSMichael Große    }
2499928f5efSMichael Große
2509928f5efSMichael Große    /**
251b2b9fbc7SMichael Große     * new user, create him - making sure the login is unique by adding a number if needed
252b2b9fbc7SMichael Große     *
253b2b9fbc7SMichael Große     * @param array $uinfo user info received from the oAuth service
254b2b9fbc7SMichael Große     * @param string $servicename
255b2b9fbc7SMichael Große     *
256b2b9fbc7SMichael Große     * @return bool
257b2b9fbc7SMichael Große     */
258b2b9fbc7SMichael Große    protected function addUser(&$uinfo, $servicename) {
259b2b9fbc7SMichael Große        global $conf;
260b2b9fbc7SMichael Große        $user = $uinfo['user'];
261b2b9fbc7SMichael Große        $count = '';
262b2b9fbc7SMichael Große        while($this->getUserData($user . $count)) {
263b2b9fbc7SMichael Große            if($count) {
264b2b9fbc7SMichael Große                $count++;
265b2b9fbc7SMichael Große            } else {
266b2b9fbc7SMichael Große                $count = 1;
267b2b9fbc7SMichael Große            }
268b2b9fbc7SMichael Große        }
269b2b9fbc7SMichael Große        $user = $user . $count;
270b2b9fbc7SMichael Große        $uinfo['user'] = $user;
271b2b9fbc7SMichael Große        $groups_on_creation = array();
272b2b9fbc7SMichael Große        $groups_on_creation[] = $conf['defaultgroup'];
273b2b9fbc7SMichael Große        $groups_on_creation[] = $this->cleanGroup($servicename); // add service as group
274b2b9fbc7SMichael Große        $uinfo['grps'] = array_merge((array) $uinfo['grps'], $groups_on_creation);
275b2b9fbc7SMichael Große
276b2b9fbc7SMichael Große        $ok = $this->triggerUserMod(
277b2b9fbc7SMichael Große            'create',
278b2b9fbc7SMichael Große            array($user, auth_pwgen($user), $uinfo['name'], $uinfo['mail'], $groups_on_creation,)
279b2b9fbc7SMichael Große        );
280b2b9fbc7SMichael Große        if(!$ok) {
281b2b9fbc7SMichael Große            return false;
282b2b9fbc7SMichael Große        }
283b2b9fbc7SMichael Große
284b2b9fbc7SMichael Große        // send notification about the new user
285b2b9fbc7SMichael Große        $subscription = new Subscription();
286b2b9fbc7SMichael Große        $subscription->send_register($user, $uinfo['name'], $uinfo['mail']);
287b2b9fbc7SMichael Große        return true;
288b2b9fbc7SMichael Große    }
289b2b9fbc7SMichael Große
290b2b9fbc7SMichael Große    /**
291b2b9fbc7SMichael Große     * Find a user by his email address
292b2b9fbc7SMichael Große     *
293b2b9fbc7SMichael Große     * @param $mail
294b2b9fbc7SMichael Große     * @return bool|string
295b2b9fbc7SMichael Große     */
296b2b9fbc7SMichael Große    protected function getUserByEmail($mail) {
297b2b9fbc7SMichael Große        if($this->users === null) $this->_loadUserData();
298b2b9fbc7SMichael Große        $mail = strtolower($mail);
299b2b9fbc7SMichael Große
300b2b9fbc7SMichael Große        foreach($this->users as $user => $uinfo) {
301b2b9fbc7SMichael Große            if(strtolower($uinfo['mail']) == $mail) return $user;
302b2b9fbc7SMichael Große        }
303b2b9fbc7SMichael Große
304b2b9fbc7SMichael Große        return false;
305b2b9fbc7SMichael Große    }
306b2b9fbc7SMichael Große
307b2b9fbc7SMichael Große    /**
308b2b9fbc7SMichael Große     * @param array  $data
309b2b9fbc7SMichael Große     * @param string $service
310b2b9fbc7SMichael Große     */
311b2b9fbc7SMichael Große    protected function setUserSession($data, $service) {
312b2b9fbc7SMichael Große        global $USERINFO;
313b2b9fbc7SMichael Große        global $conf;
314b2b9fbc7SMichael Große
315b2b9fbc7SMichael Große        // set up groups
316b2b9fbc7SMichael Große        if(!is_array($data['grps'])) {
317b2b9fbc7SMichael Große            $data['grps'] = array();
318b2b9fbc7SMichael Große        }
319b2b9fbc7SMichael Große        $data['grps'][] = $this->cleanGroup($service);
320b2b9fbc7SMichael Große        $data['grps']   = array_unique($data['grps']);
321b2b9fbc7SMichael Große
322b2b9fbc7SMichael Große        $USERINFO                               = $data;
323b2b9fbc7SMichael Große        $_SERVER['REMOTE_USER']                 = $data['user'];
324b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['user']  = $data['user'];
325b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['pass']  = $data['pass'];
326b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['info']  = $USERINFO;
327b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['buid']  = auth_browseruid();
328b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['time']  = time();
329b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['oauth'] = $service;
330b2b9fbc7SMichael Große    }
331b2b9fbc7SMichael Große
332b2b9fbc7SMichael Große    /**
3339928f5efSMichael Große     * @param string $user
334523e6571SMichael Große     * @param bool   $sticky
3359928f5efSMichael Große     * @param string $servicename
336523e6571SMichael Große     * @param int    $validityPeriodInSeconds optional, per default 1 Year
3379928f5efSMichael Große     */
338523e6571SMichael Große    private function setUserCookie($user, $sticky, $servicename, $validityPeriodInSeconds = 31536000) {
3399928f5efSMichael Große        $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode('oauth').'|'.base64_encode($servicename);
3409928f5efSMichael Große        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
341523e6571SMichael Große        $time      = $sticky ? (time() + $validityPeriodInSeconds) : 0;
3429928f5efSMichael Große        setcookie(DOKU_COOKIE,$cookie, $time, $cookieDir, '',($conf['securecookie'] && is_ssl()), true);
3439928f5efSMichael Große    }
3449928f5efSMichael Große
345827232fcSMichael Große    /**
346b2b9fbc7SMichael Große     * Unset additional stuff in session on logout
347827232fcSMichael Große     */
348b2b9fbc7SMichael Große    public function logOff() {
349b2b9fbc7SMichael Große        parent::logOff();
350b2b9fbc7SMichael Große
351af2a4e8fSMichael Große        $this->cleanLogout();
352b2b9fbc7SMichael Große    }
353b2b9fbc7SMichael Große
354b2b9fbc7SMichael Große    /**
355b2b9fbc7SMichael Große     * unset auth cookies and session information
356b2b9fbc7SMichael Große     */
357b2b9fbc7SMichael Große    private function cleanLogout() {
358af2a4e8fSMichael Große        if(isset($_SESSION[DOKU_COOKIE]['oauth-done'])) {
359b2b9fbc7SMichael Große            unset($_SESSION[DOKU_COOKIE]['oauth-done']);
360af2a4e8fSMichael Große        }
361af2a4e8fSMichael Große        if(isset($_SESSION[DOKU_COOKIE]['auth'])) {
362b2b9fbc7SMichael Große            unset($_SESSION[DOKU_COOKIE]['auth']);
363af2a4e8fSMichael Große        }
364b2b9fbc7SMichael Große        $this->setUserCookie('',true,'',-60);
365b2b9fbc7SMichael Große    }
366b2b9fbc7SMichael Große
367b2b9fbc7SMichael Große    /**
368b2b9fbc7SMichael Große     * Enhance function to check against duplicate emails
369b2b9fbc7SMichael Große     *
370b2b9fbc7SMichael Große     * @param string $user
371b2b9fbc7SMichael Große     * @param string $pwd
372b2b9fbc7SMichael Große     * @param string $name
373b2b9fbc7SMichael Große     * @param string $mail
374b2b9fbc7SMichael Große     * @param null   $grps
375b2b9fbc7SMichael Große     * @return bool|null|string
376b2b9fbc7SMichael Große     */
377b2b9fbc7SMichael Große    public function createUser($user, $pwd, $name, $mail, $grps = null) {
378b2b9fbc7SMichael Große        if($this->getUserByEmail($mail)) {
379b2b9fbc7SMichael Große            msg($this->getLang('emailduplicate'), -1);
380827232fcSMichael Große            return false;
381827232fcSMichael Große        }
382b2b9fbc7SMichael Große
383b2b9fbc7SMichael Große        return parent::createUser($user, $pwd, $name, $mail, $grps);
384827232fcSMichael Große    }
385b2b9fbc7SMichael Große
386b2b9fbc7SMichael Große    /**
387b2b9fbc7SMichael Große     * Enhance function to check aainst duplicate emails
388b2b9fbc7SMichael Große     *
389b2b9fbc7SMichael Große     * @param string $user
390b2b9fbc7SMichael Große     * @param array  $changes
391b2b9fbc7SMichael Große     * @return bool
392b2b9fbc7SMichael Große     */
393b2b9fbc7SMichael Große    public function modifyUser($user, $changes) {
394b2b9fbc7SMichael Große        global $conf;
395b2b9fbc7SMichael Große
396b2b9fbc7SMichael Große        if(isset($changes['mail'])) {
397b2b9fbc7SMichael Große            $found = $this->getUserByEmail($changes['mail']);
398b2b9fbc7SMichael Große            if($found != $user) {
399b2b9fbc7SMichael Große                msg($this->getLang('emailduplicate'), -1);
400b2b9fbc7SMichael Große                return false;
401b2b9fbc7SMichael Große            }
402b2b9fbc7SMichael Große        }
403b2b9fbc7SMichael Große
404b2b9fbc7SMichael Große        $ok = parent::modifyUser($user, $changes);
405b2b9fbc7SMichael Große
406b2b9fbc7SMichael Große        // refresh session cache
407b2b9fbc7SMichael Große        touch($conf['cachedir'] . '/sessionpurge');
408b2b9fbc7SMichael Große
409b2b9fbc7SMichael Große        return $ok;
410827232fcSMichael Große    }
411827232fcSMichael Große
41280852c15SAndreas Gohr}
41380852c15SAndreas Gohr
41480852c15SAndreas Gohr// vim:ts=4:sw=4:et:
415