-
Notifications
You must be signed in to change notification settings - Fork 9
/
captaincore.php
executable file
·4889 lines (4117 loc) · 149 KB
/
captaincore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link https://captaincore.io
* @since 0.1.0
* @package Captaincore
*
* @wordpress-plugin
* Plugin Name: CaptainCore Manager
* Plugin URI: https://captaincore.io
* Description: WordPress management toolkit for geeky maintenance professionals.
* Version: 0.18.0
* Author: Austin Ginder
* Author URI: https://austinginder.com
* License: MIT License
* License URI: https://opensource.org/licenses/MIT
* Text Domain: captaincore
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
function activate_captaincore() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-captaincore-activator.php';
Captaincore_Activator::activate();
}
function deactivate_captaincore() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-captaincore-deactivator.php';
Captaincore_Deactivator::deactivate();
}
register_activation_hook( __FILE__, 'activate_captaincore' );
register_deactivation_hook( __FILE__, 'deactivate_captaincore' );
require plugin_dir_path( __FILE__ ) . 'includes/class-captaincore.php';
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 0.1.0
*/
( new Captaincore )->run();
require plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
require 'includes/Parsedown.php';
function captaincore_cron_run() {
CaptainCore\Accounts::process_renewals();
CaptainCore\Scripts::run_scheduled();
}
add_action( 'captaincore_cron', 'captaincore_cron_run' );
// Hook to run Mailgun verify at a later time
add_action( 'schedule_mailgun_verify', '\CaptainCore\Providers\Mailgun::verify', 10, 3 );
function captaincore_failed_notify( $order_id, $old_status, $new_status ){
echo "Woocommerce $order_id, $old_status, $new_status ";
if ( $new_status == 'failed' and $old_status != "failed" ){
$order = wc_get_order( $order_id );
$account_id = $order->get_meta( "captaincore_account_id" );
( new CaptainCore\Account( $account_id, true ) )->failed_notify();
}
}
add_action( 'woocommerce_order_status_changed', 'captaincore_failed_notify', 10, 3);
function captaincore_missive_func( WP_REST_Request $request ) {
$key = $request->get_header('X-Hook-Signature');
if ( empty( $key ) ) {
return "Bad Request";
}
$computed_signature = 'sha256=' . hash_hmac( "sha256", $request->get_body(), CAPTAINCORE_MISSIVE_API );
if ( ! hash_equals( $computed_signature, $key ) ) {
return "Bad Request";
}
$errors = [];
$missive = json_decode( $request->get_body() );
$subject = empty( $missive->latest_message->subject ) ? $missive->message->subject : $missive->latest_message->subject;
if ( $subject == "Email Health Check" ) {
$message = explode( " ", $missive->message->preview);
if ( count( $message ) != 2 ) {
return;
}
$site = $message[0];
$site_id = is_string( $site ) ? explode( "-", $site )[0] : "";
$token = $message[1];
$site_check = CaptainCore\Sites::get( $site_id );
if ( ! $site_check ) {
return;
}
if ( ! is_numeric( $token ) || ! (int) $token == $token ) {
return;
}
CaptainCore\Run::CLI( "email-health response $site $token received" );
return;
}
if ( $subject == "Action is required to renew your SSL certificate" ) {
$message_id = $missive->latest_message->id;
$message = CaptainCore\Remote\Missive::get( "messages/$message_id")->messages->body;
preg_match('/TXT record for (.+) in MyKinsta/', $message, $matches );
$domain = $matches[1];
$response = ( new CaptainCore\Domains )->add_verification_record( $domain );
$errors = implode( ", ", $errors );
CaptainCore\Remote\Missive::post( "posts", [ "posts" => [
"conversation" => $missive->conversation->id,
"notification" => [ "title" => "", "body" => "" ],
"username" => "CaptainCore Bot",
"username_icon" => "https://captaincore.io/logo.png",
"markdown" => $response
] ] );
return;
}
}
function captaincore_api_func( WP_REST_Request $request ) {
$post = json_decode( file_get_contents( 'php://input' ) );
$archive = empty( $post->archive ) ? "" : $post->archive;
$command = empty( $post->command ) ? "" : $post->command;
$environment = empty( $post->environment ) ? "" : $post->environment;
$storage = empty( $post->storage ) ? "" : $post->storage;
$visits = empty( $post->visits ) ? "" : $post->visits;
$email = empty( $post->email ) ? "" : $post->email;
$server = empty( $post->server ) ? "" : $post->server;
$core = empty( $post->core ) ? "" : $post->core;
$plugins = empty( $post->plugins ) ? "" : $post->plugins;
$themes = empty( $post->themes ) ? "" : $post->themes;
$users = empty( $post->users ) ? "" : $post->users;
$fathom = empty( $post->fathom ) ? "" : $post->fathom;
$home_url = empty( $post->home_url ) ? "" : $post->home_url;
$subsite_count = empty( $post->subsite_count ) ? "" : $post->subsite_count;
$git_status = empty( $post->git_status ) ? "" : trim( base64_decode( $post->git_status ) );
$token_key = empty( $post->token_key ) ? "" : $post->token_key;
$data = empty( $post->data ) ? "" : $post->data;
$site_id = empty( $post->site_id ) ? "" : $post->site_id;
$user_id = empty( $post->user_id ) ? "" : $post->user_id;
$notes = empty( $post->notes ) ? "" : $post->notes;
$response = "";
// Error if token not valid
if ( $post->token != CAPTAINCORE_CLI_TOKEN ) {
// Create the response object
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 404 ] );
}
// Error if site not valid
$current_site = ( new CaptainCore\Sites )->get( $site_id );
if ( $current_site == "" && $site_id != "" && $command != "default-get" && $command != "configuration-get" ) {
return new WP_Error( 'command_invalid', 'Invalid Command', [ 'status' => 404 ] );
}
$site_name = $current_site->site;
$domain_name = $current_site->name;
$environment_id = ( new CaptainCore\Site( $site_id ) )->fetch_environment_id( $environment );
// Copy site
if ( $command == 'copy' and $email ) {
$site_source = get_the_title( $post->site_source_id );
$site_destination = get_the_title( $post->site_destination_id );
$business_name = get_field('business_name', 'option');
// Send out completed email notice
$to = $email;
$subject = "$business_name - Copy site ($site_source) to ($site_destination) completed";
$body = "Completed copying $site_source to $site_destination.<br /><br /><a href=\"http://$site_destination\">$site_destination</a>";
$headers = [ 'Content-Type: text/html; charset=UTF-8' ];
wp_mail( $to, $subject, $body, $headers );
echo 'copy-site email sent';
}
// Production deploy to staging
if ( $command == 'production-to-staging' and $email ) {
$business_name = get_field('business_name', 'option');
$domain_name = get_the_title( $site_id );
$db = new CaptainCore\Site( $site_id );
$site = $db->get();
$link = $site->environments[1]["link"];
// Send out completed email notice
$to = $email;
$subject = "$business_name - Deploy to Staging ($domain_name)";
$body = 'Deploy to staging completed for ' . $domain_name . '.<br /><br /><a href="' . $link . '">' . $link . '</a>';
$headers = [ 'Content-Type: text/html; charset=UTF-8' ];
wp_mail( $to, $subject, $body, $headers );
echo 'production-to-staging email sent';
}
// Kinsta staging deploy to production
if ( $command == 'staging-to-production' and $email ) {
$business_name = get_field('business_name', 'option');
$domain_name = get_the_title( $site_id );
$db = new CaptainCore\Site( $site_id );
$site = $db->get();
$link = $site->environments[0]["link"];
// Send out completed email notice
$to = $email;
$subject = "$business_name - Deploy to Production ($domain_name)";
$body = 'Deploy to production completed for ' . $domain_name . '.<br /><br /><a href="' . $link . '">' . $link . '</a>';
$headers = [ 'Content-Type: text/html; charset=UTF-8' ];
wp_mail( $to, $subject, $body, $headers );
echo 'staging-to-production email sent';
}
// Generate a new snapshot.
if ( $command == 'snapshot-add' ) {
$snapshot_check = ( new CaptainCore\Snapshots )->get( $post->data->snapshot_id );
// Insert new snapshot
if ( empty( $snapshot_check ) ) {
( new CaptainCore\Snapshots )->insert( (array) $post->data );
} else {
// Update existing quicksave
( new CaptainCore\Snapshots )->update( (array) $post->data, [ "snapshot_id" => $post->data->snapshot_id ] );
}
$response = [
"response" => "Snapshot added for $site_id",
"snapshot" => $post->data,
];
// Send out snapshot email
captaincore_download_snapshot_email( $post->data->snapshot_id );
}
// Load Token Key
if ( $command == 'token' and isset( $token_key ) ) {
( new CaptainCore\Sites )->update( [ "token" => $token_key ], [ "site_id" => $site_id ] );
echo "Adding token key. \n";
}
// Update Fathom
if ( $command == 'update-fathom' and ! empty( $post->data ) ) {
$current_environment = ( new CaptainCore\Environments )->get( $post->data->environment_id );
$environment = strtolower( $current_environment->environment );
$details = ( isset( $current_environment->details ) ? json_decode( $current_environment->details ) : (object) [] );
$details->fathom = $post->data->fathom;
( new CaptainCore\Environments )->update( [
"details" => json_encode( $details ),
], [ "environment_id" => $post->data->environment_id ] );
$response = [
"response" => "Completed update-fathom for $site_id",
"environment" => $post->data,
];
}
if ( $command == 'update-site' and ! empty( $post->data ) ) {
$current_site = ( new CaptainCore\Sites )->get( $post->data->site_id );
( new CaptainCore\Sites )->update( (array) $post->data, [ "site_id" => $post->data->site_id ] );
$response = [
"response" => "Completed update-site for $site_id",
"site" => $post->data,
];
}
if ( $command == 'update-environment' and ! empty( $post->data ) ) {
$current_environment = ( new CaptainCore\Environments )->get( $post->data->environment_id );
( new CaptainCore\Environments )->update( (array) $post->data, [ "environment_id" => $post->data->environment_id ] );
$response = [
"response" => "Completed update-environment for $site_id",
"environment" => $post->data,
];
// Mark Site as updated
( new CaptainCore\Sites )->update( [ "updated_at" => $post->data->updated_at ], [ "site_id" => $site_id ] );
}
// Sync site data
if ( $command == 'sync-data' and ! empty( $post->data ) ) {
$current_environment = ( new CaptainCore\Environments )->get( $post->data->environment_id );
$environment = strtolower( $current_environment->environment );
$upload_dir = wp_upload_dir();
$screenshot_check = $upload_dir['basedir'] . "/screenshots/{$site_name}_{$site_id}/$environment/screenshot-800.png";
if ( file_exists( $screenshot_check ) ) {
$environment_update['screenshot'] = true;
} else {
$environment_update['screenshot'] = false;
}
( new CaptainCore\Environments )->update( (array) $post->data, [ "environment_id" => $post->data->environment_id ] );
$response = [
"response" => "Completed sync-data for $site_id",
"environment" => $post->data,
];
$current_site = ( new CaptainCore\Sites )->get( $site_id );
$details = json_decode( $current_site->details );
unset( $details->connection_errors );
if ( $current_environment->environment == "Production" ) {
$details->core = $post->data->core;
$details->subsites = $post->data->subsite_count;
}
// Mark Site as updated
( new CaptainCore\Sites )->update( [ "updated_at" => $post->data->updated_at, "details" => json_encode( $details ) ], [ "site_id" => $site_id ] );
}
// Add capture
if ( $command == 'new-capture' ) {
$environment_id = ( new CaptainCore\Site( $site_id ) )->fetch_environment_id( $environment );
$captures = new CaptainCore\Captures();
$capture_lookup = $captures->where( [ "site_id" => $site_id, "environment_id" => $environment_id ] );
if ( count( $capture_lookup ) > 0 ) {
$current_capture_pages = json_decode( $capture_lookup[0]->pages );
}
$git_commit_short = substr( $data->git_commit, 0, 7 );
$image_ending = "_{$data->created_at}_{$git_commit_short}.jpg";
$capture_pages = explode( ",", $data->capture_pages );
$captured_pages = explode( ",", $data->captured_pages );
$pages = [];
foreach( $capture_pages as $page ) {
$page_name = str_replace( "/", "#", $page );
// Add page with new screenshot
if ( in_array( $page, $captured_pages ) ) {
$pages[] = [
"name" => $page,
"image" => "{$page_name}{$image_ending}",
];
continue;
}
// Lookup current image from DB
$current_image = "";
foreach($current_capture_pages as $current_capture_page) {
if ($page == $current_capture_page->name) {
$current_image = $current_capture_page->image;
break;
}
}
// Otherwise add image to current screenshot
$pages[] = [
"name" => $page,
"image" => $current_image,
];
}
// Format for mysql timestamp format. Changes "1530817828" to "2018-06-20 09:15:20"
$epoch = $data->created_at;
$created_at = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
$created_at = $created_at->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00
$new_capture = [
'site_id' => $site_id,
'environment_id' => $environment_id,
'created_at' => $created_at,
'git_commit' => $data->git_commit,
'pages' => json_encode( $pages ),
];
( new CaptainCore\Captures )->insert( $new_capture );
// Update pointer to new thumbnails for site
if ( $environment == "production" ) {
$site = ( new CaptainCore\Sites )->get( $site_id );
$details = json_decode( $site->details );
$details->screenshot_base = "{$data->created_at}_${git_commit_short}";
( new CaptainCore\Sites )->update( [ "screenshot" => true, "details" => json_encode( $details ) ], [ "site_id" => $site_id ] );
}
// Update pointer to new thumbnails for environment
$environment = ( new CaptainCore\Environments )->get( $environment_id );
$details = ( isset( $environment->details ) ? json_decode( $environment->details ) : (object) [] );
$details->screenshot_base = "{$data->created_at}_${git_commit_short}";
( new CaptainCore\Environments )->update( [ "screenshot" => true, "details" => json_encode( $details ) ], [ "environment_id" => $environment_id ] );
}
if ( $command == 'site-get-raw' ) {
$site = new CaptainCore\Site( $post->site_id );
$response = [
"response" => "Fetching site {$post->site_id}",
"site" => $site->get_raw(),
];
}
if ( $command == 'site-delete' ) {
( new CaptainCore\Sites )->delete( $post->site_id );
$response = [
"response" => "Delete site {$post->site_id}"
];
}
if ( $command == 'account-get-raw' ) {
$account = new CaptainCore\Account( $post->account_id, true );
$response = [
"response" => "Fetching account {$post->account_id}",
"account" => $account->get_raw(),
];
}
if ( $command == 'configuration-get' ) {
$configurations = ( new CaptainCore\Configurations )->get();
$response = [
"response" => "Fetching configurations",
"configurations" => $configurations,
];
}
if ( $command == 'default-get' ) {
$defaults = ( new CaptainCore\Defaults )->get();
$response = [
"response" => "Fetching global defaults",
"defaults" => $defaults,
];
}
// Updates visits and storage usage
if ( $command == 'usage-update' ) {
$current_environment = ( new CaptainCore\Environments )->get( $post->data->environment_id );
( new CaptainCore\Environments )->update( (array) $post->data, [ "environment_id" => $post->data->environment_id ] );
$response = [
"response" => "Completed usage-update for $site_id",
"environment" => $post->data,
];
( new CaptainCore\Site( $current_environment->site_id ) )->update_details();
}
return $response;
}
function captaincore_accounts_func( $request ) {
return ( new CaptainCore\Accounts )->list();
}
function captaincore_configurations_func( $request ) {
return ( new CaptainCore\Configurations )->get();
}
function captaincore_configurations_update_func( $request ) {
$configurations = $request->get_param( "configurations" );
return ( new CaptainCore\Configurations )->update( $configurations );
}
function captaincore_subscriptions_func( $request ) {
return ( new CaptainCore\User )->subscriptions();
}
function captaincore_upcoming_subscriptions_func( $request ) {
return ( new CaptainCore\User )->upcoming_subscriptions();
}
function captaincore_billing_func( $request ) {
return ( new CaptainCore\User )->billing();
}
function captaincore_provider_new_func( $request ) {
if ( ! ( new CaptainCore\User )->is_admin() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = $request->get_param( "provider" );
return ( new CaptainCore\Provider )->create( $provider );
}
function captaincore_provider_update_func( $request ) {
if ( ! ( new CaptainCore\User )->is_admin() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider_id = $request->get_param( "id" );
$provider = $request->get_param( "provider" );
unset( $provider["provider_id"] );
unset( $provider["created_at"] );
$provider["updated_at"] = date("Y-m-d H:i:s");
$provider["credentials"] = json_encode( $provider["credentials"] );
return ( new CaptainCore\Providers )->update( $provider, [ "provider_id" => $provider_id ] );
}
function captaincore_provider_delete_func( $request ) {
if ( ! ( new CaptainCore\User )->is_admin() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider_id = $request->get_param( "id" );
return ( new CaptainCore\Providers )->delete( $provider_id );
}
function captaincore_provider_func( $request ) {
if ( ! ( new CaptainCore\User )->is_admin() ){
return [];
}
return ( new CaptainCore\Provider )->all();
}
function captaincore_provider_verify_func( $request ) {
if ( ! ( new CaptainCore\User )->is_admin() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = $request->get_param( "provider" );
return ( new CaptainCore\Provider( $provider ) )->verify();
}
function captaincore_provider_themes_func( $request ) {
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = "CaptainCore\Providers\\" . ucfirst( $request->get_param( "provider" ) );
return $provider::themes();
}
function captaincore_provider_theme_download_func( $request ) {
$theme_id = $request->get_param( "id" );
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = "CaptainCore\Providers\\" . ucfirst( $request->get_param( "provider" ) );
return $provider::download_theme( $theme_id );
}
function captaincore_provider_plugin_download_func( $request ) {
$plugin_id = $request->get_param( "id" );
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = "CaptainCore\Providers\\" . ucfirst( $request->get_param( "provider" ) );
return $provider::download_plugin( $plugin_id );
}
function captaincore_provider_plugins_func( $request ) {
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = "CaptainCore\Providers\\" . ucfirst( $request->get_param( "provider" ) );
return $provider::plugins();
}
function captaincore_provider_connect_func( $request ) {
if ( ! ( new CaptainCore\User )->is_admin() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = $request->get_param( "provider" );
$token = $request['token'];
return ( new CaptainCore\Provider( $provider ) )->update_token( $token );
}
function captaincore_provider_new_site_func( $request ) {
$user = (object) ( new CaptainCore\User )->fetch();
$provider = $request->get_param( "provider" );
$site = (object) $request['site'];
$errors = [];
if ( empty( $site->name ) ) {
$errors[] = "Missing name";
}
if ( ! empty( $site->name ) && ( strlen( $site->name ) < 5 || strlen( $site->name ) > 32 ) ) {
$errors[] = "Name must be between 5 and 32 characters in length";
}
if ( empty( $site->domain ) ) {
$errors[] = "Missing domain";
}
if ( ! ( new CaptainCore\User )->is_admin() && ! captaincore_verify_permissions_account( $site->account_id ) ){
$errors[] = "Permission denied";
}
if ( ! ( new CaptainCore\User )->is_admin() && ! empty( $site->provider_id ) && ( $site->provider_id != "1" ) ) {
$provider_lookup = CaptainCore\Providers::get( $site->provider_id );
if ( $provider_lookup->user_id != $user->user_id ) {
$errors[] = "Permission denied";
}
}
if ( ! empty( $errors ) ) {
return [
'errors' => $errors,
];
}
return ( new CaptainCore\Provider( $provider ) )->new_site( $site );
}
function captaincore_provider_deploy_to_staging_func( $request ) {
$site_id = $request['site_id'];
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = $request->get_param( "provider" );
CaptainCore\ProcessLog::insert( "Deploy production to staging", $site_id );
return ( new CaptainCore\Provider( $provider ) )->deploy_to_staging( $site_id );
}
function captaincore_provider_deploy_to_production_func( $request ) {
$site_id = $request['site_id'];
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$provider = $request->get_param( "provider" );
CaptainCore\ProcessLog::insert( "Deploy staging to production", $site_id );
return ( new CaptainCore\Provider( $provider ) )->deploy_to_production( $site_id );
}
function captaincore_provider_actions_check_func( $request ) {
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
return ( new CaptainCore\ProviderAction )->check();
}
function captaincore_provider_actions_run_func( $request ) {
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
return ( new CaptainCore\ProviderAction( $request['id'] ) )->run();
}
function captaincore_provider_actions_func( $request ) {
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
return ( new CaptainCore\ProviderAction )->active();
}
function captaincore_schedule_script_func( $request ) {
$environment_id = $request['environment_id'];
$code = $request['code'];
$run_at = (object) $request['run_at'];
$timestamp = new DateTime("$run_at->date $run_at->time", new DateTimeZone($run_at->timezone));
$timestamp->setTimezone(new DateTimeZone('UTC'));
$time_now = date("Y-m-d H:i:s");
$details = [
"run_at" => $timestamp->getTimestamp()
];
$new_script = CaptainCore\Scripts::insert( [
"environment_id" => $environment_id,
"user_id" => get_current_user_id(),
"code" => $code,
"details" => json_encode( $details ),
"status" => "scheduled",
"created_at" => $time_now,
"updated_at" => $time_now,
] );
return $new_script;
}
function captaincore_update_script_func( $request ) {
$script_id = $request['id'];
$script = CaptainCore\Scripts::get( $script_id );
$site_id = CaptainCore\Environments::get( $script->environment_id )->site_id;
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$details = json_decode( $script->details );
$run_at = (object) $request['run_at'];
$time_now = date("Y-m-d H:i:s");
$timestamp = new DateTime("$run_at->date $run_at->time", new DateTimeZone($run_at->timezone));
$timestamp->setTimezone(new DateTimeZone('UTC'));
$details->run_at = $timestamp->getTimestamp();
return ( new CaptainCore\Scripts )->update( [ "code" => $request['code'], 'details' => json_encode( $details ) ], [ "script_id" => $script_id ] );
}
function captaincore_delete_script_func( $request ) {
$script_id = $request['id'];
$script = CaptainCore\Scripts::get( $script_id );
$site_id = CaptainCore\Environments::get( $script->environment_id )->site_id;
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
return ( new CaptainCore\Scripts )->delete( $script_id );
}
function captaincore_sites_func( $request ) {
return ( new CaptainCore\Sites )->list();
}
function captaincore_site_update_func( $request ) {
$site_id = $request['id'];
$updated_details = $request['details'];
if ( ! is_numeric ( $site_id ) ) {
$site = ( new CaptainCore\Sites )->where( [ "site" => $site_id ] );
if ( count( $site ) == 1 ) {
$site_id = $site[0]->site_id;
}
}
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
$site = CaptainCore\Sites::get( $site_id );
$details = empty( $site->details ) ? (object) [] : json_decode( $site->details );
foreach ( $updated_details as $field => $value ) {
$details->$field = $value;
if ( $field == "removed" ) {
$title = ( new CaptainCore\Configurations )->get()->name;
$user = (object) ( new CaptainCore\User )->fetch();
if ( $value == true ) {
$subject = "$title - Site Removal Request";
$message = "Site {$site->name} #{$site_id} has been requested to be removed.<br /><br />Requested By: {$user->name} #{$user->user_id}";
}
if ( $value != true ) {
$subject = "$title - Cancel Site Removal Request";
$message = "Site {$site->name} #{$site_id} has been requested to keep. Disregard previous removal request.<br /><br />Requested By: {$user->name} #{$user->user_id}";
}
wp_mail(
get_option( "admin_email" ),
$subject,
$message,
[ 'Content-Type: text/html; charset=UTF-8' ]
);
}
}
$query = CaptainCore\Sites::update([
"details" => json_encode( $details )
], [
"site_id" => $site_id
]);
return;
}
function captaincore_site_func( $request ) {
$site_id = $request['id'];
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
$site = new CaptainCore\Site( $site_id );
return $site->get();
}
function captaincore_domain_func( $request ) {
$domain_id = $request['id'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
return ( new CaptainCore\Domain( $domain_id ) )->fetch();
}
function captaincore_domain_privacy_func( $request ) {
$domain_id = $request['id'];
$status = $request['status'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
if ( $status == "on" ) {
return ( new CaptainCore\Domain( $domain_id ) )->privacy_on();
}
if ( $status == "off" ) {
return ( new CaptainCore\Domain( $domain_id ) )->privacy_off();
}
return new WP_Error( 'request_invalid', 'Invalid Request', [ 'status' => 404 ] );
}
function captaincore_domain_lock_func( $request ) {
$domain_id = $request['id'];
$status = $request['status'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
if ( $status == "on" ) {
return ( new CaptainCore\Domain( $domain_id ) )->lock();
}
if ( $status == "off" ) {
return ( new CaptainCore\Domain( $domain_id ) )->unlock();
}
return new WP_Error( 'request_invalid', 'Invalid Request', [ 'status' => 404 ] );
}
function captaincore_domain_update_contacts_func( $request ) {
$domain_id = $request['id'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
return ( new CaptainCore\Domain( $domain_id ) )->set_contacts( $request['contacts'] );
}
function captaincore_domain_update_nameservers_func( $request ) {
$domain_id = $request['id'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
return ( new CaptainCore\Domain( $domain_id ) )->set_nameservers( $request['nameservers'] );
}
function captaincore_domain_auth_code_func( $request ) {
$domain_id = $request['id'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
$domain = ( new CaptainCore\Domains )->get( $domain_id );
if ( empty( $domain->provider_id ) ) {
return new WP_Error( 'no_domain', 'No records', [ 'status' => 200 ] );
}
return ( new CaptainCore\Domain( $domain_id ) )->auth_code();
}
function captaincore_dns_func( $request ) {
$domain_id = $request['id'];
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
$remote_id = ( new CaptainCore\Domains )->get( $domain_id )->remote_id;
$domain = CaptainCore\Remote\Constellix::get( "domains/$remote_id" );
$response = CaptainCore\Remote\Constellix::get( "domains/$remote_id/records?perPage=100" );
if ( ! $response->errors ) {
array_multisort( array_column( $response->data, 'type' ), SORT_ASC, array_column( $response->data, 'name' ), SORT_ASC, $response->data );
}
return [
"records" => $response->data,
"nameservers" => $domain->data->nameservers
];
}
function captaincore_domains_func( $request ) {
return ( new CaptainCore\Domains() )->list();
}
function captaincore_domain_zone_func( $request ) {
$domain_id = $request->get_param( "id" );
$verify = ( new CaptainCore\Domains )->verify( $domain_id );
if ( ! $verify ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
return CaptainCore\Domain::zone( $domain_id );
}
function captaincore_domain_zone_import_func( $request ) {
$domain = $request->get_param( "domain" );
$zone = $request->get_param( "zone" );
$lines = explode( "\n", $zone );
foreach( $lines as $line ) {
if ( str_contains( $line, "\$ORIGIN" ) ) {
$domain = str_replace( "\$ORIGIN", "", $line );
$domain = trim( $domain );
$domain = trim( $domain, "." );
}
}
if ( ! ( new CaptainCore\User )->role_check() ){
return new WP_Error( 'token_invalid', "Invalid Token", [ 'status' => 403 ] );
}
return CaptainCore\Domains::records( $domain, $zone );
}
function captaincore_recipes_func( $request ) {
return ( new CaptainCore\Recipes() )->list();
}
function captaincore_running_func( $request ) {
$current_user = wp_get_current_user();
$role_check = in_array( 'administrator', $current_user->roles );
// Checks for a current user. If admin found pass
if ( $current_user && $role_check ) {
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}
$data = [
'timeout' => 45,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => CAPTAINCORE_CLI_TOKEN
],
'body' => json_encode( [ "command" => "running list" ] ),
'method' => 'POST',
'data_format' => 'body'
];
// Add command to dispatch server
$response = wp_remote_post( CAPTAINCORE_CLI_ADDRESS . "/run", $data );
$processes = json_decode( $response["body"]);
usort( $processes, function($a, $b) { return strcmp($b->created_at, $a->created_at); });
return $processes;
}
return [];
}
function captaincore_site_phpmyadmin_func( $request ) {
$site_id = $request['id'];
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
$environment = $request['environment'];
$site = new CaptainCore\Site( $site_id );
return $site->fetch_phpmyadmin();
}
function captaincore_site_magiclogin_func( $request ) {
$site_id = $request['id'];
$user_id = $request['user_id'];
$environment = $request['environment'];
if ( ! captaincore_verify_permissions( $site_id ) ) {
return new WP_Error( 'token_invalid', 'Invalid Token', [ 'status' => 403 ] );
}
$environment_id = ( new CaptainCore\Site( $site_id ) )->fetch_environment_id( $environment );
$environment = ( new CaptainCore\Environments )->get( $environment_id );
$current_email = ( new CaptainCore\User )->fetch()["email"];
$users = json_decode( $environment->users );
// Match user by ID
if ( ! empty( $user_id ) ) {
foreach ( $users as $user ) {
if ( $user->ID == $user_id ) {
$login = $user->user_login;
break;
}
}
}
// Attempt to match current user to WordPress user
if ( empty( $login ) ) {
foreach ( $users as $user ) {
if ( strpos( $user->roles, 'administrator' ) !== false && $user->user_email == $current_email ) {
$login = $user->user_login;
break;
}
}
}
if ( empty( $login ) ) {
$current_user_domain = array_pop(explode('@', $current_email));
// Attempt to match current user to a similar WordPress user
foreach ( $users as $user ) {
$user_domain = array_pop(explode('@', $user->user_email));
if ( strpos( $user->roles, 'administrator') !== false && $user_domain == $current_user_domain ) {
$login = $user->user_login;
break;
}
}
// Select random WordPress admin with same first name