-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalist.module
341 lines (322 loc) · 10.2 KB
/
alist.module
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* @file
* Module defining specific functionality for the Arlington List.
*
* This file is the base file for the functionality of the Arlington List.
*/
/**
* @cond ARLINGTONLIST
* @mainpage
* Module to build up the specific functionallity for the Arlington List.
*
* Modules:
*
* @subpage mailinglist
* @endcond
*/
/**
* Implements hook_permission().
*/
function alist_permission() {
return array(
'arlington list search' => array(
'title' => t('Search the Archives of the Arlington List'),
),
'cast of characters editing' => array(
'title' => t('Edit other peoples Cast of Characters entry'),
),
);
}
/**
* Implements hook_menu().
*/
function alist_menu() {
$items = array(
'archives/search' => array(
'title' => 'Search Archives',
'description' => 'Arlington List Search Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_searchpage'),
'access arguments' => array('access content'),
'file' => 'alist.search.inc',
),
'archives/search/login' => array(
'title' => 'Search Archives Login',
'description' => 'Arlington List Search Login Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_searchlogin'),
'access arguments' => array('access content'),
'file' => 'alist.search.inc',
),
'cast' => array(
'title' => 'Cast of Characters',
'description' => 'Arlington List Cast of Characters Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_castpage'),
'access arguments' => array('access content'),
'file' => 'alist.cast.inc',
),
'cast/entry' => array(
'title' => 'Cast of Characters Entry',
'description' => 'Arlington List Cast of Characters Entry Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_castentry'),
'access arguments' => array('access content'),
'file' => 'alist.cast.inc',
),
'cast/login' => array(
'title' => 'Cast of Characters Entry Login',
'description' => 'Arlington List Cast of Characters Login Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_castlogin'),
'access arguments' => array('access content'),
'file' => 'alist.cast.inc',
),
'options.htm' => array(
'title' => 'Arlington List Options',
'description' => 'Arlington List Change Options Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_optionspage'),
'access arguments' => array('access content'),
'file' => 'alist.options.inc',
),
'stats' => array(
'title' => 'Stats',
'description' => 'Arlington List Stats Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_statspage'),
'access arguments' => array('access content'),
'file' => 'alist.stats.inc',
),
'stats/admin' => array(
'title' => 'Top Posters',
'description' => 'Arlington List Posting Stats Page',
'type' => MENU_CALLBACK,
'page callback' => 'drupal_get_form',
'page arguments' => array('_alist_statsadmin'),
'access arguments' => array('administer users'),
'file' => 'alist.stats.inc',
),
);
return $items;
}
/**
* Modify the drupal mail system to send HTML emails.
*/
class AListMailSystem implements MailSystemInterface {
/**
* Concatenate and wrap the e-mail body for plain-text mails.
*
* @param $message
* A message array, as described in hook_mail_alter().
*
* @return
* The formatted $message.
*/
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
return $message;
}
/**
* Send an e-mail message, using Drupal variables and default settings.
*
* @see http://php.net/manual/en/function.mail.php
* @see drupal_mail()
*
* @param $message
* A message array, as described in hook_mail_alter().
* @return
* TRUE if the mail was successfully accepted, otherwise FALSE.
*/
public function mail(array $message) {
$mimeheaders = array();
foreach ($message['headers'] as $name => $value) {
$mimeheaders[] = $name . ': ' . mime_header_encode($value);
}
$line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
return mail(
$message['to'],
mime_header_encode($message['subject']),
// Note: e-mail uses CRLF for line-endings. PHP's API requires LF
// on Unix and CRLF on Windows. Drupal automatically guesses the
// line-ending format appropriate for your system. If you need to
// override this, adjust $conf['mail_line_endings'] in settings.php.
preg_replace('@\r?\n@', $line_endings, $message['body']),
// For headers, PHP's API suggests that we use CRLF normally,
// but some MTAs incorrectly replace LF with CRLF. See #234403.
implode("\n", $mimeheaders)
);
}
}
/**
* Implements hook_mail().
*/
function alist_mail($key, &$message, $parms) {
dpm($key);
dpm($parms);
dpm($message);
$email = check_plain($parms['email']);
$msg = $parms['entry'];
switch ($key) {
case 'castentry':
$message['subject'] = 'Your Arlington list Cast of Characters entry was successfully created or edited';
$message['body'][] = "Hi! Someone, hopefully you, just created or edited your cast of characters entry:\r\n\r\nhttp://new.arlingtonlist.org/cast#P" . $parms['id'] . "\r\n\r\nIf it wasn't you who did it, please reply to this message and let me know!\r\n\r\n-Richard Damon\r\nArlington List Manager";
break;
case 'castentry_admin':
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['subject'] = 'Cast of Characters entry created/edited by ' . $email;
$message['body'][] = check_markup($msg, 'filtered_html');
break;
}
dpm($message);
}
/**
* Test if we have a login cookie for the Arlington List
*
* @return string
* Email address we are loged in as, or FALSE
*/
function _alist_login_test() {
if (isset($_COOKIE['alist_email']) && isset($_COOKIE['alist_pass'])) {
$email = $_COOKIE['alist_email'];
$password = _mailinglist_decode_password($_COOKIE['alist_pass']);
if (_alist_check_password($email, $password)) {
return $email;
}
}
return FALSE;
}
/**
* Presents a login form if needed.
*
* Checks cookie for log in information, and use this if valid.
* Else, check information in form.
*
* @param $form
* Form being processed, login form will be added to it if needed
* @param $form_state
* Form state from form for processing
*/
function _alist_login(&$form, &$form_state) {
if (isset($form_state['input']['email']) && isset($form_state['input']['password'])) {
$email = $form_state['input']['email'];
$pass = $form_state['input']['password'];
}
elseif (isset($_COOKIE['alist_email']) && isset($_COOKIE['alist_pass'])) {
$email = $_COOKIE['alist_email'];
$pass = _mailinglist_decode_password($_COOKIE['alist_pass']);
}
else {
$email = '';
$pass = '';
}
$form['login'] = array(
'#weight' => 10,
'email' => array(
'#weight' => 40,
'#type' => 'textfield',
'#title' => 'EMail',
'#description' => 'EMail Address you are subscribed with',
'#default_value' => $email,
'#required' => TRUE,
),
'password' => array(
'#weight' => 50,
'#type' => 'password',
'#title' => 'Password',
'#description' => 'Your List password',
'#default_value' => $pass,
'#required' => TRUE,
),
'submit' => array(
'#weight' => 60,
'#type' => 'submit',
'#value' => 'Log In',
),
'remind' => array(
'#weight' => 70,
'#type' => 'submit',
'#value' => 'Send Password Reminder',
),
);
return $form;
}
/**
* Form Validate function for login forms.
* Forward here from other form validate functions when requesting log-in forms.
*/
function _alist_login_validate($form, &$form_state) {
if (isset($form_state['input']['email']) && isset($form_state['input']['password'])) {
$email = $form_state['input']['email'];
$pass = $form_state['input']['password'];
$valid = _alist_check_password($email, $pass);
$form_state['input']['login'] = $valid;
if (!$valid) {
form_set_error('password', t('No matched email/password subscription found'));
}
}
}
/**
* Form Submit function for login forms.
* Forward here from other forms submit functions when requesting login form.
*/
function _alist_login_submit($form, &$form_state) {
if (isset($form_state['input']['email']) && isset($form_state['input']['password']) && $form_state['input']['login']) {
setcookie('alist_email', $form_state['input']['email'], 0, '/');
setcookie('alist_pass', _mailinglist_encode_password($form_state['input']['password']), 0, '/');
}
}
/**
* Check if list password is valid.
* @param $email
* Email Address of subscription
* @param $password
* Supplied password
* @return bool
* TRUE if proper signin for the list, else FALSE
*/
function _alist_check_password($email, $password) {
$valid = FALSE;
$d["username"] = $email;
$d["password"] = $password;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "http://six.pairlist.net/mailman/private/arlington/",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($d),
));
$result = curl_exec($ch);
curl_close($ch);
if (strstr($result, "The arlington Archives")) {
$valid = TRUE;
}
return $valid;
}
/**
* Request a password for an account.
*
* @param $email The email address for the account to request the reminder for
*/
function _alist_request_password($email) {
$d["email"] = $email;
$d["password"] = "";
$d["login-remind"] = "Remind";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "http://six.pairlist.net/mailman/options/arlington",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($d),
));
curl_exec($ch);
curl_close($ch);
}