FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-obligation-file.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2014 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015-2017 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
12 
13 define("TITLE_ADMIN_OBLIGATION_FILE", _("Obligations and Risks Administration"));
14 
16 {
18  private $dbManager;
19 
21  private $obligationMap;
22 
23  function __construct()
24  {
25  $this->Name = "admin_obligation";
26  $this->Title = TITLE_ADMIN_OBLIGATION_FILE;
27  $this->MenuList = "Admin::Obligation Admin";
28  $this->DBaccess = PLUGIN_DB_ADMIN;
29  parent::__construct();
30 
31  $this->dbManager = $GLOBALS['container']->get('db.manager');
32  $this->obligationMap = $GLOBALS['container']->get('businessrules.obligationmap');
33  }
34 
36  private function ObligationTopics()
37  {
38  $topicarray = DB2ValArray("obligation_ref", "ob_topic", true, " order by ob_topic");
39  return ($topicarray);
40  }
41 
43  private function isObligationTopicAndTextBlocked($obId,$topic,$text)
44  {
45  $sql = "SELECT count(*) from obligation_ref where ob_pk <> $1 and (ob_topic <> '' and ob_topic = $2) and (ob_text <> '' and ob_text = $3)";
46  $check_count = $this->dbManager->getSingleRow($sql,array($obId,$topic,$text));
47  return (0 < $check_count['count']);
48  }
49 
53  function RegisterMenus()
54  {
55  if ($this->State != PLUGIN_STATE_READY) {
56  return(0);
57  }
58 
59  $URL = $this->Name."&add=y";
60  $text = _("Add new obligation");
61  menu_insert("Main::".$this->MenuList."::Add Obligation",0, $URL, $text);
62  $URL = $this->Name;
63  $text = _("Select obligation");
64  menu_insert("Main::".$this->MenuList."::Select Obligation",0, $URL, $text);
65  }
66 
67  public function Output()
68  {
69  $V = ""; // menu_to_1html(menu_find($this->Name, $MenuDepth),0);
70  $errorstr = "Obligation or risk not added";
71 
72  // Delete db record
73  if (@$_POST["del"]) {
74  if (@$_POST["del"] == 'y') {
75  $V .= $this->Deldb();
76  } else {
77  $V .= "<p>Obligation has not been deleted.</p>";
78  }
79  $V .= $this->Inputfm();
80  return $V;
81  }
82 
83  // update the db
84  if (@$_POST["updateit"]) {
85  $resultstr = $this->Updatedb($_POST);
86  $V .= $resultstr;
87  if (strstr($resultstr, $errorstr)) {
88  $V .= $this->Updatefm(0);
89  } else {
90  $V .= $this->Inputfm();
91  }
92  return $V;
93  }
94 
95  if (@$_REQUEST['add'] == 'y') {
96  $V .= $this->Updatefm(0);
97  return $V;
98  }
99 
100  // Add new rec to db
101  if (@$_POST["addit"]) {
102  $resultstr = $this->Adddb($_POST);
103  $V .= $resultstr;
104  if (strstr($resultstr, $errorstr)) {
105  $V .= $this->Updatefm(0);
106  } else {
107  $V .= $this->Inputfm();
108  }
109  return $V;
110  }
111 
112  // bring up the update form
113  $ob_pk = @$_REQUEST['ob_pk'];
114  if ($ob_pk) {
115  $V .= $this->Updatefm($ob_pk);
116  return $V;
117  }
118 
119  $V .= $this->Inputfm();
120  if (@$_POST['req_topic']) {
121  $V .= $this->ObligationTopic($_POST['req_topic']);
122  }
123  return $V;
124  }
125 
131  function Inputfm()
132  {
133  $V = "<FORM name='Inputfm' action='?mod=" . $this->Name . "' method='POST'>";
134  $V.= _("From which topic do you wish to view the obligations and risks:<br>");
135 
136  // qualify by license name
137  // all are optional
138  $V.= "<p>";
139  $V.= _("From topic: ");
140  $Topicarray = $this->ObligationTopics();
141  $Topicarray = array("All"=>"All") + $Topicarray;
142  $Selected = @$_REQUEST['req_topic'];
143  $Pulldown = Array2SingleSelect($Topicarray, "req_topic", $Selected, false, false, "", false);
144  $V.= $Pulldown;
145  $V.= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
146  $text = _("Find");
147  $V.= "<INPUT type='submit' value='$text'>\n";
148  $V .= "</FORM>\n";
149  $V.= "<hr>";
150 
151  return $V;
152  }
153 
154 
162  function ObligationTopic($topic)
163  {
164  global $PG_CONN;
165 
166  $ob = ""; // output buffer
167 
168  // look at all
169  if ($topic == "All") {
170  $where = "";
171  } else {
172  $where = "WHERE ob_topic='". pg_escape_string($topic) ."' ";
173  }
174 
175  $sql = "SELECT * FROM ONLY obligation_ref $where ORDER BY ob_pk";
176  $result = pg_query($PG_CONN, $sql);
177  DBCheckResult($result, $sql, __FILE__, __LINE__);
178 
179  // print simple message if we have no results
180  if (pg_num_rows($result) == 0) {
181  $topic = addslashes($topic);
182  $text1 = _("No obligation matching the topic");
183  $text2 = _("were found");
184  $ob .= "<br>$text1 '$topic' $text2.<br>";
185  pg_free_result($result);
186  return $ob;
187  }
188 
189  $plural = (pg_num_rows($result) == 1) ? "" : "s";
190  $ob .= pg_num_rows($result) . " obligation$plural found.";
191 
192  $ob .= "<table style='border: thin dotted gray'>";
193  $ob .= "<table rules='rows' cellpadding='3'>";
194  $ob .= "<tr>";
195  $text = _("Edit");
196  $ob .= "<th>$text</th>";
197  $text = _("Type");
198  $ob .= "<th>$text</th>";
199  $text = _("Topic");
200  $ob .= "<th>$text</th>";
201  $text = _("Text");
202  $ob .= "<th>$text</th>";
203  $text = _("Classification");
204  $ob .= "<th>$text</th>";
205  $text = _("Apply on modified code");
206  $ob .= "<th>$text</th>";
207  $text = _("Comment");
208  $ob .= "<th>$text</th>";
209  $text = _("Associated licenses");
210  $ob .= "<th>$text</th>";
211  $text = _("Associated candidate licenses");
212  $ob .= "<th>$text</th>";
213  $ob .= "</tr>";
214  $lineno = 0;
215  while ($row = pg_fetch_assoc($result)) {
216  if ($lineno ++ % 2) {
217  $style = "style='background-color:lavender'";
218  } else {
219  $style = "";
220  }
221  $ob .= "<tr $style>";
222 
223  $associatedLicenses = $this->obligationMap->getLicenseList($row['ob_pk']);
224  $candidateLicenses = $this->obligationMap->getLicenseList($row['ob_pk'],True);
225 
226  // Edit button brings up full screen edit of all license_ref fields
227  $ob .= "<td align=center><a href='";
228  $ob .= Traceback_uri();
229  $ob .= "?mod=" . $this->Name .
230  "&ob_pk=$row[ob_pk]' >".
231  "<img border=0 src='" . Traceback_uri() . "images/button_edit.png'></a></td>";
232 
233  $ob .= "<td align=left>$row[ob_type]</td>";
234  $ob .= "<td align=left>" . htmlspecialchars($row["ob_topic"]) . "</td>";
235  $vetext = htmlspecialchars($row['ob_text']);
236  $ob .= "<td><textarea readonly=readonly rows=3 cols=40>$vetext</textarea></td> ";
237  $ob .= "<td align=left>$row[ob_classification]</td>";
238  $ob .= "<td align=center>$row[ob_modifications]</td>";
239  $vetext = htmlspecialchars($row['ob_comment']);
240  $ob .= "<td><textarea readonly=readonly rows=3 cols=40>$vetext</textarea></td> ";
241  $ob .= "<td align=center>$associatedLicenses</td>";
242  $ob .= "<td align=center>$candidateLicenses</td>";
243  $ob .= "</tr>";
244  }
245  pg_free_result($result);
246  $ob .= "</table>";
247  return $ob;
248  }
249 
255  function Updatefm($ob_pk)
256  {
257  $vars = array();
258 
259  $ob_pk_update = "";
260 
261  if (0 < count($_POST)) {
262  $ob_pk_update = $_POST['ob_pk'];
263  if (! empty($ob_pk)) {
264  $ob_pk_update = $ob_pk;
265  } else if (empty($ob_pk_update)) {
266  $ob_pk_update = $_GET['ob_pk'];
267  }
268  }
269  $vars['actionUri'] = "?mod=" . $this->Name . "&ob_pk=$ob_pk_update";
270 
271  if ($ob_pk) { // true if this is an update
272  $row = $this->dbManager->getSingleRow(
273  "SELECT * FROM ONLY obligation_ref WHERE ob_pk=$1", array(
274  $ob_pk
275  ), __METHOD__ . '.forUpdate');
276  if ($row === false) {
277  $text = _("No obligation matching this key");
278  $text1 = _("was found");
279  return "$text ($ob_pk) $text1.";
280  }
281 
282  $associatedLicenses = $this->obligationMap->getLicenseList($ob_pk);
283  $vars['licnames'] = explode(";", $associatedLicenses);
284  $candidateLicenses = $this->obligationMap->getLicenseList($ob_pk, True);
285  $vars['candidatenames'] = explode(";", $candidateLicenses);
286  } else {
287  $row = array('ob_active' => 't',
288  'ob_modifications' => 'No',
289  'ob_text_updatable' => 't'
290  );
291  }
292 
293  foreach (array_keys($row) as $key) {
294  if (array_key_exists($key, $_POST)) {
295  $row[$key] = $_POST[$key];
296  }
297  }
298 
299  $vars['boolYesNoMap'] = array("true"=>"Yes", "false"=>"No");
300  $vars['YesNoMap'] = array("Yes"=>"Yes", "No"=>"No");
301  $row['ob_active'] = $this->dbManager->booleanFromDb($row['ob_active'])?'true':'false';
302  $row['ob_text_updatable'] = $this->dbManager->booleanFromDb($row['ob_text_updatable'])?'true':'false';
303  $vars['isReadOnly'] = !(empty($ob_pk) || $row['ob_text_updatable']=='true');
304 
305  $vars['obId'] = $ob_pk?:$ob_pk_update;
306 
307  // get list of known license shortnames
308  $vars['licenseShortnames'] = $this->obligationMap->getAvailableShortnames();
309  natcasesort($vars['licenseShortnames']);
310 
311  // get list of candidate shortnames
312  $vars['candidateShortnames'] = $this->obligationMap->getAvailableShortnames(true);
313  natcasesort($vars['candidateShortnames']);
314 
315  // build obligation type and classification arrays
319  $vars['obligationClassification'] = array("green"=>"green", "white"=>"white", "yellow"=>"yellow", "red"=>"red");
320  $vars['obligationTypes'] = array("Obligation"=>"Obligation",
321  "Restriction"=>"Restriction", "Risk"=>"Risk", "Right"=>"Right");
322 
323  $vars['ob_type'] = empty($row['ob_type']) ? 'Obligation' : $row['ob_type'];
324  $vars['ob_classification'] = empty($row['ob_classification']) ? 'green' : $row['ob_classification'];
325 
326  // build scripts
327  $vars['licenseSelectorName'] = 'licenseSelector[]';
328  $vars['licenseSelectorId'] = 'licenseSelectorId';
329  $vars['candidateSelectorName'] = 'candidateSelector[]';
330  $vars['candidateSelectorId'] = 'candidateSelectorId';
331  $scripts = "<script src='scripts/tools.js' type='text/javascript'></script>
332  <script src='scripts/select2.full.min.js'></script>
333  <script type='text/javascript'>
334  $('#licenseSelectorId').select2({'placeholder': 'Select licenses associated with this obligation'});
335  </script>
336  <script type='text/javascript'>
337  $('#candidateSelectorId').select2({'placeholder': 'Select candidate licenses associated with this obligation'});
338  </script>
339  <script type='text/javascript'>
340  function confirmDeletion() {
341 
342  var updateform = document.forms['Updatefm'];
343  var delinput = document.createElement('input');
344  delinput.name = 'del';
345 
346  if (confirm('Are you sure?')) {
347  delinput.value = 'y';
348  }
349  else {
350  delinput.value = 'n';
351  }
352  updateform.appendChild(delinput);
353  }
354  </script>";
355 
356  $this->renderScripts($scripts);
357  $allVars = array_merge($vars,$row);
358  return $this->renderString('admin_obligation-upload_form.html.twig', $allVars);
359  }
360 
366  function Updatedb()
367  {
368  $obId = intval($_POST['ob_pk']);
369  $topic = StringOperation::replaceUnicodeControlChar(trim($_POST['ob_topic']));
370  $licnames = $_POST['licenseSelector'];
371  $candidatenames = $_POST['candidateSelector'];
372  $text = StringOperation::replaceUnicodeControlChar(trim($_POST['ob_text']));
373  $comment = StringOperation::replaceUnicodeControlChar(trim($_POST['ob_comment']));
374 
375  if (empty($topic)) {
376  $text = _("ERROR: The obligation topic is empty.");
377  return "<b>$text</b><p>";
378  }
379 
380  if (empty($text)) {
381  $text = _("ERROR: The obligation text is empty.");
382  return "<b>$text</b><p>";
383  }
384 
385  if ($this->isObligationTopicAndTextBlocked($obId, $topic, $text)) {
386  $text = _(
387  "ERROR: The obligation topic and text already exist in the obligation list. Obligation not updated.");
388  return "<b>$text</b><p>";
389  }
390 
391  $sql = "UPDATE obligation_ref SET ob_active=$2, ob_type=$3, ob_modifications=$4, ob_topic=$5, ob_md5=md5($6), ob_text=$6, ob_classification=$7, ob_text_updatable=$8, ob_comment=$9 WHERE ob_pk=$1";
392  $params = array(
393  $obId,
394  $_POST['ob_active'],
395  $_POST['ob_type'],
396  $_POST['ob_modifications'],
397  $topic,
398  $text,
399  $_POST['ob_classification'],
400  $_POST['ob_text_updatable'],
401  $comment);
402  $this->dbManager->prepare($stmt=__METHOD__.".update", $sql);
403  $this->dbManager->freeResult($this->dbManager->execute($stmt,$params));
404 
405  // Add new licenses and new candiate licenses
406  $newAssociatedLicenses = $this->addNewLicenses($licnames,$obId);
407  $newCandidateLicenses = $this->addNewLicenses($candidatenames,$obId,true);
408 
409  // Remove licenses that shouldn't be associated with the obligation any more
410  $unassociatedLicenses = $this->removeLicenses($licnames,$obId);
411  $unassociatedCandidateLicenses = $this->removeLicenses($candidatenames,$obId,true);
412 
413  $ob = "Obligation '$topic' was updated - ";
414  $ob .= $newAssociatedLicenses ? "New licenses: '$newAssociatedLicenses' - " : "";
415  $ob .= $newCandidateLicenses ? "New candidate licenses: '$newCandidateLicenses' - " : "";
416  $ob .= $unassociatedLicenses ? "Removed licenses: '$unassociatedLicenses' - " : "";
417  $ob .= $unassociatedCandidateLicenses ? "Removed candidate licenses: '$unassociatedCandidateLicenses'" : "";
418  $ob .= "</p>";
419  return $ob;
420  }
421 
422 
428  function Adddb()
429  {
430  $topic = StringOperation::replaceUnicodeControlChar(trim($_POST['ob_topic']));
431  $licnames = empty($_POST['licenseSelector']) ? array() : $_POST['licenseSelector'];
432  $candidatenames = empty($_POST['candidateSelector']) ? array() : $_POST['candidateSelector'];
433  $text = StringOperation::replaceUnicodeControlChar(trim($_POST['ob_text']));
434  $comment = StringOperation::replaceUnicodeControlChar(trim($_POST['ob_comment']));
435  $message = "";
436 
437  if (empty($topic)) {
438  $text = _("ERROR: The obligation topic is empty.");
439  return "<b>$text</b><p>";
440  }
441 
442  if (empty($text)) {
443  $text = _("ERROR: The obligation text is empty.");
444  return "<b>$text</b><p>";
445  }
446 
447  if (empty($licnames) && empty($candidatenames)) {
448  $message = _("ERROR: There are no licenses associated with this topic.");
449  return "<b>$message</b><p>";
450  }
451 
452  if ($this->isObligationTopicAndTextBlocked(0, $topic, $text)) {
453  $message = _(
454  "ERROR: The obligation topic and text already exist in the obligation list. Obligation not added.");
455  return "<b>$message</b><p>";
456  }
457 
458  $stmt = __METHOD__.'.ob';
459  $sql = "INSERT into obligation_ref (ob_active, ob_type, ob_modifications, ob_topic, ob_md5, ob_text, ob_classification, ob_text_updatable, ob_comment) VALUES ($1, $2, $3, $4, md5($5), $5, $6, $7, $8) RETURNING ob_pk";
460  $this->dbManager->prepare($stmt,$sql);
461  $res = $this->dbManager->execute($stmt,
462  array($_POST['ob_active'],
463  $_POST['ob_type'],
464  $_POST['ob_modifications'],
465  $topic,
466  $text,
467  $_POST['ob_classification'],
468  $_POST['ob_text_updatable'],
469  $comment));
470  $row = $this->dbManager->fetchArray($res);
471  $obId = $row['ob_pk'];
472 
473  $associatedLicenses = $this->addNewLicenses($licnames, $obId);
474  $candidateLicenses = $this->addNewLicenses($candidatenames, $obId, True);
475 
476  $message .= "Obligation '$topic' associated with: ";
477  $message .= $associatedLicenses ? "licenses '$associatedLicenses' " : "";
478  $message .= ($associatedLicenses && $candidateLicenses) ? "and " : "";
479  $message .= $candidateLicenses ? "candidates licenses '$candidateLicenses' " : "";
480  $message .= "(id=$obId) was added.<p>";
481  return $message;
482  }
483 
490  function Deldb()
491  {
492  $this->obligationMap->deleteObligation($_POST['ob_pk']);
493 
494  return "<p>Obligation has been deleted.</p>";
495  }
496 
505  function addNewLicenses($shortnames,$obId,$candidate=false)
506  {
507  if (!empty($shortnames)) {
508  $licList = "";
509  foreach ($shortnames as $license) {
510  $licIds = $this->obligationMap->getIdFromShortname($license,$candidate);
511  $newLic = $this->obligationMap->associateLicenseFromLicenseList($obId,
512  $licIds, $candidate);
513  if ($newLic) {
514  if ($licList == "") {
515  $licList = "$license";
516  } else {
517  $licList .= ";$license";
518  }
519  }
520  }
521  return $licList;
522  }
523 
524  return "";
525  }
526 
535  function removeLicenses($shortnames,$obId,$candidate=false)
536  {
537  $unassociatedLicenses = "";
538  $licenses = $this->obligationMap->getLicenseList($obId, $candidate);
539  $current = explode(";", $licenses);
540  if (! empty($shortnames)) {
541  $obsoleteLicenses = array_diff($current, $shortnames);
542  } else {
543  $obsoleteLicenses = $current;
544  }
545 
546  if ($obsoleteLicenses) {
547  foreach ($obsoleteLicenses as $toBeRemoved) {
548  $licIds = $this->obligationMap->getIdFromShortname($toBeRemoved,
549  $candidate);
550  $this->obligationMap->unassociateLicenseFromLicenseList($obId, $licIds,
551  $candidate);
552  if ($unassociatedLicenses == "") {
553  $unassociatedLicenses = "$toBeRemoved";
554  } else {
555  $unassociatedLicenses .= ";$toBeRemoved";
556  }
557  }
558  }
559 
560  return $unassociatedLicenses;
561  }
562 }
563 
564 $NewPlugin = new admin_obligation_file();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
renderString($templateName, $vars=null)
Definition: FO_Plugin.php:414
renderScripts($scripts)
Render JavaScript in the template's footer.
Definition: FO_Plugin.php:424
Wrapper class for obligation map.
Definition: state.hpp:16
Output()
This function is called when user output is requested. This function is responsible for content....
ObligationTopic($topic)
Build the input form.
isObligationTopicAndTextBlocked($obId, $topic, $text)
check if the text of this obligation is existing
addNewLicenses($shortnames, $obId, $candidate=false)
Associate selected licenses to the obligation.
Updatefm($ob_pk)
Update forms.
Inputfm()
Build the input form.
Deldb()
Remove obligation_ref from the database and unassociate licenses.
Adddb()
Add a new obligation_ref to the database.
ObligationTopics()
return an array of all obligation topics from the DB
Updatedb()
Update the database.
removeLicenses($shortnames, $obId, $candidate=false)
Unassociate selected licenses to the obligation.
__construct()
base constructor. Most plugins will just use this
RegisterMenus()
Customize submenus.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
DB2ValArray($Table, $ValCol, $Uniq=false, $Where="")
Create an array by using table rows to source the values.
Definition: common-db.php:153
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
Array2SingleSelect($KeyValArray, $SLName="unnamed", $SelectedVal="", $FirstEmpty=false, $SelElt=true, $Options="", $ReturnKey=true)
Build a single choice select pulldown.
Definition: common-ui.php:32
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16