login: link to form to send password reset mail
[minimedit.git] / login / mailpass.inc.php
diff --git a/login/mailpass.inc.php b/login/mailpass.inc.php
new file mode 100644 (file)
index 0000000..6138ceb
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+function userbymail($email)
+{
+       foreach (glob("profile/*") as $useropt) {
+               if ($mailopt = @file_get_contents("$useropt/email.txt")
+               and rtrim($mailopt) == $email) {
+                       return substr($useropt, strlen('profile/'));
+               }
+       }
+       return FALSE;
+}
+
+function mailtoken($email)
+{
+       $found = userbymail($email);
+       if (!$found) return FALSE;
+
+       $token = substr(sha1('$Random'.rand()), 0, 10);
+       if (!file_put_contents("profile/$found/.token", $token))
+               throw new Exception("could not store token for $found");
+
+       $sitename = $_SERVER['HTTP_HOST'];
+       $sitelink = 'https://'.$sitename;
+       $rep = [
+               '[[user]]' => $found,
+               '[[link]]' => "$sitelink/login/pass?token=$found:$token",
+               '[[site]]' => $sitename,
+       ];
+
+       $mailbody = file_get_contents('login/mailpass.inc.txt');
+       $mailbody = str_replace(array_keys($rep), array_values($rep), $mailbody);
+       if (!$mailbody) throw new Exception('empty mail body');
+       $mailsub = "Wachtwoord-reset voor $sitename";
+
+       return mail($email, $mailsub, $mailbody);
+       return TRUE;
+}
+