license.php 5.5 KB

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