blob: 14a144171ab4e64dd2fedb0e942be7ad41745367 [file] [log] [blame]
<?php
/*******************************************************************************
* Copyright (c) 2017 Eclipse Foundation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
require_once (dirname(__FILE__) . "/database.inc");
class License {
var $id;
var $text;
var $url;
private function __construct($id, $text, $url) {
$this->id = $id;
$this->text = $text;
$this->url = $url;
}
public static function getLicense($id) {
// FIXME This should be in the (a) database.
switch ($id) {
case 'EPL2.0':
case 'EPL-2.0':
return new License('EPL-2.0', 'Eclipse Public License v. 2.0', 'http://www.eclipse.org/legal/epl-2.0');
case 'EPL1.0':
case 'EPL-1.0':
return new License('EPL-1.0', 'Eclipse Public License v. 1.0', 'http://www.eclipse.org/legal/epl-v10.html');
case 'EDL1.0':
case 'EDL-1.0':
case 'EDL':
return new License('BSD-3-Clause', 'Eclipse Distribution License v. 1.0', 'http://www.eclipse.org/org/documents/edl-v10.php');
case 'ASL2.0':
case 'Apache-2.0':
return new License('Apache-2.0', 'Apache License, Version 2.0', 'https://www.apache.org/licenses/LICENSE-2.0');
case 'CCBY3':
case 'CC-BY-3.0':
return new License('CC-BY-3.0', 'Creative Commons Attribution 3.0 Unported', 'https://creativecommons.org/licenses/by/4.0/');
case 'GPL-2.0':
return new License('GPL-2.0', 'GNU General Public License, version 2', 'https://www.gnu.org/copyleft/gpl.html');
case 'GPL-2.0_CP':
return new License('GPL-2.0 WITH Classpath-exception-2.0', 'GNU General Public License, version 2 with the GNU Classpath Exception', 'https://www.gnu.org/software/classpath/license.html');
case 'GPL-2.0_AE':
return new License('LicenseRef-GPL-2.0-with-Assembly-exception', 'GNU General Public License, version 2 with OpenJDK Assembly Exception', 'http://openjdk.java.net/legal/assembly-exception.html');
case 'W3C-20150513':
return new License('W3C-20150513', 'W3C Software Notice and Document License (2015-05-13)','https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document');
case 'OFL-1.1':
return new License('OFL-1.1', 'SIL Open Font License 1.1', 'http://scripts.sil.org/cms/scripts/page.php?item_id=OFL_web');
default:
return new License('UNKNOWN', 'A LICENSE THAT WE DON\'T SUPPORT YET', 'https://www.eclipse.org/legal/licenses.php');
}
}
public static function getLicenses($codes) {
$licenses = array();
foreach($codes as $code) {
$licenses[] = self::getLicense($code);
}
return $licenses;
}
/**
* Return an array of licenses for a project.
*
* @param string $id Project Id
* @return License[]
*/
public static function getLicensesForProject($id) {
$sql = "
select
l.LicenseId as id,
l.description as text
from ProjectLicenses as pl
join SYS_Licenses as l on pl.LicenseId=l.LicenseId
where pl.ProjectId='$id'";
$licenses = array ();
query ( 'foundation', $sql, array (), function ($row) use (&$licenses) {
$licenses [] = self::getLicense($row ['id']);
} );
self::sortLicenses($licenses);
return $licenses;
}
private static function sortLicenses(&$licenses) {
// Make sure that the EPL is always at the top of the list.
usort($licenses, function($a, $b) {
if ($a->getId() == 'EPL-2.0') return -1;
if ($b->getId() == 'EPL-2.0') return 1;
if ($a->getId() == 'EPL-1.0') return -1;
if ($b->getId() == 'EPL-1.0') return 1;
return strcasecmp($a->getId(), $b->getId());
});
}
public static function getDefaultFileHeader($licenses) {
$lines = array();
$lines[] = "Copyright (c) {date} {owner}[ and others]";
return self::getHeader($licenses, $lines);
}
public static function getAlternativeFileHeader($licenses) {
$lines = array();
$lines[] = "Copyright (c) {date} Contributors to the Eclipse Foundation";
$lines[] = "See the NOTICE file(s) distributed with this work for additional information regarding copyright ownership.";
return self::getHeader($licenses, $lines);
}
private static function getHeader($licenses, $lines) {
$lines[] = self::getLicensesStatement($licenses);
if (count($licenses) == 1 && ($licenses[0]->getSPDXCode() == 'Apache-2.0')) {
$lines[] = 'Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.';
}
$lines[] = "SPDX-License-Identifier: " . self::getSPDXExpression($licenses);
return implode("\n\n", $lines);
}
/**
* Answer a statement (suitable for use in a file header)
* that describes the licenses of the project.
*
* @param License[] $licenses
* @param string $nbsp value to use as the "non-breaking" space in the license names.
* @return string
*/
public static function getLicensesStatement($licenses) {
// Initial statement of license(s)
$parts = array();
$secondary = array();
foreach($licenses as $license) {
$text = $license->getText();
if ($license->isSecondaryLicense()) {
$secondary[] = "{$text} which is available at {$license->getUrl()}";
} else {
$parts[] = "{$text} which is available at {$license->getUrl()}";
}
}
$statement = "This program and the accompanying materials are made available under the terms of the ";
$statement .= implodeWithConjunction($parts, 'or the') . ".";
if ($secondary) {
$statement .= "\n\n";
// FIXME License text is inlined; should come from data.
$epl20 = License::getLicense('EPL-2.0');
// FIXME We assume that secondary licenses only apply to EPL-2.0 (true for now)
$statement .= "This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the {$epl20->getText()} are satisfied: ";
$statement .= implodeWithConjunction($secondary, 'and') . ".";
}
return $statement;
}
public static function getSPDXExpression($licenses) {
$codes = array();
foreach($licenses as $license) {
$codes[] = $license->getSPDXCode();
}
return implode(' OR ', $codes);
}
public static function parseLicenses($text) {
$licenses = array();
foreach(explode(',', $text) as $code) {
if ($license = self::getLicense($code)) {
$licenses[] = $license;
}
}
if (!$licenses) $licenses[] = License::getLicense('EPL-2.0');
self::sortLicenses($licenses);
return $licenses;
}
public function getId() {
return $this->id;
}
public function getText() {
return $this->text;
}
public function getSPDXCode() {
return $this->id;
}
/**
* Answers true when the receiver is a secondary
* license according to the terms of the EPL-2.0,
* or false otherwise.
*
* Note that the ability for a license to decide
* whether or not it is secondary is temporary
* functionality based on a stop-gap implementation.
*
* TODO Reconsider this implementation.
*/
public function isSecondaryLicense() {
return preg_match("/^GPL/", $this->getId());
}
public function getUrl() {
return $this->url;
}
}
?>