]> BookStack Code Mirror - bookstack/blob - dev/licensing/gen-js-licenses
6c44791aa27d430ff42402162d06f7c3fe1411dc
[bookstack] / dev / licensing / gen-js-licenses
1 #!/usr/bin/env php
2 <?php
3
4 // This script reads the project composer.lock file to generate
5 // clear license details for our PHP dependencies.
6
7 $rootPath = dirname(__DIR__, 2);
8 $outputPath = "{$rootPath}/dev/licensing/js-library-licenses.txt";
9 $outputSeparator = "\n-----------\n";
10 $warnings = [];
11
12 $packages = [
13     ...glob("{$rootPath}/node_modules/*/package.json"),
14     ...glob("{$rootPath}/node_modules/@*/*/package.json"),
15 ];
16
17 $packageOutput = array_map(packageToOutput(...), $packages);
18
19 $licenseInfo = implode($outputSeparator, $packageOutput) . "\n";
20 file_put_contents($outputPath, $licenseInfo);
21
22 echo "License information written to {$outputPath}\n";
23 echo implode("\n", $warnings);
24
25 function dd(mixed $data): never {
26     print_r($data);
27     exit(0);
28 }
29
30 function packageToOutput(string $packagePath): string
31 {
32     global $rootPath;
33     $package = json_decode(file_get_contents($packagePath));
34     $output = ["{$package->name}"];
35
36     $license = $package->license ?? '';
37     if ($license) {
38         $output[] = "License: {$license}";
39     } else {
40         warn("Package {$package->name}: No license found");
41     }
42
43     $licenseFile = findLicenseFile($package->name, $packagePath);
44     if ($licenseFile) {
45         $relLicenseFile = str_replace("{$rootPath}/", '', $licenseFile);
46         $output[] = "License File: {$relLicenseFile}";
47         $copyright = findCopyright($licenseFile);
48         if ($copyright) {
49             $output[] = "Copyright: {$copyright}";
50         } else {
51             warn("Package {$package->name}: no copyright found in its license");
52         }
53     }
54
55     $source = $package->repository->url ?? $package->repository ?? '';
56     if ($source) {
57         $output[] = "Source: {$source}";
58     }
59
60     $link = $package->homepage ?? $source;
61     if ($link) {
62         $output[] = "Link: {$link}";
63     }
64
65     return implode("\n", $output);
66 }
67
68 function findLicenseFile(string $packageName, string $packagePath): string
69 {
70     $licenseNameOptions = [
71         'license', 'LICENSE', 'License',
72         'license.*', 'LICENSE.*', 'License.*',
73         'license-*.*', 'LICENSE-*.*', 'License-*.*',
74     ];
75     $packageDir = dirname($packagePath);
76
77     $foundLicenses = [];
78     foreach ($licenseNameOptions as $option) {
79         $search = glob("{$packageDir}/$option");
80         array_push($foundLicenses, ...$search);
81     }
82
83     if (count($foundLicenses) > 1) {
84         warn("Package {$packageName}: more than one license file found");
85     }
86
87     if (count($foundLicenses) > 0) {
88         $fileName = basename($foundLicenses[0]);
89         return "{$packageDir}/{$fileName}";
90     }
91
92     warn("Package {$packageName}: no license files found");
93     return '';
94 }
95
96 function findCopyright(string $licenseFile): string
97 {
98     $fileContents = file_get_contents($licenseFile);
99     $pattern = '/^.*?copyright (\(c\)|\d{4})[\s\S]*?(\n\n|\.\n)/mi';
100     $matches = [];
101     preg_match($pattern, $fileContents, $matches);
102     $copyright = trim($matches[0] ?? '');
103
104     if (str_contains($copyright, 'i.e.')) {
105         return '';
106     }
107
108     $emailPattern = '/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/i';
109     return preg_replace_callback($emailPattern, obfuscateEmail(...), $copyright);
110 }
111
112 function obfuscateEmail(array $matches): string
113 {
114     return preg_replace('/[^@.]/', '*', $matches[1]);
115 }
116
117 function warn(string $text): void
118 {
119     global $warnings;
120     $warnings[] = "WARN:" . $text;
121 }