2

I want to login at cPanel through Php script and need to modified some file from the file manager.

I have cPanel username and password also but file modification I want through PHP script no by graphically.

I will use file_put_content for modified the file from the file manager.

See below code:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
amrut ratnani
  • 21
  • 1
  • 2
  • noting cpanel does that you cant just do in php. But it also has an API –  Jun 24 '17 at 07:00
  • No need to access cpanel. set a cron job – Saad Suri Jun 24 '17 at 07:42
  • You can just connect to the server using `ftp_` commands in PHP. – halfer Jun 24 '17 at 08:00
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jun 24 '17 at 08:01

2 Answers2

1

Yes, there is a way, CPanel has an API that can be used by PHP. Example from the docs:

// Instantiate the CPANEL object.
require_once "/usr/local/cpanel/php/cpanel.php";

// Connect to cPanel - only do this once.
$cpanel = new CPANEL();

// Get domain user data.
$get_userdata = $cpanel->uapi(
    'DomainInfo', 'domains_data',
    array(
        'format'    => 'hash',
    )
);

// Perform the desired actions.
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Where is the cpanel.php class ? i see your given doc link but there is also i can't fine that class. – amrut ratnani Jun 26 '17 at 05:25
  • One more thing, i can get only domain info by using this actually i want to modify the server files of website by after login to cpanel through php. Thanks, – amrut ratnani Jun 26 '17 at 05:27
  • A probable location for the file is /usr/local/cpanel/php/cpanel.php. Please, read the documentation, there would be no benefit of pasting all of its relevant content here. And you can do file operations, again, using the API, which, again, is described in the documentation: https://documentation.cpanel.net/display/SDK/cPanel+API+2+Functions+-+Fileman%3A%3Afileop – Lajos Arpad Jun 26 '17 at 09:37
0

Elements required to make this functionality are –

    Server/WHM Username
    Cpanel account Username
    Server login URL
    Server accesshash key

And for Accesshash key , New or already generated Access key can get from here:- WHM > Remote Access Key area and the Access Key located there. or it should be at this path “/usr/local/cpanel/bin/realmkaccesshash

Once you get all these details , you can follow the code steps as:-

            $query = "https://$server_login_link:2087/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

            $header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$hash);
            curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
            curl_setopt($curl, CURLOPT_URL, $query);
            $result = curl_exec($curl);

            if ($result == false) {
                // your error log
            }
            if($result){
                $decoded_response = json_decode( $result, true );
                if(isset($decoded_response['data']) && !empty($decoded_response['data'])){
                    $url = $decoded_response['data']['url'];
                    return $url;        
                }

            }        

once you get this URL, you can directly open this in a new tab or same tab and you must get logged in. It generates the similar session as cpanel login and provides you all that specific cpanel privileges.

Remeber that it only logs you in with given specific account, not for all cpanel accounts access within a server.

jagjeet
  • 376
  • 4
  • 12