Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php cpanel 11x sub-domain permissions issues

Posted on 2009-04-12
5
Medium Priority
?
1,001 Views
Last Modified: 2013-12-20
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
Comment
Question by:cacookejr
1 Comment
 
LVL 1

Accepted Solution

by:
cacookejr earned 0 total points
ID: 24201680
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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question