FOSSology  4.4.0
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  }
49 
60  public function getTableForSingleType($type, $description, $uploadId, $uploadTreeId, $filter, $agentId)
61  {
62  $sorting = json_encode($this->returnSortOrder());
63 
64  $out = array("type" => $type, "sorting" => $sorting, "uploadId" => $uploadId,
65  "uploadTreeId" => $uploadTreeId, "agentId" => $agentId, "filter" => $filter, "description" => $description);
66  $typeDescriptor = "";
67  if ($type !== "statement" || $type !== 'scancode_statement') {
68  $typeDescriptor = $description;
69  }
70  $output = "<h4>Activated $typeDescriptor statements:</h4>
71 <div>
72  <div class='btn btn-default' style='float:right; padding:5px; margin:5px;'>
73  <input type='checkbox' style='padding:2px;' id='inverseSearchActivated".$type."' name='inverseSearch'/>
74  <label class='control-label' for='inverseSearchActivated".$type."'>Inverse Search</label>
75  </div>
76 <table border=1 width='100%' id='copyright".$type."' class='wordbreaktable'></table></div>
77 <br/><br/>
78 <div>
79  <table border=0 width='100%' id='searchReplaceTable".$type."'>
80  <tr>
81  <td style='width:80%'>
82  <div class='form-group'>
83  <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>
84  <div class='col-sm-10'>
85  <input id='advSearchText".$type."' type='text' class='form-control advSearch'>
86  </div>
87  </div>
88  </td>
89  <td rowspan='2'>
90  <div class='form-group'>
91  <div class='col-sm-offset-2 col-sm-10'>
92  <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>
93  </div>
94  </div>
95  </td>
96  </tr>
97  <tr>
98  <td>
99  <div class='form-group'>
100  <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>
101  <div class='col-sm-10'>
102  <input id='replaceText".$type."' type='text' class='form-control'>
103  </div>
104  </div>
105  </td>
106  </tr></table>
107  <div class='form-group'>
108  <div class='col-sm-offset-2 col-sm-10'>
109  <button class='btn btn-default' id='testReplacement".$type."'>Test replacement</button>
110  <button class='btn btn-default' id='replaceSelected".$type."'>Replace selected rows</button>
111  <button class='btn btn-default' id='deleteSelected".$type."'>Deactivate selected rows</button>
112  </div>
113  </div>
114  <br /><br />
115  <table border=1 id='testVal".$type."' style='display:none' class='dataTable wordbreaktable'>
116  <tr><th style='width:50%'>From</th><th style='width:50%'>To</th></tr>
117  <tr><td id='testVal".$type."From'></td><td id='testVal".$type."To'></td></tr>
118  </table>
119  <br/><br/>
120  <h4>Deactivated $typeDescriptor statements:</h4>
121  <div class='btn btn-default' style='float:right; padding:5px; margin:5px;'>
122  <input type='checkbox' id='inverseSearchDeactivated".$type."' name='inverseSearch'/>
123  <label class='control-label' for='inverseSearchDeactivated".$type."'>Inverse Search</label>
124  </div>
125 </div>
126 <div><table border=1 width='100%' id='copyright".$type."deactivated' class='wordbreaktable'></table>
127  <br/><br/>
128  <a id='undoSelected".$type."' class='buttonLink'>Undo selected rows</a>
129  <br /><br />
130 </div>";
131 
132  return array($output, $out);
133  }
134 
135 
145  abstract protected function fillTables($upload_pk, $Uploadtree_pk, $filter, $agentId, $VF);
146 
159  protected function ShowUploadHist($upload_pk, $Uploadtree_pk, $Uri, $filter, $uploadtree_tablename, $Agent_pk)
160  {
161  $Agent_pks = implode("," , $Agent_pk);
162  list($ChildCount, $VF) = $this->getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pks, $upload_pk);
163  $this->vars['childcount'] = $ChildCount;
164  $this->vars['fileListing'] = $VF;
165 
166  /***************************************
167  @todo
168  Problem: $ChildCount can be zero!
169  This happens if you have a container that does not
170  unpack to a directory. For example:
171  file.gz extracts to archive.txt that contains a license.
172  Same problem seen with .pdf and .Z files.
173  Solution: if $ChildCount == 0, then just view the license!
174  $ChildCount can also be zero if the directory is empty.
175  ***************************************/
176  if ($ChildCount == 0) {
177  $isADirectory = $this->isADirectory($Uploadtree_pk);
178  if ($isADirectory) {
179  return;
180  }
181  $ModLicView = plugin_find($this->viewName);
182  return $ModLicView->execute();
183  }
184  return $this->fillTables($upload_pk, $Uploadtree_pk, $filter, $Agent_pks, $VF);
185  }
186 
191  function OutputOpen()
192  {
193  if ($this->State != PLUGIN_STATE_READY) {
194  return 0;
195  }
196  return parent::OutputOpen();
197  }
198 
203  public function Output()
204  {
205  $OutBuf="";
206  $uploadId = GetParm("upload",PARM_INTEGER);
207  $item = GetParm("item",PARM_INTEGER);
208  $filter = GetParm("filter",PARM_STRING);
209 
210  /* check upload permissions */
211  if (!$this->uploadDao->isAccessible($uploadId, Auth::getGroupId())) {
212  $text = _("Permission Denied");
213  return "<h2>$text</h2>";
214  }
215 
216  /* Get uploadtree_tablename */
217  $uploadtree_tablename = GetUploadtreeTableName($uploadId);
218  $this->uploadtree_tablename = $uploadtree_tablename;
219 
220  /************************/
221  /* Show the folder path */
222  /************************/
223 
224  $this->vars['dir2browse'] = Dir2Browse($this->Name,$item,NULL,1,"Browse",-1,'','',$uploadtree_tablename);
225  if (empty($uploadId)) {
226  return 'no item selected';
227  }
228 
229  /* advanced interface allowing user to select dataset (agent version) */
230  $dataset = $this->agentName."_dataset";
231  $arsCopyrighttable = $this->agentName."_ars";
232  /* get proper agent_id */
233 
234  $agentId[] = LatestAgentpk($uploadId, $arsCopyrighttable);
235  if ($this->agentName == "copyright") {
236  $arsResotable = "reso_ars";
237  // $agentId[] = LatestAgentpk($uploadId, $arsResotable);
238  if (LatestAgentpk($uploadId, $arsResotable) != 0) {
239  $agentId[] = LatestAgentpk($uploadId, $arsResotable);
240  }
241  }
242 
243  if (empty($agentId) || $agentId[0] == 0) {
244  /* schedule copyright */
245  $OutBuf .= ActiveHTTPscript("Schedule");
246  $OutBuf .= "<script language='javascript'>\n";
247  $OutBuf .= "function Schedule_Reply()\n";
248  $OutBuf .= " {\n";
249  $OutBuf .= " if ((Schedule.readyState==4) && (Schedule.status==200 || Schedule.status==400))\n";
250  $OutBuf .= " document.getElementById('msgdiv').innerHTML = Schedule.responseText;\n";
251  $OutBuf .= " }\n";
252  $OutBuf .= "</script>\n";
253 
254  $OutBuf .= "<form name='formy' method='post'>\n";
255  $OutBuf .= "<div id='msgdiv'>\n";
256  $OutBuf .= _("No data available.");
257  $OutBuf .= "<input type='button' class='btn btn-default btn-sm' name='scheduleAgent' value='Schedule Agent'";
258  $OutBuf .= "onClick=\"Schedule_Get('" . Traceback_uri() . "?mod=schedule_agent&upload=$uploadId&agent=agent_{$this->agentName}')\">\n";
259  $OutBuf .= "</input>";
260  $OutBuf .= "</div> \n";
261  $OutBuf .= "</form>\n";
262 
263  $this->vars['pageContent'] = $OutBuf;
264  return;
265  }
266 
267  $AgentSelect = AgentSelect($this->agentName, $uploadId, $dataset, $agentId, "onchange=\"addArsGo('newds', $dataset);\"");
268 
269  /* change the copyright result when selecting one version of copyright */
270  if (!empty($AgentSelect)) {
271  $action = Traceback_uri() . '?mod=' . GetParm('mod',PARM_RAW) . Traceback_parm_keep(array('upload','item'));
272 
273  $OutBuf .= "<script type='text/javascript'>
274  function addArsGo(formid, selectid)
275  {
276  var selectobj = document.getElementById(selectid);
277  var Agent_pk = selectobj.options[selectobj.selectedIndex].value;
278  document.getElementById(formid).action='$action'+'&agent='+Agent_pk;
279  document.getElementById(formid).submit();
280  return;
281  }
282  </script>";
283 
284  $OutBuf .= "<form action=\"$action\" id=\"newds\" method=\"POST\">$AgentSelect</form>";
285  }
286 
287  $selectKey = $filter == 'nolic' ? 'nolic' : 'all';
288  $OutBuf .= "<select name='view_filter' class='form-control-sm' id='view_filter' onchange='ChangeFilter(this,$uploadId, $item);'>";
289  foreach (array('all'=>_("Show all"), 'nolic'=> _("Show files without licenses")) as $key=>$text) {
290  $selected = ($selectKey == $key) ? "selected" : "";
291  $OutBuf .= "<option $selected value=\"$key\">$text</option>";
292  }
293  $OutBuf .= "</select>";
294 
295  $uri = preg_replace("/&item=([0-9]*)/", "", Traceback());
296  list($tables, $tableVars) = $this->ShowUploadHist($uploadId, $item, $uri, $selectKey, $uploadtree_tablename, $agentId);
297  $this->vars['tables'] = $tableVars;
298  $this->vars['pageContent'] = $OutBuf . $tables;
299  $this->vars['scriptBlock'] = $this->createScriptBlock();
300 
301  return;
302  }
303 
312  protected function getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pk, $upload_pk)
313  {
314  $VF=""; // return values for file listing
315  /******* File Listing ************/
316  /* Get ALL the items under this Uploadtree_pk */
317  $Children = GetNonArtifactChildren($Uploadtree_pk, $uploadtree_tablename);
318  $ChildCount = 0;
319  $ChildLicCount = 0;
320  $ChildDirCount = 0; /* total number of directory or containers */
321  foreach ($Children as $c) {
322  if (Iscontainer($c['ufile_mode'])) {
323  $ChildDirCount++;
324  }
325  }
326 
327  $VF .= "<table border=0>";
328  foreach ($Children as $child) {
329  if (empty($child)) {
330  continue;
331  }
332  $ChildCount++;
333 
334  global $Plugins;
335  $ModLicView = &$Plugins[plugin_find_id($this->viewName)];
336  /* Determine the hyperlink for non-containers to view-license */
337  if (!empty($child['pfile_fk']) && !empty($ModLicView)) {
338  $LinkUri = Traceback_uri();
339  $LinkUri .= "?mod=".$this->viewName."&agent=$Agent_pk&upload=$upload_pk&item=$child[uploadtree_pk]";
340  } else {
341  $LinkUri = NULL;
342  }
343 
344  /* Determine link for containers */
345  if (Iscontainer($child['ufile_mode'])) {
346  $uploadtree_pk = DirGetNonArtifact($child['uploadtree_pk'], $uploadtree_tablename);
347  $LicUri = "$Uri&item=" . $uploadtree_pk;
348  } else {
349  $LicUri = NULL;
350  }
351 
352  /* Populate the output ($VF) - file list */
353  /* id of each element is its uploadtree_pk */
354  $LicCount = 0;
355 
356  $cellContent = Isdir($child['ufile_mode']) ? $child['ufile_name'].'/' : $child['ufile_name'];
357  if (Iscontainer($child['ufile_mode'])) {
358  $cellContent = "<a class='btn btn-outline-secondary btn-sm' href='$LicUri'><b>$cellContent</b></a>";
359  } elseif (!empty($LinkUri)) { // && ($LicCount > 0))
360  $cellContent = "<a class='btn btn-outline-secondary btn-sm' href='$LinkUri'>$cellContent</a>";
361  }
362  $VF .= "<tr><td id='$child[uploadtree_pk]' align='left'>$cellContent</td><td>";
363 
364  if ($LicCount) {
365  $VF .= "[" . number_format($LicCount, 0, "", ",") . "&nbsp;";
366  $VF .= "license" . ($LicCount == 1 ? "" : "s");
367  $VF .= "</a>";
368  $VF .= "]";
369  $ChildLicCount += $LicCount;
370  }
371  $VF .= "</td></tr>\n";
372  }
373  $VF .= "</table>\n";
374  return array($ChildCount, $VF);
375  }
376 
382  protected function isADirectory($Uploadtree_pk)
383  {
384  $row = $this->uploadDao->getUploadEntry($Uploadtree_pk, $this->uploadtree_tablename);
385  $isADirectory = IsDir($row['ufile_mode']);
386  return $isADirectory;
387  }
388 
393  public static function returnSortOrder ()
394  {
395  $defaultOrder = array (
396  array(0, "desc"),
397  array(1, "desc"),
398  );
399  return $defaultOrder;
400  }
401 
406  public function getTemplateName()
407  {
408  return "copyrighthist.html.twig";
409  }
410 
415  abstract protected function createScriptBlock();
416 }
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