From ea9868f9086c7d060b9707ce325d6d65af0033ee Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Mon, 25 Jan 2016 17:15:27 +0100 Subject: [PATCH] Added function of password.php --- api/password.php | 38 +++++++++++++++++++++++++++++ js/password.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ password.php | 8 +++---- 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 api/password.php create mode 100644 js/password.js diff --git a/api/password.php b/api/password.php new file mode 100644 index 0000000..9cfc12b --- /dev/null +++ b/api/password.php @@ -0,0 +1,38 @@ +. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +require_once '../config/config-default.php'; +require_once '../lib/database.php'; +require_once '../lib/session.php'; + +$input = json_decode(file_get_contents('php://input')); + +if(isset($input->action) && $input->action == "changePassword") { + $passwordHash = password_hash($input->password, PASSWORD_DEFAULT); + + $stmt = $db->prepare("UPDATE user SET password=? WHERE id=?"); + $stmt->bind_param("si", $passwordHash, $_SESSION['id']); + $stmt->execute(); + $stmt->close(); +} + +if(isset($retval)) { + echo json_encode($retval); +} else { + echo "{}"; +} diff --git a/js/password.js b/js/password.js new file mode 100644 index 0000000..3e197e2 --- /dev/null +++ b/js/password.js @@ -0,0 +1,62 @@ +/* + * Copyright 2016 Lukas Metzger . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +$(document).ready(function() { + + $('#saveChanges').click(function(evt){ + evt.preventDefault(); + savePassword(); + }); + + $('#user-password').unbind().bind("paste keyup change", function() { + $('#user-password').parent().removeClass("has-error"); + }); + + $('#user-password2').unbind().bind("paste keyup change", function() { + if($('#user-password').val() != $('#user-password2').val()) { + $('#user-password2').parent().addClass("has-error"); + } else { + $('#user-password2').parent().removeClass("has-error"); + } + }); +}); + +function savePassword() { + + if($('#user-password').val().length <= 0) { + $('#user-password').parent().addClass("has-error"); + $('#user-password2').parent().addClass("has-error"); + } + if($('#user-password2').parent().hasClass("has-error")) { + return; + } + + var data = { + password: $('#user-password').val(), + action: "changePassword" + }; + + $.post( + "api/password.php", + JSON.stringify(data), + function(data) { + $('#user-password').val(""); + $('#user-password2').val(""); + }, + "json" + ); +} + diff --git a/password.php b/password.php index 87edf37..7aa7def 100644 --- a/password.php +++ b/password.php @@ -33,7 +33,7 @@ limitations under the License. - +