aboutsummaryrefslogtreecommitdiff
path: root/postform.inc.php
blob: 417718c4c2e11c582046c6feb696ed7e428eab82 (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
<?php
	if(!defined('ROOT')) die('Don\'t call this directly.');

	// check user credentials
	if(isset($_COOKIE['microblog_login']) && $_COOKIE['microblog_login'] === sha1($config['url'].$config['admin_pass'])) {
		// correct auth data, extend cookie life
		$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
		setcookie('microblog_login', sha1($config['url'].$config['admin_pass']), NOW+$config['cookie_life'], '/', $domain, false);
	} else {
		// wrong data, kick user to login page
		header('HTTP/1.0 401 Unauthorized');
		header('Location: '.$config['url'].'/login');
		die();
	}

	header('Content-Type: text/html; charset=utf-8');

	$message = array();
	if(!empty($_POST['message'])) {

		$id = db_insert($_POST['message'], NOW);

		if($id > 0) {
			$message = array(
				'status' => 'success',
				'message' => 'Successfully posted status #'.$id
			);

			rebuild_feed();
			if($config['ping'] == true) ping_microblog();
			if($config['crosspost_to_twitter'] == true) {
				$twitter_response = json_decode(twitter_post_status($_POST['message']), true);

				if(!empty($twitter_response['errors'])) {
					$message['message'] .= ' (But crossposting to twitter failed!)';
				}
			}
		}
	}

?><!DOCTYPE html>
<html lang="<?= $config['language'] ?>" class="postform">
<head>
	<title>micro.blog</title>
	<meta name="viewport" content="width=device-width" />
	<link rel="stylesheet" href="<?= $config['url'] ?>/microblog.css" />
</head>
<body>
	<div class="wrap">
		<nav>
			<ul>
				<li><a href="<?= $config['url'] ?>/">Timeline</a></li>
				<li><a href="<?= $config['url'] ?>/new">New Status</a></li>
			</ul>
		</nav>
		<?php if(isset($message['status']) && isset($message['message'])): ?>
		<p class="message <?= $message['status'] ?>"><?= $message['message'] ?></p>
		<?php endif; ?>
		<form action="" method="post">
			<textarea name="message" maxlength="<?= $config['max_characters'] ?>"></textarea>
			<p id="count"><?= $config['max_characters'] ?></p>
			<input type="submit" name="" value="Post" />
		</form>
	</div>

	<script>
		document.addEventListener('DOMContentLoaded', function() {
			var textarea = document.querySelector('textarea[name="message"]');
			var charCount = document.querySelector('#count');
			var maxCount = textarea.getAttribute('maxlength');

			charCount.innerHTML = maxCount;

			textarea.addEventListener('input', function() {
				// todo: this should probably respect http://blog.jonnew.com/posts/poo-dot-length-equals-two
				var textLength = this.value.length;

				charCount.innerHTML = parseInt(textarea.getAttribute('maxlength')) - this.value.length;
			}, false)
		});
	</script>
</body>
</html>