license.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. public $authors = [];
  26. public function __construct() {
  27. $this->licenseText = <<<EOD
  28. /**
  29. @AUTHORS@
  30. *
  31. * @license GNU AGPL version 3 or any later version
  32. *
  33. * This program is free software: you can redistribute it and/or modify
  34. * it under the terms of the GNU Affero General Public License as
  35. * published by the Free Software Foundation, either version 3 of the
  36. * License, or (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU Affero General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU Affero General Public License
  44. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  45. *
  46. */
  47. EOD;
  48. $this->licenseTextLegacy = <<<EOD
  49. /**
  50. @AUTHORS@
  51. *
  52. * @copyright Copyright (c) @YEAR@, ownCloud, Inc.
  53. * @license AGPL-3.0
  54. *
  55. * This code is free software: you can redistribute it and/or modify
  56. * it under the terms of the GNU Affero General Public License, version 3,
  57. * as published by the Free Software Foundation.
  58. *
  59. * This program is distributed in the hope that it will be useful,
  60. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  61. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  62. * GNU Affero General Public License for more details.
  63. *
  64. * You should have received a copy of the GNU Affero General Public License, version 3,
  65. * along with this program. If not, see <http://www.gnu.org/licenses/>
  66. *
  67. */
  68. EOD;
  69. $this->licenseTextLegacy = str_replace('@YEAR@', date("Y"), $this->licenseTextLegacy);
  70. }
  71. /**
  72. * @param string|string[] $folder
  73. * @param string|bool $gitRoot
  74. */
  75. function exec($folder, $gitRoot = false) {
  76. if (is_array($folder)) {
  77. foreach($folder as $f) {
  78. $this->exec($f, $gitRoot);
  79. }
  80. return;
  81. }
  82. if ($gitRoot !== false && substr($gitRoot, -1) !== '/') {
  83. $gitRoot .= '/';
  84. }
  85. if (is_file($folder)) {
  86. $this->handleFile($folder, $gitRoot);
  87. return;
  88. }
  89. $excludes = array_map(function($item) use ($folder) {
  90. return $folder . '/' . $item;
  91. }, ['vendor', '3rdparty', '.git', 'l10n', 'templates']);
  92. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  93. $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
  94. /** @var SplFileInfo $item */
  95. foreach($excludes as $exclude) {
  96. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. });
  102. $iterator = new RecursiveIteratorIterator($iterator);
  103. $iterator = new RegexIterator($iterator, '/^.+\.php$/i');
  104. foreach ($iterator as $file) {
  105. /** @var SplFileInfo $file */
  106. $this->handleFile($file, $gitRoot);
  107. }
  108. }
  109. function writeAuthorsFile() {
  110. ksort($this->authors);
  111. $template = "ownCloud is written by:
  112. @AUTHORS@
  113. With help from many libraries and frameworks including:
  114. Open Collaboration Services
  115. SabreDAV
  116. jQuery
  117. ";
  118. $authors = implode(PHP_EOL, array_map(function($author){
  119. return " - ".$author;
  120. }, $this->authors));
  121. $template = str_replace('@AUTHORS@', $authors, $template);
  122. file_put_contents(__DIR__.'/../AUTHORS', $template);
  123. }
  124. function handleFile($path, $gitRoot) {
  125. $source = file_get_contents($path);
  126. if ($this->isMITLicensed($source)) {
  127. echo "MIT licensed file: $path" . PHP_EOL;
  128. return;
  129. }
  130. if ($this->isOwnCloudLicensed($source)) {
  131. $authors = $this->getAuthors($path, $gitRoot, true);
  132. $license = str_replace('@AUTHORS@', $authors, $this->licenseTextLegacy);
  133. } else {
  134. $authors = $this->getAuthors($path, $gitRoot);
  135. $license = str_replace('@AUTHORS@', $authors, $this->licenseText);
  136. }
  137. $source = $this->eatOldLicense($source);
  138. $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
  139. file_put_contents($path,$source);
  140. echo "License updated: $path" . PHP_EOL;
  141. }
  142. /**
  143. * @param string $source
  144. */
  145. private function isMITLicensed($source) {
  146. $lines = explode(PHP_EOL, $source);
  147. while(!empty($lines)) {
  148. $line = $lines[0];
  149. array_shift($lines);
  150. if (strpos($line, 'The MIT License') !== false) {
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. private function isOwnCloudLicensed($source) {
  157. $lines = explode(PHP_EOL, $source);
  158. while(!empty($lines)) {
  159. $line = $lines[0];
  160. array_shift($lines);
  161. if (strpos($line, 'ownCloud, Inc') !== false) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * @param string $source
  169. * @return string
  170. */
  171. private function eatOldLicense($source) {
  172. $lines = explode(PHP_EOL, $source);
  173. while(!empty($lines)) {
  174. $line = $lines[0];
  175. if (strpos($line, '<?php') !== false) {
  176. array_shift($lines);
  177. continue;
  178. }
  179. if (strpos($line, '/**') !== false) {
  180. array_shift($lines);
  181. continue;
  182. }
  183. if (strpos($line, '*/') !== false ) {
  184. array_shift($lines);
  185. break;
  186. }
  187. if (strpos($line, '*') !== false) {
  188. array_shift($lines);
  189. continue;
  190. }
  191. if (trim($line) === '') {
  192. array_shift($lines);
  193. continue;
  194. }
  195. break;
  196. }
  197. return implode(PHP_EOL, $lines);
  198. }
  199. private function getAuthors($file, $gitRoot, $legacyFiles = false) {
  200. // only add authors that changed code and not the license header
  201. $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
  202. $buildDir = getcwd();
  203. if ($gitRoot) {
  204. chdir($gitRoot);
  205. $file = substr($file, strlen($gitRoot));
  206. }
  207. $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");
  208. if ($gitRoot) {
  209. chdir($buildDir);
  210. }
  211. $authors = explode(PHP_EOL, $out);
  212. $authors = array_filter($authors, function($author) {
  213. return !in_array($author, [
  214. '',
  215. 'Not Committed Yet <not.committed.yet>',
  216. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
  217. 'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
  218. ]);
  219. });
  220. if ($gitRoot) {
  221. $authors = array_map([$this, 'checkCoreMailMap'], $authors);
  222. $authors = array_unique($authors);
  223. }
  224. if ($legacyFiles) {
  225. $authors = array_map(function($author){
  226. $this->authors[$author] = $author;
  227. return " * @author $author";
  228. }, $authors);
  229. } else {
  230. $authors = array_map(function($author){
  231. $this->authors[$author] = $author;
  232. return " * @copyright Copyright (c) " . date("Y") . ", $author";
  233. }, $authors);
  234. }
  235. return implode(PHP_EOL, $authors);
  236. }
  237. private function checkCoreMailMap($author) {
  238. if (empty($this->mailMap)) {
  239. $content = file_get_contents(__DIR__ . '/../.mailmap');
  240. $entries = explode("\n", $content);
  241. foreach ($entries as $entry) {
  242. if (strpos($entry, '> ') === false) {
  243. $this->mailMap[$entry] = $entry;
  244. } else {
  245. list($use, $actual) = explode('> ', $entry);
  246. $this->mailMap[$actual] = $use . '>';
  247. }
  248. }
  249. }
  250. if (isset($this->mailMap[$author])) {
  251. return $this->mailMap[$author];
  252. }
  253. return $author;
  254. }
  255. }
  256. $licenses = new Licenses;
  257. if (isset($argv[1])) {
  258. $licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
  259. } else {
  260. $licenses->exec([
  261. '../apps/comments',
  262. '../apps/dav',
  263. '../apps/encryption',
  264. '../apps/federatedfilesharing',
  265. '../apps/federation',
  266. '../apps/files',
  267. '../apps/files_external',
  268. '../apps/files_sharing',
  269. '../apps/files_trashbin',
  270. '../apps/files_versions',
  271. '../apps/provisioning_api',
  272. '../apps/systemtags',
  273. '../apps/testing',
  274. '../apps/updatenotification',
  275. '../apps/user_ldap',
  276. '../core',
  277. '../lib',
  278. '../ocs',
  279. '../settings',
  280. '../console.php',
  281. '../cron.php',
  282. '../index.php',
  283. '../public.php',
  284. '../remote.php',
  285. '../status.php',
  286. '../version.php',
  287. ]);
  288. $licenses->writeAuthorsFile();
  289. }