-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExternalMailmanSync.php
90 lines (69 loc) · 2.8 KB
/
ExternalMailmanSync.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
<?php
define("MAILMAN_FILE_UPLOAD_DIRECTORY", "/root/");
getMembers();
function getMembers()
{
//url to api
$url = "REDACTED";
//Data array of post fields that includes the username and password that will be checked in the rest server for the feed
//An error will be returned if any parameter is missing a value
$data = "method=" . base64_encode("getAllMembers") .
"&username=" . base64_encode("REDACTED") .
"&password=" . base64_encode("REDACTED").
"&encodeOutput=" . base64_encode(TRUE);
// Set up curl options
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//json array returned.
$result = json_decode(curl_exec($ch), true);
// Get the array of events
$results = $result["Response"]["data"];
//if data returned it is in the array with the following details
$emails = array();
if(is_array($results) && !empty($results))
{
foreach($results as $item)
{
//Process the results here
if ($item["EmailMemberAllowed"])
{
$emails[] = $item["Email"];
}
}
// If the result from socs portal is an array and has got this far, we'll assume we have emails to sync.
// Maybe just check theres a good few emails to sync (should be over 1000)
if (count($emails) > 500) mailmanSync("compsoc-announce@lists.compsoc.ie", $emails);
} else {
var_dump("Gil: Mailman 3 sync in a bitta bother");
var_dump($result);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $emails;
}
function mailmanSync($listname, $emails)
{
$filePath = mailmanArrayToFile($emails);
exec("cd /opt/mailman && /opt/mailman/venv/bin/mailman members -s " . $filePath . " " . $listname, $res);
//exec("cd /opt/mailman && /opt/mailman/venv/bin/mailman syncmembers -W -G -n " . $filePath . " " . $listname, $res); //future update
unlink($filePath);
return $res;
}
// Convert email array to file with email per line
function mailmanArrayToFile($emails)
{
$filePath = MAILMAN_FILE_UPLOAD_DIRECTORY . "emails-" . date("His") . ".txt";
$file = fopen($filePath, "w");
$str = "";
foreach ($emails as $email)
{
$str .= $email . "\n";
}
fwrite($file, $str);
fclose($file);
return $filePath;
}
?>