license.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = array();
  24. public function __construct() {
  25. $this->licenseText = <<<EOD
  26. /**
  27. @AUTHORS@
  28. *
  29. * @copyright Copyright (c) @YEAR@, ownCloud, Inc.
  30. * @license AGPL-3.0
  31. *
  32. * This code is free software: you can redistribute it and/or modify
  33. * it under the terms of the GNU Affero General Public License, version 3,
  34. * as published by the Free Software Foundation.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU Affero General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU Affero General Public License, version 3,
  42. * along with this program. If not, see <http://www.gnu.org/licenses/>
  43. *
  44. */
  45. EOD;
  46. $this->licenseText = str_replace('@YEAR@', date("Y"), $this->licenseText);
  47. }
  48. function exec($folder) {
  49. if (is_array($folder)) {
  50. foreach($folder as $f) {
  51. $this->exec($f);
  52. }
  53. return;
  54. }
  55. if (is_file($folder)) {
  56. $this->handleFile($folder);
  57. return;
  58. }
  59. $excludes = array_map(function($item) use ($folder) {
  60. return $folder . '/' . $item;
  61. }, ['vendor', '3rdparty', '.git', 'l10n', 'templates']);
  62. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  63. $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
  64. /** @var SplFileInfo $item */
  65. foreach($excludes as $exclude) {
  66. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. });
  72. $iterator = new RecursiveIteratorIterator($iterator);
  73. $iterator = new RegexIterator($iterator, '/^.+\.php$/i');
  74. foreach ($iterator as $file) {
  75. /** @var SplFileInfo $file */
  76. $this->handleFile($file);
  77. }
  78. }
  79. function handleFile($path) {
  80. $source = file_get_contents($path);
  81. if ($this->isMITLicensed($source)) {
  82. echo "MIT licensed file: $path" . PHP_EOL;
  83. return;
  84. }
  85. $source = $this->eatOldLicense($source);
  86. $authors = $this->getAuthors($path);
  87. $license = str_replace('@AUTHORS@', $authors, $this->licenseText);
  88. $source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
  89. file_put_contents($path,$source);
  90. echo "License updated: $path" . PHP_EOL;
  91. }
  92. private function isMITLicensed($source) {
  93. $lines = explode(PHP_EOL, $source);
  94. while(!empty($lines)) {
  95. $line = $lines[0];
  96. array_shift($lines);
  97. if (strpos($line, 'The MIT License') !== false) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. private function eatOldLicense($source) {
  104. $lines = explode(PHP_EOL, $source);
  105. while(!empty($lines)) {
  106. $line = $lines[0];
  107. if (strpos($line, '<?php') !== false) {
  108. array_shift($lines);
  109. continue;
  110. }
  111. if (strpos($line, '/**') !== false) {
  112. array_shift($lines);
  113. continue;
  114. }
  115. if (strpos($line, '*/') !== false ) {
  116. array_shift($lines);
  117. break;
  118. }
  119. if (strpos($line, '*') !== false) {
  120. array_shift($lines);
  121. continue;
  122. }
  123. if (trim($line) === '') {
  124. array_shift($lines);
  125. continue;
  126. }
  127. break;
  128. }
  129. return implode(PHP_EOL, $lines);
  130. }
  131. private function getAuthors($file) {
  132. $out = shell_exec("git blame --line-porcelain $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /' | sort -f | uniq");
  133. $authors = explode(PHP_EOL, $out);
  134. $authors = array_filter($authors, function($author) {
  135. return !in_array($author, [
  136. '',
  137. 'Not Committed Yet <not.committed.yet>',
  138. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>']);
  139. });
  140. $authors = array_map(function($author){
  141. return " * @author $author";
  142. }, $authors);
  143. return implode(PHP_EOL, $authors);
  144. }
  145. }
  146. $licenses = new Licenses;
  147. if (isset($argv[1])) {
  148. $licenses->exec($argv[1]);
  149. } else {
  150. $licenses->exec([
  151. '../apps/files',
  152. '../apps/encryption',
  153. '../apps/files_external',
  154. '../apps/files_sharing',
  155. '../apps/files_trashbin',
  156. '../apps/files_versions',
  157. '../apps/provisioning_api',
  158. '../apps/user_ldap',
  159. '../apps/user_webdavauth',
  160. '../core',
  161. '../lib',
  162. '../ocs',
  163. '../settings',
  164. '../console.php',
  165. '../cron.php',
  166. '../index.php',
  167. '../public.php',
  168. '../remote.php',
  169. '../status.php',
  170. '../version.php',
  171. ]);
  172. }