Repository navigation
Expand file tree
/
Copy pathProcessAdminActions.module.php
More file actions
1991 lines (1763 loc) · 97.4 KB
/
Copy pathProcessAdminActions.module.php
File metadata and controls
1991 lines (1763 loc) · 97.4 KB
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 namespace ProcessWire;
/**
* ProcessWire Admin Actions.
* by Adrian Jones
*
* Copyright (C) 2026 by Adrian Jones
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
*/
class ProcessAdminActions extends Process implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Admin Actions',
'summary' => 'Control panel for running various admin actions',
'author' => 'Adrian Jones',
'version' => '1.1.0',
'singular' => true,
'autoload' => false,
'icon' => 'wrench',
'useNavJSON' => true,
'page' => array(
'name' => 'admin-actions',
'parent' => 'setup',
'title' => 'Admin Actions'
),
'permission' => 'page-view',
'permissionMethod' => 'permissionCheck',
'permissions' => array(
'admin-actions' => 'Run selected AdminActions actions',
'admin-actions-restore' => 'Run AdminActions restore feature'
)
);
}
/**
* Data as used by the get/set functions
*
*/
protected $data = array();
protected $action;
protected $actions = array();
protected $actionTypes = array();
protected $hasPermission = false;
protected $adminActionsCacheDir;
protected $dbBackupFilename;
protected $dbBackup;
protected $executionMode;
protected $noPermissionMessage = 'Sorry, you do not have permission to use this action.';
protected $processPageUrl;
protected $newActionsCount = 0;
protected $downloadStaged = false;
/**
* Batched execution (lets long actions get past host request time limits).
* Actions read $this->state (null on the first batch), check $this->timeUp() in their
* loop, and return $this->continueWith($state) to have the next batch run in a new request.
*/
protected $state = null;
protected $batchTimeLimit = 90;
protected $batchStartTime = null;
protected $continueState = null;
protected $progressMessage = '';
/**
* True when this batch has used up its time budget ($batchTimeLimit seconds).
* Always false when the action isn't running as a browser AJAX run, since there's
* no request time limit to avoid in that case (eg. when called via the API).
*
* @return bool
*/
protected function timeUp() {
if($this->batchStartTime === null) return false;
return (microtime(true) - $this->batchStartTime) >= $this->batchTimeLimit;
}
/**
* End this batch and have the action run again in a new request, with $this->state set to $state
*
* @param mixed $state Anything JSON-serializable (eg. array('offset' => 500))
* @param string $progress Optional text shown in the status line while running, eg. "Processed 500 of 2000 pages"
* @return bool
*/
protected function continueWith($state, $progress = '') {
$this->continueState = $state === null ? array() : $state;
$this->progressMessage = (string) $progress;
return true;
}
/**
* Populate the default config data
*
*/
public function __construct() {
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
$this->adminActionsCacheDir = $this->wire('config')->paths->cache . 'AdminActions/';
$this->dbBackupFilename = 'AdminActionsBackup.sql';
$id = $this->wire('modules')->getModuleID("ProcessAdminActions");
$this->processPageUrl = $this->wire('pages')->get("process=$id")->url;
}
public function ___executeNavJSON(array $options = array()) {
$options['items'] = array();
$i=0;
if($this->wire('user')->isSuperuser() || $this->wire('user')->hasPermission('admin-actions-restore')) {
$options['items'][$i]['id'] = '#tab_restore';
$options['items'][$i]['label'] = 'Restore';
$options['items'][$i]['icon'] = 'reply';
$i++;
}
$userRoleIds = $this->wire('user')->roles->each("id");
foreach($this->data as $actionType => $actions) {
if(is_array($actions)) {
foreach($actions as $action => $info) {
if(isset($info['menu']) && $info['menu'] == '1' && isset($info['roles']) && count(array_intersect($info['roles'], $userRoleIds)) !== 0) {
$options['items'][$i]['id'] = 'options?action='.$action;
$options['items'][$i]['label'] = $this->getActionTitle($action, $info);
$options['items'][$i]['icon'] = isset($info['icon']) ? $info['icon'] : '';
$i++;
}
}
}
}
$options['itemLabel'] = 'label';
$options['edit'] = $this->processPageUrl . '{id}';
$options['add'] = null;
$options['iconKey'] = 'icon';
$options['sort'] = false;
return parent::___executeNavJSON($options);
}
public static function permissionCheck(array $data) {
$user = $data['user'];
$wire = $data['wire'];
// if in admin/backend, then require "admin-actions" permission
if(strpos($wire->input->url, $wire->config->urls->admin) === 0 && !$user->hasPermission('admin-actions')) {
return false;
}
// else in frontend, so allow full access to call actions via the API
else {
return true;
}
}
/**
* Default configuration for module
*
*/
static public function getDefaultData() {
return array(
// intentionally blank
// this method is required by ProcessWire, but we don't use it for this module
// because defaults are saved as multidimensional JSON on install
);
}
public function init() {
parent::init();
$this->wire('modules')->get("JqueryWireTabs");
$this->getActionTypes();
if($this->wire('input')->get->action) {
$actionName = $this->wire('input')->get->action;
$userRoleIds = $this->wire('user')->roles->each("id");
if(isset($this->actionTypes[$actionName]) && isset($this->data[$this->actionTypes[$actionName]][$actionName]['roles']) && count(array_intersect($this->data[$this->actionTypes[$actionName]][$actionName]['roles'], $userRoleIds)) !== 0) {
if($this->includeAndInstantiate($actionName)) {
$this->hasPermission = true;
$this->addHookAfter('Process::breadcrumb', $this, 'modifyBreadcrumb');
}
else {
$this->noPermissionMessage = sprintf($this->_('The file for the %s action is missing. Re-save the module config settings to remove it from the list of available actions.'), $actionName);
}
}
else {
return $this->noPermissionMessage;
}
}
$this->dbBackup = isset($this->action->dbBackup) ? $this->action->dbBackup : (isset($this->data['dbBackup']) ? $this->data['dbBackup'] : 'automatic');
$this->executionMode = $this->getExecutionMode();
}
/**
* Executed when root url for module is accessed
*
*/
public function ___execute() {
$form = $this->buildSelectForm();
$links = '';
if($this->wire('user')->isSuperuser()) {
$links = '<div id="links">';
$links .= "<a href='" . $this->wire('config')->urls->admin . "module/edit?name=".$this."&collapse_info=1'><i class='fa fa-cog'></i> Settings</a> ";
$links .= "<a href='https://processwire.com/talk/topic/14921-admin-actions/' target='_blank'><i class='fa fa-comments'></i> Support</a>";
$links .= '</div>';
}
if($this->wire('input')->post->submit) {
return $links . $this->processSelectForm($form);
}
else {
return $links . (is_string($form) ? $form : $form->render());
}
}
/**
* Executed when ./options/ url for module is accessed
*
*/
public function ___executeOptions() {
if(!$this->hasPermission) return $this->noPermissionMessage;
// Pass background execution config to JS
$this->wire('config')->js('adminActions', array(
'maxExecutionTime' => (int) ini_get('max_execution_time'),
'pollUrl' => $this->wire('config')->urls->assets . 'AdminActions/runs/',
'cleanupUrl' => $this->processPageUrl . 'cleanup/',
'aliveUrl' => $this->processPageUrl . 'alive/',
));
$out = '<h2>'.$this->getActionTitle($this->wire('input')->get->action, $this->data[$this->actionTypes[$this->wire('input')->get->action]][$this->wire('input')->get->action]).'</h2>';
if(method_exists($this->action,'checkRequirements') && !$this->action->checkRequirements()) {
$out .= '<div class="adminActionsError"><p>Sorry, this action cannot proceed. ' . $this->action->requirementsMessage . '</p></div>';
}
else {
$form = $this->buildOptionsForm();
if(!$this->wire('input')->post->submit) {
$out .= is_string($form) ? $form : $form->render();
}
}
return $out;
}
/**
* Executed when ./run/ url for module is accessed
*
*/
public function ___executeRun() {
set_time_limit(0);
if(!$this->hasPermission) return $this->noPermissionMessage;
$actionTitle = $this->getActionTitle($this->wire('input')->get->action, $this->data[$this->actionTypes[$this->wire('input')->get->action]][$this->wire('input')->get->action]);
$form = $this->buildOptionsForm();
// build up $options array to pass to the action's executeAction method
$options = array();
// If the number of URL parameters (get) matches the number of required option fields in the form,
// then we want to execute with the url parameters.
// Note that the URL ($input->get) contains the additional "action", so start with $i=1
$i=1;
foreach($form->getAll() as $field) {
if($field->hasClass('required')) $i++;
}
$numGetParams = count($this->wire('input')->get);
if($numGetParams > 1 && $numGetParams === $i) {
$urlFields = new WireInputData();
foreach($this->wire('input')->get as $field => $value) {
// exclude fields that don't exist in the form, like the "action" url parameter
if(!is_object($form->get($field)) || !$form->get($field)->parent) continue;
// if field expects an array (multiple) then we need to convert the URL parameter
if($form->get($field)->multiple) {
if(strpos($value, '|') !== false) {
$value = explode('|', $value);
}
else {
$value = array($value);
}
}
$urlFields[$field] = $value;
}
// disable CSRF because we are using GET data
$form->protectCSRF = false;
$form->processInput($urlFields);
$form->protectCSRF = true;
foreach($form->getAll() as $field) {
$value = $form->get($field->name)->value;
$options[$field->name] = $value;
}
}
elseif(count($this->wire('input')->post) === 0) {
return '<h2>' . $actionTitle . '</h2><p>Some required form options are missing.</p>';
}
else {
$form->processInput($this->wire('input')->post);
foreach($form->getAll() as $field) {
$options[$field->name] = $form->get($field->name)->value;
}
}
// continuation batches of a batched run (backup was already done by batch 1)
$batch = $this->wire('config')->ajax ? max(1, (int) $this->wire('input')->post->batch) : 1;
$wantBackup = $this->dbBackup == 'automatic' || ($this->dbBackup == 'optional' && $form->get("dbBackup")->value && (bool)$form->get("dbBackup")->value === true);
// CLI execution mode: hand the run to a separate CLI process (no web request time limit applies to it)
if($this->executionMode === 'cli' && $batch === 1 && $this->wire('config')->ajax && $this->wire('input')->post->runId && !$form->getErrors()) {
$this->startCliRun($this->wire('input')->post->runId, $wantBackup);
}
// backup database before executing action
$restoreLink = '';
if($batch === 1 && $wantBackup) {
$this->backupDb();
$restoreLink = $this->getRestoreLink();
}
// if form errors, render the form without executing it
if($form->getErrors()) {
return '<h2>' . $actionTitle . '</h2>' . $form->render();
}
// Background execution support: if AJAX request with runId, make PHP resilient
// to connection drops so the action continues running even if the browser/proxy times out
$runId = '';
$runStatusDir = $this->wire('config')->paths->assets . 'AdminActions/runs/';
$runCacheDir = $this->wire('config')->paths->cache . 'AdminActions/runs/';
if($this->wire('config')->ajax && $this->wire('input')->post->runId) {
$runId = $this->sanitizeRunId($this->wire('input')->post->runId);
if($runId) {
$this->prepareRunDirs();
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'running')));
ignore_user_abort(true);
// Snapshot the session so changes the action makes to $session can be written back
// afterwards — closing it here means PHP would otherwise discard them
$sessionSnapshot = isset($_SESSION) ? $_SESSION : array();
session_write_close();
// Release TracyDebugger's FileSession lock if present — it holds LOCK_EX
// for the entire request and blocks other tabs sharing the tracy-session cookie
if(class_exists('Tracy\Debugger', false)) {
try {
$tracySessionStorage = \Tracy\Debugger::getSessionStorage();
if($tracySessionStorage instanceof \Tracy\FileSession) {
$ref = new \ReflectionProperty($tracySessionStorage, 'file');
// required on PHP < 8.1 to read a private property; a no-op since 8.1 and deprecated in 8.5
if(PHP_VERSION_ID < 80100) $ref->setAccessible(true);
$fileHandle = $ref->getValue($tracySessionStorage);
if(is_resource($fileHandle)) {
flock($fileHandle, LOCK_UN);
}
}
} catch(\Throwable $e) {
// Tracy internals changed - releasing the lock is an optimization, not a requirement
}
}
// Error/exception handlers to write failures to run status files
$writeRunError = function($message) use ($runId, $runStatusDir, $runCacheDir) {
$result = array('status' => 'error', 'output' => '<div class="adminActionsError"><p>' . $message . '</p></div>');
file_put_contents($runCacheDir . $runId . '.json', json_encode($result));
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'error')));
};
set_error_handler(function($errno, $errstr, $errfile, $errline) use ($writeRunError) {
if(error_reporting() === 0) return true;
$writeRunError(htmlspecialchars("Error: $errstr on line $errline in $errfile", ENT_QUOTES, 'UTF-8'));
return false; // let PHP's default handler continue
});
set_exception_handler(function($e) use ($writeRunError) {
$writeRunError(htmlspecialchars('Exception: ' . $e->getMessage() . ' on line ' . $e->getLine() . ' in ' . $e->getFile(), ENT_QUOTES, 'UTF-8'));
});
// Catch fatal errors during action execution
register_shutdown_function(function() use ($runId, $runStatusDir, $runCacheDir) {
$statusFile = $runStatusDir . $runId . '.json';
if(file_exists($statusFile)) {
$status = json_decode(file_get_contents($statusFile), true);
if(isset($status['status']) && $status['status'] === 'running') {
$error = error_get_last();
$errorMsg = $error ? htmlspecialchars($error['message'], ENT_QUOTES, 'UTF-8') : 'Unknown error';
$result = array('status' => 'error', 'output' => '<div class="adminActionsError"><p>Fatal error: ' . $errorMsg . '</p></div>');
file_put_contents($runCacheDir . $runId . '.json', json_encode($result));
file_put_contents($statusFile, json_encode(array('status' => 'error')));
}
}
});
// Garbage collect old run files (older than 1 hour) — throttled to ~10% of requests
if(mt_rand(1, 10) === 1) {
// Flat dirs: status/cache files keyed by runId
foreach(array($runStatusDir, $runCacheDir) as $runDir) {
if(is_dir($runDir)) {
foreach(new \DirectoryIterator($runDir) as $file) {
if($file->isDot() || $file->getFilename() === '.htaccess') continue;
if(time() - $file->getMTime() > 3600) @unlink($file->getPathname());
}
}
}
// Nested dir: per-runId subdirectories holding download file + sidecar
$downloadsDir = $this->wire('config')->paths->cache . 'AdminActions/downloads/';
if(is_dir($downloadsDir)) {
foreach(new \DirectoryIterator($downloadsDir) as $subdir) {
if($subdir->isDot() || !$subdir->isDir()) continue;
if(time() - $subdir->getMTime() > 3600) {
$subdirPath = $subdir->getPathname();
foreach(new \DirectoryIterator($subdirPath) as $f) {
if($f->isDot()) continue;
@unlink($f->getPathname());
}
@rmdir($subdirPath);
}
}
}
}
// Primary download capture: hook WireHttp::sendFile to short-circuit it and
// stage the file ourselves. PW's sendFile() calls @ob_end_clean() before output
// (WireHttp.php ~1532) which would silently discard the ob_start() buffer below
// without invoking its callback, so capturing $files->send() requires intercepting
// upstream of the buffer teardown.
$this->addDownloadCaptureHook($runId, $runStatusDir, $runCacheDir, $sessionSnapshot);
// Fallback download capture: catches actions that hand-roll
// header('Content-Disposition: attachment') + echo/readfile instead of using $files->send().
ob_start(function($buffer, $phase) use ($runId, $runStatusDir, $runCacheDir) {
return $this->handleOutputBuffer($buffer, $phase, $runId, $runStatusDir, $runCacheDir);
});
}
}
// Clear any prior PHP error so handleOutputBuffer's error_get_last() check
// only sees errors raised during the action itself.
error_clear_last();
// execute action and render output
$heading = $runId ? '' : '<h2>' . $actionTitle . '</h2>';
if($runId) {
// Each batch of a batched run is its own request; state, output so far and the
// restore link are carried between them in a server-side file keyed by runId
$stateFile = $runCacheDir . $runId . '.state.json';
$priorOutput = '';
if($batch > 1) {
$saved = is_file($stateFile) ? json_decode(file_get_contents($stateFile), true) : null;
if(!is_array($saved) || !array_key_exists('state', $saved)) {
$writeRunError('Unable to continue the action: the saved state from the previous batch is missing.');
header('Content-Type: application/json');
echo file_get_contents($runCacheDir . $runId . '.json');
exit;
}
$this->action->state = $saved['state'];
$priorOutput = (string) $saved['output'];
$restoreLink = (string) $saved['restoreLink'];
}
// count from the start of the request so bootstrap/backup time is included
$this->action->batchStartTime = isset($_SERVER['REQUEST_TIME_FLOAT']) ? (float) $_SERVER['REQUEST_TIME_FLOAT'] : microtime(true);
$exceptionHtml = '';
try {
$success = $this->action->executeAction($options);
} catch(\Throwable $e) {
// Without this, PHP \Errors (eg. calling an undefined function) reach no logger and the
// browser only sees whatever output came before them, so the run looks like it silently failed
$success = false;
$this->action->continueState = null;
$exceptionHtml = $this->handleActionException($e);
}
// Anything the action printed (echo, var_dump, Tracy's d()/db()) would otherwise land in
// front of the JSON response and break it - show it with the action's output instead
$this->action->output = $this->takeStrayOutput() . $this->action->output . $exceptionHtml;
if($success && $this->action->continueState !== null && !$form->getErrors() && !$this->downloadStaged) {
$stateJson = json_encode(array(
'state' => $this->action->continueState,
'output' => $priorOutput . $this->action->output,
'restoreLink' => $restoreLink,
));
if($stateJson === false || file_put_contents($stateFile, $stateJson) === false) {
$writeRunError('Unable to save the state passed to continueWith() - it must be JSON-serializable.');
$this->saveSessionChanges($sessionSnapshot);
header('Content-Type: application/json');
echo file_get_contents($runCacheDir . $runId . '.json');
exit;
}
$this->saveSessionChanges($sessionSnapshot);
$resultData = array('status' => 'continue', 'batch' => $batch, 'progress' => $this->action->progressMessage);
file_put_contents($runCacheDir . $runId . '.json', json_encode($resultData));
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'complete')));
header('Content-Type: application/json');
echo json_encode($resultData);
exit;
}
@unlink($stateFile);
$this->action->output = $priorOutput . $this->action->output;
}
else {
$success = $this->runActionBatches($options);
}
if($success && !$form->getErrors()) {
$resultHtml = $heading . $this->getResultHtml(true, $actionTitle, $restoreLink);
}
// execution errors so return the failure message and render the form again
else {
if($runId) {
// background mode: don't render the form (user can't interact with it via AJAX)
$resultHtml = $this->getResultHtml(false, $actionTitle, $restoreLink);
}
else {
$this->wire()->error('Sorry, the '.$actionTitle.' action could not be completed successfully.');
$resultHtml = $heading . ($this->action->failureMessage ? '<div class="adminActionsError"><p>' . $this->action->failureMessage . '</p></div>' : '') . $restoreLink . '<br /><br />' . $form->render();
}
}
// Write result to files and return JSON for background execution.
// Skip the success write when the sendFile hook already staged a download envelope —
// an action that called $files->send(['exit' => false]) and kept running would otherwise
// overwrite the 'download' envelope with a 'complete' success message here.
if($runId) {
$this->saveSessionChanges($sessionSnapshot);
if(!$this->downloadStaged) {
$resultData = array('status' => 'complete', 'output' => $resultHtml);
file_put_contents($runCacheDir . $runId . '.json', json_encode($resultData));
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'complete')));
header('Content-Type: application/json');
echo json_encode($resultData);
}
exit;
}
return $resultHtml;
}
/**
* Execution mode for the current action: 'browser' or 'cli'.
* An action can override the module setting with: protected $executionMode = 'browser' or 'cli';
*
* @return string
*/
private function getExecutionMode() {
$mode = isset($this->action->executionMode) ? $this->action->executionMode : (isset($this->data['executionMode']) ? $this->data['executionMode'] : 'browser');
return $mode === 'cli' ? 'cli' : 'browser';
}
/**
* Sanitize a run ID from the browser; returns blank if invalid
*
* @param string $runId
* @return string
*/
private function sanitizeRunId($runId) {
$runId = preg_replace('/[^a-zA-Z0-9_.]/', '', (string) $runId);
if($runId === '.' || $runId === '..' || strpos($runId, '..') !== false) return '';
return $runId;
}
/**
* Create the run status (web accessible, for polling) and run cache (private) directories
*
* @return array [statusDir, cacheDir]
*/
private function prepareRunDirs() {
$runStatusDir = $this->wire('config')->paths->assets . 'AdminActions/runs/';
$runCacheDir = $this->wire('config')->paths->cache . 'AdminActions/runs/';
if(!is_dir($runStatusDir)) wireMkdir($runStatusDir, true);
// written every run so installs with an outdated copy (no rewrite rule) get fixed - polling depends on it
file_put_contents($runStatusDir . '.htaccess', "Options -Indexes\n<IfModule mod_rewrite.c>\n RewriteEngine On\n RewriteRule ^(.+)\\.[0-9]+\\.json$ $1.json [L]\n</IfModule>\n<IfModule mod_headers.c>\n Header set Cache-Control \"no-cache, no-store, must-revalidate\"\n</IfModule>\n");
if(!is_dir($runCacheDir)) wireMkdir($runCacheDir, true);
return array($runStatusDir, $runCacheDir);
}
/**
* Restore link shown after an action that made a DB backup, for users allowed to restore
*
* @return string
*/
private function getRestoreLink() {
if(!$this->wire('user')->isSuperuser() && !$this->wire('user')->hasPermission('admin-actions-restore')) return '';
return '<br /><div class="adminActionsWarning"><p>If you find a problem with the changes, you can <a href="'.$this->processPageUrl.'#tab_restore">restore</a> the entire database.</p></div>';
}
/**
* Result HTML for a background (AJAX or CLI) run: the action's output plus its success/failure message
*
* @param bool $success
* @param string $actionTitle
* @param string $restoreLink
* @return string
*/
private function getResultHtml($success, $actionTitle, $restoreLink) {
if($success) {
$message = $this->action->successMessage == '' ? 'The '.$actionTitle.' action was completed successfully.' : $this->action->successMessage;
return $this->action->output . '<div class="adminActionsSuccess"><p>' . $message . '</p></div>' . $restoreLink;
}
$message = $this->action->failureMessage ?: 'Sorry, the '.$actionTitle.' action could not be completed successfully.';
return $this->action->output . '<div class="adminActionsError"><p>' . $message . '</p></div>' . $restoreLink;
}
/**
* Write a background run's result: cache file first, then flip the status file (polling gates on status)
*
* @param string $runId
* @param array $result
*/
private function writeRunResult($runId, array $result) {
list($runStatusDir, $runCacheDir) = $this->prepareRunDirs();
file_put_contents($runCacheDir . $runId . '.json', json_encode($result));
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => $result['status'] === 'error' ? 'error' : 'complete')));
}
/**
* Respond to the browser's AJAX request with JSON and end the request
*
* @param array $data
*/
private function sendJson(array $data) {
while(ob_get_level()) ob_end_clean();
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
/**
* Start the action in a separate CLI process and tell the browser to poll for the result.
* Called from ___executeRun() after the form has been validated. Always ends the request.
*
* @param string $runId
* @param bool $wantBackup
*/
private function startCliRun($runId, $wantBackup) {
$runId = $this->sanitizeRunId($runId);
if(!$runId) $this->sendJson(array('status' => 'error', 'output' => '<div class="adminActionsError"><p>Invalid run ID</p></div>'));
list($runStatusDir, $runCacheDir) = $this->prepareRunDirs();
$post = $this->wire('input')->post->getArray();
unset($post['runId'], $post['batch']);
$actionName = $this->wire('input')->get->action;
$job = array(
'action' => $actionName,
'userId' => $this->wire('user')->id,
'post' => $post,
'backup' => (bool) $wantBackup,
// only hand Tracy dumps to users who can see them in the browser
'tracyDumps' => class_exists('\ProcessWire\TracyDebugger', false) && TracyDebugger::$allowedTracyUser === 'development',
);
$jobFile = $runCacheDir . $runId . '.job.json';
file_put_contents($jobFile, json_encode($job));
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'running')));
$error = $this->spawnCliRunner($runId);
// The runner removes the job file as soon as it has loaded ProcessWire and read it, which is
// how we know it started. Wait for that so a process that never starts gives a clear error.
if(!$error) {
$waitUntil = microtime(true) + 15;
while(is_file($jobFile) && microtime(true) < $waitUntil) {
usleep(200000);
clearstatcache(true, $jobFile);
}
// unlink succeeding means the runner never picked the job up (and now won't)
if(@unlink($jobFile)) {
$log = is_file($runCacheDir . $runId . '.log') ? trim((string) file_get_contents($runCacheDir . $runId . '.log')) : '';
$error = 'The CLI process did not start within 15 seconds.' . ($log !== '' ? ' It reported: ' . substr($log, -2000) : '');
}
}
else {
@unlink($jobFile);
}
if($error) {
$result = array('status' => 'error', 'output' => '<div class="adminActionsError"><p>' . htmlspecialchars($error, ENT_QUOTES, 'UTF-8') . '</p><p>Check the PHP CLI path in the Admin Actions module settings, or change the Execution mode back to Browser.</p></div>');
$this->writeRunResult($runId, $result);
$this->sendJson($result);
}
$this->sendJson(array('status' => 'background'));
}
/**
* Launch cli-runner.php as a detached background process
*
* @param string $runId
* @return string Error message, or blank on success
*/
private function spawnCliRunner($runId) {
if(!function_exists('proc_open')) return 'Running actions via CLI needs PHP\'s proc_open() function, which is disabled on this server.';
$php = $this->getPhpCliPath();
if(!$php) return 'Unable to find the PHP CLI binary.';
$runCacheDir = $this->wire('config')->paths->cache . 'AdminActions/runs/';
$cmd = escapeshellarg($php) . ' ' . escapeshellarg(__DIR__ . '/cli-runner.php') . ' ' . escapeshellarg($runId) . ' ' . escapeshellarg($this->wire('config')->paths->root);
// & with HUP ignored (what nohup does - but nohup itself refuses to run without a console on macOS)
// detaches it from this request; setsid (where available) also moves it out of the web server's
// process group so it isn't killed along with the request
$setsid = @is_executable('/usr/bin/setsid') ? '/usr/bin/setsid ' : '';
$cmd = "trap '' HUP; " . $setsid . $cmd . ' > /dev/null 2>> ' . escapeshellarg($runCacheDir . $runId . '.log') . ' &';
$process = @proc_open($cmd, array(0 => array('file', '/dev/null', 'r'), 1 => array('file', '/dev/null', 'w'), 2 => array('file', '/dev/null', 'w')), $pipes);
if(!is_resource($process)) return 'Unable to start the CLI process (proc_open() failed).';
proc_close($process); // returns as soon as the shell has backgrounded the runner
return '';
}
/**
* Path to the PHP CLI binary: the module setting, or the "php" binary alongside the running PHP
*
* @return string
*/
private function getPhpCliPath() {
if(!empty($this->data['phpCliPath'])) return $this->data['phpCliPath'];
// PHP_BINARY is the web SAPI (php-fpm, lsphp, etc) here, so look for the CLI binary next to it
foreach(array(PHP_BINDIR . '/php', dirname(PHP_BINARY) . '/php', '/usr/local/bin/php', '/usr/bin/php') as $path) {
if(@is_file($path) && @is_executable($path)) return $path;
}
return 'php'; // rely on PATH
}
/**
* Run a job started by startCliRun(). Called by cli-runner.php in the CLI process.
*
* @param string $runId
*/
public function runCliJob($runId) {
if(PHP_SAPI !== 'cli') throw new WirePermissionException('CLI only');
$runId = $this->sanitizeRunId($runId);
if(!$runId) return;
list($runStatusDir, $runCacheDir) = $this->prepareRunDirs();
$jobFile = $runCacheDir . $runId . '.job.json';
$job = is_file($jobFile) ? json_decode((string) file_get_contents($jobFile), true) : null;
// removing the job file tells the web request that started us that we're running; if it has already
// removed it itself (it gave up waiting) the run has been reported as failed, so don't run it
if(!is_array($job) || !@unlink($jobFile)) return;
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'running', 'pid' => getmypid())));
$this->downloadStaged = false;
register_shutdown_function(function() use ($runId, $runStatusDir) {
$status = json_decode((string) @file_get_contents($runStatusDir . $runId . '.json'), true);
if(isset($status['status']) && $status['status'] === 'running') {
$error = !empty($GLOBALS['adminActionsCliLastError']) ? $GLOBALS['adminActionsCliLastError'] : error_get_last();
$message = $error ? $error['message'] . ' in ' . str_replace($this->wire('config')->paths->root, '/', $error['file']) . ' on line ' . $error['line'] : 'The CLI process ended unexpectedly.';
$this->writeRunResult($runId, array('status' => 'error', 'output' => '<div class="adminActionsError"><p>Fatal error: ' . htmlspecialchars($message, ENT_QUOTES, 'UTF-8') . '</p></div>'));
}
});
try {
// act as the user who submitted the form
$user = $this->wire('users')->get((int) $job['userId']);
if(!$user->id) throw new WireException('The user who started this action no longer exists.');
$this->wire('users')->setCurrentUser($user);
$actionName = (string) $job['action'];
$info = isset($this->actionTypes[$actionName]) && isset($this->data[$this->actionTypes[$actionName]][$actionName]) ? $this->data[$this->actionTypes[$actionName]][$actionName] : null;
if(!$user->hasPermission('admin-actions') || !$info || empty($info['roles']) || !count(array_intersect($info['roles'], $user->roles->each('id')))) {
throw new WirePermissionException($this->noPermissionMessage);
}
if(!$this->includeAndInstantiate($actionName)) throw new WireException("The file for the $actionName action is missing.");
$this->wire('input')->get->action = $actionName;
$this->dbBackup = isset($this->action->dbBackup) ? $this->action->dbBackup : (isset($this->data['dbBackup']) ? $this->data['dbBackup'] : 'automatic');
$actionTitle = $this->getActionTitle($actionName, $info);
// rebuild the options from the submitted form, as the web request did (it already checked CSRF)
$form = $this->buildOptionsForm();
$form->protectCSRF = false;
$form->processInput(new WireInputData($job['post']));
$options = array();
foreach($form->getAll() as $field) $options[$field->name] = $field->value;
if(!empty($job['tracyDumps']) && class_exists('\ProcessWire\TracyDebugger', false) && method_exists('\ProcessWire\TracyDebugger', 'enableCliDumps')) {
TracyDebugger::enableCliDumps();
}
$restoreLink = '';
if(!empty($job['backup'])) {
$this->backupDb();
$restoreLink = $this->getRestoreLink();
}
$this->addDownloadCaptureHook($runId, $runStatusDir, $runCacheDir, null);
ob_start();
$exceptionHtml = '';
try {
$success = $this->runActionBatches($options);
} catch(\Throwable $e) {
$success = false;
$exceptionHtml = $this->handleActionException($e);
}
$printed = (string) ob_get_clean();
if($this->downloadStaged) return;
$this->action->output = $printed . $this->action->output . $this->getTracyDumpsHtml() . $exceptionHtml;
$this->writeRunResult($runId, array('status' => 'complete', 'output' => $this->getResultHtml($success && !$form->getErrors(), $actionTitle, $restoreLink)));
} catch(\Throwable $e) {
$this->writeRunResult($runId, array('status' => 'error', 'output' => $this->handleActionException($e)));
}
}
/**
* bd() dumps captured in a CLI run (see TracyDebugger::enableCliDumps()), rendered like Tracy's Dumps panel
*
* @return string
*/
private function getTracyDumpsHtml() {
if(!class_exists('\ProcessWire\TracyDebugger', false) || empty(TracyDebugger::$dumpItems)) return '';
$out = '';
foreach(TracyDebugger::$dumpItems as $item) {
if(isset($item['title']) && $item['title'] != '') $out .= '<h2>' . htmlspecialchars($item['title'], ENT_QUOTES, 'UTF-8') . '</h2>';
$out .= $item['dump'];
}
TracyDebugger::$dumpItems = array();
return '<div class="adminActionsDumps"><p><strong>Tracy bar dumps</strong></p><div class="tracy-inner" style="height:auto !important"><div class="tracy-DumpPanel">' . $out . '</div></div></div>';
}
/**
* Executed when ./alive/ url is accessed: the browser checks a CLI run's process is still alive while
* polling, so a process killed by the server is reported instead of polling until the 1 hour cutoff
*/
public function executeAlive() {
if(!$this->wire('config')->ajax) throw new Wire404Exception();
$runId = $this->sanitizeRunId($this->wire('input')->post->runId);
$statusFile = $this->wire('config')->paths->assets . 'AdminActions/runs/' . $runId . '.json';
$status = $runId && is_file($statusFile) ? json_decode((string) file_get_contents($statusFile), true) : null;
if(!is_array($status) || $status['status'] !== 'running' || empty($status['pid'])) $this->sendJson(array('alive' => null));
$alive = $this->processAlive((int) $status['pid']);
if($alive === false) {
// it may have just finished - only report it if the status still says running
clearstatcache(true, $statusFile);
$status = json_decode((string) file_get_contents($statusFile), true);
if(isset($status['status']) && $status['status'] === 'running') {
$this->writeRunResult($runId, array('status' => 'error', 'output' => '<div class="adminActionsError"><p>The action\'s CLI process ended before it finished - it may have been stopped by the server (eg. a CLI time or memory limit).</p></div>'));
}
}
$this->sendJson(array('alive' => $alive));
}
/**
* Whether a process is running: true/false, or null if this server gives no way to tell
*
* @param int $pid
* @return bool|null
*/
private function processAlive($pid) {
if($pid < 1) return null;
if(function_exists('posix_kill')) {
if(@posix_kill($pid, 0)) return true;
// EPERM: it exists but belongs to another user
return function_exists('posix_get_last_error') && posix_get_last_error() === 1 ? true : false;
}
if(@is_dir('/proc/self')) return @is_dir('/proc/' . $pid);
return null;
}
/**
* Hook WireHttp::sendFile to short-circuit it and stage the file for the browser to fetch.
*
* @param string $runId
* @param string $runStatusDir
* @param string $runCacheDir
* @param array|null $sessionSnapshot Session snapshot to write changes back from (null when there's no session, eg. CLI)
*/
private function addDownloadCaptureHook($runId, $runStatusDir, $runCacheDir, $sessionSnapshot) {
$this->addHookBefore('WireHttp::sendFile', function($event) use ($runId, $runStatusDir, $runCacheDir, $sessionSnapshot) {
$filename = $event->arguments(0);
$options = $event->arguments(1);
if(!is_array($options)) $options = array();
if($filename === false) {
// data mode: sendFile() requires downloadFilename + data
$downloadName = isset($options['downloadFilename']) ? basename($options['downloadFilename']) : '';
if(!$downloadName) return; // let original sendFile() throw its own WireException
$payload = isset($options['data']) ? $options['data'] : '';
} else {
$base = (isset($options['downloadFilename']) && $options['downloadFilename'] !== '')
? $options['downloadFilename']
: pathinfo($filename, PATHINFO_BASENAME);
$downloadName = basename($base);
$payload = null; // sentinel: copy from $filename
}
if(!$downloadName) return;
$ext = strtolower(pathinfo($downloadName, PATHINFO_EXTENSION));
$cts = $this->wire('config')->fileContentTypes;
$contentType = isset($cts[$ext]) ? $cts[$ext] : (isset($cts['?']) ? $cts['?'] : 'application/octet-stream');
$contentType = ltrim($contentType, '+');
$dir = $this->wire('config')->paths->cache . 'AdminActions/downloads/' . $runId . '/';
if(!is_dir($dir) && !wireMkdir($dir, true)) return; // fall through to original sendFile()
$dest = $dir . $downloadName;
$ok = $payload === null ? @copy($filename, $dest) : (file_put_contents($dest, $payload) !== false);
if(!$ok) return;
if(file_put_contents($dest . '.meta.json', json_encode(array('contentType' => $contentType))) === false) {
$this->wire('log')->error('AdminActions: failed to write download sidecar at ' . $dest . '.meta.json');
}
$envelope = array(
'status' => 'download',
'url' => $this->processPageUrl . 'download/?runId=' . urlencode($runId) . '&filename=' . urlencode($downloadName),
'filename' => $downloadName,
'contentType' => $contentType,
);
// Cache first, then flip status — polling clients gate on status
file_put_contents($runCacheDir . $runId . '.json', json_encode($envelope));
file_put_contents($runStatusDir . $runId . '.json', json_encode(array('status' => 'complete')));
$this->downloadStaged = true;
if($sessionSnapshot !== null) $this->saveSessionChanges($sessionSnapshot);
// Discard the ob_start buffer below and any prior download-style headers,
// then write the JSON envelope as the inline AJAX response body.
while(ob_get_level()) ob_end_clean();
header_remove('Content-Disposition');
header_remove('Content-Length');
header('Content-Type: application/json');
echo json_encode($envelope);
$event->replace = true;
$event->return = $payload === null ? filesize($filename) : strlen($payload);
$exitOpt = isset($options['exit']) ? $options['exit'] : true;
if($exitOpt) exit;
});
}
/**
* Run the action in this request, running any continueWith() batches back-to-back.
* Used when there's no browser AJAX run to spread the batches across requests.
*
* @param array $options
* @return bool
*/
protected function runActionBatches($options) {
$output = '';
$this->action->batchStartTime = null; // no request time limit to avoid, so timeUp() stays false
while(true) {
$this->action->continueState = null;
$success = $this->action->executeAction($options);
if(!$success || $this->action->continueState === null) break;
$output .= $this->action->output;
$this->action->output = '';
$this->action->state = $this->action->continueState;
}
$this->action->output = $output . $this->action->output;
return $success;
}
/**
* Reopen the session (closed for background execution) and write back
* only what the action changed, so concurrent polling requests aren't clobbered
*
* @param array $snapshot $_SESSION as it was when the session was closed
*/
protected function saveSessionChanges(array $snapshot) {
$current = isset($_SESSION) ? $_SESSION : array();
if($current === $snapshot || session_status() !== PHP_SESSION_NONE || headers_sent()) return;
if(!@session_start()) return;
$_SESSION = $this->applySessionChanges($_SESSION, $snapshot, $current);
session_write_close();
}
/**
* Apply keys added/changed/removed between $before and $after onto $target, recursing into arrays
*
*/
protected function applySessionChanges(array $target, array $before, array $after) {
foreach($after as $key => $value) {
if(array_key_exists($key, $before) && $before[$key] === $value) continue;
if(is_array($value) && isset($before[$key]) && is_array($before[$key]) && isset($target[$key]) && is_array($target[$key])) {
$target[$key] = $this->applySessionChanges($target[$key], $before[$key], $value);
}
else {
$target[$key] = $value;
}
}
foreach(array_diff_key($before, $after) as $key => $value) unset($target[$key]);
return $target;
}
/**
* Executed when legacy ./execute/ url for module is accessed
*
*/
public function ___executeExecute() {
// "execute" is not in use anymore: renamed to "run" to prevent firewall blocking
// we'll pass through calls to the new method for legacy installs with possibly hardcoded urls
return $this->___executeRun();
}
/**
* Executed when ./cleanup/ url is accessed (background execution support)
* Fetches result from cache and cleans up run files
*/
public function executeCleanup() {
if(!$this->wire('config')->ajax) {
throw new Wire404Exception();
}