PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.3
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.3
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataAccess / API / WPDA_Apps.php

WPDA_Apps.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.3, at WPDataAccess/API/WPDA_Apps.php

1,295 lines 50.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\API;
4
5 use stdClass;
6 use WPDataAccess\Connection\WPDADB;
7 use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache;
8 use WPDataAccess\Plugin_Table_Models\WPDA_App_Container_Model;
9 use WPDataAccess\Plugin_Table_Models\WPDA_App_Model;
10 use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model;
11 use WPDataAccess\WPDA;
12 class WPDA_Apps extends WPDA_API_Core {
13 public function register_rest_routes() {
14 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/init', array(
15 'methods' => array('POST'),
16 'callback' => array($this, 'app_init'),
17 'permission_callback' => '__return_true',
18 ) );
19 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/list', array(
20 'methods' => array('POST'),
21 'callback' => array($this, 'app_list'),
22 'permission_callback' => '__return_true',
23 ) );
24 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/meta', array(
25 'methods' => array('POST'),
26 'callback' => array($this, 'app_meta'),
27 'permission_callback' => '__return_true',
28 'args' => array(
29 'app_id' => $this->get_param( 'app_id' ),
30 ),
31 ) );
32 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/table/meta', array(
33 'methods' => array('POST'),
34 'callback' => array($this, 'app_table_meta'),
35 'permission_callback' => '__return_true',
36 'args' => array(
37 'dbs' => $this->get_param( 'dbs' ),
38 'tbl' => $this->get_param( 'tbl' ),
39 'waa' => array(
40 'required' => false,
41 'type' => 'boolean',
42 'description' => __( 'With admin actions (to support table exports)', 'wp-data-access' ),
43 ),
44 ),
45 ) );
46 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/create', array(
47 'methods' => array('POST'),
48 'callback' => array($this, 'app_create'),
49 'permission_callback' => '__return_true',
50 'args' => array(
51 'app_name' => $this->get_param( 'app_name' ),
52 'app_title' => $this->get_param( 'app_title' ),
53 'app_type' => $this->get_param( 'app_type' ),
54 'app_settings' => $this->get_param( 'app_settings' ),
55 'app_dbs' => $this->get_param( 'dbs' ),
56 'app_tbl' => $this->get_param( 'tbl' ),
57 'app_cls' => $this->get_param( 'app_cls' ),
58 'app_table' => array(
59 'required' => true,
60 'type' => 'string',
61 'description' => __( 'Table settings', 'wp-data-access' ),
62 'sanitize_callback' => 'sanitize_text_field',
63 'validate_callback' => 'rest_validate_request_arg',
64 ),
65 ),
66 ) );
67 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/details', array(
68 'methods' => array('POST'),
69 'callback' => array($this, 'app_details'),
70 'permission_callback' => '__return_true',
71 'args' => array(
72 'app_id' => $this->get_param( 'app_id' ),
73 ),
74 ) );
75 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/detailmeta', array(
76 'methods' => array('POST'),
77 'callback' => array($this, 'app_detail_meta'),
78 'permission_callback' => '__return_true',
79 'args' => array(
80 'app_id' => $this->get_param( 'app_id' ),
81 'cnt_id' => $this->get_param( 'cnt_id' ),
82 ),
83 ) );
84 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/relationship/create', array(
85 'methods' => array('POST'),
86 'callback' => array($this, 'app_relationship_create'),
87 'permission_callback' => '__return_true',
88 'args' => array(
89 'app_id' => $this->get_param( 'app_id' ),
90 'app_title' => $this->get_param( 'app_title' ),
91 'app_dbs' => $this->get_param( 'dbs' ),
92 'app_tbl' => $this->get_param( 'tbl' ),
93 'app_cls' => $this->get_param( 'app_cls' ),
94 'app_relation' => array(
95 'required' => true,
96 'type' => 'string',
97 'description' => __( 'Table settings', 'wp-data-access' ),
98 'sanitize_callback' => 'sanitize_text_field',
99 'validate_callback' => 'rest_validate_request_arg',
100 ),
101 ),
102 ) );
103 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/relationship/update', array(
104 'methods' => array('POST'),
105 'callback' => array($this, 'app_relationship_update'),
106 'permission_callback' => '__return_true',
107 'args' => array(
108 'app_id' => $this->get_param( 'app_id' ),
109 'app_cnt' => $this->get_param( 'cnt_id' ),
110 'app_title' => $this->get_param( 'app_title' ),
111 'app_dbs' => $this->get_param( 'dbs' ),
112 'app_tbl' => $this->get_param( 'tbl' ),
113 'app_cls' => $this->get_param( 'app_cls' ),
114 'app_relation' => array(
115 'required' => true,
116 'type' => 'string',
117 'description' => __( 'Table settings', 'wp-data-access' ),
118 'sanitize_callback' => 'sanitize_text_field',
119 'validate_callback' => 'rest_validate_request_arg',
120 ),
121 ),
122 ) );
123 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/relationship/delete', array(
124 'methods' => array('POST'),
125 'callback' => array($this, 'app_relationship_delete'),
126 'permission_callback' => '__return_true',
127 'args' => array(
128 'cnt_id' => $this->get_param( 'cnt_id' ),
129 ),
130 ) );
131 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/save', array(
132 'methods' => array('POST'),
133 'callback' => array($this, 'app_save'),
134 'permission_callback' => '__return_true',
135 'args' => array(
136 'app_id' => $this->get_param( 'app_id' ),
137 'app_name' => $this->get_param( 'app_name' ),
138 'app_title' => $this->get_param( 'app_title' ),
139 'app_type' => $this->get_param( 'app_type' ),
140 'app_settings' => $this->get_param( 'app_settings' ),
141 'app_add_to_menu' => array(
142 'required' => true,
143 'type' => 'integer',
144 'description' => __( 'Add app to dashboard menu', 'wp-data-access' ),
145 'sanitize_callback' => 'absint',
146 'validate_callback' => 'rest_validate_request_arg',
147 ),
148 'app_dbs' => $this->get_param( 'dbs' ),
149 'app_tbl' => $this->get_param( 'tbl' ),
150 'app_cls' => $this->get_param( 'app_cls' ),
151 ),
152 ) );
153 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/remove', array(
154 'methods' => array('POST'),
155 'callback' => array($this, 'app_remove'),
156 'permission_callback' => '__return_true',
157 'args' => array(
158 'app_id' => $this->get_param( 'app_id' ),
159 ),
160 ) );
161 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/settings', array(
162 'methods' => array('POST'),
163 'callback' => array($this, 'app_settings'),
164 'permission_callback' => '__return_true',
165 'args' => array(
166 'app_id' => $this->get_param( 'app_id' ),
167 'cnt_id' => $this->get_param( 'cnt_id' ),
168 'target' => array(
169 'required' => true,
170 'type' => 'string',
171 'description' => __( 'Setting target', 'wp-data-access' ),
172 'sanitize_callback' => 'sanitize_text_field',
173 'validate_callback' => 'rest_validate_request_arg',
174 ),
175 'settings' => array(
176 'required' => false,
177 'type' => 'string',
178 'description' => __( 'Settings - JSON string', 'wp-data-access' ),
179 'sanitize_callback' => 'sanitize_text_field',
180 'validate_callback' => 'rest_validate_request_arg',
181 ),
182 'theme' => array(
183 'required' => false,
184 'type' => 'string',
185 'description' => __( 'Theme settings - JSON string', 'wp-data-access' ),
186 'sanitize_callback' => 'sanitize_text_field',
187 'validate_callback' => 'rest_validate_request_arg',
188 ),
189 ),
190 ) );
191 // DML
192 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/select', array(
193 'methods' => array('POST'),
194 'callback' => array($this, 'app_select'),
195 'permission_callback' => '__return_true',
196 'args' => array(
197 'app_id' => $this->get_param( 'app_id' ),
198 'cnt_id' => $this->get_param( 'cnt_id' ),
199 'col' => $this->get_param( 'cols' ),
200 'page_index' => $this->get_param( 'page_index' ),
201 'page_size' => $this->get_param( 'page_size' ),
202 'search' => $this->get_param( 'search' ),
203 'search_columns' => $this->get_param( 'search_columns' ),
204 'search_column_fns' => $this->get_param( 'search_column_fns' ),
205 'search_column_lov' => $this->get_param( 'search_column_lov' ),
206 'md' => array(
207 'required' => false,
208 'type' => 'mixed',
209 'description' => __( 'Master detail join conditions', 'wp-data-access' ),
210 'sanitize_callback' => function ( $param ) {
211 $columns = array();
212 foreach ( rest_sanitize_object( $param ) as $column_name => $value ) {
213 $columns[$this->sanitize_db_identifier( $column_name )] = sanitize_text_field( wp_unslash( $value ) );
214 }
215 return $columns;
216 },
217 'validate_callback' => function ( $param ) {
218 return is_array( $param );
219 },
220 ),
221 'sorting' => $this->get_param( 'sorting' ),
222 'row_count' => $this->get_param( 'row_count' ),
223 'row_count_estimate' => $this->get_param( 'row_count_estimate' ),
224 'media' => $this->get_param( 'media' ),
225 ),
226 ) );
227 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/get', array(
228 'methods' => array('POST'),
229 'callback' => array($this, 'app_get'),
230 'permission_callback' => '__return_true',
231 'args' => array(
232 'app_id' => $this->get_param( 'app_id' ),
233 'cnt_id' => $this->get_param( 'cnt_id' ),
234 'key' => $this->get_param( 'key' ),
235 'media' => $this->get_param( 'media' ),
236 ),
237 ) );
238 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/insert', array(
239 'methods' => array('POST'),
240 'callback' => array($this, 'app_insert'),
241 'permission_callback' => '__return_true',
242 'args' => array(
243 'app_id' => $this->get_param( 'app_id' ),
244 'cnt_id' => $this->get_param( 'cnt_id' ),
245 'val' => $this->get_param( 'val' ),
246 ),
247 ) );
248 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/update', array(
249 'methods' => array('POST'),
250 'callback' => array($this, 'app_update'),
251 'permission_callback' => '__return_true',
252 'args' => array(
253 'app_id' => $this->get_param( 'app_id' ),
254 'cnt_id' => $this->get_param( 'cnt_id' ),
255 'key' => $this->get_param( 'key' ),
256 'val' => $this->get_param( 'val' ),
257 ),
258 ) );
259 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/delete', array(
260 'methods' => array('POST'),
261 'callback' => array($this, 'app_delete'),
262 'permission_callback' => '__return_true',
263 'args' => array(
264 'app_id' => $this->get_param( 'app_id' ),
265 'cnt_id' => $this->get_param( 'cnt_id' ),
266 'key' => $this->get_param( 'key' ),
267 ),
268 ) );
269 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lov', array(
270 'methods' => array('POST'),
271 'callback' => array($this, 'app_lov'),
272 'permission_callback' => '__return_true',
273 'args' => array(
274 'app_id' => $this->get_param( 'app_id' ),
275 'cnt_id' => $this->get_param( 'cnt_id' ),
276 'col' => $this->get_param( 'col' ),
277 'search' => $this->get_param( 'search' ),
278 'search_columns' => $this->get_param( 'search_columns' ),
279 'search_column_fns' => $this->get_param( 'search_column_fns' ),
280 'search_column_lov' => $this->get_param( 'search_column_lov' ),
281 ),
282 ) );
283 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lookup', array(
284 'methods' => array('POST'),
285 'callback' => array($this, 'app_lookup'),
286 'permission_callback' => '__return_true',
287 'args' => array(
288 'app_id' => $this->get_param( 'app_id' ),
289 'cnt_id' => $this->get_param( 'cnt_id' ),
290 'target' => array(
291 'required' => true,
292 'type' => 'string',
293 'description' => __( 'Target: table or form', 'wp-data-access' ),
294 'sanitize_callback' => 'sanitize_text_field',
295 'validate_callback' => 'rest_validate_request_arg',
296 ),
297 'col' => $this->get_param( 'col' ),
298 'colk' => $this->get_param( 'col' ),
299 'colv' => $this->get_param( 'col' ),
300 'cold' => $this->get_param( 'key' ),
301 ),
302 ) );
303 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lookup/dbs', array(
304 'methods' => array('POST'),
305 'callback' => array($this, 'app_lookup_dbs'),
306 'permission_callback' => '__return_true',
307 'args' => array(
308 'app_id' => $this->get_param( 'app_id' ),
309 'cnt_id' => $this->get_param( 'cnt_id' ),
310 ),
311 ) );
312 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lookup/tbl', array(
313 'methods' => array('POST'),
314 'callback' => array($this, 'app_lookup_tbl'),
315 'permission_callback' => '__return_true',
316 'args' => array(
317 'app_id' => $this->get_param( 'app_id' ),
318 'cnt_id' => $this->get_param( 'cnt_id' ),
319 'dbs' => $this->get_param( 'dbs' ),
320 ),
321 ) );
322 register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lookup/cls', array(
323 'methods' => array('POST'),
324 'callback' => array($this, 'app_lookup_cls'),
325 'permission_callback' => '__return_true',
326 'args' => array(
327 'app_id' => $this->get_param( 'app_id' ),
328 'cnt_id' => $this->get_param( 'cnt_id' ),
329 'dbs' => $this->get_param( 'dbs' ),
330 'tbl' => $this->get_param( 'tbl' ),
331 ),
332 ) );
333 }
334
335 private function get_app_columns( $columns ) {
336 if ( !is_array( $columns ) ) {
337 return false;
338 }
339 return array_map( function ( $value ) {
340 return $value['columnName'];
341 }, array_filter( $columns, function ( $column ) {
342 return $column['isSelected'];
343 } ) );
344 }
345
346 private function get_app_table_columns( $settings, $table_settings ) {
347 if ( !isset( $settings['columns'] ) ) {
348 return false;
349 }
350 $columns = $this->get_app_columns( $settings['columns'] );
351 if ( false === $columns ) {
352 return false;
353 }
354 $columns_available = array_flip( $columns );
355 if ( !isset( $settings['table'] ) ) {
356 return array_map( function () {
357 return true;
358 }, $columns_available );
359 }
360 if ( !is_array( $table_settings ) || !isset( $table_settings['columns'] ) ) {
361 return false;
362 }
363 $table_columns = $table_settings['columns'];
364 for ($i = 0; $i < count( $columns ); $i++) {
365 if ( isset( $columns_available[$columns[$i]] ) ) {
366 $column_name = $columns[$i];
367 $columns_available[$columns[$i]] = count( array_filter( $table_columns, function ( $column ) use($columns, $column_name) {
368 if ( !isset( $column['columnName'], $column['queryable'] ) ) {
369 return false;
370 }
371 $queryable = $column['queryable'];
372 return $column_name === $column['columnName'] && true === $queryable;
373 } ) ) > 0;
374 }
375 }
376 return $columns_available;
377 }
378
379 private function get_app_form_columns( $settings ) {
380 if ( !isset( $settings['columns'] ) ) {
381 return false;
382 }
383 return $this->get_app_columns( $settings['columns'] );
384 }
385
386 public function app_details( $request ) {
387 if ( !$this->current_user_can_access() ) {
388 return $this->unauthorized();
389 }
390 if ( !$this->current_user_token_valid( $request ) ) {
391 return $this->invalid_nonce();
392 }
393 $app_id = $request->get_param( 'app_id' );
394 return $this->WPDA_Rest_Response( '', WPDA_App_Container_Model::select( $app_id, 1 ) );
395 }
396
397 public function app_detail_meta( $request ) {
398 if ( !$this->current_user_can_access() ) {
399 return $this->unauthorized();
400 }
401 if ( !$this->current_user_token_valid( $request ) ) {
402 return $this->invalid_nonce();
403 }
404 $app_id = $request->get_param( 'app_id' );
405 $cnt_id = $request->get_param( 'cnt_id' );
406 $container = WPDA_App_Container_Model::get_container( $cnt_id );
407 if ( !isset( $container[0] ) ) {
408 return $this->bad_request();
409 }
410 return $this->get_app_container_meta( $app_id, $container );
411 }
412
413 public function app_relationship_create( $request ) {
414 if ( !$this->current_user_can_access() ) {
415 return $this->unauthorized();
416 }
417 if ( !$this->current_user_token_valid( $request ) ) {
418 return $this->invalid_nonce();
419 }
420 $app_id = $request->get_param( 'app_id' );
421 $app_title = $request->get_param( 'app_title' );
422 $app_dbs = $request->get_param( 'app_dbs' );
423 $app_tbl = $request->get_param( 'app_tbl' );
424 $app_cls = $request->get_param( 'app_cls' );
425 $app_relation = $request->get_param( 'app_relation' );
426 return $this->WPDA_Rest_Response( '', WPDA_App_Container_Model::create(
427 $app_id,
428 $app_dbs,
429 $app_tbl,
430 json_encode( $app_cls ),
431 $app_title,
432 1,
433 null,
434 $app_relation
435 ) );
436 }
437
438 public function app_relationship_update( $request ) {
439 if ( !$this->current_user_can_access() ) {
440 return $this->unauthorized();
441 }
442 if ( !$this->current_user_token_valid( $request ) ) {
443 return $this->invalid_nonce();
444 }
445 $app_id = $request->get_param( 'app_id' );
446 $app_cnt = $request->get_param( 'app_cnt' );
447 $app_title = $request->get_param( 'app_title' );
448 $app_dbs = $request->get_param( 'app_dbs' );
449 $app_tbl = $request->get_param( 'app_tbl' );
450 $app_cls = $request->get_param( 'app_cls' );
451 $app_relation = $request->get_param( 'app_relation' );
452 return $this->WPDA_Rest_Response( '', WPDA_App_Container_Model::update(
453 $app_id,
454 $app_cnt,
455 $app_dbs,
456 $app_tbl,
457 json_encode( $app_cls ),
458 $app_title,
459 1,
460 null,
461 $app_relation
462 ) );
463 }
464
465 public function app_relationship_delete( $request ) {
466 if ( !$this->current_user_can_access() ) {
467 return $this->unauthorized();
468 }
469 if ( !$this->current_user_token_valid( $request ) ) {
470 return $this->invalid_nonce();
471 }
472 $cnt_id = $request->get_param( 'cnt_id' );
473 return $this->WPDA_Rest_Response( '', WPDA_App_Container_Model::delete_container( $cnt_id ) );
474 }
475
476 public function app_select( $request ) {
477 $app_id = $request->get_param( 'app_id' );
478 $cnt_id = $request->get_param( 'cnt_id' );
479 $col = $request->get_param( 'col' );
480 $page_index = $request->get_param( 'page_index' );
481 $page_size = $request->get_param( 'page_size' );
482 $search = $request->get_param( 'search' );
483 $search_columns = $request->get_param( 'search_columns' );
484 $search_column_fns = $request->get_param( 'search_column_fns' );
485 $search_column_lov = $request->get_param( 'search_column_lov' );
486 $md = $request->get_param( 'md' );
487 $sorting = $request->get_param( 'sorting' );
488 $row_count = $request->get_param( 'row_count' );
489 $row_count_estimate = $request->get_param( 'row_count_estimate' );
490 $media = $request->get_param( 'media' );
491 $default_where = '';
492 $default_orderby = '';
493 $lookups = array();
494 if ( $this->check_app_access(
495 $app_id,
496 $cnt_id,
497 'select',
498 $dbs,
499 $tbl,
500 $msg,
501 $settings
502 ) ) {
503 $table_settings = $settings['table'] ?? array();
504 // Get default where clause
505 if ( isset( $table_settings['table']['defaultWhere'] ) ) {
506 $default_where = $table_settings['table']['defaultWhere'];
507 }
508 // Get default order by
509 if ( isset( $table_settings['table']['defaultOrderBy'] ) ) {
510 $default_orderby_db = $table_settings['table']['defaultOrderBy'];
511 if ( is_array( $default_orderby_db ) ) {
512 foreach ( $default_orderby_db as $orderby ) {
513 if ( isset( $orderby['columnName'], $orderby['order'] ) && '' !== trim( $orderby['columnName'] ) ) {
514 $default_orderby .= (( '' === $default_orderby ? 'order by ' : ',' )) . '`' . WPDA::remove_backticks( $orderby['columnName'] ) . '` ' . (( 'desc' === $orderby['order'] ? 'desc' : 'asc' ));
515 }
516 }
517 }
518 }
519 $table_api = new WPDA_Table();
520 return $table_api->select(
521 $dbs,
522 $tbl,
523 $col,
524 $page_index,
525 $page_size,
526 $search,
527 $search_columns,
528 $search_column_fns,
529 $sorting,
530 $row_count,
531 $row_count_estimate,
532 $media,
533 $default_where,
534 $default_orderby,
535 $lookups,
536 $md
537 );
538 } else {
539 if ( 'rest_cookie_invalid_nonce' === $msg ) {
540 return $this->invalid_nonce();
541 } else {
542 return new \WP_Error('error', $msg, array(
543 'status' => 401,
544 ));
545 }
546 }
547 }
548
549 private function get_lookup_lov( $column_lookup, $search_value, $search_type ) {
550 return null;
551 }
552
553 public function app_get( $request ) {
554 $app_id = $request->get_param( 'app_id' );
555 $cnt_id = $request->get_param( 'cnt_id' );
556 $key = $request->get_param( 'key' );
557 $media = $request->get_param( 'media' );
558 if ( $this->check_app_access(
559 $app_id,
560 $cnt_id,
561 'select',
562 $dbs,
563 $tbl,
564 $msg,
565 $settings
566 ) ) {
567 $column_names = $this->get_app_form_columns( $settings );
568 if ( false === $column_names ) {
569 return $this->invalid_app_settings();
570 }
571 $default_where = '';
572 $table_api = new WPDA_Table();
573 return $table_api->get(
574 $dbs,
575 $tbl,
576 $key,
577 $media,
578 $column_names,
579 $default_where
580 );
581 } else {
582 if ( 'rest_cookie_invalid_nonce' === $msg ) {
583 return $this->invalid_nonce();
584 } else {
585 return new \WP_Error('error', $msg, array(
586 'status' => 401,
587 ));
588 }
589 }
590 }
591
592 public function app_insert( $request ) {
593 $app_id = $request->get_param( 'app_id' );
594 $cnt_id = $request->get_param( 'cnt_id' );
595 $val = $request->get_param( 'val' );
596 if ( $this->check_app_access(
597 $app_id,
598 $cnt_id,
599 'insert',
600 $dbs,
601 $tbl,
602 $msg
603 ) ) {
604 $table_api = new WPDA_Table();
605 return $table_api->insert( $dbs, $tbl, $val );
606 } else {
607 if ( 'rest_cookie_invalid_nonce' === $msg ) {
608 return $this->invalid_nonce();
609 } else {
610 return new \WP_Error('error', $msg, array(
611 'status' => 401,
612 ));
613 }
614 }
615 }
616
617 public function app_update( $request ) {
618 $app_id = $request->get_param( 'app_id' );
619 $cnt_id = $request->get_param( 'cnt_id' );
620 $key = $request->get_param( 'key' );
621 $val = $request->get_param( 'val' );
622 if ( $this->check_app_access(
623 $app_id,
624 $cnt_id,
625 'update',
626 $dbs,
627 $tbl,
628 $msg
629 ) ) {
630 $table_api = new WPDA_Table();
631 return $table_api->update(
632 $dbs,
633 $tbl,
634 $key,
635 $val
636 );
637 } else {
638 if ( 'rest_cookie_invalid_nonce' === $msg ) {
639 return $this->invalid_nonce();
640 } else {
641 return new \WP_Error('error', $msg, array(
642 'status' => 401,
643 ));
644 }
645 }
646 }
647
648 public function app_delete( $request ) {
649 $app_id = $request->get_param( 'app_id' );
650 $cnt_id = $request->get_param( 'cnt_id' );
651 $key = $request->get_param( 'key' );
652 if ( $this->check_app_access(
653 $app_id,
654 $cnt_id,
655 'delete',
656 $dbs,
657 $tbl,
658 $msg
659 ) ) {
660 $table_api = new WPDA_Table();
661 return $table_api->delete( $dbs, $tbl, $key );
662 } else {
663 if ( 'rest_cookie_invalid_nonce' === $msg ) {
664 return $this->invalid_nonce();
665 } else {
666 return new \WP_Error('error', $msg, array(
667 'status' => 401,
668 ));
669 }
670 }
671 }
672
673 public function app_lov( $request ) {
674 return $this->bad_request();
675 }
676
677 public function app_lookup( $request ) {
678 $app_id = $request->get_param( 'app_id' );
679 $cnt_id = $request->get_param( 'cnt_id' );
680 $target = $request->get_param( 'target' );
681 $col = $request->get_param( 'col' );
682 $colk = $request->get_param( 'colk' );
683 $colv = $request->get_param( 'colv' );
684 $cold = $request->get_param( 'cold' );
685 if ( $this->check_app_access(
686 $app_id,
687 $cnt_id,
688 'select',
689 $dbs,
690 $tbl,
691 $msg
692 ) ) {
693 $container = WPDA_App_Container_Model::get_container( $cnt_id );
694 if ( !isset( $container[0] ) ) {
695 return $this->bad_request();
696 }
697 $default_where = '';
698 $lookup = array();
699 $lookup_dbs = "";
700 $lookup_tbl = "";
701 if ( 'form' === $target ) {
702 // Handle form lookup
703 if ( isset( $container[0]['cnt_form'] ) ) {
704 $lookup = json_decode( (string) $container[0]['cnt_form'], true );
705 }
706 } else {
707 // Handle table lookup
708 if ( isset( $container[0]['cnt_table'] ) ) {
709 $lookup = json_decode( (string) $container[0]['cnt_table'], true );
710 }
711 }
712 if ( isset( $lookup['columns'] ) && is_array( $lookup['columns'] ) ) {
713 foreach ( $lookup['columns'] as $column ) {
714 if ( $col === $column['columnName'] ) {
715 if ( !isset( $column['lookup'] ) ) {
716 return $this->WPDA_Rest_Response( '', [] );
717 }
718 $lookup_dbs = $column['lookup']['dbs'];
719 $lookup_tbl = $column['lookup']['tbl'];
720 if ( isset( $column['columnName'], $column['lookup']['defaultWhere'] ) ) {
721 $default_where = $column['lookup']['defaultWhere'];
722 }
723 }
724 }
725 }
726 if ( $lookup_dbs === null || $lookup_dbs === "" || $lookup_tbl === null || $lookup_tbl === "" ) {
727 return $this->bad_request();
728 }
729 $table_api = new WPDA_Table();
730 return $table_api->lookup(
731 $lookup_dbs,
732 $lookup_tbl,
733 $colk,
734 $colv,
735 $cold,
736 $default_where
737 );
738 } else {
739 if ( 'rest_cookie_invalid_nonce' === $msg ) {
740 return $this->invalid_nonce();
741 } else {
742 return new \WP_Error('error', $msg, array(
743 'status' => 401,
744 ));
745 }
746 }
747 }
748
749 public function app_lookup_dbs( $request ) {
750 $app_id = $request->get_param( 'app_id' );
751 $cnt_id = $request->get_param( 'cnt_id' );
752 if ( $this->check_app_access(
753 $app_id,
754 $cnt_id,
755 'select',
756 $_dbs,
757 $_tbl,
758 $msg
759 ) ) {
760 $tree_api = new WPDA_Tree();
761 return $tree_api->get_dbs();
762 } else {
763 if ( 'rest_cookie_invalid_nonce' === $msg ) {
764 return $this->invalid_nonce();
765 } else {
766 return new \WP_Error('error', $msg, array(
767 'status' => 401,
768 ));
769 }
770 }
771 }
772
773 public function app_lookup_tbl( $request ) {
774 $app_id = $request->get_param( 'app_id' );
775 $cnt_id = $request->get_param( 'cnt_id' );
776 $dbs = $request->get_param( 'dbs' );
777 if ( $this->check_app_access(
778 $app_id,
779 $cnt_id,
780 'select',
781 $_dbs,
782 $_tbl,
783 $msg
784 ) ) {
785 $tree_api = new WPDA_Tree();
786 return $tree_api->get_tbl_vws( $dbs );
787 } else {
788 if ( 'rest_cookie_invalid_nonce' === $msg ) {
789 return $this->invalid_nonce();
790 } else {
791 return new \WP_Error('error', $msg, array(
792 'status' => 401,
793 ));
794 }
795 }
796 }
797
798 public function app_lookup_cls( $request ) {
799 $app_id = $request->get_param( 'app_id' );
800 $cnt_id = $request->get_param( 'cnt_id' );
801 $dbs = $request->get_param( 'dbs' );
802 $tbl = $request->get_param( 'tbl' );
803 if ( $this->check_app_access(
804 $app_id,
805 $cnt_id,
806 'select',
807 $_dbs,
808 $_tbl,
809 $msg
810 ) ) {
811 $tree_api = new WPDA_Tree();
812 return $tree_api->get_cls( $dbs, $tbl );
813 } else {
814 if ( 'rest_cookie_invalid_nonce' === $msg ) {
815 return $this->invalid_nonce();
816 } else {
817 return new \WP_Error('error', $msg, array(
818 'status' => 401,
819 ));
820 }
821 }
822 }
823
824 public function app_meta( $request ) {
825 $app_id = $request->get_param( 'app_id' );
826 if ( !$this->main_app_access( $app_id, $msg ) ) {
827 if ( 'rest_cookie_invalid_nonce' === $msg ) {
828 return $this->invalid_nonce();
829 }
830 return $this->unauthorized();
831 }
832 if ( !$this->current_user_token_valid( $request ) ) {
833 return $this->invalid_nonce();
834 }
835 return $this->get_app_meta( $app_id );
836 }
837
838 public function app_settings( $request ) {
839 if ( !$this->current_user_can_access() ) {
840 return $this->unauthorized();
841 }
842 if ( !$this->current_user_token_valid( $request ) ) {
843 return $this->invalid_nonce();
844 }
845 $app_id = $request->get_param( 'app_id' );
846 $cnt_id = $request->get_param( 'cnt_id' );
847 $target = $request->get_param( 'target' );
848 $settings = $request->get_param( 'settings' );
849 $theme = $request->get_param( 'theme' );
850 return $this->do_app_settings(
851 $app_id,
852 $cnt_id,
853 $target,
854 $settings,
855 $theme
856 );
857 }
858
859 public function app_init( $request ) {
860 if ( !$this->current_user_can_access() ) {
861 return $this->unauthorized();
862 }
863 if ( !$this->current_user_token_valid( $request ) ) {
864 return $this->invalid_nonce();
865 }
866 return $this->get_app_init();
867 }
868
869 public function app_list( $request ) {
870 if ( !$this->current_user_can_access() ) {
871 return $this->unauthorized();
872 }
873 if ( !$this->current_user_token_valid( $request ) ) {
874 return $this->invalid_nonce();
875 }
876 return $this->get_app_list();
877 }
878
879 public function app_table_meta( $request ) {
880 $dbs = $request->get_param( 'dbs' );
881 $tbl = $request->get_param( 'tbl' );
882 $waa = $request->get_param( 'waa' );
883 if ( !$this->current_user_can_access() ) {
884 return $this->unauthorized();
885 }
886 if ( !$this->current_user_token_valid( $request ) ) {
887 return $this->invalid_nonce();
888 }
889 $table_api = new WPDA_Table();
890 return $this->WPDA_Rest_Response( '', $table_api->get_table_meta_data( $dbs, $tbl, $waa ) );
891 }
892
893 public function app_create( $request ) {
894 if ( !$this->current_user_can_access() ) {
895 return $this->unauthorized();
896 }
897 if ( !$this->current_user_token_valid( $request ) ) {
898 return $this->invalid_nonce();
899 }
900 // App details
901 $app_name = $request->get_param( 'app_name' );
902 $app_title = $request->get_param( 'app_title' );
903 $app_type = $request->get_param( 'app_type' );
904 $app_settings = $request->get_param( 'app_settings' );
905 // App container
906 $app_dbs = $request->get_param( 'app_dbs' );
907 $app_tbl = $request->get_param( 'app_tbl' );
908 $app_cls = $request->get_param( 'app_cls' );
909 $app_table = $request->get_param( 'app_table' );
910 return $this->do_app_create(
911 $app_name,
912 $app_title,
913 $app_type,
914 $app_settings,
915 $app_dbs,
916 $app_tbl,
917 $app_cls,
918 $app_table
919 );
920 }
921
922 public function app_remove( $request ) {
923 if ( !$this->current_user_can_access() ) {
924 return $this->unauthorized();
925 }
926 if ( !$this->current_user_token_valid( $request ) ) {
927 return $this->invalid_nonce();
928 }
929 $app_id = $request->get_param( 'app_id' );
930 return $this->do_app_remove( $app_id );
931 }
932
933 public function app_save( $request ) {
934 if ( !$this->current_user_can_access() ) {
935 return $this->unauthorized();
936 }
937 if ( !$this->current_user_token_valid( $request ) ) {
938 return $this->invalid_nonce();
939 }
940 // App details
941 $app_id = $request->get_param( 'app_id' );
942 $app_name = $request->get_param( 'app_name' );
943 $app_title = $request->get_param( 'app_title' );
944 $app_type = $request->get_param( 'app_type' );
945 $app_settings = $request->get_param( 'app_settings' );
946 $app_add_to_menu = $request->get_param( 'app_add_to_menu' );
947 // App container
948 $app_dbs = $request->get_param( 'app_dbs' );
949 $app_tbl = $request->get_param( 'app_tbl' );
950 $app_cls = $request->get_param( 'app_cls' );
951 return $this->do_app_save(
952 $app_id,
953 $app_name,
954 $app_title,
955 $app_type,
956 $app_settings,
957 $app_add_to_menu,
958 $app_dbs,
959 $app_tbl,
960 $app_cls
961 );
962 }
963
964 private function do_app_settings(
965 $app_id,
966 $cnt_id,
967 $target,
968 $settings,
969 $theme
970 ) {
971 if ( 1 > $app_id || 1 > $cnt_id || 'table' !== $target && 'form' !== $target && 'theme' !== $target ) {
972 return $this->bad_request();
973 }
974 if ( null === $settings || '' === $settings ) {
975 // Perform reset
976 switch ( $target ) {
977 case 'table':
978 $error_msg = WPDA_App_Container_Model::update_table_settings( $cnt_id, null );
979 if ( '' !== $error_msg ) {
980 return new \WP_Error('error', $error_msg, array(
981 'status' => 403,
982 ));
983 }
984 break;
985 case 'form':
986 $error_msg = WPDA_App_Container_Model::update_form_settings( $cnt_id, null );
987 if ( '' !== $error_msg ) {
988 return new \WP_Error('error', $error_msg, array(
989 'status' => 403,
990 ));
991 }
992 break;
993 case 'theme':
994 $error_msg = WPDA_App_Model::update_theme( $app_id, null );
995 if ( '' !== $error_msg ) {
996 return new \WP_Error('error', $error_msg, array(
997 'status' => 403,
998 ));
999 }
1000 break;
1001 default:
1002 return $this->bad_request();
1003 }
1004 return $this->WPDA_Rest_Response( __( 'Reset was successful', 'wp-data-access' ) );
1005 }
1006 if ( 'table' === $target ) {
1007 // Update table settings
1008 $error_msg = WPDA_App_Container_Model::update_table_settings( $cnt_id, $settings );
1009 if ( '' !== $error_msg ) {
1010 return new \WP_Error('error', $error_msg, array(
1011 'status' => 403,
1012 ));
1013 }
1014 } else {
1015 // Update form settings
1016 $error_msg = WPDA_App_Container_Model::update_form_settings( $cnt_id, $settings );
1017 if ( '' !== $error_msg ) {
1018 return new \WP_Error('error', $error_msg, array(
1019 'status' => 403,
1020 ));
1021 }
1022 }
1023 $error_msg = WPDA_App_Model::update_theme( $app_id, $theme );
1024 if ( '' !== $error_msg ) {
1025 return new \WP_Error('error', $error_msg, array(
1026 'status' => 403,
1027 ));
1028 }
1029 return $this->WPDA_Rest_Response( __( 'Successfully saved settings', 'wp-data-access' ) );
1030 }
1031
1032 private function do_app_remove( $app_id ) {
1033 WPDA_App_Model::delete( $app_id );
1034 WPDA_App_Container_Model::delete( $app_id );
1035 return $this->WPDA_Rest_Response( __( 'Successfully deleted app', 'wp-data-access' ) );
1036 }
1037
1038 private function do_app_create(
1039 $app_name,
1040 $app_title,
1041 $app_type,
1042 $app_settings,
1043 $app_dbs,
1044 $app_tbl,
1045 $app_cls,
1046 $app_table
1047 ) {
1048 // Add app
1049 $insert = WPDA_App_Model::create(
1050 $app_name,
1051 $app_title,
1052 $app_type,
1053 $app_settings
1054 );
1055 if ( false !== $insert['app_id'] ) {
1056 $app_id = $insert['app_id'];
1057 // Add app container
1058 $container = WPDA_App_Container_Model::create(
1059 $app_id,
1060 $app_dbs,
1061 $app_tbl,
1062 json_encode( $app_cls ),
1063 $app_title,
1064 0,
1065 $app_table
1066 );
1067 if ( false !== $container['cnt_id'] ) {
1068 // App and container successfully saved
1069 return $this->WPDA_Rest_Response( __( 'Successfully saved changes', 'wp-data-access' ) );
1070 } else {
1071 // Insert failed
1072 // Remove previously created app
1073 WPDA_App_Model::delete( $app_id );
1074 return new \WP_Error('error', $container['msg'], array(
1075 'status' => 403,
1076 ));
1077 }
1078 } else {
1079 // Insert failed
1080 return new \WP_Error('error', $insert['msg'], array(
1081 'status' => 403,
1082 ));
1083 }
1084 }
1085
1086 private function get_app_init() {
1087 return $this->WPDA_Rest_Response( '', array(
1088 'roles' => $this->get_wp_roles(),
1089 'users' => $this->get_wp_users(),
1090 ) );
1091 }
1092
1093 private function get_app_list() {
1094 $dataset = WPDA_App_Model::list();
1095 return $this->WPDA_Rest_Response( '', $dataset );
1096 }
1097
1098 private function get_app_container_meta( $app_id, $container ) {
1099 $app = WPDA_App_Model::get_by_id( $app_id );
1100 if ( false === $app ) {
1101 return $this->bad_request();
1102 }
1103 if ( !isset( $container[0]['cnt_dbs'], $container[0]['cnt_tbl'], $container[0]['cnt_cls'] ) ) {
1104 return $this->bad_request();
1105 }
1106 $response = array(
1107 'app' => array(
1108 'app' => $app,
1109 'container' => array_map( function ( $value ) {
1110 $show = current_user_can( 'manage_options' );
1111 if ( !$show ) {
1112 // Hide database and table name in responses for non admin users.
1113 unset($value['cnt_dbs']);
1114 unset($value['cnt_tbl']);
1115 }
1116 return $value;
1117 }, $container ),
1118 ),
1119 );
1120 $dbs = $container[0]['cnt_dbs'];
1121 $tbl = $container[0]['cnt_tbl'];
1122 $cls = WPDA_List_Columns_Cache::get_list_columns( $dbs, $tbl );
1123 $access = array(
1124 'select' => array(),
1125 'insert' => array(),
1126 'update' => array(),
1127 'delete' => array(),
1128 );
1129 $settings = new stdClass();
1130 $settings_db = WPDA_Table_Settings_Model::query( $tbl, $dbs );
1131 if ( isset( $settings_db[0]['wpda_table_settings'] ) ) {
1132 $settings = json_decode( (string) $settings_db[0]['wpda_table_settings'] );
1133 // Remove old settings from response.
1134 unset($settings->form_labels);
1135 unset($settings->list_labels);
1136 unset($settings->custom_settings);
1137 unset($settings->search_settings);
1138 }
1139 $settings->env = $this->get_env();
1140 $media = $this->get_media( $dbs, $tbl, $cls->get_table_columns() );
1141 $response['columns'] = $cls->get_table_columns();
1142 $response['table_labels'] = $cls->get_table_header_labels();
1143 $response['form_labels'] = $cls->get_table_column_headers();
1144 $response['primary_key'] = $cls->get_table_primary_key();
1145 $response['access'] = $access;
1146 $response['settings'] = $settings;
1147 $response['media'] = $media['media'];
1148 $response['wp_media'] = $media['wp_media'];
1149 $table_settings = json_decode( (string) $container[0]['cnt_table'], true );
1150 if ( isset( $table_settings['table']['defaultWhere'] ) ) {
1151 $default_where = $table_settings['table']['defaultWhere'];
1152 } else {
1153 $default_where = '';
1154 }
1155 $response['table_info'] = $this->get_table_info( $dbs, $tbl, $default_where );
1156 return $this->WPDA_Rest_Response( '', $response );
1157 }
1158
1159 private function get_app_meta( $app_id ) {
1160 $container = WPDA_App_Container_Model::select( $app_id, 0 );
1161 if ( !isset( $container[0] ) ) {
1162 return $this->bad_request();
1163 }
1164 return $this->get_app_container_meta( $app_id, $container );
1165 }
1166
1167 private function do_app_save(
1168 $app_id,
1169 $app_name,
1170 $app_title,
1171 $app_type,
1172 $app_settings,
1173 $app_add_to_menu,
1174 $app_dbs,
1175 $app_tbl,
1176 $app_cls
1177 ) {
1178 $error_msg = WPDA_App_Model::update(
1179 $app_id,
1180 $app_name,
1181 $app_title,
1182 $app_type,
1183 $app_settings,
1184 $app_add_to_menu
1185 );
1186 if ( '' !== $error_msg ) {
1187 return new \WP_Error('error', $error_msg, array(
1188 'status' => 403,
1189 ));
1190 }
1191 $error_msg = WPDA_App_Container_Model::update_master(
1192 $app_id,
1193 $app_dbs,
1194 $app_tbl,
1195 json_encode( $app_cls ),
1196 0
1197 );
1198 if ( '' !== $error_msg ) {
1199 return new \WP_Error('error', $error_msg, array(
1200 'status' => 403,
1201 ));
1202 }
1203 return $this->WPDA_Rest_Response( __( 'Changes successfully saved', 'wp-data-access' ) );
1204 }
1205
1206 private function main_app_access( $app_id, &$msg = '' ) {
1207 // Get app info
1208 $app = WPDA_App_Model::get_by_id( $app_id );
1209 if ( false === $app ) {
1210 // App not found
1211 $msg = __( 'Bad request', 'wp-data-access' );
1212 return false;
1213 }
1214 // Check access
1215 $app_settings_db = $app[0]['app_settings'];
1216 $app_settings = json_decode( (string) $app_settings_db, true );
1217 if ( !isset( $app_settings['rest_api']['authorization'], $app_settings['rest_api']['authorized_roles'], $app_settings['rest_api']['authorized_users'] ) || !is_array( $app_settings['rest_api']['authorized_roles'] ) || !is_array( $app_settings['rest_api']['authorized_users'] ) ) {
1218 // App contain no rest api settings
1219 $msg = __( 'Bad request', 'wp-data-access' );
1220 return false;
1221 }
1222 if ( !$this->current_user_can_access() && 'anonymous' !== $app_settings['rest_api']['authorization'] ) {
1223 // Check authorization
1224 // Check user role
1225 $user_roles = WPDA::get_current_user_roles();
1226 if ( !is_array( $user_roles ) || empty( array_intersect( $app_settings['rest_api']['authorized_roles'], $user_roles ) ) ) {
1227 // Check user login
1228 $user_login = WPDA::get_current_user_login();
1229 if ( !in_array( $user_login, $app_settings['rest_api']['authorized_users'] ) ) {
1230 $msg = __( 'Unauthorized', 'wp-data-access' );
1231 return false;
1232 }
1233 }
1234 }
1235 return true;
1236 }
1237
1238 public function check_app_access(
1239 $app_id,
1240 $cnt_id,
1241 $action,
1242 &$dbs,
1243 &$tbl,
1244 &$msg = '',
1245 &$settings = array()
1246 ) {
1247 if ( !$this->main_app_access( $app_id, $msg ) ) {
1248 return false;
1249 }
1250 // Get container
1251 $container = WPDA_App_Container_Model::get_container( $cnt_id );
1252 if ( !is_array( $container ) || 0 === count( $container ) ) {
1253 // Container not found
1254 $msg = __( 'Bad request', 'wp-data-access' );
1255 return false;
1256 }
1257 if ( 'select' !== $action ) {
1258 $cnt_table = json_decode( (string) $container[0]['cnt_table'], true );
1259 if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) {
1260 $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true );
1261 if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $cnt_relation['cnt_id_master'], $action )) ) {
1262 $msg = __( 'Unauthorized', 'wp-data-access' );
1263 return false;
1264 }
1265 }
1266 }
1267 // Return database name, table name and columns
1268 $dbs = $container[0]['cnt_dbs'];
1269 $tbl = $container[0]['cnt_tbl'];
1270 $settings = array(
1271 'columns' => json_decode( (string) $container[0]['cnt_cls'], true ),
1272 'table' => json_decode( (string) $container[0]['cnt_table'], true ),
1273 'form' => json_decode( (string) $container[0]['cnt_form'], true ),
1274 );
1275 return true;
1276 }
1277
1278 private function check_master_container_access( $cnt_id, $action ) {
1279 $container = WPDA_App_Container_Model::get_container( $cnt_id );
1280 if ( !is_array( $container ) || 0 === count( $container ) ) {
1281 // Container not found
1282 return false;
1283 }
1284 $cnt_table = json_decode( (string) $container[0]['cnt_table'], true );
1285 if ( !isset( $cnt_table['table']['transactions'][$action] ) || false === $cnt_table['table']['transactions'][$action] ) {
1286 $cnt_relation = json_decode( (string) $container[0]['cnt_relation'], true );
1287 if ( !(isset( $cnt_relation['cnt_id_master'] ) && $this->check_master_container_access( $cnt_relation['cnt_id_master'], $action )) ) {
1288 return false;
1289 }
1290 }
1291 return true;
1292 }
1293
1294 }
1295