From 8a1fadced822944a2c8410dd5a8d680ab1e89243 Mon Sep 17 00:00:00 2001 From: Jim Birch Date: Fri, 12 Jan 2018 06:50:28 -0600 Subject: [PATCH] Fixes some coding standards (#3) * Fixes some coding standards * Fixes some WordPress Coding Standards --- Gruntfile.js | 68 +++++++++++++++++---------------- dashboard-connector-wp.php | 10 +++-- includes/CheckerInterface.php | 14 +++---- includes/admin.php | 70 ++++++++++++++++++---------------- includes/jira.php | 25 ++++++------ includes/phpChecker.php | 56 +++++++++++++-------------- includes/rest.php | 42 ++++++++++----------- includes/secure.php | 60 ++++++++++++++++------------- includes/slack.php | 71 ++++++++++++++++++----------------- includes/updates.php | 10 ++--- 10 files changed, 221 insertions(+), 205 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 9888ae2..7457b0a 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -3,52 +3,54 @@ module.exports = function( grunt ) { 'use strict'; var banner = '/**\n * <%= pkg.homepage %>\n * Copyright (c) <%= grunt.template.today("yyyy") %>\n * This file is generated automatically. Do not edit.\n */\n'; // Project configuration - grunt.initConfig( { + grunt.initConfig( + { - pkg: grunt.file.readJSON( 'package.json' ), + pkg: grunt.file.readJSON( 'package.json' ), - addtextdomain: { - options: { - textdomain: 'xdb', - }, - target: { - files: { - src: [ '*.php', '**/*.php', '!node_modules/**', '!php-tests/**', '!bin/**' ] + addtextdomain: { + options: { + textdomain: 'xdb', + }, + target: { + files: { + src: [ '*.php', '**/*.php', '!node_modules/**', '!php-tests/**', '!bin/**' ] + } } - } - }, + }, - wp_readme_to_markdown: { - your_target: { - files: { - 'README.md': 'readme.txt' - } + wp_readme_to_markdown: { + your_target: { + files: { + 'README.md': 'readme.txt' + } + }, }, - }, - makepot: { - target: { - options: { - domainPath: '/languages', - mainFile: 'dashboard-connector-wp.php', - potFilename: 'dashboard-connector-wp.pot', - potHeaders: { - poedit: true, - 'x-poedit-keywordslist': true - }, - type: 'wp-plugin', - updateTimestamp: true + makepot: { + target: { + options: { + domainPath: '/languages', + mainFile: 'dashboard-connector-wp.php', + potFilename: 'dashboard-connector-wp.pot', + potHeaders: { + poedit: true, + 'x-poedit-keywordslist': true + }, + type: 'wp-plugin', + updateTimestamp: true + } } - } - }, - } ); + }, + } + ); grunt.loadNpmTasks( 'grunt-wp-i18n' ); grunt.loadNpmTasks( 'grunt-wp-readme-to-markdown' ); grunt.registerTask( 'i18n', ['addtextdomain', 'makepot'] ); grunt.registerTask( 'readme', ['wp_readme_to_markdown'] ); - grunt.registerTask('default', ['makepot','wp_readme_to_markdown','addtextdomain']); + grunt.registerTask( 'default', ['makepot','wp_readme_to_markdown','addtextdomain'] ); grunt.util.linefeed = '\n'; }; diff --git a/dashboard-connector-wp.php b/dashboard-connector-wp.php index a845825..afc18c6 100755 --- a/dashboard-connector-wp.php +++ b/dashboard-connector-wp.php @@ -46,11 +46,15 @@ function xdb_get_settings( $setting, $denifed ) { * Enviroment indicator. */ function xdb_env_indicator() { - $colors = array( 'dev'=>'#aa3333','test'=>'#ceaf01','prod'=>'rgb(0, 0, 187)' ); - $env = xdb_get_settings( $setting = 'r_env', $defined = 'XDB_ENV'); + $colors = array( + 'dev' => '#aa3333', + 'test' => '#ceaf01', + 'prod' => 'rgb(0, 0, 187)', + ); + $env = xdb_get_settings( $setting = 'r_env', $defined = 'XDB_ENV' ); ?> capability, $this->slug, array( - $this, - 'xdb_options_page' - ) ); + add_options_page( + __( 'Dashboard Connector WP', 'xdb' ), __( 'Dashboard Connector WP', 'xdb' ), $this->capability, $this->slug, array( + $this, + 'xdb_options_page', + ) + ); } /** @@ -140,22 +141,25 @@ public function xdb_options_init() { add_settings_section( 'xdb_options_section', '', '', $this->group ); - add_settings_field( 'xdb_rest', __( 'Dashboard Connector WP', 'xdb' ), array( - $this, - 'xdb_rest_render' - ), $this->group, 'xdb_options_section' + add_settings_field( + 'xdb_rest', __( 'Dashboard Connector WP', 'xdb' ), array( + $this, + 'xdb_rest_render', + ), $this->group, 'xdb_options_section' ); - add_settings_field( 'xdb_slack', __( 'Slack', 'xdb' ), array( - $this, - 'xdb_slack_render' - ), $this->group, 'xdb_options_section' + add_settings_field( + 'xdb_slack', __( 'Slack', 'xdb' ), array( + $this, + 'xdb_slack_render', + ), $this->group, 'xdb_options_section' ); - add_settings_field( 'xdb_jira', __( 'Jira', 'xdb' ), array( - $this, - 'xdb_jira_render' - ), $this->group, 'xdb_options_section' + add_settings_field( + 'xdb_jira', __( 'Jira', 'xdb' ), array( + $this, + 'xdb_jira_render', + ), $this->group, 'xdb_options_section' ); } @@ -184,10 +188,10 @@ public function xdb_rest_render() { foreach ( $setting_options as $list => $o ) { $opt = empty( $options[ $o ] ) ? '' : $options[ $o ]; ?> - - -
+ + +
$o ) { $opt = empty( $options[ $o ] ) ? '' : $options[ $o ]; ?> - - -
+ + +
$o ) { $opt = empty( $options[ $o ] ) ? '' : $options[ $o ]; ?> - - -
+ + +
-
-

+ +

group ); do_settings_sections( $this->group ); submit_button(); ?> -
+ description = ''; foreach ( $site_data as $data => $d ) { - if ( false !== strpos( strtolower( $d['description'] ), "up to date" ) ) { + if ( false !== strpos( strtolower( $d['description'] ), 'up to date' ) ) { unset( $d ); } else { // Mix field with defaults. @@ -136,7 +138,6 @@ private function get_description() { $vulnerable = true; } } - } return $vulnerable; @@ -155,7 +156,8 @@ private function clean_transiten( $string ) { str_replace( array( "\r\n", "\n", "\r", "\t", 'plugin', 'theme', 'core' ), '', - $string ) + $string + ) ), 'rl' ); @@ -219,15 +221,14 @@ public function open_task() { $url = $server . 'rest/api/latest/issue'; $response = $this->curl( $url, json_encode( $data ) ); -// if ( false !== $response ) { -// $jira_id = json_decode( $response ); -// if ( isset( $jira_id->key ) ) { -// $data = '{"update": {"comment": [{"add": {"body": "Starts progress automatically"}}]},"transition": {"id": "' . $this->settings['progress_transition_id'] . '"}}'; -// $url = $server . 'rest/api/latest/issue/' . $jira_id->key . '/transitions?expand=transitions.fields'; -// $jira_id = $this->curl( $url, $data ); -// } -// } - + // if ( false !== $response ) { + // $jira_id = json_decode( $response ); + // if ( isset( $jira_id->key ) ) { + // $data = '{"update": {"comment": [{"add": {"body": "Starts progress automatically"}}]},"transition": {"id": "' . $this->settings['progress_transition_id'] . '"}}'; + // $url = $server . 'rest/api/latest/issue/' . $jira_id->key . '/transitions?expand=transitions.fields'; + // $jira_id = $this->curl( $url, $data ); + // } + // } // Generates new transitent. $transiten = $this->clean_transiten( $this->description ); diff --git a/includes/phpChecker.php b/includes/phpChecker.php index f0875b5..8abcfa5 100644 --- a/includes/phpChecker.php +++ b/includes/phpChecker.php @@ -28,41 +28,37 @@ class PHPChecker implements CheckerInterface { * {@inheritdoc} */ public function getChecks() { - $eol = FALSE; + $eol = false; $checks = array(); $version = $this->getVersion(); $time = $this->getTime(); // Anything older than 5.5 has been end-of-lifed already. - if ($version < 50500) { - $eol = TRUE; - } - // 5.5 will be EOL 10 Jul 2016. - elseif ($version < 50600 && $time > 1468108800) { - $eol = TRUE; - } - // 5.6 will be EOL 28 Aug 2017. - elseif ($version < 70000 && $time > 1503878400) { - $eol = TRUE; - } - // Assuming the next is 7.1, 7.0 will be EOL 3 Dec 2018. - elseif ($version < 70100 && $time > 1543795200) { - $eol = TRUE; + if ( $version < 50500 ) { + $eol = true; + } // 5.5 will be EOL 10 Jul 2016. + elseif ( $version < 50600 && $time > 1468108800 ) { + $eol = true; + } // 5.6 will be EOL 28 Aug 2017. + elseif ( $version < 70000 && $time > 1503878400 ) { + $eol = true; + } // Assuming the next is 7.1, 7.0 will be EOL 3 Dec 2018. + elseif ( $version < 70100 && $time > 1543795200 ) { + $eol = true; } - if ($eol) { + if ( $eol ) { $checks[] = array( - 'name' => 'version', - 'description' => $this->t('PHP !version is no longer maintained.', array('!version' => PHP_VERSION)), - 'type' => 'php', + 'name' => 'version', + 'description' => $this->t( 'PHP !version is no longer maintained.', array( '!version' => PHP_VERSION ) ), + 'type' => 'php', 'alert_level' => 'error', ); - } - else { + } else { $checks[] = array( - 'name' => 'version', - 'description' => $this->t('Running on PHP !version.', array('!version' => PHP_VERSION)), - 'type' => 'php', + 'name' => 'version', + 'description' => $this->t( 'Running on PHP !version.', array( '!version' => PHP_VERSION ) ), + 'type' => 'php', 'alert_level' => 'notice', ); } @@ -76,9 +72,9 @@ public function getChecks() { protected function getVersion() { // Ensure we have all the defines we're looking for, even if running // on a PHP from the stone age. - if (!defined('PHP_VERSION_ID')) { - $version = explode('.', PHP_VERSION); - return ($version[0] * 10000 + $version[1] * 100 + $version[2]); + if ( ! defined( 'PHP_VERSION_ID' ) ) { + $version = explode( '.', PHP_VERSION ); + return ( $version[0] * 10000 + $version[1] * 100 + $version[2] ); } return PHP_VERSION_ID; } @@ -95,15 +91,15 @@ protected function getTime() { * * @param string $string * A string containing the English string to translate. - * @param array $args + * @param array $args * An associative array of replacements to make after translation. - * @param array $options + * @param array $options * An associative array of additional options * * @return string * The translated string. */ - protected function t($string, array $args = array()) { + protected function t( $string, array $args = array() ) { // @codingStandardsIgnoreStart return strtr($string, $args); // @codingStandardsIgnoreEnd diff --git a/includes/rest.php b/includes/rest.php index e485a43..a5d26f9 100755 --- a/includes/rest.php +++ b/includes/rest.php @@ -88,13 +88,11 @@ public function init_hooks() { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); add_action( 'xdb_rest_notify_dashboard', array( $this, 'rest_notify_dashboard' ) ); - // Cron to post in Dashboard Connector WP. if ( ! wp_next_scheduled( 'xdb_rest_notify_dashboard' ) ) { wp_schedule_event( time(), 'twicedaily', 'xdb_rest_notify_dashboard' ); } - } /** @@ -130,22 +128,26 @@ public function rest_notify_dashboard() { public function register_routes() { // Register the updates check endpoint. - register_rest_route( $this->namespace, '/site-info', array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_site_info' ), - 'permission_callback' => array( $this, 'permissions_check' ), - ), - ) ); + register_rest_route( + $this->namespace, '/site-info', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_site_info' ), + 'permission_callback' => array( $this, 'permissions_check' ), + ), + ) + ); // Register the updates check endpoint. - register_rest_route( $this->namespace, '/slack-talk', array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_slack_talk' ), - 'permission_callback' => array( $this, 'permissions_check' ), - ), - ) ); + register_rest_route( + $this->namespace, '/slack-talk', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_slack_talk' ), + 'permission_callback' => array( $this, 'permissions_check' ), + ), + ) + ); } /** @@ -209,7 +211,6 @@ private function prepare_data() { $Dashboard_Connector_WP_Updates = new Dashboard_Connector_WP_Updates(); $Dashboard_Connector_WP_PHPChecker = new PHPChecker(); - // PHP. $data = array_merge( $data, $Dashboard_Connector_WP_PHPChecker->getChecks() ); @@ -293,7 +294,6 @@ public function permissions_check( $request ) { array( 'status' => 403 ) ); } - } else { // In case there is not a way to check for the super token. @@ -323,7 +323,7 @@ public function post_to_xeno() { $ch = curl_init(); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_URL, $this->settings['url'] ); - curl_setopt( $ch, CURLOPT_USERPWD, $this->settings['username'] . ":" . $this->settings['pwd'] ); + curl_setopt( $ch, CURLOPT_USERPWD, $this->settings['username'] . ':' . $this->settings['pwd'] ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); @@ -332,11 +332,11 @@ public function post_to_xeno() { $result = curl_exec( $ch ); $ch_error = curl_error( $ch ); if ( $ch_error ) { - //echo sprintf( 'cURL Error: %s', $ch_error ); + // echo sprintf( 'cURL Error: %s', $ch_error ); return false; } curl_close( $ch ); - //echo "\n" . $result; + // echo "\n" . $result; } } diff --git a/includes/secure.php b/includes/secure.php index a9297d4..ccfe527 100755 --- a/includes/secure.php +++ b/includes/secure.php @@ -1,4 +1,10 @@ 'HS256', + 'alg' => 'HS256', 'typ' => 'JWT', ) - ) + ) ); // JWT payload. - $encoded_payload = base64_encode( - wp_json_encode( + $encoded_payload = base64_encode( + wp_json_encode( array( - 'score' => '12', - 'name' => 'Crille', - ) - ) + 'score' => '12', + 'name' => 'Crille', + ) + ) ); // JWT combined. $header_and_payload_combined = $encoded_header . '.' . $encoded_payload; // JWT Signature. - $signature = base64_encode( + $signature = base64_encode( hash_hmac( - 'sha256', + 'sha256', $header_and_payload_combined, $supersecret, true @@ -71,15 +77,15 @@ function xdb_get_supertoken() { * * @return bool true if $signature is equal $recieved_signature otherwise false. * - * @since 1.0.0 + * @since 1.0.0 */ function xdb_check_supertoken( $recieved_jwt ) { $supersecret = ''; - + $jwt_values = explode( '.', $recieved_jwt ); - if ( count( $jwt_values) != 3 ) { + if ( count( $jwt_values ) != 3 ) { return false; } @@ -92,26 +98,28 @@ function xdb_check_supertoken( $recieved_jwt ) { } } - // JWT Payload + // JWT Payload. $recieved_header_and_payload = $jwt_values[0] . '.' . $jwt_values[1]; // JWT Signature received. $recieved_signature = $jwt_values[2]; // JWT Signature. - $signature = base64_encode( - hash_hmac( - 'sha256', - $recieved_header_and_payload, - $supersecret, - true + $signature = base64_encode( + hash_hmac( + 'sha256', + $recieved_header_and_payload, + $supersecret, + true ) ); return ( $signature == $recieved_signature ); } -// TODO for admin settings page, hides strings in input. +/** + * TODO for admin settings page, hides strings in input. + */ function xdb_get_starred( $str ) { return substr( $str, 0, 1 ) . str_repeat( '*', $len = strlen( $str ) - 2 ) . substr( $str, $len - 1, 1 ); -} \ No newline at end of file +} diff --git a/includes/slack.php b/includes/slack.php index 25f3a20..e9cd95e 100755 --- a/includes/slack.php +++ b/includes/slack.php @@ -7,7 +7,7 @@ // If this file is called directly, abort. if ( ! defined( 'ABSPATH' ) ) { - die; + die; } /** @@ -41,16 +41,16 @@ class Dashboard_Connector_WP_Slack { public function __construct() { // Slack Webhook - $setting_webhook = xdb_get_settings( $setting = 's_webhook', $defined = 'XDB_SLACK_WEBHOOK'); + $setting_webhook = xdb_get_settings( $setting = 's_webhook', $defined = 'XDB_SLACK_WEBHOOK' ); // Slack connections. $this->settings = array( - 'enable' => ! empty( $setting_webhook ), + 'enable' => ! empty( $setting_webhook ), 'end_point' => SLACK_WH . $setting_webhook, - 'bot_name' => __('Dashboard Connector WP', 'xdb'), - 'bot_icon' => '', - 'channels' => xdb_get_settings( $setting = 's_channels', $defined = 'XDB_SLACK_CHANNELS'), - 'notify' => xdb_get_settings( $setting = 's_notify', $defined = 'XDB_SLACK_NOTIFY'), + 'bot_name' => __( 'Dashboard Connector WP', 'xdb' ), + 'bot_icon' => '', + 'channels' => xdb_get_settings( $setting = 's_channels', $defined = 'XDB_SLACK_CHANNELS' ), + 'notify' => xdb_get_settings( $setting = 's_notify', $defined = 'XDB_SLACK_NOTIFY' ), ); } @@ -64,11 +64,11 @@ public function __construct() { * @access public */ public function xdb_admin_notice() { - ?> -
-

-
- +
+

+
+ prepare_core_response( $data ); } // Plugins - if ( empty( $type ) || 'all' == $type || 'plugins' == $type ) { + if ( empty( $type ) || 'all' == $type || 'plugins' == $type ) { $Dashboard_Connector_WP_Updates->prepare_plugins_response( $data ); } // Themes. - if ( empty( $type ) || 'all' == $type || 'themes' == $type ) { + if ( empty( $type ) || 'all' == $type || 'themes' == $type ) { $Dashboard_Connector_WP_Updates->prepare_themes_response( $data ); } $vulnerable = false; // Build fields. - $the_fields = array(); if ( ! empty( $data ) ) { // Setup each attachment. foreach ( $data as $attachments => $attachment ) { - if ( true === $updates_only && false !== strpos( strtolower( $attachment['description'] ), "up to date" ) ) { - unset($attachment); + if ( true === $updates_only && false !== strpos( strtolower( $attachment['description'] ), 'up to date' ) ) { + unset( $attachment ); } else { $field = array( @@ -154,16 +153,16 @@ private function send_reponse_to_slack( $the_fields, $vulnerable ) { return false; } - $webhook_url = $this->settings['end_point'] ; + $webhook_url = $this->settings['end_point']; // Set defaults. $payload = array( - 'channel' => 'jenkins-ci', - 'username' => get_bloginfo( 'name' ), - 'text' => sprintf( '*<%1$s|%2$s>*' . "\n" . '%3$s', get_bloginfo( 'url' ), get_bloginfo( 'name' ), 'Xeno vulnerabilities tests' ), - 'icon_emoji' => ( $vulnerable ) ? ':fire': ':mega:', - 'icon_url' => trailingslashit( plugin_dir_url( dirname( __FILE__ ) ) ) . 'assets/images/xeno.png', - 'attachments' => array(), + 'channel' => 'jenkins-ci', + 'username' => get_bloginfo( 'name' ), + 'text' => sprintf( '*<%1$s|%2$s>*' . "\n" . '%3$s', get_bloginfo( 'url' ), get_bloginfo( 'name' ), 'Xeno vulnerabilities tests' ), + 'icon_emoji' => ( $vulnerable ) ? ':fire' : ':mega:', + 'icon_url' => trailingslashit( plugin_dir_url( dirname( __FILE__ ) ) ) . 'assets/images/xeno.png', + 'attachments' => array(), ); // Set field defaults. @@ -174,8 +173,8 @@ private function send_reponse_to_slack( $the_fields, $vulnerable ) { ); $payload['attachments'][] = array( - 'color' => ( $vulnerable ) ? '#d52121': '#21759b', // Default color. - 'fields' => $the_fields, + 'color' => ( $vulnerable ) ? '#d52121' : '#21759b', // Default color. + 'fields' => $the_fields, ); // Channels @@ -201,13 +200,15 @@ private function send_reponse_to_slack( $the_fields, $vulnerable ) { $payload['channel'] = $channel; // Send to Slack. - $slack_response = wp_remote_post( $webhook_url, array( - 'sslverify' => false, // for old versions. - 'body' => json_encode( $payload ), - 'headers' => array( - 'Content-Type' => 'application/json', - ), - )); + $slack_response = wp_remote_post( + $webhook_url, array( + 'sslverify' => false, // for old versions. + 'body' => json_encode( $payload ), + 'headers' => array( + 'Content-Type' => 'application/json', + ), + ) + ); // Handle errors. if ( is_wp_error( $slack_response ) ) { @@ -220,7 +221,7 @@ private function send_reponse_to_slack( $the_fields, $vulnerable ) { && '200' != $slack_response['response']['code'] ) { // Set an error. - $xdb_errors->add( 'xdb_slack_api_error', __( 'Error: The payload did not send to Slack', 'xdb' ) ); + $xdb_errors->add( 'xdb_slack_api_error', __( 'Error: The payload did not send to Slack', 'xdb' ) ); } } diff --git a/includes/updates.php b/includes/updates.php index 9b642f6..c7dbde0 100755 --- a/includes/updates.php +++ b/includes/updates.php @@ -45,11 +45,11 @@ public function prepare_core_response( &$data, $only_available = false ) { // WordPress update check. if ( ! function_exists( 'get_core_updates' ) ) { - require_once( ABSPATH . 'wp-admin/includes/update.php' ); + require_once ABSPATH . 'wp-admin/includes/update.php'; } // Some plugins maybe hidding core version, so let's check it in version.php. - require_once( ABSPATH . WPINC . '/version.php' ); + require_once ABSPATH . WPINC . '/version.php'; global $wp_version; // Gets current version of wp core. @@ -334,8 +334,8 @@ private function check_with_wpvulndb( $type, $slug = null, $ver ) { $xdb_errors->add( 'xdb_slack_api_error', $response->get_error_message() ); } elseif ( ! empty( $response['response'] ) - && ! empty( $response['response']['code'] ) - && '200' != $response['response']['code'] ) { + && ! empty( $response['response']['code'] ) + && '200' != $response['response']['code'] ) { // Set an error. $xdb_errors->add( 'xdb_slack_api_error', __( 'Error: Couldn\'t connect to wpvulndb', 'xdb' ) ); @@ -354,7 +354,7 @@ private function check_with_wpvulndb( $type, $slug = null, $ver ) { * with all the versions reported as vulnerables * return true if the current version is not greater than all the * versions reported as fixed. - **/ + */ $cur_ver = filter_var( $ver, FILTER_SANITIZE_NUMBER_INT ); foreach ( $json as $vul => $key ) { $count = count( $key->vulnerabilities );