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 6.9KB

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