Skip to content

Instantly share code, notes, and snippets.

@dsantos3526
Created November 19, 2021 10:59
Show Gist options
  • Select an option

  • Save dsantos3526/eb4415d7f16c17fd945f7ee29465bb37 to your computer and use it in GitHub Desktop.

Select an option

Save dsantos3526/eb4415d7f16c17fd945f7ee29465bb37 to your computer and use it in GitHub Desktop.

Revisions

  1. @dsthedev dsthedev created this gist Sep 25, 2019.
    91 changes: 91 additions & 0 deletions create-subdomain.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    <?php

    /**
    * Create Subdomains
    *
    * Note: This script was designed to be called silently by a bash script for project creation.
    * Add to bash script: php "$HOME/scripts/php/create-subdomains.php" "$new_account_subdomain"
    *
    * Requires "jamesryanbell/cloudflare": "^1.11" for the CloudFlare PHP library
    * Recommends "kint-php/kint": "^3.2" for improved temporary PHP debugging
    *
    * @author Darren Sopiarz <[email protected]>
    * @todo pass in bash variables from main script and verify that everything all works together
    */

    // Load Composer
    require_once dirname(__FILE__) . '/../../vendor/autoload.php';

    // This grabs the first argument being passed in from a bash script that calls this script
    $new_subdomain = $argv[1];
    // $new_subdomain = 'noselfsigned';

    if ('' == $new_subdomain || empty($new_subdomain)) {
    die("No subdomain entered.\n");
    }

    /**
    * create_cloudflare_subdomain
    *
    * @todo Move the credentials into a safer folder on the server and include it above to gain secure access
    * @todo Find a way to add a check to see if it fails, and pass a message back into calling bash script
    */
    function create_cloudflare_subdomain($subdomain = '')
    {
    // Cloudflare Credentials
    $email = '[email protected]';
    $auth_key = 'getfromcloudflareaccount';
    $zone_id = 'getfromcloudflareaccount';

    // New Record Information
    $record_type = 'A';
    $servers_ip = 'XXX.XX.XX.XX';
    $ttl = null;
    $proxied = false;

    // 1. Connect to CloudFlare
    $CFAPI = new \Cloudflare\Api($email, $auth_key);

    // 2. Instantiate the DNS Class for CloudFlare.
    $CFDNS = new \Cloudflare\Zone\Dns($CFAPI);

    // 3. Create the DNS Record
    $CFDNS->create($zone_id, $record_type, $subdomain, $servers_ip, $ttl, $proxied);
    }

    create_cloudflare_subdomain($new_subdomain);

    /**
    * create_new_cpanel_subdomain
    *
    * @link https://stackoverflow.com/questions/31534861/how-to-create-subdomains-using-curl#answer-31542859
    */
    function create_new_cpanel_subdomain($subdomain = '')
    {
    $cpanel_user = 'username';
    $rootdomain = 'sub.domain.com';
    $whm_user = 'root';
    $whm_port = '2087';
    $whm_pass = 'superstrongpassword';
    $doc_root = '%2Fprojects%2F' . $subdomain . '%2Fpublic_html';
    $header[0] = "Authorization: Basic " . base64_encode($whm_user . ":" . $whm_pass) . "\n\r";
    $result = '';
    $query = 'https://' . $rootdomain . ':' . $whm_port . '/json-api/cpanel?cpanel_jsonapi_user=' . $cpanel_user
    . '&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain='
    . $subdomain . '&rootdomain=' . $rootdomain . '&dir=' . $doc_root;

    $curl_handle = curl_init(); // Create Curl Object
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 1); // Don't allow self-signed certs?
    // curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 0); // Allow certs that do not match the hostname
    curl_setopt($curl_handle, CURLOPT_HEADER, 0); // Do not include header in output
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Return contents of transfer on curl_exec
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header); // set the username and password
    curl_setopt($curl_handle, CURLOPT_URL, $query); // execute the query
    $result = curl_exec($curl_handle);
    if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl_handle) . "\" for $query");
    }
    curl_close($curl_handle);
    }

    create_new_cpanel_subdomain($new_subdomain);