PluginProbe
CatFolders – WordPress Media Library Folders & Categories / trunk
CatFolders – WordPress Media Library Folders & Categories vtrunk
2.5.6 2.5.5 trunk 1.6.1 1.8 1.9 2.0 2.2 2.3.1 2.3.2 2.4.4 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4
catfolders / includes / Rest / Controllers / PublicAPIController.php

PublicAPIController.php in CatFolders – WordPress Media Library Folders & Categories trunk, at includes/Rest/Controllers/PublicAPIController.php

120 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CatFolders\Rest\Controllers;
4
5 use CatFolders\Classes\Helpers;
6
7 class PublicAPIController {
8
9 const CATF_ROUTE_PUBLIC_NAMESPACE = CATF_ROUTE_NAMESPACE . '/public';
10
11 private $folderController;
12
13 public function __construct() {
14 $this->folderController = new FolderController();
15 }
16
17 public function register_routes() {
18 register_rest_route(
19 CATF_ROUTE_NAMESPACE,
20 '/generate-api-key',
21 array(
22 array(
23 'methods' => \WP_REST_Server::CREATABLE,
24 'callback' => array( $this, 'generate_api_key' ),
25 'permission_callback' => array( $this, 'permission_callback' ),
26 ),
27 )
28 );
29
30 register_rest_route(
31 self::CATF_ROUTE_PUBLIC_NAMESPACE,
32 '/folders',
33 array(
34 array(
35 'methods' => \WP_REST_Server::READABLE,
36 'callback' => array( $this->folderController, 'get_folders' ),
37 'permission_callback' => array( $this, 'public_permission_callback' ),
38 ),
39 array(
40 'methods' => \WP_REST_Server::CREATABLE,
41 'callback' => array( $this->folderController, 'new_folder' ),
42 'permission_callback' => array( $this, 'public_permission_callback' ),
43 ),
44 array(
45 'methods' => \WP_REST_Server::DELETABLE,
46 'callback' => array( $this->folderController, 'delete_folder' ),
47 'permission_callback' => array( $this, 'public_permission_callback' ),
48 ),
49 )
50 );
51
52 register_rest_route(
53 self::CATF_ROUTE_PUBLIC_NAMESPACE,
54 '/attachment-to-folder',
55 array(
56 array(
57 'methods' => \WP_REST_Server::CREATABLE,
58 'callback' => array( $this->folderController, 'set_attachment_to_folder' ),
59 'permission_callback' => array( $this, 'public_permission_callback' ),
60 ),
61 )
62 );
63 }
64
65 public function generate_api_key() {
66 if( ! current_user_can( 'manage_options' ) ) {
67 return new \WP_Error( 403, __( 'You are not allowed to generate API key.', 'catfolders' ) );
68 }
69 $key = Helpers::generateRandomString( 40 );
70 update_option( 'catf_rest_api_key', $key );
71 return new \WP_REST_Response( $key );
72 }
73
74 public function permission_callback() {
75 return current_user_can( 'upload_files' ) && current_user_can( 'manage_options' );
76 }
77
78 public function public_permission_callback() {
79 $key = get_option( 'catf_rest_api_key', '' );
80 if ( \strlen( $key ) == 40 ) {
81 return $key === $this->getBearerToken();
82 }
83 return false;
84 }
85
86 private function getAuthorizationHeader() {
87 $headers = null;
88 if ( isset( $_SERVER['Authorization'] ) ) {
89 $headers = trim( $_SERVER['Authorization'] );
90 } elseif ( isset( $_SERVER['HTTP_AUTHORIZATION'] ) ) { //Nginx or fast CGI
91 $headers = trim( $_SERVER['HTTP_AUTHORIZATION'] );
92 } elseif ( function_exists( 'apache_request_headers' ) ) {
93 $requestHeaders = apache_request_headers();
94 // Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
95 $requestHeaders = array_combine( array_map( 'ucwords', array_keys( $requestHeaders ) ), array_values( $requestHeaders ) );
96 //print_r($requestHeaders);
97 if ( isset( $requestHeaders['Authorization'] ) ) {
98 $headers = trim( $requestHeaders['Authorization'] );
99 }
100 }
101 return $headers;
102 }
103
104 private function getBearerToken() {
105 // phpcs:disable WordPress.Security.NonceVerification.Recommended
106 $token = null;
107 $headers = $this->getAuthorizationHeader();
108 // HEADER: Get the access token from the header
109 if ( ! empty( $headers ) ) {
110 if ( preg_match( '/Bearer\s(\S+)/', $headers, $matches ) ) {
111 $token = $matches[1];
112 }
113 }
114 if ( is_null( $token ) && isset( $_REQUEST['token'] ) ) {
115 $token = $_REQUEST['token'];
116 }
117 return $token;
118 }
119 }
120