login/pass: error messages below page title
[minimedit.git] / login / mailpass.inc.php
1 <?php
2 function userbymail($email)
3 {
4         $email = strtolower($email);
5         foreach (glob("profile/*") as $useropt) {
6                 if ($mailopt = @file_get_contents("$useropt/email.txt")
7                 and strtolower(rtrim($mailopt)) == $email) {
8                         return substr($useropt, strlen('profile/'));
9                 }
10         }
11         return FALSE;
12 }
13
14 function mailtoken($email)
15 {
16         $found = userbymail($email);
17         if (!$found) return FALSE;
18         $user = new User("profile/$found");
19         if (empty($user)) return FALSE;
20
21         $token = substr(sha1('$Random'.rand()), 0, 10);
22         if (!file_put_contents("profile/$found/.token", $token))
23                 throw new Exception("could not store token for $found");
24
25         $sitename = preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']);
26         $sitelink = 'https://'.$sitename;
27         $rep = [
28                 '[[user]]' => $user->name ?: $found,
29                 '[[link]]' => "$sitelink/login/pass?token=$found:$token",
30                 '[[site]]' => $sitename,
31         ];
32
33         $mailbody = file_get_contents('login/mailpass.inc.txt');
34         $mailbody = str_replace(array_keys($rep), array_values($rep), $mailbody);
35         if (!$mailbody) throw new Exception('empty mail body');
36         $mailsub = "Wachtwoord-reset voor $sitename";
37         $mailhead = "From: $sitename <info@$sitename>";
38         $rcpt = "$found <$email>";
39
40         return mail($rcpt, $mailsub, $mailbody, $mailhead);
41         return TRUE;
42 }
43