forked from Kandepet/HackerNews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_story.php
164 lines (140 loc) · 5.19 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
<?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;
}
if($_SESSION['hn_login']['name'] != 'maosea0125' && $_SESSION['hn_login']['name'] != 'jmao'){
header("Location: index.php");
}
// 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);'));
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'];
}
if(!isset($_POST['submit']) || empty($_POST['submit'])){
$twig = new HackerNews\TwigEngine($config);
$vars = array(
'loggedin' => $loggedin,
'username' => $_SESSION['hn_login']['name'],
'userid' => $_SESSION['hn_login']['id'],
);
echo $twig->setVars($vars)->render('new_story');
exit;
}
$_POST['story_title'] = strip_tags($_POST['story_title']);
$_POST['story_desc'] = strip_tags($_POST['story_desc']);
// Check title & url & desc
$errors = 0;
if(strlen($_POST['story_title']) < 4) {
$errors++;
$titleError = "文章标题太短啦,最少4个字符";
}
if(empty($_POST['story_desc']) || (strlen($_POST['story_desc']) < 10)) {
$errors++;
$descError = "描述太短啦,最少10个字符";
}
if(!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $_POST['story_url'])) {
$errors++;
$urlError = "请输入有效的URL地址";
}
$storyUrls = parse_url($_POST['story_url']);
if($storyUrls['hostname'] != 'mp.weixin.qq.com'){
$urlError = "请输入微信的URL地址";
}
if($errors != 0){
$twig = new HackerNews\TwigEngine($config);
$vars = 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'],
'title_error' => $titleError,
'url_error' => $urlError,
'desc_error' => $descError,
);
echo $twig->setVars($vars)->render('new_story');
exit;
}
// validate duplicate
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;
}
}
$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);