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