From acd454b06bbdbdaa763e8130c972c88b5a117472 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 27 Nov 2025 09:24:02 +0000 Subject: [PATCH] t3 --- index.php | 38 +++------ login.php | 91 +++++++++++++++++++++ logout.php | 6 ++ roles.php | 161 +++++++++++++++++++++++++++++++++++++ users.php | 228 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 495 insertions(+), 29 deletions(-) create mode 100644 login.php create mode 100644 logout.php create mode 100644 roles.php create mode 100644 users.php diff --git a/index.php b/index.php index baa46a5..94f8a65 100644 --- a/index.php +++ b/index.php @@ -1,3 +1,4 @@ + @@ -35,13 +36,17 @@
- + + خروج + + ورود +
@@ -92,31 +97,6 @@ - - - diff --git a/login.php b/login.php new file mode 100644 index 0000000..3885ff3 --- /dev/null +++ b/login.php @@ -0,0 +1,91 @@ +prepare("SELECT id, password, role_id FROM users WHERE username = :username"); + $stmt->bindParam(':username', $username); + $stmt->execute(); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($user && password_verify($password, $user['password'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $username; + $_SESSION['role_id'] = $user['role_id']; + header("Location: index.php"); + exit(); + } else { + $error_message = 'Invalid username or password.'; + } + } +} + +// Fetch header content +ob_start(); +include 'index.php'; +$page_content = ob_get_clean(); + +// Extract only the and
+$head_and_header = ''; +if (preg_match('/.*?<\/head>/s', $page_content, $head_match)) { + $head_and_header .= $head_match[0]; +} +if (preg_match('/
.*?<\/header>/s', $page_content, $header_match)) { + $head_and_header .= $header_match[0]; +} + +// Replace active nav link +$head_and_header = str_replace('Home', 'Home', $head_and_header); +$head_and_header = preg_replace('//', '', $head_and_header); + + +echo str_replace('', '', $head_and_header); + +?> + +
+ +
+ +.*?<\/footer>/s', $page_content, $footer_match)) { + echo $footer_match[0]; +} +?> diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..e4bc3fa --- /dev/null +++ b/logout.php @@ -0,0 +1,6 @@ +exec("CREATE TABLE IF NOT EXISTS roles ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL UNIQUE + )"); + + // Handle Create and Update + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_POST['add_role'])) { + $name = trim($_POST['role_name']); + if (!empty($name)) { + $stmt = $pdo->prepare("INSERT INTO roles (name) VALUES (:name)"); + $stmt->execute(['name' => $name]); + } + } elseif (isset($_POST['update_role'])) { + $id = $_POST['role_id']; + $name = trim($_POST['role_name']); + if (!empty($name) && !empty($id)) { + $stmt = $pdo->prepare("UPDATE roles SET name = :name WHERE id = :id"); + $stmt->execute(['name' => $name, 'id' => $id]); + } + } + header("Location: roles.php"); + exit; + } + + // Handle Delete + if (isset($_GET['delete_id'])) { + $id = $_GET['delete_id']; + $stmt = $pdo->prepare("DELETE FROM roles WHERE id = :id"); + $stmt->execute(['id' => $id]); + header("Location: roles.php"); + exit; + } + + // Fetch all roles + $roles = $pdo->query("SELECT * FROM roles ORDER BY id DESC")->fetchAll(); + + // Fetch role for editing + $editing_role = null; + if (isset($_GET['edit_id'])) { + $id = $_GET['edit_id']; + $stmt = $pdo->prepare("SELECT * FROM roles WHERE id = :id"); + $stmt->execute(['id' => $id]); + $editing_role = $stmt->fetch(); + } + +} catch (PDOException $e) { + die("Database error: " . $e->getMessage()); +} +?> + + + + + + مدیریت نقش‌ها + + + + + + + +
+ +
+ +
+
+
+

فهرست نقش‌ها

+
+ + + + + + + + + + + + + + + + + +
#نام نقشعملیات
+ ویرایش + حذف +
+
+
+
+

+
+ + + +
+ + +
+ + + انصراف + + + +
+
+
+
+ +
+
+ © 2025 سیستم مدیریت مدرسه +
+
+ + + + + diff --git a/users.php b/users.php new file mode 100644 index 0000000..f775aba --- /dev/null +++ b/users.php @@ -0,0 +1,228 @@ +exec("CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(255) NOT NULL UNIQUE, + email VARCHAR(255) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + role_id INT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE SET NULL + )"); + + // Fetch all roles for the dropdown + $roles = $pdo->query("SELECT * FROM roles ORDER BY name")->fetchAll(); + + // Handle Create and Update + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_POST['add_user'])) { + $username = trim($_POST['username']); + $email = trim($_POST['email']); + $password = $_POST['password']; + $role_id = $_POST['role_id']; + + if (!empty($username) && !empty($email) && !empty($password) && !empty($role_id)) { + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role_id) VALUES (:username, :email, :password, :role_id)"); + $stmt->execute([ + 'username' => $username, + 'email' => $email, + 'password' => $hashed_password, + 'role_id' => $role_id + ]); + } + } elseif (isset($_POST['update_user'])) { + $id = $_POST['user_id']; + $username = trim($_POST['username']); + $email = trim($_POST['email']); + $password = $_POST['password']; + $role_id = $_POST['role_id']; + + if (!empty($id) && !empty($username) && !empty($email) && !empty($role_id)) { + if (!empty($password)) { + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, password = :password, role_id = :role_id WHERE id = :id"); + $stmt->execute([ + 'username' => $username, + 'email' => $email, + 'password' => $hashed_password, + 'role_id' => $role_id, + 'id' => $id + ]); + } else { + $stmt = $pdo->prepare("UPDATE users SET username = :username, email = :email, role_id = :role_id WHERE id = :id"); + $stmt->execute([ + 'username' => $username, + 'email' => $email, + 'role_id' => $role_id, + 'id' => $id + ]); + } + } + } + header("Location: users.php"); + exit; + } + + // Handle Delete + if (isset($_GET['delete_id'])) { + $id = $_GET['delete_id']; + $stmt = $pdo->prepare("DELETE FROM users WHERE id = :id"); + $stmt->execute(['id' => $id]); + header("Location: users.php"); + exit; + } + + // Fetch all users with their role names + $users = $pdo->query(" + SELECT users.*, roles.name AS role_name + FROM users + LEFT JOIN roles ON users.role_id = roles.id + ORDER BY users.id DESC + ")->fetchAll(); + + // Fetch user for editing + $editing_user = null; + if (isset($_GET['edit_id'])) { + $id = $_GET['edit_id']; + $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id"); + $stmt->execute(['id' => $id]); + $editing_user = $stmt->fetch(); + } + +} catch (PDOException $e) { + die("Database error: " . $e->getMessage()); +} +?> + + + + + + مدیریت کاربران + + + + + + + +
+ +
+ +
+
+
+

فهرست کاربران

+
+ + + + + + + + + + + + + + + + + + + + + +
#نام کاربریایمیلنقشعملیات
+ ویرایش + حذف +
+
+
+
+

+
+ + + +
+ + +
+
+ + +
+
+ + > +
+
+ + +
+ + + انصراف + + + +
+
+
+
+ +
+
+ © 2025 سیستم مدیریت مدرسه +
+
+ + + + +