PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 5.3.2
FileBird – WordPress Media Library Folders & File Manager v5.3.2
6.5.8 6.5.7 6.5.5 6.5.4 trunk 4.3.1 5.1.6 5.3 5.3.2 5.4.3 5.4.4 5.4.5 5.5 5.5.3 5.5.5 5.5.8 5.5.8.1 5.6 5.6.1 5.6.2 5.6.3 5.6.4 6.2.3 6.2.5 6.3 All 36 releases
filebird / includes / Controller / Api.php

Api.php in FileBird – WordPress Media Library Folders & File Manager 5.3.2, at includes/Controller/Api.php

261 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FileBird\Controller;
3
4 use FileBird\Model\Folder as FolderModel;
5 use FileBird\Classes\Helpers as Helpers;
6 use FileBird\Classes\Tree;
7
8 defined( 'ABSPATH' ) || exit;
9
10
11 class Api {
12 public function __construct() {
13 add_action( 'rest_api_init', array( $this, 'registerRestFields' ) );
14 }
15
16
17 public function registerRestFields() {
18 register_rest_route(
19 NJFB_REST_URL,
20 'fbv-api',
21 array(
22 'methods' => 'POST',
23 'callback' => array( $this, 'restApi' ),
24 'permission_callback' => array( $this, 'resAdminPermissionsCheck' ),
25 )
26 );
27
28 //GET http://yoursite/wp-json/filebird/public/v1/folders
29 register_rest_route(
30 NJFB_REST_PUBLIC_URL,
31 'folders',
32 array(
33 'methods' => 'GET',
34 'callback' => array( $this, 'publicRestApiGetFolders' ),
35 'permission_callback' => array( $this, 'resPublicPermissionsCheck' ),
36 )
37 );
38
39 //GET http://yoursite/wp-json/filebird/public/v1/folder/?folder_id=
40 register_rest_route(
41 NJFB_REST_PUBLIC_URL,
42 'folder',
43 array(
44 'methods' => 'GET',
45 'callback' => array( $this, 'publicRestApiGetFolderDetail' ),
46 'permission_callback' => array( $this, 'resPublicPermissionsCheck' ),
47 )
48 );
49
50 //POST http://yoursite/wp-json/filebird/public/v1/folder/set-attachment
51 //ids=&folder=
52 register_rest_route(
53 NJFB_REST_PUBLIC_URL,
54 'folder/set-attachment',
55 array(
56 'methods' => 'POST',
57 'callback' => array( $this, 'publicRestApiSetAttachment' ),
58 'permission_callback' => array( $this, 'resPublicPermissionsCheck' ),
59 )
60 );
61
62 //GET http://yoursite/wp-json/filebird/public/v1/attachment-id/?folder_id=
63 register_rest_route(
64 NJFB_REST_PUBLIC_URL,
65 'attachment-id',
66 array(
67 'methods' => 'GET',
68 'callback' => array( $this, 'publicRestApiGetAttachmentIds' ),
69 'permission_callback' => array( $this, 'resPublicPermissionsCheck' ),
70 )
71 );
72
73 //GET http://yoursite/wp-json/filebird/public/v1/attachment-count/?folder_id=
74 register_rest_route(
75 NJFB_REST_PUBLIC_URL,
76 'attachment-count',
77 array(
78 'methods' => 'GET',
79 'callback' => array( $this, 'publicRestApiGetAttachmentCount' ),
80 'permission_callback' => array( $this, 'resPublicPermissionsCheck' ),
81 )
82 );
83
84 //POST http://yoursite/wp-json/filebird/public/v1/folders
85 //parent_id=&name=
86 register_rest_route(
87 NJFB_REST_PUBLIC_URL,
88 'folders',
89 array(
90 'methods' => 'POST',
91 'callback' => array( $this, 'publicRestApiNewFolder' ),
92 'permission_callback' => array( $this, 'resPublicPermissionsCheck' ),
93 )
94 );
95
96 }
97 public function restApi( $request ) {
98 $act = $request->get_param( 'act' );
99 $act = isset( $act ) ? sanitize_text_field( $act ) : '';
100 if ( $act == 'generate-key' ) {
101 $key = $this->generateRandomString( 40 );
102 update_option( 'fbv_rest_api_key', $key );
103 wp_send_json_success( array( 'key' => $key ) );
104 }
105 wp_send_json_error(
106 array(
107 'mess' => __( 'Invalid action', 'filebird' ),
108 )
109 );
110 }
111 public function publicRestApiGetFolders() {
112 $data = array();
113
114 $order_by = null;
115 $data['folders'] = Tree::getFolders( $order_by );
116
117 wp_send_json_success( $data );
118 }
119 public function publicRestApiGetFolderDetail( $request ) {
120 $folder_id = $request->get_param( 'folder_id' );
121 wp_send_json_success(
122 array(
123 'folder' => FolderModel::findById( $folder_id, 'id, name, parent' ),
124 )
125 );
126 }
127 public function publicRestApiGetAttachmentIds( $request ) {
128 $folder_id = $request->get_param( 'folder_id' );
129
130 if ( $folder_id != '' ) {
131 wp_send_json_success(
132 array(
133 'attachment_ids' => Helpers::getAttachmentIdsByFolderId( $folder_id ),
134 )
135 );
136 }
137 wp_send_json_error(
138 array(
139 'mess' => __( 'folder_id is missing.', 'filebird' ),
140 )
141 );
142 }
143 public function publicRestApiGetAttachmentCount( $request ) {
144 $folder_id = $request->get_param( 'folder_id' );
145 $icl_lang = $request->get_param( 'icl_lang' );
146 if ( $folder_id !== '' ) {
147 $count = 0;
148 if ( $folder_id > 0 ) {
149 $count = Helpers::getAttachmentCountByFolderId( $folder_id );
150 } elseif ( $folder_id == -1 ) {
151 $count = is_null( $icl_lang ) ? Tree::getCount( -1 ) : Tree::getCount( -1, $icl_lang );
152 } else {
153 //Uncategorized
154 $total = is_null( $icl_lang ) ? Tree::getCount( -1 ) : Tree::getCount( -1, $icl_lang );
155 $folders = is_null( $icl_lang ) ? Tree::getAllFoldersAndCount() : Tree::getAllFoldersAndCount( $icl_lang );
156 $count_of_folders = 0;
157 foreach ( $folders as $k => $v ) {
158 $count_of_folders += $v;
159 }
160 $count = $total - $count_of_folders;
161 }
162 wp_send_json_success(
163 array(
164 'count' => $count,
165 )
166 );
167 }
168 wp_send_json_error(
169 array(
170 'mess' => __( 'folder_id is missing.', 'filebird' ),
171 )
172 );
173 }
174 public function publicRestApiNewFolder( $request ) {
175 $parent_id = (int) ( $request->get_param( 'parent_id' ) );
176 $name = sanitize_text_field( $request->get_param( 'name' ) );
177
178 if ( $name != '' ) {
179 $id = FolderModel::newOrGet( $name, $parent_id );
180 wp_send_json_success( array( 'id' => $id ) );
181 }
182 wp_send_json_error(
183 array(
184 'mess' => __( 'Required fields are missing.', 'filebird' ),
185 )
186 );
187 }
188 public function publicRestApiSetAttachment( $request ) {
189 $ids = $request->get_param( 'ids' );
190 $folder = $request->get_param( 'folder' );
191
192 $ids = ! empty( $ids ) ? Helpers::sanitize_array( $ids ) : '';
193 $folder = sanitize_text_field( $folder );
194
195 if ( \is_numeric( $ids ) ) {
196 $ids = array( $ids );
197 }
198
199 if ( $ids != '' && is_numeric( $folder ) ) {
200 FolderModel::setFoldersForPosts( $ids, $folder );
201 wp_send_json_success();
202 }
203 wp_send_json_error(
204 array(
205 'mess' => __( 'Validation failed', 'filebird' ),
206 )
207 );
208 }
209 public function resAdminPermissionsCheck() {
210 return current_user_can( 'upload_files' ) && current_user_can( 'manage_options' );
211 }
212 public function resPublicPermissionsCheck() {
213 $key = get_option( 'fbv_rest_api_key', '' );
214 if ( \strlen( $key ) == 40 ) {
215 return $key === $this->getBearerToken();
216 }
217 return false;
218 }
219
220 private function generateRandomString( $length = 10 ) {
221 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
222 $charactersLength = strlen( $characters );
223 $randomString = '';
224 for ( $i = 0; $i < $length; $i++ ) {
225 $randomString .= $characters[ wp_rand( 0, $charactersLength - 1 ) ];
226 }
227 return $randomString;
228 }
229 private function getAuthorizationHeader() {
230 $headers = null;
231 if ( isset( $_SERVER['Authorization'] ) ) {
232 $headers = trim( $_SERVER['Authorization'] );
233 } elseif ( isset( $_SERVER['HTTP_AUTHORIZATION'] ) ) { //Nginx or fast CGI
234 $headers = trim( $_SERVER['HTTP_AUTHORIZATION'] );
235 } elseif ( function_exists( 'apache_request_headers' ) ) {
236 $requestHeaders = apache_request_headers();
237 // 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)
238 $requestHeaders = array_combine( array_map( 'ucwords', array_keys( $requestHeaders ) ), array_values( $requestHeaders ) );
239 //print_r($requestHeaders);
240 if ( isset( $requestHeaders['Authorization'] ) ) {
241 $headers = trim( $requestHeaders['Authorization'] );
242 }
243 }
244 return $headers;
245 }
246 private function getBearerToken() {
247 $token = null;
248 $headers = $this->getAuthorizationHeader();
249 // HEADER: Get the access token from the header
250 if ( ! empty( $headers ) ) {
251 if ( preg_match( '/Bearer\s(\S+)/', $headers, $matches ) ) {
252 $token = $matches[1];
253 }
254 }
255 if ( is_null( $token ) && isset( $_REQUEST['token'] ) ) {
256 $token = $_REQUEST['token'];
257 }
258 return $token;
259 }
260 }
261