Hooks

Hooks allow you to execute custom code when certain events occur in WHMCS. You will need to add S3-related action links to the admin page in WHMCS.

Change to the directory whmcs/includes/hooks and create a file S3_adminAreaClientSummaryActionLinks.php with the following contents:

<?php

// Modify other actions admin page.
function S3_adminAreaClientSummaryActionLinks($vars) {

    // Create additional links.
    $result[] = '<b>S3 - User Management</b>';
    $result[] = '<a href="staas_scripts/S3_createUser.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Create User</a>';
    $result[] = '<a href="staas_scripts/S3_deleteUser.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Delete User</a>';
    $result[] = '<a href="staas_scripts/S3_enableUser.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Enable User</a>';
    $result[] = '<a href="staas_scripts/S3_disableUser.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Disable User</a>';
    $result[] = '<a href="staas_scripts/S3_generateAccessKey.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Generate Access Key</a>';
    $result[] = '<a href="staas_scripts/S3_revokeAccessKey.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Revoke Access Key</a>';
    $result[] = '<a href="staas_scripts/S3_queryUser.php?userid=' .
        $vars['userid'] . '"><img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> Query User (on/off)</a>';
    $result[] = '<a href="staas_scripts/S3_listUsers.php">
        <img src="http://logo.acronis.com/ogimage.png"
            width="16" height="16" border="0" align="absmiddle" /> List Users (on/off)</a>';
    $result[] = '&nbsp;';
    $result[] = '<b>S3 - User Limits Management</b>';
    $result[] = '
        <form>
            <input name="userid" type="hidden" value="' . $vars['userid'] . '">
            <input name="ops-value" size="4">
            <select name="ops-name">
                <option>default</option>
                <option>get</option>
                <option>put</option>
                <option>list</option>
                <option>delete</option>
            </select> ops/s
            <br />
            <input name="bandwidth-value" size="4">
            <select name="bandwidth-name">
                <option>out</option>
            </select> bandwidth/s
            <br />
            <button type="submit"
                formaction="staas_scripts/S3_setLimitsForUser.php">Set</button>
            <button type="submit"
                formaction="staas_scripts/S3_getLimitsForUser.php">Get</button>
            <button type="submit"
                formaction="staas_scripts/S3_deleteLimitsForUser.php">Delete</button>
        </form>
    ';
    $result[] = '&nbsp;';
    $result[] = '<b>S3 - Bucket Limits Management</b>';
    $result[] = '
        <form>
            <input name="userid" type="hidden" value="' . $vars['userid'] . '">
            <input name="ops-value" size="4">
            <select name="ops-name">
                <option>default</option>
                <option>get</option>
                <option>put</option>
                <option>list</option>
                <option>delete</option>
            </select> ops/s
            <br />
            <input name="bandwidth-value" size="4">
            <select name="bandwidth-name">
                <option>out</option>
            </select> bandwidth/s
            <br />
            <input name="bucket" size="4"> bucket name
            <br />
            <button type="submit"
                formaction="staas_scripts/S3_setLimitsForBucket.php">Set</button>
            <button type="submit"
                formaction="staas_scripts/S3_getLimitsForBucket.php">Get</button>
            <button type="submit"
                formaction="staas_scripts/S3_deleteLimitsForBucket.php">Delete</button>
        </form>
    ';
    $result[] = '&nbsp;';
    $result[] = '<b>S3 - Usage Statistics</b>';
    $result[] = '
        <a href="staas_scripts/S3_listStatsObjects.php">
            <img src="http://logo.acronis.com/ogimage.png"
                width="16" height="16" border="0" align="absmiddle" />
                    List Statistics Objects (on/off)
        </a>
        <p>
            <form>
                <input name="object" size="15"> object name
                <br />
                <button type="submit"
                    formaction="staas_scripts/S3_getStatsForObject.php">Get</button>
                <button type="submit"
                    formaction="staas_scripts/S3_deleteStatsForObject.php">Delete</button>
            </form>
        </p>
    ';
    $result[] = '&nbsp;';

    // Return links.
    return $result;
}

// Modify admin area.
add_hook('AdminAreaClientSummaryActionLinks', 1, "S3_adminAreaClientSummaryActionLinks");
?>

The last file extends the admin summary page and displays S3 user information as well as user and bucket limits if the corresponding links are clicked. Create a file S3_adminAreaClientSummaryPage.php with the following contents:

<?php

// Modify admin client summary to show S3 information.
function S3_adminAreaClientSummaryPage($vars) {

    // Sane default.
    $result = '
    <div class="row client-summary-panels">
    ';

    // Show users.
    if ($_SESSION['s3_list_users'] == 1) {

        // Table header.
        $result = $result . '
        <div class="col-lg-6 col-sm-12">
            <div class="clientssummarybox">
                <div class="title">
                    S3 Users List
                </div>
                <table class="clientssummarystats" cellspacing="0" cellpadding="2">
                    <tr>
                        <td><b>UserId</b></td>
                        <td><b>UserEmail</b></td>
                    </tr>
        ';

        // One row per access key pair.
        foreach ($_SESSION['s3_list'] as $s3_row) {
            $result = $result . '
                    <tr class="altrow">
                        <td>' . $s3_row['UserId'] . '</td>
                        <td>' . $s3_row['UserEmail'] . '</td>
                    </tr>
            ';
        }

        // Table footer.
        $result = $result . '
                </table>
            </div>
        </div>
        ';
    }

    // Show user.
    if ($_SESSION['s3_query_user'] == 1) {

        // Table header.
        $result = $result . '
        <div class="col-lg-6 col-sm-12">
            <div class="clientssummarybox">
                <div class="title">
                    S3 Information for User: ' . $_SESSION['s3_userid'] . '
                </div>
                <table class="clientssummarystats" cellspacing="0" cellpadding="2">
                    <tr>
                        <td><b>AWSAccessKeyId</b></td>
                        <td><b>AWSSecretAccessKey</b></td>
                    </tr>
        ';

        // One row per access key pair.
        foreach ($_SESSION['s3_aws_access_keys'] as $s3_row) {
            $result = $result . '
                    <tr class="altrow">
                        <td>' . $s3_row['AWSAccessKeyId'] . '</td>
                        <td>' . $s3_row['AWSSecretAccessKey'] . '</td>
                    </tr>
            ';
        }

        // Table footer.
        $result = $result . '
                </table>
            </div>
        </div>
        ';
    }

    // Table footer and next header.
    $result = $result . '
    </div>
    <div class="row client-summary-panels">
    ';

    // Show statistics list.
    if ($_SESSION['s3_stat_objects'] == 1) {

        // Table header.
        $result = $result . '
        <div class="col-lg-6 col-sm-12">
            <div class="clientssummarybox">
                <div class="title">
                    S3 Statistics List
                </div>
                <table class="clientssummarystats" cellspacing="0" cellpadding="2">
                    <tr>
                        <td><b>Object Name</b></td>
                    </tr>
        ';

        // One row per access key pair.
        foreach ($_SESSION['s3_stat']['items'] as $s3_object) {
            $result = $result . '
                    <tr class="altrow">
                        <td>' . $s3_object . '</td>
                    </tr>
            ';
        }

        // Table footer.
        $result = $result . '
                </table>
            </div>
        </div>
        ';
    }

    // Show limits for user.
    if (!empty($_SESSION['s3_limits_user'])) {

        // Table header.
        $result = $result . '
        <div class="col-lg-3 col-sm-6">
            <div class="clientssummarybox">
                <div class="title">
                    S3 Limits for User
                </div>
                <table class="clientssummarystats" cellspacing="0" cellpadding="2">
                    <tr>
                        <td><b>Type</b></td>
                        <td><b>Name</b></td>
                        <td><b>Value</b></td>
                    </tr>
        ';

        // One row per access key pair.
        foreach ($_SESSION['s3_limits_user'] as $s3_limits => $s3_value) {
            list($s3_type, $s3_limit) = explode(":", $s3_limits);
            $result = $result . '
                    <tr class="altrow">
                        <td>' . $s3_type . '</td>
                        <td>' . $s3_limit . '</td>
                        <td>' . $s3_value . '</td>
                    </tr>
            ';
        }

        // Table footer.
        $result = $result . '
                </table>
            </div>
        </div>
        ';
    }

    // Show limits for bucket.
    if (!empty($_SESSION['s3_limits_bucket'])) {

        // Table header.
        $result = $result . '
        <div class="col-lg-3 col-sm-6">
            <div class="clientssummarybox">
                <div class="title">
                    S3 Limits for Bucket: ' . $_SESSION['s3_bucket'] . '
                </div>
                <table class="clientssummarystats" cellspacing="0" cellpadding="2">
                    <tr>
                        <td><b>Type</b></td>
                        <td><b>Name</b></td>
                        <td><b>Value</b></td>
                    </tr>
        ';

        // One row per access key pair.
        foreach ($_SESSION['s3_limits_bucket'] as $s3_limits => $s3_value) {
            list($s3_type, $s3_limit) = explode(":", $s3_limits);
            $result = $result . '
                    <tr class="altrow">
                        <td>' . $s3_type . '</td>
                        <td>' . $s3_limit . '</td>
                        <td>' . $s3_value . '</td>
                    </tr>
            ';
        }

        // Table footer.
        $result = $result . '
                </table>
            </div>
        </div>
        ';
    }

    // Table footer and next header.
    $result = $result . '
    </div>
    <div class="row client-summary-panels">
    ';

    // Show statistics for object.
    if (!empty($_SESSION['s3_object_statistic'])) {

        // Table header.
        $result = $result . '
        <div class="col-lg-12 col-sm-24">
            <div class="clientssummarybox">
                <div class="title">
                    S3 Statistics for Object: ' . $_SESSION['s3_object'] . '
                </div>
                <table class="clientssummarystats" cellspacing="0" cellpadding="2">
                    <tr>
                        <td><b>fmt_version</b></td>
                        <td><b>service_id</b></td>
                        <td><b>start_ts</b></td>
                        <td><b>period</b></td>
                        <td><b>bucket</b></td>
                        <td><b>epoch</b></td>
                        <td><b>user_id</b></td>
                        <td><b>tag</b></td>
                        <td><b>put</b></td>
                        <td><b>get</b></td>
                        <td><b>list</b></td>
                        <td><b>other</b></td>
                        <td><b>uploaded</b></td>
                        <td><b>downloaded</b></td>
                    </tr>
        ';

        // One row per access key pair.
        foreach ($_SESSION['s3_object_statistic']['items'] as $s3_object) {
            $result = $result . '
                    <tr class="altrow">
                        <td>' . $_SESSION['s3_object_statistic']['fmt_version']. '</td>
                        <td>' . $_SESSION['s3_object_statistic']['service_id']. '</td>
                        <td>' . $_SESSION['s3_object_statistic']['start_ts']. '</td>
                        <td>' . $_SESSION['s3_object_statistic']['period']. '</td>
                        <td>' . $s3_object['key']['bucket'] . '</td>
                        <td>' . $s3_object['key']['epoch'] . '</td>
                        <td>' . $s3_object['key']['user'] . '</td>
                        <td>' . $s3_object['key']['tag'] . '</td>
                        <td>' . $s3_object['counters']['ops']['put'] . '</td>
                        <td>' . $s3_object['counters']['ops']['get'] . '</td>
                        <td>' . $s3_object['counters']['ops']['list'] . '</td>
                        <td>' . $s3_object['counters']['ops']['other'] . '</td>
                        <td>' . $s3_object['counters']['net_io']['uploaded'] . '</td>
                        <td>' . $s3_object['counters']['net_io']['downloaded'] . '</td>
                    </tr>
            ';
        }

        // Table footer.
        $result = $result . '
                </table>
            </div>
        </div>
        ';
    }

    // Table footer.
    $result = $result . '
    </div>
    ';

    // Return table.
    return $result;
}

// Modify admin area.
add_hook('AdminAreaClientSummaryPage', 1, "S3_adminAreaClientSummaryPage");
?>