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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /**
  109. * @param string $source
  110. */
  111. private function isMITLicensed($source) {
  112. $lines = explode(PHP_EOL, $source);
  113. while(!empty($lines)) {
  114. $line = $lines[0];
  115. array_shift($lines);
  116. if (strpos($line, 'The MIT License') !== false) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /**
  123. * @param string $source
  124. */
  125. private function eatOldLicense($source) {
  126. $lines = explode(PHP_EOL, $source);
  127. while(!empty($lines)) {
  128. $line = $lines[0];
  129. if (strpos($line, '<?php') !== false) {
  130. array_shift($lines);
  131. continue;
  132. }
  133. if (strpos($line, '/**') !== false) {
  134. array_shift($lines);
  135. continue;
  136. }
  137. if (strpos($line, '*/') !== false ) {
  138. array_shift($lines);
  139. break;
  140. }
  141. if (strpos($line, '*') !== false) {
  142. array_shift($lines);
  143. continue;
  144. }
  145. if (trim($line) === '') {
  146. array_shift($lines);
  147. continue;
  148. }
  149. break;
  150. }
  151. return implode(PHP_EOL, $lines);
  152. }
  153. private function getAuthors($file) {
  154. // only add authors that changed code and not the license header
  155. $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
  156. $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");
  157. $authors = explode(PHP_EOL, $out);
  158. $authors = array_filter($authors, function($author) {
  159. return !in_array($author, [
  160. '',
  161. 'Not Committed Yet <not.committed.yet>',
  162. 'Jenkins for ownCloud <owncloud-bot@tmit.eu>']);
  163. });
  164. $authors = array_map(function($author){
  165. $this->authors[$author] = $author;
  166. return " * @author $author";
  167. }, $authors);
  168. return implode(PHP_EOL, $authors);
  169. }
  170. }
  171. $licenses = new Licenses;
  172. if (isset($argv[1])) {
  173. $licenses->exec($argv[1]);
  174. } else {
  175. $licenses->exec([
  176. '../apps/dav',
  177. '../apps/encryption',
  178. '../apps/federation',
  179. '../apps/files',
  180. '../apps/files_external',
  181. '../apps/files_sharing',
  182. '../apps/files_trashbin',
  183. '../apps/files_versions',
  184. '../apps/provisioning_api',
  185. '../apps/user_ldap',
  186. '../core',
  187. '../lib',
  188. '../ocs',
  189. '../settings',
  190. '../console.php',
  191. '../cron.php',
  192. '../index.php',
  193. '../public.php',
  194. '../remote.php',
  195. '../status.php',
  196. '../version.php',
  197. ]);
  198. $licenses->writeAuthorsFile();
  199. }