diff --git a/php/php-dog/classes/lib.php b/php/php-dog/classes/lib.php index eca8b48..5ad99b1 100644 --- a/php/php-dog/classes/lib.php +++ b/php/php-dog/classes/lib.php @@ -119,6 +119,34 @@ function getNewFilename($targetDir, $fileExt, $length) return $newFname .".".$fileExt; } +function getNewPwtoken($length, $conn) +{ + $newToken = random_str($length); + + $maxtries = 100000; // prevent endless loop, most unlikely + $tries = 0; + + do + { + $stmt = $conn->prepare("SELECT id FROM `dogs` WHERE `pwtoken`=?"); + $stmt->execute([$newToken]); + + if ($stmt->rowCount() == 0) + { + break; + } + $newToken = random_str($length); + echo ('newToken ' . $newToken); + ++$tries; + } while ($tries <= $maxtries); + + if($tries >= $maxtries) + { + $newToken = ""; + } + return $newToken; +} + function getDogByQrId($qr_id, $conn, $fields = null) { if(strcmp($qr_id, 'SESS') === 0) diff --git a/php/php-dog/foundEmail.php b/php/php-dog/foundEmail.php index 5092399..68d0f46 100644 --- a/php/php-dog/foundEmail.php +++ b/php/php-dog/foundEmail.php @@ -114,7 +114,7 @@ try { } else { - $result = new CMsg(1, 500, "Sorry, there was an error sending your email."); + $result = new CMsg(0, 500, "Sorry, there was an error sending your email."); } echo $result->jsonarray(); } diff --git a/php/php-dog/wantNewPw.php b/php/php-dog/wantNewPw.php new file mode 100644 index 0000000..fdf1bf9 --- /dev/null +++ b/php/php-dog/wantNewPw.php @@ -0,0 +1,141 @@ +dbConnection(); + + +function updatePwtoken($email, $pwtoken) +{ + global $conn; + + $sql = "UPDATE dogs SET pwtoken=? WHERE email=?"; + $conn->prepare($sql)->execute([$pwtoken, $email]); +} + +function sendMail($email, $pwtoken) +{ + $mail = new CNetcupMailer(); + $mail->CharSet = "UTF-8"; + + $mail->setFrom('dog@hope-fly.de', 'Administrator'); + $mail->addAddress($email); //Add a recipient + $mail->addReplyTo('No Reply'); + $mail->isHTML(true); + //Attachments + + //Content + $mail->Subject = 'Passwort zurücksetzen!'; + $mail->Body = '

Neues Passwort setzen

Setzen'; + + return $mail->send(); +} + +$data = json_decode(file_get_contents("php://input")); +// $data = json_decode( +// '{ +// "email": "a@q.q" +// }' +// ); +$returnData = new CMsg(0); + +//IF REQUEST METHOD IS NOT EQUAL TO POST +if($_SERVER["REQUEST_METHOD"] != "POST") +{ + $returnData = new CMsg(0,404,'Page Not Found! REQUEST_METHOD'); + echo $returnData->jsonarray(); + return; +} + +$fields = ['fields' => ['email']]; + +// CHECKING EMPTY FIELDS +if( + !isset($data->email) + || empty(trim($data->email)) + ) +{ + $returnData = new CMsg(0,422,'Please Fill in all Required Fields!',$fields); + echo $returnData->jsonarray(); + return; +} + +// IF THERE ARE NO EMPTY FIELDS THEN- +$email = trim($data->email); + +if (!filter_var($email, FILTER_VALIDATE_EMAIL)) +{ + $returnData = new CMsg(0, 422, 'Invalid Email Address!'); + echo $returnData->jsonarray(); + return; +} + +try +{ + $stmt = $conn->prepare("SELECT * FROM `dogs` WHERE `email`=?"); + $stmt->execute([$email]); + + // IF THE USER IS FOUND BY EMAIL + if($stmt->rowCount()) + { + $row = $stmt->fetch(PDO::FETCH_ASSOC); + + $pwtoken = getNewPwtoken(20, $conn); + if (strlen($pwtoken) == 0) + { + $returnData = new CMsg( + 0, + 507, + 'Too many Passwordrequests on the server, try it again later' + ); + echo $returnData->jsonarray(); + return; + } + updatePwtoken($email, $pwtoken); + if(sendMail($email, $pwtoken)) + { + $returnData = new CMsg( + 1, + 200, + 'Successful Passwordrequest, Email is send! pwtoken: ' . $pwtoken, + null, + $row + ); + } + else + { + $returnData = new CMsg(0, 500, "Sorry, there was an error sending your email."); + } + // IF THE USER IS NOT FOUND BY EMAIL THEN SHOW THE FOLLOWING ERROR + } + else + { + $returnData = new CMsg(0,422,'Invalid Email Address! No Email found!'); + } +} +catch(PDOException $e) +{ + $returnData = new CMsg(0,500,$e->getMessage()); +} + +echo $returnData->jsonarray(); +?> \ No newline at end of file diff --git a/src/components/WantNewPw.tsx b/src/components/WantNewPw.tsx index fd0ea10..7e64f30 100644 --- a/src/components/WantNewPw.tsx +++ b/src/components/WantNewPw.tsx @@ -1,30 +1,34 @@ import React, { useState } from 'react' import toast, { Toaster } from 'react-hot-toast'; -import './ImportForm.css'; +import {wantNewPw} from '../services/PhpApi' +import './InputForm.css'; function WantNewPw() { const [email, setEmail] = useState(''); - const onChangeInput = (e: React.FormEvent ) => { + const onChangeInput = (e: React.FormEvent ) => + { +// console.log(e.currentTarget.value); setEmail(e.currentTarget.value); } const submitForm = async (e: React.FormEvent) => { e.preventDefault(); - - if(e.currentTarget.value.trim() !== '') - { - toast.error('Bitte Feld ausfüllen!'); - return; - } +// console.log(email); + // console.log((e.currentTarget.elements[0] as HTMLInputElement).value); + // if((e.currentTarget.elements[0] as HTMLInputElement).value.trim() === '') + // { + // toast.error('Bitte Feld ausfüllen!'); + // return; + // } - const data = await wantNewPw(email); + const data = await wantNewPw({email: email}); if(data.success) { - toast.success('Erfolgreich Passwort geändert!'); - e.currentTarget.reset(); + toast.success('Passwort angefordert! Bitte Postfach ' + email + ' checken!'); + //e.currentTarget.reset(); } else if(!data.success && data.message) { diff --git a/src/services/PhpApi.ts b/src/services/PhpApi.ts index 58f49cc..c55786a 100644 --- a/src/services/PhpApi.ts +++ b/src/services/PhpApi.ts @@ -267,7 +267,7 @@ export const wantNewPw = async ({email}:{email: string}) => } catch(err) { - return {success:0, message:'Password Reset Server Error!'}; + return {success:0, message:'Password Request Server Error!'}; } }