-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist_funcs.php
196 lines (170 loc) · 5.44 KB
/
list_funcs.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
<?php
define('ANY_CAN_VIEW_LIST', true); /* whether everyone can view a list, or admins only */
define('BASE_URL', 'http://yuba.stanford.edu/github');
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '1cthychr0n0s');
define('DB_HOST', 'localhost');
function connect_to_db() {
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
@mysql_select_db(DB_NAME) or die( "Unable to select database");
}
function get_table() {
return 'github_email_lists';
}
function get_list_or_die() {
if(isset($_GET['list'])) {
return $_GET['list'];
}
else {
echo "Missing list name";
exit(0);
}
}
function list_email($list, $subj, $body) {
$to_addrs = list_get_active_to_addrs($list);
$to = '';
foreach($to_addrs as $a) {
$to .= "$a,";
}
$headers = "From: noreply@yuba.stanford.edu ($list List)\r\n";
return mail($to, $subj, $body, $headers);
}
function list_get_active_to_addrs($list) {
connect_to_db();
$table = get_table();
$result = mysql_query("SELECT email FROM $table WHERE list='$list' AND active='1'");
$addrs = array();
$i = 0;
while($row=mysql_fetch_array($result)) {
$addrs[$i++] = $row[0];
}
mysql_close();
return $addrs;
}
function list_get_active_to_addrs_as_string($list) {
$addrs = '';
foreach(list_get_active_to_addrs($list) as $addr)
$addrs .= "$addr,";
return $addrs;
}
function list_exists($list) {
connect_to_db();
$table = get_table();
$result = mysql_query("SELECT COUNT(*) FROM $table WHERE list='$list'");
$ret = mysql_fetch_array($result);
mysql_close();
$count = $ret[0];
return $count > 0;
}
function list_is_admin($list, $addr, $code) {
connect_to_db();
$table = get_table();
$result = mysql_query("SELECT COUNT(*) FROM $table WHERE list='$list' AND email='$addr' AND code='$code' AND admin='1'");
$ret = mysql_fetch_array($result);
mysql_close();
$count = $ret[0];
return $count > 0;
}
function list_is_subscribed($list, $addr, $code) {
connect_to_db();
$table = get_table();
$result = mysql_query("SELECT COUNT(*) FROM $table WHERE list='$list' AND email='$addr' AND code='$code'");
$ret = mysql_fetch_array($result);
mysql_close();
$count = $ret[0];
return $count > 0;
}
function list_add_to_addr($list, $addr, $admin) {
connect_to_db();
$table = get_table();
$result = mysql_query("SELECT COUNT(*) FROM $table WHERE list='$list' AND email='$addr'");
$row = mysql_fetch_array($result);
$count = $row[0];
if($count > 0) {
return false;
}
$code = md5(uniqid(rand(), true));
$ret = mysql_query("INSERT INTO $table (list, email, active, code, admin)
VALUES ('$list', '$addr', '0', '$code', '$admin')");
$status = mysql_affected_rows() > 0;
mysql_close();
if($status)
list_email_code_direct($list, $addr, $code, $admin==1, false);
return $status;
}
function list_verify_addr($list, $addr, $code) {
connect_to_db();
$table = get_table();
$ret = mysql_query("UPDATE $table SET active=1
WHERE list='$list' AND email='$addr' AND code='$code'
LIMIT 1");
$status = mysql_affected_rows() > 0;
mysql_close();
return $status;
}
function list_delete_addr($list, $addr, $code) {
connect_to_db();
$table = get_table();
$ret = mysql_query("DELETE FROM $table
WHERE list='$list' AND email='$addr' AND code='$code'
LIMIT 1");
$status = mysql_affected_rows() > 0;
mysql_close();
return $status;
}
function list_email_code($list, $addr) {
connect_to_db();
$table = get_table();
$result = mysql_query("SELECT code, admin, active FROM $table WHERE list='$list' AND email='$addr'");
if(mysql_numrows($result) > 0) {
$row = mysql_fetch_array($result);
$code = $row[0];
$admin = ($row[1] == 1);
$active = ($row[2] == 1);
list_email_code_direct($list, $addr, $code, $admin, $active);
}
else
$code = false;
mysql_close();
return $code;
}
function list_email_code_direct($list, $addr, $code, $admin, $active) {
$url = BASE_URL;
if($admin || ANY_CAN_VIEW_LIST)
$admin_txt = "View the list: $url/list.php?list=$list&addr=$addr&code=$code&action=view";
else
$admin_txt = '';
if(!$active)
$active_txt = "Verify that you want to join the list: $url/list.php?list=$list&addr=$addr&code=$code&action=verify";
else
$active_txt = '';
$body = <<<BODY
$admin_txt
$active_txt
Leave the list: $url/list.php?list=$list&addr=$addr&code=$code&action=leave
BODY;
$headers = "From: noreply@yuba.stanford.edu ($list List)\r\n";
mail($addr, "$list List", $body, $headers);
}
function list_create($list, $addr) {
connect_to_db();
$table = get_table();
$code = md5(uniqid(rand(), true));
$ret = mysql_query("CREATE TABLE IF NOT EXISTS $table (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
list VARCHAR(100),
email VARCHAR(100),
code VARCHAR(32),
admin INT,
active INT)");
mysql_close();
if(!list_exists($list))
return list_add_to_addr($list, $addr, 1);
else {
echo "List alread exists. ";
return false;
}
}
?>