-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailman_sync.module
153 lines (134 loc) · 4.88 KB
/
mailman_sync.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
<?php
function mailman_sync_menu() {
return array(
'admin/config/mailman' => array(
'title' => 'Mailman Synchronization',
'page callback' => 'drupal_get_form',
'page arguments' => array('_mailman_sync_configuration_form'),
'access arguments' => array('administer users'),
'file' => 'mailman_sync.admin.inc',
),
'admin/config/mailman/list' => array(
'title' => T('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
),
'admin/config/mailman/%mailman_sync_group/edit' => array(
'title' => 'Edit Group',
'page callback' => 'drupal_get_form',
'page arguments' => array('_mailman_sync_edit_form', 3),
'access arguments' => array('administer users'),
'file' => 'mailman_sync.admin.inc',
),
'mailman/sync/map' => array(
'page callback' => '_mailman_sync_list_map',
'access callback' => '_mailman_sync_verify_access',
),
'mailman/sync/%' => array(
'page callback' => '_mailman_sync_list_users',
'page arguments' => array(2),
'access callback' => '_mailman_sync_verify_access',
'access arguments' => array(2),
),
);
}
function mailman_sync_hook_info() {
return array(
'mailman_sync_group_info' => array(
'group' => 'mailman_sync',
),
);
}
function mailman_sync_group_info_load() {
// Load groups info; try cache first.
if (!$groups = cache_get('mailman_sync_groups')->data) {
$groups = module_invoke_all('mailman_sync_group_info');
cache_set('mailman_sync_groups', $groups, 'cache', CACHE_TEMPORARY);
// Create new entries in configuration table for missing items.
$current_groups = db_query("SELECT group_id FROM mailman_sync")->fetchCol();
$new_groups = array_diff(array_keys($groups), $current_groups);
foreach ($new_groups as $new_group) {
$record = array('group_id' => $new_group);
drupal_write_record('mailman_sync', $record);
}
}
else {
// We still need the inlcude files loaded for any other functions that
// may be defined within.
module_load_all_includes('mailman_sync.inc');
}
// Append configuration info to each group.
$groups_conf = db_query("SELECT group_id, mailman_list, mailman_password, status FROM mailman_sync")->fetchAllAssoc('group_id');
foreach ($groups as $group_id => $group) {
$groups[$group_id]['status'] = $groups_conf[$group_id]->status;
$groups[$group_id]['mailman_list'] = $groups_conf[$group_id]->mailman_list;
$groups[$group_id]['mailman_password'] = $groups_conf[$group_id]->mailman_password;
}
return $groups;
}
function mailman_sync_group_load($group_id) {
$groups = mailman_sync_group_info_load();
if (!empty($group_id) && !empty($groups[$group_id])) {
$group = $groups[$group_id];
$group['group_id'] = $group_id;
$group['members'] = call_user_func_array($group['members callback'], $group['members arguments']);
return $group;
}
else return array();
}
function _mailman_sync_verify_access($list = NULL) {
// First check key
if (isset($_GET['mailman_key']) && $submitted_key = $_GET['mailman_key']) {
$mailman_key = variable_get('mailman_sync_key');
if (!empty($mailman_key) && $mailman_key == $submitted_key) {
return TRUE;
}
}
}
function _mailman_sync_list_users($list) {
$results = db_query("SELECT group_id FROM {mailman_sync} WHERE mailman_list = :list AND status = 1", array(':list' => $list))
->fetchAll();
// Merge members from each group
$members = array();
foreach ($results as $result) {
$group = mailman_sync_group_load($result->group_id);
if (!empty($group['members'])) {
$members = $members + $group['members'];
}
}
drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
$text = '';
foreach ($members as $member) {
// Remove invalid email addresses, as mailman will bork otherwise
if (empty($member['email']) || !filter_var($member['email'], FILTER_VALIDATE_EMAIL)) {
continue;
}
if (!empty($member['name'])) {
$name = str_replace('"', '', $member['name']);
// Mailman doens't handle names with non-ascii characters
$name = filter_var($name, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$text .= "\"{$name}\" <{$member['email']}>\n";
}
else {
$text .= "{$member['email']}\n";
}
}
print $text;
drupal_exit();
}
function _mailman_sync_list_map() {
drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
$result = db_query("SELECT DISTINCT(mailman_list) mailman_list FROM mailman_sync WHERE status = 1 AND mailman_list <> ''");
$text = '';
foreach ($result as $row) {
$link = url("mailman/sync/{$row->mailman_list}", array(
'absolute' => TRUE,
));
$text .= "{$row->mailman_list} {$link}\n";
}
print $text;
drupal_exit();
}
function _mailman_sync_generate_key() {
return md5(mt_rand());
}