Advertisement
ryanpetris

Horde - Virtual Alias Update

May 10th, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Updating Postfix virtual alias maps from Horde.
  4.  *
  5.  * Horde allows users to have multiple "identities" and email aliases under
  6.  * those identities. When running a personal mail server, it's more than
  7.  * likely that all of those email addresses should be deliverable. This script
  8.  * takes all of the aliases in Horde's database and rewrites the virtual alias
  9.  * maps and domains files using the identities setup in Horde. Additionally,
  10.  * it will only update the files and reload postfix if a file has changed, so
  11.  * it is safe to run as a cron job as often as needed.
  12.  *
  13.  * Note that this script was written for Postfix on CentOS 6.4, and therefore
  14.  * you may need to update the locations of the "postmap" and "service"
  15.  * executables at the end of this script if you use a distribution other than
  16.  * CentOS. If you use a mail server other than postscript, if that mail server
  17.  * has the concept of alias maps/domains, then you may still be able to use
  18.  * this script, however it will need to be modified to use a format specific
  19.  * to that application. You may also need to update the locations of the maps
  20.  * and domains file at the beginning of this script if you have an existing
  21.  * setup with those files in different locations, or if you just want to put
  22.  * these files somewhere else. Additionally, you WILL need to update the MySQL
  23.  * connection information at the beginning of this script with your own.
  24.  *
  25.  * @author     Ryan Petris <ryan@petris.net>
  26.  * @copyright  (c) 2013 Ryan Petris
  27.  * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  28.  */
  29.  
  30. $db = new PDO("mysql:host=localhost;dbname=horde", "username", "password");
  31. $maps_file = "/etc/postfix/virtual_alias_maps";
  32. $domains_file = "/etc/postfix/virtual_alias_domains";
  33.  
  34. $stmt = $db->prepare("SELECT * FROM horde_prefs WHERE pref_name = 'identities';");
  35. $stmt->execute();
  36. $prefs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  37.  
  38. $emails = array();
  39. $email_lines = array();
  40. $domains = array();
  41.  
  42. if (!count($prefs)) {
  43.     die("Nothing returned from database query!");
  44. }
  45.  
  46. foreach ($prefs as $pref) {
  47.     if (!array_key_exists($pref["pref_uid"], $emails)) {
  48.         $emails[$pref["pref_uid"]] = array();
  49.     }
  50.  
  51.     $items = unserialize($pref["pref_value"]);
  52.  
  53.     foreach ($items as $item) {
  54.         if (!empty($item["from_addr"]) && !in_array($item["from_addr"], $emails[$pref["pref_uid"]])) {
  55.             $emails[$pref["pref_uid"]][] = $item["from_addr"];
  56.         }
  57.  
  58.         if (array_key_exists("alias_addr", $item)) {
  59.             foreach ($item["alias_addr"] as $alias) {
  60.                 if (!empty($alias) && !in_array($alias, $emails[$pref["pref_uid"]])) {
  61.                     $emails[$pref["pref_uid"]][] = $alias;
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. foreach ($emails as $user => $useremails) {
  69.     foreach ($useremails as $email) {
  70.         $email_lines[] = $email . " " . $user;
  71.  
  72.         $parts = explode('@', $email);
  73.  
  74.         if ($parts) {
  75.             $domain = $parts[count($parts) - 1];
  76.  
  77.             if (!in_array($domain, $domains)) {
  78.                 $domains[] = $domain;
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. $update = false;
  85.  
  86. $maps_contents = file_get_contents($maps_file);
  87. $domains_contents = file_get_contents($domains_file);
  88.  
  89. $maps_contents_new = implode("\n", $email_lines) . "\n";
  90. $domains_contents_new = implode("\n", $domains) . "\n";
  91.  
  92. if ($maps_contents != $maps_contents_new || $domains_contents != $domains_contents_new) {
  93.     file_put_contents($maps_file, $maps_contents_new);
  94.     file_put_contents($domains_file, $domains_contents_new);
  95.  
  96.     exec("/usr/sbin/postmap " . $maps_file);
  97.     exec("/sbin/service postfix reload");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement