kennyfraser /
wordpress-github-sync
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Base API client class. |
||
| 4 | * @package WordPress_GitHub_Sync |
||
| 5 | */ |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class WordPress_GitHub_Sync_Base_Client |
||
| 9 | */ |
||
| 10 | class WordPress_GitHub_Sync_Base_Client { |
||
| 11 | |||
| 12 | const TOKEN_OPTION_KEY = 'wpghs_oauth_token'; |
||
| 13 | const REPO_OPTION_KEY = 'wpghs_repository'; |
||
| 14 | const HOST_OPTION_KEY = 'wpghs_host'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Application container. |
||
| 18 | * |
||
| 19 | * @var WordPress_GitHub_Sync |
||
| 20 | */ |
||
| 21 | protected $app; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Instantiates a new Api object. |
||
| 25 | * |
||
| 26 | * @param WordPress_GitHub_Sync $app Application container. |
||
| 27 | */ |
||
| 28 | 19 | public function __construct( WordPress_GitHub_Sync $app ) { |
|
| 29 | 19 | $this->app = $app; |
|
| 30 | 19 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * Generic GitHub API interface and response handler |
||
| 34 | * |
||
| 35 | * @param string $method HTTP method. |
||
| 36 | * @param string $endpoint API endpoint. |
||
| 37 | * @param array $body Request body. |
||
| 38 | * |
||
| 39 | * @return stdClass|WP_Error |
||
| 40 | */ |
||
| 41 | 16 | protected function call( $method, $endpoint, $body = array() ) { |
|
| 42 | 16 | if ( is_wp_error( $error = $this->can_call() ) ) { |
|
|
0 ignored issues
–
show
Bug
Compatibility
introduced
by
Loading history...
|
|||
| 43 | 3 | return $error; |
|
| 44 | } |
||
| 45 | |||
| 46 | $args = array( |
||
| 47 | 13 | 'method' => $method, |
|
| 48 | 'headers' => array( |
||
| 49 | 13 | 'Authorization' => 'token ' . $this->oauth_token(), |
|
| 50 | 13 | ), |
|
| 51 | 13 | 'body' => function_exists( 'wp_json_encode' ) ? |
|
| 52 | 13 | wp_json_encode( $body ) : |
|
| 53 | 13 | json_encode( $body ), |
|
| 54 | 13 | ); |
|
| 55 | |||
| 56 | 13 | $response = wp_remote_request( $endpoint, $args ); |
|
| 57 | 13 | $status = wp_remote_retrieve_header( $response, 'status' ); |
|
| 58 | 13 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 59 | |||
| 60 | 13 | if ( '2' !== substr( $status, 0, 1 ) && '3' !== substr( $status, 0, 1 ) ) { |
|
| 61 | 7 | return new WP_Error( |
|
| 62 | 7 | strtolower( str_replace( ' ', '_', $status ) ), |
|
| 63 | 7 | sprintf( |
|
| 64 | 7 | __( 'Method %s to endpoint %s failed with error: %s', 'wordpress-github-sync' ), |
|
| 65 | 7 | $method, |
|
| 66 | 7 | $endpoint, |
|
| 67 | 7 | $body && $body->message ? $body->message : 'Unknown error' |
|
| 68 | 7 | ) |
|
| 69 | 7 | ); |
|
| 70 | } |
||
| 71 | |||
| 72 | 11 | return $body; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Validates whether the Api object can make a call. |
||
| 77 | * |
||
| 78 | * @return true|WP_Error |
||
| 79 | */ |
||
| 80 | 16 | protected function can_call() { |
|
| 81 | 16 | if ( ! $this->oauth_token() ) { |
|
| 82 | 1 | return new WP_Error( |
|
| 83 | 1 | 'missing_token', |
|
| 84 | 1 | __( 'WordPress-GitHub-Sync needs an auth token. Please update your settings.', 'wordpress-github-sync' ) |
|
| 85 | 1 | ); |
|
| 86 | } |
||
| 87 | |||
| 88 | 15 | $repo = $this->repository(); |
|
| 89 | |||
| 90 | 15 | if ( ! $repo ) { |
|
| 91 | 1 | return new WP_Error( |
|
| 92 | 1 | 'missing_repository', |
|
| 93 | 1 | __( 'WordPress-GitHub-Sync needs a repository. Please update your settings.', 'wordpress-github-sync' ) |
|
| 94 | 1 | ); |
|
| 95 | } |
||
| 96 | |||
| 97 | 14 | $parts = explode( '/', $repo ); |
|
| 98 | |||
| 99 | 14 | if ( 2 !== count( $parts ) ) { |
|
| 100 | 1 | return new WP_Error( |
|
| 101 | 1 | 'malformed_repository', |
|
| 102 | 1 | __( 'WordPress-GitHub-Sync needs a properly formed repository. Please update your settings.', 'wordpress-github-sync' ) |
|
| 103 | 1 | ); |
|
| 104 | } |
||
| 105 | |||
| 106 | 13 | return true; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the repository to sync with |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 16 | public function repository() { |
|
| 115 | 16 | return (string) get_option( self::REPO_OPTION_KEY ); |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns the user's oauth token |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 16 | public function oauth_token() { |
|
| 124 | 16 | return (string) get_option( self::TOKEN_OPTION_KEY ); |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns the GitHub host to sync with (for GitHub Enterprise support) |
||
| 129 | */ |
||
| 130 | 16 | public function api_base() { |
|
| 131 | 16 | return get_option( self::HOST_OPTION_KEY ); |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * API endpoint for the master branch reference |
||
| 136 | */ |
||
| 137 | 14 | public function reference_endpoint() { |
|
| 138 | 14 | $sync_branch = apply_filters( 'wpghs_sync_branch', 'master' ); |
|
| 139 | |||
| 140 | 14 | if ( ! $sync_branch ) { |
|
| 141 | throw new Exception( __( 'Sync branch not set. Filter `wpghs_sync_branch` misconfigured.', 'wordpress-github-sync' ) ); |
||
| 142 | } |
||
| 143 | |||
| 144 | 14 | $url = $this->api_base() . '/repos/'; |
|
| 145 | 14 | $url = $url . $this->repository() . '/git/refs/heads/' . $sync_branch; |
|
| 146 | |||
| 147 | 14 | return $url; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Api to get and create commits |
||
| 152 | */ |
||
| 153 | 10 | public function commit_endpoint() { |
|
| 154 | 10 | $url = $this->api_base() . '/repos/'; |
|
| 155 | 10 | $url = $url . $this->repository() . '/git/commits'; |
|
| 156 | |||
| 157 | 10 | return $url; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Api to get and create trees |
||
| 162 | */ |
||
| 163 | 9 | public function tree_endpoint() { |
|
| 164 | 9 | $url = $this->api_base() . '/repos/'; |
|
| 165 | 9 | $url = $url . $this->repository() . '/git/trees'; |
|
| 166 | |||
| 167 | 9 | return $url; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Builds the proper blob API endpoint for a given post |
||
| 172 | * |
||
| 173 | * Returns String the relative API call path |
||
| 174 | */ |
||
| 175 | 2 | public function blob_endpoint() { |
|
| 176 | 2 | $url = $this->api_base() . '/repos/'; |
|
| 177 | 2 | $url = $url . $this->repository() . '/git/blobs'; |
|
| 178 | |||
| 179 | 2 | return $url; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Builds the proper content API endpoint for a given post |
||
| 184 | * |
||
| 185 | * Returns String the relative API call path |
||
| 186 | */ |
||
| 187 | public function content_endpoint() { |
||
| 188 | $url = $this->api_base() . '/repos/'; |
||
| 189 | $url = $url . $this->repository() . '/contents/'; |
||
| 190 | |||
| 191 | return $url; |
||
| 192 | } |
||
| 193 | } |
||
| 194 |