-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path!sak4wp.php
4515 lines (3652 loc) · 187 KB
/
!sak4wp.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
/**
Swiss Army Knife for WordPress - standalone open source tool (GPL) that allows you to do system admin work on your WordPress site. Created by @orbisius
You must remove it after the work is complete to avoid security issues.
License: GPL (v2 or later)
Author: Svetoslav Marinov (SLAVI)
Author Site: https://orbisius.com
Product Site: https://orbisius.com/products/tools/swiss-army-knife-for-wordpress
Copyright: All Rights Reserved.
Disclaimer: By using this tool you take full responsibility to remove it.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
DISCLAIMER OF WARRANTY
The Software is provided "AS IS" and "WITH ALL FAULTS," without warranty of any kind, including without limitation the warranties of merchantability,
fitness for a particular purpose and non-infringement. The Licensor makes no warranty that the Software is free of defects or is suitable for any
particular purpose. In no event shall the Licensor be responsible for loss or damages arising from the installation or use of the Software,
including but not limited to any indirect, punitive, special, incidental or consequential damages of any character including, without limitation,
damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses.
The entire risk as to the quality and performance of the Software is borne by you. Should the Software prove defective, you and not the
Licensor assume the entire cost of any service and repair.
*/
define('ORBISIUS_WP_SAK_APP_SHORT_NAME', 'SAK4WP');
define('ORBISIUS_WP_SAK_APP_NAME', 'Swiss Army Knife for WordPress');
define('ORBISIUS_WP_SAK_APP_URL', 'https://orbisius.com/products/tools/swiss-army-knife-for-wordpress');
define('ORBISIUS_WP_SAK_APP_VER', '1.1.7');
define('ORBISIUS_WP_SAK_APP_SCRIPT', basename(__FILE__));
define('ORBISIUS_WP_SAK_APP_BASE_DIR', dirname(__FILE__));
define('ORBISIUS_WP_SAK_HOST', str_replace('www.', '', $_SERVER['HTTP_HOST']));
ob_start();
//define('WP_DEBUG_DISPLAY', 0);
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$ctrl->init();
// @todo: check for wp-cli and offer quick install via
// set ups from orbisius.com account.
$ctrl->preRun();
// Do we define quick run without theme loading???
// This WP load may fail which we'll check()
$wp_load_locations = array(
dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'wp-load.php',
dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'wp-load.php',
dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'wp-load.php',
dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . DIRECTORY_SEPARATOR . 'wp-load.php',
dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) . DIRECTORY_SEPARATOR . 'wp-load.php',
);
foreach ( $wp_load_locations as $wp_load_php ) {
if ( file_exists( $wp_load_php ) ) {
include_once( $wp_load_php );
if ( defined( 'ABSPATH' ) ) {
break;
}
}
}
// This stops WP Super Cache and W3 Total Cache from caching
defined( 'WP_CACHE' ) || define( 'WP_CACHE', false );
$ctrl->check();
$ctrl->run();
$ctrl->postRun();
} catch (Exception $e) {
echo "Error: " . htmlentities($e->getMessage());
} finally {
$dbg_output = ob_get_clean();
echo $dbg_output;
}
/**
*
*/
class Orbisius_WP_SAK_Controller_Module {
protected $description = '';
private $params = array();
public function init($params = null) {
if (!is_null($params)) {
$this->params = $params;
}
}
public function handleAction() {
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$ctrl->sendHeader(Orbisius_WP_SAK_Controller::HEADER_JS, json_encode( array('status' => 0, 'message' => "Invalid Action") ) );
}
/**
* Descripton about what the module does
*/
public function getInfo() {
return $this->description;
}
/**
* Descripton about what the module does
*/
public function run() {
trigger_error("Module doesn't implement run() method.", E_USER_ERROR);
}
}
/**
* Self_Protect Module - Make sure that only one user can access SAK.
*/
class Orbisius_WP_SAK_Controller_Module_Self_Protect extends Orbisius_WP_SAK_Controller_Module {
private $first_run_file;
/**
* Setups the object stuff and defines some descriptions
*/
public function __construct() {
$this->first_run_file = Orbisius_WP_SAK_Util::getWPUploadsDir() . '.ht-sak4wp-' . ORBISIUS_WP_SAK_HOST;
$this->description = <<<EOF
<h4>Self Protect</h4>
<p>
This module allows you to run SAK4WP more securely. The first time you access SAK4WP, it will save your IP and browser info.
If somebody else accesses the file from a different IP or browser, he/she will be stopped.
The module doesn't require any configurations. It is always run before other modules.
</p>
EOF;
}
/**
* Creates a first run file and stores IP + browser info
*/
public function run() {
$buff = '';
$this->checkFirstRun();
return $buff;
}
/**
* Checks if the script is access from a different IP or browser.
* It dies with an error
*/
public function checkFirstRun() {
// Creates a (host specific) file which stops people from accessing SAK4WP as the same time as you.
$first_run_file = $this->first_run_file;
$ip = Orbisius_WP_SAK_Util::getIP();
$ua = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
if (file_exists($first_run_file)) {
$data = Orbisius_WP_SAK_Util_File::read($first_run_file, Orbisius_WP_SAK_Util_File::UNSERIALIZE);
// If any of the recorded info is different, we don't want to deal with that person.
if (empty($data) || $data['ip'] != $ip || $data['ua'] != $ua) {
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$ctrl->doExit('Error');
}
} else {
$creation_time = time();
$data['ua'] = $ua;
$data['ip'] = $ip;
$data['creation_time'] = $creation_time;
$data['hash'] = sha1($ua . $ip . $creation_time . ORBISIUS_WP_SAK_HOST);
Orbisius_WP_SAK_Util_File::write($first_run_file, $data, Orbisius_WP_SAK_Util_File::SERIALIZE);
}
return true;
}
/**
* Removes the first run file. This file prevents other users from accessing the file.
* Even the browser has to match.
*/
public function clean() {
unlink($this->first_run_file);
}
}
/**
* Example Module - Handles ...
*/
class Orbisius_WP_SAK_Controller_Module_Example extends Orbisius_WP_SAK_Controller_Module {
/**
* Setups the object stuff and defines some descriptions
*/
public function __construct() {
$this->description = <<<EOF
<h4>Example</h4>
<p>This module allows you to ...
</p>
EOF;
}
/**
*
*/
public function run() {
$buff = '';
if (!empty($_REQUEST['cmd'])) {
if ($_REQUEST['cmd'] == 'command') {
$text = empty($_REQUEST['text']) ? substr(sha1(mt_rand(100, 1000) . time()), 0, 6) : trim($_REQUEST['text']);
$this->doSomething($text);
$buff .= "<br/>";
}
}
// Let's show delete button if any if the files exists.
if ($ht_files_exist) {
$buff .= "<p><br/><a href='?page=mod_htaccess&cmd=delete_htaccess' class='app-module-self-destroy-button'
onclick='return confirm('Are you sure?', '');'>delete .htaccess & .htpasswd files.</a></p>\n";
}
return $buff;
}
/**
* Sample Method
*/
public function doSomething($text) {
return $text;
}
}
/**
* Example Module - Handles ...
*/
class Orbisius_WP_SAK_Controller_Module_PostMeta extends Orbisius_WP_SAK_Controller_Module {
/**
* Setups the object stuff and defines some descriptions
*/
public function __construct() {
$this->description = <<<EOF
<h4>Post Meta</h4>
<p>
This module allows you to view and edit post meta information.
</p>
EOF;
$form_js_handler = <<<BUFF_EOF
// Orbisius_WP_SAK_Controller_Module_PostMeta
// Load form.
$('.mod_post_meta_form').submit(function() {
var form = $(this);
var container = '.results_container';
$(container).empty().append("<div class='app-ajax-message app-alert-notice'>loading ...</div>");
var action = 'getPostMetaAjax';
if ( form.hasClass('mod_post_meta_edit_form') ) {
action = 'setPostMetaAjax';
}
if ( form.hasClass('mod_post_meta_delete_form') ) {
action = 'setPostMetaAjax';
}
$.ajax({
type : "post",
dataType : "json",
url : wpsak_json_cfg.ajax_url, // contains all the necessary params
data : $(form).serialize() + '&module=PostMeta&action=' + action,
success : function(json) {
$('.app-ajax-message').remove();
if (json.status) {
$(container).html(json.results);
} else {
$(container).append("<span class='app-ajax-message app-alert-error'>There was an error. Error: "
+ json.message + "</span>");
}
},
error : function(jqXHR, text_status, error_thrown) {
$('.app-ajax-message').remove();
alert("There was an error. " + text_status + ' ' + error_thrown);
},
}); // ajax
return false;
});
// Orbisius_WP_SAK_Controller_Module_PostMeta
BUFF_EOF;
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$ctrl->enqeueOnDocumentReady($form_js_handler);
}
/**
*
*/
public function run() {
$buff = '';
$post_id = empty($_REQUEST['post_id']) ? '' : (int) $_REQUEST['post_id'];
$meta_key = empty($_REQUEST['meta_key']) ? '' : trim($_REQUEST['meta_key']);
$meta_value = empty($_REQUEST['meta_value']) ? '' : trim( $_REQUEST['meta_value'] );
$post_id_esc = esc_attr($post_id);
$meta_key_esc = esc_attr($meta_key);
$meta_value_esc = esc_attr($meta_value);
//$buff .= "<br/><h4>Plugin List from Text/HTML File</h4>\n";
$buff .= "<h4>Load Post Meta</h4>";
$buff .= "<form method='post' class='mod_post_meta_form mod_post_meta_load_form' id='mod_post_meta_load_form'>\n";
$buff .= "<input type='hidden' id='cmd' name='cmd' value='get_post_meta' />\n";
$buff .= "ID: <input type='text' id='post_id' name='post_id' value='$post_id_esc' /> \n";
$buff .= "<input type='submit' class='app-btn-primary' value='Load Post Meta' />\n<br/>";
$buff .= "Examples: <br/>- Enter ID e.g. 1 <br/>- OR 1, 2, 3 <br/>- OR even a page slug e.g. my-service-page\n";
$buff .= "</form>\n";
$buff .= "<hr />\n";
$buff .= "<div id='results_container' class='results_container'></div>\n";
$buff .= "<h4>Edit Post Meta</h4>";
$buff .= "<form method='post' class='mod_post_meta_form mod_post_meta_edit_form' id='mod_post_meta_edit_form'>\n";
$buff .= "<input type='hidden' id='cmd' name='cmd' value='edit_post_meta' />\n";
$buff .= "ID: <input type='text' id='post_id' name='post_id' value='$post_id_esc' /> \n";
$buff .= "Meta Key: <input type='text' id='meta_key' name='meta_key' value='$meta_key_esc' /> \n";
$buff .= "Meta Value: <textarea id='meta_value' name='meta_value' value='$meta_value_esc' rows='4'></textarea>\n";
$buff .= "<input type='submit' class='app-btn-primary' value='Update Post Meta' />\n<br/>";
$buff .= "Examples: <br/>- Enter ID e.g. 1 <br/>- OR 1, 2, 3 <br/>- OR even a page slug e.g. my-service-page\n";
$buff .= "</form>\n";
$buff .= "<hr />\n";
$buff .= "<h4>Delete Post Meta</h4>";
$buff .= "<form method='post' class='mod_post_meta_form mod_post_meta_delete_form' id='mod_post_meta_delete_form'>\n";
$buff .= "<input type='hidden' id='cmd' name='cmd' value='delete_post_meta' />\n";
$buff .= "ID: <input type='text' id='post_id' name='post_id' value='$post_id_esc' /> \n";
$buff .= "Meta Key: <input type='text' id='meta_key' name='meta_key' value='$meta_key_esc' /> \n";
$buff .= "<input type='submit' class='app-btn-primary' value='Delete Post Meta' />\n<br/>";
$buff .= "Examples: <br/>- Enter ID e.g. 1 <br/>- OR 1, 2, 3 <br/>- OR even a page slug e.g. my-service-page\n";
$buff .= "</form>\n";
if (!empty($_REQUEST['cmd'])) {
if ($_REQUEST['cmd'] == 'delete_post_meta') {
$post_id = empty($_REQUEST['post_id']) ? 0 : $_REQUEST['post_id'];
$this->deletePostMeta($post_id, $meta_key);
}
if ($_REQUEST['cmd'] == 'edit_post_meta') {
$this->setPostMeta($post_id, $meta_key, $meta_value);
}
// This should fetch the freshest info.
if ($_REQUEST['cmd'] == 'get_post_meta') {
$buff .= $this->getMetaAsString($post_id);
$buff .= "<br/>";
}
}
return $buff;
}
/**
* This method is called when we have the module and action specified.
*/
public function getPostMetaAjaxAction() {
$msg = '';
$result_html = '';
$status = 1;
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$post_ids_list = $ctrl->getVar('post_id'); // could be 1 or more IDs
$ids = explode(',', $post_ids_list);
$ids = array_map('trim', $ids);
$ids = array_filter($ids); // non empty ones
$ids = array_unique($ids);
foreach ($ids as $post_id) {
// If the user has entered a slug we'll use it to get the post ID
if (!is_numeric($post_id)) {
$post = get_page_by_path($post_id);
if (empty($post)) {
$result_html .= Orbisius_WP_SAK_Util::msg('Path: [' . esc_attr($post_id) . '] was not found. Skipping. <br/>', 0);
continue;
}
$post_id = $post->ID;
}
$result_html .= $this->getMetaAsString($post_id);
}
$result_status = array('status' => $status, 'message' => $msg, 'results' => $result_html, );
$ctrl->sendHeader(Orbisius_WP_SAK_Controller::HEADER_JS, $result_status);
}
/**
* This method is called when we have the module and action specified.
*/
public function setPostMetaAjaxAction() {
$msg = '';
$result_html = '';
$status = 1;
$cmd = isset($_REQUEST['cmd']) ? trim($_REQUEST['cmd']) : null;
$cmd_label = preg_match('#delete#si', $cmd) ? 'Delete Meta' : 'Update Meta';
// raw data
$meta_key = empty($_REQUEST['meta_key']) ? '' : trim($_REQUEST['meta_key']);
$meta_value = isset($_REQUEST['meta_value']) ? trim($_REQUEST['meta_value']) : null;
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$post_ids_list = $ctrl->getVar('post_id'); // could be 1 or more IDs
$ids = explode(',', $post_ids_list);
$ids = array_map('trim', $ids);
$ids = array_filter($ids); // non empty ones
$ids = array_unique($ids);
foreach ($ids as $post_id) {
// If the user has entered a slug we'll use it to get the post ID
if (!is_numeric($post_id)) {
$post = get_page_by_path($post_id);
if (empty($post)) {
$result_html .= Orbisius_WP_SAK_Util::msg('Path: [' . esc_attr($post_id) . '] was not found. Skipping. <br/>', 0);
continue;
}
$post_id = $post->ID;
}
$res = $this->setPostMeta($post_id, $meta_key, $meta_value);
$result_html .= ($res === false)
? Orbisius_WP_SAK_Util::msg($cmd_label . ' Error. post meta for post/page ID: ' . esc_attr($post_id) . '] or the new value matches the old one. <br/>', 0)
: Orbisius_WP_SAK_Util::msg($cmd_label. ' OK meta for post/page ID: ' . esc_attr($post_id) . ']. <br/>', 1);
}
$result_status = array('status' => $status, 'message' => $msg, 'results' => $result_html, );
$ctrl->sendHeader(Orbisius_WP_SAK_Controller::HEADER_JS, $result_status);
}
/**
*
* @param int $post_id
* @param string $meta_key
* @param string $meta_value
* @see https://codex.wordpress.org/Function_Reference/update_post_meta
*/
public function setPostMeta($post_id, $meta_key, $meta_value) {
if ( is_null( $meta_value ) ) {
$mixed_res = delete_post_meta($post_id, $meta_key);
} else {
/*Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure.
* NOTE: If the meta_value passed to this function is the same as the value that is already in the database, this function returns false. */
$mixed_res = update_post_meta($post_id, $meta_key, $meta_value);
}
return $mixed_res;
}
/**
* Sample Method
*/
public function getMetaAsString($post_id) {
$str = '';
$post = get_post($post_id);
$meta = !empty($post)
? get_post_meta($post_id)
: null;
$author_meta = !empty($post->post_author)
? get_user_meta($post->post_author)
: null;
$link_str = '';
if (!empty($post)) {
$link = get_permalink($post_id);
$link_str = "(<a href='$link' target='_blank'>View</a>)";
}
// if the item is one element that means that it's one value
if (count($meta) == 1) {
$meta = $meta[0];
}
$str .= "<h3>Post/Page ID: $post_id $link_str</h3><pre>" . var_export($post, 1) . "</pre>\n";
$str .= '<h3>Post/Page Meta</h3><pre class="toggle_info000">' . var_export($meta, 1) . "</pre>\n";
$str .= '<h3>Author Meta</h3><pre class="toggle_info000">' . var_export($author_meta, 1) . "</pre>\n";
return $str;
}
}
/**
* Example Module - Handles ...
*/
class Orbisius_WP_SAK_Controller_Module_UserMeta extends Orbisius_WP_SAK_Controller_Module {
/**
* Setups the object stuff and defines some descriptions
*/
public function __construct() {
$this->description = <<<EOF
<h4>User Meta</h4>
<p>
This module allows you to view and edit user meta information.
</p>
EOF;
$form_js_handler = <<<BUFF_EOF
// Orbisius_WP_SAK_Controller_Module_UserMeta
// Load form.
$('.mod_user_meta_form').submit(function() {
var form = $(this);
var container = '.results_container';
$(container).empty().append("<div class='app-ajax-message app-alert-notice'>loading ...</div>");
var action = 'getUserMetaAjax';
if ( form.hasClass('mod_user_meta_edit_form') ) {
action = 'setUserMetaAjax';
}
if ( form.hasClass('mod_user_meta_delete_form') ) {
action = 'setUserMetaAjax';
}
$.ajax({
//type : "user",
dataType : "json",
url : wpsak_json_cfg.ajax_url, // contains all the necessary params
data : $(form).serialize() + '&module=UserMeta&action=' + action,
success : function(json) {
$('.app-ajax-message').remove();
if (json.status) {
$(container).html(json.results);
} else {
$(container).append("<span class='app-ajax-message app-alert-error'>There was an error. Error: "
+ json.message + "</span>");
}
},
error : function(jqXHR, text_status, error_thrown) {
$('.app-ajax-message').remove();
alert("There was an error. " + text_status + ' ' + error_thrown);
},
}); // ajax
return false;
});
// Orbisius_WP_SAK_Controller_Module_UserMeta
BUFF_EOF;
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$ctrl->enqeueOnDocumentReady($form_js_handler);
}
/**
*
*/
public function run() {
$buff = '';
$user_id = empty($_REQUEST['user_id']) ? '' : (int) $_REQUEST['user_id'];
$meta_key = empty($_REQUEST['meta_key']) ? '' : trim($_REQUEST['meta_key']);
$meta_value = empty($_REQUEST['meta_value']) ? '' : trim( $_REQUEST['meta_value'] );
$user_id_esc = esc_attr($user_id);
$meta_key_esc = esc_attr($meta_key);
$meta_value_esc = esc_attr($meta_value);
//$buff .= "<br/><h4>Plugin List from Text/HTML File</h4>\n";
$buff .= "<h4>Load User Meta</h4>";
$buff .= "<form method='post' class='mod_user_meta_form mod_user_meta_load_form' id='mod_user_meta_load_form'>\n";
$buff .= "<input type='hidden' id='cmd' name='cmd' value='get_user_meta' />\n";
$buff .= "ID: <input type='text' id='user_id' name='user_id' value='$user_id_esc' /> \n";
$buff .= "<input type='submit' class='app-btn-primary' value='Load User Meta' />\n<br/>";
$buff .= "Examples: <br/>- Enter ID e.g. 1 <br/>- OR 1, 2, 3 <br/>- OR even a page slug e.g. my-service-page\n";
$buff .= "</form>\n";
$buff .= "<hr />\n";
$buff .= "<div id='results_container' class='results_container'></div>\n";
$buff .= "<h4>Edit User Meta</h4>";
$buff .= "<form method='post' class='mod_user_meta_form mod_user_meta_edit_form' id='mod_user_meta_edit_form'>\n";
$buff .= "<input type='hidden' id='cmd' name='cmd' value='edit_user_meta' />\n";
$buff .= "ID: <input type='text' id='user_id' name='user_id' value='$user_id_esc' /> \n";
$buff .= "Meta Key: <input type='text' id='meta_key' name='meta_key' value='$meta_key_esc' /> \n";
$buff .= "Meta Value: <textarea id='meta_value' name='meta_value' value='$meta_value_esc' rows='4'></textarea>\n";
$buff .= "<input type='submit' class='app-btn-primary' value='Update User Meta' />\n<br/>";
$buff .= "Examples: <br/>- Enter ID e.g. 1 <br/>- OR 1, 2, 3 <br/>- OR even a page slug e.g. my-service-page\n";
$buff .= "</form>\n";
$buff .= "<hr />\n";
$buff .= "<h4>Delete User Meta</h4>";
$buff .= "<form method='post' class='mod_user_meta_form mod_user_meta_delete_form' id='mod_user_meta_delete_form'>\n";
$buff .= "<input type='hidden' id='cmd' name='cmd' value='delete_user_meta' />\n";
$buff .= "ID: <input type='text' id='user_id' name='user_id' value='$user_id_esc' /> \n";
$buff .= "Meta Key: <input type='text' id='meta_key' name='meta_key' value='$meta_key_esc' /> \n";
$buff .= "<input type='submit' class='app-btn-primary' value='Delete User Meta' />\n<br/>";
$buff .= "Examples: <br/>- Enter ID e.g. 1 <br/>- OR 1, 2, 3 <br/>- OR even a page slug e.g. my-service-page\n";
$buff .= "</form>\n";
if (!empty($_REQUEST['cmd'])) {
if ($_REQUEST['cmd'] == 'delete_user_meta') {
$user_id = empty($_REQUEST['user_id']) ? 0 : $_REQUEST['user_id'];
$this->deleteUserMeta($user_id, $meta_key);
}
if ($_REQUEST['cmd'] == 'edit_user_meta') {
$this->setUserMeta($user_id, $meta_key, $meta_value);
}
// This should fetch the freshest info.
if ($_REQUEST['cmd'] == 'get_user_meta') {
$buff .= $this->getMetaAsString($user_id);
$buff .= "<br/>";
}
}
return $buff;
}
/**
* This method is called when we have the module and action specified.
*/
public function getUserMetaAjaxAction() {
$msg = '';
$result_html = '';
$status = 1;
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$user_ids_list = $ctrl->getVar('user_id'); // could be 1 or more IDs
$ids = explode(',', $user_ids_list);
$ids = array_map('abs', $ids);
$ids = array_map('trim', $ids);
$ids = array_filter($ids); // non empty ones
$ids = array_unique($ids);
foreach ($ids as $user_id) {
$result_html .= $this->getMetaAsString($user_id);
}
$result_status = array('status' => $status, 'message' => $msg, 'results' => $result_html, );
$ctrl->sendHeader(Orbisius_WP_SAK_Controller::HEADER_JS, $result_status);
}
/**
* This method is called when we have the module and action specified.
*/
public function setUserMetaAjaxAction() {
$msg = '';
$result_html = '';
$status = 1;
$cmd = isset($_REQUEST['cmd']) ? trim($_REQUEST['cmd']) : null;
$cmd_label = preg_match('#delete#si', $cmd) ? 'Delete Meta' : 'Update Meta';
// raw data
$meta_key = empty($_REQUEST['meta_key']) ? '' : trim($_REQUEST['meta_key']);
$meta_value = isset($_REQUEST['meta_value']) ? trim($_REQUEST['meta_value']) : null;
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$user_ids_list = $ctrl->getVar('user_id'); // could be 1 or more IDs
$ids = explode(',', $user_ids_list);
$ids = array_map('trim', $ids);
$ids = array_map('abs', $ids);
$ids = array_filter($ids); // non empty ones
$ids = array_unique($ids);
foreach ($ids as $user_id) {
$res = $this->setUserMeta($user_id, $meta_key, $meta_value);
$result_html .= ($res === false)
? Orbisius_WP_SAK_Util::msg($cmd_label . ' Error. user meta for user ID: ' . esc_attr($user_id) . '] or the new value matches the old one. <br/>', 0)
: Orbisius_WP_SAK_Util::msg($cmd_label. ' OK meta for user ID: ' . esc_attr($user_id) . ']. <br/>', 1);
}
$result_status = array('status' => $status, 'message' => $msg, 'results' => $result_html, );
$ctrl->sendHeader(Orbisius_WP_SAK_Controller::HEADER_JS, $result_status);
}
/**
*
* @param int $user_id
* @param string $meta_key
* @param string $meta_value
* @see https://codex.wordpress.org/Function_Reference/update_user_meta
*/
public function setUserMeta($user_id, $meta_key, $meta_value = null) {
if ( is_null( $meta_value ) ) {
$mixed_res = delete_user_meta($user_id, $meta_key);
} else {
/*Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure.
* NOTE: If the meta_value passed to this function is the same as the value that is already in the database, this function returns false. */
$mixed_res = update_user_meta($user_id, $meta_key, $meta_value);
}
return $mixed_res;
}
/**
* Sample Method
*/
public function getMetaAsString($user_id) {
$str = '';
$user_id = abs($user_id);
$user = get_user_by( 'id', $user_id);
$meta = !empty($user)
? get_user_meta($user_id)
: null;
$link_str = '';
// if the item is one element that means that it's one value
if (count($meta) == 1) {
$meta = $meta[0];
}
$str .= "<h3>User ID: $user_id $link_str</h3><pre>" . var_export($user, 1) . "</pre>\n";
$str .= '<h3>User Meta</h3><pre class="toggle_info000">' . var_export($meta, 1) . "</pre>\n";
return $str;
}
}
/**
* Example Module - Handles ...
*/
class Orbisius_WP_SAK_Controller_Module_User_Manager extends Orbisius_WP_SAK_Controller_Module {
/**
* Setups the object stuff and defines some descriptions.
* If user ID is present it will call method so the method can execute before any content is out.
*/
public function __construct() {
if (!empty($_REQUEST['user_id'])) {
$this->loginAs();
}
$this->description = <<<EOF
<h4>User Manager</h4>
<p>This module allows you to see user account, user meta info, to log in as a user without knowing their password. TODO: Create, delete users.
Administrator accounts are highlighted.</p>
EOF;
$current_user = wp_get_current_user();
if ( empty( $_REQUEST['load_user_meta'] ) ) {
$this->description .= " <a href='?page=mod_user_manager&load_user_meta=1'>Load user meta</a> | ";
} else {
$this->description .= " <a href='?page=mod_user_manager&load_user_meta=0'>Skip user meta loading</a> | ";
}
if (!empty($current_user->ID)) {
$this->description .= "<span class='app_logged_in app-simple-alert-success'>Currently Logged User: $current_user->display_name
[$current_user->user_email] (ID: $current_user->ID)</span>";
} else {
$this->description .= "<span class='app_not_logged_in'>Currently Logged User: (none)</span>";
}
$this->description .= "<br/>";
}
/**
* Lists users and their meta info.
* The user info is dumped on the screen. TODO: make it look pretty.
* This listing also allows the user of SAK4WP to login as given user.
*/
public function run() {
$buff = '';
$args = array(
'number' => 250,
'orderby' => 'registered',
);
$data = get_users( $args );
$records = array();
$highlight_admins = array();
foreach ($data as $idx => $user_obj) {
$rec = (array) $user_obj->data;
// Let's remove those fields because the table can't fit more than 5
unset($rec['user_url']);
unset($rec['user_pass']);
unset($rec['user_status']);
unset($rec['user_activation_key']);
// This allows us to swich the user to a different one.
$rec['user_login'] .= " (<a href='?page=mod_user_manager&user_id=$user_obj->ID'>Login</a>)";
if ( ! empty( $_REQUEST['load_user_meta'] ) ) {
$user_meta_html = '<pre class="toggle_info app_hide">' . var_export( get_user_meta( $user_obj->ID ), 1 ) . "</pre>\n";
$rec['ID'] .= " (<a href='javascript:void(0);' class='toggle_info_trigger'>show/hide meta</a>)\n" . $user_meta_html;
}
if ( user_can( $user_obj->ID, 'manage_options' ) ) {
$highlight_admins[] = $idx;
$rec['user_login'] .= ' (admin)';
}
$records[] = $rec;
}
$buff .= "<p class='results'></p>\n";
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$buff .= $ctrl->renderTable('Users: ' . count($data), '', $records, $highlight_admins);
return $buff;
}
/**
* This method auto logins in the user with certain user ID (int)
* After the user is logged in this will redirect again to the User Manager's page.
*/
public function loginAs() {
$user_id = empty($_REQUEST['user_id']) ? 0 : intval($_REQUEST['user_id']);
wp_set_auth_cookie( $user_id, false, is_ssl() );
wp_redirect('?page=mod_user_manager&logged_in_as=' . $user_id);
exit;
}
}
/**
* Plugin_Manager Module - Allows you to manage plugins: bulk install, de/activate, delete
*/
class Orbisius_WP_SAK_Controller_Module_Plugin_Manager extends Orbisius_WP_SAK_Controller_Module {
private $target_dir = ''; // plugins directory i.e. wp-content/plugins/
/**
* Setups the object stuff and defines some descriptions
*/
public function __construct() {
$this->target_dir = WP_PLUGIN_DIR;
$warning = Orbisius_WP_SAK_Util::msg("If the plugin already exists its files will be overridden!", 0);
$this->description = <<<EOF
<h4>Plugin Manager</h4>
<p>This module allows you to manage plugins: bulk download, install, and activate plugins. TODO: (de)activate, delete. Just enter the plugin's WordPress.org page
or a zip file location (web). Enter multiple links each on a new line. Warning: If the plugin already exists its files will be overridden!
<br/> Plugins will be extracted in: <strong>$this->target_dir</strong> <br/>
$warning
</p>
EOF;
}
/**
*
*/
public function run() {
$buff = '';
$download_list_url = empty($_REQUEST['download_list_url']) ? '' : trim($_REQUEST['download_list_url']);
$download_list_url_esc = esc_attr($download_list_url);
$download_list_buff = empty($_REQUEST['download_list_buff']) ? '' : trim($_REQUEST['download_list_buff']);
$download_list_buff_esc = esc_attr($download_list_buff);
$buff .= "<br/><h4>Plugin List from Text/HTML File</h4>\n";
$buff .= "<p>Download Plugin list from a text file (e.g. from the public folder of your dropbox account, on your site etc.).</p>\n";
$buff .= "<form method='post' id='mod_plugin_manager_download_list_form'>\n";
$buff .= "<input type='text' name='download_list_url' id='download_list_url' class='app_full_width' value='$download_list_url_esc' />\n";
$buff .= "<input type='submit' name='submit' class='app-btn-primary' value='Download Plugin List' />\n";
$buff .= "</form>\n";
$buff .= "<p class='download_list_results'></p>\n";
$buff .= "<br/><h4>Plugin Page Links</h4>\n";
$buff .= "<p>You can enter direct links to .zip files and/or plugin pages on WordPress.org only</p>\n";
$buff .= "<form method='post' id='mod_plugin_manager_download_plugins_form'>\n";
$buff .= "<textarea name='download_list_buff' id='download_list_buff' class='app_full_width' rows='8'>$download_list_buff_esc</textarea>\n";
$buff .= "<input type='checkbox' id='activate_plugins' name='activate_plugins' class='app-btn-primary' value='1' />
<label for='activate_plugins'>Activate plugin(s) after installation</label><br/>\n";
$buff .= "<input type='submit' name='submit' class='app-btn-primary' value='Download & Extract' />\n";
$buff .= "</form>\n";
//$buff .= "<p><br/><a href='?page=mod_locate_wp&cmd=search' class='app-btn-primary mod_search_for_wordpress'>Search</a></p>\n";
$buff .= "<p class='results'></p>\n";
return $buff;
}
/**
* This is called via ajax and downloads some plugins and extracts the zip files' contents into plugins folder.
* The result is JSON
*/
public function downloadAction() {
$msg = '';
$ctrl = Orbisius_WP_SAK_Controller::getInstance();
$params = $ctrl->getParams();
$download_list_buff = empty($params['download_list_buff']) ? '' : trim($params['download_list_buff']);
$activate_plugins = empty($params['activate_plugins']) ? 0 : 1;
$locations = preg_split('#[\r\n]+#si', $download_list_buff); // let's split things by new lines.
$locations = array_map('trim', $locations); // no spaces
$locations = array_unique($locations); // only unique links
$locations = array_filter($locations); // skip empty lines
if (!empty($locations)) {
$plugins_dir = $this->target_dir;
$req_cnt = 0;
$limit = 10 * 1024 * 1024; // 10MB
$limit_fmt = Orbisius_WP_SAK_Util::formatFileSize($limit);
foreach ($locations as $link) {
if (empty($link)) {
continue;
}
// If the link points to a plugin hosted by wordpress.org go and find the download link.
// otherwise we'll skip them because SAK4WP can find other random ZIP files or bad plugins
// which could break WP. There could be some www
if (preg_match('#https?://[w\.]*wordpress.org/(?:extend/)?plugins/#si', $link)
&& !preg_match('#\.zip$#si', $link)) {
// let's load WP page
$result = Orbisius_WP_SAK_Util::makeHttpRequest($link);
$org_link_esc = esc_attr($link);
if (empty($result['debug']['http_code']) || $result['debug']['http_code'] != 200) {
$result_html .= Orbisius_WP_SAK_Util::msg("Couldn't Download Link: HTTP Code: " . $result['debug']['http_code'], 2);
} elseif (empty($result['error'])) {
$body_buff = $result['buffer'];
// the download link may contain alphanumeric + some versioning.
// e.g. http://downloads.wordpress.org/plugin/orbisius-cyberstore.1.1.7.zip
if (preg_match('#(https?://downloads.wordpress.org/plugin/(?:[\w-.]+).zip)#si', $body_buff, $matches)) {
$link = $matches[1];
$result_html .= Orbisius_WP_SAK_Util::msg("Found Download Link: [$org_link_esc => $link]", 2);
}
} else {
$result_html .= Orbisius_WP_SAK_Util::msg("Couldn't Find Plugin Download Link: [$link]", 2);
}
// let's give the server a chance to relax for a sec.
if (++$req_cnt % 5 == 0) {
sleep(1);
}
}
$link_esc = esc_attr($link);
// skip links not ending in .zip for now.
if (!preg_match('#\.zip$#si', $link)) {
$result_html .= Orbisius_WP_SAK_Util::msg("Skipping link: [$link_esc]. Reason: doesn't end in .zip");
continue;
} else {
$dl_status = null;
$extract_status = null;
$result_html .= Orbisius_WP_SAK_Util::msg("Processing link: [$link_esc]", 2);
// Let's do a quick check
$remote_file_size = Orbisius_WP_SAK_Util::getRemoteFileSize($link);
// The max plugin size is 10MB
if ($remote_file_size > $limit) {
$file_size_fmt = Orbisius_WP_SAK_Util::formatFileSize($remote_file_size);
$result_html .= Orbisius_WP_SAK_Util::msg("Download Failed: [$link_esc, $file_size_fmt]."
. " Plugin file is bigger than $limit_fmt.", 0);
} else {
$dl_status = Orbisius_WP_SAK_Util::downloadFile($link);
if (empty($dl_status['status'])
|| empty($dl_status['debug']['http_code'])
|| $dl_status['debug']['http_code'] != 200) {