1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

create custom UI page

Discussion in 'cPanel Developers' started by B12Org, Aug 19, 2010.

  1. B12Org

    B12Org Member

    Joined:
    Jul 15, 2003
    Messages:
    694
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    Hello,

    I want to create a custom page that is in the cpanel customer root screen that when clicked will allow me to call an external web service and based on that external call, display a form to the user to fill out and post, which then posts back to the original web service and then displays the results.

    I have the php code that will connect to the remote web service but I dont know how to get the cpanel variables to send to the web service:

    For example I need to know
    - which user they are logged in as
    - which domains that user has in their account

    I need to know those items before I can make the initial web service call as those items are used to retrieve the initial data set.

    TIA
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    570
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    Hi TIA,

    You're probably looking for Expression Variables (used in cpanel tags), ExpVarRef < DeveloperResources < TWiki. cPanel tags get parsed before the HTML, so it will be rendered as plain text. You can see their use scatter throughout the cPanel html pages (/usr/local/cpanel/base/frontend/x3 ; x3 is the default theme, yours may differ). You could use that when generating your links. I'm thinking specifically the $user variable and the $CPDATA{'DNS'} variable for the account username and the primary domain respectively.

    For more information about front-end development, head over to the Developer Resources page, Developer Resources

    If you require a list of all domain information(like subdomains, parks and addons), then you can include that information within your link or you could have your remote web app get the info from the cPanel system (provide you have root or proper reseller privileges).

    A) If you want to get the info and include in link:
    - you'll have to perform three different API2 calls to get the various types of domains possible within cPanel:
    ApiAddonDomain < ApiDocs/Api2 < TWiki
    ApiPark < ApiDocs/Api2 < TWiki
    ApiSubDomain < ApiDocs/Api2 < TWiki

    B) If you want to remotely fetch the data for the particular user, use the xml-api:
    -You'll make the same API2 calls above, but using the xml-api HTTP query mechanism, CallingAPIFunctions < AllDocumentation/AutomationIntegration < TWiki . This type of polling is very common; we have a PHP client class that simplifies the authentication and formatting the query string. It's use is heavy documented in the forums, but feel free to ask specific questions.

    ALTERNATIVELY:
    You could use CpanelPHP for you cPanel custom page, which has some basic documentation here, CpanelPhp < AllDocumentation/AutomationIntegration < TWiki . One thing the document is not clear on is you must have your files in a theme directory (or any descending directory within a theme). Probably the best way to explain how CpanelPHP is by example. You can look at /usr/local/cpanel/base/frontend/x3/test.cpphp and then example what it renders by logging into a cPanel account and navigating to $yourserver:2083/frontend/x3/test.cpphp . And like the document says, you can have just the .php extension and the behavior is the same;)

    Regards,
    -DavidN
     
  3. B12Org

    B12Org Member

    Joined:
    Jul 15, 2003
    Messages:
    694
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    Hi,

    So if I wanted to get the logged in username and pass that to a php function I would do:

    <?php

    $username = "<cpanel print="$user"> ";

    ?>

    is that right?


    How would I access package variables - such as how many pop accounts or email forwarding accounts they can have?
     
  4. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    570
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    TIA,

    Yes, you're correct, you can do that. Remember, for that info to be parse prior to PHP parsing, your file will need to live in some place like /usr/local/cpanel/base/frontend/x3/youfilehere.php

    Only theme directories and their descendant directories get parsed like this. I've seen some suggestions to make symbolically link in the theme directory to those files (where ever they exist on the file system). If you do that it will still parse correctly, BUT if that directory happens to be served path in its self, say the /usr/local/cpanel/base/3rdparty/, someone could manually hit the CpanelPHP style page and it will either not parse correctly or possibly render the file's code as text. So, keep that caveat in mind.

    As for obtaining stats info on pop and forwarders, you can use the API2 StatsBar::stat, documented here.

    If you plan to get that info on the cPanel/CpanelPHP page, the you should use a <?cp ?> tag, documented here, or a <?cptt ?> tag, documented here.

    Below are the results, but I just used the xml-api to source the data; The data is the same, if you use the cp or cptt your data will be in a hash instead of XML (you will parse the hash with template directives as outlined in the documentation).
    My example account has unlimited pop and forwarders and it currently has 2 pop and 1 forwarder in use.
    Code:
    URL: https://10.1.1.1:2083/xml-api/cpanel
    DATA: display=emailaccounts%7Cemailforwarders&cpanel_xmlapi_user=dave&cpanel_xmlapi_module=StatsBar&cpanel_xmlapi_func=stat&cpanel_xmlapi_apiversion=2
    
    RESPONSE:
       <cpanelresult>
        <apiversion>2</apiversion>
        <data>
          <name>emailaccounts</name>
          <_count>2</_count>
          <_max>unlimited</_max>
          <_maxed>1</_maxed>
          <count>2</count>
          <feature>popaccts</feature>
          <id>emailaccounts</id>
          <item>Email Accounts</item>
          <langkey>INDXEmailAccounts</langkey>
          <max>unlimited </max>
          <module>Email</module>
          <percent>0</percent>
          <percent10>0</percent10>
          <percent20>0</percent20>
          <percent5>0</percent5>
          <zeroisunlimited>0</zeroisunlimited>
        </data>
        <data>
          <name>emailforwarders</name>
          <_count>1</_count>
          <_max>unlimited</_max>
          <_maxed>1</_maxed>
          <count>1</count>
          <id>emailforwarders</id>
          <item>Email Forwarders</item>
          <langkey>INDXEmailFowarders</langkey>
          <max>unlimited </max>
          <module>Email</module>
          <percent>0</percent>
          <percent10>0</percent10>
          <percent20>0</percent20>
          <percent5>0</percent5>
          <zeroisunlimited>1</zeroisunlimited>
        </data>
        <event>
          <result>1</result>
        </event>
        <func>stat</func>
        <module>StatsBar</module>
        <postevent>
          <result>1</result>
        </postevent>
        <preevent>
          <result>1</result>
        </preevent>
      </cpanelresult>
    
    Regards,
    -DavidN
     
  5. B12Org

    B12Org Member

    Joined:
    Jul 15, 2003
    Messages:
    694
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    David,

    What did you run in order to get that Xml data in your page?

    I did this basic test

    PHP:
    <cpanel setvar="dprefix=../">
    <cpanel Branding="include(stdheader.html)">

        <?php

            $username 
    "<cpanel print=\"$user\">"
            echo 
    "user = " $username "<br /><br /><br />";

            if(isset(
    $_POST["action"]) && $_POST["action"] == "go")
            {
                
    $html $_POST["html"];
                echo 
    "You posted!<br />" $html
            
    }
        
    ?>

        <FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="hidden" name="action" value="go" />
            <textarea name="html"></textarea><br />
            <input type="submit" name="Submit" value="Submit" />
            <br />
         </FORM>

    <cpanel Branding="include(stdfooter.html)">

    and put it in
    /usr/local/cpanel/base/frontend/x3/test2.php

    then going to
    http://10.10.10.10:2082/cpsess7987838652/frontend/x3/test2.php

    just shows a blank page.
    How do I get it to actually write the data?

    Additionally if I only enter a single echo line

    PHP:
    <?php
    echo "AAA";
    ?>
    it still displays an empty page, so I think something isnt quite right.
     
    #5 B12Org, Aug 20, 2010
    Last edited: Aug 20, 2010
  6. mbd5882

    mbd5882 Member

    Joined:
    Apr 30, 2005
    Messages:
    60
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Manchester, United Kingdom
    I don't know anything about cpanel UI and stuff, but I know a lot about PHP. Could the blank page be cpanels way of saying there is a PHP error? You missed out a ; in your code.
    Code:
    <cpanel setvar="dprefix=../">
    <cpanel Branding="include(stdheader.html)">
    
    	<?php
    
    		$username = "<cpanel print=\"$user\">"; 
    		echo "user = " . $username . "<br /><br /><br />";
    
    		if(isset($_POST["action"]) && $_POST["action"] == "go")
    		{
    			$html = $_POST["html"];
    			echo "You posted!<br />" . $html[B][SIZE="7"][COLOR="Red"];[/COLOR][/SIZE][/B]
    		}
    	?>
    
    	<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    		<input type="hidden" name="action" value="go" />
    		<textarea name="html"></textarea><br />
    		<input type="submit" name="Submit" value="Submit" />
    		<br />
    	 </FORM>
    
    <cpanel Branding="include(stdfooter.html)">
    
    
     
  7. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    570
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    Hi TIA,

    That's curious that a simple, lone, echo statement in a PHP file doesn't render for you. Do you get any messages in /usr/local/cpanel/logs/error_log ?

    And mbd5882 is right. You're missing a semi-colon line terminator. Also, you don't need to escape you double quotes that demarcate the value being passed into the cpanel functions (in the cpanel tag). The reason why: the page will be parsed for the cpanel tag, the tag will be evaluated all on it's own and the quotes that encompass the cpanel tag are not relevant at that point. Once the tag is evaluated the result literally replaces the whole cpanel tag. After that, Apache will parse the page (with the PHP engine).

    Here's the refined example that rendered properly for me:
    Code:
    <cpanel setvar="dprefix=../">
    <cpanel Branding="include(stdheader.html)">
    
        <?php
    
            $username = "<cpanel print="$user">";
            echo "user = " . $username . "<br /><br /><br />";
    
            if(isset($_POST["action"]) && $_POST["action"] == "go")
            {
                $html = $_POST["html"];
                echo "You posted!<br />" . $html;
            }
      
      ?>
    
        <FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="hidden" name="action" value="go" />
            <textarea name="html"></textarea><br />
            <input type="submit" name="Submit" value="Submit" />
            <br />
         </FORM>
    
    <cpanel Branding="include(stdfooter.html)">
    

    Regards,
    -DavidN
     
  8. B12Org

    B12Org Member

    Joined:
    Jul 15, 2003
    Messages:
    694
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    /usr/local/cpanel/base/frontend/x3/test2.php
    now contains

    PHP:

    <cpanel setvar="dprefix=../">
    <cpanel Branding="include(stdheader.html)">

        <?php

            $username 
    "<cpanel print="$user">";
            echo 
    "user = " $username "<br /><br /><br />";

            if(isset(
    $_POST["action"]) && $_POST["action"] == "go")
            {
                
    $html $_POST["html"];
                echo 
    "You posted!<br />" $html;
            }
      
      
    ?>

        <FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="hidden" name="action" value="go" />
            <textarea name="html"></textarea><br />
            <input type="submit" name="Submit" value="Submit" />
            <br />
         </FORM>

    <cpanel Branding="include(stdfooter.html)">


    I also copied that file into
    /usr/local/cpanel/base/frontend/x3/test2.html

    The php page renders as blank page from the URL
    https://10.10.10.10:2083/cpsess7847484008/frontend/x3/test2.php

    The html version does render correctly, however when you click submit it fails badly
    https://10.10.10.10:2083/cpsess7847484008/frontend/x3/test2.html
    when posted redirects to
    https://10.10.10.10:2083/cpsess7847484008/frontend/x3/<?php echo $_SERVER['PHP_SELF']; ?>

    I do notice that the rendered page looks like this
    PHP:

    <!-- END stdheader.html -->

        <?php

            $username 
    "muuser1";
            echo 
    "user = " $username "<br /><br /><br />";

            if(isset(
    $_POST["action"]) && $_POST["action"] == "go")
            {
                
    $html $_POST["html"];
                echo 
    "You posted!<br />" $html;
            }
      
      
    ?>

        <FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <input type="hidden" name="action" value="go" />
            <textarea name="html"></textarea><br />
            <input type="submit" name="Submit" value="Submit" />
            <br />
         </FORM>

    <!-- START stdfooter.html -->


    So it doesnt seem that the php tags are pre-processed but are just directly rendered as text - both in .php or in .html version of the page.

    Am i missing something simple here??
     
  9. B12Org

    B12Org Member

    Joined:
    Jul 15, 2003
    Messages:
    694
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    Fixed this, apparently php itself wasnt working becuse it was missing the mysqlclient15 libs - installing the RPM package mysqlclient15 solved the problem.
     
  10. Kevinfrom

    Kevinfrom Member

    Joined:
    Jan 18, 2008
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    6
    Great, glad you got it working.
     

Share This Page