• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visavi / rotor / 28340133337

28 Jun 2026 11:47PM UTC coverage: 16.561% (+0.09%) from 16.474%
28340133337

push

github

visavi
Ядро и модули переведены на datetime, удалена константа SITETIME

18 of 95 new or added lines in 31 files covered. (18.95%)

7 existing lines in 6 files now uncovered.

989 of 5972 relevant lines covered (16.56%)

2.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/Http/Controllers/Admin/ModuleController.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Http\Controllers\Admin;
6

7
use App\Models\Module;
8
use App\Models\ModuleRegistry;
9
use App\Providers\ModuleServiceProvider;
10
use Illuminate\Http\RedirectResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Artisan;
13
use Illuminate\Support\Facades\Http;
14
use Illuminate\View\View;
15
use ZipArchive;
16

17
class ModuleController extends AdminController
18
{
19
    /**
20
     * Главная страница
21
     */
22
    public function index(): View
×
23
    {
24
        $modules = Module::query()->get();
×
25
        $moduleInstall = [];
×
26
        foreach ($modules as $module) {
×
27
            $moduleInstall[$module->name] = $module;
×
28
        }
29

30
        $moduleNames = [];
×
31
        $modulesLoaded = glob(base_path('modules/*'), GLOB_ONLYDIR);
×
32
        foreach ($modulesLoaded as $module) {
×
33
            if (file_exists($module . '/module.php')) {
×
34
                $moduleNames[basename($module)] = include $module . '/module.php';
×
35
            }
36
        }
37

38
        $installed = array_intersect_key($moduleInstall, $moduleNames);
×
39
        $counts = [
×
40
            'all'           => count($moduleNames),
×
41
            'installed'     => count(array_filter($installed, fn ($m) => $m->active)),
×
42
            'disabled'      => count(array_filter($installed, fn ($m) => ! $m->active)),
×
43
            'not-installed' => count($moduleNames) - count($installed),
×
44
        ];
×
45

46
        $registryModules = ModuleRegistry::getAvailableModules();
×
47
        $failedModules = ModuleServiceProvider::$failed;
×
48

49
        return view('admin/modules/index', compact('moduleInstall', 'moduleNames', 'counts', 'registryModules', 'failedModules'));
×
50
    }
51

52
    /**
53
     * Просмотр модуля
54
     */
55
    public function module(Request $request): View
×
56
    {
57
        $moduleName = (string) $request->input('module');
×
58
        $modulePath = base_path('modules/' . $moduleName);
×
59

60
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
61
            abort(200, __('admin.modules.module_not_found'));
×
62
        }
63

64
        $moduleConfig = include $modulePath . '/module.php';
×
65
        $module = Module::query()->where('name', $moduleName)->first();
×
66

67
        if (file_exists($modulePath . '/screenshots')) {
×
68
            $moduleConfig['screenshots'] = glob($modulePath . '/screenshots/*.{gif,png,jpg,jpeg,webp}', GLOB_BRACE);
×
69
        }
70

71
        if (file_exists($modulePath . '/database/migrations')) {
×
72
            $migrations = [];
×
73
            foreach (glob($modulePath . '/database/migrations/*.php') as $migration) {
×
74
                $migrations[basename($migration)] = file_get_contents($migration);
×
75
            }
76
            $moduleConfig['migrations'] = $migrations;
×
77
        }
78

79
        if (file_exists($modulePath . '/resources/assets')) {
×
80
            $moduleConfig['symlink'] = Module::getLinkNameByPath($modulePath);
×
81
        }
82

83
        if (file_exists($modulePath . '/config.php')) {
×
84
            $moduleConfig['config'] = file_get_contents($modulePath . '/config.php');
×
85
        }
86

87
        if (file_exists($modulePath . '/routes.php')) {
×
88
            $moduleConfig['routes'] = file_get_contents($modulePath . '/routes.php');
×
89
        }
90

91
        if (file_exists($modulePath . '/hooks.php')) {
×
92
            $moduleConfig['hooks'] = file_get_contents($modulePath . '/hooks.php');
×
93
        }
94

95
        if (file_exists($modulePath . '/helpers.php')) {
×
96
            $moduleConfig['helpers'] = file_get_contents($modulePath . '/helpers.php');
×
97
        }
98

99
        if (file_exists($modulePath . '/middleware.php')) {
×
100
            $moduleConfig['middleware'] = file_get_contents($modulePath . '/middleware.php');
×
101
        }
102

103
        foreach (['changelog.md', 'CHANGELOG.md'] as $changelog) {
×
104
            if (file_exists($modulePath . '/' . $changelog)) {
×
105
                $moduleConfig['changelog'] = file_get_contents($modulePath . '/' . $changelog);
×
106
                break;
×
107
            }
108
        }
109

110
        $registryInfo = ModuleRegistry::getAvailableModules()[$moduleName] ?? null;
×
111

112
        return view('admin/modules/module', compact('module', 'moduleConfig', 'moduleName', 'registryInfo'));
×
113
    }
114

115
    /**
116
     * Установка модуля
117
     */
118
    public function install(Request $request): RedirectResponse
×
119
    {
120
        $moduleName = $request->input('module');
×
121
        $enable = int($request->input('enable'));
×
122
        $update = int($request->input('update'));
×
123
        $modulePath = base_path('modules/' . $moduleName);
×
124

125
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
126
            abort(200, __('admin.modules.module_not_found'));
×
127
        }
128

129
        $module = Module::query()->firstOrNew(['name' => $moduleName]);
×
130

131
        $moduleConfig = include $modulePath . '/module.php';
×
132

133
        $requires = $moduleConfig['requires'] ?? null;
×
134
        if ($requires && version_compare(ROTOR_VERSION, $requires, '<')) {
×
135
            setFlash('danger', __('admin.modules.requires') . ' ' . $requires . '!');
×
136

137
            return redirect('admin/modules/module?module=' . $moduleName);
×
138
        }
139

140
        // Файлы на диск кладём только для активного модуля: свежая установка,
141
        // включение или обновление уже активного. Обновление выключенного модуля
142
        // лишь повышает версию — его файлы не должны возвращаться на диск.
143
        if (! $module->exists || $enable || $module->active) {
×
144
            $module->createSymlink();
×
145
            $module->publish();
×
146
            $module->migrate();
×
147
        }
148

149
        Artisan::call('route:clear');
×
150
        $result = __('admin.modules.module_success_installed');
×
151

152
        if ($module->exists) {
×
153
            if ($update) {
×
154
                $module->update([
×
NEW
155
                    'version' => $moduleConfig['version'],
×
156
                ]);
×
157
                $result = __('admin.modules.module_success_updated');
×
158
            }
159

160
            if ($enable) {
×
161
                $module->update([
×
NEW
162
                    'active' => true,
×
163
                ]);
×
164
                $result = __('admin.modules.module_success_enabled');
×
165
            }
166
        } else {
167
            $module->fill([
×
NEW
168
                'version' => $moduleConfig['version'],
×
169
            ])->save();
×
170
        }
171

172
        // Полная синхронизация активных модулей: порядок установки перестаёт
173
        // иметь значение (напр. перевод модуля-языка подмешается в Форум,
174
        // даже если Форум поставили позже). Сама сбрасывает кэш модулей.
175
        Module::syncAll();
×
176

177
        setFlash('success', $result);
×
178

179
        return redirect('admin/modules/module?module=' . $moduleName);
×
180
    }
181

182
    /**
183
     * Каталог модулей из реестров
184
     */
185
    public function marketplace(Request $request): View
×
186
    {
187
        $force = (bool) $request->input('refresh');
×
188
        $available = ModuleRegistry::getAvailableModules($force);
×
189

190
        $modules = Module::query()->get()->keyBy('name');
×
191
        $moduleNames = [];
×
192

193
        $modulesLoaded = glob(base_path('modules/*'), GLOB_ONLYDIR);
×
194
        foreach ($modulesLoaded as $module) {
×
195
            $moduleNames[] = basename($module);
×
196
        }
197

198
        $counts = ['all' => count($available), 'installed' => 0, 'disabled' => 0, 'not-installed' => 0];
×
199
        foreach ($available as $name => $info) {
×
200
            $localExists = in_array($name, $moduleNames, true);
×
201
            $installed = $modules->has($name) && $localExists;
×
202

203
            if ($installed) {
×
204
                $counts[$modules[$name]->active ? 'installed' : 'disabled']++;
×
205
            } else {
206
                $counts['not-installed']++;
×
207
            }
208
        }
209

210
        return view('admin/modules/marketplace', compact('available', 'modules', 'moduleNames', 'counts'));
×
211
    }
212

213
    /**
214
     * Форма загрузки модуля
215
     */
216
    public function upload(): View
×
217
    {
218
        return view('admin/modules/upload');
×
219
    }
220

221
    /**
222
     * Установка модуля из ZIP-файла
223
     */
224
    public function uploadZip(Request $request): RedirectResponse
×
225
    {
226
        if (! $request->hasFile('zip') || ! $request->file('zip')->isValid()) {
×
227
            setFlash('danger', __('admin.modules.upload_invalid_file'));
×
228

229
            return redirect()->route('admin.modules.upload');
×
230
        }
231

232
        try {
233
            $moduleName = $this->extractZip($request->file('zip')->getPathname());
×
234
        } catch (\Exception $e) {
×
235
            setFlash('danger', $e->getMessage());
×
236

237
            return redirect()->route('admin.modules.upload');
×
238
        }
239

240
        return redirect('/admin/modules/module?module=' . $moduleName)
×
241
            ->with('success', __('admin.modules.upload_success_extracted'));
×
242
    }
243

244
    /**
245
     * Установка модуля по URL
246
     */
247
    public function download(Request $request): RedirectResponse
×
248
    {
249
        $url = trim($request->input('url', ''));
×
250

251
        if (! filter_var($url, FILTER_VALIDATE_URL) || ! in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true)) {
×
252
            setFlash('danger', __('admin.modules.download_invalid_url'));
×
253

254
            return redirect()->back();
×
255
        }
256

257
        $maxSize = (int) config('modules.download_max_size') * 1024 * 1024;
×
258

259
        $tempDir = storage_path('app/temp');
×
260
        if (! is_dir($tempDir)) {
×
261
            mkdir($tempDir, 0755, true);
×
262
        }
263
        $tempFile = $tempDir . '/rotor_module_' . uniqid() . '.zip';
×
264

265
        try {
266
            // Потоковая запись: лимит проверяется по мере чтения, тело не держим в памяти
267
            $response = Http::timeout(30)->withOptions(['stream' => true])->get($url);
×
268

269
            if (! $response->ok()) {
×
270
                setFlash('danger', __('admin.modules.download_failed'));
×
271

272
                return redirect()->back();
×
273
            }
274

275
            $stream = $response->toPsrResponse()->getBody();
×
276
            $handle = fopen($tempFile, 'wb');
×
277

278
            if ($handle === false) {
×
279
                setFlash('danger', __('admin.modules.download_failed'));
×
280

281
                return redirect()->back();
×
282
            }
283
            $written = 0;
×
284
            $tooLarge = false;
×
285

286
            while (! $stream->eof()) {
×
287
                $chunk = $stream->read(8192);
×
288
                $written += strlen($chunk);
×
289

290
                if ($written > $maxSize) {
×
291
                    $tooLarge = true;
×
292
                    break;
×
293
                }
294

295
                fwrite($handle, $chunk);
×
296
            }
297

298
            fclose($handle);
×
299

300
            if ($tooLarge) {
×
301
                @unlink($tempFile);
×
302
                setFlash('danger', __('admin.modules.download_too_large', ['size' => formatSize($maxSize)]));
×
303

304
                return redirect()->back();
×
305
            }
306

307
            if (file_get_contents($tempFile, false, null, 0, 4) !== "PK\x03\x04") {
×
308
                @unlink($tempFile);
×
309
                setFlash('danger', __('admin.modules.download_not_zip'));
×
310

311
                return redirect()->back();
×
312
            }
313

314
            try {
315
                $moduleName = $this->extractZip($tempFile);
×
316
            } finally {
317
                @unlink($tempFile);
×
318
            }
319
        } catch (\Exception $e) {
×
320
            @unlink($tempFile);
×
321
            setFlash('danger', $e->getMessage());
×
322

323
            return redirect()->back();
×
324
        }
325

326
        // Для уже установленного модуля распаковка — лишь первый шаг обновления:
327
        // подсказываем, что версия применится по кнопке «Применить обновление»
328
        $extracted = Module::query()->where('name', $moduleName)->exists()
×
329
            ? __('admin.modules.update_extracted')
×
330
            : __('admin.modules.upload_success_extracted');
×
331

332
        return redirect('/admin/modules/module?module=' . $moduleName)
×
333
            ->with('success', $extracted);
×
334
    }
335

336
    /**
337
     * Распаковка ZIP-архива модуля
338
     */
339
    private function extractZip(string $zipPath): string
×
340
    {
341
        $zip = new ZipArchive();
×
342

343
        if ($zip->open($zipPath) !== true) {
×
344
            throw new \RuntimeException(__('admin.modules.zip_open_failed'));
×
345
        }
346

347
        $topDirs = [];
×
348
        for ($i = 0; $i < $zip->numFiles; $i++) {
×
349
            $name = $zip->getNameIndex($i);
×
350

351
            if (str_contains($name, '..')) {
×
352
                $zip->close();
×
353
                throw new \RuntimeException(__('admin.modules.zip_invalid_path'));
×
354
            }
355

356
            $parts = explode('/', $name);
×
357
            if ($parts[0] !== '') {
×
358
                $topDirs[$parts[0]] = true;
×
359
            }
360
        }
361

362
        if (count($topDirs) !== 1) {
×
363
            $zip->close();
×
364
            throw new \RuntimeException(__('admin.modules.zip_invalid_structure'));
×
365
        }
366

367
        $moduleName = array_key_first($topDirs);
×
368

369
        if (! preg_match('/^[A-Z][A-Za-z0-9]+$/', $moduleName)) {
×
370
            $zip->close();
×
371
            throw new \RuntimeException(__('admin.modules.zip_invalid_name'));
×
372
        }
373

374
        $targetPath = base_path('modules/' . $moduleName);
×
375

376
        // Существующий модуль уводим в резервную копию, чтобы чистая распаковка
377
        // не оставила старых файлов и можно было откатиться при сбое
378
        $backupPath = null;
×
379
        if (is_dir($targetPath)) {
×
380
            $backupPath = base_path('modules/.backup_' . $moduleName . '_' . time());
×
381
            if (! rename($targetPath, $backupPath)) {
×
382
                $zip->close();
×
383
                throw new \RuntimeException(__('admin.modules.zip_backup_failed'));
×
384
            }
385
        }
386

387
        if (! $zip->extractTo(base_path('modules/'))) {
×
388
            $zip->close();
×
389
            $this->restoreBackup($targetPath, $backupPath);
×
390
            throw new \RuntimeException(__('admin.modules.zip_extract_failed'));
×
391
        }
392
        $zip->close();
×
393

394
        $this->chmodRecursive($targetPath);
×
395

396
        if (! file_exists($targetPath . '/module.php')) {
×
397
            $this->restoreBackup($targetPath, $backupPath);
×
398
            throw new \RuntimeException(__('admin.modules.zip_no_module_file'));
×
399
        }
400

401
        if ($backupPath) {
×
402
            $this->deleteDirectory($backupPath);
×
403
        }
404

405
        // Файлы перезаписаны на диске, но opcache (revalidate_freq) ещё держит
406
        // старый module.php — без сброса кнопка обновления и новый код модуля
407
        // подхватятся только со следующим запросом после ревалидации
408
        if (function_exists('opcache_reset')) {
×
409
            opcache_reset();
×
410
        }
411

412
        return $moduleName;
×
413
    }
414

415
    /**
416
     * Удаление файлов модуля с диска
417
     */
418
    public function deleteFiles(Request $request): RedirectResponse
×
419
    {
420
        $moduleName = $request->input('module');
×
421
        $modulePath = base_path('modules/' . $moduleName);
×
422

423
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
424
            abort(200, __('admin.modules.module_not_found'));
×
425
        }
426

427
        if (Module::query()->where('name', $moduleName)->exists()) {
×
428
            abort(200, __('admin.modules.delete_files_not_uninstalled'));
×
429
        }
430

431
        $this->deleteDirectory($modulePath);
×
432

433
        Artisan::call('route:clear');
×
434
        setFlash('success', __('admin.modules.module_files_deleted'));
×
435

436
        return redirect()->route('admin.modules.index');
×
437
    }
438

439
    /**
440
     * Откат распаковки: удалить частично распакованное и вернуть резервную копию
441
     */
442
    private function restoreBackup(string $targetPath, ?string $backupPath): void
×
443
    {
444
        $this->deleteDirectory($targetPath);
×
445

446
        if ($backupPath && is_dir($backupPath)) {
×
447
            rename($backupPath, $targetPath);
×
448
        }
449
    }
450

451
    /**
452
     * Рекурсивно устанавливает права доступа (755 для директорий, 644 для файлов)
453
     */
454
    private function chmodRecursive(string $path): void
×
455
    {
456
        chmod($path, 0755);
×
457

458
        foreach (scandir($path) as $item) {
×
459
            if ($item === '.' || $item === '..') {
×
460
                continue;
×
461
            }
462

463
            $full = $path . '/' . $item;
×
464
            if (is_dir($full)) {
×
465
                $this->chmodRecursive($full);
×
466
            } else {
467
                chmod($full, 0644);
×
468
            }
469
        }
470
    }
471

472
    /**
473
     * Рекурсивно удаляет директорию, включая симлинки
474
     */
475
    private function deleteDirectory(string $path): void
×
476
    {
477
        if (is_link($path)) {
×
478
            unlink($path);
×
479

480
            return;
×
481
        }
482

483
        if (! is_dir($path)) {
×
484
            return;
×
485
        }
486

487
        foreach (scandir($path) as $item) {
×
488
            if ($item === '.' || $item === '..') {
×
489
                continue;
×
490
            }
491

492
            $full = $path . '/' . $item;
×
493
            is_dir($full) && ! is_link($full) ? $this->deleteDirectory($full) : unlink($full);
×
494
        }
495

496
        rmdir($path);
×
497
    }
498

499
    /**
500
     * Удаление/Выключение модуля
501
     */
502
    public function uninstall(Request $request): RedirectResponse
×
503
    {
504
        $moduleName = $request->input('module');
×
505
        $disable = int($request->input('disable'));
×
506
        $modulePath = base_path('modules/' . $moduleName);
×
507

508
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
509
            abort(200, __('admin.modules.module_not_found'));
×
510
        }
511

512
        $module = Module::query()->where('name', $moduleName)->first();
×
513
        if (! $module) {
×
514
            abort(200, __('admin.modules.module_not_found'));
×
515
        }
516

517
        $module->deleteSymlink();
×
518
        $module->unpublish();
×
519
        Artisan::call('route:clear');
×
520

521
        if ($disable) {
×
522
            $module->update([
×
NEW
523
                'active' => false,
×
524
            ]);
×
525
            $result = __('admin.modules.module_success_disabled');
×
526
        } else {
527
            if (config('modules.safe_mode')) {
×
528
                setFlash('danger', __('admin.modules.safe_mode_enabled'));
×
529

530
                return redirect('admin/modules/module?module=' . $moduleName);
×
531
            }
532

533
            $module->rollback();
×
534
            $module->delete();
×
535
            $result = __('admin.modules.module_success_deleted');
×
536
        }
537

538
        clearCache(['modules', 'settings']);
×
539
        setFlash('success', $result);
×
540

541
        return redirect('admin/modules/module?module=' . $moduleName);
×
542
    }
543
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc