FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ajax-spasht-copyright-hist.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Vivek Kumar <vvksindia@gmail.com>
4  Author: Vivek Kumar<vvksindia@gmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
13 use Symfony\Component\HttpFoundation\JsonResponse;
14 use Symfony\Component\HttpFoundation\Response;
15 
16 define("TITLE_SPASHTCOPYRIGHTHISTOGRAMPROCESSPOST", _("Private: Browse post"));
17 
23 {
27  protected $listPage;
31  private $uploadtree_tablename;
35  private $uploadDao;
39  private $spashtDao;
40 
44  private $dataTablesUtility;
45 
49  private $textFindingTypes = ["copyFindings"];
50 
51  function __construct()
52  {
53  $this->Name = "ajax-spasht-copyright-hist";
54  $this->Title = TITLE_SPASHTCOPYRIGHTHISTOGRAMPROCESSPOST;
55  $this->DBaccess = PLUGIN_DB_WRITE;
56  $this->OutputType = 'JSON';
57  $this->LoginFlag = 0;
58  $this->NoMenu = 0;
59 
60  parent::__construct();
61  global $container;
62  $this->dataTablesUtility = $container->get('utils.data_tables_utility');
63  $this->uploadDao = $container->get('dao.upload');
64  $this->dbManager = $container->get('db.manager');
65  $this->spashtDao = $container->get('dao.spasht');
66  }
67 
68 
73  function Output()
74  {
75  $returnValue = 0;
76  if ($this->State != PLUGIN_STATE_READY) {
77  return 0;
78  }
79 
80  $action = GetParm("action", PARM_STRING);
81  $upload = GetParm("upload", PARM_INTEGER);
82  $type = GetParm("type", PARM_STRING);
83 
84  if ($action=="deletedecision" || $action=="undodecision") {
85  $decision = GetParm("decision", PARM_INTEGER);
86  $pfile = GetParm("pfile", PARM_INTEGER);
87  } else if ($action=="deleteHashDecision" || $action=="undoHashDecision") {
88  $hash = GetParm("hash", PARM_STRING);
89  } else if ($action=="update" || $action=="delete" || $action=="undo") {
90  $id = GetParm("id", PARM_STRING);
91  $getEachID = array_filter(explode(",", trim($id, ',')), function($var) {
92  return $var !== "";
93  });
94  if (count($getEachID) == 4) {
95  list($upload, $item, $hash, $type) = $getEachID;
96  } else {
97  return new Response('bad request while '.$action,
98  Response::HTTP_BAD_REQUEST,
99  array('Content-type'=>'text/plain')
100  );
101  }
102  }
103 
104  /* check upload permissions */
105  if (!(($action == "getData" || $action == "getDeactivatedData") &&
106  ($this->uploadDao->isAccessible($upload, Auth::getGroupId())) ||
107  ($this->uploadDao->isEditable($upload, Auth::getGroupId())))) {
108  $permDeniedText = _("Permission Denied");
109  $returnValue = "<h2>$permDeniedText</h2>";
110  }
111  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName(
112  $upload);
113 
114  if (in_array($type, $this->textFindingTypes) &&
115  ($action == "getData" || $action == "getDeactivatedData")) {
116  $textFindingsHandler = new TextFindingsAjax($this->uploadtree_tablename);
117  if ($action == "getData") {
118  $returnValue = $textFindingsHandler->doGetData($type, $upload);
119  } elseif ($action == "getDeactivatedData") {
120  $returnValue = $textFindingsHandler->doGetData($type, $upload, false);
121  }
122  } else {
123  switch ($action) {
124  case "getData":
125  $returnValue = $this->doGetData($upload);
126  break;
127  case "getDeactivatedData":
128  $returnValue = $this->doGetData($upload, false);
129  break;
130  case "update":
131  $returnValue = $this->doUpdate($item, $hash, $type);
132  break;
133  case "delete":
134  $returnValue = $this->doDelete($item, $hash, $type);
135  break;
136  case "undo":
137  $returnValue = $this->doUndo($item, $hash, $type);
138  break;
139  default:
140  $returnValue = "<h2>" . _("Unknown action") . "</h2>";
141  }
142  }
143  return $returnValue;
144  }
145 
155  protected function doGetData($upload, $activated = true)
156  {
157  $item = GetParm("item", PARM_INTEGER);
158  $agent_pk = GetParm("agent", PARM_STRING);
159  $type = GetParm("type", PARM_STRING);
160  $filter = GetParm("filter", PARM_STRING);
161  $this->listPage = "spasht-copyright-list";
162 
163  header('Content-type: text/json');
164  list ($aaData, $iTotalRecords, $iTotalDisplayRecords) = $this->getTableData(
165  $upload, $item, $agent_pk, $type, $this->listPage, $filter, $activated);
166  return new JsonResponse(array(
167  'sEcho' => intval($_GET['sEcho']),
168  'aaData' => $aaData,
169  'iTotalRecords' => $iTotalRecords,
170  'iTotalDisplayRecords' => $iTotalDisplayRecords
171  )
172  );
173  }
174 
188  private function getTableData($upload, $item, $agent_pk, $type, $listPage,
189  $filter, $activated = true)
190  {
191  list ($rows, $iTotalDisplayRecords, $iTotalRecords) = $this->getCopyrights(
192  $upload, $item, $this->uploadtree_tablename, $agent_pk, $type, $filter,
193  $activated);
194  $aaData = array();
195  if (!empty($rows)) {
196  $rw = $this->uploadDao->isEditable($upload, Auth::getGroupId());
197  foreach ($rows as $row) {
198  $aaData [] = $this->fillTableRow($row, $item, $upload, $agent_pk, $type,
199  $listPage, $filter, $activated, $rw);
200  }
201  }
202 
203  return array($aaData, $iTotalRecords, $iTotalDisplayRecords);
204  }
205 
219  protected function getCopyrights($upload_pk, $item, $uploadTreeTableName,
220  $agentId, $type, $filter, $activated = true)
221  {
222  $offset = GetParm('iDisplayStart', PARM_INTEGER);
223  $limit = GetParm('iDisplayLength', PARM_INTEGER);
224 
225  $tableName = $this->getTableName($type);
226  $orderString = $this->getOrderString();
227 
228  list($left, $right) = $this->uploadDao->getLeftAndRight($item,
229  $uploadTreeTableName);
230 
231  if ($filter == "") {
232  $filter = "none";
233  }
234 
235  $sql_upload = "";
236  if ('uploadtree_a' == $uploadTreeTableName) {
237  $sql_upload = " AND UT.upload_fk=$upload_pk ";
238  }
239 
240  $join = "";
241  $filterQuery = "";
242  if ($type == 'statement' && $filter == "nolic") {
243  $noLicStr = "No_license_found";
244  $voidLicStr = "Void";
245  $join = " INNER JOIN license_file AS LF on cp.pfile_fk=LF.pfile_fk ";
246  $filterQuery = " AND LF.rf_fk IN (SELECT rf_pk FROM license_ref WHERE rf_shortname IN ('$noLicStr','$voidLicStr')) ";
247  } else {
248  // No filter, nothing to do
249  }
250  $params = array($left, $right, $agentId);
251 
252  $filterParms = $params;
253  $searchFilter = $this->addSearchFilter($filterParms);
254  $unorderedQuery = "FROM $tableName AS cp " .
255  "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
256  $join .
257  "WHERE textfinding!='' " .
258  "AND ( UT.lft BETWEEN $1 AND $2 ) " .
259  "AND cp.agent_fk= $3 " .
260  "AND cp.is_enabled=" . ($activated ? 'true' : 'false') .
261  $sql_upload;
262  $totalFilter = $filterQuery . " " . $searchFilter;
263 
264  $grouping = " GROUP BY hash, textfinding ";
265 
266  $countQuery = "SELECT count(*) FROM (SELECT textfinding AS content, count(*) $unorderedQuery $totalFilter $grouping) as K";
267  $iTotalDisplayRecordsRow = $this->dbManager->getSingleRow($countQuery,
268  $filterParms, __METHOD__.$tableName . ".count" .
269  ($activated ? '' : '_deactivated'));
270  $iTotalDisplayRecords = $iTotalDisplayRecordsRow['count'];
271 
272  $countAllQuery = "SELECT count(*) FROM (SELECT textfinding AS content, count(*) $unorderedQuery$grouping) as K";
273  $iTotalRecordsRow = $this->dbManager->getSingleRow($countAllQuery, $params,
274  __METHOD__,$tableName . "count.all" . ($activated ? '' : '_deactivated'));
275  $iTotalRecords = $iTotalRecordsRow['count'];
276 
277  $range = "";
278  $filterParms[] = $offset;
279  $range .= ' OFFSET $' . count($filterParms);
280  $filterParms[] = $limit;
281  $range .= ' LIMIT $' . count($filterParms);
282 
283  $sql = "SELECT textfinding AS content, hash, count(*) as copyright_count " .
284  $unorderedQuery . $totalFilter . " GROUP BY content, hash " .
285  $orderString . $range;
286  $statement = __METHOD__ . $filter.$tableName . $uploadTreeTableName .
287  ($activated ? '' : '_deactivated');
288  $rows = $this->dbManager->getRows($sql, $filterParms, $statement);
289 
290  return array($rows, $iTotalDisplayRecords, $iTotalRecords);
291  }
292 
302  private function getTableName($type)
303  {
304  return "copyright_spasht";
305  }
306 
311  private function getOrderString()
312  {
313  $columnNamesInDatabase = array('copyright_count', 'content');
314 
315  $defaultOrder = HistogramBase::returnSortOrder();
316 
317  return $this->dataTablesUtility->getSortingString($_GET,
318  $columnNamesInDatabase, $defaultOrder);
319  }
320 
326  private function addSearchFilter(&$filterParams)
327  {
328  $searchPattern = GetParm('sSearch', PARM_STRING);
329  if (empty($searchPattern)) {
330  return '';
331  }
332  $filterParams[] = "%$searchPattern%";
333  return ' AND textfinding like $'.count($filterParams).' ';
334  }
335 
346  private function getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated = true, $rw = true)
347  {
348  if ($rw) {
349  $act = "<img";
350  if (!$activated) {
351  $act .= " hidden='true'";
352  }
353  $act .= " id='delete$type$hash' onClick='delete$type($upload,$uploadTreeId,\"$hash\",\"$type\");' class=\"delete\" src=\"images/space_16.png\">";
354  $act .= "<span";
355  if ($activated) {
356  $act .= " hidden='true'";
357  }
358  $act .= " id='update$type$hash'>deactivated [<a href=\"#\" id='undo$type$hash' onClick='undo$type($upload,$uploadTreeId,\"$hash\",\"$type\");return false;'>Undo</a>]</span>";
359  return $act;
360  }
361  if (!$activated) {
362  return "deactivated";
363  }
364  return "";
365  }
366 
380  private function fillTableRow($row, $uploadTreeId, $upload, $agentId, $type,
381  $listPage, $filter = "", $activated = true, $rw = true)
382  {
383  $hash = $row['hash'];
384  $output = array('DT_RowId' => "$upload,$uploadTreeId,$hash,$type" );
385 
386  $link = "<a href='";
387  $link .= Traceback_uri();
388  $urlArgs = "?mod=".$listPage."&agent=$agentId&item=$uploadTreeId&hash=$hash&type=$type";
389  if (!empty($filter)) {
390  $urlArgs .= "&filter=$filter";
391  }
392  $link .= $urlArgs . "'>" . $row['copyright_count'] . "</a>";
393  // Copyright filtering needs work. Removing link from output[0]
394  $output['0'] = $row['copyright_count'];
395  $output['1'] = convertToUTF8($row['content']);
396  $output['2'] = $this->getTableRowAction($hash, $uploadTreeId, $upload,
397  $type, $activated, $rw);
398  if ($rw && $activated) {
399  $output['3'] = "<input type='checkbox' class='deleteBySelect$type' id='deleteBySelect$type$hash' value='".$upload.",".$uploadTreeId.",".$hash.",".$type."'>";
400  } else {
401  $output['3'] = "<input type='checkbox' class='undoBySelect$type' id='undoBySelect$type$hash' value='".$upload.",".$uploadTreeId.",".$hash.",".$type."'>";
402  }
403  return $output;
404  }
405 
413  protected function doUpdate($itemId, $hash, $type)
414  {
415  $content = GetParm("value", PARM_RAW);
416  if (!$content) {
417  return new Response('empty content not allowed',
418  Response::HTTP_BAD_REQUEST, array('Content-type'=>'text/plain'));
419  }
420 
421  $item = $this->uploadDao->getItemTreeBounds($itemId,
422  $this->uploadtree_tablename);
423  $this->spashtDao->updateCopyright($item, $hash, $content);
424 
425  return new Response('success', Response::HTTP_OK,
426  array('Content-type'=>'text/plain'));
427  }
428 
436  protected function doDelete($itemId, $hash, $type)
437  {
438  $item = $this->uploadDao->getItemTreeBounds($itemId,
439  $this->uploadtree_tablename);
440  $this->spashtDao->updateCopyright($item, $hash, '', 'delete');
441  return new Response('Successfully deleted', Response::HTTP_OK,
442  array('Content-type'=>'text/plain'));
443  }
444 
452  protected function doUndo($itemId, $hash, $type)
453  {
454  $item = $this->uploadDao->getItemTreeBounds($itemId,
455  $this->uploadtree_tablename);
456  $this->spashtDao->updateCopyright($item, $hash, '', 'rollback');
457  return new Response('Successfully restored', Response::HTTP_OK,
458  array('Content-type'=>'text/plain'));
459  }
460 }
461 
462 $NewPlugin = new SpashtCopyrightHistogramProcessPost();
463 $NewPlugin->Initialize();
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Handles Ajax requests for text findings.
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static returnSortOrder()
Get sorting orders.
Handles Ajax requests for copyright.
addSearchFilter(&$filterParams)
Add filter on content.
doUpdate($itemId, $hash, $type)
Update result.
getCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $filter, $activated=true)
Get results from database and format for JSON.
doDelete($itemId, $hash, $type)
Disable a result.
getOrderString()
Create sorting string for database query.
Output()
Display the loaded menu and plugins.
doGetData($upload, $activated=true)
Handles GET request and create a JSON response.
getTableName($type)
Get table name based on statement type.
doUndo($itemId, $hash, $type)
Rollback a result.
__construct()
base constructor. Most plugins will just use this
getTableData($upload, $item, $agent_pk, $type, $listPage, $filter, $activated=true)
Get the copyright data and fill in expected format.
getTableRowAction($hash, $uploadTreeId, $upload, $type, $activated=true, $rw=true)
Helper to create action column for results.
fillTableRow($row, $uploadTreeId, $upload, $agentId, $type, $listPage, $filter="", $activated=true, $rw=true)
Fill table content for JSON response.
Definition: state.hpp:16
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
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
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16