license.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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']);
  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, '/^.+\.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. $source = file_get_contents($path);
  131. if ($this->isMITLicensed($source)) {
  132. echo "MIT licensed file: $path" . PHP_EOL;
  133. return;
  134. }
  135. $copyrightNotices = $this->getCopyrightNotices($path, $source);
  136. $authors = $this->getAuthors($path, $gitRoot);
  137. if ($this->isOwnCloudLicensed($source)) {
  138. $license = str_replace('@AUTHORS@', $authors, $this->licenseTextLegacy);
  139. $this->checkCopyrightState($path, $gitRoot);
  140. } else {
  141. $license = str_replace('@AUTHORS@', $authors, $this->licenseText);
  142. }
  143. if ($copyrightNotices === '') {
  144. $license = str_replace('@COPYRIGHT@', ' *', $license);
  145. } else {
  146. $license = str_replace('@COPYRIGHT@', $copyrightNotices, $license);
  147. }
  148. [$source, $isStrict] = $this->eatOldLicense($source);
  149. if ($isStrict) {
  150. $source = "<?php" . PHP_EOL . PHP_EOL . 'declare(strict_types=1);' . PHP_EOL . PHP_EOL . $license . PHP_EOL . $source;
  151. } else {
  152. $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
  153. }
  154. file_put_contents($path,$source);
  155. echo "License updated: $path" . PHP_EOL;
  156. }
  157. /**
  158. * @param string $source
  159. * @return bool
  160. */
  161. private function isMITLicensed($source) {
  162. $lines = explode(PHP_EOL, $source);
  163. while (!empty($lines)) {
  164. $line = $lines[0];
  165. array_shift($lines);
  166. if (strpos($line, 'The MIT License') !== false) {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. private function isOwnCloudLicensed($source) {
  173. $lines = explode(PHP_EOL, $source);
  174. while (!empty($lines)) {
  175. $line = $lines[0];
  176. array_shift($lines);
  177. if (strpos($line, 'ownCloud, Inc') !== false || strpos($line, 'ownCloud GmbH') !== false) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. /**
  184. * @param string $source
  185. * @return string
  186. */
  187. private function eatOldLicense($source) {
  188. $lines = explode(PHP_EOL, $source);
  189. $isStrict = false;
  190. while (!empty($lines)) {
  191. $line = $lines[0];
  192. if (trim($line) === '<?php') {
  193. array_shift($lines);
  194. continue;
  195. }
  196. if (strpos($line, '<?php declare(strict_types') !== false) {
  197. $isStrict = true;
  198. array_shift($lines);
  199. continue;
  200. }
  201. if (strpos($line, 'declare (strict_types') !== false) {
  202. $isStrict = true;
  203. array_shift($lines);
  204. continue;
  205. }
  206. if (strpos($line, 'declare(strict_types') !== false) {
  207. $isStrict = true;
  208. array_shift($lines);
  209. continue;
  210. }
  211. if (strpos($line, '/**') !== false) {
  212. array_shift($lines);
  213. continue;
  214. }
  215. if (strpos($line, '*/') !== false) {
  216. array_shift($lines);
  217. break;
  218. }
  219. if (strpos($line, '*') !== false) {
  220. array_shift($lines);
  221. continue;
  222. }
  223. if (trim($line) === '') {
  224. array_shift($lines);
  225. continue;
  226. }
  227. break;
  228. }
  229. return [implode(PHP_EOL, $lines), $isStrict];
  230. }
  231. private function getCopyrightNotices($path, $file) {
  232. $licenseHeaderEndsAtLine = (int)trim(shell_exec("grep -n '*/' $path | head -n 1 | cut -d ':' -f 1"));
  233. $lineByLine = explode(PHP_EOL, $file, $licenseHeaderEndsAtLine + 1);
  234. $copyrightNotice = [];
  235. $licensePart = array_slice($lineByLine, 0, $licenseHeaderEndsAtLine);
  236. foreach ($licensePart as $line) {
  237. if (strpos($line, '@copyright') !== false) {
  238. $copyrightNotice[] = $line;
  239. }
  240. }
  241. return implode(PHP_EOL, $copyrightNotice);
  242. }
  243. /**
  244. * check if all lines where changed after the Nextcloud fork.
  245. * That's not a guarantee that we can switch to AGPLv3 or later,
  246. * but a good indicator that we should have a look at the file
  247. *
  248. * @param $path
  249. * @param $gitRoot
  250. */
  251. private function checkCopyrightState($path, $gitRoot) {
  252. // This was the date the Nextcloud fork was created
  253. $deadline = new DateTime('06/06/2016');
  254. $deadlineTimestamp = $deadline->getTimestamp();
  255. $buildDir = getcwd();
  256. if ($gitRoot) {
  257. chdir($gitRoot);
  258. $path = substr($path, strlen($gitRoot));
  259. }
  260. $out = shell_exec("git --no-pager blame --line-porcelain $path | sed -n 's/^author-time //p'");
  261. if ($gitRoot) {
  262. chdir($buildDir);
  263. }
  264. $timestampChanges = explode(PHP_EOL, $out);
  265. $timestampChanges = array_slice($timestampChanges, 0, count($timestampChanges) - 1);
  266. foreach ($timestampChanges as $timestamp) {
  267. if ((int)$timestamp < $deadlineTimestamp) {
  268. return;
  269. }
  270. }
  271. //all changes after the deadline
  272. $this->checkFiles[] = $path;
  273. }
  274. private function printFilesToCheck() {
  275. if (!empty($this->checkFiles)) {
  276. print "\n";
  277. print "For following files all lines changed since the Nextcloud fork." . PHP_EOL;
  278. print "Please check if these files can be moved over to AGPLv3 or later" . PHP_EOL;
  279. print "\n";
  280. foreach ($this->checkFiles as $file) {
  281. print $file . PHP_EOL;
  282. }
  283. print "\n";
  284. }
  285. }
  286. private function getAuthors($file, $gitRoot) {
  287. // only add authors that changed code and not the license header
  288. $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
  289. $buildDir = getcwd();
  290. if ($gitRoot) {
  291. chdir($gitRoot);
  292. $file = substr($file, strlen($gitRoot));
  293. }
  294. $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");
  295. if ($gitRoot) {
  296. chdir($buildDir);
  297. }
  298. $authors = explode(PHP_EOL, $out);
  299. $authors = array_filter($authors, function ($author) {
  300. return !in_array($author, [
  301. '',
  302. 'Not Committed Yet <not.committed.yet>',
  303. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
  304. 'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
  305. ]);
  306. });
  307. if ($gitRoot) {
  308. $authors = array_map([$this, 'checkCoreMailMap'], $authors);
  309. $authors = array_unique($authors);
  310. }
  311. $authors = array_map(function ($author) {
  312. $author = $this->fixInvalidEmail($author);
  313. $this->authors[$author] = $author;
  314. return " * @author $author";
  315. }, $authors);
  316. return implode(PHP_EOL, $authors);
  317. }
  318. private function checkCoreMailMap($author) {
  319. if (empty($this->mailMap)) {
  320. $content = file_get_contents(__DIR__ . '/../.mailmap');
  321. $entries = explode("\n", $content);
  322. foreach ($entries as $entry) {
  323. if (strpos($entry, '> ') === false) {
  324. $this->mailMap[$entry] = $entry;
  325. } else {
  326. list($use, $actual) = explode('> ', $entry);
  327. $this->mailMap[$actual] = $use . '>';
  328. }
  329. }
  330. }
  331. if (isset($this->mailMap[$author])) {
  332. return $this->mailMap[$author];
  333. }
  334. return $author;
  335. }
  336. private function fixInvalidEmail($author) {
  337. preg_match('/<(.*)>/', $author, $mailMatch);
  338. if (count($mailMatch) === 2 && !filter_var($mailMatch[1], FILTER_VALIDATE_EMAIL)) {
  339. $author = str_replace('<'.$mailMatch[1].'>', '"'.$mailMatch[1].'"', $author);
  340. }
  341. return $author;
  342. }
  343. }
  344. $licenses = new Licenses;
  345. if (isset($argv[1])) {
  346. $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
  347. } else {
  348. $licenses->exec([
  349. '../apps/accessibility',
  350. '../apps/admin_audit',
  351. '../apps/cloud_federation_api',
  352. '../apps/comments',
  353. '../apps/dav',
  354. '../apps/encryption',
  355. '../apps/federatedfilesharing',
  356. '../apps/federation',
  357. '../apps/files',
  358. '../apps/files_external',
  359. '../apps/files_sharing',
  360. '../apps/files_trashbin',
  361. '../apps/files_versions',
  362. '../apps/lookup_server_connector',
  363. '../apps/oauth2',
  364. '../apps/provisioning_api',
  365. '../apps/settings',
  366. '../apps/sharebymail',
  367. '../apps/systemtags',
  368. '../apps/testing',
  369. '../apps/theming',
  370. '../apps/twofactor_backupcodes',
  371. '../apps/updatenotification',
  372. '../apps/user_ldap',
  373. '../build/integration/features/bootstrap',
  374. '../core',
  375. '../lib',
  376. '../ocs',
  377. '../console.php',
  378. '../cron.php',
  379. '../index.php',
  380. '../public.php',
  381. '../remote.php',
  382. '../status.php',
  383. '../version.php',
  384. ]);
  385. $licenses->writeAuthorsFile();
  386. }