public static function updatePassword($token, $oldPassword, $newPassword) { if ($oldPassword == $newPassword) { throw new \Exception("Please insert different password with old password"); } else { if ($token == null or $token == "") { throw new \Exception("Session expired, please re-login"); } else { if ($oldPassword == null or $oldPassword == "") { throw new \Exception("Invalid old password"); } else { if ($newPassword == null or $newPassword == "") { throw new \Exception("New password must be not empty!"); } } } } $user = User::query()->where('token', '=', $token)->where('password', '=', md5($oldPassword))->first(); if ($user == null) { throw new \Exception("Invalid password"); } else { $user->password = md5($newPassword); $user->save(); return "Password updated."; } }
public static function del($token, $id) { if ($token == null or $token == "") { throw new \Exception("Session expired, please re-login"); } $userId = User::query()->where('token', '=', $token)->first()->id; if ($userId == null or $userId == "") { throw new \Exception("Session expired, please re-login"); } $comment = Comment::find($id); if ($comment == null) { throw new \Exception("Invalid comment item"); } if ($comment->delete()) { return "Comment item has ben deleted"; } else { return "Error while deleting comment"; } }
try { $params = $request->getQueryParams(); $user = User::register($params['username'], $params['name'], $params['password']); if ($user == null) { throw new Exception("Invalid username or password!"); } return ResultWrapper::getResult($user, $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); $app->put('/update-password', function (Request $request, Response $response) { try { $params = $request->getQueryParams(); $token = $request->getHeader('token'); $result = User::updatePassword($token, $params['oldPassword'], $params['newPassword']); if ($result == null) { throw new Exception("Wrong password!"); } return ResultWrapper::getResult($result, $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); $app->get('/promo/{page}', function (Request $request, Response $response, $page) { try { return ResultWrapper::getResult(Promo::get($page), $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } });