2015-02-20 11:16:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Thomas Müller
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Licenses
|
|
|
|
{
|
2016-02-12 08:06:07 +00:00
|
|
|
protected $paths = [];
|
|
|
|
protected $mailMap = [];
|
2015-10-26 12:51:22 +00:00
|
|
|
public $authors = [];
|
2015-02-20 11:16:23 +00:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->licenseText = <<<EOD
|
|
|
|
/**
|
2016-06-06 16:19:29 +00:00
|
|
|
@AUTHORS@
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
EOD;
|
|
|
|
$this->licenseTextLegacy = <<<EOD
|
|
|
|
/**
|
2015-02-20 11:16:23 +00:00
|
|
|
@AUTHORS@
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) @YEAR@, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
EOD;
|
2016-06-06 16:19:29 +00:00
|
|
|
$this->licenseTextLegacy = str_replace('@YEAR@', date("Y"), $this->licenseTextLegacy);
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 08:04:40 +00:00
|
|
|
/**
|
|
|
|
* @param string|string[] $folder
|
|
|
|
* @param string|bool $gitRoot
|
|
|
|
*/
|
|
|
|
function exec($folder, $gitRoot = false) {
|
2015-02-20 11:16:23 +00:00
|
|
|
|
|
|
|
if (is_array($folder)) {
|
|
|
|
foreach($folder as $f) {
|
2016-02-12 08:04:40 +00:00
|
|
|
$this->exec($f, $gitRoot);
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:04:40 +00:00
|
|
|
if ($gitRoot !== false && substr($gitRoot, -1) !== '/') {
|
|
|
|
$gitRoot .= '/';
|
|
|
|
}
|
|
|
|
|
2015-02-20 11:16:23 +00:00
|
|
|
if (is_file($folder)) {
|
2016-02-12 08:04:40 +00:00
|
|
|
$this->handleFile($folder, $gitRoot);
|
2015-02-20 11:16:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$excludes = array_map(function($item) use ($folder) {
|
|
|
|
return $folder . '/' . $item;
|
|
|
|
}, ['vendor', '3rdparty', '.git', 'l10n', 'templates']);
|
|
|
|
|
|
|
|
$iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
|
|
|
|
$iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
|
|
|
|
/** @var SplFileInfo $item */
|
|
|
|
foreach($excludes as $exclude) {
|
|
|
|
if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
$iterator = new RecursiveIteratorIterator($iterator);
|
|
|
|
$iterator = new RegexIterator($iterator, '/^.+\.php$/i');
|
|
|
|
|
|
|
|
foreach ($iterator as $file) {
|
|
|
|
/** @var SplFileInfo $file */
|
2016-02-12 08:04:40 +00:00
|
|
|
$this->handleFile($file, $gitRoot);
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
2015-10-26 12:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeAuthorsFile() {
|
|
|
|
ksort($this->authors);
|
|
|
|
$template = "ownCloud is written by:
|
|
|
|
@AUTHORS@
|
2015-02-20 11:16:23 +00:00
|
|
|
|
2015-10-26 12:51:22 +00:00
|
|
|
With help from many libraries and frameworks including:
|
|
|
|
Open Collaboration Services
|
|
|
|
SabreDAV
|
|
|
|
jQuery
|
|
|
|
…
|
|
|
|
";
|
|
|
|
$authors = implode(PHP_EOL, array_map(function($author){
|
|
|
|
return " - ".$author;
|
|
|
|
}, $this->authors));
|
|
|
|
$template = str_replace('@AUTHORS@', $authors, $template);
|
|
|
|
file_put_contents(__DIR__.'/../AUTHORS', $template);
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 08:04:40 +00:00
|
|
|
function handleFile($path, $gitRoot) {
|
2015-02-20 11:16:23 +00:00
|
|
|
$source = file_get_contents($path);
|
2015-03-17 10:58:46 +00:00
|
|
|
if ($this->isMITLicensed($source)) {
|
|
|
|
echo "MIT licensed file: $path" . PHP_EOL;
|
|
|
|
return;
|
|
|
|
}
|
2016-06-06 16:19:29 +00:00
|
|
|
if ($this->isOwnCloudLicensed($source)) {
|
|
|
|
$authors = $this->getAuthors($path, $gitRoot, true);
|
|
|
|
$license = str_replace('@AUTHORS@', $authors, $this->licenseTextLegacy);
|
|
|
|
} else {
|
|
|
|
$authors = $this->getAuthors($path, $gitRoot);
|
|
|
|
$license = str_replace('@AUTHORS@', $authors, $this->licenseText);
|
|
|
|
}
|
2015-02-20 11:16:23 +00:00
|
|
|
|
2016-06-06 16:19:29 +00:00
|
|
|
$source = $this->eatOldLicense($source);
|
2015-02-20 11:16:23 +00:00
|
|
|
$source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
|
|
|
|
file_put_contents($path,$source);
|
2015-03-17 10:58:46 +00:00
|
|
|
echo "License updated: $path" . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2016-01-15 12:54:26 +00:00
|
|
|
/**
|
|
|
|
* @param string $source
|
|
|
|
*/
|
2015-03-17 10:58:46 +00:00
|
|
|
private function isMITLicensed($source) {
|
|
|
|
$lines = explode(PHP_EOL, $source);
|
|
|
|
while(!empty($lines)) {
|
|
|
|
$line = $lines[0];
|
|
|
|
array_shift($lines);
|
|
|
|
if (strpos($line, 'The MIT License') !== false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-06 16:19:29 +00:00
|
|
|
private function isOwnCloudLicensed($source) {
|
|
|
|
$lines = explode(PHP_EOL, $source);
|
|
|
|
while(!empty($lines)) {
|
|
|
|
$line = $lines[0];
|
|
|
|
array_shift($lines);
|
|
|
|
if (strpos($line, 'ownCloud, Inc') !== false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-15 12:54:26 +00:00
|
|
|
/**
|
|
|
|
* @param string $source
|
2016-02-12 08:04:40 +00:00
|
|
|
* @return string
|
2016-01-15 12:54:26 +00:00
|
|
|
*/
|
2015-02-20 11:16:23 +00:00
|
|
|
private function eatOldLicense($source) {
|
|
|
|
$lines = explode(PHP_EOL, $source);
|
|
|
|
while(!empty($lines)) {
|
|
|
|
$line = $lines[0];
|
|
|
|
if (strpos($line, '<?php') !== false) {
|
|
|
|
array_shift($lines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strpos($line, '/**') !== false) {
|
|
|
|
array_shift($lines);
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-04 13:33:40 +00:00
|
|
|
if (strpos($line, '*/') !== false ) {
|
2015-02-20 11:16:23 +00:00
|
|
|
array_shift($lines);
|
2015-03-04 13:33:40 +00:00
|
|
|
break;
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
2015-03-04 13:33:40 +00:00
|
|
|
if (strpos($line, '*') !== false) {
|
2015-02-20 11:16:23 +00:00
|
|
|
array_shift($lines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (trim($line) === '') {
|
|
|
|
array_shift($lines);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(PHP_EOL, $lines);
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:19:29 +00:00
|
|
|
private function getAuthors($file, $gitRoot, $legacyFiles = false) {
|
2015-10-05 19:15:01 +00:00
|
|
|
// only add authors that changed code and not the license header
|
|
|
|
$licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
|
2016-02-12 08:04:40 +00:00
|
|
|
$buildDir = getcwd();
|
|
|
|
if ($gitRoot) {
|
|
|
|
chdir($gitRoot);
|
|
|
|
$file = substr($file, strlen($gitRoot));
|
|
|
|
}
|
2015-10-05 19:15:01 +00:00
|
|
|
$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");
|
2016-02-12 08:04:40 +00:00
|
|
|
if ($gitRoot) {
|
|
|
|
chdir($buildDir);
|
|
|
|
}
|
2015-02-20 11:16:23 +00:00
|
|
|
$authors = explode(PHP_EOL, $out);
|
|
|
|
|
|
|
|
$authors = array_filter($authors, function($author) {
|
2015-02-25 13:00:30 +00:00
|
|
|
return !in_array($author, [
|
|
|
|
'',
|
|
|
|
'Not Committed Yet <not.committed.yet>',
|
2016-03-01 16:48:23 +00:00
|
|
|
'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
|
|
|
|
'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
|
|
|
|
]);
|
2015-02-20 11:16:23 +00:00
|
|
|
});
|
2016-02-12 08:06:07 +00:00
|
|
|
|
|
|
|
if ($gitRoot) {
|
|
|
|
$authors = array_map([$this, 'checkCoreMailMap'], $authors);
|
|
|
|
$authors = array_unique($authors);
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:19:29 +00:00
|
|
|
if ($legacyFiles) {
|
|
|
|
$authors = array_map(function($author){
|
|
|
|
$this->authors[$author] = $author;
|
|
|
|
return " * @author $author";
|
|
|
|
}, $authors);
|
|
|
|
} else {
|
|
|
|
$authors = array_map(function($author){
|
|
|
|
$this->authors[$author] = $author;
|
|
|
|
return " * @copyright Copyright (c) " . date("Y") . ", $author";
|
|
|
|
}, $authors);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 11:16:23 +00:00
|
|
|
return implode(PHP_EOL, $authors);
|
|
|
|
}
|
2016-02-12 08:06:07 +00:00
|
|
|
|
|
|
|
private function checkCoreMailMap($author) {
|
|
|
|
if (empty($this->mailMap)) {
|
|
|
|
$content = file_get_contents(__DIR__ . '/../.mailmap');
|
|
|
|
$entries = explode("\n", $content);
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
if (strpos($entry, '> ') === false) {
|
|
|
|
$this->mailMap[$entry] = $entry;
|
|
|
|
} else {
|
|
|
|
list($use, $actual) = explode('> ', $entry);
|
|
|
|
$this->mailMap[$actual] = $use . '>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->mailMap[$author])) {
|
|
|
|
return $this->mailMap[$author];
|
|
|
|
}
|
|
|
|
return $author;
|
|
|
|
}
|
2015-02-20 11:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$licenses = new Licenses;
|
2015-03-04 13:33:40 +00:00
|
|
|
if (isset($argv[1])) {
|
2016-02-12 08:04:40 +00:00
|
|
|
$licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
|
2015-03-04 13:33:40 +00:00
|
|
|
} else {
|
|
|
|
$licenses->exec([
|
2016-03-01 16:52:32 +00:00
|
|
|
'../apps/comments',
|
2016-01-12 13:39:31 +00:00
|
|
|
'../apps/dav',
|
2015-03-30 20:36:48 +00:00
|
|
|
'../apps/encryption',
|
2016-03-01 16:52:32 +00:00
|
|
|
'../apps/federatedfilesharing',
|
2016-01-12 13:39:31 +00:00
|
|
|
'../apps/federation',
|
|
|
|
'../apps/files',
|
2015-03-04 13:33:40 +00:00
|
|
|
'../apps/files_external',
|
|
|
|
'../apps/files_sharing',
|
|
|
|
'../apps/files_trashbin',
|
|
|
|
'../apps/files_versions',
|
|
|
|
'../apps/provisioning_api',
|
2016-03-01 16:52:32 +00:00
|
|
|
'../apps/systemtags',
|
|
|
|
'../apps/testing',
|
|
|
|
'../apps/updatenotification',
|
2015-03-04 13:33:40 +00:00
|
|
|
'../apps/user_ldap',
|
|
|
|
'../core',
|
|
|
|
'../lib',
|
|
|
|
'../ocs',
|
|
|
|
'../settings',
|
|
|
|
'../console.php',
|
|
|
|
'../cron.php',
|
|
|
|
'../index.php',
|
|
|
|
'../public.php',
|
|
|
|
'../remote.php',
|
|
|
|
'../status.php',
|
|
|
|
'../version.php',
|
|
|
|
]);
|
2015-10-26 12:51:22 +00:00
|
|
|
$licenses->writeAuthorsFile();
|
2015-03-04 13:33:40 +00:00
|
|
|
}
|