forked from fgiobergia/RiDi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
277 lines (269 loc) · 9.22 KB
/
index.php
File metadata and controls
277 lines (269 loc) · 9.22 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<html>
<head>
<link rel="stylesheet" type="text/css" href="/css/RiDi.css">
<title>
Rivoluzione Digitale ~ Elenco argomenti
</title>
</head>
<body>
<center>
<?php
/*
*
* http://darkjoker.voidsec.com
*
* RiDi ~ Piattaforma per Rivoluzione Digitale
* Copyright (C) 2013 Flavio 'darkjoker' Giobergia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
error_reporting (E_ALL ^ E_NOTICE);
include "mysql.php";
function message ($message, $k, $die) {
$kind = ($k) ? 'error' : 'info';
echo "<div class = '{$kind}'><div class = 'msg'>{$message}</div></div>";
if ($die) {
die ("</center></body></html>");
}
}
/* Il cookie viene salvato come
* id_md5(username:password)
*/
$data = $_COOKIE['data'];
if (empty ($data)) {
$username = mysql_real_escape_string ($_POST['username']);
$password = md5 ($_POST['password']);
if (!empty ($username) && !empty ($password)) {
$query = "SELECT id FROM users WHERE (username = '{$username}' AND password = '{$password}')";
$row = mysql_fetch_row (mysql_query ($query));
if (!empty ($row[0])) {
$data = "{$row[0]}_".md5("{$username}:{$password}");
setcookie ("data",$data);
}
else {
message ("Login fallito. <a href = 'index.php'>Riprova</a>",1,1);
}
}
else {
if (isset ($_GET['register'])) {
$user = mysql_real_escape_string ($_POST['uname']);
$pass = md5 ($_POST['pword']);
$mail = $_POST['email'];
if (!empty ($user) && !empty ($pass) && preg_match ("|^[a-z0-9\._]{1,}@[a-z0-9\.]{1,}\.[a-z]{2,4}$|",$mail)) {
// Si controlla che non ci sia un utente con la stessa password
$query = "SELECT id FROM users WHERE username = '{$user}' OR email = '{$mail}'";
$row = mysql_fetch_row (mysql_query ($query));
if (!empty ($row[0])) {
// Username/mail gia' in uso
message ("Username/email gia' in uso. <a href = 'index.php'>Riprova</a>",1,1);
}
$query = "INSERT INTO users (username, password, email) VALUES ('{$user}','{$pass}','{$mail}')";
mysql_query ($query);
message ("Registrazione effettuata. Procedi al <a href = 'index.php'>login</a> :)",0,1);
}
?>
<form method = 'POST'>
<b>Registrati</b>
<table>
<tr><td>Username:</td><td><input name = 'uname'></td></tr>
<tr><td>Password:</td><td><input name = 'pword' type = 'password'></td></tr>
<tr><td>Email:</td><td><input name = 'email'></td></tr>
<tr><td><input type = 'submit' value = 'Registrati'></td></tr>
</table>
<a href = 'index.php'>Login</a>
</form>
<?php
}
else {
?>
<form method = 'POST'>
<table>
<tr><td>Username:</td><td><input name = 'username'></td></tr>
<tr><td>Password:</td><td><input name = 'password' type = 'password'></td></tr>
<tr><td><input type = 'submit' value = 'Login'></td></tr>
</table>
<a href = 'index.php?register'>Registrati</a>
</form>
<?php
}
}
/* Parte di login - registrazione */
}
// Non uso l'else perche', in questo modo, una volta effettuato
// il login non si dovra' ricaricare la pagina
if (!empty ($data)) {
if (!preg_match ("|^([0-9]{1,})_([a-f0-9]{32})$|",$data,$info)) {
message ("Si è verificato un errore con i cookie!",1,1);
}
array_shift($info);
list ($id,$hash) = $info; // Controlli su $id per SQLi non necessari, visto che arrivano da un preg_match()
$query = "SELECT username,password FROM users WHERE id = '{$id}'";
$row = mysql_fetch_row (mysql_query ($query));
if (empty ($row[0]) || md5("{$row[0]}:{$row[1]}")!=$hash) {
message ("Dai, almeno ci hai provato :)",1,1);
}
?>
<a href = 'index.php'>Pagina principale</a><pre>
</pre>
<?php
// A seconda di cosa si e' scelto di fare, viene visualizzata la relativa pagina
if (isset($_GET['new'])) {
$title = trim(mysql_real_escape_string (utf8_encode($_POST['title'])));
$descr = trim(mysql_real_escape_string (utf8_encode($_POST['descr'])));
if (!empty ($title) && !empty($descr)) {
$query = "INSERT INTO topics (title,description,author) VALUES ('{$title}','{$descr}',{$id})";
mysql_query ($query);
message ("Argomento pubblicato.",0,1);
}
else {
?>
<form method = 'POST'>
<b>Pubblica un nuovo argomento</b><br>
Titolo: <input name = 'title'><br>
<textarea name = 'descr' style = 'width: 500px; height: 300px;'>Descrizione</textarea><br>
<input type = 'submit' value = 'Invia'>
</form>
<?php
}
}
// Per iscriversi ad un argomento
else if (isset($_GET['join'])) {
$tid = intval($_GET['tid']);
$query = "SELECT title FROM topics WHERE id = '{$tid}'";
$row = mysql_fetch_row (mysql_query ($query));
if ($row[0]) {
// Esiste
// Si controlla che non sia gia' registrato
$query = "SELECT author FROM topics WHERE id = {$tid}";
$row = mysql_fetch_row(mysql_query($query));
if ($row[0]==$id) {
message ("Stai provando ad iscriverti ad un argomento che hai creato!",1,1);
}
$query = "SELECT id FROM selected WHERE uid = {$id} AND tid = {$tid}";
$row = mysql_fetch_row (mysql_query ($query));
if (!empty($row[0])) {
message ("Hai gia' dato la tua disponibilita' per questo corso.",1,1);
}
$query = "SELECT COUNT(*) FROM selected WHERE tid = {$tid}";
$row = mysql_fetch_row (mysql_query($query));
// Si confronta con 3, perche' il quarto e' l'utente
// che ha creato il gruppo
if ($row[0]==3) {
message ("L'argomento ha già raggiunto 4 partecipanti.",1,1);
}
$query = "INSERT INTO selected (uid,tid) VALUES ({$id},{$tid})";
mysql_query ($query);
message ("Registrato con successo all'argomento.",0,1);
}
else {
message ("Stai provando ad iscriverti ad un argomento inesistente.",1,1);
}
}
// Mostra un corso proposto
else if (isset($_GET['show'])) {
$tid = intval ($_GET['tid']);
$query = "SELECT * FROM topics WHERE id = {$tid}";
$row = mysql_fetch_row (mysql_query ($query));
if (empty ($row[0])) {
message ("Argomento inesistente.",1,1);
}
?>
<b>Titolo:</b> <?php echo htmlentities (utf8_decode($row[1])); ?><br>
<b>Descrizione:</b>
<div class = 'descr'>
<pre>
<?php echo htmlentities (wordwrap (utf8_decode($row[2]))); ?>
</pre>
</div>
<?php
$query = "SELECT uid FROM selected WHERE tid = {$tid} UNION SELECT author FROM topics WHERE id = {$tid}";
$res = mysql_query ($query);
$users = array ();
while ($row=mysql_fetch_row($res)) {
array_push ($users,$row[0]);
}
if (in_array ($id,$users)) {
// L'utente fa parte di chi ha dato
// disponibilita' per il corso, quindi
// gli vengono visualizzati gli altri utenti
echo "<pre>\n\n</pre><b>Utenti iscritti:</b><br>";
for ($i=0;$i<count($users);$i++) {
$query = "SELECT username,email FROM users WHERE id = {$users[$i]}";
$row = mysql_fetch_row (mysql_query($query));
echo "<a href = 'mailto:".htmlentities(utf8_decode($row[1]))."'>".htmlentities(utf8_decode($row[0]))."</a>";
if ($users[$i]==$id) {
echo " <b>[you]</b>";
}
echo "<br>";
}
echo "<pre>\n\n</pre><a href = 'index.php?leave&tid={$tid}'>Lascia questo argomento</a>";
}
else {
echo "<pre>\n\n</pre><a href = 'index.php?join&tid={$tid}'>Dai la tua disponibilità</a>";
}
}
else if (isset($_GET['leave'])) {
$tid = intval ($_GET['tid']);
$query = "DELETE FROM selected WHERE uid = {$id} AND tid = {$tid}";
mysql_query ($query);
// Se l'utente che ha creato il gruppo cerca di togliersi
// se c'e' qualcun'altro che ha dato la disponibilita' viene
// messo come creatore, altrimenti il gruppo viene cancellato
$query = "SELECT author FROM topics WHERE id = {$tid}";
$row = mysql_fetch_row (mysql_query ($query));
if ($row[0]==$id) {
$query = "SELECT uid FROM selected WHERE tid = {$tid}";
$row = mysql_fetch_row (mysql_query ($query));
if (!empty ($row[0])) {
$query = "UPDATE topics SET author = {$row[0]} WHERE id = {$tid}";
mysql_query ($query);
$query = "DELETE FROM selected WHERE uid = {$row[0]} AND tid = {$tid}";
mysql_query ($query);
}
else {
$query = "DELETE FROM topics WHERE id = {$tid}";
mysql_query ($query);
}
}
message ("Hai lasciato l'argomento.",0,1);
}
else if (isset($_GET['logout'])) {
setcookie ("data","");
header ("Location: index.php");
message ("Logout effettuato con successo!",0,1);
}
// Mostra l'elenco dei corsi
else {
echo "<a href = 'index.php?logout'>Logout</a><br>";
echo "<a href = 'index.php?new'>Proponi un argomento!" .
"</a><pre>\n\n</pre>";
echo "<b>Elenco argomenti:</b><br>\n";
$query = "SELECT id, title FROM topics";
$res = mysql_query($query);
while (($row = mysql_fetch_row($res))) {
$tid = intval($row[0]);
$title = htmlentities(utf8_decode($row[1]));
$query = "SELECT COUNT(*) FROM selected " .
"WHERE tid = {$tid}";
$count = intval(mysql_fetch_row(mysql_query($query)));
echo "<a href = 'index.php?show&tid={$row[0]}'>" .
$title . "</a> [{$count} iscritti]<br>";
}
}
}
?>
</center>
</body>
</html>