FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ClearedGetterCommon.php
1 <?php
2 /*
3  Copyright (C) 2014-2017, Siemens AG
4  Author: Daniele Fognini, Shaheem Azmal M MD
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 
20 namespace Fossology\Lib\Report;
21 
24 
25 abstract class ClearedGetterCommon
26 {
28  protected $uploadDao;
29 
31  protected $treeDao;
32 
34  private $fileNameCache = array();
35 
36  private $userId;
37  private $groupId;
38  private $uploadId;
39  private $groupBy;
40 
41  public function __construct($groupBy = "content")
42  {
43  global $container;
44 
45  $this->uploadDao = $container->get('dao.upload');
46  $this->treeDao = $container->get('dao.tree');
47 
48  $this->groupBy = $groupBy;
49  }
50 
51  public function getCliArgs()
52  {
53  $args = getopt("u:", array("uId:","gId:"));
54 
55  if (!array_key_exists('u',$args)) {
56  throw new Exception("missing required parameter -u {uploadId}\n",2);
57  }
58 
59  $this->uploadId = intval($args['u']);
60  $this->userId = intval(@$args['uId']);
61  $this->groupId = intval(@$args['gId']);
62  }
63 
64  public function getUploadId()
65  {
66  $uploadId = $this->uploadId;
67 
68  if ($uploadId <= 0) {
69  print "invalid uploadId ".$uploadId;
70  exit(2);
71  }
72  return $uploadId;
73  }
74 
75  public function getUserId()
76  {
77  $userId = $this->userId;
78 
79  if ($userId <= 0) {
80  print "invalid user ".$userId;
81  exit(2);
82  }
83  return $userId;
84  }
85 
86  public function getGroupId()
87  {
88  $groupId = $this->groupId;
89 
90  if ($groupId <= 0) {
91  print "invalid group ".$groupId;
92  exit(2);
93  }
94  return $groupId;
95  }
96 
97  protected function changeTreeIdsToPaths(&$ungrupedStatements, $uploadTreeTableName, $uploadId)
98  {
99  $parentId = $this->treeDao->getMinimalCoveringItem($uploadId, $uploadTreeTableName);
100 
101  foreach ($ungrupedStatements as &$statement) {
102  $uploadTreeId = $statement['uploadtree_pk'];
103  unset($statement['uploadtree_pk']);
104 
105  if (!array_key_exists($uploadTreeId, $this->fileNameCache)) {
106  $this->fileNameCache[$uploadTreeId] = $this->treeDao->getFullPath($uploadTreeId, $uploadTreeTableName, $parentId);
107  }
108 
109  $statement['fileName'] = $this->fileNameCache[$uploadTreeId];
110  }
111  unset($statement);
112  }
113 
114  protected function groupStatements($ungrupedStatements, $extended, $agentCall, $isUnifiedReport, $objectAgent)
115  {
116  $statements = array();
117  $findings = array();
118  $countLoop = 0;
119  foreach ($ungrupedStatements as $statement) {
120  $licenseId = $statement['licenseId'];
121  $content = convertToUTF8($statement['content'], false);
122  $content = htmlspecialchars($content, ENT_DISALLOWED);
123  $comments = convertToUTF8($statement['comments'], false);
124  $fileName = $statement['fileName'];
125 
126  if (!array_key_exists('text', $statement)) {
127  $description = $statement['description'];
128  $textfinding = $statement['textfinding'];
129 
130  if ($description === null) {
131  $text = "";
132  } else {
133  if (!empty($textfinding) && empty($agentCall)) {
134  $content = $textfinding;
135  }
136  $text = $description;
137  }
138  } else {
139  $text = $statement['text'];
140  }
141 
142  if ($agentCall == "license") {
143  $this->groupBy = "text";
144  } else {
145  $this->groupBy = "content";
146  }
147  $groupBy = $statement[$this->groupBy];
148 
149  if (empty($comments) && array_key_exists($groupBy, $statements)) {
150  $currentFiles = &$statements[$groupBy]['files'];
151  if (!in_array($fileName, $currentFiles)) {
152  $currentFiles[] = $fileName;
153  }
154  } else {
155  $singleStatement = array(
156  "licenseId" => $licenseId,
157  "content" => convertToUTF8($content, false),
158  "text" => convertToUTF8($text, false),
159  "files" => array($fileName)
160  );
161  if ($extended) {
162  $singleStatement["comments"] = convertToUTF8($comments, false);
163  $singleStatement["risk"] = $statement['risk'];
164  }
165 
166  if (empty($comments)) {
167  $statements[$groupBy] = $singleStatement;
168  } else {
169  $statements[] = $singleStatement;
170  }
171  }
172  if (!empty($statement['textfinding']) && !empty($agentCall) && $agentCall != "license") {
173  $findings[] = array(
174  "licenseId" => $licenseId,
175  "content" => convertToUTF8($statement['textfinding'], false),
176  "text" => convertToUTF8($text, false),
177  "files" => array($fileName)
178  );
179  if ($extended) {
180  $key = array_search($statement['textfinding'], array_column($findings, 'content'));
181  $findings[$key]["comments"] = convertToUTF8($comments, false);
182  }
183  }
184  //To keep the schedular alive for large files
185  $countLoop += 1;
186  if ($countLoop % 500 == 0) {
187  $objectAgent->heartbeat(0);
188  }
189  }
190  arsort($statements);
191  if ($isUnifiedReport) {
192  arsort($findings);
193  if (!empty($objectAgent)) {
194  $actualHeartbeat = (count($statements) + count($findings));
195  $objectAgent->heartbeat($actualHeartbeat);
196  }
197  return array("userFindings" => $findings, "scannerFindings" => $statements);
198  } else {
199  $statements = array_merge($findings, $statements);
200  if (!empty($objectAgent)) {
201  $objectAgent->heartbeat(count($statements));
202  }
203  return array("statements" => array_values($statements));
204  }
205  }
206 
213  abstract protected function getStatements($uploadId, $uploadTreeTableName, $groupId=null);
214 
215  public function getCleared($uploadId, $groupId=null, $extended=true, $agentcall=null, $isUnifiedReport=false, $objectAgent)
216  {
217  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
218  $ungrupedStatements = $this->getStatements($uploadId, $uploadTreeTableName, $groupId);
219  $this->changeTreeIdsToPaths($ungrupedStatements, $uploadTreeTableName, $uploadId);
220  $statements = $this->groupStatements($ungrupedStatements, $extended, $agentcall, $isUnifiedReport, $objectAgent);
221  return $statements;
222  }
223 
224  public function getLicenseHistogramForReport($uploadId, $groupId)
225  {
226  $histogramStatements = $this->getHistogram($uploadId, $groupId);
227  return array("statements" => $histogramStatements);
228  }
229 
230  public function cJson($uploadId, $groupId=null)
231  {
232  $escapeChars = array('\\f',"\\", "/", "\"");
233  $withThisValue = array("","\\\\", "\\/", "\\\"");
234  $clearedString = str_replace($escapeChars, $withThisValue, $this->getCleared($uploadId, $groupId, false, null, false, null));
235  $json = json_encode($clearedString);
236  return str_replace('\u001b','',$json);
237  }
238 
239  public function cJsonHist($uploadId, $groupId=null)
240  {
241  $jsonHist = json_encode($this->getLicenseHistogramForReport($uploadId, $groupId));
242  return str_replace('\u001b','',str_replace('\\f','',$jsonHist));
243  }
244 }
Fossology exception.
Definition: Exception.php:25
if(!defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
getStatements($uploadId, $uploadTreeTableName, $groupId=null)