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
20namespace Fossology\Lib\Report;
21
22use Fossology\Lib\Dao\TreeDao;
23use Fossology\Lib\Dao\UploadDao;
24
25abstract class ClearedGetterCommon
26{
27  /** @var UploadDao */
28  protected $uploadDao;
29
30  /** @var TreeDao */
31  protected $treeDao;
32
33  /** @var array */
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          $findings[$key]["licenseId"] = $licenseId;
183        }
184      }
185      //To keep the schedular alive for large files
186      $countLoop += 1;
187      if ($countLoop % 500 == 0) {
188        $objectAgent->heartbeat(0);
189      }
190    }
191    arsort($statements);
192    if ($agentCall == "copyright" && $isUnifiedReport) {
193      arsort($findings);
194      if (!empty($objectAgent)) {
195        $actualHeartbeat = (count($statements) + count($findings));
196        $objectAgent->heartbeat($actualHeartbeat);
197      }
198      return array("userFindings" => $findings, "scannerFindings" => $statements);
199    } else {
200      $statements = array_merge($findings, $statements);
201      if (!empty($objectAgent)) {
202        $objectAgent->heartbeat(count($statements));
203      }
204      return array("statements" => array_values($statements));
205    }
206  }
207
208  /**
209   * @param int $uploadId
210   * @param string $uploadTreeTableName
211   * @param null|int $groupId
212   * @return array
213   */
214  abstract protected function getStatements($uploadId, $uploadTreeTableName, $groupId=null);
215
216  public function getCleared($uploadId, $objectAgent, $groupId=null, $extended=true, $agentcall=null, $isUnifiedReport=false)
217  {
218    $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
219    $ungrupedStatements = $this->getStatements($uploadId, $uploadTreeTableName, $groupId);
220    $this->changeTreeIdsToPaths($ungrupedStatements, $uploadTreeTableName, $uploadId);
221    $statements = $this->groupStatements($ungrupedStatements, $extended, $agentcall, $isUnifiedReport, $objectAgent);
222    return $statements;
223  }
224
225  public function getLicenseHistogramForReport($uploadId, $groupId)
226  {
227    $histogramStatements = $this->getHistogram($uploadId, $groupId);
228    return array("statements" => $histogramStatements);
229  }
230
231  public function cJson($uploadId, $groupId=null)
232  {
233    $escapeChars = array('\\f',"\\", "/", "\"");
234    $withThisValue = array("","\\\\", "\\/", "\\\"");
235    $clearedString = str_replace($escapeChars, $withThisValue, $this->getCleared($uploadId, null, $groupId, false, null, false));
236    $json = json_encode($clearedString);
237    return str_replace('\u001b','',$json);
238  }
239
240  public function cJsonHist($uploadId, $groupId=null)
241  {
242    $jsonHist = json_encode($this->getLicenseHistogramForReport($uploadId, $groupId));
243    return str_replace('\u001b','',str_replace('\\f','',$jsonHist));
244  }
245}
246