aboutsummaryrefslogtreecommitdiff
path: root/templates/single.inc.php
blob: 35c9244e889db94e549f06cb6b6499791c874ebe (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
	if(!defined('ROOT')) die('Don\'t call this directly.');

	$id = (!empty(path(0))) ? (int) path(0) : 0;

	$action = 'display';
	if(mb_strtolower(path(1)) == 'delete') $action = 'delete';
	if(mb_strtolower(path(1)) == 'undelete') $action = 'undelete';
	if(mb_strtolower(path(1)) == 'edit') $action = 'edit';
	if(mb_strtolower(path(1)) == 'json') $action = 'json';

	$error = false;
	if($config['logged_in']) {

		// delete post
		if(!empty($_POST['action']) && $_POST['action'] == 'delete') {
			$result = db_delete((int) $_POST['id']);

			if(!$result) {
				$error = 'Post could not be deleted!';
			} else {

				rebuild_feeds();

				if($config['activitypub']) {
					// todo: send DELETE activity to followers
					// https://www.w3.org/TR/activitypub/#delete-activity-inbox
					activitypub_delete_post($_POST['id']);
				}

				header('Location: '.$config['url']);
				die();
			}
		}

		// undelete post
		if($action == 'undelete') {
			$result = db_delete((int) $id, true);

			if(!$result) {
				$error = 'Post could not be restored!';
			} else {
				rebuild_feeds();
			}
		}

		// edit post
		if(!empty($_POST['action']) && $_POST['action'] == 'edit') {

			// check changes to attachments
			$attached_files = db_get_attached_files($_POST['id']);
			if(!empty($attached_files)) {
				$files_ids = array_column($attached_files, 'id');

				if(empty($_POST['attachments'])) {
					// remove ALL attachments
					$to_remove = $files_ids;
				} else {
					// remove specified attachments
					/*
					$to_remove = array_filter($attached_files, function($v) {
						return !in_array($v['id'], $_POST['attachments']);
					});
					*/
					$to_remove = array_diff($files_ids, $_POST['attachments']);
				}

				if(count($to_remove) > 0) {
					if(!detatch_files($to_remove, $_POST['id'])) {
						// could not remove attachments
						// var_dump($to_remove);
					}
				}
			}

			$result = db_update((int) $_POST['id'], $_POST['content']);

			if(!$result) {
				$error = 'Post could not be updated!';
			} else {
				rebuild_feeds();

				if($config['activitypub']) {
					// todo: send UPDATE activity to followers
					// https://www.w3.org/TR/activitypub/#update-activity-inbox
					activitypub_update_post($_POST['id']);
				}

				header('Location: '.$config['url'].'/'.$_POST['id']);
				die();
			}
		}
	}

	// load the actual post
	$post = db_select_post($id);
	if(is_numeric($post['post_deleted'])) {
		if(!$config['logged_in']) {
			header('Location: '.$config['url']);
		}
	}

	if($action == 'json') {

		$json = activitypub_activity_from_post($post, true);

		header('Content-Type: application/ld+json');
		echo($json);
		die();
	}

	$title_suffix = 'entry #' . $id;
	require(ROOT.DS.'snippets'.DS.'header.snippet.php');

?><body ontouchstart="">
	<div class="wrap">
		<?php require(ROOT.DS.'snippets'.DS.'nav.snippet.php'); ?>
		<ul class="posts">
		<?php if(!empty($post)): ?>
			<li class="single-post" data-post-id="<?= $post['id'] ?>">
			<?php if($action == 'edit'): ?>
				<form action="" method="post" class="edit">
					<input type="hidden" name="action" value="edit" />
					<input type="hidden" name="id" value="<?= $post['id'] ?>" />

					<textarea name="content" maxlength="<?= $config['max_characters'] ?>"><?= $post['post_content'] ?></textarea>

					<div class="post-nav">
						<!--<label id="post-attachments-label">Add Files<input type="file" multiple="multiple" name="attachments[]" id="post-attachments" accept="image/*" /></label>
						<div id="post-droparea" class="hidden">Add Files</div>-->
						<ul id="post-attachments-list">
							<?php
								$attachments = db_get_attached_files($post['id']);
							?>
							<?php if(!empty($attachments) && !empty($attachments[$post['id']])): ?>
								<?php foreach($attachments[$post['id']] as $a): ?>
									<?php if(strpos($a['file_mime_type'], 'image') === 0): ?>
									<?php
										$abs = ROOT.DS.get_file_path($a);
										list($width, $height, $_, $size_string) = getimagesize($abs);
										$url = $config['url'] .'/'. get_file_path($a);
									?>
										<li>
											<label>
												<input type="checkbox" name="attachments[]" value="<?= $a['id'] ?>" checked /> 
												<img class="file-preview" src="<?= $url ?>" alt="<?= $a['file_original'] ?>" <?= $size_string ?> loading="lazy" />
												<?= $a['file_original'] ?>
											</label>
										</li>
								<?php else: ?>
								<?php endif; ?>
								<?php endforeach; ?>
							<?php endif; ?>
						</ul>
						<p id="count"><?= $config['max_characters'] ?></p>
						<input type="submit" class="button" value="Update this post" />
					</div>
				</form>
			<?php else: ?>
				<?php
					$date = date_create();
					date_timestamp_set($date, $post['post_timestamp']);
					
					$datetime = date_format($date, 'Y-m-d H:i:s');
					$formatted_time = date_format($date, 'M d Y H:i');

					$attachments = db_get_attached_files($post['id']);
				?>
				<span class="post-timestamp">
					<time class="published" datetime="<?= $datetime ?>" data-unix-time="<?= $post['post_timestamp'] ?>"><?= $formatted_time ?></time>
					<?php if(is_numeric($post['post_edited']) && $config['show_edits']): ?>
					<time class="modified" datetime="<?= gmdate('Y-m-d\TH:i:s\Z', $post['post_edited']) ?>" data-unix-time="<?= $post['post_edited'] ?>">Edited on <?= date('M d Y H:i', $post['post_edited']) ?></time>
					<?php endif; ?>
				</span>
				<nav class="post-meta">
					<ul>
						<?php if($config['activitypub']):
							// todo: is it possible to retrieve this at the same time as post data?
							$post_stats = activitypub_get_post_stats('both', $post['id']);
						?>
						<li class="post-likes"><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/likes" title="This post has been liked <?= $post_stats['like'] ?> times in the Fediverse"><span class="amount"><?= $post_stats['like'] ?></span><span class="word">Likes</span></a></li>
						<li class="post-boosts"><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/boosts" title="This post has been announced <?= $post_stats['announce'] ?> times in the Fediverse"><span class="amount"><?= $post_stats['announce'] ?></span><span class="word">Boosts</span></a></li>
						<?php endif; ?>

						<?php if($config['logged_in']): ?>
						<?php if(is_numeric($post['post_deleted'])): ?>
						<li><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/undelete" title="Restore">Deleted on <?= date('M d Y', $post['post_deleted']) ?></a></li>
						<?php else: ?>
						<li><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/edit">Edit</a></li>
						<li><a href="<?= $config['url'] ?>/<?= $post['id'] ?>/delete">Delete</a></li>
						<?php endif; ?>
						<?php endif; ?>
					</ul>
				</nav>
				<div class="post-content"><?= nl2br(autolink($post['post_content'])) ?></div>
				<?php if(!empty($attachments) && !empty($attachments[$post['id']])): ?>
				<ul class="post-attachments">
					<?php foreach($attachments[$post['id']] as $a): ?>
					<li>
						<?php if(strpos($a['file_mime_type'], 'image') === 0): ?>
							<?php
								$abs = ROOT.DS.get_file_path($a);
								list($width, $height, $_, $size_string) = getimagesize($abs);
								$url = $config['url'] .'/'. get_file_path($a);
							?>
							<a href="<?= $url ?>">
								<picture>
									<source srcset="<?= $url ?>" type="image/jpeg" />
									<img src="<?= $url ?>" alt="<?= $a['file_original'] ?>" <?= $size_string ?> loading="lazy" />
								</picture>
							</a>
						<?php else: ?>
							<a href="<?= $url ?>" download="<?= $a['file_original'] ?>"><?= $a['file_original'] ?></a>
						<?php endif; ?>
					</li>
					<?php endforeach; ?>
				</ul>
				<?php endif; ?>
				<?php if($action == 'delete'): ?>
					<form action="" method="post" class="delete">
						<input type="hidden" name="action" value="delete" />
						<input type="hidden" name="id" value="<?= $post['id'] ?>" />
						<input type="submit" class="button alert" value="Delete this post" />
					</form>
					<?php if($error !== false): ?>
						<p class="message error"><?= $error ?></p>
					<?php endif; ?>
				<?php endif; ?>
			<?php endif; ?>
			</li>
		<?php else: ?>
			<p>No post with this ID.</p>
		<?php endif; ?>
		</ul>
	</div>
	<?php require(ROOT.DS.'snippets'.DS.'footer.snippet.php'); ?>
</body>
</html>