[type, scope, label, why] */ public static function permissions() { return array( 'zone' => array( 'read', 'zone', __( 'Zone: Read', 'zonepilot' ), __( 'find the domain of this site', 'zonepilot' ) ), 'zone_settings' => array( 'edit', 'zone', __( 'Zone Settings: Edit', 'zonepilot' ), __( 'read SSL/TLS settings for the health check', 'zonepilot' ) ), 'dns' => array( 'edit', 'zone', __( 'DNS: Edit', 'zonepilot' ), __( 'find records that leak the server IP and fix them when you confirm', 'zonepilot' ) ), 'cache_settings' => array( 'edit', 'zone', __( 'Cache Rules: Edit', 'zonepilot' ), __( 'page caching rule', 'zonepilot' ) ), 'cache' => array( 'purge', 'zone', __( 'Cache Purge', 'zonepilot' ), __( 'purge changed pages', 'zonepilot' ) ), 'zone_waf' => array( 'edit', 'zone', __( 'Zone WAF: Edit', 'zonepilot' ), __( 'security and rate limiting rules', 'zonepilot' ) ), 'zone_transform_rules' => array( 'edit', 'zone', __( 'Transform Rules: Edit', 'zonepilot' ), __( 'security headers and the origin lock header', 'zonepilot' ) ), 'config_settings' => array( 'edit', 'zone', __( 'Config Rules: Edit', 'zonepilot' ), __( 'per-site SSL and image settings', 'zonepilot' ) ), 'dynamic_redirect' => array( 'edit', 'zone', __( 'Dynamic URL Redirects: Edit', 'zonepilot' ), __( 'HTTP to HTTPS redirect for this site', 'zonepilot' ) ), 'ssl_and_certificates' => array( 'read', 'zone', __( 'SSL and Certificates: Read', 'zonepilot' ), __( 'certificate health check', 'zonepilot' ) ), 'challenge_widgets' => array( 'edit', 'account', __( 'Turnstile: Edit', 'zonepilot' ), __( 'create the Turnstile widget for your forms', 'zonepilot' ) ), 'workers_r2' => array( 'edit', 'account', __( 'Workers R2 Storage: Edit', 'zonepilot' ), __( 'backup bucket', 'zonepilot' ) ), 'account_settings' => array( 'read', 'account', __( 'Account Settings: Read', 'zonepilot' ), __( 'identify your account', 'zonepilot' ) ), ); } /** * Permission keys for the dashboard "create token" template link (built in admin.js). * * @return array List of [key, type]. */ public static function template_keys() { $keys = array(); foreach ( self::permissions() as $key => $perm ) { $keys[] = array( 'key' => $key, 'type' => $perm[0], ); } return $keys; } /** * Connect with a token. * * @param string $token Token. * @return array|WP_Error Connection summary. */ public static function connect( $token ) { $token = trim( (string) $token ); if ( ! preg_match( '/^[A-Za-z0-9_\-]{30,80}$/', $token ) ) { return new WP_Error( 'fehu_ap_token_format', __( 'This does not look like a Cloudflare API token. Copy the token value shown once after you create it (not the Global API Key).', 'zonepilot' ) ); } $cf = new Cloudflare( $token ); $verify = $cf->verify(); if ( is_wp_error( $verify ) ) { return new WP_Error( 'fehu_ap_token_invalid', sprintf( /* translators: %s: error */ __( 'Cloudflare did not accept the token: %s', 'zonepilot' ), $verify->get_error_message() ) ); } if ( 'active' !== ( isset( $verify['status'] ) ? $verify['status'] : '' ) ) { return new WP_Error( 'fehu_ap_token_inactive', __( 'The token is not active.', 'zonepilot' ) ); } $zone = $cf->find_zone_for_host( Site::host() ); if ( is_wp_error( $zone ) ) { return $zone; } Store::set_secret( 'cf_token', $token ); Store::set( 'token_id', isset( $verify['id'] ) ? (string) $verify['id'] : '' ); Store::set( 'zone_id', $zone['id'] ); Store::set( 'zone_name', $zone['name'] ); Store::set( 'zone_status', isset( $zone['status'] ) ? $zone['status'] : '' ); Store::set( 'account_id', isset( $zone['account']['id'] ) ? $zone['account']['id'] : '' ); Store::set( 'zone_plan', isset( $zone['plan']['name'] ) ? $zone['plan']['name'] : '' ); Store::set( 'connected_at', time() ); // Hosts: this site plus its www / non-www twin when that twin is proxied in this zone. $hosts = array( Site::host() ); $twin = 0 === strpos( Site::host(), 'www.' ) ? substr( Site::host(), 4 ) : 'www.' . Site::host(); $records = $cf->dns_records( $zone['id'] ); $proxied = false; if ( ! is_wp_error( $records ) ) { foreach ( $records as $r ) { if ( strtolower( $r['name'] ) === $twin && ! empty( $r['proxied'] ) ) { $hosts[] = $twin; } if ( strtolower( $r['name'] ) === Site::host() && ! empty( $r['proxied'] ) ) { $proxied = true; } } } Store::set( 'hosts', array_values( array_unique( $hosts ) ) ); $perms = self::check_permissions( $cf, $zone['id'], Store::get( 'account_id' ) ); Store::set( 'permissions', $perms ); Store::log( 'core', sprintf( /* translators: %s: zone */ __( 'Connected to Cloudflare zone %s.', 'zonepilot' ), $zone['name'] ), 'ok' ); return array( 'zone' => $zone['name'], 'plan' => Site::plan(), 'hosts' => Site::hosts(), 'proxied' => $proxied, 'permissions' => $perms, ); } /** * Read-only probes for each permission group. * * @param Cloudflare $cf Client. * @param string $zone Zone id. * @param string $account Account id. * @return array key => ['ok' => bool|null, 'label' => string] */ public static function check_permissions( Cloudflare $cf, $zone, $account ) { $z = '/zones/' . rawurlencode( $zone ); $a = '/accounts/' . rawurlencode( $account ); $probes = array( 'dns' => array( 'GET', $z . '/dns_records', array( 'per_page' => '5' ) ), 'zone_settings' => array( 'GET', $z . '/settings/ssl', array() ), 'cache_settings' => array( 'GET', $z . '/rulesets/phases/http_request_cache_settings/entrypoint', array() ), 'zone_waf' => array( 'GET', $z . '/rulesets/phases/http_request_firewall_custom/entrypoint', array() ), 'zone_transform_rules' => array( 'GET', $z . '/rulesets/phases/http_request_late_transform/entrypoint', array() ), 'config_settings' => array( 'GET', $z . '/rulesets/phases/http_config_settings/entrypoint', array() ), 'challenge_widgets' => array( 'GET', $a . '/challenges/widgets', array() ), 'workers_r2' => array( 'GET', $a . '/r2/buckets', array() ), ); $labels = self::permissions(); $out = array(); foreach ( $probes as $key => $probe ) { $res = $cf->request( $probe[0], $probe[1], null, $probe[2] ); $ok = true; $note = ''; if ( is_wp_error( $res ) ) { $data = $res->get_error_data(); $status = is_array( $data ) && isset( $data['status'] ) ? (int) $data['status'] : 0; if ( 404 === $status ) { $ok = true; // Entry point does not exist yet: readable. } elseif ( 'workers_r2' === $key && false !== stripos( $res->get_error_message(), 'enable' ) ) { $ok = null; $note = __( 'R2 is not enabled on this account yet (only needed for backups).', 'zonepilot' ); } else { $ok = false; $note = $res->get_error_message(); } } $out[ $key ] = array( 'ok' => $ok, 'label' => $labels[ $key ][2], 'note' => $note, ); } return $out; } /** * Forget the token (rules on Cloudflare stay as they are). */ public static function disconnect() { Store::set_secret( 'cf_token', '' ); foreach ( array( 'zone_id', 'zone_name', 'account_id', 'zone_plan', 'token_id', 'permissions', 'connected_at', 'hosts' ) as $key ) { Store::delete( $key ); } Store::log( 'core', __( 'Disconnected from Cloudflare. Rules created earlier stay active until you undo them.', 'zonepilot' ), 'info' ); } }