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

205 lines 7.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
6 use FileBird\Controller\Folder as FolderController;
7
8 use FileBird\Classes\Helpers as Helpers;
9 use FileBird\Classes\Tree;
10
11 defined('ABSPATH') || exit;
12
13
14 class Api {
15
16 protected static $instance = null;
17
18 public function __construct() {
19 add_action('rest_api_init', array($this, 'registerRestFields'));
20 }
21 public function registerRestFields() {
22 register_rest_route(NJFB_REST_URL,
23 'fbv-api',
24 array(
25 'methods' => 'POST',
26 'callback' => array($this, 'restApi'),
27 'permission_callback' => array($this, 'resAdminPermissionsCheck'),
28 )
29 );
30
31 //GET http://yoursite/wp-json/filebird/public/v1/folders
32 register_rest_route(NJFB_REST_PUBLIC_URL,
33 'folders',
34 array(
35 'methods' => 'GET',
36 'callback' => array($this, 'publicRestApiGetFolders'),
37 'permission_callback' => array($this, 'resPublicPermissionsCheck'),
38 )
39 );
40
41 //GET http://yoursite/wp-json/filebird/public/v1/folder/?folder_id=
42 register_rest_route(NJFB_REST_PUBLIC_URL,
43 'folder',
44 array(
45 'methods' => 'GET',
46 'callback' => array($this, 'publicRestApiGetFolderDetail'),
47 'permission_callback' => array($this, 'resPublicPermissionsCheck'),
48 )
49 );
50
51 //POST http://yoursite/wp-json/filebird/public/v1/folder/set-attachment
52 //ids=&folder=
53 register_rest_route(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(NJFB_REST_PUBLIC_URL,
64 'attachment-id',
65 array(
66 'methods' => 'GET',
67 'callback' => array($this, 'publicRestApiGetAttachmentIds'),
68 'permission_callback' => array($this, 'resPublicPermissionsCheck'),
69 )
70 );
71
72 //POST http://yoursite/wp-json/filebird/public/v1/folders
73 //parent_id=&name=
74 register_rest_route(NJFB_REST_PUBLIC_URL,
75 'folders',
76 array(
77 'methods' => 'POST',
78 'callback' => array($this, 'publicRestApiNewFolder'),
79 'permission_callback' => array($this, 'resPublicPermissionsCheck'),
80 )
81 );
82
83 }
84 public function restApi() {
85 $act = isset($_POST['act']) ? sanitize_text_field($_POST['act']) : '';
86 if($act == 'generate-key') {
87 $key = $this->generateRandomString(40);
88 update_option('fbv_rest_api_key', $key);
89 wp_send_json_success(array('key' => $key));
90 }
91 wp_send_json_error(array(
92 'mess' => __('Invalid action')
93 ));
94 }
95 public function publicRestApiGetFolders() {
96 $data = array();
97
98 $order_by = null;
99 $data['folders'] = Tree::getFolders($order_by);
100
101 wp_send_json_success($data);
102 }
103 public function publicRestApiGetFolderDetail() {
104 $folder_id = isset($_GET['folder_id']) ? (int)$_GET['folder_id'] : '';
105 wp_send_json_success(array(
106 'folder' => FolderModel::findById($folder_id, 'id, name, parent')
107 ));
108 }
109 public function publicRestApiGetAttachmentIds() {
110 $folder_id = isset($_GET['folder_id']) ? (int)$_GET['folder_id'] : '';
111 if($folder_id != '') {
112 wp_send_json_success(array(
113 'attachment_ids' => Helpers::getAttachmentIdsByFolderId((int)$_GET['folder_id'])
114 ));
115 }
116 wp_send_json_error(array(
117 'mess' => __('folder_id is missing.', 'filebird')
118 ));
119 }
120 public function publicRestApiNewFolder() {
121 $parent_id = isset($_POST['parent_id']) ? (int)sanitize_text_field($_POST['parent_id']) : '';
122 $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
123 if($name != '') {
124 $id = FolderModel::newOrGet($name, $parent_id);
125 wp_send_json_success(array('id' => $id));
126 }
127 wp_send_json_error(array(
128 'mess' => __('Required fields are missing.', 'filebird')
129 ));
130 }
131 public function publicRestApiSetAttachment() {
132 $ids = ((isset($_POST['ids'])) ? Helpers::sanitize_array($_POST['ids']) : '');
133 $folder = ((isset($_POST['folder'])) ? sanitize_text_field($_POST['folder']) : '');
134
135 if(\is_numeric($ids)) $ids = array($ids);
136
137 if($ids != '' && is_numeric($folder)) {
138 FolderModel::setFoldersForPosts($ids, $folder);
139 wp_send_json_success();
140 }
141 wp_send_json_error(array(
142 'mess' => __('Validation failed', 'filebird')
143 ));
144 }
145 public function resAdminPermissionsCheck() {
146 return current_user_can("upload_files");
147 }
148 public function resPublicPermissionsCheck() {
149 $key = get_option('fbv_rest_api_key', '');
150 if(\strlen($key) == 40) {
151 return $key === $this->getBearerToken();
152 }
153 return false;
154 }
155
156 public static function getInstance() {
157 if (null == self::$instance) {
158 self::$instance = new self;
159 }
160 return self::$instance;
161 }
162
163 private function generateRandomString($length = 10) {
164 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
165 $charactersLength = strlen($characters);
166 $randomString = '';
167 for ($i = 0; $i < $length; $i++) {
168 $randomString .= $characters[rand(0, $charactersLength - 1)];
169 }
170 return $randomString;
171 }
172 private function getAuthorizationHeader(){
173 $headers = null;
174 if (isset($_SERVER['Authorization'])) {
175 $headers = trim($_SERVER["Authorization"]);
176 }
177 else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
178 $headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
179 } elseif (function_exists('apache_request_headers')) {
180 $requestHeaders = apache_request_headers();
181 // 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)
182 $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
183 //print_r($requestHeaders);
184 if (isset($requestHeaders['Authorization'])) {
185 $headers = trim($requestHeaders['Authorization']);
186 }
187 }
188 return $headers;
189 }
190 private function getBearerToken() {
191 $token = null;
192 $headers = $this->getAuthorizationHeader();
193 // HEADER: Get the access token from the header
194 if (!empty($headers)) {
195 if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
196 $token = $matches[1];
197 }
198 }
199 if(is_null($token) && isset($_REQUEST['token'])) {
200 $token = $_REQUEST['token'];
201 }
202 return $token;
203 }
204 }
205