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