php/vmail.inc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 <?
       
     2 // Copyright (C) 2000 Mike Bell <mike@mikebell.org>
       
     3 //
       
     4 // Examples available at http://mailtest.mikebell.org/
       
     5 // Latest version will be included in each vmailmgr release
       
     6 //
       
     7 // This program is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2 of the License, or
       
    10 // (at your option) any later version.
       
    11 //
       
    12 // This program is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 //
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with this program; if not, write to the Free Software
       
    19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    20 
       
    21 function vm_daemon_raw($arg)
       
    22 {
       
    23         $vmailfile = "/tmp/.vmailmgrd";
       
    24 	if (file_exists("/etc/vmailmgr/socket-file"))
       
    25 		$socketfile = (file ("/etc/vmailmgr/socket-file"));
       
    26 	$socketfile = trim($socketfile[0]);
       
    27 	if ($socketfile != "")
       
    28 		$vmailfile = $socketfile;
       
    29 		
       
    30 
       
    31         $vmailsock = fsockopen ($vmailfile, 0, $errno, $errstr, 4);
       
    32 	if (!$vmailsock) die("Failed to open socket file, is daemon running?");
       
    33 
       
    34 /*      Parse $arg, which should be an array of arguments to pass to the
       
    35         daemon, into a glob consisting of each argument proceeded by a
       
    36         two-byte representation of its length. */
       
    37 
       
    38         for ($x=0; $x < sizeof($arg); $x++ )
       
    39         {
       
    40                 $commandlength = strlen($arg[$x]);
       
    41                 $high=(($commandlength & (0xFF << 8)) >> 8);
       
    42                 $low=($commandlength & 0xFF);
       
    43                 $command .= sprintf("%c%c%s", $high, $low, $arg[$x]);
       
    44         };
       
    45 
       
    46 
       
    47 /*      Create the header, which consists of another two byte length
       
    48         representation, the number of arguments being passed, and the
       
    49         command string created above. */
       
    50 
       
    51         $args=$x-1;
       
    52         $commandlength=strlen($command);
       
    53         $high=(($commandlength & (255 << 8)) >> 8);
       
    54         $low=($commandlength & 255);
       
    55         $commandstr = sprintf("\002%c%c%c", $high, $low+1, $args).$command;
       
    56 
       
    57 /*      Pass it all to the daemon */
       
    58 
       
    59         fputs($vmailsock, $commandstr);
       
    60 
       
    61 /*      Get the response        */
       
    62 
       
    63 	$value = ord( fgetc ($vmailsock));
       
    64 	$length = (ord(fgetc($vmailsock)) << 8) + ord(fgetc($vmailsock));
       
    65 
       
    66 	if ($length == 0) {
       
    67 		while (!feof($vmailsock))
       
    68 			$out=fread($vmailsock, 65535);
       
    69 		fclose($vmailsock);
       
    70 		return $out;
       
    71 	}
       
    72 
       
    73 	$message = fread ($vmailsock, $length);
       
    74 
       
    75 /*	Close the socket	*/
       
    76 	fclose($vmailsock);
       
    77 
       
    78 return array($value, $message);
       
    79 
       
    80 }
       
    81 
       
    82 function listdomain_parse_userdata($line, $username){
       
    83 	//Grab the protocol version
       
    84 	$ver=ord(substr($line, 0, 1));
       
    85 	if ($ver != 2) die("Protocol version is $ver, not the 2 as this script expects.");
       
    86 	//Chop off the version
       
    87 	$line=substr($line, 1);
       
    88 	//Grab the flags
       
    89 	while (substr($line, 0, 1) != chr(0)){
       
    90 		$flags[ord(substr($line, 0, 1))]=ord(substr($line, 1, 1));
       
    91 		$line=substr($line, 2);
       
    92 	};
       
    93 	//Explode the remainder into an array based on the NULs used to end the fields
       
    94 	$user=explode(chr(0), $line);
       
    95 	$x=1;
       
    96 	if ($user[$x++]!=("*")) $password="Set";
       
    97 	$mailbox=$user[$x++];
       
    98 	while($user[$x]!="")
       
    99 		$aliases[]=$user[$x++];
       
   100 	$x++;
       
   101 	$PersonalInfo=$user[$x++];
       
   102 	$HardQuota=$user[$x++];
       
   103 	$SoftQuota=$user[$x++];
       
   104 	$SizeLimit=$user[$x++];
       
   105 	$CountLimit=$user[$x++];
       
   106 	$CreationTime=$user[$x++];
       
   107 	$ExpiryTime=$user[$x++];
       
   108 	
       
   109 	return array($username, $password, $mailbox, $aliases,
       
   110 $PersonalInfo, $HardQuota, $SoftQuota, $SizeLimit, $CountLimit,
       
   111 $CreationTime, $ExpiryTime, $flags);
       
   112 }	
       
   113 
       
   114 /* Parses the lines from listdomain into fields. */
       
   115 
       
   116 function listdomain_parse_line($line){
       
   117 	//Grab the user's name
       
   118 	$data_position = strpos($line, chr(0));
       
   119 	$username=substr($line, 0, $data_position);
       
   120 	// Send that user's data to be parsed. The $username is nessesary because array manipulation in PHP3 is so limited.
       
   121 	return listdomain_parse_userdata(substr($line, $data_position+1), $username);
       
   122 }
       
   123 
       
   124 /* Does the ugly stuff for listdomain, and calls listdomain_parse_line once
       
   125    for each user */
       
   126 
       
   127 function listdomain_parse($output){
       
   128 	$cur=1;
       
   129 	do {
       
   130 		$linelength=(ord(substr($output, $cur++, 1)) << 8 ) + ord(substr($output, $cur++, 1));
       
   131 		if ($linelength == 0) break;
       
   132 		$array[] = listdomain_parse_line(substr($output, $cur, $linelength));
       
   133 		$cur+=$linelength+1;
       
   134 	} while (1);
       
   135 	return $array;
       
   136 }
       
   137 
       
   138 /* listdomain, takes domain name and password, returns listdomain output
       
   139    parsed as stated in listdomain_parse_userdata */
       
   140 
       
   141 function listdomain($domain, $password){
       
   142 if ($domain=="") return array(1, "Empty domain");
       
   143 if ($password=="") return array(1, "Empty domain password");
       
   144 	$command=array("listdomain",$domain, $password);
       
   145 	$temp=vm_daemon_raw(array("listdomain", $domain, $password));
       
   146 	if (is_array($temp)) return $temp;
       
   147 return listdomain_parse($temp);
       
   148 }
       
   149 
       
   150 /* Lookup, returns for a single virtual user what listdomain does for an entire domain. */
       
   151 
       
   152 function lookup($domain, $user, $password){
       
   153 	$command=array("lookup", $domain, $user, $password);
       
   154 	$tmp = vm_daemon_raw($command);
       
   155 	return listdomain_parse_userdata($tmp[1], $user);
       
   156 }
       
   157 			
       
   158 /* vadduser, takes domain name, password, username, userpassword, and an
       
   159    array of forwarding desinations, returns an array consisting of an
       
   160    integer exit code and message. */
       
   161 
       
   162 function vadduser($domain, $password, $username, $userpass, $forwards){
       
   163 if ($domain=="") return array(1, "Empty domain");
       
   164 if ($password=="") return array(1, "Empty domain password");
       
   165 if ($username=="") return array(1, "Empty username");
       
   166 if ($userpass=="") return array(1, "No user password supplied");
       
   167 	$command=array("adduser2", $domain, $username, $password,
       
   168 	               $userpass, $username);
       
   169 	for ($x=0; $x < sizeof($forwards); $x++)
       
   170 		if ($forwards[$x]!="") $command[]=$forwards[$x];
       
   171 return vm_daemon_raw($command);
       
   172 }
       
   173 
       
   174 /* vaddalias, takes domain name, password, username, userpassword, and an
       
   175    array of forwarding desinations, returns an array consisting of an
       
   176    integer exit code and message. If the user's password is left empty an
       
   177    alias with no password will be created. */
       
   178 
       
   179 function vaddalias($domain, $password, $username, $userpass, $forwards){
       
   180 if ($domain=="") return array(1, "Empty domain");
       
   181 if ($password=="") return array(1, "Empty domain password");
       
   182 if ($username=="") return array(1, "Empty username");
       
   183 	$command=array("adduser2", $domain, $username, $password,
       
   184 	               $userpass, "");
       
   185 	for ($x=0; $x < sizeof($forwards); $x++)
       
   186 		if ($forwards[$x]!="")
       
   187 			$command[]=$forwards[$x];
       
   188 return vm_daemon_raw($command);
       
   189 }
       
   190 
       
   191 /* vdeluser, takes domain name, password, and username, returns an array
       
   192    consisting of an integer exit code and message. */
       
   193 
       
   194 function vdeluser($domain, $password, $username){
       
   195 if ($domain=="") return array(1, "Empty domain");
       
   196 if ($password=="") return array(1, "Empty domain password");
       
   197 if ($username=="") return array(1, "Empty username");
       
   198 	$command=array("deluser", $domain, $username, $password);
       
   199 return vm_daemon_raw($command);
       
   200 }
       
   201 
       
   202 /* vchpass, takes domain name, password, username and a new password,
       
   203    returns an array consisting of an integer exit code and message. Scripts
       
   204    allowing users to change their own passwords should check the password
       
   205    was entered correctly by having the user enter it twice and checking
       
   206    these are equal*/
       
   207 
       
   208 function vchpass($domain, $password, $username, $newpass){
       
   209 if ($domain=="") return array(1, "Empty domain");
       
   210 if ($password=="") return array(1, "Empty domain password");
       
   211 if ($username=="") return array(1, "Empty username");
       
   212 if ($newpass=="") return array(1, "Empty new password");
       
   213 	$command=array("chattr", $domain, $username, $password, "1", $newpass);
       
   214 return vm_daemon_raw($command);
       
   215 }
       
   216 
       
   217 /* vchforward, takes domain name, password, username and an array of
       
   218    forwarding addresses, returns an array consisting of an integer exit code
       
   219    and message. */ 
       
   220 
       
   221 function vchforward($domain, $password, $username, $forwards){
       
   222 if ($domain=="") return array(1, "Empty domain");
       
   223 if ($password=="") return array(1, "Empty domain password");
       
   224 if ($username=="") return array(1, "Empty username");
       
   225 	$command=array("chattr", $domain, $username, $password, "2");
       
   226 	for ($x=0; $x< sizeof($forwards); $x++)
       
   227 		if ($forwards[$x] != "") $command[]=$forwards[$x];
       
   228 	if (sizeof($forwards) == 0) $command[] = "";
       
   229 return vm_daemon_raw($command);
       
   230 }
       
   231 
       
   232 /* v*/
       
   233 
       
   234 function vchattr($domain, $password, $username, $attr, $value){
       
   235 	$ATTR = array(
       
   236 		"PASS" => "1",
       
   237 		"DEST" => "2",
       
   238 		"HARDQUOTA" => "3",
       
   239 		"SOFTQUOTA" => "4",
       
   240 		"MSGSIZE" => "5",
       
   241 		"MSGCOUNT" => "6",
       
   242 		"EXPIRY" => "7",
       
   243 		"MAILBOX_ENABLED" => "8",
       
   244 		"PERSONAL" => "9"
       
   245 	);
       
   246 	$command = array ("chattr", $domain, $username, $password, $ATTR[$attr], $value);
       
   247 return vm_daemon_raw($command);
       
   248 }
       
   249 
       
   250 function vwriteautoresponse($domain, $password, $username, $message){
       
   251 	$command = array("autoresponse", $domain, $username, $password, "write", $message);
       
   252 	return vm_daemon_raw($command);
       
   253 }
       
   254 
       
   255 function vreadautoresponse($domain, $password, $username){
       
   256 	$command = array("autoresponse", $domain, $username, $password, "read");
       
   257 	return vm_daemon_raw($command);
       
   258 }
       
   259 
       
   260 function vdisableautoresponse($domain, $password, $username){
       
   261 	$command = array("autoresponse", $domain, $username, $password, "disable");
       
   262 	return vm_daemon_raw($command);
       
   263 }
       
   264 
       
   265 function venableautoresponse($domain, $password, $username){
       
   266         $command = array("autoresponse", $domain, $username, $password, "enable");
       
   267 	return vm_daemon_raw($command);
       
   268 }
       
   269 
       
   270 function vautoresponsestatus($domain, $password, $username){
       
   271 	$command = array("autoresponse", $domain, $username, $password, "status");
       
   272 	return vm_daemon_raw($command);
       
   273 }
       
   274 
       
   275 ?>