Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/functions.inc.phps
<?	/*
	// File:	functions.inc.php
	// Purpose:	web://cp server functions (API)
	// Creation:	2002-02-28
	// Author:	Felix <hide@address.com>
	*/

// system file update function.

function updatesys($type) {
	GLOBAL $cfg, $remapmail, $restarthttp, $restartmail, $restartdns, $echo;

	// Include service 'modules'
	$libpath = $cfg['basedir']."/server/lib/";
	
	// Web
	include($libpath."apache.sys.phps");
	
	// SMTP
	if ($cfg['mailserver'] == 'sendmail')
		include($libpath."sendmail.sys.phps");
	
	// DNS
	// 3 possibilities : none|mail|bind
	if ($cfg['dns_system'] == 'bind' OR $cfg['dns2_system'] == 'webcp')
		include($libpath."bind.sys.phps");

} // end of system file function


   
// domain and user system manipulation
function webcp($type,$data) {
	GLOBAL $cfg, $restarthttp, $restartdns, $echo;
	
	//
	// Include service 'modules'
	$libpath = $cfg['basedir']."/server/lib/";
	
	// Web
	include($libpath."apache.wcp.phps");
	
	// Apache Server-side modules
	$tmp = explode(',',$cfg['sslang']);
	foreach($tmp AS $val) {
		// Webware Support
		if ($val == 'webware')
			include($libpath."webware.wcp.phps");
	}
		
	// POP3
	include($libpath."pop3.wcp.phps");
	
	// UW-IMAP
	if ($cfg['uwimap'])
		include($libpath."uw-imap.wcp.phps");
	
	// SMTP
	if ($cfg['mailserver'] == 'virtualqmail')
		include($libpath."virtualqmail.wcp.phps");
	elseif ($cfg['mailserver'] == 'sendmail')
		include($libpath."sendmail.wcp.phps");
	
	// PureFTPd
	if ($cfg['ftpserver'] == 'pureftpd')
		include($libpath."pureftpd.wcp.phps");
		
	// DNS management
	if ($cfg['dns_system'] == 'bind')
		include($libpath."bind.wcp.phps");
	elseif ($cfg['dns_system'] == 'mail')
		include($libpath."dnsmail.wcp.phps");
	elseif ($cfg['dns_system'] == 'tinydns')
		include($libpath."tinydns.wcp.phps");
	elseif ($cfg['dns_system'] == 'mydns')
		include($libpath."mydns.wcp.phps");
	elseif ($cfg['dns_system'] == 'pdns')
		include($libpath."pdns.wcp.phps");
		
	
	// Creation of domains on the system
	include($libpath."domain.wcp.phps");
	
	// Creation of users on the system
	include($libpath."user.wcp.phps");
	
	// Database Update
	include($libpath."mysql.wcp.phps");

}


// seed the random number generator only once
function make_seed() {
	static $randomized;

	// php/4.2.0+ seeds itself
	if (!$randomized && version_compare(phpversion(), "4.2.0", "<")) {
		list($usec, $sec) = explode(' ', microtime());

		$init = (int) $sec + (int)($sec * $usec);
		$init .= @getmypid();

		$seed = hexdec(substr(md5($init), -8)) & 0x7fffffff;
		mt_srand($seed);
		$randomized = true;
	}
}


// return a random string of $length characters
function make_salt($length='8') {
	make_seed();

	$char_list = '0123456789'.
		'abcdefghijklmnopqrstuvwxyz'.
		'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
		'@^&/.'.
		'';

	$salt = '';
	while (strlen($salt) < $length) {
		$rand = mt_rand(0, strlen($char_list) - 1);
		$salt .= $char_list[$rand];
	}
	return $salt;
}


// function to restart all services / reload their config under 'any' OS
// only restart what's necessary
function reload_services() {
	GLOBAL $cfg, $echo, $remapmail, $restartmail, $restarthttp, $restartdns;
	
	// Check signals to restart whats needed (or remap sendmail)
	if ($remapmail AND !$restartmail) {
		// interactive dump
		webcp_log(3,0,'webcp.php',"Remapping Mail",0,$echo);
		
		if ($cfg['mailserver'] == 'sendmail') {
			if ($cfg['os'] == 'linux') {
				exec($cfg['prog']['nalias']." > /dev/null 2>&1");
				exec($cfg['prog']['makemap']." hash ".$cfg['mail_virtuser']." < ".$cfg['mail_virtuser']." > /dev/null 2>&1");
				exec($cfg['prog']['makemap']." hash ".$cfg['mail_access']." < ".$cfg['mail_access']." > /dev/null 2>&1");
			}
			elseif ($cfg["os"] == "freebsd"){
				$pwd = getcwd();
				chdir("/etc/mail");
				exec($cfg['prog']['make']." maps > /dev/null 2>&1");
				exec($cfg['prog']['make']." aliases > /dev/null 2>&1");
				chdir($pwd);
			}
		}
		
	}
	if ($restarthttp) {
		// interactive dump
		webcp_log(3,0,'webcp.php',"Restarting HTTP",0,$echo);
		exec($cfg['init']['httpd']." restart");
			
	}
	if ($restartdns) {
		// interactive dump
		webcp_log(3,0,'webcp.php',"Restarting DNS",0,$echo);
		
		switch($cfg["dns_system"]){
			case 'bind':
				if ($cfg['os'] == 'linux')
					exec($cfg['init']['named']." restart");
				elseif ($cfg["os"] == "freebsd"){
					exec("/bin/kill -HUP `cat ".$cfg["spid"]["named"]."` > /dev/null 2>&1");
				}
				break;
			case 'mydns':
				exec($cfg['init']['mydns']." restart");
				break;
			case 'tinydns':
				$pwd = getcwd();
				chdir($cfg["tinydns_dir"]."/root");
				exec("make",$dummy);
				chdir($pwd);
				break;
		}
	}
	if ($restartmail) {
		// interactive dump
		webcp_log(3,0,'webcp.php',"Restarting Mail",0,$echo);
		
		if ($cfg['os'] == 'linux') {
			exec($cfg['init']['sendmail']." stop");
			sleep(1);
			exec('id=`ps aux |grep sendmail |grep -v grep |cut -c 8-15` && test $id && kill $id');
			exec($cfg['init']['sendmail']." start");
		}
		elseif ($cfg["os"] == "freebsd"){
			$pwd = getcwd();
			chdir("/etc/mail");
			exec($cfg['prog']['make']." restart > /dev/null 2>&1");
			chdir($pwd);
		}
	}
	
	// Re-initialize
	$remapmail = FALSE;
	$restarthttp = FALSE;
	$restartdns = FALSE;
	$restartmail = FALSE;
}

function file_edit($pattern, $replace, $file_name) {
/* replace all matches in a file according to a perl compatible
 * regular expression. */

	if ($fp = fopen($file_name, 'r+b')) {
		flock($fp, LOCK_EX);

		/* read file into variable */
		$content = '';
		while (!feof($fp)) {
			$content .= fread($fp, 4096);
		}

		/* debated going straight to preg_replace() for speed reasons
		 * but decided it was too useful to know if it was successful
		 * and how many replaces took place. */
		$result = preg_match_all($pattern, $content, $match);
		if ($result > 0) {
			/* replace strings in the current file using regex */
			$content = preg_replace($pattern, $replace, $content, $result);
		}

		/* go back to start of file, write new data, then
		 * wipe out any data left over from before */
		rewind($fp);
		fwrite($fp, $content, strlen($content));
		fflush($fp);
		ftruncate($fp, ftell($fp));

		flock($fp, LOCK_UN);
		fclose($fp);
	} else {
		$result = false;
	}

	return $result;
}

function file_append($append, $file_name) {
/* Add to the end of a file */
	if ($fp = fopen($file_name, 'ab')) {
		flock($fp, LOCK_EX);

		if (fwrite($fp, $append, strlen($append)) !== false) {
			$result = true;
		} else {
			$result = false;
		}
		
		flock($fp, LOCK_UN);
		fclose($fp);

	} else {
		$result = false;
	}
	return $result;
}

function user_exists($username) {
	// Check for existing user
	// Use getent if it is defined and grep otherwise
	if ($cfg['prog']['getent'] != "") {
		$userexists = (exec($cfg['prog']['getent'].' passwd '.$username) != "") ? true : false;
	} else {
		$userexists = (exec('grep '.$username.": /etc/passwd") != "") ? true : false;
	}
	return $userexists;
}

?>
Return current item: web-cp - Web Hosting Control Panel