Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/lib/domain.wcp.phps
<?	/*
	// File:	linux-domain.wcp.phps
	// Purpose:	linux domain manipulation
	// Author:	Felix <hide@address.com>
	*/



// Handle domains manipulation
if ($type == "domain") {
	
	// Handle Domain Creation :: expect *
	if ($data['action'] == 'create') {
		
		// Create new account
		if ($data['type'] == 'domain' OR $data['type'] == 'subdomain') {
			// Alias ID to domain if its a sub, else create a new dir; create group & set hd quota if necessary.
			if ($data['type'] == 'subdomain') {
				$gid = "g".$data['owner'];
				symlink($cfg['webdir']."/".$data['owner'], $data['path']);
			}
			elseif ($data['type'] == 'domain') {
				$gid = "g".$data['id'];
				if (!file_exists($data['path']))
					mkdir($data['path'],0755);
				
				if (!file_exists($data['path']."/_logs"))
					mkdir($data['path']."/_logs",0755);
					
				if (!file_exists($data['path']."/_backups"))
					mkdir($data['path']."/_backups",0777);
					
				$hdquota = $data['hd'] * 1024;
				if ($cfg['os'] == 'linux') {
					exec($cfg['prog']['gadd']." ".$gid);
					switch ($cfg['osversion']) {
						case 'RedHat6.2':
					//	case 'Suse7.2':
					//	...
							exec($cfg['prog']['squota']." -g $gid ".$cfg['devhd']." $hdquota $hdquota 0 0");
							break;
						default:
							exec($cfg['prog']['squota']." -g $gid $hdquota $hdquota 0 0 ".$cfg['devhd']);
					}
				}
				elseif ($cfg['os'] == 'freebsd') {
					exec($cfg['prog']['pw']." groupadd -q -n $gid");
					/*
						To add a group quota on FreeBSD:
						edquota -e /home:0:99999999 -g group
						adds a soft/hard limit of 0/9999999 kb for the group 'group' on filesystem /home
					*/
					exec($cfg['prog']['squota']." ".$cfg['webdir'].":0:$hdquota -g $gid");
				}
			}
			
			if (file_exists($data['path']) AND $gid) {
				// Create needed directories
				$path = $data['path']."/".$data['host'];
				if (!file_exists($path))
					mkdir($path,0755);
				if (!file_exists($path."/".$cfg['webname']))
					mkdir($path."/".$cfg['webname'],0775);
				if (!file_exists($path."/users"))
					mkdir($path."/users",0755);
				if (!file_exists($path."/data"))
					mkdir($path."/data",0775);
				if (!file_exists($path."/ssl"))
					mkdir($path."/ssl",0755);
				if (!file_exists($path."/logs"))
					mkdir($path."/logs",0755);
				
				// Copy over skel content to new account, then 'parse' index.html
				if (!file_exists($path."/".$cfg['webname']."/index.html"))
					exec($cfg['prog']['cp']." -Rf ".$cfg['basedir']."/skel/* ".$path."/".$cfg['webname']);
				
				if (file_exists($path."/".$cfg['webname']."/index.html")) {
					// fetch reseller name & email
					$tmp = fetchdata("name,email","reseller",$data['owner']);
					$index_skel = @implode("",file($path."/".$cfg['webname']."/index.html"));
					$index_skel = str_replace("[HOST]",$data['host'],$index_skel);
	 				$index_skel = str_replace("[DOMAIN]",$data['domain'],$index_skel);
	 				$index_skel = str_replace("[RESELLER NAME]",$tmp['name'],$index_skel);
	 				$index_skel = str_replace("[RESELLER EMAIL]",$tmp['email'],$index_skel);
	 				// write it back parsed
	 				$fp = fopen($path."/".$cfg['webname']."/index.html", "w+");
	 				fwrite($fp,$index_skel,51200);
	 				fclose($fp);
	 			}
	 			
	 			// Set final permissions on the dir structure.
	 			chmod($path,0755);
				chmod($path."/".$cfg['webname'],0775);
				chmod($path."/users",0755);
				chmod($path."/data",0775);
				chmod($path."/ssl",0755);
				chmod($path."/logs",0755);
				// Set groupe permissions
	 			chgrp($path."/".$cfg['webname'],$gid);
	 			chgrp($path."/users",$gid);
	 			chgrp($path."/data",$gid);
	 			// Set permissions on all new web dirs.
	 			$webdir = dirlist($path."/".$cfg['webname'],"dir");
	 			while (list($key, $val) = each($webdir)) {
	 				chmod($val,0775);
	 				chgrp($val,$gid);
	 			}
	 			// Set permissions on all new web files.
	 			$webdir = dirlist($path."/".$cfg['webname'],"file");
	 			while (list($key, $val) = each($webdir)) {
	 				chmod($val,0664);
	 				chgrp($val,$gid);
	 			}
	 			
	 			// Create domain symbolic link (look friendlier to customers)
	 			if (!@is_link($cfg['webdir']."/".$data['domain']) AND $data['type'] != "pointer")
					symlink($data['path'], $cfg['webdir']."/".$data['domain']);
				
				
				// Add cgi-bin if perl support is needed
				if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
					if (!file_exists($path."/".$cfg['webname']."/cgi-bin")) {
						mkdir($path."/".$cfg['webname']."/cgi-bin", 0775);
						chmod($path."/".$cfg['webname']."/cgi-bin",0775);
						chgrp($path."/".$cfg['webname']."/cgi-bin",$gid);
					}
				}
			}
			else // else log it: error
				webcp_log(0,0,"system","Domain Creation Error: ".$data['path']." does not exist",0);
		}
		
		// Create only cgi-bin for pointer if its needed.
		elseif ($data['type'] == "pointer") {
			// get group id
			$gid = "g".$data['owner'];
			
			// Add cgi-bin if perl support is needed
			if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
				if (!file_exists($path."/cgi-bin")) {
					mkdir($path."/cgi-bin", 0775);
					chmod($path."/cgi-bin",0775);
					chgrp($path."/cgi-bin",$gid);
				}
			}
		}
	}
	
	// Handle Domain updates :: expect *
	elseif ($data['action'] == 'update') {
					
		// use host1 for the path in case of a hostname change
		$path = $data['path'];
		
		// get group id and set quota if needed
		if ($data['type'] == 'subdomain' OR $data['type'] == 'pointer')
			$gid = "g".$data['owner'];
		
		elseif ($data['type'] == 'domain') {
			$gid = "g".$data['id'];
			$hdquota = $data['hd'] * 1024;
			if ($cfg['os'] == 'linux') {
				switch ($cfg['osversion']) {
					case 'RedHat6.2':
						exec($cfg['prog']['squota']." -g $gid ".$cfg['devhd']." $hdquota $hdquota 0 0");
						break;
					default:
						exec($cfg['prog']['squota']." -g $gid $hdquota $hdquota 0 0 ".$cfg['devhd']);
				}
			}
			elseif ($cfg['os']== 'freebsd') {
				exec($cfg['prog']['squota']." ".$cfg['webdir'].":0:$hdquota -g $gid");
			}
		}
		
		// Add cgi-bin if perl support is needed
		if ($data['type'] == 'pointer') {
			if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
				if (!file_exists($path."/cgi-bin") AND file_exists($path)) {
					mkdir($path."/cgi-bin", 0775);
					chmod($path."/cgi-bin",0775);
					chgrp($path."/cgi-bin",$gid);
				}
			}
		}
		else {
			if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
				if (!file_exists($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin")) {
					mkdir($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin", 0775);
					chmod($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin",0775);
					chgrp($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin",$gid);
				}
			}
		}
		
		// Check if host or domain changed, handle it.
		if ($data['host'] != $data['host1'] AND $data['type'] != "pointer") {
			$oldpath = $data['path']."/".$data['host1'];
			$newpath = $data['path']."/".$data['host'];
			rename($oldpath,$newpath);
			mysql_query("UPDATE domains SET host1=host WHERE id='".$data['id']."'");
		}
		if ($data['domain'] != $data['domain1'] AND $data['type'] != "pointer") {
			$oldpath = $cfg['webdir']."/".$data['domain1'];
			$newpath = $cfg['webdir']."/".$data['domain'];
			rename($oldpath,$newpath);
			mysql_query("UPDATE domains SET domain1=domain WHERE id='".$data['id']."'");
		}
		// update domain pointers
		if (($data['host'] != $data['host1'] OR $data['domain'] != $data['domain1']) AND $data['type'] == "pointer")
			mysql_query("UPDATE domains SET domain1=domain,host1=host WHERE id='".$data['id']."'");
	}
	
	// Suspend Domain :: expect 'id'
	elseif ($data['action'] == "suspend") {
		if ($data['id']) {
			$tmp = fetchdata("type,path","domain",$data['id']);
						
			// The httpd conf file has the document root modified for suspended domain
			// suspend users
			$dbp = mysql_query("SELECT username,type FROM users WHERE id='".$data['id']."'");
			while ($udata = mysql_fetch_array($dbp)) {
				$udata['action'] = 'suspend';
				webcp("user",$udata);
			}
			
			if ($tmp['type'] == "domain") {
				// suspend sub-domains & domain pointers
				$dbp = mysql_query("SELECT * FROM domains WHERE owner='".$data['id']."'");
				while ($ddata = mysql_fetch_array($dbp)) {
					$ddata['action'] = 'suspend';
					webcp("domain",$ddata);
				}
				
				// chmod home dir 0 if its a domain
				chmod($tmp['path'],0);
			}
		}
	}
	
	// Unsuspend Domain :: expect 'id',
	elseif ($data['action'] == "unsuspend") {
		if ($data['id']) {
			$tmp = fetchdata("type,path","domain",$data['id']);
						
			// The httpd conf file has the document root reset, unsuspend users
			$dbp = mysql_query("SELECT username,type FROM users WHERE id='".$data['id']."'");
			while ($udata = mysql_fetch_array($dbp)) {
				$udata['action'] = 'unsuspend';
				webcp("user",$udata);
			}
			
			
			if ($tmp['type'] == "domain") {
				// unsuspend sub-domains & domain pointers
				$dbp = mysql_query("SELECT * FROM domains WHERE owner='".$data['id']."'");
				while ($ddata = mysql_fetch_array($dbp)) {
					$ddata['action'] = 'unsuspend';
					webcp("domain",$ddata);
				}
				
				// chmod home dir 0755 if its a domain
				chmod($tmp['path'],0755);
			}
				
		}
	}
	
	// Remove Domain :: expect *
	elseif ($data['action'] == "remove") {
		if ($data['id'] AND file_exists($data['path'])) {
			// If its a domain, remove all subdomains and domain pointers and its users
			if ($data['type'] == "domain") {
				$dbp = mysql_query("SELECT * FROM domains WHERE owner='".$data['id']."' AND (type = 'subdomain' OR type='pointer')");
				while ($ddata = mysql_fetch_array($dbp)) {
					$ddata['action'] = 'remove';
					webcp("domain",$ddata);
				}
				$dbp = mysql_query("SELECT * FROM users WHERE id='".$data['id']."'");
				while ($udata = mysql_fetch_array($dbp)) {
					$udata['action'] = 'remove';
					webcp("user",$udata);
				}
			}
			
			// If its a domain remove WEBDIR/ID
			if ($data['type'] == 'domain') {
				// Remove all files.
	 			$webdir = dirlist($data['path'],"file");
	 			while (list($key, $val) = each($webdir))
	 				unlink($val);
				// Remove all sub dirs.
	 			$webdir = dirlist($data['path'],"dir");
	 			arsort($webdir);
	 			reset($webdir);
	 			while (list($key, $val) = each($webdir))
	 				rmdir($val);
	 			// Remove WEBDIR/ID
	 			@rmdir($data['path']);
	 			// Unlink domain symbolic link
	 			unlink($cfg['webdir']."/".$data['domain']);
	 			
	 			// Delete group
	 			$gid = "g".$data['id'];
	 			if ($cfg['os'] == 'linux')
	 				exec($cfg['prog']['gdel']." $gid");
	 			elseif ($cfg['os'] == 'freebsd')
	 				exec($cfg['prog']['pw']." groupdel -q -n $gid");
	 		}
	 		// If its a sub-domain remove WEBDIR/ID/subname
			elseif ($data['type'] == 'subdomain') {
				$path = $data['path']."/".$data['host1'];
				// Remove all files.
	 			$webdir = dirlist($path,"file");
	 			while (list($key, $val) = each($webdir))
	 				unlink($val);
				// Remove all dirs
	 			$webdir = dirlist($path,"dir");
	 			arsort($webdir);
	 			reset($webdir);
	 			while (list($key, $val) = each($webdir))
	 				rmdir($val);
	 			// Remove WEBDIR/ID
	 			@rmdir($path);
	 			// unlink ID alias
	 			unlink($data['path']);
	 		}
	 	}
	}
}
?>
Return current item: web-cp - Web Hosting Control Panel