Sunday, June 24, 2012

PHP Script to create subdomains

function create_subdomain($subDomain,$cPanelUser,$cPanelPass,$rootDomain)
    {
        $buildRequest = "/frontend/x3/subdomain/doadddomain.html?rootdomain=" . $rootDomain . "&domain=" . $subDomain . "&dir=public_html/subdomains/affiliate/" . $subDomain;
   
        $openSocket = fsockopen('localhost',2082);
        if(!$openSocket)
        {
            return "Socket error";
            exit();
        }
        $authString = $cPanelUser . ":" . $cPanelPass;
        $authPass = base64_encode($authString);
        $buildHeaders  = "GET " . $buildRequest ."\r\n";
        $buildHeaders .= "HTTP/1.0\r\n";
        $buildHeaders .= "Host:localhost\r\n";
        $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
        $buildHeaders .= "\r\n";
   
        fputs($openSocket, $buildHeaders);
        while(!feof($openSocket))
        {
            fgets($openSocket,128);
        }
        fclose($openSocket);
        //$newDomain = "http://" . $subDomain . "." . $rootDomain . "/";
        return "Created subdomain $newDomain";
    }
   
    function full_copy($source,$target)
    {
        if(is_dir($source))
        {
            @mkdir($target);
            $d = dir($source);
            while(FALSE !== ($entry = $d->read()))
            {
                if($entry == '.' || $entry == '..')
                {
                    continue;
                }
                $Entry = $source . '/' . $entry;         
                if(is_dir($Entry))
                {
                    full_copy( $Entry, $target . '/' . $entry );
                    continue;
                }
                copy($Entry,$target.'/'.$entry);
            }
            $d->close();
        }
        else
        {
            copy($source,$target);
        }
    }
    ///////////////////////// for insert into table ////////////////////////////////////////
    if(isset($_POST["txtSubmitAffiliate"]))
    {
        $strSubdomain = $_POST["txtSubdomainname"];
        $sqlAffiliateQuery = "INSERT INTO affiliate SET
        username='".$_POST["txtUsername"]."',
        password='".$_POST["txtPassword"]."',
        name='".$_POST["txtName"]."',
        email='".$_POST["txtEmail"]."',
        sub_domain_name='".$_POST["txtSubdomainname"]."',
        street_address='".$_POST["txtStreetAddress"]."',
        city='".$_POST["txtCity"]."',
        state='".$_POST["txtState"]."',
        country='".$_POST["txtCountry"]."',
        zipcode='".$_POST["txtZipcode"]."',
        status='1',
        created_at=NOW(),
        updated_at=NOW()";
        db_res($sqlAffiliateQuery);
       
        $intLastId = mysql_insert_id();
        @mkdir($dir['root']."subdomains/affiliate/".$strSubdomain, 0777);
        full_copy($dir['root']."resources/affiliate/",$dir['root']."subdomains/affiliate/".$strSubdomain."/");
       
        @unlink($dir['root']."subdomains/affiliate/".$strSubdomain."/inc/config.inc.php");
       
        $strContentfile = file_get_contents($dir['root']."resources/affiliate/inc/config.sample.inc.php", true);
        $strContentfile = str_replace('#aff_id#',$intLastId,$strContentfile);
        $strContentfile = str_replace('#sub_url#',$strSubdomain,$strContentfile);
        $strContentfile = str_replace('#root_dir#',$strSubdomain,$strContentfile);
       
        $myFile = $dir['root']."subdomains/affiliate/".$strSubdomain."/inc/config.sample.inc.php";
        $fh = fopen($myFile, 'w');
        fwrite($fh, $strContentfile);
        fclose($fh);
       
        $oldFile = $dir['root']."subdomains/affiliate/".$strSubdomain."/inc/config.sample.inc.php";
        $newFile = $dir['root']."subdomains/affiliate/".$strSubdomain."/inc/config.inc.php";
        @rename($oldFile,$newFile);
       
        $subdomain = create_subdomain($strSubdomain,'newyugco','9730951424','newyug.com');
        @mkdir($dir['root']."subdomains/affiliate/".$strSubdomain, 0755);
       
        header("Location:".$site["url"]."affiliate_success.php?msg=success");
    }

No comments:

Post a Comment

Please mention your comments.......