-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOjtControlPanelPlugin.php
More file actions
1135 lines (921 loc) · 38 KB
/
OjtControlPanelPlugin.php
File metadata and controls
1135 lines (921 loc) · 38 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 APP\plugins\generic\ojtControlPanel;
use APP\facades\Repo;
use Exception;
use PKPApplication;
use ZipArchive;
use Monolog\Logger;
use PKP\plugins\Hook;
use RuntimeException;
use PKP\config\Config;
use PKP\security\Role;
use PKP\db\DAORegistry;
use PKP\plugins\Plugin;
use PKP\site\VersionDAO;
use APP\core\Application;
use PKP\file\FileManager;
use PKP\site\VersionCheck;
use PKP\cache\CacheManager;
use PKP\plugins\ThemePlugin;
use PKP\linkAction\LinkAction;
use PKP\plugins\GenericPlugin;
use PKP\plugins\LazyLoadPlugin;
use PKP\plugins\PluginRegistry;
use APP\template\TemplateManager;
use Monolog\Handler\StreamHandler;
use PKP\linkAction\request\OpenWindowAction;
use GuzzleHttp\Exception\BadResponseException;
use APP\plugins\generic\ojtControlPanel\classes\ErrorHandler;
use APP\plugins\generic\ojtControlPanel\classes\ParamHandler;
use APP\plugins\generic\ojtControlPanel\classes\ServiceHandler;
use APP\plugins\generic\ojtControlPanel\classes\ApiServicePanel;
use Monolog\Utils;
use PKP\userGroup\relationships\enums\UserUserGroupStatus;
use PKP\userGroup\UserGroup;
use Psr\Log\LogLevel;
use Throwable;
require_once(dirname(__FILE__) . '/vendor/autoload.php');
class OjtControlPanelPlugin extends GenericPlugin
{
public $registeredModule;
const API = "https://openjournaltheme.com/index.php/wp-json/openjournalvalidation/v3";
const SERVICE_API = "https://sp.openjournaltheme.com/";
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled()) {
register_shutdown_function([$this, 'fatalHandler']);
$this->init();
$this->setLogger();
$this->createModulesFolder();
$this->createStagingFolder();
$this->registerModules();
// HookRegistry::register('Template::Settings::website', array($this, 'settingsWebsite'));
Hook::add('LoadHandler', [$this, 'setPageHandler']);
Hook::add('TemplateManager::setupBackendPage', [$this, 'setupBackendPage']);
Hook::add('TemplateManager::display', [$this, 'fixThemeNotLoadedOnFrontend']);
// Hook::add('TemplateManager::display', [$this, 'addHeader']);
}
return true;
}
return false;
}
public function init()
{
$paramHandler = new ParamHandler($this);
$paramHandler->handle();
}
/**
* Determine whether the plugin can be enabled.
* @return boolean
*/
function getCanEnable(): bool
{
return $this->getCanDisable();
}
/**
* @copydoc Plugin::getCanDisable()
*/
function getCanDisable(): bool
{
if($this->isCurrentUserAreJournalManager()) return true;
$currentUser = $this->getRequest()->getUser();
if(!$currentUser) return false;
if(version_compare($this->getJournalVersion(), '35', '>=')) {
return $currentUser->hasRole([Role::ROLE_ID_SITE_ADMIN], Application::SITE_CONTEXT_ID);
}
return $currentUser->hasRole([Role::ROLE_ID_SITE_ADMIN], Application::CONTEXT_SITE);
}
public function isCurrentUserAreJournalManager(): bool
{
$currentUser = $this->getRequest()->getUser();
if(!$currentUser) return false;
$journalVersion = $this->getJournalVersion();
if(version_compare($journalVersion, '35', '>=')) {
$context = $this->getRequest()->getContext();
$currentUserGroups = UserGroup::query()
->withUserIds([$currentUser->getId()])
// ->withUserUserGroupStatus(UserUserGroupStatus::STATUS_ACTIVE->value)
->when($context, fn($query) => $query->withContextIds($context->getId()))
->lazy();
return $currentUserGroups->contains(fn($userGroup) => $userGroup->nameLocaleKey == 'default.groups.name.manager');
}
$currentUserGroups = Repo::userGroup()->userUserGroups($currentUser->getId());
$currentUserGroupNameLocaleKeys = collect($currentUserGroups->toArray())
->map(fn($userGroup) => $userGroup->getData('nameLocaleKey'))
->toArray();
return in_array('default.groups.name.manager', $currentUserGroupNameLocaleKeys);
}
public function apiUrl()
{
return static::API;
}
public static function get(): OjtControlPanelPlugin
{
$plugin = PluginRegistry::getPlugin('generic', 'OjtControlPanelPlugin');
if (!$plugin) return new static();
return $plugin;
}
public function isAllowSendLog($hour = 4): bool
{
$now = time();
$lastSendLogTime = $this->getSetting(Application::CONTEXT_SITE, 'lastSendLogTime');
if ($lastSendLogTime === null) {
return true;
}
$diff = $now - $lastSendLogTime;
$diffInHour = round($diff / (60 * 60));
return $diffInHour >= $hour;
}
public function getHttpClient($headers = [])
{
$versionDao = DAORegistry::getDAO('VersionDAO');
/** @var VersionDAO $versionDao */
$version = $versionDao->getCurrentVersion();
$agents = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100508 SeaMonkey/2.0.4',
'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; da-dk) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'
];
$headers = array_merge($headers, [
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => $agents[rand(0, 3)],
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Headers' => 'x-csrf-uap-admin-token',
'ojt-plugin-version' => $this->getPluginVersion(),
'ojs-version' => $this->getJournalVersion(),
'ojs-version-detail' => $version->getVersionString(),
'journal-url' => $this->getJournalUrl(),
'php-version' => PHP_VERSION,
]);
return new \GuzzleHttp\Client([
'timeout' => 60,
'headers' => $headers
]);
}
/**
* Remove modules disaat terjadi fatal error
*/
function fatalHandler()
{
$error = error_get_last();
// Sometimes fatalHandler called without error
if (!is_array($error)) return;
// Fatal error, E_ERROR === 1
if (!isset($error['type']) || !in_array($error['type'], [E_ERROR, E_COMPILE_ERROR], true)) return;
// Sometime there's no file in error so we need to check it first
if (!array_key_exists('file', $error)) return;
if (!str_contains($error['file'], 'ojtControlPanel')) {
return;
}
error_log('OJTErrorPlugin : ' . $error['message'] . ', File : ' . $error['file'] . ' - ' . $error['line']);
/**
* Get folder name from error file
*/
$folders = explode('/', $error['file']);
$key = array_search('modules', $folders);
if (!is_int($key)) {
return;
}
$errorPluginFolder = $folders[$key + 1];
$path = __DIR__ . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $errorPluginFolder;
try {
if (!is_dir($path)) {
throw new Exception("$path is not directory");
return;
}
$this->recursiveDelete($path);
} catch (\Throwable $th) {
}
}
public static function getErrorLogFile(): string
{
return Config::getVar('files', 'files_dir') . DIRECTORY_SEPARATOR . 'ojtPlugin' . DIRECTORY_SEPARATOR . 'error.log';
}
public static function deleteLogFile(): bool
{
$errorLogFile = static::getErrorLogFile();
if (!is_file($errorLogFile)) return false;
return unlink($errorLogFile);
}
public function isTimeToDeleteLog($days = 2)
{
$errorLogFile = static::getErrorLogFile();
if (!is_file($errorLogFile)) return false;
$dateCreatedFile = filemtime($errorLogFile);
$now = time();
$datediff = $now - $dateCreatedFile;
$diffInDays = round($datediff / (60 * 60 * 24));
return $diffInDays > $days;
}
public function setLogger()
{
// Jangan simpan log error ketika setting ini didisable
if (!$this->isDiagnosticEnabled()) return;
$logger = new Logger('OJTLog');
$logger->pushHandler(new StreamHandler(static::getErrorLogFile()));
$logger->pushHandler(new ServiceHandler());
set_exception_handler(function (Throwable $e) use ($logger): void {
if ($this->isTimeToDeleteLog()) {
static::deleteLogFile();
};
$logger->log(
LogLevel::ERROR,
sprintf('Uncaught Exception %s: "%s" at %s line %s', Utils::getClass($e), $e->getMessage(), $e->getFile(), $e->getLine()),
['exception' => $e]
);
throw $e;
});
set_error_handler(function (int $code, string $message, string $file = '', int $line = 0, ?array $context = []) use ($logger): bool {
if ($code !== E_ERROR) return false;
if ($this->isTimeToDeleteLog()) {
static::deleteLogFile();
};
$logger->log(LogLevel::CRITICAL, 'E_ERROR: ' . $message, ['code' => $code, 'message' => $message, 'file' => $file, 'line' => $line]);
return false;
});
}
public function fixThemeNotLoadedOnFrontend($hookName, $args)
{
$templateMgr = $args[0];
if ($templateMgr->getTemplateVars('activeTheme')) return;
$allThemes = PluginRegistry::loadCategory('themes', true);
$activeTheme = null;
$context = $this->getCurrentContextId() ? $this->getRequest()->getContext() : $this->getRequest()->getSite();
$themePluginPath = $context->getData('themePluginPath');
foreach ($allThemes as $theme) {
if ($themePluginPath === basename($theme->pluginPath) && $theme->getEnabled()) {
$activeTheme = $theme;
break;
}
}
$templateMgr->assign('activeTheme', $activeTheme);
}
function addHeader($hookName, $args)
{
$templateMgr = &$args[0];
$templateMgr->addHeader(
'ojtcontrolpanel',
'<meta name="ojtcontrolpanel" content="OJT Control Panel Version ' . $this->getPluginVersion() . ' by openjournaltheme.com">',
[
'contexts' => ['frontend'],
]
);
}
public function flushCache()
{
$templateMgr = TemplateManager::getManager($this->getRequest());
$templateMgr->clearTemplateCache();
$templateMgr->clearCssCache();
$cacheMgr = CacheManager::getManager();
$cacheMgr->flush();
}
public function setupBackendPage($hookName, $args)
{
$request = $this->getRequest();
if (!$request->getContext()) return;
$templateMgr = TemplateManager::getManager($this->getRequest());
$router = $request->getRouter();
$userRoles = (array) $router->getHandler()->getAuthorizedContextObject(Application::ASSOC_TYPE_USER_ROLES);
$user = $request->getUser();
if (!$user || !count(array_intersect([Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN], $userRoles))) return;
$menu = $templateMgr->getState('menu');
$menu['ojtPlugin'] = [
'name' => 'OJT Control Panel',
'url' => $request->getDispatcher()->url($request, Application::ROUTE_PAGE, $request->getContext()->getPath(), 'ojt') . '?PageSpeed=off',
"isCurrent" => false,
];
if($this->getSetting($this->getCurrentContextId(), 'show_support_link_ojs') ?? true){
$menu['ojtSupportTicketing'] = [
'name' => 'Get OJT support',
'url' => $request->getDispatcher()->url($request, Application::ROUTE_PAGE, $request->getContext()->getPath(), 'ojt', 'support'),
"isCurrent" => false
];
}
$templateMgr->setState(['menu' => $menu]);
}
public function getModulesPath($path = '')
{
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $path;
}
/**
* Get staging base path for plugin installation
*
* @param string $path Optional subdirectory path
* @return string Full path to staging directory
*/
public function getStagingBasePath($path = '')
{
$basePath = Config::getVar('files', 'files_dir') . DIRECTORY_SEPARATOR . 'ojtPlugin' . DIRECTORY_SEPARATOR . 'staging';
return $path ? $basePath . DIRECTORY_SEPARATOR . $path : $basePath;
}
public function registerModules(): void
{
$modulesFolder = $this->getDirs($this->getModulesPath());
$plugins = [];
foreach ($modulesFolder as $moduleFolder) {
$plugin = $this->instatiatePlugin($moduleFolder);
$versionFile = $this->getModulesPath($moduleFolder . DIRECTORY_SEPARATOR . "version.xml");
$version = VersionCheck::getValidPluginVersionInfo($versionFile);
$versionDao = DAORegistry::getDAO('VersionDAO');
$versionDao->disableVersion($version->getData('productType'), $version->getData('product'));
$categoryPlugin = explode('.', $version->getData('productType'))[1];
$categoryDir = $this->getModulesPath();
$pluginDir = $categoryDir . $moduleFolder;
$contextSite = version_compare($this->getJournalVersion(), '35', '>=') ? Application::SITE_CONTEXT_ID : Application::CONTEXT_SITE;
if ($plugin->getEnabled() || $this->getCurrentContextId() == $contextSite) {
PluginRegistry::register($categoryPlugin, $plugin, $pluginDir);
if ($plugin instanceof ThemePlugin) {
$plugin->init();
}
} else {
// manually load locale data for disabled plugins
$plugin->pluginPath = $pluginDir;
$plugin->addLocaleData();
}
$data = $version->getAllData();
$data['version'] = $version->getVersionString();
$data['name'] = $plugin->getDisplayName();
$data['className'] = $plugin->getName();
$data['description'] = $plugin->getDescription();
$data['enabled'] = $plugin->getEnabled();
if($this->getRequest()->getUser() && method_exists($plugin, 'getCanEnable') && !$plugin->getCanEnable()) {
$data['isAuthorized'] = $plugin->getCanEnable();
} else {
$data['isAuthorized'] = $this->getCanEnable();
}
$data['open'] = false;
$data['icon'] = method_exists($plugin, 'getPageIcon') ? $plugin->getPageIcon() : $this->getDefaultPluginIcon();
$data['documentation'] = method_exists($plugin, 'getDocumentation') ? $plugin->getDocumentation() : null;
$data['page'] = method_exists($plugin, 'getPage') ? $plugin->getPage() : null;
$data['canDelete'] = method_exists($plugin, 'getCanDelete') ? $plugin->getCanDelete() : true;
$plugins[] = $data;
}
$this->registeredModule = $plugins;
}
public function getRegisteredModules()
{
if (!$this->registeredModule) {
$this->registerModules();
}
return $this->registeredModule;
}
public static function reportToServicePanel($plugin, $isGlobalPlugin = false, $params = [], $force = false)
{
$ojtPlugin = new self();
if (!$plugin->getEnabled()) return;
if (!$force) {
$serviceData = $plugin->getSetting(Application::CONTEXT_SITE, 'service_panel_data');
if ($serviceData && isset($serviceData['url'])) {
$serviceData['journal_site'] = $serviceData['url'];
unset($serviceData['url']);
$plugin->updateSetting(Application::CONTEXT_SITE, 'service_panel_data', $serviceData);
}
if ($serviceData) return;
}
$apiService = ApiServicePanel::make($plugin);
if (!isset($params['product-class'])) {
$params['product-class'] = get_class($plugin);
}
if ($isGlobalPlugin) {
$headers['Client-Url'] = $plugin->getRequest()->getBaseUrl();
} else {
$headers ['Client-Url'] = $ojtPlugin->getJournalURL();
}
try {
$response = $apiService->registerClient($params, $headers);
if ($response['status']) {
$plugin->updateSetting(Application::CONTEXT_SITE, 'service_panel_data', $response['journal_data']);
}
return true;
} catch (\Throwable $th) {
// throw $th;
error_log("Report to Service Panel Error: " . $th->getMessage());
return false;
}
}
public function getDefaultPluginIcon()
{
$templateMgr = TemplateManager::getManager($this->getRequest());
return $templateMgr->fetch($this->getTemplateResource('defaultIcon.tpl'));
}
public function createModulesFolder()
{
if (is_dir(getcwd() . DIRECTORY_SEPARATOR . $this->getModulesPath())) {
return;
}
mkdir(getcwd() . DIRECTORY_SEPARATOR . $this->getModulesPath());
}
/**
* Create staging folder for plugin installation with permission validation
*
* @throws Exception if directory cannot be created or is not writable
* @return void
*/
public function createStagingFolder()
{
$stagingPath = $this->getStagingBasePath();
if (is_dir($stagingPath)) {
// Verify directory is writable
if (!is_writable($stagingPath)) {
error_log("Staging directory exists but is not writable: {$stagingPath}");
}
return;
}
// Create directory recursively
if (!mkdir($stagingPath, 0755, true)) {
error_log("Failed to create staging directory: {$stagingPath}");
throw new Exception("Unable to create staging directory. Please check file permissions.");
}
// Verify the created directory is writable
if (!is_writable($stagingPath)) {
error_log("Created staging directory but it's not writable: {$stagingPath}");
throw new Exception("Staging directory created but not writable. Please check file permissions.");
}
}
/**
* Cleanup old .staging directories from modules folder (backward compatibility)
* This removes orphaned .staging directories from the old staging location
*
* @return void
*/
protected function cleanupOldModuleStagingDirectories()
{
$oldStagingPath = $this->getModulesPath('.staging');
if (!is_dir($oldStagingPath)) {
return;
}
try {
$this->recursiveDelete($oldStagingPath);
error_log("Cleaned up old staging directory from modules: {$oldStagingPath}");
} catch (Exception $e) {
error_log("Failed to cleanup old staging directory: " . $e->getMessage());
}
}
// Show available update on Setting -> Website
function settingsWebsite($hookName, $args)
{
if (!$this->getSetting(Application::CONTEXT_SITE, 'isNewVersionAvailable')) {
return false;
}
$templateMgr = $args[1];
$output = &$args[2];
$output .= $templateMgr->fetch($this->getTemplateResource('backend/notif.tpl'));
// Permit other plugins to continue interacting with this hook
return false;
}
public function updatePanel($url)
{
// Check ziparchive extension
if (!class_exists('ZipArchive')) {
throw new Exception('Please Install PHP Zip Extension');
}
// Download file
$file_name = Config::getVar('files', 'files_dir') . DIRECTORY_SEPARATOR . 'OJTPanel.zip';
$resource = \GuzzleHttp\Psr7\Utils::tryFopen($file_name, 'w');
$stream = \GuzzleHttp\Psr7\Utils::streamFor($resource);
$this->getHttpClient()->request('GET', $url, ['sink' => $stream]);
$zip = new \ZipArchive;
if (!$zip->open($file_name)) {
unlink($file_name);
throw new Exception('Failed to Open Files plugin file');
}
$path = 'plugins' . DIRECTORY_SEPARATOR . 'generic';
if (!$zip->extractTo($path)) {
unlink($file_name);
throw new Exception('Failed to Extract Plugin,maybe because of folder permission.');
}
$zip->close();
unlink($file_name);
}
/**
* Install default settings on journal creation.
* @return string
*/
public function getContextSpecificPluginSettingsFile(): string
{
return $this->getPluginPath() . '/settings.xml';
}
public function getPluginVersionFile(): string
{
$pluginPath = $this->getPluginPath() ?? 'plugins/generic/ojtControlPanel';
return $pluginPath . '/version.xml';
}
/**
* Get the display name of this plugin.
* @return String
*/
public function getDisplayName(): string
{
return 'OJT Control Panel';
}
/**
* Get a description of the plugin.
*/
public function getDescription(): string
{
return 'Control Panel Service Plugin From OpenJournalTheme.com';
}
public function getPluginType()
{
$info = VersionCheck::getValidPluginVersionInfo($this->getPluginVersionFile());
return $info[1];
}
public function getName()
{
return 'OjtControlPanelPlugin';
}
public function getPluginVersion()
{
$version = VersionCheck::parseVersionXML($this->getPluginVersionFile());
return $version['release'];
}
public function getPluginName(): String
{
return $this->getName();
}
public function getPluginFullUrl($path = '', $withVersion = true)
{
$fullUrl = $this->getRequest()->getBaseUrl() . '/' . $this->getPluginPath() . '/' . $path;
if ($withVersion) {
return $fullUrl . '?v=' . $this->getPluginVersion();
}
return $fullUrl;
}
public function setPageHandler($hookName, $params)
{
$handler = &$params[3];
if ($this->getCurrentContextId() == 0) {
// Panel tidak support untuk sitewide
return false;
}
$page = $params[0];
// Force http or https based on host
$request = Application::get()->getRequest();
$serverHost = $request->getServerHost(null, false);
$host = explode(':', (string) $serverHost)[0];
$shouldUseHttpProtocol = $this->shouldUseHttpProtocolForHost($host);
$request->_protocol = $shouldUseHttpProtocol ? 'http' : 'https';
switch ($page) {
case 'ojt':
$handler = new OjtPageHandler($this->getRequest());
return true;
break;
}
return false;
}
private function shouldUseHttpProtocolForHost(string $host)
{
if (in_array($host, ['localhost', '127.0.0.1', '::1'], true)) {
return true;
}
if (preg_match('/^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)/', $host)) {
return true;
}
return false;
}
/**
* Add a settings action to the plugin's entry in the
* plugins list.
*
* @param Request $request
* @param array $actionArgs
* @return array
*/
public function getActions($request, $actionArgs)
{
// Get the existing actions
$actions = parent::getActions($request, $actionArgs);
// Only add the settings action when the plugin is enabled
if (!$this->getEnabled()) {
return $actions;
}
$linkAction = new LinkAction(
'ojt_control_panel',
new OpenWindowAction($request->getDispatcher()->url($request, Application::ROUTE_PAGE, $request->getContext()->getPath()) . '/ojt?PageSpeed=off'),
'Control Panel',
null
);
// Add the LinkAction to the existing actions.
// Make it the first action to be consistent with
// other plugins.
array_unshift($actions, $linkAction);
return $actions;
}
/**
* Check the folder this $folder is.
* @return bool - true if folder exist
*/
public function isPluginExist($folder)
{
if (!$folder) {
return false;
}
return is_dir(getcwd() . DIRECTORY_SEPARATOR . $this->getModulesPath() . $folder);
}
/**
* Removing plugin folder
* @return bool - true if success.
*/
public function uninstallPlugin($plugin): bool
{
$path = $this->getModulesPath($plugin->product);
if ($plugin->sitewide == true) {
$path = 'plugins' . DIRECTORY_SEPARATOR . 'generic' . DIRECTORY_SEPARATOR . $plugin->product;
}
try {
if (!is_dir($path)) {
throw new Exception("$plugin->name not Found");
}
return $this->recursiveDelete($path);
} catch (\Throwable $th) {
throw $th;
}
}
public function recursiveDelete($dirPath, $deleteParent = true): bool
{
$fileManager = new FileManager();
return $fileManager->rmtree($dirPath);
}
public function getJournalURL(): string
{
$request = $this->getRequest();
return $request->getDispatcher()->url($request, Application::ROUTE_PAGE, $request->getContext()->getPath());
}
public function getPluginDownloadLink($pluginToken, $license = false): array
{
try {
$payload = [
'token' => $pluginToken,
'license' => $license,
'journal_url' => $this->getJournalURL(),
'ojs_version' => $this->getJournalVersion()
];
$request = $this->getHttpClient(['Content-Type' => 'application/x-www-form-urlencoded',])
->post(
static::API . '/product/get_download_link',
[
'form_params' => $payload,
]
);
$response = json_decode((string) $request->getBody(), true);
if (isset($response['error']) && $response['error']) throw new Exception($response['msg']);
$result['product'] = $response['data']['download_link'];
$dependencies = [];
foreach ($response['data']['dependencies'] as $dependency) {
$data['link'] = $dependency['download_link'];
$data['folder'] = $dependency['folder'];
$dependencies[] = $data;
}
$result['dependencies'] = $dependencies;
return $result;
} catch (BadResponseException $e) {
throw $e;
} catch (Exception $e) {
throw $e;
}
}
/**
* Installing plugin to staging directory first for validation.
* Returns staging info for caller to validate and move to final location.
*
* @param string $url Plugin download URL
* @return array ['stagingPath' => string, 'pluginFolder' => string, 'stagingId' => string]
* @throws Exception on download or extraction failure
*/
public function installPlugin($url)
{
$url = str_replace('https', 'http', $url);
$stagingId = time() . '_' . uniqid();
// unique temporary file name to prevent conflicts
$file_name = Config::getVar('files', 'files_dir') . DIRECTORY_SEPARATOR . "OJTTemp_{$stagingId}.zip";
try {
$resource = \GuzzleHttp\Psr7\Utils::tryFopen($file_name, 'w');
$stream = \GuzzleHttp\Psr7\Utils::streamFor($resource);
$this->getHttpClient()->request('GET', $url, ['sink' => $stream]);
if (!class_exists('ZipArchive')) {
throw new Exception('Please Install PHP Zip Extension');
}
$zip = new ZipArchive;
if (!$zip->open($file_name)) {
throw new Exception('Failed to Open Files');
}
$stagingPath = $this->getStagingBasePath($stagingId);
if (!is_dir($stagingPath)) {
if (!mkdir($stagingPath, 0755, true)) {
throw new Exception('Failed to create staging directory. Please check file permissions.');
}
}
if (!$zip->extractTo($stagingPath)) {
throw new Exception('Failed to Extract Plugin, because of folder permission.');
}
$extractedFolders = array_diff(scandir($stagingPath), ['.', '..']);
if (count($extractedFolders) !== 1) {
throw new Exception('Invalid plugin archive structure: expected single root folder');
}
$pluginFolder = reset($extractedFolders);
$zip->close();
unlink($file_name);
return [
'stagingPath' => $stagingPath,
'pluginFolder' => $pluginFolder,
'stagingId' => $stagingId
];
} catch (Exception $e) {
if (file_exists($file_name)) {
unlink($file_name);
}
if (isset($stagingPath) && is_dir($stagingPath)) {
$this->recursiveDelete($stagingPath);
}
throw $e;
}
}
public function getJournalVersion()
{
$versionDao = DAORegistry::getDAO('VersionDAO');
/** @var VersionDAO $versionDao */
$version = $versionDao->getCurrentVersion();
$data = $version->_data;
return $data['major'] . $data['minor'];
}
public function clearDataCache()
{
$pluginSettingsDAO = DAORegistry::getDAO('PluginSettingsDAO'); // As good as any
$pluginSettingsDAO->flushCache();
return true;
}
function getDirs($path, $recursive = false, array $filtered = [])
{
$this->createModulesFolder();
if (!is_dir($path)) {
throw new RuntimeException("$path does not exist.");
}
$filtered += ['.', '..', '.git', 'pluginTemplate', '.staging', 'staging'];
$dirs = [];
$d = dir($path);
while (($entry = $d->read()) !== false) {
if (is_dir("$path" . DIRECTORY_SEPARATOR . "$entry") && !in_array($entry, $filtered)) {
$dirs[] = $entry;
if ($recursive) {
$newDirs = $this->getDirs("$path" . DIRECTORY_SEPARATOR . "$entry");
foreach ($newDirs as $newDir) {
$dirs[] = "$path" . DIRECTORY_SEPARATOR . "$entry";
}
}
}
}
sort($dirs);
return $dirs;
}
public function isDiagnosticEnabled()
{
return $this->getSetting(Application::CONTEXT_SITE, 'enable_diagnostic') ?? true;
}
public function instatiatePlugin($moduleFolder): LazyLoadPlugin
{
$fileManager = new FileManager();
$plugin = null;
$versionFile = $this->getModulesPath($moduleFolder . DIRECTORY_SEPARATOR . "version.xml");
if (
!$fileManager->fileExists($versionFile)
) {
throw new Exception("Plugin $moduleFolder not found");
}
$version = VersionCheck::getValidPluginVersionInfo($versionFile);
$pluginClassName = __NAMESPACE__ . "\\modules\\{$moduleFolder}\\" . $version->getProductClassName();
if (!class_exists($pluginClassName)) {
$indexFile = $this->getModulesPath(DIRECTORY_SEPARATOR . $moduleFolder . DIRECTORY_SEPARATOR . "index.php");
if (!$fileManager->fileExists($indexFile)) {
// throw new Exception("Plugin with classname : $pluginClassName not found");
}
$plugin = @include($indexFile);
}
$plugin = $plugin ?? new $pluginClassName();
if (!$plugin && $plugin instanceof Plugin) {
throw new Exception("Plugin with classname : $pluginClassName not found");
}
return $plugin;
}
public function instatiantePluginWithoutThrow($moduleFolder): ?LazyLoadPlugin
{
try {
return $this->instatiatePlugin($moduleFolder);
} catch (\Throwable $th) {
return null;
}
}
/**
* Instantiate a plugin from the global plugins directory (not modules)
* This is used for site-wide plugins that have been moved to plugins/generic/
*
* @param string $pluginFolder The plugin folder name