-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.php
More file actions
228 lines (216 loc) · 5.58 KB
/
backend.php
File metadata and controls
228 lines (216 loc) · 5.58 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
<?php
// Mini docs.
/*
Home made error codes:
1 == no errors.
2 == username already exits.
3 == wrong username or password
*/
// Connect to the server, no problem.
$hostname = "host";
$username = "username";
$password = "pass";
$dabebase = "db";
mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dabebase) or die(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS notes_users(
id INT AUTO_INCREMENT,
username TEXT,
password TEXT,
guid INT,
PRIMARY KEY(id)
)") OR DIE(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS notes_notes(
id INT AUTO_INCREMENT,
username TEXT,
guid TEXT,
note TEXT,
PRIMARY KEY(id)
)") OR DIE(mysql_error());
mysql_query("CREATE TABLE IF NOT EXISTS notes_session(
id INT AUTO_INCREMENT,
username TEXT,
sid TEXT,
userAgent TEXT,
PRIMARY KEY(id)
)") OR DIE(mysql_error());
include 'functions.php';
require('pusher.php');
// First i handle the case, where we create a new user.
if ( $_POST["createUser"] )
{
$user = $_POST["createUser"];
$user = mysql_real_escape_string(stripslashes($user));
$user = mysql_real_escape_string($user);
$remember = $_POST['remember']; // remember either == "true" or something else.
// First i check if someone already used that name.
$result = mysql_query("SELECT * FROM notes_users WHERE username='$user'") OR DIE(mysql_error());
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// There cant be any.
if($count==0){
// We continue
$password = $_POST["password"];
$password = stripslashes($password);
$password = mysql_real_escape_string($password);
$password = sha1($password);
mysql_query("INSERT INTO notes_users (username, password, guid) VALUES ('$user', '$password', '0')") OR DIE(mysql_error());
// Then we need to log in.
login($user, $remember);
echo '1';// 1 == no errors.
}
else {
echo "2";// 2 == username already exits.
}
}
// If the user tries to log in.
if ( $_POST["login"] )
{
// username and password sent from form
$user=$_POST['login'];
$password = $_POST['password'];
$password = sha1($password);
$remember = $_POST['remember'];
// To protect MySQL injection (more detail about MySQL injection)
$user = stripslashes($user);
$user = mysql_real_escape_string($user);
$result = mysql_query("SELECT password FROM notes_users WHERE username='$user'") OR DIE(mysql_error());
while($row = mysql_fetch_assoc($result)) //Lav en while der kører alle rækker igennem
{
$MySQLPassWord = $row['password'];
}
// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
// If result matched $user and $password, table row must be 1 row
if($count == 1){
if ($password == $MySQLPassWord)
{
// Remeber it.
if (login($user, $remember))
{
echo '1';// 1 == no errors. '
}
else
{
echo '7'; // Plain unkown error, this shouldn't happen.
}
}
else {
echo '3';// 3 == wrong username or password.
}
}
else {
echo '3';// 3 == wrong username or password.
}
}
// I just ask.
if ( $_POST["Checklogin"] )
{
$user = loginCheck();
if ($user)
{
echo $user;
}
else
{
echo '3';
}
}
if ($_POST["newNote"])
{
$user = loginCheck();
if ($user)
{
$query = mysql_query("SELECT guid FROM notes_users WHERE username = '$user'") OR DIE(mysql_error());
while($row = mysql_fetch_assoc($query)) //Lav en while der kører alle rækker igennem
{
$guid = $row['guid'] + 1;
echo '1' . $guid;
}
mysql_query("UPDATE notes_users SET guid = '$guid' WHERE username = '$user'") OR DIE(mysql_error());
// Now comes the good part. Actually inserting the new note.
mysql_query("INSERT INTO notes_notes (username, guid, note) VALUES ('$user', '$guid', '')") OR DIE(mysql_error());
}
else
{
echo '3';
}
}
if ( $_POST["logout"] )
{
// Simple, effective.
$ID = $_COOKIE['ToDoSID'];
mysql_query("DELETE FROM notes_session WHERE sid='$ID'");
$pastdate = mktime(0,0,0,2,2,1970);
setcookie("ToDoSID","",$pastdate, '/', 'webbies.dk');
echo '1';// 1 == no errors.
}
//mysql_query("INSERT INTO notes_users (content, deleted) VALUES ('Anders', 1)") OR DIE(mysql_error());
if ( $_POST["save"] )
{
$user = loginCheck();
$id = $_POST["save"];
$content = $_POST["content"];
$content = str_replace("\\", "", $content);
$content = mysql_real_escape_string(stripslashes($content));
if ($user)
{
$foundRow = false;
$query = mysql_query("SELECT * FROM notes_notes WHERE guid = '$id' AND username = '$user'") OR DIE(mysql_error());
while($row = mysql_fetch_assoc($query)) //Lav en while der kører alle rækker igennem
{
$foundRow = true;
}
if (!$foundRow) {
echo "4";
}
else {
mysql_query("UPDATE notes_notes SET note = '$content' WHERE guid = '$id' AND username = '$user'") OR DIE(mysql_error());
echo '1';
$data[$id] = $_POST["content"];
$data = json_encode($data);
pusher_sync($data);
}
}
else
{
echo '3';
}
}
if ( $_POST["deleteNote"] )
{
$user = loginCheck();
$id = $_POST["deleteNote"];
$content = $_POST["content"];
if ($user)
{
mysql_query("DELETE FROM notes_notes WHERE guid = '$id' AND username = '$user'") OR DIE(mysql_error());
echo '1';
$data['delete'] = $id;
$data = json_encode($data);
pusher_sync($data);
}
else
{
echo '3';
}
}
if ( $_POST["get"] )
{
$user = loginCheck();
if ($user)
{
$query = mysql_query("SELECT * FROM notes_notes WHERE username = '$user'") OR DIE(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($query)) //Lav en while der kører alle rækker igennem
{
$data[$row['guid']] = $row['note'];
}
echo json_encode($data);
}
else
{
echo '3';
}
}
?>