PHP Classes

File: sendmail.class

Recommend this page to a friend!
  Classes of Oliver Schlag   sendmail_class   sendmail.class   Download  
File: sendmail.class
Role: ???
Content type: text/plain
Description: The Main Class
Class: sendmail_class
Author: By
Last change:
Date: 23 years ago
Size: 13,928 bytes
 

Contents

Class file image Download
<?php /* Class to manage the users of a sendmail installation Version : 0.1a Date : 09.30.2001 Author : Oliver Schlag <cjcs@ipfb.net> This class only handles the flatfile style tables. If you have dbm files or other then this class is the wrong one. Perhaps you like to port this class to another data storage ;-) If you port it please drop me a line im intrested in every port. If you find an error please send me a message and i will try to fix it. Please send me as many informations as possible in your error message so i can try to reproduce and fix it. You can use this class without any restrictions, you can modify it and you can do what ever you want to do with it, BUT you're not allowed to sell this class or use it in a product you sell. Please note that this class is not for use in a webserver enviroment, this class is only for the cgi version of php. This class is to be used in PHP Scripts which will be run from shell or cron like bash scripts. Any other comments, except flames ;-), are welcome. Functiontable : sendmail_flat_class($aliastablepath,$virtusertablepath,$debuglevel,$log) This is the constructor, the constructor will open the files, create the array's. Arguments : $aliastablepath : Path to the alias file (normal /etc/mail/aliases) $virtusertablepath : Path to the virtusertable file (normal /etc/mail/virtusertable) $debuglevel : 0 = no Debug Output, 1 = Debug Output $log : 1 = Log to the syslog, 0 = no logging If you specify a log then the debug output will be written into this log and there's no output on the console. sendmail_flat_close() This is the destructor, you need to call it on the end of your work or nothing will be written into the files and all your changes will be work for nothing. Arguments : This function did not take any arguments. add_virtual($address,$domain,$user) This function will create a new entry in the virtual user table. If an error occurs the function returns -1 if all went ok the function will return a 0. Arguments : $address : The part before the @ in the mail address $domain : The part after the @ in the new mail address $user : The user which should receive the mail to this address del_virtual($address,$domain) This function will delete an entry in the virtual user table. If an error occurs the function returns -1 if all went ok the function will return a 0. Arguments : $address : The part before the @ in the mail address $domain : The part after the @ in the mail address add_route($source,$dest) This function will create a virtual host route. If an error occurs the function returns -1 if all went ok the function will return a 0. Arguments : $source : The source domain $dest : The destination domain del_route($source,$dest) This function will delete a virtual host route If an error occurs the function returns -1 if all went ok the function will return a 0. Arguments : $source : The source domain $dest : The destination domain add_alias($source,$dest) This function will add an alias to the alias map If an error occurs the function returns -1 if all went ok the function will return a 0. Arguments : $source : The user which should be forwarded $dest : The destination of the mails del_alias($source,$dest) This function will delete an alias from the alias map If an error occurs the function returns -1 if all went ok the function will return a 0. Arguments : $source : The user which should be forwarded $dest : The destination of the mails function template (only for me so i don't have to type so much :-) ) function name () { if ( $error != 0 ) { return -1; } else { return 0; } } */ class sendmail { var $ALIAS; var $VIRTUAL; var $ALIASPATH; var $VIRTUSERPATH; var $DEBUG; var $LOG; # Start of initalize function function sendmail_flat_class($aliastablepath,$virtusertablepath,$debuglevel,$log) { if ( $aliastablepath != "") { $this->ALIASPATH = $aliastablepath; } else { return -1; } if ( $virtusertablepath != "") { $this->VIRTUSERPATH = $virtusertablepath; } else { return -1; } if ( $debuglevel != "") { $this->DEBUG = 1; } else { $this->DEBUG = 0; } if ($log != "0") { $this->LOG = 1; } else { $this->LOG = 0; } # Open the alias file and create the array $ap = fopen($this->ALIASPATH,"r"); if(!$ap) { # Output the Debug informations if ($this->LOG) { syslog(LOG_ERR,"Error opening alias file : $this->ALIASPATH"); } return -1; } # Output the Debug informations if ($this->DEBUG) { syslog(LOG_DEBUG,"Aliastable opened"); } while(!feof($ap)) { $data = trim(fgets($ap,4096)); if($data[0] != '#' && $data[0] != '') { $alias_temp = explode(":",$data); $key = trim($alias_temp[0]); $value = trim($alias_temp[1]); $this->ALIAS[$key] = $value; } } fclose($ap); # Output the Debug informations if ($this->DEBUG) { $cnr = count($this->ALIAS); syslog(LOG_DEBUG,"$cnr aliases read"); syslog(LOG_DEBUG,"Aliastable closed"); } # Open the virtusertable file and create the array $vp = fopen($this->VIRTUSERPATH,"r"); if(!$vp) { # Output the Debug informations if ($this->LOG) { syslog(LOG_ERR,"Error opening virtusertable file : $this->VIRTUSERPATH"); } return -1; } # Output the Debug informations if ($this->DEBUG) { syslog(LOG_DEBUG,"Virtusertable opened"); } while(!feof($vp)) { $data = trim(fgets($vp,4096)); if($data[0] != '#' && $data[0] != '') { $virt_temp = explode("\t",$data); $key = trim($virt_temp[0]); $value = trim($virt_temp[1]); $this->VIRTUAL[$key] = $value; } } fclose($vp); # Output the Debug informations if ($this->DEBUG) { $cnr = count($this->VIRTUAL); syslog(LOG_DEBUG,"$cnr virtusers read"); syslog(LOG_DEBUG,"Virtusertable closed"); } } # End of initalize function # Start of destructor function function sendmail_flat_close() { # Write new alias Database (we copy the .new file later onto the original file) $new_alias = $this->ALIASPATH.".new"; # Check if the .new file aread exist and delete it if (file_exists($new_alias)) { unlink($new_alias); } $ap = fopen($new_alias,"w"); if (!$ap) { if ($this->LOG) { syslog(LOG_ERR,"New aliastable create error"); return -1; } } foreach ($this->ALIAS as $key => $value) { $temp_alias = $key.":\t".$value."\n"; fputs($ap,$temp_alias); } fclose($ap); if ($this->DEBUG) { syslog(LOG_DEBUG,"New Aliastable File created"); } # Write a new virtualuser file $new_virtual = $this->VIRTUSERPATH.".new"; # Check if the .new file aread exist and delete it if (file_exists($new_virtual)) { unlink($new_virtual); } $vp = fopen($new_virtual,"w"); if (!$vp) { if ($this->LOG) { syslog(LOG_ERR,"New virtusertable create error"); return -1; } } foreach ($this->VIRTUAL as $key => $value) { $temp_virtual = $key."\t".$value."\n"; fputs($vp,$temp_virtual); } fclose($vp); if ($this->DEBUG) { syslog(LOG_DEBUG,"New virtusertable created"); } # Now we need to copy the .new files onver the old files. To do this we first # rename the original files to .old and then rename the .new to the correct # filename. If this went ok we remove the .old files $old_alias = $this->ALIASPATH.".old"; $old_virtual = $this->VIRTUSERPATH.".old"; if (!rename($this->ALIASPATH,$old_alias)) { if ($this->LOG) { syslog(LOG_ERR,"Error renaming $this->ALIASPATH"); return -1; } } else { if ($this->DEBUG) { syslog(LOG_DEBUG,"$this->ALIASPATH renamed to $old_alias"); } } # Create the new alias table if (!rename($new_alias, $this->ALIASPATH)) { if ($this->LOG) { syslog(LOG_ERR,"Error renaming $new_alias"); return -1; } } else { if ($this->DEBUG) { syslog(LOG_DEBUG,"$new_alias renamed to $this->ALIASPATH"); } } # Create backup copy of the original virtusertable if (!rename($this->VIRTUSERPATH,$old_virtual)) { if ($this->LOG) { syslog(LOG_ERR,"Error renaming $this->VIRTUSERPATH"); return -1; } } else { if ($this->DEBUG) { syslog(LOG_DEBUG,"$this->VIRTUSERPATH renamed to $old_virtual"); } } # Create the new virtuser table if (!rename($new_virtual, $this->VIRTUSERPATH)) { if ($this->LOG) { syslog(LOG_ERR,"Error renaming $new_virtual"); return -1; } } else { if ($this->DEBUG) { syslog(LOG_DEBUG,"$new_virtual renamed to $this->VIRTUSERPATH"); } } # Now delete the .old files unlink($old_alias); unlink($old_virtual); return 0; # End of destructor function } function add_virtual($address,$domain,$user) { $email = $address."@".$domain; if($this->VIRTUAL[$email] == "") { $this->VIRTUAL[$email] = $user; if ($this->LOG) { syslog(LOG_INFO,"New virtual user created : $email => $user"); } } else { if ($this->LOG) { syslog(LOG_ERR,"Error creating virtual user : $email => $user"); return -1; } } return 0; } #End of add_virtual function function del_virtual($address,$domain) { $email = $address."@".$domain; if($this->VIRTUAL[$email] != "") { unset($this->VIRTUAL[$email]); if ($this->LOG) { syslog(LOG_INFO,"Virtual user deleted : $email"); } } else { if ($this->LOG) { syslog(LOG_ERR,"Error deleting virtual user : $email"); return -1; } } return 0; } #End of del_virtual function function add_route($source,$dest) { $source = "@".$source; $dest = "%1@".$dest; if($this->VIRTUAL[$source] == "") { $this->VIRTUAL[$source] = $dest; if ($this->LOG) { syslog(LOG_INFO,"New domain route created : $source => $dest"); } } else { if ($this->LOG) { syslog(LOG_ERR,"Error creating new domain route : $source => $dest"); return -1; } } return 0; } # End of add route function function del_route($source,$dest) { $source = "@".$source; $dest = "%1@".$dest; if($this->VIRTUAL[$source] != "") { unset($this->VIRTUAL[$source]); if ($this->LOG) { syslog(LOG_INFO,"Domain route deleted : $source"); } } else { if ($this->LOG) { syslog(LOG_ERR,"Error deleting domain route : $source"); return -1; } } return 0; } #End of del_route function function add_alias($source,$dest) { if($this->ALIAS[$source] == "") { $this->ALIAS[$source] = $dest; if ($this->LOG) { syslog(LOG_INFO,"New alias created : $source => $dest"); } } else { if ($this->LOG) { syslog(LOG_ERR,"Error creating new alias : $source => $dest"); return -1; } } return 0; } # End of add alias function function del_alias($source,$dest) { if($this->ALIAS[$source] != "") { unset($this->ALIAS[$source]); if ($this->LOG) { syslog(LOG_INFO,"Alias deleted : $source"); } } else { if ($this->LOG) { syslog(LOG_ERR,"Error deleting alias : $source"); return -1; } } return 0; } #End of del_alias function } # End of class ?>