Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1008
  • Last Modified:

php cpanel 11x sub-domain permissions issues

I'm having an issue with sub-domain permissions.

Synopsis:
I need to make a turn key solution for creating sub-domains and then copying the directory structure and files from a template sub-domain to the new sub-domain.

Using a script from http://www.zubrag.com and making the 2 simple mods to use on cpanel version 11+, the sub-doamin creation seems to work like a charm.
My script then continues on to copy the needed structure to the new sub-domain, using something like this:

<?
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 );
      }
}
?>

and

<?

$source ='../template/';
$target = '../test3';
full_copy($source, $target)

?>


This fails.  Trying to use chmod in the script doesn't work either.

Warning: chmod() [function.chmod]: Operation not permitted in /home/ccooke/public_html/cpanel_subdomains/cpanel_subdomains.php on line 154

Warning: copy(../test3/roadtrip1.php) [function.copy]: failed to open stream: Permission denied in /home/ccooke/public_html/cpanel_subdomains/cpanel_subdomains.php on line 15

The copy works fine if I change the permissions of test3 to 777 via ftp GUI.

There was another post on this but the actual answer was not provided.
Quote: Sub-domain issue was sorted out ourselves and we discovered, that rather than the permissions being an incorrect install parameter, it was due an incorrect parameter in the user account, hence it was solved by ourselves."
Close Quote.

I asked for a more complete answer about what he found, but have heard nothing back yet.

As always, this is an emergency.
Full points will be awarded.
Thanks in advance,
Please advise...
0
cacookejr
Asked:
cacookejr
1 Solution
 
cacookejrAuthor Commented:
After many, many hours of trial and error, I have found a way to make this to work for me.
I have used your resource for many years (as a paying member) and am very suprised that lately no one has been able to assist in my questions.  Am I not stating the issue correctly?  Not enough detail?

The goal here was to automate making a sub-domain and then copying a template to the new sub-domain, all in 1 script.

As noted in my original post, this fails with using php because of user and owner restrictions.
PHP mkdir works but sets the owner (in my case) to nobody and UID to 99 with file permissions set to 755.  (This is my apache default)
Then the PHP copy function fails because of the permissions.
The directory permissions need to be set at 0777 ( I could be wrong, but the copy function does not seem to work using the same default NOBODY and 99 that the directory was made with)  and then the copy fails.
The script from http://www.zubrag.com to make a sub-domain works like a charm, with the changes made to support cPanel 11+

I have tried shell_exec with limited success as it runs into same the problems with permissions.
 
Anyway, here is my solution.
It is not elegant, but works for me.

Use the php ftp functions to create the directories and set file permissions.  This will ensure that the owner etc is the correct one for the new sub-domian.
Sample Script:

// set up basic connection
$ftp_server = 'ftp.yourdomain.com';

$conn_id = ftp_connect($ftp_server);
$ftp_user_name = 'yourusername';
$ftp_user_pass = 'yourpassword';

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!";
            echo '<br>';
        echo "Attempted to connect to $ftp_server for user $ftp_user_name";
            echo '<br>';
        exit;
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
            echo '<br>';
    }

echo "Current directory: " . ftp_pwd($conn_id) . "<br>";

if (ftp_chdir($conn_id, "public_html")) {
    echo "Current directory is now: " . ftp_pwd($conn_id) . "<br>";
} else {
    echo "Couldn't change directory";
}

// try to create the directory $dir
//Note: I put in this line because my GOODURL is actuall parsed out of a databse entry.
$goodurl = "newsubdomaindir";

//Make new directory - This wil create the new directory with the proper owner and id
 $dir = $goodurl;
if (ftp_mkdir($conn_id, $dir)) {
 echo "successfully created $dir<br>";
} else {
 echo "There was a problem while creating $dir<br>";
}

//CHMOD New directory to 777 to allow for directory/file (template) copy
if (ftp_chmod($conn_id, 0777, $dir) !== false) {
 echo "$dir chmoded successfully to 777<br>";
} else {
 echo "could not chmod $dir<br>";
}
ftp_close($conn_id);

//Now you can run the copy script

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 );
      }
}
//I'm running this script fro another sub-domain so your paths may vary for the next 2 lines
$source ='../' . $goodtemplate . '/';
$target = '../' . $goodurl;
full_copy($source, $target);

// Now run the sub-domain creation script
###############################################################
# cPanel Subdomains Creator 1.1
###############################################################
# Visit http://www.zubrag.com/scripts/ for updates
###############################################################
#
# Can be used in 3 ways:
# 1. just open script in browser and fill the form
# 2. pass all info via url and form will not appear
# Sample: cpanel_subdomains.php?cpaneluser=USER&cpanelpass=PASSWORD&domain=DOMAIN&subdomain=SUBDOMAIN
# 3. list subdomains in file. In this case you must provide all the defaults below
#
# Note: you can omit any parameter, except "subdomain".
# When omitted, default value specified below will be taken
###############################################################

// cpanel user
define('CPANELUSER','maindomainuser');

// cpanel password
define('CPANELPASS','maindomainpassword');

// name of the subdomains list file.
// file format may be 1 column or 2 columns divided with semicilon (;)
// Example for two columns:
//   rootdomain1;subdomain1
//   rootdomain1;subdomain2
// Example for one columns:
//   subdomain1
//   subdomain2
define('INPUT_FILE','domains.txt');

// cPanel skin (mainly "x")
// Check http://www.zubrag.com/articles/determine-cpanel-skin.php
// to know it for sure
define('CPANEL_SKIN','x3');

// Default domain (subdomains will be created for this domain)
// Will be used if not passed via parameter and not set in subdomains file
define('DOMAIN','maindoamin.com');


/////////////// END OF INITIAL SETTINGS ////////////////////////
////////////////////////////////////////////////////////////////

function getVar($name, $def = '') {
  if (isset($_REQUEST[$name]) && ($_REQUEST[$name] != ''))
    return $_REQUEST[$name];
  else
    return $def;
}

$cpaneluser=getVar('cpaneluser', CPANELUSER);
$cpanelpass=getVar('cpanelpass', CPANELPASS);
$cpanel_skin = getVar('cpanelskin', CPANEL_SKIN);

$_REQUEST["subdomain"] = $goodurl;

if (isset($_REQUEST["subdomain"])) {
  // get parameters passed via URL or form, emulate string from file
  $doms = array( getVar('domain', DOMAIN) . ";" . $_REQUEST["subdomain"]);
  if (getVar('domain', DOMAIN) == '') die("You must specify domain name");
}
else {
  // open file with domains list
  $doms = @file(INPUT_FILE);
  if (!$doms) {
    // file does not exist, show input form
    echo "
Cannot find input file with subdomains information. It is ok if you are not creating subdomains from file.<br>
Tip: leave field empty to use default value you have specified in the script's code.<br>
<form method='post'>
  Subdomain:<input name='subdomain'><br>
  Domain:<input name='domain'><br>
  cPanel User:<input name='cpaneluser'><br>
  cPanel Password:<input name='cpanelpass'><br>
  cPanel Skin:<input name='cpanelskin'><br>
  <input type='submit' value='Create Subdomain' style='border:1px solid black'>
</form>";
    die();
  }
}

// create subdomain
function subd($host,$port,$ownername,$passw,$request) {

  $sock = fsockopen('localhost',2082);
  if(!$sock) {
    print('Socket error');
    exit();
  }

  $authstr = "$ownername:$passw";
  $pass = base64_encode($authstr);
  $in = "GET $request\r\n";
  $in .= "HTTP/1.0\r\n";
  $in .= "Host:$host\r\n";
  $in .= "Authorization: Basic $pass\r\n";
  $in .= "\r\n";
 
  fputs($sock, $in);
  while (!feof($sock)) {
    $result .= fgets ($sock,128);
  }
  fclose( $sock );

  return $result;
}

foreach($doms as $dom) {
  $lines = explode(';',$dom);
  if (count($lines) == 2) {
    // domain and subdomain passed
    $domain = trim($lines[0]);
    $subd = trim($lines[1]);
  }
  else {
    // only subdomain passed
    $domain = getVar('domain', DOMAIN);
    $subd = trim($lines[0]);
  }
  // http://[domainhere]:2082/frontend/x/subdomain/doadddomain.html?domain=[subdomain here]&rootdomain=[domain here]
  // orignial $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd";
  $dir = 'public_html/' . $subd; // the Document Root folder for subdomain
  $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd&dir=$dir";

  $result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request);
  $show = strip_tags($result);
  echo $show;

This script, actually cPanel, will reset the permsissions to 644 after creating the sub-domain..
If you still need them to be 777, re-initiate an ftp session like above to change the permissions to what you need.  Your FTP GUI, cPanel and everthing else else will still work as normal, just as if you created a sub-domain manually and copied all the files manually.

If anyone has questions or comments about this, please feel free to contact me.

 




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now