add.php 3.69 KB
Newer Older
w3challs's avatar
w3challs committed
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
<?php

$link = getPost('link');
$author = getPost('author');
$errors = array();
$count = 0;

define('MAX_AUTHORIZED_LINKS',3);

# Form submitted

function startsWith($string, $query)
{
	return substr($string, 0, strlen($query)) === $query;
}

if (!is_null($link) && !is_null($author) && checkToken('facepalm_anticsrf'))
{
	if (!filter_var($link, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED) ||
	    !(startsWith($link, 'http://') || startsWith($link, 'https://')))
	{
		$errors[] = invalidLink;
	}

	$len = strlen($author);
	if (!preg_match('/^[a-zA-Z0-9]{3,20}$/', $author)) {
		$errors[] = invalidAuthor;
	}

	$antiflood = getSession('facepalm_antiflood');
	$count = count($errors);

	if (!$count && !is_null($antiflood) && $antiflood > time() - 120) {
		$errors[] = waitABitDude;
	} elseif (!$count) {
		setSession('facepalm_antiflood', time());
	}

	if (!$count)
	{
		# Check there is no flood

		$query = $db->prepare("
			SELECT COUNT(*) AS count
			FROM facepalm_proposal
			WHERE ip=:ip AND status='wait'"
		);

		$query->bindParam(':ip', getIp(), PDO::PARAM_STR);
		$query->execute();

		$row = $query->fetch(PDO::FETCH_ASSOC);

		if ($row['count'] >= MAX_AUTHORIZED_LINKS)
		{
			$errors[] = waitForIt;
			$count++;
		}

		# Insert data into database

		if (!$count)
		{
			$query = $db->prepare('
				INSERT INTO facepalm_proposal (author, link, ip)
				VALUES (:author, :link, :ip)'
			);

			$query->bindParam(':author', $author, PDO::PARAM_STR);
			$query->bindParam(':link', $link, PDO::PARAM_STR);
			$query->bindParam(':ip', getIp(), PDO::PARAM_STR);

			try {
				$query->execute();
				print '<div class="center validated">'.successAddLink.'</div>';
			} catch (PDOException $e) {
				print '<div class="center fail">'.failAddLink.'</div>';
			}

		}
	}
}

if (is_null($link))
	$link = '';

if (is_null($author))
	$author = '';

print '<div class="center">';

# Last 15 proposed links (hide the link, just show author / status / date)
print '<h2>'.last15proposals.'</h2>';

$query = $db->prepare('
	SELECT author, date, status
	FROM facepalm_proposal
	ORDER BY date DESC
	LIMIT 15'
);
$query->execute();

$format = '<tr><td>%s</td><td>%s</td><td>%s</td></tr>';

print '<table class="centered center liststatus">';
printf($format,
       '<strong>'.author.'</strong>',
       '<strong>'.status.'</strong>',
       '<strong>Date</strong>'
);

while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
	printf($format,
	       safeHTML($row['author']),
	       constant($row['status']),
	       safeHTML($row['date'])
	);
}

print '</table>';

# Errors detected

if ($count)
{
	printf ('<div class="left error"><strong>Errors detected ( %d ):</strong></div>', $count);
	print '<ul class="left">';

	foreach ($errors as $error) {
		printf ('<li class="error">%s</li>', safeHTML($error));
	}

	print '</ul>';
}

# Display the form
print '<form method="post" action="" class="validate">';
printf('<input type="hidden" name="facepalm_anticsrf" value="%s" />', getToken('facepalm_anticsrf'));
print '<table class="centered left">';
printf('<tr><td><h2>%s</h2></td></tr>', addFacepalm);
printf('<tr>
	<td>
	<blockquote class="box">
	<table>
		<tr>
		<tr>
			<td><strong>%1$s:%4$s</strong></td>
			<td><input type="text" name="link" placeholder="http://yoursite/facepalm.jpg" value="%5$s" size="42" class="required" /></td>
		</tr>
		<tr>
			<td><strong>%2$s:%4$s</strong></td>
			<td><input type="text" name="author" value="%6$s" size="15" class="required" /></td>
		</tr>
		<tr>
			<td colspan="2" class="center"><br /><br /><input type="submit" value="%3$s" /></td>
	</table>
	</blockquote>
	</td>
	</tr>',
	link, yourNick, addFacepalm,
	mandatory,
	safeHTML($link),
	safeHTML($author)
);

print '</table>';
print '</form>';
print '</div>';

?>