You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

license.php 11KB

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