The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WHM Automatic account setup (PHP)

Discussion in 'cPanel Developers' started by ryanbnm666, Nov 29, 2012.

  1. ryanbnm666

    ryanbnm666 Member

    Joined:
    Nov 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Reseller Owner
    Hey,

    First I would like to say sorry if I posted this in the wrong place.
    Second I would like to make it known that I am relatively new to PHP and have only it for for basics like MySQL databases etc.

    I am in the process of starting a web-hosting website and I am looking to have the cPanel accounts set up automatically when a user purchases the hosting. I have Google'd far and wide to find the solution to this, but it hasn't gotten me anywhere. I keep coming across old posts that no-longer work, or incomplete posts that don't really help me.

    Because I am relatively new to PHP I am finding it difficult to use the documentation, and any posts I do find are confusing and fragmented and I cannot find my way around them.

    If possible would anyone be able to point me to any complete scripts to help, or possibly a tutorial or an easier to follow piece of documentation. I am looking for a script that is in its minimum, just being able to create an account with a specified user, domain, password, package and contact e-mail.

    I am not the root admin on the WHM system, however I am a re-seller and have previously used WHMCS to create accounts.

    Thanks in advanced, and sorry if this has already been resolved somewhere else. I am having trouble with this and can't think of any other way.

    Regards,

    Ryan
     
  2. ehask71

    ehask71 Active Member

    Joined:
    Jul 13, 2007
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Tampa, Florida, United States
    cPanel Access Level:
    Root Administrator
    Your not using WHMCS any more? I love that billing solution anyway I will see if I can whip up something quick for you but if your not a programmer and your getting into hosting you should really bite the bullet even on the leased license ($15/month) for WHMCS

    Even WHMAutopilot is like $80 to own it.
     
  3. ehask71

    ehask71 Active Member

    Joined:
    Jul 13, 2007
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Tampa, Florida, United States
    cPanel Access Level:
    Root Administrator
    Ok I am pulling some functions out of my API server for our wordpress optimized hosting platform. We only use this type of api call for editing DNS entries for our caching servers (we use WHMCS ;-) ) anyway

    Function I just wrote for example make sure you edit the query string params to match:

    Code:
    function createAcct($args){
           $query = '/json-api/createacct?username=user123&plan=level3&ip=n&cpmod=x3&password=h@rd2gu3ss!p@ss&contact%20email=username@example.com&domain=example.com&useregns=0&reseller=0';
    
           
          $result =  CpanelRequest($query);
          // Check result if failed
    }
    Function to call your WHM and run the query returns false or an array(). This was modified from class I wrote you will need to edit the username and password below in the $header[0] line.
    Code:
    private function CpanelRequest($query,$ip=false) {
              if($ip){
                $query = 'https://'.$ip.':2087'.$query;
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $header[0] = "Authorization: Basic " . base64_encode($this->server['user']. ":" . $this->server['passwd']) . "\n\r";
                curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
                curl_setopt($curl, CURLOPT_URL, $query);
                $result = curl_exec($curl);
                if ($result == false) {
                    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
                }
                curl_close($curl);
                $result = json_decode($result,1);
                
                return $result;
            } else {
                return false;
            }
        }
    That will create an acct and return the array more on it
    Create an Account — createacct API

    Hope its a start for you
     
    #3 ehask71, Nov 29, 2012
    Last edited: Nov 29, 2012
  4. ryanbnm666

    ryanbnm666 Member

    Joined:
    Nov 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Reseller Owner
    Hey,

    Thanks for the quick reply. I have just tried running it and it doesn't seem to be working for me.
    When I load the script I get chrome saying 'Server error' and 'The website encountered an error while retrieving http://mywebsite.com/your_script.php. It may be down for maintenance or configured incorrectly.'
    I have been trying to find what is going wrong but I just can't seem to find it.

    Thanks for the help,

    Regards,
    Ryan
     
  5. ehask71

    ehask71 Active Member

    Joined:
    Jul 13, 2007
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Tampa, Florida, United States
    cPanel Access Level:
    Root Administrator
    Can you post your script here or send me a private message so I can see how you are calling it

    Thx
     
  6. ryanbnm666

    ryanbnm666 Member

    Joined:
    Nov 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Reseller Owner
    Code:
    <?php 
    private function CpanelRequest($query,$ip=false) {
    	if($ip){
    		$query = 'http://'.$ip.':2087'.$query;
    		$curl = curl_init();
    		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    		curl_setopt($curl, CURLOPT_HEADER, 0);
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    		$header[0] = "Authorization: Basic " . base64_encode($this->server['MYUSERNAME']. ":" . $this->server['MYPASSWORD']) . "\n\r";
    		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    		curl_setopt($curl, CURLOPT_URL, $query);
    		$result = curl_exec($curl);
    		if ($result == false) {
    			error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
    		}
    		curl_close($curl);
    		$result = json_decode($result,1);
    		return $result;
    	} else {
    		return false;
    	}
    }
    $qry = '/json-api/createacct?username=user123&plan=level3&ip=n&cpmod=x3&password=h@rd2gu3ss!p@ss&contact%20email=username@example.com&domain=example.com&useregns=0&reseller=0';
    $result =  CpanelRequest($qry);
    ?>
    
    I am just trying to call it like this. I took the code out of the first function because I don't need it just for testing, and I was just changing things to see if I could get it working. I did also change the username and password.

    Sorry if I am doing something incorrectly, I'm still learning.

    Thanks,

    Regards,

    Ryan
     
  7. ehask71

    ehask71 Active Member

    Joined:
    Jul 13, 2007
    Messages:
    35
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Tampa, Florida, United States
    cPanel Access Level:
    Root Administrator
    Sorry copy and paste error left the private declaration on the function from the class. Also you didnt send an IP to the request function or change the Username & Password to authenticate your request. I refactored the code change the fields I marked. Again Im not trying dissuade you from learning php but if you dont have a good understanding of PHP and how to protect inputs etc. If a script kiddie figures out where you script lives they will be able to add accounts. I have been a php programmer fulltime for the last 12 years and still dealing with building better mouse traps

    I havent tested but it should work for you. Also make sure you change the query params in $qry

    Code:
    <?php 
    // Change this!!!
    $server_ip = '192.168.1.1';
    
    function CpanelRequest($query,$ip=false) {
        /**
         *   Change These!!!!
         */
        $whm_user = 'reselleruser';
        $whm_passwd = 'resellerpass';
        
    	if($ip){
    		$query = 'http://'.$ip.':2087'.$query;
    		$curl = curl_init();
    		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    		curl_setopt($curl, CURLOPT_HEADER, 0);
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    		$header[0] = "Authorization: Basic " . base64_encode($whm_user. ":" . $whm_passwd) . "\n\r";
    		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    		curl_setopt($curl, CURLOPT_URL, $query);
    		$result = curl_exec($curl);
    		if ($result == false) {
    			error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
    		}
    		curl_close($curl);
    		$result = json_decode($result,1);
    		return $result;
    	} else {
    		return false;
    	}
    }
    $qry = '/json-api/createacct?username=user123&plan=level3&ip=n&cpmod=x3&password=h@rd2gu3ss!p@ss&contact%20email=username@example.com&domain=example.com&useregns=0&reseller=0';
    $result =  CpanelRequest($qry,$server_ip);
    
    if($result){
        echo "<pre>";
        print_r($result);
    }
    ?>
    
     
  8. ryanbnm666

    ryanbnm666 Member

    Joined:
    Nov 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Reseller Owner
    Thanks,

    But it still doesn't seem to work.

    I understand about protecting the inputs, and I have dealt with that before when a script kiddie managed to gain access to a database that was the foundation for my website. Thankfully every part of sensitive information was hashed or encrypted so it wasn't too much of an issue.

    Once I can get this working I will re-locate the variables etc so it is all secure, and obviously make sure it isn't possible for someone to just locate the script and start creating accounts. At the moment I just want to get it so when I do load the script it works.

    I have found something out, that may be the source of the problem. The location (IP) that I use to log in to WHM and where this script would sent information to does use the 'HTTPS' protocol, however, on Chrome, I get the warning that the certificate for the site is not trusted. Would this affect how the script works? And if so would it be possible to not use HTTPS?
    I did manage to get an account created when I went to the WHM panel, logged in, and pasted what is inside the 'qry' variable in my URL bar and it did create the account. I am not sure if that is because of a problem with the scripts (Or how I am using it) or if it is the server, in which case I would have to contact my host about it.

    Thanks for you help,
    I find the best way for me to learn these things is to view scripts, find out how they work and be able to re-create them.

    Regards,
    Ryan
     
  9. kbuser

    kbuser Well-Known Member

    Joined:
    Aug 25, 2008
    Messages:
    66
    Likes Received:
    1
    Trophy Points:
    8
    If you're literally just going to copy and paste people's code and expect it to work, you're going to waste everyone's time and piss people off.

    Read this:
    Create an Account — createacct

    Basically what you're trying to do is craft a URL and call it with PHP. In order to do that you need to first set a few variables specific to you. Aside from the PHP errors, the function you're using tries calling http on port 2087. WHM on 2087 uses SSL, so it would need to be https, or use port 2086.
    Try this code, and make sure to set the variables.
    If you can't get this working, echo the $url, and copy and paste it into a browser window. You should be logged in with your reseller information first.
     
  10. kbuser

    kbuser Well-Known Member

    Joined:
    Aug 25, 2008
    Messages:
    66
    Likes Received:
    1
    Trophy Points:
    8
    Because you didn't change the code at all.

    Oh, I just hate them, don't you?! :p

    That's what the verifypeer 0 line is for, it should negate this as an issue. If it were you would an error like this:
    Mind you that's not the "right" way to fix it, but it will work.
    This blog describes the proper fix, but don't worry about that (at least not yet).
    Using cURL in PHP to access HTTPS (SSL/TLS) protected sites » unitstep.net
     
  11. ryanbnm666

    ryanbnm666 Member

    Joined:
    Nov 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Reseller Owner
    Thanks for the help, it now works.
     
  12. beetwist

    beetwist Registered

    Joined:
    Dec 7, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    i think this is most useful reply for you. Good start ;)
    Visit my site: Beetwist - Product of Art & Entertainment: Makebeat program, Converse film,... to have best product of Art & Entertainment :D
     
Loading...

Share This Page