-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewpost.php
More file actions
174 lines (153 loc) · 4.64 KB
/
newpost.php
File metadata and controls
174 lines (153 loc) · 4.64 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
<?php
session_start();
$display_form = true;
if (!isset($_SESSION['user'])) {
// check if the user is responding to login form
$user = $_POST['user'];
$password = $_POST['password'];
if (isset($user)) {
if (isvalid($user, $password)) {
// the user logged in - no need to display form
$_SESSION['user'] = $user;
$display_form = false;
}
}
} else {
// returning user - no need to display the form
$display_form = false;
}
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Comments</title>
<link rel="stylesheet" type="text/css" href="forum.css" />
</head>
<body>
<?php
if ($display_form) {
print "<div id='info'><h3>You are not authorized to post.<h3><br/>\n";
print "<p>Please go back to the main page.<br/>\n";
print "</p></div>";
}else{
$name = $_POST["name"];
$post_subject = $_POST["post_subject"];
$post_content = $_POST["post_content"];
$submit = $_POST["submit"];
$namelookup = sprintf("SELECT user_login, ID FROM wp_users WHERE user_login LIKE '%s'", $name);
// connect to the server
$connection = mysql_connect("localhost","team8readwrite","5zYuteam8");
// select a database
mysql_select_db("1101sp09team8", $connection);
function showerror()
{
die("Error ". mysql_errno(). " : " .mysql_error());
}
function display_form($style1 = "", $style2 = "", $style3 = "", $name_value="", $postsubject_value = "", $postbody_value = "") {
print "<div id='info'>
<h3>New Post:</h3>
<form method=\"post\"
action=\"post.php\">
<table style='border: none'>
<tr>
<td $style1>
Username:
</td>
<td>
<input type='text' name='name' $name_value style='width: 30' />
</td>
</tr>
<tr>
<td $style2>
Post Subject:
</td>
<td>
<input type='text' name='post_subject' $postsubject_value style='width: 30' />
</td>
</tr>
<tr>
<td $style3>Post Body:</td>
<td><textarea rows='5' cols='50' name='post_content' $postbody_value>
</textarea>
</td>
</tr>
<tr>
<td>
<input type=\"submit\" name = \"submit\" value=\"submit\" />
</td>
</tr>
</table>
</form>
</div>";
}
$style1 = "";
$style2 = "";
$style3 = "";
$error_style = "style = \"color: red\"";
$message = "";
$name_value = "";
$postsubject_value = "";
$postbody_value = "";
$errors = false;
$comment_count=0;
$post_type = 'post';
$post_status = 'publish';
//Username no more than 10 characters long, only letters, digits, and underscores
$usernametest = "^[a-zA-Z0-9_]{1,10}$";
//Subject with length limitation, more characters
$postsubjecttest = "^[a-z0-9_.:;<> \"]{1,50}$";
//Post body length limitation, no form tag or more than three anchor tags
$postbodytest = "^[a-zA-Z0-9_.:;<>]{1,10000}";
$postbodytest2 = "[<a>]{0,3}";
$postbodytest3 = "[^<form>]]";
if (!isset($submit)) {
display_form(); // display the form
}
else {
// start validation
if(!eregi($usernametest, $name) || !$result = mysql_query($namelookup)){
$style1 = $error_style;
$message = $message."The name is invalid.<br/>";
$errors = true;
} else {
$name_value = "value = \"$name\"";
$row = mysql_fetch_array($result);
$userid = $row['ID'];
}
if(!eregi($postsubjecttest, $post_subject)){
$style2 = $error_style;
$message = $message."The post subject is invalid.<br/>";
$errors = true;
} else {
$postsubject_value = "value = \"$post_subject\"";
}
if(!eregi($postbodytest1, $post_body) && !eregi($postbodytest2, $post_body) && !eregi($postbodytest3, $post_body)){
$style3 = $error_style;
$message = $message."The post body is invalid.<br/>";
$errors = true;
} else {
$postbody_value = "value = \"$post_body\"";
}
if (!$errors) {
$password = "5zYuteam8";
if (! ($connection = @mysql_connect("localhost","team8readwrite",$password)))
die ("connection to the database failed");
if (!@mysql_select_db("1101sp09team8", $connection)) showerror();
$insert_q1 = "INSERT INTO wp_posts (post_title, post_author, post_date, post_content, post_type, post_status, comment_count) VALUES ('$post_subject', '$userid', NOW(), '$post_content', '$post_type', '$post_status', '$comment_count');";
if (! (@mysql_query($insert_q1, $connection))) {
showerror();
}
print "Thank you, $name, for submitting your information!<br/>\n";
} else {
// priniting the form
display_form($style1, $style2, $style3, $name_value, $postsubject_value, $postbody_value, $alertname, $alertsubject, $alertbody);
// printing the error message
print "<p $error_style>$message You need to resubmit the form</p>";
}
}}
?>
</body>
</html>