Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

license.php 12KB

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