PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 5.6
FileBird – WordPress Media Library Folders & File Manager v5.6
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.6, at includes/Controller/Api.php

275 lines 8.1 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
112 private function addFolderCounter( $folders, $counter ) {
113 if ( is_array( $folders ) ) {
114 foreach ( $folders as $k => $v ) {
115 $folders[ $k ]['li_attr']['data-count'] = isset( $counter[ $v['id'] ] ) ? $counter[ $v['id'] ] : 0;
116 if ( isset( $v['children'] ) ) {
117 $folders[ $k ]['children'] = $this->addFolderCounter( $v['children'], $counter );
118 }
119 }
120 }
121 return $folders;
122 }
123
124 public function publicRestApiGetFolders() {
125 $data = array();
126 $data['folders'] = Tree::getFolders( null );
127 $counter = Tree::getAllFoldersAndCount();
128
129 $results['folders'] = $this->addFolderCounter( $data['folders'], $counter );
130
131 wp_send_json_success( $results );
132 }
133 public function publicRestApiGetFolderDetail( $request ) {
134 $folder_id = $request->get_param( 'folder_id' );
135 wp_send_json_success(
136 array(
137 'folder' => FolderModel::findById( $folder_id, 'id, name, parent' ),
138 )
139 );
140 }
141 public function publicRestApiGetAttachmentIds( $request ) {
142 $folder_id = $request->get_param( 'folder_id' );
143
144 if ( $folder_id != '' ) {
145 wp_send_json_success(
146 array(
147 'attachment_ids' => Helpers::getAttachmentIdsByFolderId( $folder_id ),
148 )
149 );
150 }
151 wp_send_json_error(
152 array(
153 'mess' => __( 'folder_id is missing.', 'filebird' ),
154 )
155 );
156 }
157 public function publicRestApiGetAttachmentCount( $request ) {
158 $folder_id = $request->get_param( 'folder_id' );
159 $icl_lang = $request->get_param( 'icl_lang' );
160 if ( $folder_id !== '' ) {
161 $count = 0;
162 if ( $folder_id > 0 ) {
163 $count = Helpers::getAttachmentCountByFolderId( $folder_id );
164 } elseif ( $folder_id == -1 ) {
165 $count = is_null( $icl_lang ) ? Tree::getCount( -1 ) : Tree::getCount( -1, $icl_lang );
166 } else {
167 //Uncategorized
168 $total = is_null( $icl_lang ) ? Tree::getCount( -1 ) : Tree::getCount( -1, $icl_lang );
169 $folders = is_null( $icl_lang ) ? Tree::getAllFoldersAndCount() : Tree::getAllFoldersAndCount( $icl_lang );
170 $count_of_folders = 0;
171 foreach ( $folders as $k => $v ) {
172 $count_of_folders += $v;
173 }
174 $count = $total - $count_of_folders;
175 }
176 wp_send_json_success(
177 array(
178 'count' => $count,
179 )
180 );
181 }
182 wp_send_json_error(
183 array(
184 'mess' => __( 'folder_id is missing.', 'filebird' ),
185 )
186 );
187 }
188 public function publicRestApiNewFolder( $request ) {
189 $parent_id = (int) ( $request->get_param( 'parent_id' ) );
190 $name = sanitize_text_field( $request->get_param( 'name' ) );
191
192 if ( $name != '' ) {
193 $id = FolderModel::newOrGet( $name, $parent_id );
194 wp_send_json_success( array( 'id' => $id ) );
195 }
196 wp_send_json_error(
197 array(
198 'mess' => __( 'Required fields are missing.', 'filebird' ),
199 )
200 );
201 }
202 public function publicRestApiSetAttachment( $request ) {
203 $ids = $request->get_param( 'ids' );
204 $folder = $request->get_param( 'folder' );
205
206 $ids = ! empty( $ids ) ? Helpers::sanitize_array( $ids ) : '';
207 $folder = sanitize_text_field( $folder );
208
209 if ( \is_numeric( $ids ) ) {
210 $ids = array( $ids );
211 }
212
213 if ( $ids != '' && is_numeric( $folder ) ) {
214 FolderModel::setFoldersForPosts( $ids, $folder );
215 wp_send_json_success();
216 }
217 wp_send_json_error(
218 array(
219 'mess' => __( 'Validation failed', 'filebird' ),
220 )
221 );
222 }
223 public function resAdminPermissionsCheck() {
224 return current_user_can( 'upload_files' ) && current_user_can( 'manage_options' );
225 }
226 public function resPublicPermissionsCheck() {
227 $key = get_option( 'fbv_rest_api_key', '' );
228 if ( \strlen( $key ) == 40 ) {
229 return $key === $this->getBearerToken();
230 }
231 return false;
232 }
233
234 private function generateRandomString( $length = 10 ) {
235 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
236 $charactersLength = strlen( $characters );
237 $randomString = '';
238 for ( $i = 0; $i < $length; $i++ ) {
239 $randomString .= $characters[ wp_rand( 0, $charactersLength - 1 ) ];
240 }
241 return $randomString;
242 }
243 private function getAuthorizationHeader() {
244 $headers = null;
245 if ( isset( $_SERVER['Authorization'] ) ) {
246 $headers = trim( $_SERVER['Authorization'] );
247 } elseif ( isset( $_SERVER['HTTP_AUTHORIZATION'] ) ) { //Nginx or fast CGI
248 $headers = trim( $_SERVER['HTTP_AUTHORIZATION'] );
249 } elseif ( function_exists( 'apache_request_headers' ) ) {
250 $requestHeaders = apache_request_headers();
251 // 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)
252 $requestHeaders = array_combine( array_map( 'ucwords', array_keys( $requestHeaders ) ), array_values( $requestHeaders ) );
253 //print_r($requestHeaders);
254 if ( isset( $requestHeaders['Authorization'] ) ) {
255 $headers = trim( $requestHeaders['Authorization'] );
256 }
257 }
258 return $headers;
259 }
260 private function getBearerToken() {
261 $token = null;
262 $headers = $this->getAuthorizationHeader();
263 // HEADER: Get the access token from the header
264 if ( ! empty( $headers ) ) {
265 if ( preg_match( '/Bearer\s(\S+)/', $headers, $matches ) ) {
266 $token = $matches[1];
267 }
268 }
269 if ( is_null( $token ) && isset( $_REQUEST['token'] ) ) {
270 $token = $_REQUEST['token'];
271 }
272 return $token;
273 }
274 }
275