FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ajax-copyright-hist.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014-2019 Siemens AG
4  * Author: Daniele Fognini, Johannes Najjar, Steffen Weber
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 
28 
29 define("TITLE_COPYRIGHTHISTOGRAMPROCESSPOST", _("Private: Browse post"));
30 
36 {
40  protected $listPage;
44  private $uploadtree_tablename;
48  private $dbManager;
52  private $uploadDao;
56  private $copyrightDao;
57 
61  private $dataTablesUtility;
62 
66  private $textFindingTypes = ["copyFindings"];
67 
68  function __construct()
69  {
70  $this->Name = "ajax-copyright-hist";
71  $this->Title = TITLE_COPYRIGHTHISTOGRAMPROCESSPOST;
72  $this->DBaccess = PLUGIN_DB_READ;
73  $this->OutputType = 'JSON';
74  $this->LoginFlag = 0;
75  $this->NoMenu = 0;
76 
77  parent::__construct();
78  global $container;
79  $this->dataTablesUtility = $container->get('utils.data_tables_utility');
80  $this->uploadDao = $container->get('dao.upload');
81  $this->dbManager = $container->get('db.manager');
82  $this->copyrightDao = $container->get('dao.copyright');
83  }
84 
85 
90  function Output()
91  {
92  $returnValue = 0;
93  if ($this->State != PLUGIN_STATE_READY) {
94  return 0;
95  }
96 
97  $action = GetParm("action", PARM_STRING);
98  $upload = GetParm("upload", PARM_INTEGER);
99  $type = GetParm("type", PARM_STRING);
100 
101  if ($action=="deletedecision" || $action=="undodecision") {
102  $decision = GetParm("decision", PARM_INTEGER);
103  $pfile = GetParm("pfile", PARM_INTEGER);
104  } else if ($action=="deleteHashDecision" || $action=="undoHashDecision") {
105  $hash = GetParm("hash", PARM_STRING);
106  } else if($action=="update" || $action=="delete" || $action=="undo") {
107  $id = GetParm("id", PARM_STRING);
108  $getEachID = array_filter(explode(",", trim($id, ',')), function($var) {
109  return $var !== "";
110  });
111  if(count($getEachID) == 4) {
112  list($upload, $item, $hash, $type) = $getEachID;
113  } else {
114  return new Response('bad request while '.$action,
115  Response::HTTP_BAD_REQUEST,
116  array('Content-type'=>'text/plain')
117  );
118  }
119  }
120 
121  /* check upload permissions */
122  if (!(($action == "getData" || $action == "getDeactivatedData") &&
123  ($this->uploadDao->isAccessible($upload, Auth::getGroupId())) ||
124  ($this->uploadDao->isEditable($upload, Auth::getGroupId())))) {
125  $permDeniedText = _("Permission Denied");
126  $returnValue = "<h2>$permDeniedText</h2>";
127  }
128  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName(
129  $upload);
130 
131  if (in_array($type, $this->textFindingTypes) &&
132  ($action == "getData" || $action == "getDeactivatedData")) {
133  $textFindingsHandler = new TextFindingsAjax($this->uploadtree_tablename);
134  if ($action == "getData") {
135  $returnValue = $textFindingsHandler->doGetData($type, $upload);
136  } elseif ($action == "getDeactivatedData") {
137  $returnValue = $textFindingsHandler->doGetData($type, $upload, false);
138  }
139  } else {
140  switch ($action) {
141  case "getData":
142  $returnValue = $this->doGetData($upload);
143  break;
144  case "getDeactivatedData":
145  $returnValue = $this->doGetData($upload, false);
146  break;
147  case "update":
148  $returnValue = $this->doUpdate($item, $hash, $type);
149  break;
150  case "delete":
151  $returnValue = $this->doDelete($item, $hash, $type);
152  break;
153  case "undo":
154  $returnValue = $this->doUndo($item, $hash, $type);
155  break;
156  case "deletedecision":
157  $returnValue = $this->doDeleteDecision($decision, $pfile, $type);
158  break;
159  case "undodecision":
160  $returnValue = $this->doUndoDecision($decision, $pfile, $type);
161  break;
162  case "deleteHashDecision":
163  $returnValue = $this->doDeleteHashDecision($hash, $upload, $type);
164  break;
165  case "undoHashDecision":
166  $returnValue = $this->doUndoHashDecision($hash, $upload, $type);
167  break;
168  default:
169  $returnValue = "<h2>" . _("Unknown action") . "</h2>";
170  }
171  }
172  return $returnValue;
173  }
174 
184  protected function doGetData($upload, $activated = true)
185  {
186  $item = GetParm("item", PARM_INTEGER);
187  $agent_pk = GetParm("agent", PARM_STRING);
188  $type = GetParm("type", PARM_STRING);
189  $filter = GetParm("filter", PARM_STRING);
190  $listPage = "copyright-list";
191 
192  header('Content-type: text/json');
193  list($aaData, $iTotalRecords, $iTotalDisplayRecords) = $this->getTableData($upload, $item, $agent_pk, $type,$listPage, $filter, $activated);
194  return new JsonResponse(array(
195  'sEcho' => intval($_GET['sEcho']),
196  'aaData' => $aaData,
197  'iTotalRecords' => $iTotalRecords,
198  'iTotalDisplayRecords' => $iTotalDisplayRecords
199  )
200  );
201  }
202 
214  private function getTableData($upload, $item, $agent_pk, $type, $listPage, $filter, $activated = true)
215  {
216  list ($rows, $iTotalDisplayRecords, $iTotalRecords) = $this->getCopyrights($upload, $item, $this->uploadtree_tablename, $agent_pk, $type, $filter, $activated);
217  $aaData = array();
218  if (!empty($rows))
219  {
220  $rw = $this->uploadDao->isEditable($upload, Auth::getGroupId());
221  foreach ($rows as $row)
222  {
223  $aaData [] = $this->fillTableRow($row, $item, $upload, $agent_pk, $type,$listPage, $filter, $activated, $rw);
224  }
225  }
226 
227  return array($aaData, $iTotalRecords, $iTotalDisplayRecords);
228 
229  }
230 
242  protected function getCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $filter, $activated = true)
243  {
244  $offset = GetParm('iDisplayStart', PARM_INTEGER);
245  $limit = GetParm('iDisplayLength', PARM_INTEGER);
246 
247  $tableName = $this->getTableName($type);
248  $orderString = $this->getOrderString();
249 
250  list($left, $right) = $this->uploadDao->getLeftAndRight($item, $uploadTreeTableName);
251 
252  if ($filter == "")
253  {
254  $filter = "none";
255  }
256 
257  $sql_upload = "";
258  if ('uploadtree_a' == $uploadTreeTableName)
259  {
260  $sql_upload = " AND UT.upload_fk=$upload_pk ";
261  }
262 
263  $join = "";
264  $filterQuery = "";
265  if ($type == 'statement' && $filter == "nolic")
266  {
267  $noLicStr = "No_license_found";
268  $voidLicStr = "Void";
269  $join = " INNER JOIN license_file AS LF on cp.pfile_fk=LF.pfile_fk ";
270  $filterQuery = " AND LF.rf_fk IN (SELECT rf_pk FROM license_ref WHERE rf_shortname IN ('$noLicStr','$voidLicStr')) ";
271  } else
272  {
273  // No filter, nothing to do
274  }
275  $params = array($left, $right, $type, $agentId);
276 
277  $filterParms = $params;
278  $searchFilter = $this->addSearchFilter($filterParms);
279  $unorderedQuery = "FROM $tableName AS cp " .
280  "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
281  $join .
282  "WHERE cp.content!='' " .
283  "AND ( UT.lft BETWEEN $1 AND $2 ) " .
284  "AND cp.type = $3 " .
285  "AND cp.agent_fk= $4 " .
286  "AND cp.is_enabled=" . ($activated ? 'true' : 'false') .
287  $sql_upload;
288  $totalFilter = $filterQuery . " " . $searchFilter;
289 
290  $grouping = " GROUP BY content ";
291 
292  $countQuery = "SELECT count(*) FROM (SELECT content, count(*) $unorderedQuery $totalFilter $grouping) as K";
293  $iTotalDisplayRecordsRow = $this->dbManager->getSingleRow($countQuery,
294  $filterParms, __METHOD__.$tableName . ".count" . ($activated ? '' : '_deactivated'));
295  $iTotalDisplayRecords = $iTotalDisplayRecordsRow['count'];
296 
297  $countAllQuery = "SELECT count(*) FROM (SELECT content, count(*) $unorderedQuery$grouping) as K";
298  $iTotalRecordsRow = $this->dbManager->getSingleRow($countAllQuery, $params, __METHOD__,$tableName . "count.all" . ($activated ? '' : '_deactivated'));
299  $iTotalRecords = $iTotalRecordsRow['count'];
300 
301  $range = "";
302  $filterParms[] = $offset;
303  $range .= ' OFFSET $' . count($filterParms);
304  $filterParms[] = $limit;
305  $range .= ' LIMIT $' . count($filterParms);
306 
307  $sql = "SELECT content, hash, count(*) as copyright_count " .
308  $unorderedQuery . $totalFilter . " GROUP BY content, hash " . $orderString . $range;
309  $statement = __METHOD__ . $filter.$tableName . $uploadTreeTableName . ($activated ? '' : '_deactivated');
310  $this->dbManager->prepare($statement, $sql);
311  $result = $this->dbManager->execute($statement, $filterParms);
312  $rows = $this->dbManager->fetchAll($result);
313  $this->dbManager->freeResult($result);
314 
315  return array($rows, $iTotalDisplayRecords, $iTotalRecords);
316  }
317 
327  private function getTableName($type)
328  {
329  switch ($type) {
330  case "ecc" :
331  $tableName = "ecc";
332  break;
333  case "keyword" :
334  $tableName = "keyword";
335  $filter="none";
336  break;
337  case "statement" :
338  $tableName = "copyright";
339  break;
340  default:
341  $tableName = "author";
342  }
343  return $tableName;
344  }
345 
350  private function getOrderString()
351  {
352  $columnNamesInDatabase = array('copyright_count', 'content');
353 
354  $defaultOrder = CopyrightHistogram::returnSortOrder();
355 
356  $orderString = $this->dataTablesUtility->getSortingString($_GET, $columnNamesInDatabase, $defaultOrder);
357 
358  return $orderString;
359  }
360 
366  private function addSearchFilter(&$filterParams)
367  {
368  $searchPattern = GetParm('sSearch', PARM_STRING);
369  if (empty($searchPattern))
370  {
371  return '';
372  }
373  $filterParams[] = "%$searchPattern%";
374  return ' AND CP.content ilike $'.count($filterParams).' ';
375  }
376 
387  private function getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated = true, $rw = true)
388  {
389  if($rw)
390  {
391  $act = "<img";
392  if(!$activated)
393  {
394  $act .= " hidden='true'";
395  }
396  $act .= " id='delete$type$hash' onClick='delete$type($upload,$uploadTreeId,\"$hash\",\"$type\");' class=\"delete\" src=\"images/space_16.png\">";
397  $act .= "<span";
398  if($activated) {
399  $act .= " hidden='true'";
400  }
401  $act .= " id='update$type$hash'>deactivated [<a href=\"#\" id='undo$type$hash' onClick='undo$type($upload,$uploadTreeId,\"$hash\",\"$type\");return false;'>Undo</a>]</span>";
402  return $act;
403  }
404  if(!$activated) {
405  return "deactivated";
406  }
407  return "";
408  }
409 
423  private function fillTableRow($row, $uploadTreeId, $upload, $agentId, $type,$listPage, $filter = "", $activated = true, $rw = true)
424  {
425  $hash = $row['hash'];
426  $output = array('DT_RowId' => "$upload,$uploadTreeId,$hash,$type" );
427 
428  $link = "<a href='";
429  $link .= Traceback_uri();
430  $urlArgs = "?mod=".$listPage."&agent=$agentId&item=$uploadTreeId&hash=$hash&type=$type";
431  if (!empty($filter)) {
432  $urlArgs .= "&filter=$filter";
433  }
434  $link .= $urlArgs . "'>" . $row['copyright_count'] . "</a>";
435  $output['0'] = $link;
436  $output['1'] = convertToUTF8($row['content']);
437  $output['2'] = $this->getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated, $rw);
438  if($rw && $activated)
439  {
440  $output['3'] = "<input type='checkbox' class='deleteBySelect$type' id='deleteBySelect$type$hash' value='".$upload.",".$uploadTreeId.",".$hash.",".$type."'>";
441  }
442  else
443  {
444  $output['3'] = "";
445  }
446  return $output;
447  }
448 
456  protected function doUpdate($itemId, $hash, $type)
457  {
458  $content = GetParm("value", PARM_RAW);
459  if (!$content)
460  {
461  return new Response('empty content not allowed', Response::HTTP_BAD_REQUEST ,array('Content-type'=>'text/plain'));
462  }
463 
464  $item = $this->uploadDao->getItemTreeBounds($itemId, $this->uploadtree_tablename);
465  $cpTable = $this->getTableName($type);
466  $this->copyrightDao->updateTable($item, $hash, $content, Auth::getUserId(), $cpTable);
467 
468  return new Response('success', Response::HTTP_OK,array('Content-type'=>'text/plain'));
469  }
470 
478  protected function doDelete($itemId, $hash, $type)
479  {
480  $item = $this->uploadDao->getItemTreeBounds($itemId, $this->uploadtree_tablename);
481  $cpTable = $this->getTableName($type);
482  $this->copyrightDao->updateTable($item, $hash, '', Auth::getUserId(), $cpTable, 'delete');
483  return new Response('Successfully deleted', Response::HTTP_OK, array('Content-type'=>'text/plain'));
484  }
485 
493  protected function doUndo($itemId, $hash, $type) {
494  $item = $this->uploadDao->getItemTreeBounds($itemId, $this->uploadtree_tablename);
495  $cpTable = $this->getTableName($type);
496  $this->copyrightDao->updateTable($item, $hash, '', Auth::getUserId(), $cpTable, 'rollback');
497  return new Response('Successfully restored', Response::HTTP_OK, array('Content-type'=>'text/plain'));
498  }
499 
507  protected function doDeleteDecision($decisionId, $pfileId, $type) {
508  $this->copyrightDao->removeDecision($type."_decision", $pfileId, $decisionId);
509  return new JsonResponse(array("msg" => $decisionId . " .. " . $pfileId . " .. " . $type));
510  }
511 
519  protected function doUndoDecision($decisionId, $pfileId, $type) {
520  $this->copyrightDao->undoDecision($type."_decision", $pfileId, $decisionId);
521  return new JsonResponse(array("msg" => $decisionId . " .. " . $pfileId . " .. " . $type));
522  }
523 
531  protected function doDeleteHashDecision($hash, $upload, $type)
532  {
533  $tableName = $type."_decision";
534  $decisions = $this->copyrightDao->getDecisionsFromHash($tableName, $hash,
535  $upload, $this->uploadtree_tablename);
536  foreach ($decisions as $decision) {
537  $this->copyrightDao->removeDecision($tableName, $decision['pfile_fk'],
538  $decision[$tableName . '_pk']);
539  }
540  return new JsonResponse(array("msg" => "$hash .. $upload .. $type"));
541  }
542 
550  protected function doUndoHashDecision($hash, $upload, $type)
551  {
552  $tableName = $type."_decision";
553  $decisions = $this->copyrightDao->getDecisionsFromHash($tableName, $hash,
554  $upload, $this->uploadtree_tablename);
555  foreach ($decisions as $decision) {
556  $this->copyrightDao->undoDecision($tableName, $decision['pfile_fk'],
557  $decision[$tableName . '_pk']);
558  }
559  return new JsonResponse(array("msg" => "$hash .. $upload .. $type"));
560  }
561 }
562 
563 $NewPlugin = new CopyrightHistogramProcessPost();
564 $NewPlugin->Initialize();
Handles Ajax requests for copyright.
const PARM_RAW
Definition: common-parm.php:33
getTableName($type)
Get table name based on statement type.
Traceback_uri()
Get the URI without query to this location.
doGetData($upload, $activated=true)
Handles GET request and create a JSON response.
doUndoHashDecision($hash, $upload, $type)
Rollback decisions for an upload which matches a hash.
doDelete($itemId, $hash, $type)
Disable a result.
addSearchFilter(&$filterParams)
Add filter on content.
doUndo($itemId, $hash, $type)
Rollback a result.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
Handles Ajax requests for text findings.
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
getOrderString()
Create sorting string for database query.
getTableData($upload, $item, $agent_pk, $type, $listPage, $filter, $activated=true)
Get the copyright data and fill in expected format.
const PARM_STRING
Definition: common-parm.php:29
doDeleteHashDecision($hash, $upload, $type)
Disable decisions for an upload which matches a hash.
const PARM_INTEGER
Definition: common-parm.php:25
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
doUndoDecision($decisionId, $pfileId, $type)
Rollback a decision.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
getCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $filter, $activated=true)
Get results from database and format for JSON.
fillTableRow($row, $uploadTreeId, $upload, $agentId, $type, $listPage, $filter="", $activated=true, $rw=true)
Fill table content for JSON response.
doUpdate($itemId, $hash, $type)
Update result.
Output()
Display the loaded menu and plugins.
if(!defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
returnSortOrder()
Get sorting orders.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695
getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated=true, $rw=true)
Helper to create action column for results.
doDeleteDecision($decisionId, $pfileId, $type)
Disable a decision.