1
0

license.php 13 KB

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