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

require_once('settings.php');
require_once('functions_user.php');
require_once('functions_company.php');


function checked($val)
{
	return (($val!==false)?' checked ':'');
}

function generate_password($password)
{
	return crypt($password,'Sa');
}

function redir($location)
{
	header("Location: {$location}");
	exit();
}

function create_garbage($length)
{
	$ret = '';
	for ($i=0;$i<$length;$i++)
	{
		$ret .= chr(rand(1,255));
	}
	return $ret;
}

function update_databases()
{
	update_assign();
	update_poppasswd();
	update_domains();
}

function update_domains()
{
	$companies = get_companies();
	$rcpt = '';
	$virtualdomains = '';
	foreach($companies as $k => $v)
	{
		$company = $v['ID'];
		$arr = get_company_domains($company);
		if (is_array($arr))
  		foreach($arr as $ck => $cv)
  		{
  			$virtualdomains .= trim($cv).':'.$company."\n";
			$rcpt .= trim($cv)."\n";
  		}
	}
	echo $rcpt;
	$fp = fopen(QMAIL_CONTROL_DIR.DIR_SLASH.'rcpthosts','wb');
	@flock($fp,LOCK_EX);
	fwrite($fp,$rcpt);
	@flock($fp,LOCK_UN);
	fclose($fp);
	$fp = fopen(QMAIL_CONTROL_DIR.DIR_SLASH.'virtualdomains','wb');
	@flock($fp,LOCK_EX);
	fwrite($fp,$virtualdomains);
	@flock($fp,LOCK_UN);
	fclose($fp);
	exec(QMAIL_BIN_RELOAD);
	return true;
}

function update_poppasswd()
{
	$companies = get_companies();
	$pass = '';
	foreach($companies as $ck => $cv)
	{
		$company = $cv['ID'];
		$users = get_users($company);
		foreach ($users as $uk => $uv)
		{
			$user = $uv['ID'];
			$user_pass = get_user_file($company,$user,'passwd');
			
			$pass .= $company.'|'.$user.':';
			$pass .= $user_pass.':';
			$pass .= QMAIL_USER.':';
			$pass .= QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user;
			$pass .= "\n";
		}
	}
	$fp = fopen(QMAIL_USERS_DIR.DIR_SLASH.'poppasswd','wb');
	@flock($fp,LOCK_EX);
	fwrite($fp,$pass);
	@flock($fp,LOCK_UN);
	fclose($fp);
	return true;
}

function update_assign()
{
	$companies = get_companies();
	$assign = '';
	foreach($companies as $ck => $cv)
	{
		$company = $cv['ID'];
		$users = get_users($company);
		foreach ($users as $uk => $uv)
		{
			$user = $uv['ID'];
			$aliases = get_user_aliases($company,$user);
			$mailinglist = get_user_file($company,$user,'mailinglist'); 
        	if ($mailinglist)
        	{
            	$assign .= '='.$company.'-'.$user.':';
            	$assign .= QMAIL_USER.':';
            	$assign .= QMAIL_USER_ID.':';
            	$assign .= QMAIL_GROUP_ID.':';
            	$assign .= QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.'/';
            	$assign .= '-:';
            	$assign .= $user.':';
            	$assign .= "\n";
            
            	$assign .= '+'.$company.'-'.$user.'-:';
            	$assign .= QMAIL_USER.':';
            	$assign .= QMAIL_USER_ID.':';
            	$assign .= QMAIL_GROUP_ID.':';
            	$assign .= QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.'/';
            	$assign .= '-:';
            	$assign .= $user.'-default:';
            	$assign .= "\n";
			}
			else if (is_array($aliases))
			{
    			foreach ($aliases as $ak => $av)
    			{
    				if (!trim($av))
    				{
    					continue;
    				}
    				else
    				{
        				$assign .= substr($av,0,1).$company.'-'.trim(substr($av,1)).':';
        				$assign .= QMAIL_USER.':';
        				$assign .= QMAIL_USER_ID.':';
        				$assign .= QMAIL_GROUP_ID.':';
        				$assign .= QMAIL_POP_DIR.DIR_SLASH.$company.DIR_SLASH.$user.'/:';
        				$assign .= ':';
        				$assign .= ':';
        				$assign .= "\n";
    				}
    			}
			}
		}
	}
	$assign .= '.';
	$filename = QMAIL_USERS_DIR.DIR_SLASH.'assign';
	$fp = fopen($filename,'rb');
	$old_assign = fread($fp,filesize($filename));
	fclose($fp); 
	echo "old assign: ".$assign."\n";
	if ($old_assign != $assign)
	{
    	$fp = fopen($filename,'wb');
		@flock($fp,LOCK_EX);
    	fwrite($fp,$assign);
		@flock($fp,LOCK_UN);
    	fclose($fp);
		exec(QMAIL_BIN_NEWU);
	}
	$a = exec(QMAIL_BIN_NEWU."");
	return true;
}


?>
Return current item: Phpqadmin