forked from hultqvist/Comments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.php
186 lines (166 loc) · 5.79 KB
/
shared.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
<?php
//Database and service parameters
require_once("config.php");
function PrintComment($site, $row, $session = false)
{
if($row['VerifiedDate'] === null)
echo '<li id="comment'.$row['CommentID'].'" class="unverified">';
else
echo '<li id="comment'.$row['CommentID'].'">';
echo '<div class="commentAuthor"><img src="https://secure.gravatar.com/avatar/'.md5(strtolower(trim($row['CommentEmail']))).'?s=40&d=identicon">';
if(isset($row['Page']))
{
//$url=htmlentities($site['SiteUrl'].$row['Page']);
//echo '<div><a href="'.$url.'">'.$url.'</a></div>';
}
echo '<span>'.date('Y-m-d H:i', strtotime($row['CommentDate'])).'</span> ';
if($session && ($session['Email'] === $site['AdminEmail'] || $session['Email'] === $row['CommentEmail']))
{
if($row['CommentEmail'] === "")
echo '<strong>Anonymous</strong>';
elseif($session['Email'] != $row['CommentEmail'])
echo htmlentities($row['CommentEmail']);
if($row['VerifiedDate'] === null)
{
echo ' <em>('.htmlentities($row['CommentIP']).')</em>';
echo ' <strong>(unverified)</strong>';
echo ' <a href="'.service_url.'/update.php?cid='.$row['CommentID'].'&action=verify">verify</a>';
echo ' <a href="'.service_url.'/update.php?cid='.$row['CommentID'].'&action=delete">delete</a>';
}
elseif($session['Email'] === $site['AdminEmail'])
echo ' <a href="'.service_url.'/update.php?cid='.$row['CommentID'].'&action=delete">delete</a>';
}
echo '</div>';
echo Markdown($row['CommentText']);
echo '</li>';
}
function PrintLink($site, $row, $session = false)
{
echo '<li class="unverified">';
echo '<div class="commentAuthor">';
$url=htmlentities($row['Referer']);
echo '<div>Referer: <a href="'.$url.'">'.$url.'</a></div>';
$url=htmlentities($site['SiteUrl'].$row['Page']);
echo '<div>Page: <a href="'.$url.'">'.$url.'</a></div>';
if($session && $session['Email'] === $site['AdminEmail'])
{
echo ' <a href="'.service_url.'/update.php?cid='.$row['LinkID'].'&action=verify">verify</a>';
echo ' <a href="'.service_url.'/update.php?cid='.$row['LinkID'].'&action=delete">delete</a>';
}
echo '</div>';
echo '<p>Unique IP: '.htmlentities($row['Count']).'</p>';
echo '</li>';
}
//Page URL checks and normalization
function GetSiteConstants($sid, $checkReferer = TRUE)
{
//Page URL
if($checkReferer){
if(isset($_SERVER['HTTP_REFERER']))
$url = $_SERVER['HTTP_REFERER'];
else
{
define("urlError", 'Missing referer');
return;
}
if(isset($_GET['url']))
$url = $_GET['url'];
if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
{
define("urlError", 'Invalid url: '.htmlentities($url));
return;
}
}
//Verify pageUrl and referer
$res = @mysql_query('SELECT * FROM Sites WHERE SiteID='.$sid)
or die(mysql_error());
$row = mysql_fetch_assoc($res);
if($row === FALSE)
{
define("urlError", 'No site with sid: '.$sid);
return;
}
$siteUrl = parse_url(rtrim($row['SiteUrl'], "/"));
if($checkReferer){
$pageUrl = parse_url(rtrim($url, "/"));
if($pageUrl['host'] != $siteUrl['host'] || strpos($pageUrl['path'], $siteUrl['path']) !== 0)
{
define("urlError", 'Wrong url of page: '.htmlentities($url).' expected: '.htmlentities($row['SiteUrl']));
return;
}
$serviceUrl = parse_url(service_url);
$refUrl = parse_url($_SERVER['HTTP_REFERER']);
if($refUrl['host'] != $serviceUrl['host'] || strpos($refUrl['path'], $serviceUrl['path']) !== 0)
{
if($refUrl['host'] != $siteUrl['host'] || strpos($refUrl['path'], $siteUrl['path']) !== 0)
{
define("urlError", 'Wrong referer: '.htmlentities($_SERVER['HTTP_REFERER']).' expected: '.htmlentities($row['SiteUrl']));
return;
}
}
}
define("urlError", FALSE);
return $row;
}
function GenerateAndSendVerificationCode($email, $url)
{
$code = substr(sha1(time().rand().$email.$_SERVER['REMOTE_ADDR']), 0, 10);
@mysql_query('REPLACE Authors SET Email=\''.mysql_real_escape_String($email).'\', VerifyDate=NOW(), VerifyCode=\''.mysql_real_escape_String($code).'\'')
or die('<div class="commentError">'.mysql_error().'</div>');
//Email verification link
$mailed = mail($email,
'Verify your comment',
'To verify the comment you made on '.$url.'
Click here to login and review your comments:
'.service_url.'/auth.php?email='.urlencode($email).'&code='.$code,
'From: '.service_email)
or die('<div class="commentError">Failed to send verification email, try again</div>');
}
//Return the email address verified by session cookie
function GetSessionConstants()
{
$email = isset($_COOKIE['email'])? $_COOKIE['email'] : null;
$session = isset($_COOKIE['session'])? $_COOKIE['session'] : null;
$res = @mysql_query('SELECT * FROM Authors
WHERE Email=\''.mysql_real_escape_string($email).'\'
AND Session=\''.mysql_real_escape_string($session).'\'')
or die('<div class="commentError">'.mysql_error().'</div>');
if(mysql_num_rows($res) !== 1)
{
setcookie("session", null, time()-24*3600);
return NULL;
}
return mysql_fetch_assoc($res);
}
//Remove static pages
function UpdateComments($sid, $page){
//clean input
$page = trim(str_replace(array('/','?'), ' ', $page));
$page = str_replace(' ', ' ', $page);
//Remove static files
$path = 'inc/'.intval($sid).'/'.$page;
if(file_exists($path.'.html'))
unlink($path.'.html');
if(file_exists($path.'.xml'))
unlink($path.'.xml');
}
function LogReferer($site, $page)
{
if(!$site)
return;
if(!isset($_GET['ref']))
return;
$ref = $_GET['ref'];
if(filter_var($ref, FILTER_VALIDATE_URL) === FALSE)
return;
if(strpos($ref, $site['SiteUrl']) !== false) //Don't log internal links
return;
$res = @mysql_query('INSERT INTO Links (SiteID, Page, VisitorIP, Referer)
VALUES
('.$site['ID'].',
\''.mysql_real_escape_string($page).'\',
\''.mysql_real_escape_string($_SERVER['REMOTE_ADDR']).'\',
\''.mysql_real_escape_string($ref).'\'
)')
or die('<div class="commentError">STAT:'.mysql_error().'</div>');
}