license.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. {
  23. protected $paths = [];
  24. protected $mailMap = [];
  25. protected $checkFiles = [];
  26. public $authors = [];
  27. public function __construct() {
  28. $this->licenseText = <<<EOD
  29. /**
  30. @COPYRIGHT@
  31. *
  32. @AUTHORS@
  33. *
  34. * @license GNU AGPL version 3 or any later version
  35. *
  36. * This program is free software: you can redistribute it and/or modify
  37. * it under the terms of the GNU Affero General Public License as
  38. * published by the Free Software Foundation, either version 3 of the
  39. * License, or (at your option) any later version.
  40. *
  41. * This program is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. * GNU Affero General Public License for more details.
  45. *
  46. * You should have received a copy of the GNU Affero General Public License
  47. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  48. *
  49. */
  50. EOD;
  51. $this->licenseTextLegacy = <<<EOD
  52. /**
  53. @COPYRIGHT@
  54. *
  55. @AUTHORS@
  56. *
  57. * @license AGPL-3.0
  58. *
  59. * This code is free software: you can redistribute it and/or modify
  60. * it under the terms of the GNU Affero General Public License, version 3,
  61. * as published by the Free Software Foundation.
  62. *
  63. * This program is distributed in the hope that it will be useful,
  64. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. * GNU Affero General Public License for more details.
  67. *
  68. * You should have received a copy of the GNU Affero General Public License, version 3,
  69. * along with this program. If not, see <http://www.gnu.org/licenses/>
  70. *
  71. */
  72. EOD;
  73. $this->licenseTextLegacy = str_replace('@YEAR@', date("Y"), $this->licenseTextLegacy);
  74. }
  75. /**
  76. * @param string|string[] $folder
  77. * @param string|bool $gitRoot
  78. */
  79. function exec($folder, $gitRoot = false) {
  80. if (is_array($folder)) {
  81. foreach($folder as $f) {
  82. $this->exec($f, $gitRoot);
  83. }
  84. return;
  85. }
  86. if ($gitRoot !== false && substr($gitRoot, -1) !== '/') {
  87. $gitRoot .= '/';
  88. }
  89. if (is_file($folder)) {
  90. $this->handleFile($folder, $gitRoot);
  91. $this->printFilesToCheck();
  92. return;
  93. }
  94. $excludes = array_map(function($item) use ($folder) {
  95. return $folder . '/' . $item;
  96. }, ['vendor', '3rdparty', '.git', 'l10n', 'templates']);
  97. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  98. $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
  99. /** @var SplFileInfo $item */
  100. foreach($excludes as $exclude) {
  101. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  102. return false;
  103. }
  104. }
  105. return true;
  106. });
  107. $iterator = new RecursiveIteratorIterator($iterator);
  108. $iterator = new RegexIterator($iterator, '/^.+\.php$/i');
  109. foreach ($iterator as $file) {
  110. /** @var SplFileInfo $file */
  111. $this->handleFile($file, $gitRoot);
  112. }
  113. $this->printFilesToCheck();
  114. }
  115. function writeAuthorsFile() {
  116. ksort($this->authors);
  117. $template = "Nextcloud is written by:
  118. @AUTHORS@
  119. With help from many libraries and frameworks including:
  120. Open Collaboration Services
  121. SabreDAV
  122. jQuery
  123. ";
  124. $authors = implode(PHP_EOL, array_map(function($author){
  125. return " - ".$author;
  126. }, $this->authors));
  127. $template = str_replace('@AUTHORS@', $authors, $template);
  128. file_put_contents(__DIR__.'/../AUTHORS', $template);
  129. }
  130. function handleFile($path, $gitRoot) {
  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. $license = str_replace('@COPYRIGHT@', $copyrightNotices, $license);
  145. $source = $this->eatOldLicense($source);
  146. $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
  147. file_put_contents($path,$source);
  148. echo "License updated: $path" . PHP_EOL;
  149. }
  150. /**
  151. * @param string $source
  152. * @return bool
  153. */
  154. private function isMITLicensed($source) {
  155. $lines = explode(PHP_EOL, $source);
  156. while(!empty($lines)) {
  157. $line = $lines[0];
  158. array_shift($lines);
  159. if (strpos($line, 'The MIT License') !== false) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. private function isOwnCloudLicensed($source) {
  166. $lines = explode(PHP_EOL, $source);
  167. while(!empty($lines)) {
  168. $line = $lines[0];
  169. array_shift($lines);
  170. if (strpos($line, 'ownCloud, Inc') !== false) {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. /**
  177. * @param string $source
  178. * @return string
  179. */
  180. private function eatOldLicense($source) {
  181. $lines = explode(PHP_EOL, $source);
  182. while(!empty($lines)) {
  183. $line = $lines[0];
  184. if (strpos($line, '<?php') !== false) {
  185. array_shift($lines);
  186. continue;
  187. }
  188. if (strpos($line, '/**') !== false) {
  189. array_shift($lines);
  190. continue;
  191. }
  192. if (strpos($line, '*/') !== false ) {
  193. array_shift($lines);
  194. break;
  195. }
  196. if (strpos($line, '*') !== false) {
  197. array_shift($lines);
  198. continue;
  199. }
  200. if (trim($line) === '') {
  201. array_shift($lines);
  202. continue;
  203. }
  204. break;
  205. }
  206. return implode(PHP_EOL, $lines);
  207. }
  208. private function getCopyrightNotices($path, $file) {
  209. $licenseHeaderEndsAtLine = (int)trim(shell_exec("grep -n '*/' $path | head -n 1 | cut -d ':' -f 1"));
  210. $lineByLine = explode(PHP_EOL, $file, $licenseHeaderEndsAtLine + 1);
  211. $copyrightNotice = [];
  212. $licensePart = array_slice($lineByLine, 0, $licenseHeaderEndsAtLine);
  213. foreach ($licensePart as $line) {
  214. if (strpos($line, '@copyright') !== false) {
  215. $copyrightNotice[] = $line;
  216. }
  217. }
  218. return implode(PHP_EOL, $copyrightNotice);
  219. }
  220. /**
  221. * check if all lines where changed after the Nextcloud fork.
  222. * That's not a guarantee that we can switch to AGPLv3 or later,
  223. * but a good indicator that we should have a look at the file
  224. *
  225. * @param $path
  226. * @param $gitRoot
  227. */
  228. private function checkCopyrightState($path, $gitRoot) {
  229. // This was the date the Nextcloud fork was created
  230. $deadline = new DateTime('06/06/2016');
  231. $deadlineTimestamp = $deadline->getTimestamp();
  232. $buildDir = getcwd();
  233. if ($gitRoot) {
  234. chdir($gitRoot);
  235. $path = substr($path, strlen($gitRoot));
  236. }
  237. $out = shell_exec("git --no-pager blame --line-porcelain $path | sed -n 's/^author-time //p'");
  238. if ($gitRoot) {
  239. chdir($buildDir);
  240. }
  241. $timestampChanges = explode(PHP_EOL, $out);
  242. $timestampChanges = array_slice($timestampChanges, 0, count($timestampChanges)-1);
  243. foreach ($timestampChanges as $timestamp) {
  244. if ((int)$timestamp < $deadlineTimestamp) {
  245. return;
  246. }
  247. }
  248. //all changes after the deadline
  249. $this->checkFiles[] = $path;
  250. }
  251. private function printFilesToCheck() {
  252. if (!empty($this->checkFiles)) {
  253. print "\n";
  254. print "For following files all lines changed since the Nextcloud fork." . PHP_EOL;
  255. print "Please check if these files can be moved over to AGPLv3 or later" . PHP_EOL;
  256. print "\n";
  257. foreach ($this->checkFiles as $file) {
  258. print $file . PHP_EOL;
  259. }
  260. print "\n";
  261. }
  262. }
  263. private function getAuthors($file, $gitRoot) {
  264. // only add authors that changed code and not the license header
  265. $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
  266. $buildDir = getcwd();
  267. if ($gitRoot) {
  268. chdir($gitRoot);
  269. $file = substr($file, strlen($gitRoot));
  270. }
  271. $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");
  272. if ($gitRoot) {
  273. chdir($buildDir);
  274. }
  275. $authors = explode(PHP_EOL, $out);
  276. $authors = array_filter($authors, function($author) {
  277. return !in_array($author, [
  278. '',
  279. 'Not Committed Yet <not.committed.yet>',
  280. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
  281. 'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
  282. ]);
  283. });
  284. if ($gitRoot) {
  285. $authors = array_map([$this, 'checkCoreMailMap'], $authors);
  286. $authors = array_unique($authors);
  287. }
  288. $authors = array_map(function($author){
  289. $this->authors[$author] = $author;
  290. return " * @author $author";
  291. }, $authors);
  292. return implode(PHP_EOL, $authors);
  293. }
  294. private function checkCoreMailMap($author) {
  295. if (empty($this->mailMap)) {
  296. $content = file_get_contents(__DIR__ . '/../.mailmap');
  297. $entries = explode("\n", $content);
  298. foreach ($entries as $entry) {
  299. if (strpos($entry, '> ') === false) {
  300. $this->mailMap[$entry] = $entry;
  301. } else {
  302. list($use, $actual) = explode('> ', $entry);
  303. $this->mailMap[$actual] = $use . '>';
  304. }
  305. }
  306. }
  307. if (isset($this->mailMap[$author])) {
  308. return $this->mailMap[$author];
  309. }
  310. return $author;
  311. }
  312. }
  313. $licenses = new Licenses;
  314. if (isset($argv[1])) {
  315. $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
  316. } else {
  317. $licenses->exec([
  318. '../apps/admin_audit',
  319. '../apps/comments',
  320. '../apps/dav',
  321. '../apps/encryption',
  322. '../apps/federatedfilesharing',
  323. '../apps/federation',
  324. '../apps/files',
  325. '../apps/files_external',
  326. '../apps/files_sharing',
  327. '../apps/files_trashbin',
  328. '../apps/files_versions',
  329. '../apps/provisioning_api',
  330. '../apps/systemtags',
  331. '../apps/testing',
  332. '../apps/theming',
  333. '../apps/updatenotification',
  334. '../apps/user_ldap',
  335. '../build/integration/features/bootstrap',
  336. '../core',
  337. '../lib',
  338. '../ocs',
  339. '../settings',
  340. '../console.php',
  341. '../cron.php',
  342. '../index.php',
  343. '../public.php',
  344. '../remote.php',
  345. '../status.php',
  346. '../version.php',
  347. ]);
  348. $licenses->writeAuthorsFile();
  349. }