markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Hello all. I am using the following script to try to auto create an account but it does nothing.

What am I missing here - Something major I'm guessing?

PHP:
<form action="setup.php" method='post'>
<table border="0" align="center" cellpadding="3" cellspacing="0">
  <tr>
    <td><div align="right">
      <input name="domain" type="text" id="domain">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input name="pass" type="text" id="pass">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input name="user" type="text" id="user">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input type="submit" name="Submit" value="Submit">
    </div></td>
  </tr>
</table></form>

<?php

require '/usr/local/cpanel/Cpanel/Accounting.php.inc';
$host = "localhost";
$user = "root";
$accesshash = 'The remote access key goes here, I think';
$usessl = "0";
$acctdomain = "";
$acctplan = "Business Package";

createacct ($host,$user,$accesshash,$usessl,$acctdomain,$acctuser,$acctpass,$acctplan);

?>
 

Spiral

BANNED
Jun 24, 2005
2,020
8
193
What am I missing here - Something major I'm guessing?
Your basic format is good except that the way your example is written,
you would depend on register global being enabled plus you are not
properly setting your variable names and you made a few mistakes
where variable names in the script are mismatched from the form.

I would do something a little more like the following:
Code:
<form action="setup.php" method='post'>
<table border="0" align="center" cellpadding="3" cellspacing="0">
  <tr>
    <td><div align="right">
      <input name="domain" type="text">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input name="apass" type="text">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input name="auser" type="text">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input type="submit" name="Submit" value="Submit">
    </div></td>
  </tr>
</table></form>
PHP:
<?php
require '/usr/local/cpanel/Cpanel/Accounting.php.inc';

foreach($_POST as $key => $val) {
  $$key = strtolower(trim(stripslashes($val)));
}

$host = "localhost";
$user = "root";
$rkey = '(paste your remote access key here)';
$plan = "(paste your plan name here)";

@createacct("$host", "$user", "$rkey", 0, "$domain", "$auser", "$apass", "$plan");
Note that you originally named your plan "Business Package" which is a bad name!

Names of account packages should not contain any spaces or punctuation
which the sole exception being the underscore (_) character which is permitted.

The variables used in your "createacct" function should match the names of
the respective variables used in your form. In your original example, you had
different variable names for the username and password for your form and
for the createacct function in your PHP script which is your main problem here.

Because of the "require" line calling a file outside the web path, you will need
to either disable "open_basedir" for the hosting account using the setup script
or you will need to add the /usr/local/cpanel/Cpanel path to open_basedir
in the /etc/httpd/conf/httpd.conf configuration file.

Naturally you can greatly expand upon this sample code and add in sanity checks
and test code to make sure the proper variables have been set but, this is
the basics of what you need to know here.
 
Last edited:

markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Excellent. Works great. Is there a way to set the nameservers using this script?

It sets up the account but for some reason with the wrong nameserver (auth20.ns.wcom.com).

I know my NS's are configured correctly in the WHM on this server, so is this "auth20.ns.wcom.com" in the config on the file this script is requiring?
EDIT: I just looked in the file /usr/local/cpanel/Cpanel/Accounting.php.inc and there isn't anything in there like that.

Where do you think that NS is coming from?
 
Last edited:

Spiral

BANNED
Jun 24, 2005
2,020
8
193
Using the root hash in your scripts, the DNS servers you want to use as the default
would be the ones specfied in WHM's "Basic cPanel/WHM Setup".

Also under "Tweak Settings" the following option should NOT be checked:
Code:
[i]"When adding a new domain, if the domain is already registered, ignore the configured nameservers, and set the NS line to the authoritative (registered) ones."[/i]
Things are a little bit different for resellers ....

For resellers, their default DNS servers are configured in the "Reseller Center".
 

markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Ok, got it. Thank you very much. It's because the test domain was already registered. :eek:

Thank you very much for your help!
 

markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Sorry to ask again, but what would be a way to list accounts with this script?

Thanks in advance.
 

Spiral

BANNED
Jun 24, 2005
2,020
8
193
Sorry to ask again, but what would be a way to list accounts with this script?

Thanks in advance.

Yes, there is a function in the API to list account ....

Code:
$accounts = listaccts ($host,$user,$accesshash,$usessl);
$host -- usually localhost

$user -- 'root' or whatever reseller user you are using

$accesshash -- same hash as the account create function

$usessl --- if using SSL then '1', else set to '0'


Aside from the API, another simple way for getting a list of usernames
is just call "ls /var/cpanel/users" on the system and pipe the results back.
 

markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Thank you very much. You truly are a guru! :)
 

markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Yes, there is a function in the API to list account ....

Code:
$accounts = listaccts ($host,$user,$accesshash,$usessl);
$host -- usually localhost

$user -- 'root' or whatever reseller user you are using

$accesshash -- same hash as the account create function

$usessl --- if using SSL then '1', else set to '0'


Aside from the API, another simple way for getting a list of usernames
is just call "ls /var/cpanel/users" on the system and pipe the results back.
That's the one to use, jameshsi.
 

jameshsi

Well-Known Member
Oct 22, 2001
347
0
316
Hi!
Thanks for your reply,markfrompf, but what I want is to list a remote server's accounts, not localhost, it should be able to do that if you put the remote server's IP to $host , right, but the truth is, you can only list all accounts on the same server your scripts reside, am I right about this ?
 

markfrompf

Well-Known Member
Mar 27, 2006
176
0
166
Los Angeles, CA
Not necessarily, but quite possibly. I'd definitely give it a shot.

:) Good luck! Please let us know about the outcome of this.
 

EstebanC

Member
Jul 1, 2007
9
0
51
Hello, I am using the example provided by Spiral but the page setup.php just stay in blank.

What is my error?

thanks


Your basic format is good except that the way your example is written,
you would depend on register global being enabled plus you are not
properly setting your variable names and you made a few mistakes
where variable names in the script are mismatched from the form.

I would do something a little more like the following:
Code:
<form action="setup.php" method='post'>
<table border="0" align="center" cellpadding="3" cellspacing="0">
  <tr>
    <td><div align="right">
      <input name="domain" type="text">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input name="apass" type="text">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input name="auser" type="text">
    </div></td>
  </tr>
  <tr>
    <td><div align="right">
      <input type="submit" name="Submit" value="Submit">
    </div></td>
  </tr>
</table></form>
PHP:
<?php
require '/usr/local/cpanel/Cpanel/Accounting.php.inc';

foreach($_POST as $key => $val) {
  $$key = strtolower(trim(stripslashes($val)));
}

$host = "localhost";
$user = "root";
$rkey = '(paste your remote access key here)';
$plan = "(paste your plan name here)";

@createacct("$host", "$user", "$rkey", 0, "$domain", "$auser", "$apass", "$plan");
Note that you originally named your plan "Business Package" which is a bad name!

Names of account packages should not contain any spaces or punctuation
which the sole exception being the underscore (_) character which is permitted.

The variables used in your "createacct" function should match the names of
the respective variables used in your form. In your original example, you had
different variable names for the username and password for your form and
for the createacct function in your PHP script which is your main problem here.

Because of the "require" line calling a file outside the web path, you will need
to either disable "open_basedir" for the hosting account using the setup script
or you will need to add the /usr/local/cpanel/Cpanel path to open_basedir
in the /etc/httpd/conf/httpd.conf configuration file.

Naturally you can greatly expand upon this sample code and add in sanity checks
and test code to make sure the proper variables have been set but, this is
the basics of what you need to know here.
 

EstebanC

Member
Jul 1, 2007
9
0
51
Solved!

the problem was that I must set the "username" with 8 characters and setup a stronger password ;)

Now, I only need to know which is the "email" contact variable, I tried with "acctcontact" but it didn't work.

Any help? :$

Thanks!
 
Last edited:

Ryan88

Registered
Mar 6, 2011
1
0
51
Hate to bump a thread from 2008 but does anyone know how to add a e-mail variable to this?
 

MrLeN

Active Member
Dec 12, 2010
38
0
56
I have set this up also, but once I run the script, it just shows setup.php as a blank white page.

Then, when I go into WHM to see if the account was created, it wasn't.

I realise this thread is old (I got here from a Google search).

I am using cPanel 11.30.5

Any ideas?
 
Last edited:

MrLeN

Active Member
Dec 12, 2010
38
0
56
I am getting sick and tired :(

How hard can it possibly be to find a script on the Internet that allows php to add an account to WHM.

There's scripts everywhere, but most of them are old and don't work because now cpanel requires a key.

And any new script I can find, just plain doesn't work.

ie: http://forums.cpanel.net/f42/create-account-cpanel-php-251911.html

Why can't I find this anywhere?
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,216
11
313
Houston, TX
cPanel Access Level
Root Administrator
I have set this up also, but once I run the script, it just shows setup.php as a blank white page.

Then, when I go into WHM to see if the account was created, it wasn't.

I realise this thread is old (I got here from a Google search).

I am using cPanel 11.30.5

Any ideas?
cPanel has changed much in 6 years, and this post predates most of the APIs currently available in the product. Those looking for more information and resources can now reference cPanel & WHM's SDK
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,216
11
313
Houston, TX
cPanel Access Level
Root Administrator
There's scripts everywhere, but most of them are old and don't work because now cpanel requires a key.
Actually there are several scripts in production use based off that PHP class on that thread. Furthermore, we do not require a key, that is every bit as optional as it was back in 2006. However, it is generally considered good practice to use a key.

Many scripts from over 6 years ago no longer work due to them relying on kludges that were in place prior to us officially supporting those functionalities via API as well as new security functionality introduced over the years such as CSRF protection which older scripts have a tendency to not accommodate well. This is why we recommend use of the APIs (instead of workaround kludges) as it helps ensure scripts that work today will continue to work for years into the future.