-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.php
280 lines (243 loc) · 7.87 KB
/
subscribe.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Gets user emails and subscription options
*
* @author Tatiana Braginets
* Random string f-n: http://stackoverflow.com/questions/853813/how-to-create-a-random-string-using-php
* Regex for email: http://www.regular-expressions.info/email.html
*/
header('content-type: text/html;charset=utf-8');
require_once 'connection.php';
$dbc = $GLOBALS['dbc'];
/**
* Creates random string of requested length
*@param int $length required length
*@param string $charset chars to choose from
*@return string random string
**/
function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
$str = '';
$count = strlen($charset);
while ($length--) {
$str .= $charset[mt_rand(0, $count-1)];
}
return $str;
}
/**
* Gets list of sites from db
* @global $dbc databse connection
* @return string sites
*/
function getSites()
{
global $dbc;
$que="SELECT DISTINCT site FROM course_data";
$result = $dbc->query($que);
$ret = "";
if($result) {
while($row = $result->fetch_array()){
$site = "" . $row['site'];
$checked = "";
if (isset($_REQUEST['sites'])
&& (array_search($site, $_REQUEST['sites']) !== FALSE)) {
$checked = "checked";
}
$ret .= '<label><input name="sites[]" type="checkbox" value="'
. $site . '" '. $checked . '> ' . $site .'</label><br>';
}
}
return $ret;
}
/**
* Checks the form and returns errors
* @global $dbc databse connection
* @return string errors
*/
function getFormErrors() {
$errors = "";
global $dbc;
if (!isset($_REQUEST['email'])
|| !preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/",
$dbc->real_escape_string($_REQUEST['email']))) {
$errors .= '<p class="text-warning">Please enter a valid email</p>';
}
if (!isset($_REQUEST['sites'])) {
$errors .= '<p class="text-warning">At least one site must be checked</p>';
}
return $errors;
}
?>
<!DOCTYPE html>
<head>
<title>Kazoom - Email subscription</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="css/style.css" type="text/css">
<style type="text/css">
body {
padding-top: 10px;
padding-bottom: 10px;
}
.subscribe-holder {
max-width: 500px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
background-color: #efefef;
box-shadow: 2px 3px 9px 2px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<h3 ><a class="muted" href="index.php">KaZoom</a></h3>
</div>
<hr class="gradientHr">
<div class="subscribe-holder">
<h4 class="subscribe-holder-heading">Subscribe to new course notifications</h4>
<p>We'll send you an email whenever we find new courses.</p>
<hr class="gradientHr">
<?php
$errors = '';
if (isset($_REQUEST['subscribe']) && ($errors = getFormErrors()) === "") {
$email = $dbc->real_escape_string($_REQUEST['email']);
//echo $email;
//$email = 'tbraginets@gmail.com';
$freq = $dbc->real_escape_string($_REQUEST['freq']);
$sites = array();
foreach($_REQUEST['sites'] as $site) {
$sites[] = $dbc->real_escape_string($site);
}
$site_options = implode(',', $sites);
//echo $site_options;
$subject = 'Confirm you subscription to Kazoom';
$headers = 'Reply-To: noreply@box334.bluehost.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$que="SELECT email, verified, rand_key, max FROM subscription_emails WHERE 1 AND email='$email'";
$result = $dbc->query($que);
if($result && $result->num_rows) {
$row = $result->fetch_array();
$verified = $row['verified'];
if ($verified) {
echo "You are already subscribed to notifications.";
} else {
$max = $row['max'];
if ($max == 3) {
echo $email + " You have reached maximum number of confirmation emails sent.
Please, make sure you are entering a correct email address and check your spam folder,
or contact site administrator for assistance.";
} else {
$max++;
$que="UPDATE subscription_emails SET max='$max'
WHERE email = '$email'";
$dbc->query($que);
$key = $row['rand_key'];
$link = 'http://www.sjsu-cs.org/cs160/spring2013/sec1group1/verifyEmail.php?email='
. $email . '&key=' . $key;
$message = 'Please confirm you subscription to Kazoom updates by following the link: ' . "\r\n\n";
$message .= $link;
$message .= " \r\n\nIf you do not recognize this subscription, please disregard this email.";
mail($email, $subject, $message, $headers);
echo "You have already subscribed to notifications, but have not verified your email address.
A new confirmation email have been sent. Please, click confirmation link in it to
verify your email address.";
}
}
} else {
$key = randString(50);
$que ="INSERT INTO subscription_emails (email, rand_key, verified, frequency, date_added, sites, max)
VALUES ('$email', '$key', '0', '$freq', now(), '$site_options', '1');";
$dbc->query($que) or die($dbc->error);
$link = 'http://www.sjsu-cs.org/cs160/spring2013/sec1group1/verifyEmail.php?email='
. $email . '&key=' . $key;
$message = 'Please confirm you subscription to Kazoom updates by following the link: ' . "\r\n\n";
$message .= $link;
$message .= " \r\n\nIf you do not recognize this subscription, please disregard this email.";
mail($email, $subject, $message, $headers);
echo "Confirmation email has been sent. Please, click confirmation link in it to verify your email address.";
}
?>
</div>
<footer>
<hr class="gradientHr">
<p>© San Jose State University</p>
</footer>
</div>
</body>
</html>
<?php exit();
} else if (isset($_REQUEST['subscribe'])) {
echo $errors;
} ?>
<form class="form-horizontal" method="post" action="subscribe.php" onsubmit="return validateForm();">
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input type="text" id="email" name="email" value="<?php echo @$_REQUEST['email'] ?>">
<p class="text-error" id="email_error"></p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="sites">Site(s)</label>
<div class="controls">
<?php echo getSites(); ?>
<p class="text-error" id="sites_error"></p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="freq">Frequency</label>
<div class="controls">
<select name="freq">
<option id="w" value="w"
<?php if(@$_REQUEST['freq'] === "w") echo "selected"; ?>>Weekly</option>
<option id="d" value="d"
<?php if(@$_REQUEST['freq'] === "d") echo "selected"; ?>>Daily</option>
</select>
</div>
</div>
<div class="controls">
<input class="btn btn-success" type="submit" name="subscribe" value="Subscribe">
</div>
</form>
</div>
<footer>
<hr class="gradientHr">
<p>© San Jose State University</p>
</footer>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
/**
* Validates form
* @returns {Boolean} true if form is valid, false otherwise
*/
function validateForm()
{
var success = true;
$('#email_error').html("");
$('#sites_error').html("");
var email = $('#email').val();
var reg = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/;
if (!email || !reg.test(email) ) {
$('#email_error').html("Please, enter a valid email");
success = false;
}
if (!countChecked()) {
$('#sites_error').html("At least one site must be checked");
success = false;
}
return success;
}
var countChecked = function() {
return $("input:checked").length;
};
</script>
</body>
</html>