Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Fileman::savefile && Fileman::uploadfiles Problems

Discussion in 'cPanel Developers' started by b81erikan, Jun 10, 2018.

  1. b81erikan

    b81erikan Registered

    Joined:
    Jun 10, 2018
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Turkey
    cPanel Access Level:
    Reseller Owner
    Firstly,I'm so sorry for my bad english. I use this class for api2 and i wanna upload file && save file but i have a some problems.

    Class:
    PHP:
    /**
     * PHP class to handle connections with cPanel's UAPI and API2 specifically through cURL requests as seamlessly and simply as possible.
     *
     * For documentation on cPanel's UAPI:
     * @see https://documentation.cpanel.net/display/SDK/UAPI+Functions
     *
     * For documentation on cPanel's API2:
     * @see https://documentation.cpanel.net/display/SDK/Guide+to+cPanel+API+2
     *
     * Please use UAPI where possible, only use API2 where the equivalent doesn't exist for UAPI
     *
     * @author Toby New - www.source-control.co.uk
     * @copyright 2016 N1ghteyes
     * @license license.txt The MIT License (MIT)
     * @link https://github.com/N1ghteyes/cpanel-UAPI-php-class
     *
     * @todo - im sure there is a lot that can be improved here
     */
    /**
     * Class cPanelAPI
     */
    class cpanelAPI
    {
        public 
    $version '2.0';
        public 
    $ssl 1//Bool - TRUE / FALSE for ssl connection
        
    public $port 2083//default for ssl servers.
        
    public $server;
        public 
    $maxredirect 0//Number of redirects to make, typically 0 is fine. on some shared setups this will need to be increased.
        
    public $user;
        public 
    $json '';
        public 
    $scope//String - Module we want to use
        
    protected $api;
        protected 
    $auth;
        protected 
    $pass;
        protected 
    $secret;
        protected 
    $type;
        protected 
    $session;
        protected 
    $method;
        protected 
    $requestUrl;
        protected 
    $eno;
        protected 
    $emes;
        protected 
    $token FALSE;
        protected 
    $httpMethod 'GET';
        protected 
    $postData '';
        
    /**
         * @param $user
         * @param $pass
         * @param $server
         * @param $secret
         */
        
    function __construct($user$pass$server$secret FALSE)
        {
            
    $this->user $user;
            
    $this->pass $pass;
            
    $this->server $server;
            if (
    $this->secret) {
                
    $this->secret $secret;
                
    $this->set2Fa();
            }
        }
        
    /**
         * Include the ot class so we can generate a valid token for 2FA
         */
        
    protected function set2Fa()
        {
            require 
    'otphp/lib/otphp.php';
            
    $totp = new \OTPHP\TOTP($this->secret);
            
    $this->token $totp->now();
        }
        public function 
    __get($name)
        {
            switch (
    strtolower($name)) {
                case 
    'get':
                    
    $this->httpMethod 'GET';
                    break;
                case 
    'post':
                    
    $this->httpMethod 'POST';
                    break;
                case 
    'api2':
                    
    $this->setApi('api2');
                    break;
                case 
    'uapi':
                    
    $this->setApi('uapi');
                    break;
                default:
                    
    $this->scope $name;
            }
            return 
    $this;
        }
        
    /**
         * Set the api to use for connections.
         * @param $api
         * @return $this
         * @throws Exception
         */
        
    protected function setApi($api)
        {
            
    $this->api $api;
            
    $this->setMethod();
            return 
    $this;
        }
        
    /**
         * Function to set the method used to communicate with the chosen api.
         * @return $this
         * @throws Exception
         */
        
    protected function setMethod()
        {
            switch (
    $this->api) {
                case 
    'uapi':
                    
    $this->method '/execute/';
                    break;
                case 
    'api2':
                    
    $this->method '/json-api/cpanel/';
                    break;
                default:
                    throw new 
    Exception('$this->api is not set or is incorrectly set. The only available options are \'uapi\' or \'api2\'');
            }
            return 
    $this;
        }
        
    /**
         * Magic __toSting() method, allows us to return the result as raw json
         * @return mixed
         */
        
    public function __toString()
        {
            return 
    $this->json;
        }
        
    /**
         * Magic __call method, will translate all function calls to object to API requests
         * @param $name - name of the function
         * @param $arguments - an array of arguments
         * @return mixed
         * @throws Exception
         */
        
    public function __call($name$arguments)
        {
            if (
    count($arguments) < || !is_array($arguments[0]))
                
    $arguments[0] = [];
            
    $this->json $this->APIcall($name$arguments[0]);
            return 
    json_decode($this->json);
        }
        
    /**
         * @param $name
         * @param $arguments
         * @return bool|mixed
         * @throws Exception
         */
        
    protected function APIcall($name$arguments)
        {
            
    $this->auth base64_encode($this->user ":" $this->pass);
            
    $this->type $this->ssl == "https://" "http://";
            
    $this->requestUrl $this->type $this->server ':' $this->port $this->method;
            switch (
    $this->api) {
                case 
    'uapi':
                    
    $this->requestUrl .= ($this->scope != '' $this->scope "/" '') . $name '?';
                    break;
                case 
    'api2':
                    if (
    $this->scope == '') {
                        throw new 
    Exception('Scope must be set.');
                    }
                    
    $this->requestUrl .= '?cpanel_jsonapi_user=' $this->user '&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=' $this->scope '&cpanel_jsonapi_func=' $name '&';
                    break;
                default:
                    throw new 
    Exception('$this->api is not set or is incorrectly set. The only available options are \'uapi\' or \'api2\'');
            }
            if(
    $this->httpMethod == 'GET') {
                
    $this->requestUrl .= http_build_query($arguments);
            }
            if(
    $this->httpMethod == 'POST'){
                
    $this->postData $arguments;
            }
            return 
    $this->curl_request($this->requestUrl);
        }
        
    /**
         * @param $url
         * @return bool|mixed
         */
        
    protected function curl_request($url)
        {
            
    $httpHeaders = array("Authorization: Basic " $this->auth);
            
    //If we have a token then send that with the request for 2FA.
            
    if ($this->token) {
                
    $httpHeaders[] = "X-CPANEL-OTP: " $this->token;
            }
            
    $ch curl_init();
            if(
    $this->httpMethod == 'POST'){
                
    $httpHeaders[] = "Content-type: multipart/form-data";
                
    curl_setopt($ch,CURLOPT_POSTFIELDS$this->postData);
            }
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
            
    curl_setopt($chCURLOPT_HEADER0);
            
    curl_setopt($chCURLOPT_URL$url);
            
    curl_setopt($chCURLOPT_HTTPHEADER$httpHeaders);
            
    curl_setopt($chCURLOPT_TIMEOUT100020);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
            
    $content $this->curl_exec_follow($ch$this->maxredirect);
            
    $this->eno curl_errno($ch);
            
    $this->emes curl_error($ch);
            
    curl_close($ch);
            return 
    $content;
        }
        
    /**
         * @param $ch
         * @param null $maxredirect
         * @return bool|mixed
         */
        
    protected function curl_exec_follow($ch, &$maxredirect null)
        {
            
    // we emulate a browser here since some websites detect
            // us as a bot and don't let us do our job
            
    $user_agent "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)" .
                
    " Gecko/20041107 Firefox/1.0";
            
    curl_setopt($chCURLOPT_USERAGENT$user_agent);
            
    $mr $maxredirect === null intval($maxredirect);
            
    curl_setopt($chCURLOPT_FOLLOWLOCATION$mr 0);
            
    curl_setopt($chCURLOPT_MAXREDIRS$mr);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
            return 
    curl_exec($ch);
        }
        
    /**
         * Function to get the last request made
         * @return mixed
         */
        
    public function getLastRequest()
        {
            return 
    $this->requestUrl;
        }
        
    /**
         * Function to return the error if there was one, or FALSE if not.
         * @return array|bool
         */
        
    public function getError()
        {
            if (!empty(
    $this->eno)) {
                return [
    'no' => $this->eno'message' => $this->emes];
            }
            return 
    FALSE;
        }
    }
    Uploadfile code:
    PHP:
    $uapi           = new cpanelAPI('CPANELUSERNAME''CPANELPASSWORD''SERVER'); //instantiate the object
    $uapi->scope    'Fileman';
    $upload_file    realpath("12312s1223213213.zip");
    $response       $uapi->api2->uploadfiles(array('dir' => '/public_html/demo','file-1'=> $upload_file ));
    RETURN:
    PHP:
    stdClass Object ( [cpanelresult] => stdClass Object ( [event] => stdClass Object ( [result] => ) [data] => Array ( [0] => stdClass Object ( [warned] => [succeeded] => [failed] => [uploads] => Array ( ) ) ) [func] => uploadfiles [apiversion] => [module] => Fileman [error] => You must specify at least one file to upload. ) )
    Savefile code:

    PHP:
    $uapi           = new cpanelAPI('CPANELUSERNAME''CPANELPASSWORD''SERVER'); //instantiate the object
    $uapi->scope    'Fileman';

    $content        "MY CONTENT';

    $response       = $uapi->api2->savefile(array('filename'=>'db.php','content'=>$content,'path'=>'/home/USERNAME/public_html/','charset'=>"utf-8" ));
    My problem for uploadfile: I don't know how to upload file

    My problem for savefile: I write path but does not change , everytime path '/home/USERNAME/'.

    How to file upload and how to change path.
     
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. b81erikan

    b81erikan Registered

    Joined:
    Jun 10, 2018
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Turkey
    cPanel Access Level:
    Reseller Owner
    I work at localhost. I wanna upload from computer to website. I looked this tutorial, i write realpath("file1.zip") for "file-1". This path is true but when i start this page i look this error "You must specify at least one file to upload. " How to choose file?

    Thanks for your interest.
     
  4. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @b81erikan,

    Steps 5 thru 9 on that document explain how to upload the file you defined in step 4. Additionally, here's an example of how you'd setup a form for someone to access and choose a file from their local computer to upload:

    php send file to remote server using cURL

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Visad182

    Visad182 Member

    Joined:
    Sep 8, 2017
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Canada
    cPanel Access Level:
    Website Owner
    Hello Michael.

    Cpanel should rethink his API.

    To much method without example.
     
  6. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,408
    Likes Received:
    1,953
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello @Visad182,

    Thank you for taking the time to provide us with feedback on the usability of this API function. I encourage you to open a feature request to request an easier method to perform this task or to request a change to the existing API functionality:

    Submit A Feature Request

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice