2022-10-09 02:40:06 +01:00
< ? php /** @noinspection PhpIncludeInspection */
2022-07-29 20:00:36 +01:00
2021-08-22 10:44:49 +01:00
////////////////// Index file //////////////
/// Creates base routes and runs ///
/// respective functions ///
////////////////////////////////////////////
2021-08-31 18:50:08 +01:00
//require “routes.php”;
require " ../vendor/autoload.php " ;
2022-10-09 02:40:06 +01:00
include " middleware.php " ;
2021-09-28 11:29:26 +01:00
include " timelineData.php " ;
include " projectData.php " ;
2022-07-29 20:00:36 +01:00
include " user.php " ;
2022-10-09 02:40:06 +01:00
use api\middleware ;
2021-12-28 21:20:00 +00:00
use api\projectData ;
use api\timelineData ;
2022-07-29 20:00:36 +01:00
use api\user ;
2021-12-28 21:20:00 +00:00
use Psr\Http\Message\ResponseInterface as Response ;
use Psr\Http\Message\ServerRequestInterface as Request ;
2022-01-23 21:52:01 +00:00
use Selective\SameSiteCookie\SameSiteCookieConfiguration ;
2022-01-23 20:59:06 +00:00
use Selective\SameSiteCookie\SameSiteCookieMiddleware ;
2022-10-09 02:40:06 +01:00
use Slim\Factory\AppFactory ;
use Tuupola\Middleware\JwtAuthentication ;
2021-08-22 10:44:49 +01:00
// Start slim
$app = AppFactory :: create ();
2022-07-29 20:00:36 +01:00
// set base path for all routes
2021-08-31 18:50:08 +01:00
$app -> setBasePath ( " /api " );
2022-10-09 02:40:06 +01:00
// Add middleware
new middleware ( $app );
2022-07-29 20:00:36 +01:00
2021-12-28 21:20:00 +00:00
$timelineData = new timelineData ();
$projectData = new projectData ();
2022-07-29 20:00:36 +01:00
$user = new user ();
2021-08-31 18:50:08 +01:00
$app -> get ( " /timelineData/ { timeline} " , function ( Request $request , Response $response , array $args )
2021-08-22 10:44:49 +01:00
{
2021-08-31 18:50:08 +01:00
global $timelineData ;
2021-12-28 21:20:00 +00:00
//check if route is available if it is get the data
2021-08-31 18:50:08 +01:00
//otherwise return an error
if ( $args [ " timeline " ] == " edu " )
{
2022-08-07 22:34:31 +01:00
$response -> getBody () -> write ( json_encode ( $timelineData -> getEduData ()));
return $response ;
2021-08-31 18:50:08 +01:00
}
2022-07-29 20:00:36 +01:00
if ( $args [ " timeline " ] == " work " )
2022-01-11 19:51:53 +00:00
{
2022-08-07 22:34:31 +01:00
$response -> getBody () -> write ( json_encode ( $timelineData -> getWorkData ()));
return $response ;
2022-01-11 19:51:53 +00:00
}
2022-07-29 20:00:36 +01:00
2023-01-17 01:56:03 +00:00
2022-07-29 20:00:36 +01:00
// something went wrong
$response -> getBody () -> write ( json_encode ( array ( " errorMessage " => " Error, timeline data not found " )));
return $response -> withStatus ( 404 );
2021-12-28 21:20:00 +00:00
});
2022-10-29 19:31:34 +01:00
$app -> patch ( " /timelineData/ { timeline}/ { id} " , function ( Request $request , Response $response , array $args )
{
global $timelineData ;
$data = $request -> getParsedBody ();
if ( $args [ " timeline " ] == " edu " && $args [ " id " ] != " undefined " )
{
if ( empty ( $data [ " dateFrom " ]) || empty ( $data [ " dateTo " ]) || empty ( $data [ " grade " ]) || empty ( $data [ " course " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
if ( ! $timelineData -> updateEduData ( $data [ " dateFrom " ], $data [ " dateTo " ], $data [ " grade " ], $data [ " course " ], $args [ " id " ]))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
2022-11-11 13:56:42 +00:00
return $response ;
2022-10-29 19:31:34 +01:00
}
2022-11-11 13:56:42 +00:00
if ( $args [ " timeline " ] == " work " && $args [ " id " ] != " undefined " )
2022-10-29 19:31:34 +01:00
{
if ( empty ( $data [ " dateFrom " ]) || empty ( $data [ " dateTo " ]) || empty ( $data [ " companyName " ]) || empty ( $data [ " area " ]) || empty ( $data [ " title " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
if ( ! $timelineData -> updateWorkData ( $data [ " dateFrom " ], $data [ " dateTo " ], $data [ " companyName " ], $data [ " area " ], $data [ " title " ], $args [ " id " ]))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
2022-11-11 13:56:42 +00:00
return $response ;
2022-10-29 19:31:34 +01:00
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " The correct data was not sent " )));
return $response -> withStatus ( 400 );
});
$app -> delete ( " /timelineData/ { timeline}/ { id} " , function ( Request $request , Response $response , array $args )
{
global $timelineData ;
if ( $args [ " timeline " ] == " edu " && $args [ " id " ] != null )
{
if ( ! $timelineData -> deleteEduData ( $args [ " id " ]))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
2022-11-11 13:56:42 +00:00
return $response ;
2022-10-29 19:31:34 +01:00
}
if ( $args [ " timeline " ] == " work " && $args [ " id " ] != null )
{
if ( ! $timelineData -> deleteWorkData ( $args [ " id " ]))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
2022-11-11 13:56:42 +00:00
return $response ;
2022-10-29 19:31:34 +01:00
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " The correct data was not sent " )));
return $response -> withStatus ( 400 );
});
$app -> post ( " /timelineData/ { timeline} " , function ( Request $request , Response $response , array $args )
{
global $timelineData ;
$data = $request -> getParsedBody ();
if ( $args [ " timeline " ] == " edu " )
{
if ( empty ( $data [ " dateFrom " ]) || empty ( $data [ " dateTo " ]) || empty ( $data [ " grade " ]) || empty ( $data [ " course " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
$insertedID = $timelineData -> addEduData ( $data [ " dateFrom " ], $data [ " dateTo " ], $data [ " grade " ], $data [ " course " ]);
if ( ! is_int ( $insertedID ))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( array ( " ID " => $insertedID )));
2022-11-11 13:56:42 +00:00
return $response ;
2022-10-29 19:31:34 +01:00
}
if ( $args [ " timeline " ] == " work " )
{
if ( empty ( $data [ " dateFrom " ]) || empty ( $data [ " companyName " ]) || empty ( $data [ " area " ]) || empty ( $data [ " title " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
if ( empty ( $data [ " dateTo " ]))
{
$data [ " dateTo " ] = " " ;
}
$insertedID = $timelineData -> addWorkData ( $data [ " dateFrom " ], $data [ " dateTo " ], $data [ " companyName " ], $data [ " area " ], $data [ " title " ]);
if ( ! is_int ( $insertedID ))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( array ( " ID " => $insertedID )));
2022-11-11 13:56:42 +00:00
return $response ;
2022-10-29 19:31:34 +01:00
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " The correct data was not sent " )));
return $response -> withStatus ( 400 );
});
2022-08-07 22:34:31 +01:00
$app -> get ( " /projectData " , function ( Request $request , Response $response )
2021-12-28 21:20:00 +00:00
{
global $projectData ;
2022-01-11 19:51:53 +00:00
$result = $projectData -> getProjectData ();
2021-12-28 21:20:00 +00:00
$json = json_encode ( $result );
$response -> getBody () -> write ( $json );
2022-01-11 19:51:53 +00:00
if ( array_key_exists ( " errorMessage " , $result ))
2021-12-28 21:20:00 +00:00
{
2022-01-11 19:51:53 +00:00
$response = $response -> withStatus ( 404 );
2021-12-28 21:20:00 +00:00
}
2021-08-31 18:50:08 +01:00
//use content type json to indicate json data on frontend.
2022-07-29 20:00:36 +01:00
return $response ;
2021-08-22 10:44:49 +01:00
});
2021-08-31 18:50:08 +01:00
2022-11-11 13:56:42 +00:00
$app -> patch ( " /projectData/ { id} " , function ( Request $request , Response $response , array $args )
{
global $projectData ;
2023-02-06 01:26:35 +00:00
$data = $request -> getParsedBody ();
if ( $args [ " id " ] != " undefined " )
2022-11-11 13:56:42 +00:00
{
2023-02-07 03:09:14 +00:00
if ( empty ( $data [ " title " ]) || empty ( $data [ " isMainProject " ]) || empty ( $data [ " information " ]) || empty ( $data [ " gitLink " ]))
2022-11-11 13:56:42 +00:00
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
2023-02-07 03:09:14 +00:00
if ( ! $projectData -> updateProjectData ( $args [ " id " ], $data [ " title " ], $data [ " isMainProject " ], $data [ " information " ], $data [ " projectLink " ], $data [ " gitLink " ]))
2022-11-11 13:56:42 +00:00
{
// uh oh something went wrong
2023-02-07 03:09:14 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " , " data " => $projectData -> updateProjectData ( $args [ " id " ], $data [ " title " ], $data [ " isMainProject " ], $data [ " information " ], $data [ " projectLink " ], $data [ " gitLink " ]))));
2022-11-11 13:56:42 +00:00
return $response -> withStatus ( 500 );
}
return $response ;
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an ID " )));
return $response -> withStatus ( 400 );
});
$app -> delete ( " /projectData/ { id} " , function ( Request $request , Response $response , array $args )
{
global $projectData ;
if ( $args [ " id " ] != null )
{
2023-02-07 03:09:14 +00:00
$message = $projectData -> deleteProjectData ( $args [ " id " ]);
if ( $message === " error " )
2022-11-11 13:56:42 +00:00
{
// uh oh something went wrong
2023-02-07 03:09:14 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong or the project with ID " . $args [ " id " ] . " does not exist " )));
2022-11-11 13:56:42 +00:00
return $response -> withStatus ( 500 );
}
2023-02-07 03:09:14 +00:00
if ( $message === " cannot delete " )
{
//uh oh cannot delete the main project
$response -> getBody () -> write ( json_encode ( array ( " error " => " Cannot delete the main project " )));
return $response -> withStatus ( 409 );
}
2022-11-11 13:56:42 +00:00
return $response ;
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an ID " )));
return $response -> withStatus ( 400 );
});
$app -> post ( " /projectData " , function ( Request $request , Response $response )
{
global $projectData ;
$data = $request -> getParsedBody ();
2023-02-07 03:09:14 +00:00
if ( empty ( $data [ " title " ]) || empty ( $data [ " isMainProject " ]) || empty ( $data [ " information " ]) || empty ( $data [ " gitLink " ]))
2022-11-11 13:56:42 +00:00
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
2023-02-06 01:26:35 +00:00
$insertedID = $projectData -> addProjectData ( $data [ " title " ], $data [ " isMainProject " ], $data [ " information " ], $data [ " projectLink " ], $data [ " gitLink " ]);
2022-11-11 13:56:42 +00:00
if ( ! is_int ( $insertedID ))
{
// uh oh something went wrong
2023-02-07 03:09:14 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Something went wrong " , " message " => $insertedID )));
2022-11-11 13:56:42 +00:00
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( array ( " ID " => $insertedID )));
return $response ;
});
2023-02-06 01:26:35 +00:00
$app -> post ( " /projectImage/ { id} " , function ( Request $request , Response $response , array $args )
{
global $projectData ;
$files = $request -> getUploadedFiles ();
if ( empty ( $args [ " id " ]) || empty ( $files ))
{
// uh oh only some of the data was sent
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
$message = $projectData -> uploadImage ( $args [ " id " ], $files [ " img " ]);
if ( ! is_array ( $message ))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => $message )));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( $message ));
return $response ;
});
2022-08-07 22:34:31 +01:00
$app -> post ( " /contact " , function ( Request $request , Response $response )
2022-01-11 19:51:53 +00:00
{
$data = $request -> getParsedBody ();
if ( empty ( $data [ " fName " ]) || empty ( $data [ " lName " ]) || empty ( $data [ " email " ]) || empty ( $data [ " subject " ]) || empty ( $data [ " message " ]))
{
$response -> getBody () -> write ( json_encode ( array ( " errorMessage " => " Please fill out all the fields " )));
2022-07-29 20:00:36 +01:00
return $response -> withStatus ( 400 );
2023-01-17 01:56:03 +00:00
2022-01-11 19:51:53 +00:00
}
if ( ! filter_var ( $data [ " email " ], FILTER_VALIDATE_EMAIL ))
{
$response -> getBody () -> write ( json_encode ( array ( " errorMessage " => " Email is not the correct format " )));
2022-09-14 13:35:58 +01:00
return $response -> withStatus ( 400 );
2022-01-11 19:51:53 +00:00
}
// email form filler/conatcter
2022-07-29 20:00:36 +01:00
$headers1 = " From: noreply@rohitpai.co.uk \r \n " ;
$headers1 .= " Reply-To: rohit@rohitpai.co.uk \r \n " ;
2022-01-11 19:51:53 +00:00
$headers1 .= " MIME-Version: 1.0 \r \n " ;
$headers1 .= " Content-Type: text/html; charset=UTF-8 \r \n " ;
$message1 = "
2022-01-23 20:59:06 +00:00
< html lang = \ " en \" >
2022-01-11 19:51:53 +00:00
< head >
< title > { $data [ 'subject' ]} </ title >
< style >
@ import url ( \ " https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,400;0,700;1,400;1,700&family=Share+Tech+Mono&family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&display=swap \" );
body {
font - family : Noto Sans KR , sans - serif ;
font - style : normal ;
font - weight : 500 ;
font - size : var ( -- generalFS );
line - height : 1.625 rem ;
}
table {
border - collapse : collapse ;
width : 100 % ;
}
table td , table th {
border : 1 px solid #ddd;
padding : 8 px ;
}
table tr : nth - child ( even ) {
background - color : #f2f2f2;
}
table tr : hover {
background - color : #ddd;
}
table th {
padding - top : 12 px ;
padding - bottom : 12 px ;
text - align : left ;
background - color : hsla ( 79 , 62 % , 59 % , 1 );
color : white ;
}
hr {
border - color : hsla ( 0 , 0 % , 78 % , 1 );
}
</ style >
</ head >
< body >
< p > Thank you for filling out the form on my website , I will try to respond to your query as soon as I can .</ p >
< br >
< p > Below is what you filled in for your record </ p >
< table >
< thead >
< th > Firstname </ th >
< th > Lastname </ th >
< th > Email </ th >
< th > Subject </ th >
< th > message </ th >
</ thead >
< tr >
< td > { $data [ 'fName' ]} </ td >
< td > { $data [ 'lName' ]} </ td >
< td >< a href = \ " mailto: { $data [ 'email' ] } \" > { $data [ 'email' ] } </a></td>
< td > { $data [ 'subject' ]} </ td >
< td > { $data [ 'message' ]} </ td >
</ tr >
</ table >
< br >
< hr >
2022-07-29 20:00:36 +01:00
< p > Regards , < br > Rohit Pai < br > < a href = \ " mailto:rohit@rohitpai.co.uk \" >rohit@rohitpai.co.uk</a>
2022-01-11 19:51:53 +00:00
</ body >
</ html >
" ;
mail ( $data [ " email " ], $data [ " subject " ], $message1 , $headers1 );
// email to me
2022-07-29 20:00:36 +01:00
$headers2 = " From: noreply@rohitpai.co.uk \r \n " ;
2022-01-11 19:51:53 +00:00
$headers2 .= " Reply-To: { $data [ 'email' ] } \r \n " ;
$headers2 .= " MIME-Version: 1.0 \r \n " ;
$headers2 .= " Content-Type: text/html; charset=UTF-8 \r \n " ;
$message2 = "
2022-01-23 20:59:06 +00:00
< html lang = \ " en \" >
2022-01-11 19:51:53 +00:00
< head >
< title > { $data [ 'subject' ]} </ title >
< style >
@ import url ( \ " https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,400;0,700;1,400;1,700&family=Share+Tech+Mono&family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&display=swap \" );
body {
font - family : Noto Sans KR , sans - serif ;
font - style : normal ;
font - weight : 500 ;
font - size : var ( -- generalFS );
line - height : 1.625 rem ;
}
table {
border - collapse : collapse ;
width : 100 % ;
}
table td , table th {
border : 1 px solid #ddd;
padding : 8 px ;
}
table tr : nth - child ( even ) {
background - color : #f2f2f2;
}
table tr : hover {
background - color : #ddd;
}
table th {
padding - top : 12 px ;
padding - bottom : 12 px ;
text - align : left ;
background - color : hsla ( 79 , 62 % , 59 % , 1 );
color : white ;
}
hr {
border - color : hsla ( 0 , 0 % , 78 % , 1 );
}
</ style >
</ head >
< body >
< p > { $data [ 'fName' ]} { $data [ 'lName' ]} filled in the form on the website , here is what they sent .</ p >
< table >
< thead >
< th > Firstname </ th >
< th > Lastname </ th >
< th > Email </ th >
< th > Subject </ th >
< th > message </ th >
</ thead >
< tr >
< td > { $data [ 'fName' ]} </ td >
< td > { $data [ 'lName' ]} </ td >
< td >< a href = \ " mailto: { $data [ 'email' ] } \" > { $data [ 'email' ] } </a></td>
< td > { $data [ 'subject' ]} </ td >
< td > { $data [ 'message' ]} </ td >
</ tr >
</ table >
</ body >
</ html >
" ;
2022-07-29 20:00:36 +01:00
mail ( " rohit@rohitpai.co.uk " , " { $data [ 'fName' ] } { $data [ 'lName' ] } filled in the form " , $message2 , $headers2 );
2022-01-11 19:51:53 +00:00
return $response -> withStatus ( 201 );
});
2022-09-14 13:35:58 +01:00
$app -> post ( " /user/login " , function ( Request $request , Response $response )
{
2022-07-29 20:00:36 +01:00
global $user ;
// get request data
$data = $request -> getParsedBody ();
if ( empty ( $data [ " username " ]) || empty ( $data [ " password " ]))
{
// uh oh user sent empty data
return $response -> withStatus ( 400 );
}
if ( $user -> checkUser ( $data [ " username " ], $data [ " password " ]))
{
2022-09-14 13:35:58 +01:00
// yay, user is logged in
2022-10-09 02:40:06 +01:00
$_SESSION [ " token " ] = $user -> createToken ( $data [ " username " ]);
2022-07-29 20:00:36 +01:00
$_SESSION [ " username " ] = $data [ " username " ];
2022-10-09 02:40:06 +01:00
$response -> getBody () -> write ( json_encode ( array ( " token " => $_SESSION [ " token " ])));
2022-07-29 20:00:36 +01:00
return $response ;
}
2022-11-01 04:59:39 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Unauthorised " )));
2022-07-29 20:00:36 +01:00
return $response -> withStatus ( 401 );
});
2023-02-06 01:26:35 +00:00
$app -> get ( " /user/logout " , function ( Request $request , Response $response )
{
session_unset ();
return $response ;
});
2022-09-14 13:35:58 +01:00
$app -> get ( " /user/isLoggedIn " , function ( Request $request , Response $response )
{
2022-07-29 20:00:36 +01:00
global $user ;
if ( empty ( $_SESSION [ " token " ]) && empty ( $_SESSION [ " username " ]))
{
// uh oh user not logged in
return $response -> withStatus ( 401 );
}
if ( empty ( $_SESSION [ " token " ]))
{
// user is logged in but no token was created
2022-10-09 02:40:06 +01:00
$_SESSION [ " token " ] = $user -> createToken ( $_SESSION [ " username " ]);
2022-07-29 20:00:36 +01:00
return $response ;
2022-09-14 13:35:58 +01:00
}
$response -> getBody () -> write ( json_encode ( array ( " token " => $_SESSION [ " token " ])));
return $response ;
2023-02-06 01:26:35 +00:00
2022-07-29 20:00:36 +01:00
});
2022-08-07 22:34:31 +01:00
$app -> get ( " /user/checkResetEmail/ { email} " , function ( Request $request , Response $response , array $args )
{
global $user ;
if ( empty ( $args [ " email " ]))
{
// uh oh sent empty data
return $response -> withStatus ( 400 );
}
if ( $user -> checkEmail ( $args [ " email " ]))
{
// yay email does exist
2022-10-09 02:40:06 +01:00
$_SESSION [ " resetToken " ] = $user -> sendResetEmail ( $args [ " email " ]);
2022-08-07 22:34:31 +01:00
$_SESSION [ " resetEmail " ] = $args [ " email " ];
return $response ;
}
return $response -> withStatus ( 404 );
});
$app -> get ( " /user/resendEmail " , function ( Request $request , Response $response )
{
if ( empty ( $_SESSION [ " resetToken " ]))
{
// uh oh not authorized to resend email
return $response -> withStatus ( 401 );
}
global $user ;
2022-10-09 02:40:06 +01:00
$_SESSION [ " resetToken " ] = $user -> sendResetEmail ( $_SESSION [ " resetEmail " ]);
2022-08-07 22:34:31 +01:00
return $response ;
});
$app -> get ( " /user/checkResetCode/ { code} " , function ( Request $request , Response $response , array $args )
{
if ( empty ( $args [ " code " ]))
{
// uh oh sent empty data
return $response -> withStatus ( 400 );
}
if ( $_SESSION [ " resetToken " ] === $args [ " code " ])
{
// yay, code code matches
return $response ;
}
2022-09-14 13:35:58 +01:00
2022-08-07 22:34:31 +01:00
return $response -> withStatus ( 401 );
});
2022-09-14 13:35:58 +01:00
$app -> post ( " /user/changePassword " , function ( Request $request , Response $response )
{
global $user ;
if ( empty ( $_SESSION [ " resetToken " ]) && empty ( $_SESSION [ " resetEmail " ]))
{
// uh oh not authorized to change password
return $response -> withStatus ( 401 );
}
$data = $request -> getParsedBody ();
if ( empty ( $data [ " password " ]))
{
// uh oh sent empty data
return $response -> withStatus ( 400 );
}
if ( $user -> changePassword ( $_SESSION [ " resetEmail " ], $data [ " password " ]))
{
// yay, password changed
unset ( $_SESSION [ " resetToken " ]);
unset ( $_SESSION [ " resetEmail " ]);
return $response ;
}
return $response -> withStatus ( 500 );
});
2022-07-29 20:00:36 +01:00
$app -> run ();