license.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 = "ownCloud 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);
  211. $copyrightNotice = [];
  212. foreach ($lineByLine as $line) {
  213. if (strpos($line, '@copyright') !== false) {
  214. $copyrightNotice[] = $line;
  215. }
  216. }
  217. return implode(PHP_EOL, $copyrightNotice);
  218. }
  219. /**
  220. * check if all lines where changed after the Nextcloud fork.
  221. * That's not a guarantee that we can switch to AGPLv3 or later,
  222. * but a good indicator that we should have a look at the file
  223. *
  224. * @param $path
  225. * @param $gitRoot
  226. */
  227. private function checkCopyrightState($path, $gitRoot) {
  228. // This was the date the Nextcloud fork was created
  229. $deadline = new DateTime('06/06/2016');
  230. $deadlineTimestamp = $deadline->getTimestamp();
  231. $buildDir = getcwd();
  232. if ($gitRoot) {
  233. chdir($gitRoot);
  234. $path = substr($path, strlen($gitRoot));
  235. }
  236. $out = shell_exec("git --no-pager blame --line-porcelain $path | sed -n 's/^author-time //p'");
  237. if ($gitRoot) {
  238. chdir($buildDir);
  239. }
  240. $timestampChanges = explode(PHP_EOL, $out);
  241. $timestampChanges = array_slice($timestampChanges, 0, count($timestampChanges)-1);
  242. foreach ($timestampChanges as $timestamp) {
  243. if ((int)$timestamp < $deadlineTimestamp) {
  244. return;
  245. }
  246. }
  247. //all changes after the deadline
  248. $this->checkFiles[] = $path;
  249. }
  250. private function printFilesToCheck() {
  251. if (!empty($this->checkFiles)) {
  252. print "\n";
  253. print "For following files all lines changes since the Nextcloud fork." . PHP_EOL;
  254. print "Please check if these files can be moved over to AGPLv3 or later" . PHP_EOL;
  255. print "\n";
  256. foreach ($this->checkFiles as $file) {
  257. print $file . PHP_EOL;
  258. }
  259. print "\n";
  260. }
  261. }
  262. private function getAuthors($file, $gitRoot) {
  263. // only add authors that changed code and not the license header
  264. $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
  265. $buildDir = getcwd();
  266. if ($gitRoot) {
  267. chdir($gitRoot);
  268. $file = substr($file, strlen($gitRoot));
  269. }
  270. $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");
  271. if ($gitRoot) {
  272. chdir($buildDir);
  273. }
  274. $authors = explode(PHP_EOL, $out);
  275. $authors = array_filter($authors, function($author) {
  276. return !in_array($author, [
  277. '',
  278. 'Not Committed Yet <not.committed.yet>',
  279. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
  280. 'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
  281. ]);
  282. });
  283. if ($gitRoot) {
  284. $authors = array_map([$this, 'checkCoreMailMap'], $authors);
  285. $authors = array_unique($authors);
  286. }
  287. $authors = array_map(function($author){
  288. $this->authors[$author] = $author;
  289. return " * @author $author";
  290. }, $authors);
  291. return implode(PHP_EOL, $authors);
  292. }
  293. private function checkCoreMailMap($author) {
  294. if (empty($this->mailMap)) {
  295. $content = file_get_contents(__DIR__ . '/../.mailmap');
  296. $entries = explode("\n", $content);
  297. foreach ($entries as $entry) {
  298. if (strpos($entry, '> ') === false) {
  299. $this->mailMap[$entry] = $entry;
  300. } else {
  301. list($use, $actual) = explode('> ', $entry);
  302. $this->mailMap[$actual] = $use . '>';
  303. }
  304. }
  305. }
  306. if (isset($this->mailMap[$author])) {
  307. return $this->mailMap[$author];
  308. }
  309. return $author;
  310. }
  311. }
  312. $licenses = new Licenses;
  313. if (isset($argv[1])) {
  314. $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
  315. } else {
  316. $licenses->exec([
  317. '../apps/comments',
  318. '../apps/dav',
  319. '../apps/encryption',
  320. '../apps/federatedfilesharing',
  321. '../apps/federation',
  322. '../apps/files',
  323. '../apps/files_external',
  324. '../apps/files_sharing',
  325. '../apps/files_trashbin',
  326. '../apps/files_versions',
  327. '../apps/provisioning_api',
  328. '../apps/systemtags',
  329. '../apps/testing',
  330. '../apps/updatenotification',
  331. '../apps/user_ldap',
  332. '../core',
  333. '../lib',
  334. '../ocs',
  335. '../settings',
  336. '../console.php',
  337. '../cron.php',
  338. '../index.php',
  339. '../public.php',
  340. '../remote.php',
  341. '../status.php',
  342. '../version.php',
  343. ]);
  344. $licenses->writeAuthorsFile();
  345. }