FOSSology  4.5.1
Open Source License Compliance by Open Source Software
HistogramBase.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2017 Siemens AG
4  Author: J.Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
11 
17 abstract class HistogramBase extends FO_Plugin
18 {
22  protected $agentName;
26  protected $viewName;
30  private $uploadtree_tablename;
34  private $uploadDao;
35 
36  function __construct()
37  {
38  $this->DBaccess = PLUGIN_DB_READ;
39  $this->LoginFlag = 0;
40 
41  parent::__construct();
42 
43  global $container;
44  $this->uploadDao = $container->get('dao.upload');
45  $this->renderer = $container->get('twig.environment');
46 
47  $this->vars['name']=$this->Name;
48  $this->additionalAgentName = "scancode";
49  }
50 
61  public function getTableForSingleType($type, $description, $uploadId, $uploadTreeId, $filter, $agentId)
62  {
63  $sorting = json_encode($this->returnSortOrder());
64 
65  $out = array("type" => $type, "sorting" => $sorting, "uploadId" => $uploadId,
66  "uploadTreeId" => $uploadTreeId, "agentId" => $agentId, "filter" => $filter, "description" => $description);
67  $typeDescriptor = "";
68  if ($type !== "statement" || $type !== 'scancode_statement') {
69  $typeDescriptor = $description;
70  }
71  $output = "<h4>Activated $typeDescriptor statements:</h4>
72 <div>
73  <div class='btn btn-default' style='float:right; padding:5px; margin:5px;'>
74  <input type='checkbox' style='padding:2px;' id='inverseSearchActivated".$type."' name='inverseSearch'/>
75  <label class='control-label' for='inverseSearchActivated".$type."'>Inverse Search</label>
76  </div>
77 <table border=1 width='100%' id='copyright".$type."' class='wordbreaktable'></table></div>
78 <br/><br/>
79 <div>
80  <table border=0 width='100%' id='searchReplaceTable".$type."'>
81  <tr>
82  <td style='width:80%'>
83  <div class='form-group'>
84  <label class='control-label col-sm-2'>Advance search:<img src='images/info_16.png' title='Use \"(*)\" to match any thing.\nExample: \"Copyright (*) All rights reserved(*)\" will match \"Copyright 2012-2020 Company ABC. All rights reserved {and some garbage here}\"' alt='' class='info-bullet'></label>
85  <div class='col-sm-10'>
86  <input id='advSearchText".$type."' type='text' class='form-control advSearch'>
87  </div>
88  </div>
89  </td>
90  <td rowspan='2'>
91  <div class='form-group'>
92  <div class='col-sm-offset-2 col-sm-10'>
93  <button class='btn btn-default' style='margin-top:25px;padding:15%;' onClick='createReplacementText(\"".$type."\")' title='Create a replacement text with all placeholders.'>Create replacement text</button>
94  </div>
95  </div>
96  </td>
97  </tr>
98  <tr>
99  <td>
100  <div class='form-group'>
101  <label class='control-label col-sm-2'>Replace:<img src='images/info_16.png' title='Use \"$1 $2\" as placeholder for corresponding (*) values.\nExample: \"Copyright $1 All rights reserved\" will result in \"Copyright 2012-2020 Company ABC. All rights reserved\" from example above' alt='' class='info-bullet'></label>
102  <div class='col-sm-10'>
103  <input id='replaceText".$type."' type='text' class='form-control'>
104  </div>
105  </div>
106  </td>
107  </tr></table>
108  <div class='form-group'>
109  <div class='col-sm-offset-2 col-sm-10'>
110  <button class='btn btn-default' id='testReplacement".$type."'>Test replacement</button>
111  <button class='btn btn-default' id='replaceSelected".$type."'>Replace selected rows</button>
112  <button class='btn btn-default' id='deleteSelected".$type."'>Deactivate selected rows</button>
113  </div>
114  </div>
115  <br /><br />
116  <table border=1 id='testVal".$type."' style='display:none' class='dataTable wordbreaktable'>
117  <tr><th style='width:50%'>From</th><th style='width:50%'>To</th></tr>
118  <tr><td id='testVal".$type."From'></td><td id='testVal".$type."To'></td></tr>
119  </table>
120  <br/><br/>
121  <h4>Deactivated $typeDescriptor statements:</h4>
122  <div class='btn btn-default' style='float:right; padding:5px; margin:5px;'>
123  <input type='checkbox' id='inverseSearchDeactivated".$type."' name='inverseSearch'/>
124  <label class='control-label' for='inverseSearchDeactivated".$type."'>Inverse Search</label>
125  </div>
126 </div>
127 <div><table border=1 width='100%' id='copyright".$type."deactivated' class='wordbreaktable'></table>
128  <br/><br/>
129  <a id='undoSelected".$type."' class='buttonLink'>Undo selected rows</a>
130  <br /><br />
131 </div>";
132 
133  return array($output, $out);
134  }
135 
136 
146  abstract protected function fillTables($upload_pk, $Uploadtree_pk, $filter, $agentId, $VF);
147 
160  protected function ShowUploadHist($upload_pk, $Uploadtree_pk, $Uri, $filter, $uploadtree_tablename, $Agent_pk)
161  {
162  $Agent_pks = implode("," , $Agent_pk);
163  list($ChildCount, $VF) = $this->getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pks, $upload_pk);
164  $this->vars['childcount'] = $ChildCount;
165  $this->vars['fileListing'] = $VF;
166 
167  /***************************************
168  @todo
169  Problem: $ChildCount can be zero!
170  This happens if you have a container that does not
171  unpack to a directory. For example:
172  file.gz extracts to archive.txt that contains a license.
173  Same problem seen with .pdf and .Z files.
174  Solution: if $ChildCount == 0, then just view the license!
175  $ChildCount can also be zero if the directory is empty.
176  ***************************************/
177  if ($ChildCount == 0) {
178  $isADirectory = $this->isADirectory($Uploadtree_pk);
179  if ($isADirectory) {
180  return;
181  }
182  $ModLicView = plugin_find($this->viewName);
183  return $ModLicView->execute();
184  }
185  return $this->fillTables($upload_pk, $Uploadtree_pk, $filter, $Agent_pks, $VF);
186  }
187 
192  function OutputOpen()
193  {
194  if ($this->State != PLUGIN_STATE_READY) {
195  return 0;
196  }
197  return parent::OutputOpen();
198  }
199 
204  public function Output()
205  {
206  $OutBuf="";
207  $uploadId = GetParm("upload",PARM_INTEGER);
208  $item = GetParm("item",PARM_INTEGER);
209  $filter = GetParm("filter",PARM_STRING);
210 
211  /* check upload permissions */
212  if (!$this->uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
213  $text = _("Permission Denied");
214  return "<h2>$text</h2>";
215  }
216 
217  /* Get uploadtree_tablename */
218  $uploadtree_tablename = GetUploadtreeTableName($uploadId);
219  $this->uploadtree_tablename = $uploadtree_tablename;
220 
221  /************************/
222  /* Show the folder path */
223  /************************/
224 
225  $this->vars['dir2browse'] = Dir2Browse($this->Name,$item,NULL,1,"Browse",-1,'','',$uploadtree_tablename);
226  if (empty($uploadId)) {
227  return 'no item selected';
228  }
229 
230  /* advanced interface allowing user to select dataset (agent version) */
231  $dataset = $this->agentName."_dataset";
232  $arsCopyrighttable = $this->agentName."_ars";
233  $additionalArsCopyrighttable = $this->additionalAgentName."_ars";
234  /* get proper agent_id */
235 
236  $agentId[] = LatestAgentpk($uploadId, $arsCopyrighttable);
237  $additionalAgentId[] = LatestAgentpk($uploadId, $additionalArsCopyrighttable);
238  if ($this->agentName == "copyright") {
239  $arsResotable = "reso_ars";
240  // $agentId[] = LatestAgentpk($uploadId, $arsResotable);
241  if (LatestAgentpk($uploadId, $arsResotable) != 0) {
242  $agentId[] = LatestAgentpk($uploadId, $arsResotable);
243  }
244  }
245 
246  if ((empty($agentId) || $agentId[0] == 0) && (empty($additionalAgentId) || $additionalAgentId[0] == 0)) {
247  /* schedule copyright */
248  $OutBuf .= ActiveHTTPscript("Schedule");
249  $OutBuf .= "<script language='javascript'>\n";
250  $OutBuf .= "function Schedule_Reply()\n";
251  $OutBuf .= " {\n";
252  $OutBuf .= " if ((Schedule.readyState==4) && (Schedule.status==200 || Schedule.status==400))\n";
253  $OutBuf .= " document.getElementById('msgdiv').innerHTML = Schedule.responseText;\n";
254  $OutBuf .= " }\n";
255  $OutBuf .= "</script>\n";
256 
257  $OutBuf .= "<form name='formy' method='post'>\n";
258  $OutBuf .= "<div id='msgdiv'>\n";
259  $OutBuf .= _("No data available.");
260  $OutBuf .= "<input type='button' class='btn btn-default btn-sm' name='scheduleAgent' value='Schedule Agent'";
261  $OutBuf .= "onClick=\"Schedule_Get('" . Traceback_uri() . "?mod=schedule_agent&upload=$uploadId&agent=agent_{$this->agentName}')\">\n";
262  $OutBuf .= "</input>";
263  $OutBuf .= "</div> \n";
264  $OutBuf .= "</form>\n";
265 
266  $this->vars['pageContent'] = $OutBuf;
267  return;
268  }
269 
270  $AgentSelect = AgentSelect($this->agentName, $uploadId, $dataset, $agentId, "onchange=\"addArsGo('newds', $dataset);\"");
271 
272  /* change the copyright result when selecting one version of copyright */
273  if (!empty($AgentSelect)) {
274  $action = Traceback_uri() . '?mod=' . GetParm('mod',PARM_RAW) . Traceback_parm_keep(array('upload','item'));
275 
276  $OutBuf .= "<script type='text/javascript'>
277  function addArsGo(formid, selectid)
278  {
279  var selectobj = document.getElementById(selectid);
280  var Agent_pk = selectobj.options[selectobj.selectedIndex].value;
281  document.getElementById(formid).action='$action'+'&agent='+Agent_pk;
282  document.getElementById(formid).submit();
283  return;
284  }
285  </script>";
286 
287  $OutBuf .= "<form action=\"$action\" id=\"newds\" method=\"POST\">$AgentSelect</form>";
288  }
289 
290  $selectKey = $filter == 'nolic' ? 'nolic' : 'all';
291  $OutBuf .= "<select name='view_filter' class='form-control-sm' id='view_filter' onchange='ChangeFilter(this,$uploadId, $item);'>";
292  foreach (array('all'=>_("Show all"), 'nolic'=> _("Show files without licenses")) as $key=>$text) {
293  $selected = ($selectKey == $key) ? "selected" : "";
294  $OutBuf .= "<option $selected value=\"$key\">$text</option>";
295  }
296  $OutBuf .= "</select>";
297 
298  $uri = preg_replace("/&item=([0-9]*)/", "", Traceback());
299  list($tables, $tableVars) = $this->ShowUploadHist($uploadId, $item, $uri, $selectKey, $uploadtree_tablename, $agentId);
300  $this->vars['tables'] = $tableVars;
301  $this->vars['pageContent'] = $OutBuf . $tables;
302  $this->vars['scriptBlock'] = $this->createScriptBlock();
303 
304  return;
305  }
306 
315  protected function getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pk, $upload_pk)
316  {
317  $VF=""; // return values for file listing
318  /******* File Listing ************/
319  /* Get ALL the items under this Uploadtree_pk */
320  $Children = GetNonArtifactChildren($Uploadtree_pk, $uploadtree_tablename);
321  $ChildCount = 0;
322  $ChildLicCount = 0;
323  $ChildDirCount = 0; /* total number of directory or containers */
324  foreach ($Children as $c) {
325  if (Iscontainer($c['ufile_mode'])) {
326  $ChildDirCount++;
327  }
328  }
329 
330  $VF .= "<table border=0>";
331  foreach ($Children as $child) {
332  if (empty($child)) {
333  continue;
334  }
335  $ChildCount++;
336 
337  global $Plugins;
338  $ModLicView = &$Plugins[plugin_find_id($this->viewName)];
339  /* Determine the hyperlink for non-containers to view-license */
340  if (!empty($child['pfile_fk']) && !empty($ModLicView)) {
341  $LinkUri = Traceback_uri();
342  $LinkUri .= "?mod=".$this->viewName."&agent=$Agent_pk&upload=$upload_pk&item=$child[uploadtree_pk]";
343  } else {
344  $LinkUri = NULL;
345  }
346 
347  /* Determine link for containers */
348  if (Iscontainer($child['ufile_mode'])) {
349  $uploadtree_pk = DirGetNonArtifact($child['uploadtree_pk'], $uploadtree_tablename);
350  $LicUri = "$Uri&item=" . $uploadtree_pk;
351  } else {
352  $LicUri = NULL;
353  }
354 
355  /* Populate the output ($VF) - file list */
356  /* id of each element is its uploadtree_pk */
357  $LicCount = 0;
358 
359  $cellContent = Isdir($child['ufile_mode']) ? $child['ufile_name'].'/' : $child['ufile_name'];
360  if (Iscontainer($child['ufile_mode'])) {
361  $cellContent = "<a class='btn btn-outline-secondary btn-sm' href='$LicUri'><b>$cellContent</b></a>";
362  } elseif (!empty($LinkUri)) { // && ($LicCount > 0))
363  $cellContent = "<a class='btn btn-outline-secondary btn-sm' href='$LinkUri'>$cellContent</a>";
364  }
365  $VF .= "<tr><td id='$child[uploadtree_pk]' align='left'>$cellContent</td><td>";
366 
367  if ($LicCount) {
368  $VF .= "[" . number_format($LicCount, 0, "", ",") . "&nbsp;";
369  $VF .= "license" . ($LicCount == 1 ? "" : "s");
370  $VF .= "</a>";
371  $VF .= "]";
372  $ChildLicCount += $LicCount;
373  }
374  $VF .= "</td></tr>\n";
375  }
376  $VF .= "</table>\n";
377  return array($ChildCount, $VF);
378  }
379 
385  protected function isADirectory($Uploadtree_pk)
386  {
387  $row = $this->uploadDao->getUploadEntry($Uploadtree_pk, $this->uploadtree_tablename);
388  $isADirectory = IsDir($row['ufile_mode']);
389  return $isADirectory;
390  }
391 
396  public static function returnSortOrder ()
397  {
398  $defaultOrder = array (
399  array(0, "desc"),
400  array(1, "desc"),
401  );
402  return $defaultOrder;
403  }
404 
409  public function getTemplateName()
410  {
411  return "copyrighthist.html.twig";
412  }
413 
418  abstract protected function createScriptBlock();
419 }
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Base class for histogram plugins.
ShowUploadHist($upload_pk, $Uploadtree_pk, $Uri, $filter, $uploadtree_tablename, $Agent_pk)
Given an $Uploadtree_pk, display: (1) The histogram for the directory BY LICENSE....
getTableForSingleType($type, $description, $uploadId, $uploadTreeId, $filter, $agentId)
getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pk, $upload_pk)
Output()
This function is called when user output is requested. This function is responsible for content....
static returnSortOrder()
Get sorting orders.
__construct()
base constructor. Most plugins will just use this
fillTables($upload_pk, $Uploadtree_pk, $filter, $agentId, $VF)
Get copyright statements and fill the main content table.
createScriptBlock()
Create JavaScript block for histogram.
isADirectory($Uploadtree_pk)
Check if passed element is a directory.
OutputOpen()
This function is called when user output is requested. This function is responsible for assigning hea...
Definition: state.hpp:16
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
AgentSelect($TableName, $upload_pk, $SLName, &$agent_pk, $extra="")
LatestAgentpk($upload_pk, $arsTableName, $arsSuccess=false)
Given an upload_pk, find the latest enabled agent_pk with results.
Isdir($mode)
Definition: common-dir.php:20
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk).
Definition: common-dir.php:158
Iscontainer($mode)
Definition: common-dir.php:38
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
Traceback()
Get the URI + query to this location.
Definition: common-parm.php:89
const PARM_RAW
Definition: common-parm.php:22
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:319