9 use Composer\Spdx\SpdxLicenses;
16 use Symfony\Component\HttpFoundation\Response;
18 define(
"TITLE_ADMIN_LICENSE_FILE", _(
"License Administration"));
30 $this->Name =
"admin_license";
31 $this->Title = TITLE_ADMIN_LICENSE_FILE;
32 $this->MenuList =
"Admin::License Admin";
34 $this->vars = array();
35 $this->obligationSelectorName =
"assocObligations";
36 $this->obligationSelectorId =
"assocObligations";
37 parent::__construct();
39 $this->
dbManager = $GLOBALS[
'container']->get(
'db.manager');
40 $this->licenseDao = $GLOBALS[
'container']->get(
'dao.license');
48 if ($this->
State != PLUGIN_STATE_READY) {
52 $URL = $this->Name.
"&add=y";
53 $text = _(
"Add new license");
54 menu_insert(
"Main::".$this->MenuList.
"::Add License",0, $URL, $text);
56 $text = _(
"Select license family");
57 menu_insert(
"Main::".$this->MenuList.
"::Select License",0, $URL, $text);
63 $errorstr =
"License not added";
66 if (@$_POST[
"updateit"]) {
67 $resultstr = $this->
Updatedb($_POST);
68 $this->vars[
'message'] = $resultstr;
69 if (strstr($resultstr, $errorstr)) {
70 return $this->Updatefm(0);
76 if (@$_REQUEST[
'add'] ==
'y') {
77 return $this->Updatefm(0);
81 if (@$_POST[
"addit"]) {
82 $resultstr = $this->
Adddb();
83 $this->vars[
'message'] = $resultstr;
84 if (strstr($resultstr, $errorstr)) {
85 return $this->Updatefm(0);
92 $rf_pk = @$_REQUEST[
'rf_pk'];
94 return $this->Updatefm($rf_pk);
98 if (@$_GET[
"getLicenseText"] && @$_GET[
"licenseID"]) {
99 $licenseText = $this->getLicenseTextForID(@$_GET[
"licenseID"]);
100 if (! $licenseText) {
101 return new Response(
"Error in querying license text.",
102 Response::HTTP_BAD_REQUEST, array(
103 'Content-type' =>
'text/plain'
106 return new Response($licenseText, Response::HTTP_OK,
108 'Content-type' =>
'text/plain'
112 if (@$_POST[
"req_shortname"]) {
113 $this->vars += $this->
getLicenseListData($_POST[
"req_shortname"], $_POST[
"req_marydone"]);
115 $this->vars[
'Inputfm'] = $this->
Inputfm();
116 return $this->
render(
'admin_license_file.html.twig');
126 $V =
"<FORM name='Inputfm' action='?mod=" . $this->Name .
"' method='POST'>";
127 $V.= _(
"What license family do you wish to view:<br>");
133 $V.=
"<select name='req_marydone'>\n";
134 $Selected = (@$_REQUEST[
'req_marydone'] ==
'all') ?
" SELECTED ":
"";
136 $V.=
"<option value='all' $Selected> $text </option>";
137 $Selected = (@$_REQUEST[
'req_marydone'] ==
'done') ?
" SELECTED ":
"";
138 $text = _(
"Checked");
139 $V.=
"<option value='done' $Selected> $text </option>";
140 $Selected = (@$_REQUEST[
'req_marydone'] ==
'notdone') ?
" SELECTED ":
"";
141 $text = _(
"Not Checked");
142 $V.=
"<option value='notdone' $Selected> $text </option>";
144 $V.=
" ";
147 $V.= _(
"License family name: ");
148 $Shortnamearray = $this->FamilyNames();
149 $Shortnamearray = array(
"All"=>
"All") + $Shortnamearray;
150 $Selected = @$_REQUEST[
'req_shortname'];
153 $V.=
" ";
155 $V.=
"<INPUT type='submit' value='$text'>\n";
163 private function getLicenseData($where)
165 $sql =
"SELECT rf_pk, marydone, rf_shortname, rf_spdx_id, " .
166 "rf_shortname, rf_fullname, rf_url, rf_text, ".
167 "string_agg(ob_topic, ';') AS ob_topic " .
168 "FROM ONLY license_ref " .
169 "LEFT OUTER JOIN obligation_map ON rf_fk = rf_pk " .
170 "LEFT OUTER JOIN obligation_ref ON ob_fk = ob_pk " .
171 "$where GROUP BY rf_pk ORDER BY rf_shortname";
187 if ($namestr ==
"All") {
190 $where =
"where rf_shortname like '" . pg_escape_string($namestr) .
"%' ";
194 if ($filter !=
"all") {
200 if ($filter ==
"done") {
201 $where .=
" marydone=true";
203 if ($filter ==
"notdone") {
204 $where .=
" marydone=false";
208 $data = $this->getLicenseData($where);
211 "No licenses matching the filter and name pattern were found");
213 $dataSize =
sizeof($data);
219 $dataMessage = $dataSize . _(
" License$plural found");
224 'dataMessage' => $dataMessage,
229 function Updatefm($rf_pk)
232 return $this->
render(
'admin_license-upload_form.html.twig', $this->vars);
247 if (0 < count($_POST)) {
248 $rf_pk_update = $_POST[
'rf_pk'];
249 if (! empty($rf_pk)) {
250 $rf_pk_update = $rf_pk;
251 }
else if (empty($rf_pk_update)) {
252 $rf_pk_update = $_GET[
'rf_pk'];
256 $vars[
'obligationSelectorName'] = $this->obligationSelectorName .
"[]";
257 $vars[
'obligationSelectorId'] = $this->obligationSelectorId;
259 $vars[
'actionUri'] =
"?mod=" . $this->Name .
"&rf_pk=$rf_pk_update";
260 $vars[
'req_marydone'] = array_key_exists(
'req_marydone', $_POST) ? $_POST[
'req_marydone'] :
'';
261 $vars[
'req_shortname'] = array_key_exists(
'req_shortname', $_POST) ? $_POST[
'req_shortname'] :
'';
262 $vars[
'rf_shortname'] = array_key_exists(
'rf_shortname', $_POST) ? $_POST[
'rf_shortname'] :
'';
263 $vars[
'rf_fullname'] = array_key_exists(
'rf_fullname', $_POST) ? $_POST[
'rf_fullname'] :
'';
264 $vars[
'rf_text'] = array_key_exists(
'rf_text', $_POST) ? $_POST[
'rf_text'] :
'';
265 $vars[
'rf_licensetype'] = array_key_exists(
'rf_licensetype', $_POST) ? $_POST[
'rf_licensetype'] :
'---';
266 $selectedObligations = array_key_exists($this->obligationSelectorName,
267 $_POST) ? $_POST[$this->obligationSelectorName] : [];
270 $parentLicenes = $parentMap->getTopLevelLicenseRefs();
271 $vars[
'parentMap'] = array(0=>
'[self]');
272 foreach ($parentLicenes as $licRef) {
273 $vars[
'parentMap'][$licRef->getId()] = $licRef->getShortName();
277 $reportLicenes = $reportMap->getTopLevelLicenseRefs();
278 $vars[
'reportMap'] = array(0=>
'[self]');
279 foreach ($reportLicenes as $licRef) {
280 $vars[
'reportMap'][$licRef->getId()] = $licRef->getShortName();
284 $obligations = $obligationMap->getObligations();
285 foreach ($obligations as $obligation) {
286 $vars[
'obligationTopics'][$obligation[
'ob_pk']] = $obligation[
'ob_topic'];
288 foreach ($selectedObligations as $obligation) {
289 $row[
'obligationSelected'][$obligation] = $obligationMap->getTopicNameFromId(
295 "SELECT * FROM ONLY license_ref WHERE rf_pk=$1", array($rf_pk),
296 __METHOD__ .
'.forUpdate');
297 if ($row ===
false) {
298 $text = _(
"ERROR: No licenses matching this key");
299 $text1 = _(
"was found");
300 return [
"error" =>
"$text ($rf_pk) $text1."];
302 $row[
'rf_parent'] = $parentMap->getProjectedId($rf_pk);
303 $row[
'rf_report'] = $reportMap->getProjectedId($rf_pk);
305 $obligationsAssigned = $parentMap->getObligationsForLicenseRef($rf_pk);
306 foreach ($obligationsAssigned as $obligation) {
307 $row[
'obligationSelected'][$obligation] = $obligationMap->getTopicNameFromId(
314 'rf_text_updatable' =>
't',
319 'rf_detector_type' => 1,
321 'rf_licensetype' =>
'Permissive'
325 foreach (array_keys($row) as $key) {
326 if (array_key_exists($key, $_POST)) {
327 $row[$key] = $_POST[$key];
331 $vars[
'boolYesNoMap'] = array(
"true"=>
"Yes",
"false"=>
"No");
332 $row[
'rf_active'] = $this->
isTrue($row[
'rf_active']) ?
'true' :
'false';
333 $row[
'marydone'] = $this->
isTrue($row[
'marydone']) ?
'true' :
'false';
334 $row[
'rf_text_updatable'] = $this->
isTrue($row[
'rf_text_updatable']) ?
'true' :
'false';
335 $vars[
'risk_level'] = array_key_exists(
'risk_level', $_POST) ? intval($_POST[
'risk_level']) : $row[
'rf_risk'];
336 $vars[
'isReadOnly'] = !(empty($rf_pk) || $row[
'rf_text_updatable']==
'true');
337 $vars[
'detectorTypes'] = array(
"1"=>
"Reference License",
"2"=>
"Nomos",
"3"=>
"Unconcrete License");
338 $licenseType = $SysConf[
'SYSCONFIG'][
'LicenseTypes'];
339 $licenseType = explode(
',', $licenseType);
340 $licenseType = array_map(
'trim', $licenseType);
341 $vars[
'licenseTypes'] = array_combine($licenseType, $licenseType);
342 $vars[
'rfId'] = $rf_pk?:$rf_pk_update;
344 return array_merge($vars,$row);
353 if (is_bool($value)) {
356 $value = strtolower($value);
357 return ($value ===
't' || $value ===
'true');
364 $sql =
"SELECT count(*) from license_ref where rf_pk <> $1 and (LOWER(rf_shortname) = LOWER($2) or (rf_text <> ''
365 and rf_text = $3 and LOWER(rf_text) NOT LIKE 'license by nomos.'))";
366 $check_count = $this->
dbManager->getSingleRow($sql,
371 ), __METHOD__ .
'.countLicensesByNomos');
372 return (0 < $check_count[
'count']);
378 $sql =
"SELECT LOWER(rf_shortname) AS rf_shortname FROM license_ref WHERE rf_pk=$1";
379 $row = $this->
dbManager->getSingleRow($sql,array($rfId),__METHOD__.
'.DoNotChnageShortName');
380 if ($row[
'rf_shortname'] === strtolower($shortname)) {
394 $spdxLicenses =
new SpdxLicenses();
397 $rfId = intval($_POST[
'rf_pk']);
398 $shortname = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_shortname']));
399 $fullname = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_fullname']));
400 $url = $_POST[
'rf_url'];
401 $notes = $_POST[
'rf_notes'];
402 $text = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_text']));
403 $spdxId = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_spdx_id']));
404 $parent = $_POST[
'rf_parent'];
405 $report = $_POST[
'rf_report'];
406 $licensetype =
trim($_POST[
'rf_licensetype']);
407 $riskLvl = intval($_POST[
'risk_level']);
408 $selectedObligations = array_key_exists($this->obligationSelectorName,
409 $_POST) ? $_POST[$this->obligationSelectorName] : [];
411 if (! empty($spdxId) &&
412 strstr(strtolower($spdxId), strtolower(LicenseRef::SPDXREF_PREFIX)) ===
false) {
413 if (! $spdxLicenses->validate($spdxId)) {
414 $spdxId = LicenseRef::convertToSpdxId($spdxId,
null);
415 $errors[] =
"SPDX ID changed to $spdxId to be compliant with SPDX.";
417 } elseif (empty($spdxId)) {
420 if (! empty($spdxId)) {
421 $spdxId = LicenseRef::replaceSpaces($spdxId);
424 if (empty($shortname)) {
425 $text = _(
"ERROR: The license shortname is empty. License not added.");
426 return "<b>$text</b><p>";
430 $text = _(
"ERROR: The shortname can not be changed. License not added.");
431 return "<b>$text</b><p>";
434 $md5term = (empty($text) || stristr($text,
"License by Nomos")) ?
'null' :
'md5($10)';
436 $sql =
"UPDATE license_ref SET
437 rf_active=$2, marydone=$3, rf_shortname=$4, rf_fullname=$5,
438 rf_url=$6, rf_notes=$7, rf_text_updatable=$8, rf_detector_type=$9, rf_text=$10,
439 rf_md5=$md5term, rf_risk=$11, rf_spdx_id=$12, rf_flag=$13, rf_licensetype=$14
441 $params = array($rfId,
442 $_POST[
'rf_active'],$_POST[
'marydone'],$shortname,$fullname,
443 $url,$notes,$_POST[
'rf_text_updatable'],$_POST[
'rf_detector_type'],$text,
444 $riskLvl,$spdxId,2, $licensetype);
445 $statement = __METHOD__ .
".updateLicense";
446 if ($md5term ==
"null") {
447 $statement .=
".nullMD5";
449 $this->
dbManager->prepare($statement, $sql);
452 $licenseMapDelStatement = __METHOD__ .
'.deleteFromMap';
453 $licenseMapDelSql =
'DELETE FROM license_map WHERE rf_fk=$1 AND usage=$2';
454 $this->
dbManager->prepare($licenseMapDelStatement, $licenseMapDelSql);
459 $this->
dbManager->execute($licenseMapDelStatement,
460 array($rfId, LicenseMap::CONCLUSION));
462 $parentLicenses = $parentMap->getTopLevelLicenseRefs();
463 if (array_key_exists($parent, $parentLicenses) &&
464 $parent != $parentMap->getProjectedId($rfId)) {
465 $this->
dbManager->execute($licenseMapDelStatement,
466 array($rfId, LicenseMap::CONCLUSION));
467 $this->
dbManager->insertTableRow(
'license_map',
468 array(
'rf_fk'=>$rfId,
'rf_parent'=>$parent,
'usage'=>LicenseMap::CONCLUSION));
474 $this->
dbManager->execute($licenseMapDelStatement,
475 array($rfId, LicenseMap::REPORT));
478 $reportLicenses = $parentMap->getTopLevelLicenseRefs();
479 if (array_key_exists($report, $reportLicenses) &&
480 $report != $reportMap->getProjectedId($rfId)) {
481 $this->
dbManager->execute($licenseMapDelStatement,
482 array($rfId, LicenseMap::REPORT));
483 $this->
dbManager->insertTableRow(
'license_map',
484 array(
'rf_fk'=>$rfId,
'rf_parent'=>$report,
'usage'=>LicenseMap::REPORT));
489 foreach ($selectedObligations as $obligation) {
490 $obligationMap->associateLicenseWithObligation($obligation, $rfId);
493 $allObligations = $parentMap->getObligationsForLicenseRef($rfId);
494 $removedObligations = array_diff($allObligations, $selectedObligations);
495 foreach ($removedObligations as $obligation) {
496 $obligationMap->unassociateLicenseFromObligation($obligation, $rfId);
499 return "License $_POST[rf_shortname] updated. " . join(
" ", $errors) .
"<p>";
510 $spdxLicenses =
new SpdxLicenses();
513 $rf_shortname = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_shortname']));
514 $rf_fullname = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_fullname']));
515 $rf_spdx_id = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_spdx_id']));
516 $rf_url = $_POST[
'rf_url'];
517 $rf_notes = $_POST[
'rf_notes'];
518 $rf_text = StringOperation::replaceUnicodeControlChar(
trim($_POST[
'rf_text']));
519 $rf_licensetype =
trim($_POST[
'rf_licensetype']);
520 $parent = $_POST[
'rf_parent'];
521 $report = $_POST[
'rf_report'];
522 $riskLvl = intval($_POST[
'risk_level']);
523 $selectedObligations = array_key_exists($this->obligationSelectorName,
524 $_POST) ? $_POST[$this->obligationSelectorName] : [];
526 if (! empty($rf_spdx_id) &&
527 strstr(strtolower($rf_spdx_id), strtolower(LicenseRef::SPDXREF_PREFIX)) ===
false) {
528 if (! $spdxLicenses->validate($rf_spdx_id)) {
529 $rf_spdx_id = LicenseRef::convertToSpdxId($rf_spdx_id,
null);
530 $errors[] =
"SPDX ID changed to $rf_spdx_id to be compliant with SPDX.";
532 } elseif (empty($rf_spdx_id)) {
535 if (! empty($rf_spdx_id)) {
536 $rf_spdx_id = LicenseRef::replaceSpaces($rf_spdx_id);
539 if (empty($rf_shortname)) {
540 $text = _(
"ERROR: The license shortname is empty. License not added.");
541 return "<b>$text</b><p>";
545 $text = _(
"ERROR: The shortname or license text already exist in the license list. License not added.");
546 return "<b>$text</b><p>";
549 $md5term = (empty($rf_text) || stristr($rf_text,
"License by Nomos")) ?
'null' :
'md5($7)';
550 $stmt = __METHOD__.
'.rf';
551 $sql =
"INSERT into license_ref (
552 rf_active, marydone, rf_shortname, rf_fullname,
553 rf_url, rf_notes, rf_md5, rf_text, rf_text_updatable,
554 rf_detector_type, rf_risk, rf_spdx_id, rf_licensetype)
556 $1, $2, $3, $4, $5, $6, $md5term, $7, $8, $9, $10, $11, $12) RETURNING rf_pk";
567 $_POST[
'rf_text_updatable'],
568 $_POST[
'rf_detector_type'],
573 $row = $this->
dbManager->fetchArray($res);
574 $rfId = $row[
'rf_pk'];
577 $parentLicenses = $parentMap->getTopLevelLicenseRefs();
578 if (array_key_exists($parent, $parentLicenses)) {
579 $this->
dbManager->insertTableRow(
'license_map',
582 'rf_parent' => $parent,
583 'usage' => LicenseMap::CONCLUSION
588 $reportLicenses = $reportMap->getTopLevelLicenseRefs();
589 if (array_key_exists($report, $reportLicenses)) {
590 $this->
dbManager->insertTableRow(
'license_map',
593 'rf_parent' => $report,
594 'usage' => LicenseMap::REPORT
599 foreach ($selectedObligations as $obligation) {
600 $obligationMap->associateLicenseWithObligation($obligation, $rfId);
603 return "License $_POST[rf_shortname] (id=$rfId) added. " .
604 join(
" ", $errors) .
"<p>";
617 function FamilyNames()
619 $familynamearray = array();
620 $Shortnamearray =
DB2KeyValArray(
"license_ref",
"rf_pk",
"rf_shortname",
" order by rf_shortname");
623 foreach ($Shortnamearray as $shortname) {
625 if (($shortname ==
"No_license_found") || ($shortname ==
"Unknown license")) {
626 $familynamearray[$shortname] = $shortname;
628 $tok = strtok($shortname,
" _-([/");
629 $familynamearray[$tok] = $tok;
633 return ($familynamearray);
636 private function getLicenseTextForID($licenseID)
638 $license = $this->licenseDao->getLicenseById($licenseID);
640 if ($license ==
null) {
643 return $license->getText();
This is the Plugin class. All plugins should:
render($templateName, $vars=null)
Wrapper class for license map.
Wrapper class for obligation map.
__construct()
base constructor. Most plugins will just use this
RegisterMenus()
Customize submenus.
Output()
This function is called when user output is requested. This function is responsible for content....
isShortnameBlocked($rfId, $shortname, $text)
check if shortname or license text of this license is existing
Inputfm()
Build the input form.
Updatedb()
Update the database.
getUpdatefmData($rf_pk)
Update forms.
isTrue($value)
Check if a variable is true.
getLicenseListData($namestr, $filter)
Build the input form.
Adddb()
Add a new license_ref to the database.
isShortNameExists($rfId, $shortname)
check if shortname is changed
DB2KeyValArray($Table, $KeyCol, $ValCol, $Where="")
Create an associative array by using table rows to source the key/value pairs.
Traceback_uri()
Get the URI without query to this location.
Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal="", $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
Build a single choice select pulldown.
char * trim(char *ptext)
Trimming whitespace.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
fo_dbManager * dbManager
fo_dbManager object