From 5304073d51d3b375783cd9c0fdb9039d63c1a366 Mon Sep 17 00:00:00 2001
From: Leif Andersson <leif.andersson@control.lth.se>
Date: Sat, 12 Sep 2015 18:13:56 +0200
Subject: [PATCH] Added handling of mailing list when groups change.

---
 action.changeGroups.php |  2 ++
 lib/class.Group.php     | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/action.changeGroups.php b/action.changeGroups.php
index 5c4251b..4641b8d 100644
--- a/action.changeGroups.php
+++ b/action.changeGroups.php
@@ -30,11 +30,13 @@ $smarty->assign('headline',$headline);
 if (isset($params['setMember']))  {
   $GroupID = $params['nonmembers'];
   Group::setMember($ID,$GroupID);
+  Group::setMailLists();
 }
 
 if (isset($params['unsetMember']))  {
   $GroupID = $params['members'];
   Group::UnsetMember($ID,$GroupID);
+  Group::setMailLists();
 }
 
 $members = Group::getMembers($ID);
diff --git a/lib/class.Group.php b/lib/class.Group.php
index 23d39d3..439fde1 100644
--- a/lib/class.Group.php
+++ b/lib/class.Group.php
@@ -2,6 +2,9 @@
 
 class Group {
 
+  private static $maildir = '/tmp/maildir';
+
+
   private static $queryList = <<<EOS
     select ID, Gruppnamn from Grupper order by Gruppnamn
 EOS;
@@ -19,6 +22,11 @@ EOS;
     delete from GruppAdress where AdressID = ? and GruppID = ?
 EOS;
 
+  private static $queryMail = <<<EOS
+    select Epost from Adresser, GruppAdress where
+    Adresser.ID = AdressID and GruppID = ?
+EOS;
+
   private static $instance;
 
   private $list;
@@ -77,6 +85,32 @@ EOS;
     return self::$instance->list;
   }
 
-
+  public static function setMailLists() {
+    global $gCms; if (!isset($gCms)) exit;
+    $db = AddressDB::getDB();
+    $groups = self::getGroup();
+    if(!is_dir(self::$maildir)) mkdir(self::$maildir,'0755',true);
+    foreach ($groups as $name => $ID) {
+      $pos = strrpos($name, '.dis');
+      if ($pos === false) continue;
+      $epost = array();
+      $values = array($ID);
+      $result = $db->Execute(self::$queryMail,$values);
+      if ($result === false) { die("Database error!<br>" . $db->ErrorMsg()); }
+      while ($row = $result->FetchRow()) {
+	if (!$row['Epost']) continue;
+	$epost[] = $row['Epost'];
+      }
+      if (count($epost) > 0) {
+	//	echo '<pre>'; print_r($epost); echo '</pre>';
+	$filename  = self::$maildir . '/' . $name;
+	$file = fopen($filename,'w');
+	if ($file === false) die('Cannot write '.$filename);
+	foreach ($epost as $line) fwrite($file,$line.PHP_EOL);
+	fclose($file);
+      }
+    }
+    //  exit;
+  }
 }
 ?>
-- 
GitLab