1be0d68e14c3c4c5e9e4bb4532570fdd84f3da8e
[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
18         $token = substr(sha1('$Random'.rand()), 0, 10);
19         if (!file_put_contents("profile/$found/.token", $token))
20                 throw new Exception("could not store token for $found");
21
22         $sitename = $_SERVER['HTTP_HOST'];
23         $sitelink = 'https://'.$sitename;
24         $rep = [
25                 '[[user]]' => $found,
26                 '[[link]]' => "$sitelink/login/pass?token=$found:$token",
27                 '[[site]]' => $sitename,
28         ];
29
30         $mailbody = file_get_contents('login/mailpass.inc.txt');
31         $mailbody = str_replace(array_keys($rep), array_values($rep), $mailbody);
32         if (!$mailbody) throw new Exception('empty mail body');
33         $mailsub = "Wachtwoord-reset voor $sitename";
34         $mailhead = "From: $sitename <info@$sitename>";
35         $rcpt = "$found <$email>";
36
37         return mail($rcpt, $mailsub, $mailbody, $mailhead);
38         return TRUE;
39 }
40