| <?php | |
| /******************************************************************************* | |
| * Copyright (c) 2017 Eclipse Foundation and others. | |
| * All rights reserved. This program and the accompanying materials | |
| * are made available under the terms of the Eclipse Public License v1.0 | |
| * which accompanies this distribution, and is available at | |
| * http://www.eclipse.org/legal/epl-v10.html | |
| *******************************************************************************/ | |
| use Composer\Spdx\SpdxLicenses; | |
| require_once(dirname(__FILE__) . '/spdx-licenses/src/SpdxLicenses.php'); | |
| class SPDX { | |
| static $SpdxLicenses; | |
| private static function getSpdxLicenses() { | |
| if (!self::$SpdxLicenses) | |
| self::$SpdxLicenses = new SpdxLicenses(); | |
| return self::$SpdxLicenses; | |
| } | |
| /** | |
| * Normalize the license text into an SPDX expression if possible. | |
| * | |
| * We have lots of old data that expresses licenses in various | |
| * forms. This function tries to make sense of the form to express | |
| * what's there in SPDX format. | |
| * | |
| * @param string $text | |
| * @return string | |
| */ | |
| static function asSpdx($text) { | |
| // if ($spdx = self::getSpdxLicenses()->getIdentifierByName($text)) | |
| // return $spdx; | |
| $replacements = array( | |
| '/ASL/' => 'Apache-2.0', | |
| '/Apache 1\.1/i' => 'Apache-1.1', | |
| '/Apache License,? 2\.0/i' => 'Apache-2.0', | |
| '/Apache Software License 1\.1/' => 'Apache-1.1', | |
| '/New BSD License/i' => 'BSD-2-Clause', | |
| '/Common Development and Distribution License(?=,|$)/i' => 'CDDL-1.0', | |
| '/Common Development and Distribution License 1\.0/i' => 'CDDL-1.0', | |
| '/Common Development and Distribution License 1\.1/i' => 'CDDL-1.1', | |
| '/Creative Commons Attribution(?: License)? (\d\.\d)(?: unported)?(?: international)?/i' => 'CC-BY-\1', | |
| '/Common Public License 1\.0/i' => 'CPL-1.0', | |
| '/Eclipse Public License(?=,|$)/i' => 'EPL-1.0', | |
| '/Eclipse Public License v?1\.0/i' => 'EPL-1.0', | |
| '/MIT License/i' => 'MIT', | |
| '/Mozilla Public License 1\.1/i' => 'MPL-1.1', | |
| '/Mozilla Public License 2\.0/i' => 'MPL-2.0', | |
| '/Public[\s-_]Domain/i' => 'LicenseRef-Public-Domain', | |
| '/SIL OPEN FONT LICENSE \(OFL-1\.1\)/i' => 'OFL-1.1', | |
| '/W3C License/' => 'W3C' | |
| ); | |
| $licenses = preg_replace(array_keys($replacements), array_values($replacements), trim($text)); | |
| $licenses = preg_replace('/\s*\([^\)]*\)\s*/', '', $licenses); | |
| $spdx = implode(' OR ', preg_split('/\s*,\s*/', $licenses)); | |
| if (self::getSpdxLicenses()->validate($spdx)) return $spdx; | |
| return null; | |
| } | |
| } | |
| ?> |