1
0

license.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. class Licenses {
  8. protected $paths = [];
  9. protected $mailMap = [];
  10. protected $checkFiles = [];
  11. public $authors = [];
  12. public function __construct() {
  13. $this->licenseText = <<<EOD
  14. /**
  15. @COPYRIGHT@
  16. *
  17. @AUTHORS@
  18. *
  19. * @license GNU AGPL version 3 or any later version
  20. *
  21. * This program is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License as
  23. * published by the Free Software Foundation, either version 3 of the
  24. * License, or (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. *
  34. */
  35. EOD;
  36. $this->licenseTextLegacy = <<<EOD
  37. /**
  38. @COPYRIGHT@
  39. *
  40. @AUTHORS@
  41. *
  42. * @license AGPL-3.0
  43. *
  44. * This code is free software: you can redistribute it and/or modify
  45. * it under the terms of the GNU Affero General Public License, version 3,
  46. * as published by the Free Software Foundation.
  47. *
  48. * This program is distributed in the hope that it will be useful,
  49. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  50. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  51. * GNU Affero General Public License for more details.
  52. *
  53. * You should have received a copy of the GNU Affero General Public License, version 3,
  54. * along with this program. If not, see <http://www.gnu.org/licenses/>
  55. *
  56. */
  57. EOD;
  58. $this->licenseTextLegacy = str_replace('@YEAR@', date("Y"), $this->licenseTextLegacy);
  59. }
  60. /**
  61. * @param string|string[] $folder
  62. * @param string|bool $gitRoot
  63. */
  64. public function exec($folder, $gitRoot = false) {
  65. if (is_array($folder)) {
  66. foreach ($folder as $f) {
  67. $this->exec($f, $gitRoot);
  68. }
  69. return;
  70. }
  71. if ($gitRoot !== false && substr($gitRoot, -1) !== '/') {
  72. $gitRoot .= '/';
  73. }
  74. if (is_file($folder)) {
  75. $this->handleFile($folder, $gitRoot);
  76. $this->printFilesToCheck();
  77. return;
  78. }
  79. $excludes = array_map(function ($item) use ($folder) {
  80. return $folder . '/' . $item;
  81. }, ['vendor', '3rdparty', '.git', 'l10n', 'templates', 'composer', 'js', 'node_modules']);
  82. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  83. $iterator = new RecursiveCallbackFilterIterator($iterator, function ($item) use ($folder, $excludes) {
  84. /** @var SplFileInfo $item */
  85. foreach ($excludes as $exclude) {
  86. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. });
  92. $iterator = new RecursiveIteratorIterator($iterator);
  93. $iterator = new RegexIterator($iterator, '/^.+\.(js|php)$/i');
  94. foreach ($iterator as $file) {
  95. /** @var SplFileInfo $file */
  96. $this->handleFile($file, $gitRoot);
  97. }
  98. $this->printFilesToCheck();
  99. }
  100. public function writeAuthorsFile() {
  101. ksort($this->authors);
  102. $template = "Nextcloud is written by:
  103. @AUTHORS@
  104. With help from many libraries and frameworks including:
  105. Open Collaboration Services
  106. SabreDAV
  107. jQuery
  108. ";
  109. $authors = implode(PHP_EOL, array_map(function ($author) {
  110. return " - ".$author;
  111. }, $this->authors));
  112. $template = str_replace('@AUTHORS@', $authors, $template);
  113. file_put_contents(__DIR__.'/../AUTHORS', $template);
  114. }
  115. public function handleFile($path, $gitRoot) {
  116. $isPhp = preg_match('/^.+\.php$/i', $path);
  117. $source = file_get_contents($path);
  118. if ($this->isMITLicensed($source)) {
  119. echo "MIT licensed file: $path" . PHP_EOL;
  120. return;
  121. }
  122. $copyrightNotices = $this->getCopyrightNotices($path, $source);
  123. $authors = $this->getAuthors($path, $gitRoot);
  124. if ($this->isOwnCloudLicensed($source)) {
  125. $license = str_replace('@AUTHORS@', $authors, $this->licenseTextLegacy);
  126. $this->checkCopyrightState($path, $gitRoot);
  127. } else {
  128. $license = str_replace('@AUTHORS@', $authors, $this->licenseText);
  129. }
  130. if ($copyrightNotices === '') {
  131. $creator = $this->getCreatorCopyright($path, $gitRoot);
  132. $license = str_replace('@COPYRIGHT@', $creator, $license);
  133. } else {
  134. $license = str_replace('@COPYRIGHT@', $copyrightNotices, $license);
  135. }
  136. [$source, $isStrict] = $this->eatOldLicense($source);
  137. if ($isPhp) {
  138. if ($isStrict) {
  139. $source = "<?php" . PHP_EOL . PHP_EOL . 'declare(strict_types=1);' . PHP_EOL . PHP_EOL . $license . PHP_EOL . $source;
  140. } else {
  141. $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
  142. }
  143. } else {
  144. $source = $license . PHP_EOL . PHP_EOL . $source;
  145. }
  146. file_put_contents($path, $source);
  147. echo "License updated: $path" . PHP_EOL;
  148. }
  149. /**
  150. * @param string $source
  151. * @return bool
  152. */
  153. private function isMITLicensed($source) {
  154. $lines = explode(PHP_EOL, $source);
  155. while (!empty($lines)) {
  156. $line = $lines[0];
  157. array_shift($lines);
  158. if (strpos($line, 'The MIT License') !== false) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. private function isOwnCloudLicensed($source) {
  165. $lines = explode(PHP_EOL, $source);
  166. while (!empty($lines)) {
  167. $line = $lines[0];
  168. array_shift($lines);
  169. if (strpos($line, 'ownCloud, Inc') !== false || strpos($line, 'ownCloud GmbH') !== false) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. /**
  176. * @param string $source
  177. * @return string
  178. */
  179. private function eatOldLicense($source) {
  180. $lines = explode(PHP_EOL, $source);
  181. $isStrict = false;
  182. $index = 0;
  183. while (!empty($lines) && array_key_exists($index, $lines)) {
  184. $line = $lines[$index];
  185. if (trim($line) === '<?php') {
  186. array_splice($lines, $index, 1);
  187. continue;
  188. }
  189. // Skipping if the line contains important js keywords
  190. if (strpos($line, 'eslint-') !== false
  191. || strpos($line, 'globals') !== false
  192. || strpos($line, 'const') !== false
  193. || strpos($line, 'import') !== false) {
  194. $index++;
  195. continue;
  196. }
  197. if (strpos($line, '<?php declare(strict_types') !== false) {
  198. $isStrict = true;
  199. array_splice($lines, $index, 1);
  200. continue;
  201. }
  202. if (strpos($line, 'declare (strict_types') !== false) {
  203. $isStrict = true;
  204. array_splice($lines, $index, 1);
  205. continue;
  206. }
  207. if (strpos($line, 'declare(strict_types') !== false) {
  208. $isStrict = true;
  209. array_splice($lines, $index, 1);
  210. continue;
  211. }
  212. if (strpos($line, '/**') !== false) {
  213. array_splice($lines, $index, 1);
  214. continue;
  215. }
  216. // If we reach the end of the copyright header (and it's not a one-line comment /* xxx */)
  217. if (strpos($line, '*/') !== false && strpos($line, '/*') !== false) {
  218. array_splice($lines, $index, 1);
  219. break;
  220. }
  221. if (strpos($line, '*') !== false) {
  222. array_splice($lines, $index, 1);
  223. continue;
  224. }
  225. if (trim($line) === '') {
  226. array_splice($lines, $index, 1);
  227. continue;
  228. }
  229. break;
  230. }
  231. return [implode(PHP_EOL, $lines), $isStrict];
  232. }
  233. private function getCopyrightNotices($path, $file) {
  234. $licenseHeaderCopyrightAtLines = trim(shell_exec("grep -ni 'copyright' $path | cut -d ':' -f 1"));
  235. $lineByLine = explode(PHP_EOL, $file);
  236. $copyrightNotice = [];
  237. if (trim($licenseHeaderCopyrightAtLines !== '')) {
  238. $copyrightNotice = array_map(function ($line) use ($lineByLine) {
  239. return $lineByLine[(int)$line - 1];
  240. }, explode(PHP_EOL, $licenseHeaderCopyrightAtLines));
  241. }
  242. return implode(PHP_EOL, $copyrightNotice);
  243. }
  244. /**
  245. * check if all lines where changed after the Nextcloud fork.
  246. * That's not a guarantee that we can switch to AGPLv3 or later,
  247. * but a good indicator that we should have a look at the file
  248. *
  249. * @param $path
  250. * @param $gitRoot
  251. */
  252. private function checkCopyrightState($path, $gitRoot) {
  253. // This was the date the Nextcloud fork was created
  254. $deadline = new DateTime('06/06/2016');
  255. $deadlineTimestamp = $deadline->getTimestamp();
  256. $buildDir = getcwd();
  257. if ($gitRoot) {
  258. chdir($gitRoot);
  259. $path = substr($path, strlen($gitRoot));
  260. }
  261. $out = shell_exec("git --no-pager blame --line-porcelain $path | sed -n 's/^author-time //p'");
  262. if ($gitRoot) {
  263. chdir($buildDir);
  264. }
  265. $timestampChanges = explode(PHP_EOL, $out);
  266. $timestampChanges = array_slice($timestampChanges, 0, count($timestampChanges) - 1);
  267. foreach ($timestampChanges as $timestamp) {
  268. if ((int)$timestamp < $deadlineTimestamp) {
  269. return;
  270. }
  271. }
  272. //all changes after the deadline
  273. $this->checkFiles[] = $path;
  274. }
  275. private function printFilesToCheck() {
  276. if (!empty($this->checkFiles)) {
  277. print "\n";
  278. print "For following files all lines changed since the Nextcloud fork." . PHP_EOL;
  279. print "Please check if these files can be moved over to AGPLv3 or later" . PHP_EOL;
  280. print "\n";
  281. foreach ($this->checkFiles as $file) {
  282. print $file . PHP_EOL;
  283. }
  284. print "\n";
  285. }
  286. }
  287. private function filterAuthors($authors = []) {
  288. $authors = array_filter($authors, function ($author) {
  289. return !in_array($author, [
  290. '',
  291. 'Not Committed Yet <not.committed.yet>',
  292. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
  293. 'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
  294. ]);
  295. });
  296. // Strip out dependabot
  297. $authors = array_filter($authors, function ($author) {
  298. return strpos($author, 'dependabot') === false;
  299. });
  300. return $authors;
  301. }
  302. private function getCreatorCopyright($file, $gitRoot) {
  303. $buildDir = getcwd();
  304. if ($gitRoot) {
  305. chdir($gitRoot);
  306. $file = substr($file, strlen($gitRoot));
  307. }
  308. $year = trim(shell_exec('date +%Y -d "$(git log --format=%aD ../apps/files/lib/Controller/ViewController.php | tail -1)"'));
  309. $blame = shell_exec("git blame --line-porcelain $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /'");
  310. $authors = explode(PHP_EOL, $blame);
  311. if ($gitRoot) {
  312. chdir($buildDir);
  313. }
  314. $authors = $this->filterAuthors($authors);
  315. if ($gitRoot) {
  316. $authors = array_map([$this, 'checkCoreMailMap'], $authors);
  317. $authors = array_unique($authors);
  318. }
  319. $creator = array_key_exists(0, $authors)
  320. ? $this->fixInvalidEmail($authors[0])
  321. : '';
  322. return " * @copyright Copyright (c) $year $creator";
  323. }
  324. private function getAuthors($file, $gitRoot) {
  325. // only add authors that changed code and not the license header
  326. $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
  327. $buildDir = getcwd();
  328. if ($gitRoot) {
  329. chdir($gitRoot);
  330. $file = substr($file, strlen($gitRoot));
  331. }
  332. $out = shell_exec("git blame --line-porcelain -L $licenseHeaderEndsAtLine, $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /' | sort -f | uniq");
  333. if ($gitRoot) {
  334. chdir($buildDir);
  335. }
  336. $authors = explode(PHP_EOL, $out);
  337. $authors = $this->filterAuthors($authors);
  338. if ($gitRoot) {
  339. $authors = array_map([$this, 'checkCoreMailMap'], $authors);
  340. $authors = array_unique($authors);
  341. }
  342. $authors = array_map(function ($author) {
  343. $author = $this->fixInvalidEmail($author);
  344. $this->authors[$author] = $author;
  345. return " * @author $author";
  346. }, $authors);
  347. return implode(PHP_EOL, $authors);
  348. }
  349. private function checkCoreMailMap($author) {
  350. if (empty($this->mailMap)) {
  351. $content = file_get_contents(__DIR__ . '/../.mailmap');
  352. $entries = explode("\n", $content);
  353. foreach ($entries as $entry) {
  354. if (strpos($entry, '> ') === false) {
  355. $this->mailMap[$entry] = $entry;
  356. } else {
  357. [$use, $actual] = explode('> ', $entry);
  358. $this->mailMap[$actual] = $use . '>';
  359. }
  360. }
  361. }
  362. if (isset($this->mailMap[$author])) {
  363. return $this->mailMap[$author];
  364. }
  365. return $author;
  366. }
  367. private function fixInvalidEmail($author) {
  368. preg_match('/<(.*)>/', $author, $mailMatch);
  369. if (count($mailMatch) === 2 && !filter_var($mailMatch[1], FILTER_VALIDATE_EMAIL)) {
  370. $author = str_replace('<'.$mailMatch[1].'>', '"'.$mailMatch[1].'"', $author);
  371. }
  372. return $author;
  373. }
  374. }
  375. $licenses = new Licenses;
  376. if (isset($argv[1])) {
  377. $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
  378. } else {
  379. $licenses->exec([
  380. '../apps/admin_audit',
  381. '../apps/cloud_federation_api',
  382. '../apps/comments',
  383. '../apps/contactsinteraction',
  384. '../apps/dashboard',
  385. '../apps/dav',
  386. '../apps/encryption',
  387. '../apps/federatedfilesharing',
  388. '../apps/federation',
  389. '../apps/files',
  390. '../apps/files_external',
  391. '../apps/files_sharing',
  392. '../apps/files_trashbin',
  393. '../apps/files_versions',
  394. '../apps/lookup_server_connector',
  395. '../apps/oauth2',
  396. '../apps/provisioning_api',
  397. '../apps/settings',
  398. '../apps/sharebymail',
  399. '../apps/systemtags',
  400. '../apps/testing',
  401. '../apps/theming',
  402. '../apps/twofactor_backupcodes',
  403. '../apps/updatenotification',
  404. '../apps/user_ldap',
  405. '../apps/user_status',
  406. '../apps/weather_status',
  407. '../apps/workflowengine',
  408. '../build/integration/features/bootstrap',
  409. '../core',
  410. '../lib',
  411. '../ocs',
  412. '../console.php',
  413. '../cron.php',
  414. '../index.php',
  415. '../public.php',
  416. '../remote.php',
  417. '../status.php',
  418. '../version.php',
  419. ]);
  420. $licenses->writeAuthorsFile();
  421. }