FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ClearedGetterCommon.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2017 Siemens AG
4  Author: Daniele Fognini, Shaheem Azmal M MD
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Report;
9 
12 
13 abstract class ClearedGetterCommon
14 {
16  protected $uploadDao;
17 
19  protected $treeDao;
20 
22  private $fileNameCache = array();
23 
25  private $fileHashes = array();
26 
27  private $userId;
28  private $groupId;
29  private $uploadId;
30  private $groupBy;
31 
32  public function __construct($groupBy = "content")
33  {
34  global $container;
35 
36  $this->uploadDao = $container->get('dao.upload');
37  $this->treeDao = $container->get('dao.tree');
38 
39  $this->groupBy = $groupBy;
40  }
41 
45  public function getCliArgs()
46  {
47  $args = getopt("u:", array("uId:","gId:"));
48 
49  if (!array_key_exists('u',$args)) {
50  throw new \Exception("missing required parameter -u {uploadId}\n",2);
51  }
52 
53  $this->uploadId = intval($args['u']);
54  $this->userId = intval(@$args['uId']);
55  $this->groupId = intval(@$args['gId']);
56  }
57 
58  public function getUploadId()
59  {
60  $uploadId = $this->uploadId;
61 
62  if ($uploadId <= 0) {
63  print "invalid uploadId ".$uploadId;
64  exit(2);
65  }
66  return $uploadId;
67  }
68 
69  public function getUserId()
70  {
71  $userId = $this->userId;
72 
73  if ($userId <= 0) {
74  print "invalid user ".$userId;
75  exit(2);
76  }
77  return $userId;
78  }
79 
80  public function getGroupId()
81  {
82  $groupId = $this->groupId;
83 
84  if ($groupId <= 0) {
85  print "invalid group ".$groupId;
86  exit(2);
87  }
88  return $groupId;
89  }
90 
91  protected function changeTreeIdsToPaths(&$ungrupedStatements, $uploadTreeTableName, $uploadId)
92  {
93  $parentId = $this->treeDao->getMinimalCoveringItem($uploadId, $uploadTreeTableName);
94 
95  foreach ($ungrupedStatements as &$statement) {
96  $uploadTreeId = $statement['uploadtree_pk'];
97  unset($statement['uploadtree_pk']);
98 
99  if (!array_key_exists($uploadTreeId, $this->fileNameCache)) {
100  $this->fileNameCache[$uploadTreeId] = $this->treeDao->getFullPath($uploadTreeId, $uploadTreeTableName, $parentId);
101  $this->fileHashes[$uploadTreeId] = $this->treeDao->getItemHashes($uploadTreeId);
102  }
103 
104  $statement['fileName'] = $this->fileNameCache[$uploadTreeId];
105  $statement['fileHash'] = strtolower($this->fileHashes[$uploadTreeId]["sha1"]);
106  }
107  unset($statement);
108  }
109 
115  protected function groupUserFindings($findings)
116  {
117  $uniqueArray = array();
118  foreach ($findings as $item) {
119  $contentKey = $item['content'];
120  if (!isset($uniqueArray[$contentKey])) {
121  $uniqueArray[$contentKey] = array(
122  "licenseId" => $item["licenseId"],
123  "content" => $item["content"],
124  "text" => $item["text"],
125  "files" => array(),
126  "hash" => array(),
127  "comments" => $item["comments"]
128  );
129  }
130  $uniqueArray[$contentKey]['files'] = array_merge($uniqueArray[$contentKey]['files'], $item['files']);
131  $uniqueArray[$contentKey]['hash'] = array_merge($uniqueArray[$contentKey]['hash'], $item['hash']);
132  }
133  return array_values($uniqueArray);
134  }
135 
136  protected function groupStatements($ungrupedStatements, $extended, $agentCall, $isUnifiedReport, $objectAgent)
137  {
138  $statements = array();
139  $findings = array();
140  $countLoop = 0;
141  foreach ($ungrupedStatements as $statement) {
142  $licenseId = (array_key_exists('licenseId', $statement)) ? convertToUTF8($statement['licenseId'], false) : '';
143  $content = (array_key_exists('content', $statement)) ? convertToUTF8($statement['content'], false) : '';
144  $content = htmlspecialchars($content, ENT_DISALLOWED);
145  $comments = (array_key_exists('comments', $statement)) ? convertToUTF8($statement['comments'], false) : '';
146  $fileName = $statement['fileName'];
147  $fileHash = $statement['fileHash'];
148  if (array_key_exists('acknowledgement', $statement)) {
149  $acknowledgement = $statement['acknowledgement'];
150  } else {
151  $acknowledgement = "";
152  }
153 
154  if (!array_key_exists('text', $statement)) {
155  $description = (array_key_exists('description', $statement)) ? convertToUTF8($statement['description'], false) : '';
156  $textfinding = (array_key_exists('textfinding', $statement)) ? convertToUTF8($statement['textfinding'], false) : '';
157 
158  if ($description === null) {
159  $text = "";
160  } else {
161  if (!empty($textfinding) && empty($agentCall)) {
162  $content = $textfinding;
163  }
164  $text = $description;
165  }
166  } else {
167  $text = $statement['text'];
168  }
169 
170  if ($agentCall == "license") {
171  $this->groupBy = "text";
172  $groupBy = md5($statement[$this->groupBy].$statement["content"]);
173  } else {
174  $this->groupBy = "content";
175  $groupBy = $statement[$this->groupBy];
176  }
177 
178  if (empty($comments) && array_key_exists($groupBy, $statements)) {
179  $currentFiles = &$statements[$groupBy]['files'];
180  $currentHash = &$statements[$groupBy]['hash'];
181  $currentAcknowledgement = &$statements[$groupBy]['acknowledgement'];
182  if (!in_array($fileName, $currentFiles)) {
183  $currentFiles[] = $fileName;
184  $currentHash[] = $fileHash;
185  $currentAcknowledgement[] = $acknowledgement;
186  }
187  } else {
188  $singleStatement = array(
189  "licenseId" => $licenseId,
190  "content" => convertToUTF8($content, false),
191  "text" => convertToUTF8($text, false),
192  "files" => array($fileName),
193  "hash" => array($fileHash),
194  "acknowledgement" => array($acknowledgement)
195  );
196  if ($extended) {
197  $singleStatement["licenseId"] = $licenseId;
198  $singleStatement["comments"] = convertToUTF8($comments, false);
199  $singleStatement["risk"] = (array_key_exists('risk', $statement)) ? convertToUTF8($statement['risk'], false) : 0;
200  }
201 
202  if (empty($comments)) {
203  $statements[$groupBy] = $singleStatement;
204  } else {
205  $statements[] = $singleStatement;
206  }
207  }
208 
209  if (!empty($statement['textfinding']) && !empty($agentCall) && $agentCall != "license") {
210  $findings[] = array(
211  "licenseId" => $licenseId,
212  "content" => convertToUTF8($statement['textfinding'], false),
213  "text" => convertToUTF8($text, false),
214  "files" => array($fileName),
215  "hash" => array($fileHash)
216  );
217  if ($extended) {
218  $key = array_search($statement['textfinding'], array_column($findings, 'content'));
219  $findings[$key]["comments"] = convertToUTF8($comments, false);
220  $findings[$key]["licenseId"] = $licenseId;
221  }
222  }
223  //To keep the schedular alive for large files
224  $countLoop += 1;
225  if ($countLoop % 500 == 0) {
226  $objectAgent->heartbeat(0);
227  }
228  }
229  arsort($statements);
230  if ($agentCall == "copyright" && $isUnifiedReport) {
231  arsort($findings);
232  if (!empty($objectAgent)) {
233  $actualHeartbeat = (count($statements) + count($findings));
234  $objectAgent->heartbeat($actualHeartbeat);
235  }
236  return array("userFindings" => $this->groupUserFindings($findings), "scannerFindings" => $statements);
237  } else {
238  $statements = array_merge($findings, $statements);
239  if (!empty($objectAgent)) {
240  $objectAgent->heartbeat(count($statements));
241  }
242  if ($agentCall != "license") {
243  return array("statements" => $this->groupUserFindings(array_values($statements)));
244  }
245  return array("statements" => array_values($statements));
246  }
247  }
248 
255  abstract protected function getStatements($uploadId, $uploadTreeTableName, $groupId=null);
256 
257  public function getCleared($uploadId, $objectAgent, $groupId=null, $extended=true, $agentcall=null, $isUnifiedReport=false)
258  {
259  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
260  $ungroupedStatements = $this->getStatements($uploadId, $uploadTreeTableName, $groupId);
261  $this->changeTreeIdsToPaths($ungroupedStatements, $uploadTreeTableName, $uploadId);
262  return $this->groupStatements($ungroupedStatements, $extended, $agentcall,
263  $isUnifiedReport, $objectAgent);
264  }
265 
266  public function getLicenseHistogramForReport($uploadId, $groupId)
267  {
268  $histogramStatements = $this->getHistogram($uploadId, $groupId);
269  return array("statements" => $histogramStatements);
270  }
271 
272  public function cJson($uploadId, $groupId=null)
273  {
274  $escapeChars = array('\\f',"\\", "/", "\"");
275  $withThisValue = array("","\\\\", "\\/", "\\\"");
276  $groupedStatements = $this->getCleared($uploadId, null, $groupId, false,
277  null, false);
278  if (array_key_exists("statements", $groupedStatements)) {
279  $clearedString = str_replace($escapeChars, $withThisValue,
280  $groupedStatements["statements"]);
281  } else { // Called from unknown entity
282  $clearedString = $groupedStatements;
283  }
284  $json = json_encode($clearedString);
285  return str_replace('\u001b','',$json);
286  }
287 
288  public function cJsonHist($uploadId, $groupId=null)
289  {
290  $jsonHist = json_encode($this->getLicenseHistogramForReport($uploadId, $groupId));
291  return str_replace('\u001b','',str_replace('\\f','',$jsonHist));
292  }
293 }
groupUserFindings($findings)
Group the content inside a array.
getStatements($uploadId, $uploadTreeTableName, $groupId=null)
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)