forked from Kandepet/HackerNews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_story.php
223 lines (190 loc) · 8.17 KB
/
add_story.php
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
<?php
require 'vendor/autoload.php';
// Start the session
session_start();
$config = new HackerNews\Config('config.ini');
$base_url = $config['site.base_url'] . $config['site.base_path'] . '/';
$db = HackerNews\Common::database($config);
HackerNews\Common::logincheck('add_story.php');
$loggedin = 0;
if(isset($_SESSION['hn_login']['id'])) $loggedin = 1;
// Check if the user has a remember cookie set
HackerNews\Common::checkremember($config, $db);
array_walk($_POST, create_function('&$val', '$val = trim($val);'));
array_walk($_GET, create_function('&$val', '$val = trim($val);'));
$_POST['story_title'] = strip_tags($_POST['story_title']);
$_POST['story_desc'] = strip_tags($_POST['story_desc']);
if(isset($_GET['ref'])) {
if(isset($_SERVER['HTTP_REFERER'])) {
$_POST['story_url'] = $_SERVER['HTTP_REFERER'];
}
}
if(isset($_GET['story_url'])) {
$_POST['story_url'] = $_GET['story_url'];
}
// Check if a url has been submitted
if(isset($_POST['story_url']) && !empty($_POST['story_url'])) {
if(!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i',$_POST['story_url'])) {
$twig = new HackerNews\TwigEngine($config);
echo $twig->setVars(array(
'loggedin' => $loggedin,
'username' => $_SESSION['hn_login']['name'],
'userid' => $_SESSION['hn_login']['id'],
'story_url' => $_POST['story_url'],
'story_title' => $_POST['story_title'],
'story_desc' => $_POST['story_desc'],
'url_error' => "Please enter a valid URL"
))->render('new_story');
exit;
} else {
if(!isset($_POST['dupe'])) {
$db->where("story_url", HackerNews\Common::validate_input($_POST['story_url']));
$dup = $db->getOne("stories", Array("story_id"));
if($dup['story_id']) {
// Initiate stories array
$stories = array();
$db->where("story_url", HackerNews\Common::validate_input($_POST['story_url']));
$dup_stories = $db->get("stories", 10,
Array(
"story_id",
"story_title",
"story_desc",
"story_cat",
"story_votes",
"story_url",
"story_comments",
"user_name",
"user_id",
"story_time",
"story_tags"
));
foreach($dup_stories as $info)
{
$stories[$info['story_id']] = $info;
$stories[$info['story_id']]['voted'] = 0;
$stories[$info['story_id']]['ago'] = HackerNews\Common::time_taken((time()-$info['story_time']));
$stories[$info['story_id']]['domain'] = HackerNews\Common::getDomain($info['story_url']);
$stories[$info['story_id']]['story_link'] = $base_url.'story.php?id='.$info['story_id'];
$stories[$info['story_id']]['user_link'] = $base_url.'profile.php?id='.$info['user_id'];
}
$twig = new HackerNews\TwigEngine($config);
echo $twig->setVars(array(
'stories' => $stories,
'loggedin' => $loggedin,
'username' => $_SESSION['hn_login']['name'],
'userid' => $_SESSION['hn_login']['id'],
'dup_stories' => $stories,
'story_url' => $_POST['story_url'],
'story_title' => $_POST['story_title'],
'story_desc' => $_POST['story_desc']
))->render('new_story_dup');
exit;
}
}
// Initiate the variables
$title_error = '';
$desc_error = '';
if(isset($_POST['story_title']))
{
// Set the error count to 0
$errors = 0;
// Check the story tile isn't less than 4 chars long
if(strlen($_POST['story_title']) < 4) {
$errors++;
$title_error = "Title is too short. Please enter at least 4 characters";
}
// Check the story desciption isn't less than 10 chars long
if(!empty($_POST['story_desc']) && (strlen($_POST['story_desc']) < 10)) {
$errors++;
$desc_error = "Description is too small. Enter atleast 10 characters";
}
// Check that there are no errors
if($errors == 0) {
$story_details = Array(
"story_id" => "",
"user_id" => $_SESSION['hn_login']['id'],
"user_name" => $_SESSION['hn_login']['name'],
"story_url" => HackerNews\Common::validate_input($_POST['story_url']),
"story_title" => HackerNews\Common::validate_input($_POST['story_title']),
"story_desc" => HackerNews\Common::validate_input($_POST['story_desc']),
"story_cat" => '',
"story_score" => 9999,
"story_votes" => 1,
"story_time" => time(),
"story_tags" => ''
);
$story_id = $db->insert("stories", $story_details);
// Self-Vote
$vote_details = Array("story_id" => HackerNews\Common::validate_input($story_id), "user_id" => $_SESSION['hn_login']['id']);
$db->insert("votes", $vote_details);
// Redirect them to the story
header("Location: story.php?id=".$story_id);
exit;
} else {
$twig = new HackerNews\TwigEngine($config);
echo $twig->setVars(array(
'loggedin' => $loggedin,
'username' => $_SESSION['hn_login']['name'],
'userid' => $_SESSION['hn_login']['id'],
'story_url' => $_POST['story_url'],
'story_title' => $_POST['story_title'],
'story_desc' => $_POST['story_desc'],
'desc_error' => $desc_error,
'title_error' => $title_error
))->render('new_story');
}
}
}
} else if(isset($_POST['story_desc']) && !empty($_POST['story_desc'])) {
// Check the story tile isn't less than 4 chars long
if(strlen($_POST['story_title']) < 4) {
$errors++;
$title_error = "Title is too short. Please enter at least 4 characters";
}
if(!empty($_POST['story_desc']) && (strlen($_POST['story_desc']) < 10)) {
$errors++;
$desc_error = "Description is too small. Enter atleast 10 characters";
}
if($errors) {
$twig = new HackerNews\TwigEngine($config);
echo $twig->setVars(array(
'loggedin' => $loggedin,
'username' => $_SESSION['hn_login']['name'],
'userid' => $_SESSION['hn_login']['id'],
'story_url' => $_POST['story_url'],
'story_title' => $_POST['story_title'],
'story_desc' => $_POST['story_desc'],
'desc_error' => $desc_error,
'title_error' => $title_error
))->render('new_story');
} else {
$story_details = Array(
"story_id" => "",
"user_id" => $_SESSION['hn_login']['id'],
"user_name" => $_SESSION['hn_login']['name'],
"story_url" => HackerNews\Common::validate_input($_POST['story_url']),
"story_title" => HackerNews\Common::validate_input($_POST['story_title']),
"story_desc" => HackerNews\Common::validate_input($_POST['story_desc']),
"story_cat" => '',
"story_score" => 9999,
"story_votes" => 1,
"story_time" => time(),
"story_tags" => ''
);
$story_id = $db->insert("stories", $story_details);
// Self-Digg
$vote_details = Array("story_id" => HackerNews\Common::validate_input($story_id), "user_id" => $_SESSION['hn_login']['id']);
$db->insert("votes", $vote_details);
// Redirect them to the index
header("Location: story.php?id=".$story_id);
}
} else {
$twig = new HackerNews\TwigEngine($config);
echo $twig->setVars(array(
'stories' => $stories,
'loggedin' => $loggedin,
'username' => $_SESSION['hn_login']['name'],
'userid' => $_SESSION['hn_login']['id'],
))->render('new_story');
}
?>