forked from exitcas/pbpush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_url.php
82 lines (71 loc) · 2.15 KB
/
add_url.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
<?php
// TODO: Give a warning if the file is not found
$config = parse_ini_file('.config');
if ($config['debug_mode']) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
require_once('session.php');
if (!isset($_SESSION['valid_session'])) {
$secretKey = $config['totp_secret'];
$cookieVal = decodeCookie($secretKey);
if ($cookieVal === false) { # Valid cookie ?
header('Location: login.php');
exit();
}
}
if (isset($_POST['submit'])) {
$new_post = filter_input(INPUT_POST, 'new_post');
// Replace new lines for Line separator character (U+2028)
$new_post = str_replace("\n", "\u{2028}", $new_post);
$new_post = str_replace("\r", '', $new_post);
if ($new_post) {
// Check if we have a point to insert the next Twt
define('NEW_TWT_MARKER', "#~~~#\n");
$contents = file_get_contents($txt_file_path);
if (!date_default_timezone_set($timezone)) {
date_default_timezone_set('UTC');
}
$twt = date('c') . "\t$new_post\n";
if (strpos($contents, NEW_TWT_MARKER) !== false) {
// Add the previous marker
$twt = NEW_TWT_MARKER . $twt;
$contents = str_replace(NEW_TWT_MARKER, $twt, $contents);
} else {
$contents .= $twt;
}
// TODO: Add error handling if write to the file fails
// For example due to permissions problems
$file_write_result = file_put_contents($txt_file_path, $contents);
header('Refresh:0; url=.');
exit;
} else {
echo "Oops something went wrong...\n\nCheck the error_log in the server";
exit;
}
} else { ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>twtxt</title>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1><a href=".">twtxt</a></h1>
<form method="POST" class="column">
<div id="login">
<?php if ($invalidURL) { ?>
<div class="alert">URL is invalid, check it!</div><br>
<?php } ?>
<label for="fname">URL to twtxt.txt file</label>
<br>
<input type="text" id="url" name="url" class="input" autocomplete="off"><br>
<input type="submit" value="Add URL" class="btn">
</div>
</form>
</body>
</html>
<?php } ?>