Location: PHPKode > projects > Phpqadmin > phpqadmin-0.4/include/functions_user.php
<?php

function is_user($company,$user)
{
	return is_dir(QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user) && !in_array($user,array('.','..')) && $user != '' && $company != '';
}

function is_user_mailinglist($company,$user)
{
	$m = get_user_file($company,$user,'mailinglist');
	return strlen($m)>0;
}

function set_user_mailinglist_qmailfiles($company,$user)
{
	$fp = @fopen(QMAIL_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.'lock','w');
	@flock($fp,LOCK_EX);
	$str = '#!/usr/bin/perl
#
# fix-env
# fixes enviorment created by qmail-local to match qmail-command(8) spec
# basically, a fix for a bug in qmail
#
# found at: http://www.sumthin.nu/archives/qmail/Jan_1999/msg00024.html

use strict var;

{
         my $host = $ENV{\'HOST\'};

         if ( $host ne \''.get_user_file($company,$user,'inhost').'\' )
         {
                my $local_wrong = $ENV{\'LOCAL\'};
                my $local_right = $local_wrong;

                $local_right =~ s/^[^-]+-//;

                my $local_wrong_re = quotemeta $local_wrong;
                my $local_right_re = quotemeta $local_right;

                $ENV{\'DTLINE\'}    =~ s/$local_wrong_re/$local_right_re/g;
                $ENV{\'RECIPIENT\'} =~ s/$local_wrong_re/$local_right_re/g;

                $ENV{\'LOCAL\'} = $local_right;
        }

        exec(@ARGV);

        exit 111;
}';
	$str = str_replace("\r",'',$str); // The script will crash if \r\n is present
	set_user_file($company,$user,'fix-env',$str,0777);
	$fixenv = '|'.QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.'fix-env ';
	$str = '';
	$str .= $fixenv.EZMLM_DIR.DIR_SLASH."ezmlm-manage '".QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user."'\n";
	$str .= $fixenv.EZMLM_DIR.DIR_SLASH."ezmlm-warn '".QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user."' || exit 0";
	set_user_file($company,$user,'.qmail-'.$user.'-default',$str);
	$str = '';
	$str .= $fixenv.EZMLM_DIR.DIR_SLASH."ezmlm-reject\n";
	$str .= $fixenv.EZMLM_DIR.DIR_SLASH."ezmlm-send '".QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user."'\n";
	$str .= $fixenv.EZMLM_DIR.DIR_SLASH."ezmlm-warn '".QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user."' || exit 0";
	set_user_file($company,$user,'.qmail-'.$user,$str);
	$str = '';
	$str .= QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH."Maildir\n";
	$str .= $fixenv.EZMLM_DIR.DIR_SLASH."ezmlm-warn '".QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user."' || exit 0";
	set_user_file($company,$user,'.qmail-'.$user.'-owner');
	set_user_file($company,$user,'headerremove',"return-path\nreturn-recepit-to\ncontent-length\nfrom");
	$outname = get_user_file($company,$user,'outname');
	create_owned_user_dir($company,$user,'subscribers');
	create_owned_user_dir($company,$user,'archive');
	create_owned_user_dir($company,$user,'bounce');
	create_owned_user_dir($company,$user,'text');
	if ($outname)
		$outname = '"'.$outname.'" ';
	set_user_file($company,$user,'headeradd','From: '.$outname.'<'.get_user_file($company,$user,'outlocal').'@'.get_user_file($company,$user,'outhost').'>');
	@flock($fp,LOCK_UN);
	@fclose($fp);
}


function set_user_mailinglist_key($company,$user)
{
	if (!get_user_file($company,$user,'key'))
		set_user_file($company,$user,'key',create_garbage(128));
}

function get_users($company)
{
	$dir = opendir(QMAIL_POP_DIR.DIR_SLASH.$company);
	$users = array();
	while(false !== ($file = readdir($dir)))
	{
		if (is_dir(QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$file) && !in_array($file,array('.','..')))
		{
			array_push($users,array('ID' => $file));
		}
	}
	@closedir($dir);
	return $users;
}


function get_user_aliases($company,$user)
{
	return array_values(explode("\n",trim(get_user_file($company,$user,'aliases'))));
}


function create_user($company,$user,$password='')
{
	if (!is_company($company))
		return false;
	
	$new_dir = QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user;
	create_owned_user_dir($company,$user,'');
	create_owned_user_dir($company,$user,'Maildir');
	create_owned_user_dir($company,$user,'Maildir'.DIR_SLASH.'cur');
	create_owned_user_dir($company,$user,'Maildir'.DIR_SLASH.'new');
	create_owned_user_dir($company,$user,'Maildir'.DIR_SLASH.'tmp');
		
	if ($password)
		set_user_password($company,$user,$password);
		
	$filename = QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.'.qmail';

	$str = "# maildir\n";
	$str .= '.'.DIR_SLASH.'Maildir'.DIR_SLASH;
	set_user_file($company,$user,'.qmail',$str);
	
	return true;
}

function create_owned_user_dir($company,$user,$dir='',$rights = 0700)
{
		$new_dir = QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.$dir;
		if (!is_dir($new_dir))
		{
      		mkdir($new_dir);
		}
   		@chmod($new_dir,$rights);
		return true;
}


function set_user_aliases($company,$user,$aliases)
{
	return set_user_file($company,$user,'aliases',$aliases);
}

function set_user_file($company,$user,$filename,$value,$mode=0600)
{
	$filename = QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.$filename;
	@chmod(QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user,0700);
	@chmod($filename,0700);
	$fp = fopen($filename,'wb');
	@flock($fp,LOCK_EX);
	fwrite($fp,$value);
	@flock($fp,LOCK_UN);
	fclose($fp);
	@chmod($filename,$mode);
	return true;
}

function get_user_file($company,$user,$filename,$returnifnotfound='')
{
	@chmod(QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user,0700);
	$filename = QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.$filename;
	if (!file_exists($filename)) return $returnifnotfound;
	@chmod($filename,01600);
	$fp = @fopen($filename,'rb');
	@flock($fp,LOCK_SH);
	$contents = @fread($fp,filesize($filename));
	@flock($fp,LOCK_UN);
	@fclose($fp);
	@chmod($filename,0600);
	return $contents;
}

function delete_user_file($company,$user,$filename)
{
	@chmod(QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user,0700);
	$filename = QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.DIR_SLASH.$filename;
	if (!file_exists($filename)) return false;
	//@chmod($filename,01600);
	//$fp = @fopen($filename,'rb');
	//$contents = @fread($fp,filesize($filename));
	//@fclose($fp);
	//@chmod($filename,0600);
	return unlink($filename);
}

function set_user_password($company,$user,$password)
{
	return set_user_file($company,$user,'passwd',generate_password($password));
}


?>
Return current item: Phpqadmin