-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_post.php
187 lines (165 loc) · 6.12 KB
/
edit_post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit();
}
if (isset($_GET["username"])) {
$viewedUsername = $_GET["username"];
} else {
header("Location: index.php");
exit();
}
$loggedInUsername = $_SESSION["username"];
// Load the posts data
$postsData = [];
$postsFile = "channels/$viewedUsername/posts.json";
if (file_exists($postsFile)) {
$postsData = json_decode(file_get_contents($postsFile), true);
}
// Handle post editing
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["editPost"])) {
$postIndex = $_POST["postIndex"];
$newContent = $_POST["postContent"];
if ($postIndex >= 0 && $postIndex < count($postsData)) {
$postsData[$postIndex]["content"] = $newContent;
// Handle image upload
if (isset($_FILES["postImage"]) && $_FILES["postImage"]["error"] === 0) {
$imageFileType = strtolower(pathinfo($_FILES["postImage"]["name"], PATHINFO_EXTENSION));
$allowedExtensions = array("jpg", "jpeg", "png", "gif");
if (in_array($imageFileType, $allowedExtensions)) {
$targetPath = "channels/$viewedUsername/images/" . uniqid() . ".$imageFileType";
move_uploaded_file($_FILES["postImage"]["tmp_name"], $targetPath);
$postsData[$postIndex]["image"] = $targetPath;
}
}
file_put_contents($postsFile, json_encode($postsData, JSON_PRETTY_PRINT));
}
header("Location: channel.php?username=$viewedUsername");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Post - <?php echo $viewedUsername; ?>'s Channel</title>
<style>
/* Your CSS styles here */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.channel-banner {
/* Channel banner styles */
}
.profile-picture {
/* Profile picture styles */
}
.edit-form textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
resize: vertical;
}
.edit-form button[type="submit"] {
background-color: #007bff;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
}
.edit-form button[type="submit"]:hover {
background-color: #0056b3;
}
.post-list {
display: grid;
grid-gap: 20px;
}
.post {
/* Post styles */
}
.post .post-content {
margin-bottom: 10px;
}
.post .post-image img {
max-width: 100%;
height: auto;
}
/* Add more styles as needed */
</style>
</head>
<body>
<div class="channel-banner">
<!-- Display banner and edit forms for banner/profile -->
<?php
if (file_exists("channels/$viewedUsername/banner.jpg")) {
echo '<img src="channels/' . $viewedUsername . '/banner.jpg" alt="Channel Banner">';
} else {
echo '<h1>' . $viewedUsername . "'s Channel</h1>";
}
?>
<p>Welcome to <?php echo $viewedUsername; ?>'s Channel!</p>
</div>
<div class="container">
<div class="profile-picture">
<!-- Display profile picture and edit forms for profile picture -->
<?php
if (file_exists("channels/$viewedUsername/profile.jpg")) {
echo '<img src="channels/' . $viewedUsername . '/profile.jpg" alt="Profile Picture">';
} else {
echo '<img src="default_profile.jpg" alt="Profile Picture">';
}
?>
</div>
<div class="edit-form">
<?php
if ($loggedInUsername === $viewedUsername) {
echo '<h2>Edit Posts</h2>';
echo '<ul class="post-list">';
foreach ($postsData as $postKey => $post) {
echo '<li class="post">';
echo '<div class="post-content">';
echo '<form action="edit_post.php?username=' . $viewedUsername . '" method="post" enctype="multipart/form-data">';
echo '<textarea name="postContent" rows="4">' . nl2br(htmlspecialchars($post["content"])) . '</textarea>';
echo '<input type="hidden" name="postIndex" value="' . $postKey . '">';
echo '<input type="file" name="postImage" accept="image/*">';
echo '<button type="submit" name="editPost">Save Changes</button>';
echo '</form>';
echo '</div>';
if (isset($post["image"])) {
echo '<div class="post-image"><img src="' . htmlspecialchars($post["image"]) . '" alt="Post Image"></div>';
}
echo '</li>';
}
echo '</ul>';
} else {
echo '<h2>Posts</h2>';
echo '<ul class="post-list">';
foreach ($postsData as $post) {
echo '<li class="post">';
if (isset($post["content"])) {
echo '<div class="post-content">' . nl2br(htmlspecialchars($post["content"])) . '</div>';
}
if (isset($post["image"])) {
echo '<div class="post-image"><img src="' . htmlspecialchars($post["image"]) . '" alt="Post Image"></div>';
}
echo '</li>';
}
echo '</ul>';
}
?>
</div>
</div>
</body>
</html>