-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathProcessModule.module
More file actions
1271 lines (1076 loc) · 48.9 KB
/
ProcessModule.module
File metadata and controls
1271 lines (1076 loc) · 48.9 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
/**
* ProcessWire Module Process
*
* Provides list, install, and uninstall capability for ProcessWire modules
*
* For more details about how Process modules work, please see:
* /wire/core/Process.php
*
* This version also lifts several pieces of code from Soma's Modules Manager
* specific to the parts involved with downloading modules from the directory.
*
* ProcessWire 2.x
* Copyright (C) 2013 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class ProcessModule extends Process {
public static function getModuleInfo() {
return array(
'title' => __('Modules', __FILE__), // getModuleInfo title
'summary' => __('List, edit or install/uninstall modules', __FILE__), // getModuleInfo summary
'version' => 117,
'permanent' => true,
'permission' => 'module-admin',
'useNavJSON' => true,
'nav' => array(
array(
'url' => '?site#tab_site_modules',
'label' => 'Site',
'icon' => 'plug',
'navJSON' => 'navJSON/?site=1'
),
array(
'url' => '?core#tab_core_modules',
'label' => 'Core',
'icon' => 'plug',
'navJSON' => 'navJSON/?core=1',
),
array(
'url' => '?configurable#tab_configurable_modules',
'label' => 'Configure',
'icon' => 'gear',
'navJSON' => 'navJSON/?configurable=1',
),
array(
'url' => '?install#tab_install_modules',
'label' => 'Install',
'icon' => 'sign-in',
'navJSON' => 'navJSON/?install=1',
),
array(
'url' => '?reset=1',
'label' => 'Refresh',
'icon' => 'refresh',
)
)
);
}
protected $labels = array();
/**
* All modules indexed by class name and sorted by class name
*
*/
protected $modulesArray = array();
/**
* All modules that may be deleted
*
*/
protected $deleteableModules = array();
/**
* Categories of modules that we can't uninstall via this module
*
*/
protected $uninstallableCategories = array(
'language-pack',
'site-profile',
);
/**
* Number of new modules found after a reset
*
*/
protected $numFound = 0;
public function __construct() {
$this->labels['download'] = $this->_('Download');
if($this->input->get->update) {
$this->labels['download_install'] = $this->_('Download and Update');
} else {
$this->labels['download_install'] = $this->_('Download and Install');
}
$this->labels['download_dir'] = $this->_('Add Module From Directory');
$this->labels['upload'] = $this->_('Upload');
$this->labels['upload_zip'] = $this->_('Add Module From Upload');
$this->labels['download_zip'] = $this->_('Add Module From URL');
$this->labels['check_new'] = $this->_('Check for New Modules');
$this->labels['installed_date'] = $this->_('Installed');
$this->labels['requires'] = $this->_x("Requires", 'list'); // Label that precedes list of required prerequisite modules
$this->labels['installs'] = $this->_x("Also Installs", 'list'); // Label that precedes list of other modules a given one installs
$this->labels['reset'] = $this->_('Refresh');
$this->labels['core'] = $this->_('Core');
$this->labels['site'] = $this->_('Site');
$this->labels['configure'] = $this->_('Configure');
$this->labels['install_btn'] = $this->_x('Install', 'button'); // Label for Install button
$this->labels['install'] = $this->_('Install'); // Label for Install tab
$this->labels['cancel'] = $this->_('Cancel'); // Label for Cancel button
if($this->wire('languages') && !$this->wire('user')->language->isDefault()) {
// Use previous translations when new labels aren't available (can be removed in PW 2.6+ when language packs assumed updated)
if($this->labels['install'] == 'Install') $this->labels['install'] = $this->labels['install_btn'];
if($this->labels['reset'] == 'Refresh') $this->labels['reset'] = $this->labels['check_new'];
}
require(dirname(__FILE__) . '/ProcessModuleInstall.php');
}
/**
* Format a module version number from 999 to 9.9.9
*
* @param string $version
* @return string
*
*/
protected function formatVersion($version) {
return $this->wire('modules')->formatVersion($version);
}
/**
* Output JSON list of navigation items for this (intended to for ajax use)
*
* For 2.5+ admin themes
*
*/
public function ___executeNavJSON(array $options = array()) {
$page = $this->wire('page');
$data = array(
'url' => $page->url,
'label' => (string) $page->get('title|name'),
'icon' => 'plug',
'list' => array(),
);
$site = $this->wire('input')->get('site');
$core = $this->wire('input')->get('core');
$configurable = $this->wire('input')->get('configurable');
$install = $this->wire('input')->get('install');
if($site || $install) $data['add'] = array(
'url' => "?new#tab_new_modules",
'label' => __('Add New', '/wire/templates-admin/default.php'),
);
$modules = $this->wire('modules');
$moduleNames = array();
if($install) {
$moduleNames = array_keys($modules->getInstallable());
} else {
foreach($modules as $module) $moduleNames[] = $module->className();
}
sort($moduleNames);
foreach($moduleNames as $moduleName) {
$info = $this->wire('modules')->getModuleInfoVerbose($moduleName);
if($site && $info['core']) continue;
if($core && !$info['core']) continue;
if($configurable && (!$info['configurable'] || !$info['installed'])) continue;
if($install) {
// exclude already installed modules
if($info['installed']) continue;
// check that it can be installed NOW (i.e. all dependencies met)
if(!$this->wire('modules')->isInstallable($moduleName, true)) continue;
}
$label = $info['name'];
$_label = $label;
while(isset($data['list'][$_label])) $_label .= "_";
if(empty($info['icon'])) $info['icon'] = $info['configurable'] ? 'gear' : 'plug';
$url = $install ? "installConfirm" : "edit";
$url .= "?name=$info[name]";
$data['list'][$_label] = array(
'url' => $url,
'label' => $label,
'icon' => $info['icon'],
);
}
ksort($data['list']);
$data['list'] = array_values($data['list']);
if($this->wire('config')->ajax) header("Content-Type: application/json");
return json_encode($data);
}
/**
* Load all modules, install any requested, and render a list of all modules
*
*/
public function ___execute() {
foreach($this->modules as $module) {
$this->modulesArray[$module->className()] = 1;
}
foreach($this->modules->getInstallable() as $module) {
$this->modulesArray[basename(basename($module, '.php'), '.module')] = 0;
}
ksort($this->modulesArray);
if($this->input->post->install) {
$this->session->CSRF->validate();
$name = $this->input->post->install;
if($name && isset($this->modulesArray[$name]) && !$this->modulesArray[$name]) {
$module = $this->modules->get($name);
$this->modulesArray[$name] = 1;
$this->session->message($this->_("Module Install") . " - " . $module->className); // Message that precedes the name of the module installed
$this->session->redirect("edit?name={$module->className}");
}
}
if($this->input->post->delete) {
$this->session->CSRF->validate();
$name = $this->input->post->delete;
if($name && isset($this->modulesArray[$name])) {
$info = $this->modules->getModuleInfoVerbose($name);
try {
$this->modules->delete($name);
$this->message($this->_('Deleted module files') . ' - ' . $info['title']);
} catch(WireException $e) {
$this->error($e->getMessage());
}
$this->session->redirect("./");
}
}
if($this->input->post->download && $this->input->post->download_name) {
$this->session->CSRF->validate();
return $this->downloadConfirm($this->input->post->download_name);
} else if($this->input->get->download_name) {
return $this->downloadConfirm($this->input->get->download_name);
}
if($this->input->post->upload) {
$this->session->CSRF->validate();
$this->executeUpload('upload_module');
}
if($this->input->post->download_zip && $this->input->post->download_zip_url) {
$this->session->CSRF->validate();
$this->executeDownloadURL($this->input->post->download_zip_url);
}
if($this->input->get->update) {
$name = $this->sanitizer->name($this->input->get->update);
if(isset($this->modulesArray[$name])) return $this->downloadConfirm($name, true);
}
if($this->input->get->reset == 1) {
$this->modules->resetCache();
$edit = $this->input->get->edit;
if($edit) $this->session->redirect("./edit?name=" . $this->sanitizer->fieldName($edit) . "&reset=2");
else $this->session->redirect("./?reset=2");
}
return $this->renderList();
}
/**
* Render a list of all modules
*
*/
protected function renderList() {
$modulesArray = $this->modulesArray;
$installedArray = array();
$uninstalledArray = array();
$configurableArray = array();
$uninstalledNames = array();
$siteModulesArray = array();
$coreModulesArray = array();
$newModulesArray = array();
// by default, core modules don't appear in the "new" list,
// this array contains a list of core modules that are allowed to appear there
$newCoreModules = array(
'InputfieldCKEditor',
'TextformatterImgQA',
);
if($this->wire('input')->post('new_seconds')) {
$this->wire('session')->set('ProcessModuleNewSeconds', (int) $this->wire('input')->post('new_seconds'));
}
$newSeconds = (int) $this->wire('session')->get('ProcessModuleNewSeconds');
if(!$newSeconds) $newSeconds = 86400;
foreach($modulesArray as $name => $installed) {
if($installed) {
$installedArray[$name] = $installed;
$errors = $this->modules->getDependencyErrors($name);
if($errors) foreach($errors as $error) $this->error($error);
} else {
$uninstalledNames[] = $name;
$uninstalledArray[$name] = $installed;
}
$info = $this->modules->getModuleInfoVerbose($name);
$isNew = !$info['core'] || ($info['core'] && in_array($name, $newCoreModules));
if($isNew) $isNew = $info['created'] > 0 && $info['created'] > (time()-$newSeconds);
if($isNew) $newModulesArray[$name] = $installed;
if($info['core']) {
$coreModulesArray[$name] = $installed;
} else {
$siteModulesArray[$name] = $installed;
}
if($info['configurable'] && $info['installed']) $configurableArray[$name] = $installed;
}
$form = $this->modules->get('InputfieldForm');
$form->attr('action', './');
$form->attr('method', 'post');
$form->attr('enctype', 'multipart/form-data');
$form->attr('id', 'modules_form');
$this->modules->get('JqueryWireTabs');
// site
$tab = new InputfieldWrapper();
$tab->attr('id', 'tab_site_modules');
$tab->attr('title', $this->labels['site']);
$tab->attr('class', 'WireTab');
$markup = $this->modules->get('InputfieldMarkup');
$markup->label = $this->_('/site/modules/ - Modules specific to your site');
$markup->icon = 'folder-open-o';
$markup->value .= $this->renderListTable($siteModulesArray, true);
$markup->value .= "<p class='detail'><i class='fa fa-fw fa-star'></i> " . sprintf($this->_('Browse the modules directory at %s'), "<a target='_blank' href='http://modules.processwire.com'>modules.processwire.com</a>") . "</p>";
$markup->value .= "<p class='detail'><i class='fa fa-fw fa-eraser'></i> " . $this->_("To remove a module, click the module to edit, check the 'uninstall' box, then save. Once uninstalled, the module's file(s) may be removed from /site/modules/. If it still appears in the list above, you may need to click the 'check for new modules' button for ProcessWire to see the change."); // Instructions on how to remove a module
$tab->add($markup);
$form->add($tab);
// core
$tab = new InputfieldWrapper();
$tab->attr('id', 'tab_core_modules');
$tab->attr('title', $this->labels['core']);
$tab->attr('class', 'WireTab');
$markup = $this->modules->get('InputfieldMarkup');
$markup->value = $this->renderListTable($coreModulesArray);
$markup->label = $this->_('/wire/modules/ - Modules included with the ProcessWire core');
$markup->icon = 'folder-open-o';
$tab->add($markup);
$form->add($tab);
// configurable
$tab = new InputfieldWrapper();
$tab->attr('id', 'tab_configurable_modules');
$tab->attr('title', $this->labels['configure']);
$tab->attr('class', 'WireTab');
$markup = $this->modules->get('InputfieldMarkup');
$markup->value = $this->renderListTable($configurableArray, true, true, false, false, true);
$markup->label = $this->_('Modules that have configuration options');
$markup->icon = 'folder-open-o';
$tab->add($markup);
$form->add($tab);
// installable
$tab = new InputfieldWrapper();
$tab->attr('id', 'tab_install_modules');
$tabLabel = $this->labels['install'];
$tab->attr('title', $tabLabel);
$tab->attr('class', 'WireTab');
$markup = $this->modules->get('InputfieldMarkup');
$markup->value = $this->renderListTable($uninstalledArray, true, true, false, false, true);
$markup->label = $this->_('Modules on the file system that are not currently installed');
$markup->icon = 'folder-open-o';
$tab->add($markup);
$form->add($tab);
// new
$tab = new InputfieldWrapper();
$tab->attr('id', 'tab_new_modules');
$tab->attr('title', $this->_('New'));
$tab->attr('class', 'WireTab');
if($this->wire('session')->ProcessModuleNewModules) {
foreach($this->wire('session')->ProcessModuleNewModules as $name => $created) {
if(!is_numeric($name) && !isset($newModulesArray[$name])) $newModulesArray[$name] = 0;
}
}
$select = $this->wire('modules')->get('InputfieldSelect');
$select->attr('name', 'new_seconds');
$select->addClass('modules_filter');
$select->addOption(3600, $this->_('Within the last hour'));
$select->addOption(86400, $this->_('Within the last day'));
$select->addOption(604800, $this->_('Within the last week'));
$select->addOption(2419200, $this->_('Within the last month'));
$select->required = true;
$select->attr('value', $newSeconds);
$markup = $this->modules->get('InputfieldMarkup');
$markup->icon = 'lightbulb-o';
$markup->value = $select->render() . $this->renderListTable($newModulesArray, false, false, true, true);
$markup->label = $this->_('Recently Found and Installed Modules');
$tab->add($markup);
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = $this->labels['download_dir'];
$fieldset->icon = 'cloud-download';
//if($this->wire('input')->post('new_seconds')) $fieldset->collapsed = Inputfield::collapsedYes;
$f = $this->modules->get('InputfieldName');
$f->attr('id+name', 'download_name');
$f->label = $this->_('Module Class Name');
$f->description = $this->_('You may browse the modules directory and locate the module you want to download and install. Type or paste in the "class name" for the module you want to install.');
$f->notes = $this->_('The modules directory is located at [modules.processwire.com](http://modules.processwire.com)');
$f->attr('placeholder', $this->_('ModuleClassName')); // placeholder
$f->required = false;
$fieldset->add($f);
$f = $this->modules->get('InputfieldSubmit');
$f->attr('id+name', 'download');
$f->value = $this->labels['download_install'];
$f->icon = $fieldset->icon;
$fieldset->add($f);
$tab->add($fieldset);
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = $this->labels['download_zip'];
$fieldset->icon = 'download';
$fieldset->collapsed = Inputfield::collapsedYes;
$trustNote = $this->_('Be absolutely certain that you trust the source of the ZIP file.');
$f = $this->modules->get('InputfieldURL');
$f->attr('id+name', 'download_zip_url');
$f->label = $this->_('Module ZIP file URL');
$f->description = $this->_('Download a ZIP file containing a module. If you download a module that is already installed, the installed version will be overwritten with the newly downloaded version.');
$f->notes = $trustNote;
$f->attr('placeholder', $this->_('http://domain.com/ModuleName.zip')); // placeholder
$f->required = false;
$fieldset->add($f);
$f = $this->modules->get('InputfieldSubmit');
$f->attr('id+name', 'download_zip');
$f->value = $this->labels['download'];
$f->icon = $fieldset->icon;
$fieldset->add($f);
$tab->add($fieldset);
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = $this->labels['upload_zip'];
$fieldset->icon = 'upload';
$fieldset->collapsed = Inputfield::collapsedYes;
$f = $this->modules->get('InputfieldFile');
$f->extensions = 'zip';
$f->maxFiles = 1;
$f->descriptionRows = 0;
$f->overwrite = true;
$f->attr('id+name', 'upload_module');
$f->label = $this->_('Module ZIP File');
$f->description = $this->_('Upload a ZIP file containing module file(s). If you upload a module that is already installed, it will be overwritten with the one you upload.');
$f->notes = $trustNote;
$f->required = false;
$fieldset->add($f);
$f = $this->modules->get('InputfieldSubmit');
$f->attr('id+name', 'upload');
$f->value = $this->labels['upload'];
$f->icon = $fieldset->icon;
$fieldset->add($f);
$tab->add($fieldset);
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->attr('id', 'fieldset_check_new');
$fieldset->label = $this->labels['reset'];
$fieldset->description = $this->_('If you have placed new modules in /site/modules/ yourself, click this button to find them.');
$fieldset->collapsed = Inputfield::collapsedYes;
$fieldset->icon = 'refresh';
$submit = $this->modules->get('InputfieldButton');
$submit->attr('href', './?reset=1');
$submit->attr('id', 'reset_modules');
$submit->attr('class', $submit->attr('class') . ' head_button_clone');
$submit->attr('name', 'reset');
$submit->attr('value', $this->labels['reset']);
$submit->icon = $fieldset->icon;
$fieldset->add($submit);
$tab->add($fieldset);
$form->add($tab);
if($this->input->get->reset == 2 && !$this->numFound) $this->message($this->_("No new modules found"));
$this->session->ModulesUninstalled = $uninstalledNames;
return $form->render();
}
/**
* Render a modules listing table, as it appears in the 'site' and 'core' tabs
*
* @param array $modulesArray
* @param bool $allowDelete Whether or not delete is allowed (default=false)
* @param bool $allowSections Whether to show module sections/categories (default=true)
* @param bool $allowDates Whether to show created dates (default=false)
* @param bool $allowClasses Whether to show module class names(default=false)
* @param bool $allowType Whether to show if module is site or core
* @return string
*
*/
protected function renderListTable($modulesArray, $allowDelete = false, $allowSections = true, $allowDates = false, $allowClasses = false, $allowType = false) {
if(!count($modulesArray)) return "<div class='ProcessModuleNoneFound'>" . $this->_('No modules found.') . "</div>";
static $numCalls = 0;
$numCalls++;
$uninstalledPrev = is_array($this->session->ModulesUninstalled) ? $this->session->ModulesUninstalled : array();
$section = 'none';
$tableHeader = array(
$this->_x('Module', 'list'), // Modules list table header for 'Module' column
$this->_x('Version', 'list'), // Modules list table header for 'Version' column
$this->_x('Summary', 'list') // Modules list table header for 'Summary' column
);
$table = null;
$total = 0;
$out = '';
$this->numFound = 0;
$newModules = $this->wire('session')->get('ProcessModuleNewModules');
if(!is_array($newModules)) $newModules = array();
$sections = array();
$sectionsQty = array();
foreach($modulesArray as $name => $installed) {
if(strpos($name, $section) !== 0 || preg_match('/' . $section . '[^A-Z]/', $name)) {
if(!preg_match('/^([A-Za-z][a-z]+)/', $name, $matches)) $this->error(sprintf($this->_('Invalid module name: %s'), $name));
if($allowSections || is_null($table)) {
$section = $matches[1];
$sections[] = $section;
if($table) $out .= $table->render() . "</div>";
$table = $this->modules->get("MarkupAdminDataTable");
$table->setEncodeEntities(false);
$table->headerRow($tableHeader);
if($allowSections) $out .= "\n<div class='modules_section modules_$section'><h2>$section</h2>";
}
}
$info = $this->modules->getModuleInfoVerbose($name);
// $interfaces = @class_implements($name, false);
// $configurable = is_array($interfaces) && in_array('ConfigurableModule', $interfaces);
$configurable = $info['configurable'];
$title = !empty($info['title']) ? $this->wire('sanitizer')->entities1($info['title']) : substr($name, strlen($section));
if($allowClasses) $title .= "<br /><small class='ModuleClass ui-priority-secondary'>$name</small>";
if($info['icon']) $title = "<i class='fa fa-fw fa-$info[icon]'></i> $title";
$class = $configurable ? 'ConfigurableModule' : '';
if(!empty($info['permanent'])) $class .= ($class ? ' ' : '') . 'PermanentModule';
if($class) $title = "<span class='$class'>$title</span>";
$version = $this->formatVersion(isset($info['version']) ? $info['version'] : 0);
if($allowType) $version .= "<br /><small class='ModuleClass ui-priority-secondary'>" . ($info['core'] ? $this->labels['core'] : $this->labels['site']) . "</small>";
$summary = empty($info['summary']) ? '' : $this->wire('sanitizer')->entities1($info['summary']);
if(strpos($summary, '<') !== false) $summary = preg_replace('/([^\s]{35})[^\s]{20,}/', '$1...', $summary); // prevent excessively long text without whitespace
$summary .= empty($info['href']) ? '' : (" <a href='$info[href]'>" . $this->_('more') . "</a>");
if($summary) $summary = "<p class='module-summary'>$summary</p>";
$buttons = '';
$confirmJS = "return confirm('" . sprintf($this->_('Delete %s?'), $name) . "')";
$editUrl = "edit?name={$name}";
if(!$installed) {
if(count($info['requires'])) {
$requires = $this->modules->getRequiresForInstall($name);
if(count($requires)) {
foreach($requires as $key => $value) {
$nameOnly = preg_replace('/^([_a-zA-Z0-9]+)[=<>]+.*$/', '$1', $value);
$requiresInfo = $this->modules->getModuleInfo($nameOnly);
if(!empty($requiresInfo['error'])) $requires[$key] = "<a href='./?download_name=$nameOnly'>$value</a>";
}
$summary .= "<span class='notes requires'>" . $this->labels['requires'] . " - " . implode(', ', $requires) . "</span>";
}
} else $requires = array();
if(count($info['installs'])) {
$summary .= "<span class='detail installs'>" . $this->labels['installs'] . " - " . implode(', ', $info['installs']) . "</span>";
}
$class = 'not_installed';
if(count($uninstalledPrev) && !in_array($name, $uninstalledPrev)) {
$class .= " new_module";
if(!$this->input->get->uninstalled) $this->message($this->_("Found new module") . " - $name"); // Message that precedes module name when new module is found
$newModules[$name] = time();
$this->numFound++;
}
$title = "<span data-name='$name' class='$class'>$title</span>";
if(count($requires)) {
//$buttonState = 'ui-state-default ui-state-disabled';
//$buttonType = 'button';
} else {
$isConfirm = count($modulesArray) == 1 && $this->wire('input')->get('name');
$buttonState = 'ui-state-default';
$buttonType = 'submit';
$buttonPriority = $isConfirm ? "ui-priority-primary" : "ui-priority-secondary";
$buttons .=
"<button type='$buttonType' name='install' data-install='$name' class='install_$name $buttonState ui-button $buttonPriority' value='$name'>" .
"<span class='ui-button-text'><i class='fa fa-sign-in'></i> " . $this->labels['install_btn'] . "</span></button>"; // Text for 'Install' button
// install confirm, needs a cancel button
if($isConfirm) $buttons .=
"<button type='$buttonType' name='cancel' class='cancel_$name ui-button ui-priority-secondary' value='$name'>" .
"<span class='ui-button-text'><i class='fa fa-times-circle'></i> " . $this->labels['cancel'] . "</span></button>"; // Text for 'Cancel' button
}
if($allowDelete && $this->wire('modules')->isDeleteable($name)) $buttons .=
"<button type='submit' name='delete' data-delete='$name' class='delete_$name ui-state-default ui-priority-secondary ui-button' value='$name' onclick=\"$confirmJS\">" .
"<span class='ui-button-text'><i class='fa fa-eraser'></i> " . $this->_x('Delete', 'button') . "</span></button>"; // Text for 'Delete' button
$editUrl = '#';
} else if($configurable) {
$buttons .=
"<button type='button' class='ProcessModuleSettings ui-state-default ui-button'>" .
"<span class='ui-button-text'><i class='fa fa-cog'></i> " . $this->_x('Settings', 'button') . "</span></button>"; // Text for 'Settings' button
}
if($buttons) $buttons = "<small class='buttons'>$buttons</small>";
if($allowDates) {
$summary .= "<span class='detail date'>";
$summary .= $installed ? $this->labels['installed_date'] : $this->_('Found');
$created = isset($newModules[$name]) ? $newModules[$name] : $info['created'];
$summary .= ': ' . wireRelativeTimeStr($created) . "</span>";
}
$row = array(
$title => $editUrl,
$version,
$summary . $buttons,
);
$table->row($row);
$total++;
if(!isset($sectionsQty[$section])) $sectionsQty[$section] = 0;
$sectionsQty[$section]++;
}
$out .= $table->render();
if($allowSections) {
$out .= "</div>";
$select = "<p><select name='modules_section$numCalls' class='modules_filter modules_section_select'>";
$select .= "<option value=''>" . $this->_('Show All') . "</option>";
$current = $this->wire('input')->cookie("modules_section$numCalls");
foreach($sections as $section) {
$qty = $sectionsQty[$section];
$selected = $current == $section ? " selected='selected'" : "";
$select .= "<option$selected value='$section'>$section ($qty)</option>";
}
$select .= "</select></p>";
$out = $select . $out;
}
$resetNewModules = false;
foreach($newModules as $key => $newModule) {
$info = $this->wire('modules')->getModuleInfoVerbose($newModule);
if(!$info['file'] || !file_exists($info['file'])) {
unset($newModules[$key]);
$resetNewModules = true;
}
}
if($this->numFound) $resetNewModules = true;
if($resetNewModules) $this->wire('session')->set('ProcessModuleNewModules', $newModules);
return $out;
}
/**
* Checks for compatibility, polls the modules directory web service and returns rendered markup for the download info table and confirmation form
*
* @param $name Class name of module
* @param bool $update Whether this is a 'check for updates' request
* @return string
*
*/
protected function downloadConfirm($name, $update = false) {
$name = $this->wire('sanitizer')->name($name);
$info = self::getModuleInfo();
$this->wire('processHeadline', $this->labels['download_install']);
$this->wire('breadcrumbs')->add(new Breadcrumb('./', $info['title']));
if($update) $this->wire('breadcrumbs')->add(new Breadcrumb("./?edit=$name", $name));
$redirectURL = $update ? "./edit?name=$name" : "./";
$className = $name;
$url = trim($this->wire('config')->moduleServiceURL, '/') . "/$className/?apikey=" . $this->wire('sanitizer')->name($this->wire('config')->moduleServiceKey);
$http = new WireHttp();
$data = $http->get($url);
if(empty($data)) {
$this->error($this->_('Error retrieving data from web service URL') . ' - ' . $http->getError());
return $this->session->redirect($redirectURL);
}
$data = json_decode($data, true);
if(empty($data)) {
$this->error($this->_('Error decoding JSON from web service'));
return $this->session->redirect($redirectURL);
}
if($data['status'] !== 'success') {
$this->error($this->_('Error reported by web service:') . ' ' . wire('sanitizer')->entities($data['error']));
return $this->session->redirect($redirectURL);
}
$installable = true;
foreach($data['categories'] as $category) {
if(!in_array($category['name'], $this->uninstallableCategories)) continue;
$this->error(sprintf($this->_('Sorry modules of type "%s" are not installable from the admin.'), $category['title']));
$installable = false;
}
if(!$installable) $this->session->redirect($redirectURL);
$form = $this->buildDownloadConfirmForm($data, $update);
return $form->render();
}
/**
* Builds a confirmation form and table showing information about the requested module before download
*
* @param array $data Array of information about the module from the directory service
* @param bool $update Whether or not this is an 'update module' request
* @return InputfieldForm
*
*/
protected function ___buildDownloadConfirmForm(array $data, $update = false) {
$warnings = array();
$authors = '';
foreach($data['authors'] as $author) $authors .= $author['title'] . ", ";
$authors = rtrim($authors, ", ");
$compat = '';
$isCompat = false;
$myVersion = substr($this->wire('config')->version, 0, 3);
foreach($data['pw_versions'] as $v) {
$compat .= $v['name'] . ", ";
if(version_compare($v['name'], $myVersion) >= 0) $isCompat = true;
}
$compat = trim($compat, ", ");
if(!$isCompat) $warnings[] = $this->_('This module does not indicate compatibility with this version of ProcessWire. It may still work, but you may want to check with the module author.');
$form = $this->wire('modules')->get('InputfieldForm');
$form->attr('action', './download/');
$form->attr('method', 'post');
$form->attr('id', 'ModuleInfo');
$markup = $this->wire('modules')->get('InputfieldMarkup');
$form->add($markup);
$installed = $this->modules->isInstalled($data['class_name']) ? $this->modules->getModuleInfoVerbose($data['class_name']) : null;
$moduleVersionNote = '';
if($installed) {
$installedVersion = $this->formatVersion($installed['version']);
if($installedVersion == $data['module_version']) {
$note = $this->_('Current installed version is already up-to-date');
$installedVersion .= ' - ' . $note;
$this->message($note);
$this->session->redirect("./edit?name=$data[class_name]");
} else {
if(version_compare($installedVersion, $data['module_version']) < 0) {
$this->message($this->_('An update to this module is available!'));
} else {
$moduleVersionNote = " <span class='ui-state-error-text'>(" . $this->_('older than the one you already have installed!') . ")</span>";
}
}
} else {
$installedVersion = $this->_x('Not yet', 'install-table');
}
$table = $this->wire('modules')->get('MarkupAdminDataTable');
$table->setEncodeEntities(false);
$table->row(array($this->_x('Class', 'install-table'), $this->wire('sanitizer')->entities($data['class_name'])));
$table->row(array($this->_x('Version', 'install-table'), $this->wire('sanitizer')->entities($data['module_version']) . $moduleVersionNote));
$table->row(array($this->_x('Installed?', 'install-table'), $installedVersion));
$table->row(array($this->_x('Authors', 'install-table'), $this->wire('sanitizer')->entities($authors)));
$table->row(array($this->_x('Summary', 'install-table'), $this->wire('sanitizer')->entities($data['summary'])));
$table->row(array($this->_x('Release State', 'install-table'), $this->wire('sanitizer')->entities($data['release_state']['title'])));
$table->row(array($this->_x('Compatibility', 'install-table'), $this->wire('sanitizer')->entities($compat)));
// $this->message("<pre>" . print_r($data, true) . "</pre>", Notice::allowMarkup);
$installable = true;
if(!empty($data['requires_versions'])) {
$requiresVersions = array();
foreach($data['requires_versions'] as $name => $requires) {
list($op, $ver) = $requires;
$label = $ver ? $this->sanitizer->entities("$name $op $ver") : $this->sanitizer->entities($name);
if($this->modules->isInstalled("$name$op$ver") || in_array($name, $data['installs'])) {
// installed
$requiresVersions[] = "$label <i class='fa fa-fw fa-thumbs-up'></i>";
} else if($this->modules->isInstalled($name)) {
// installed, but version isn't adequate
$installable = false;
$info = $this->modules->getModuleInfo($name);
$requiresVersions[] = $this->sanitizer->entities($name) . " " . $this->modules->formatVersion($info['version']) . " " .
"<span class='ui-state-error-text'>" . $this->sanitizer->entities("$op $ver") . " " .
"<i class='fa fa-fw fa-thumbs-down'></i></span>";
} else {
// not installed at all
$requiresVersions[] = "<span class='ui-state-error-text'>$label <i class='fa fa-fw fa-thumbs-down'></i></span>";
$installable = false;
}
}
$table->row(array($this->labels['requires'], implode('<br />', $requiresVersions)));
if(!$installable) $this->error("Module is not installable because not all required dependencies are currently met.");
}
if(!empty($data['installs'])) {
$installs = $this->sanitizer->entities(implode("\n", $data['installs']));
$table->row(array($this->labels['installs'], nl2br($installs)));
}
$links = array();
$moduleName = wire('sanitizer')->entities1($data['name']);
$links[] = "<a target='_blank' href='http://modules.processwire.com/modules/$moduleName/'>" . $this->_('More Information') . "</a>";
if($data['project_url']) {
$projectURL = wire('sanitizer')->entities($data['project_url']);
$links[] = "<a target='_blank' href='$projectURL'>" . $this->_('Project Page') . "</a>";
}
if($data['forum_url']) {
$forumURL = wire('sanitizer')->entities($data['forum_url']);
$links[] = "<a target='_blank' href='$forumURL'>" . $this->_('Support Page') . "</a>";
}
if(count($links)) $table->row(array($this->_x('Links', 'install-table'), implode(' / ', $links)));
if($data['download_url']) {
$downloadURL = wire('sanitizer')->entities($data['download_url']);
$table->row(array($this->_x('ZIP file', 'install-table'), $downloadURL));
$warnings[] = $this->_('Ensure that you trust the source of the ZIP file above before continuing!');
} else {
$warnings[] = $this->_('This module has no download URL specified and must be installed manually.');
}
foreach($warnings as $warning) {
$table->row(array($this->_x('Please Note', 'install-table'), "<strong class='ui-state-error-text'> $warning</strong>"));
}
$markup->value = $table->render();
if($installable && $data['download_url']) {
$btn = $this->wire('modules')->get('InputfieldSubmit');
$btn->attr('id+name', 'godownload');
$btn->value = $this->labels['download_install'];
$btn->icon = 'cloud-download';
if($update) $btn->value .= " ($data[module_version])";
$form->add($btn);
$this->session->ProcessModuleDownloadURL = $data['download_url'];
$this->session->ProcessModuleClassName = $data['class_name'];
} else {
$this->session->remove('ProcessModuleDownloadURL');
$this->session->remove('ProcessModuleClassName');
}
$btn = $this->wire('modules')->get('InputfieldButton');
$btn->attr('name', 'cancel');
$btn->href = $update ? "./edit?name=$data[class_name]" : './';
$btn->value = $this->labels['cancel'];
$btn->icon = 'times-circle';
$btn->class .= ' ui-priority-secondary';
$form->add($btn);
$form->description = $this->wire('sanitizer')->entities($data['title']);
return $form;
}
/**
* Triggered on the /download/ action - Downloads a module from the directory
*
* Most code lifted from Soma's Modules Manager
*
* @return string Rendered output or redirect
* @throws WireException
*
*/
public function ___executeDownload() {
if(!$this->input->post->godownload) {
$this->message($this->_('Download cancelled'));
return $this->session->redirect('../');
}
$this->session->CSRF->validate();
$this->modules->resetCache();
$url = $this->session->ProcessModuleDownloadURL;
$className = $this->session->ProcessModuleClassName;
$this->session->remove('ProcessModuleDownloadURL');
$this->session->remove('ProcessModuleClassName');
if(!$url) throw new WireException("No download URL specified");
if(!$className) throw new WireException("No class name specified");
$destinationDir = $this->wire('config')->paths->siteModules . $className . '/';
$install = new ProcessModuleInstall();
$completedDir = $install->downloadModule($url, $destinationDir);
if($completedDir) {
return $this->buildDownloadSuccessForm($className)->render();
} else {
return $this->session->redirect('../');
}
}
/**
* Build the form that gets displayed after a module has been successfully downloaded
*
* @param string $className
* @return InputfieldForm
*
*/
protected function ___buildDownloadSuccessForm($className) {
$form = $this->modules->get('InputfieldForm');
// check if modules isn't already installed and this isn't an update
if(!$this->modules->isInstalled($className)) {
$info = $this->wire('modules')->getModuleInfoVerbose($className);
$requires = array();
if(count($info['requires'])) $requires = $this->modules->getRequiresForInstall($className);
if(count($requires)) {
foreach($requires as $moduleName) {
$this->error("$className - " . sprintf($this->_('Requires module "%s" before it can be installed'), $moduleName), Notice::warning | Notice::allowMarkup);
}
$this->wire('session')->redirect('../');
}
$this->wire('processHeadline', $this->_('Downloaded:') . ' ' . $className);
$form->description = sprintf($this->_('%s is ready to install'), $className);
$form->attr('action', '../');
$form->attr('method', 'post');
$form->attr('id', 'install_confirm_form');
$f = $this->modules->get('InputfieldHidden');
$f->attr('name', 'install');
$f->attr('value', $className);
$form->add($f);
$submit = $this->modules->get('InputfieldSubmit');
$submit->attr('name', 'submit');
$submit->attr('id', 'install_now');
$submit->attr('value', $this->_('Install Now'));
$submit->icon = 'sign-in';
$form->add($submit);
$button = $this->modules->get('InputfieldButton');
$button->attr('href', '../');
$button->attr('value', $this->_('Leave Uninstalled'));
$button->class .= " ui-priority-secondary";
$button->icon = 'times-circle';
$button->attr('id', 'no_install');
$form->add($button);
} else {
$this->wire('processHeadline', $this->_('Updated:') . ' ' . $className);
$form->description = sprintf($this->_('%s was updated successfully.'), $className);
$button = $this->modules->get('InputfieldButton');
$button->attr('href', "../?reset=1&edit=$className");
$button->attr('value', $this->_('Continue to module settings'));
$button->attr('id', 'gosettings');
$form->add($button);