Code Monkey home page Code Monkey logo

wp-cloudflare-api's Introduction

WP CloudFlare API

A WordPress php library for interacting with the CloudFlare API.

Code Climate Test Coverage Issue Count

wp-cloudflare-api's People

Contributors

bhubbard avatar bradleymoore111 avatar cjhaas avatar sfgarza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

troplo

wp-cloudflare-api's Issues

Compare/Review from Sunny Plugin

/**
 * Make CloudFlare Client API calls via wp_remote_post
 * A fork from CloudFlare-API by VEXXHOST, Inc.
 *
 * @package    Sunny
 * @subpackage Sunny/includes
 * @author     Tang Rufus <[email protected]>
 * @since      1.0.0
 * @link       https://github.com/vexxhost/CloudFlare-API
 * @link       https://www.cloudflare.com/docs/client-api.html
 */

class Sunny_CloudFlare_API_Helper {

    //The URL of the API
    private static $CLOUDFLARE_API_ENDPOINT = 'https://www.cloudflare.com/api_json.html';

    //Service mode values.
    private static $MODE_SERVICE = array( 'A', 'AAAA', 'CNAME' );

    //Prio values.
    private static $PRIO = array( 'MX', 'SRV' );

    //Stores the api key
    private $token_key;

    //Stores the email login
    private $email;

    public function __construct( $email, $token_key )
    {

        $this->email = sanitize_email( $email );
        $this->token_key = sanitize_text_field( $token_key );

    }

    /**
     * CLIENT API
     * Section 3
     * Access
     */

    /**
     * 3.1 - Retrieve Domain Statistics For A Given Time Frame
     * This function retrieves the current stats and settings for a particular website.
     * It can also be used to get currently settings of values such as the security level.
     */
    public function stats( $domain, $interval = 20 )
    {
        $data = array(
            'a'        => 'stats',
            'z'        => $domain,
            'interval' => $interval
        );
        return $this->http_post( $data );
    }

    /**
     * 3.2 - Retrieve A List Of The Domains
     * This lists all domains in a CloudFlare account along with other data.
     */
    public function zone_load_multi()
    {
        $data = array(
            'a' => 'zone_load_multi'
        );
        return $this->http_post( $data );
    }

    /**
     * 3.3 - Retrieve DNS Records Of A Given Domain
     * This function retrieves the current DNS records for a particular website.
     */
    public function rec_load_all( $domain )
    {
        $data = array(
            'a' => 'rec_load_all',
            'z' => $domain
        );
        return $this->http_post( $data );
    }

    /**
     * 3.4 - Checks For Active Zones And Returns Their Corresponding Zids
     * This function retrieves domain statistics for a given time frame.
     */
    public function zone_check( $zones )
    {
        if ( is_array( $zones ) ) {
            $zones = implode( ',', $zones );
        }
        $data = array(
            'a'     => 'zone_check',
            'zones' => $zones
        );
        return $this->http_post( $data );
    }

    /**
     * 3.6 - Check The Threat Score For A Given IP
     * This function retrieves the current threat score for a given IP.
     * Note that scores are on a logarithmic scale, where a higher score indicates a higher threat.
     */
    public function threat_score( $ip )
    {
        $data = array(
            'a'  => 'ip_lkup',
            'ip' => $ip
        );
        return $this->http_post( $data );
    }

    /**
     * 3.7 - List All The Current Settings
     * This function retrieves all the current settings for a given domain.
     */
    public function zone_settings( $domain )
    {
        $data = array(
            'a' => 'zone_settings',
            'z' => $domain
        );
        return $this->http_post( $data );
    }

    /**
     * Undocumented method
     * SEE: https://github.com/vexxhost/CloudFlare-API/pull/3
     */
    public function zone_init( $zone )
    {
     $data['a']    = 'zone_init';
     $data['z']    = $zone;
     return $this->http_post( $data );
 }

    /**
     * CLIENT API
     * Section 4
     * Modify
     */

    /**
     * 4.1 - Set The Security Level
     * This function sets the Basic Security Level to I'M UNDER ATTACK! / HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
     * The switches are: (help|high|med|low|eoff).
     */
    public function sec_lvl( $domain, $mode )
    {
        $data = array(
            'a' => 'sec_lvl',
            'z' => $domain,
            'v' => $mode
        );
        return $this->http_post( $data );
    }

    /**
     * 4.2 - Set The Cache Level
     * This function sets the Caching Level to Aggressive or Basic.
     * The switches are: (agg|basic).
     */
    public function cache_lvl( $domain, $mode )
    {
        $data = array(
            'a' => 'cache_lvl',
            'z' => $domain,
            'v' => ( 'agg' == strtolower( $mode ) ) ? 'agg' : 'basic'
        );
        return $this->http_post( $data );
    }

    /**
     * 4.3 - Toggling Development Mode
     * This function allows you to toggle Development Mode on or off for a particular domain.
     * When Development Mode is on the cache is bypassed.
     * Development mode remains on for 3 hours or until when it is toggled back off.
     */
    public function devmode( $domain, $mode )
    {
        $data = array(
            'a' => 'devmode',
            'z' => $domain,
            'v' => ( true == $mode ) ? 1 : 0
        );
        return $this->http_post( $data );
    }

    /**
     * 4.4 - Clear CloudFlare's Cache
     * This function will purge CloudFlare of any cached files.
     * It may take up to 48 hours for the cache to rebuild and optimum performance to be achieved.
     * This function should be used sparingly.
     */
    public function fpurge_ts( $domain )
    {
        $data = array(
            'a' => 'fpurge_ts',
            'z' => $domain,
            'v' => 1
        );
        return $this->http_post( $data );
    }

    /**
     * 4.5 - Purge A Single File In CloudFlare's Cache
     * This function will purge a single file from CloudFlare's cache.
     */
    public function zone_file_purge( $domain, $url )
    {
        $data = array(
            'a'   => 'zone_file_purge',
            'z'   => $domain,
            'url' => $url
        );
        return $this->http_post( $data );
    }

    /**
     * 4.6 - Update The Snapshot Of Your Site
     * This snapshot is used on CloudFlare's challenge page
     * This function tells CloudFlare to take a new image of your site.
     * Note that this call is rate limited to once per zone per day.
     * Also the new image may take up to 1 hour to appear.
     */
    public function update_image( $zoneid )
    {
        $data = array(
            'a'   => 'zone_grab',
            'zid' => $zoneid
        );
        return $this->http_post( $data );
    }

    /**
     * 4.7a - Whitelist IPs
     * You can add an IP address to your whitelist.
     */
    public function wl( $ip )
    {
        $data = array(
            'a'   => 'wl',
            'key' => $ip
        );
        return $this->http_post( $data );
    }

    /**
     * 4.7b - Blacklist IPs
     * You can add an IP address to your blacklist.
     */
    public function ban( $ip )
    {
        $data = array(
            'a'   => 'ban',
            'key' => $ip
        );
        return $this->http_post( $data );
    }

    /**
     * 4.7c - Unlist IPs
     * You can remove an IP address from the whitelist and the blacklist.
     */
    public function nul( $ip )
    {
        $data = array(
            'a'   => 'nul',
            'key' => $ip
        );
        return $this->http_post( $data );
    }

    /**
     * 4.8 - Toggle IPv6 Support
     * This function toggles IPv6 support.
     */
    public function ipv46( $domain, $mode )
    {
        $data = array(
            'a' => 'ipv46',
            'z' => $domain,
            'v' => ( true == $mode ) ? 1 : 0
        );
        return $this->http_post( $data );
    }

    /**
     * 4.9 - Set Rocket Loader
     * This function changes Rocket Loader setting.
     */
    public function async( $domain, $mode )
    {
        $data = array(
            'a' => 'async',
            'z' => $domain,
            'v' => $mode
        );
        return $this->http_post( $data );
    }

    /**
     * 4.10 - Set Minification
     * This function changes minification settings.
     */
    public function minify( $domain, $mode )
    {
        $data = array(
            'a' => 'minify',
            'z' => $domain,
            'v' => $mode
        );
        return $this->http_post( $data );
    }


    /**
     * CLIENT API
     * Section 5
     * DNS Record Management
     */

    /**
     * 5.1 - Add A New DNS Record
     * This function creates a new DNS record for a zone.
     * See http://www.cloudflare.com/docs/client-api.html#s5.1 for documentation.
     */
    public function rec_new( $domain, $type, $name, $content, $ttl = 1, $mode = 1, $prio = 1, $service = 1, $srvname = 1, $protocol = 1, $weight = 1, $port = 1, $target = 1 )
    {
        $data = array(
            'a'       => 'rec_new',
            'z'       => $domain,
            'type'    => $type,
            'name'    => $name,
            'content' => $content,
            'ttl'     => $ttl
        );
        if ( in_array( $type, self::$MODE_SERVICE ) )
            $data['service_mode'] = ( true == $mode ) ? 1 : 0;
        elseif ( in_array( $type, self::$PRIO ) ) {
            $data['prio'] = $prio;
            if ( $type == 'SRV' ) {
                $data = array_merge( $data, array(
                    'service'  => $service,
                    'srvname'  => $srvname,
                    'protocol' => $protocol,
                    'weight'   => $weight,
                    'port'     => $port,
                    'target'   => $target
                ) );
            }
        }
        return $this->http_post( $data );
    }

    /**
     * 5.2 - Edit A DNS Record
     * This function edits a DNS record for a zone.
     * See http://www.cloudflare.com/docs/client-api.html#s5.1 for documentation.
     */
    public function rec_edit( $domain, $type, $id, $name, $content, $ttl = 1, $mode = 1, $prio = 1, $service = 1, $srvname = 1, $protocol = 1, $weight = 1, $port = 1, $target = 1 )
    {
        $data = array(
            'a'       => 'rec_edit',
            'z'       => $domain,
            'type'    => $type,
            'id'      => $id,
            'name'    => $name,
            'content' => $content,
            'ttl'     => $ttl
        );
        if ( in_array( $type, self::$MODE_SERVICE ) )
            $data['service_mode'] = ( true == $mode ) ? 1 : 0;
        elseif ( in_array( $type, self::$PRIO ) ) {
            $data['prio'] = $prio;
            if ( 'SRV' == $type ) {
                $data = array_merge( $data, array(
                    'service'  => $service,
                    'srvname'  => $srvname,
                    'protocol' => $protocol,
                    'weight'   => $weight,
                    'port'     => $port,
                    'target'   => $target
                ) );
            }
        }
        return $this->http_post( $data );
    }

    /**
     * 5.3 - Delete A DNS Record
     * This function deletes a DNS record for a zone.
     * $zone = zone
     * $id = The DNS Record ID (Available by using the rec_load_all call)
     * $type = A|CNAME
     */
    public function delete_dns_record( $domain, $id )
    {
        $data = array(
            'a'  => 'rec_delete',
            'z'  => $domain,
            'id' => $id
        );
        return $this->http_post( $data );
    }

    /**
     * GLOBAL API CALL
     * HTTP POST a specific task with the supplied data
     */
    private function http_post( $data )
    {
        $data['email'] = $this->email;
        $data['tkn'] = $this->token_key;

        $response = wp_remote_post(
            self::$CLOUDFLARE_API_ENDPOINT,
            array(
                'body' => $data
                )
            );

        do_action( 'sunny_after_cloudflare_api_request', $response, $data );

        return $response;
    }

}

Compare/Review from WP Rocket CloudFlare API

What can we quickly reuse?

<?php
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );

/**
 * CloudFlare API
 */
class WP_Rocket_CloudFlareAPI
{
    // The URL of the API
    private $api_endpoint  = 'https://www.cloudflare.com/api_json.html';

    // The URL of Spam API
    private $spam_endpoint = 'https://www.cloudflare.com/ajax/external-event.html';

    // Timeout for the API requests in seconds
    const TIMEOUT = 5;

    // Stores the api key
    private $api_key;

    // Stores the email login
    private $email;

    /**
     * @var The single instance of the class
     */
    protected static $_instance = null;

    /**
     * Make a new instance of the API client
     */
    public function __construct( $email, $api_key )
    {
        $this->email   = $email;
        $this->api_key = $api_key;
    }

    /**
     * Main WP_Rocket_CloudFlareAPI Instance
     *
     * Ensures only one instance of class is loaded or can be loaded.
     *
     * @static
     * @return Main instance
     */
    public static function instance( $email, $api_key ) {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self( $email, $api_key );
        }
        return self::$_instance;
    }

    /**
     * Retrieve A List Of The Domains
     * This lists all domains in a CloudFlare account along with other data.
     */
    public function zone_load_multi()
    {
        $data = array(
            'a' => 'zone_load_multi'
        );
        return $this->http_post( $data );
    }

    /**
     * List All The Current Settings
     * This function retrieves all the current settings for a given domain.
     */
    public function zone_settings( $domain )
    {
        $data = array(
            'a' => 'zone_settings',
            'z' => $domain
        );
        return $this->http_post( $data );
    }

    /**
     * Set The Cache Level
     * This function sets the Caching Level to Aggressive or Basic.
     * The switches are: (agg|basic).
     */
    public function cache_lvl( $domain, $mode )
    {
        $data = array(
            'a' => 'cache_lvl',
            'z' => $domain,
            'v' => (strtolower($mode) == 'agg') ? 'agg' : 'basic'
        );
        return $this->http_post( $data );
    }

    /**
     * Toggling Development Mode
     * This function allows you to toggle Development Mode on or off for a particular domain.
     * When Development Mode is on the cache is bypassed.
     * Development mode remains on for 3 hours or until when it is toggled back off.
     */
    public function devmode( $domain, $mode )
    {
        $data = array(
            'a' => 'devmode',
            'z' => $domain,
            'v' => ($mode == true) ? 1 : 0
        );
        return $this->http_post( $data );
    }

    /**
     * Clear CloudFlare's Cache
     * This function will purge CloudFlare of any cached files.
     * It may take up to 48 hours for the cache to rebuild and optimum performance to be achieved.
     * This function should be used sparingly.
     */
    public function fpurge_ts( $domain )
    {
        $data = array(
            'a' => 'fpurge_ts',
            'z' => $domain,
            'v' => 1
        );
        return $this->http_post( $data );
    }

    /**
     * Purge A Single File In CloudFlare's Cache
     * This function will purge a single file from CloudFlare's cache.
     */
    public function zone_file_purge( $domain, $url )
    {
        $data = array(
            'a'   => 'zone_file_purge',
            'z'   => $domain,
            'url' => $url
        );
        return $this->http_post( $data );
    }

    /**
     * Set Rocket Loader
     * This function changes Rocket Loader setting.
     */
    public function async( $domain, $mode )
    {
        $data = array(
            'a' => 'async',
            'z' => $domain,
            'v' => $mode
        );
        return $this->http_post( $data );
    }

    /**
     * Set Minification
     * This function changes minification settings.
     */
    public function minify( $domain, $mode )
    {
        $data = array(
            'a' => 'minify',
            'z' => $domain,
            'v' => $mode
        );
        return $this->http_post( $data );
    }

    /**
     * GLOBAL API CALL
     * HTTP POST a specific task with the supplied data
     */
    private function http_post( $data )
    {
        $data['u']   = $this->email;
        $data['tkn'] = $this->api_key;

        $response = wp_remote_post( $this->api_endpoint, array(
            'timeout'     => self::TIMEOUT,
            'headers'     => array(),
            'body'        => $data,
            'cookies'     => array()
        ));

        if ( is_wp_error( $response ) ) {
            return $response->get_error_message();
        } else {
            return json_decode( wp_remote_retrieve_body( $response ) );
        }
    }

    // Reporting Spam IP to CloudFlare
    public function reporting_spam_ip( $payload ) {
        $response = wp_remote_get(
            sprintf( '%s?evnt_v=%s&u=%s&tkn=%s&evnt_t=%s', $this->spam_endpoint, $payload, $this->email, $this->api_key, 'WP_SPAM' ), 
            array(
                'method'        => 'GET',
                'timeout'       => self::TIMEOUT,
                'sslverify'     => true,
                'user-agent'    => 'CloudFlare/WordPress/' . WP_ROCKET_VERSION,
            ) 
        );

        if ( is_wp_error( $response ) ) {
            return $response->get_error_message();
        } else {
            return json_decode( wp_remote_retrieve_body( $response ) );
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.