]> BookStack Code Mirror - bookstack/blob - dev/licensing/gen-php-licenses
Licensing: Added script to build PHP library licensing information
[bookstack] / dev / licensing / gen-php-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/php-library-licenses.txt";
9 $composerLock = json_decode(file_get_contents($rootPath . "/composer.lock"));
10 $outputSeparator = "\n-----------\n";
11 $warnings = [];
12
13 $packages = $composerLock->packages;
14 $packageOutput = array_map(packageToOutput(...), $packages);
15
16 $licenseInfo =  implode($outputSeparator, $packageOutput) . "\n";
17 file_put_contents($outputPath, $licenseInfo);
18
19 echo "License information written to {$outputPath}\n";
20 echo implode("\n", $warnings);
21
22 function packageToOutput(stdClass $package) : string {
23     $output = ["{$package->name}"];
24
25     $licenses = is_array($package->license) ? $package->license : [$package->license];
26     $output[] = "License: " . implode(' ', $licenses);
27
28     $licenseFile = findLicenseFile($package->name);
29     if ($licenseFile) {
30         $output[] = "License File: {$licenseFile}";
31         $copyright = findCopyright($licenseFile);
32         if ($copyright) {
33             $output[] = "Copyright: {$copyright}";
34         } else {
35             warn("Package {$package->name} has no copyright found in its license");
36         }
37     }
38
39     $source = $package->source->url;
40     if ($source) {
41         $output[] = "Source: {$source}";
42     }
43
44     $link = $package->homepage ?? $package->source->url ?? '';
45     if ($link) {
46         $output[] = "Link: {$link}";
47     }
48
49     return implode("\n", $output);
50 }
51
52 function findLicenseFile(string $packageName): string {
53     global $rootPath;
54     $licenseNameOptions = ['license', 'LICENSE', 'license.*', 'LICENSE.*'];
55
56     $packagePath = "vendor/{$packageName}";
57     $filePath = "{$rootPath}/{$packagePath}";
58
59     $foundLicenses = [];
60     foreach ($licenseNameOptions as $option) {
61         $search = glob("{$filePath}/$option");
62         array_push($foundLicenses, ...$search);
63     }
64
65     if (count($foundLicenses) > 1) {
66         warn("Package {$packagePath} has more than one license file found");
67     }
68
69     if (count($foundLicenses) > 0) {
70         $fileName = basename($foundLicenses[0]);
71         return "{$packagePath}/{$fileName}";
72     }
73
74     warn("Package {$packageName} has no license files found");
75     return '';
76 }
77
78 function findCopyright(string $licenseFile): string {
79     $fileContents = file_get_contents($licenseFile);
80     $pattern = '/^.*?copyright (\(c\)|\d{4})[\s\S]*?(\n\n|\.\n)/mi';
81     $matches = [];
82     preg_match($pattern, $fileContents, $matches);
83     $copyright = trim($matches[0] ?? '');
84
85     $emailPattern = '/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/i';
86     return preg_replace_callback($emailPattern, obfuscateEmail(...), $copyright);
87 }
88
89 function obfuscateEmail(array $matches): string {
90     return preg_replace('/[^@.]/', '*', $matches[1]);
91 }
92
93 function warn(string $text): void {
94     global $warnings;
95     $warnings[] = "WARN:" . $text;
96 }