FOSSology  4.4.0
Open Source License Compliance by Open Source Software
readmeoss.php
Go to the documentation of this file.
1 <?php
2 /*
3  Author: Daniele Fognini, Shaheem Azmal M MD
4  SPDX-FileCopyrightText: © 2016-2018 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
44 
45 include_once(__DIR__ . "/version.php");
46 
52 class ReadmeOssAgent extends Agent
53 {
54  const UPLOAD_ADDS = "uploadsAdd";
55 
60 
65 
70 
74  private $reportutils;
75 
79  private $uploadDao;
80 
84  protected $additionalUploadIds = array();
85 
86  function __construct()
87  {
88  $this->cpClearedGetter = new XpClearedGetter("copyright", "statement");
89  $this->licenseClearedGetter = new LicenseClearedGetter();
90  $this->licenseMainGetter = new LicenseMainGetter();
91  $this->reportutils = new ReportUtils();
92 
93  parent::__construct(README_AGENT_NAME, AGENT_VERSION, AGENT_REV);
94 
95  $this->uploadDao = $this->container->get('dao.upload');
96 
97  $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.':';
98  }
99 
105  function processUploadId($uploadId)
106  {
108 
109  $args = $this->args;
110  $this->additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,$args) ? explode(',',$args[self::UPLOAD_ADDS]) : array();
111  $uploadIds = $this->additionalUploadIds;
112  array_unshift($uploadIds, $uploadId);
113 
114  $this->heartbeat(0);
115  $licenseStmts = array();
116  $copyrightStmts = array();
117  $licenseStmtsMain = array();
118  $licenseAcknowledgements = array();
119 
120  foreach ($uploadIds as $addUploadId) {
121  if (!$this->uploadDao->isAccessible($addUploadId, $groupId)) {
122  continue;
123  }
124  $moreLicenses = $this->licenseClearedGetter->getCleared($addUploadId, $this, $groupId, true, "license", false);
125  $licenseStmts = array_merge($licenseStmts, $moreLicenses['statements']);
126  $this->heartbeat(count($moreLicenses['statements']));
127  $this->licenseClearedGetter->setOnlyAcknowledgements(true);
128  $moreAcknowledgements = $this->licenseClearedGetter->getCleared($addUploadId, $this, $groupId, true, "license", false);
129  $licenseAcknowledgements = array_merge($licenseAcknowledgements, $moreAcknowledgements['statements']);
130  $this->heartbeat(count($moreAcknowledgements['statements']));
131  $moreCopyrights = $this->cpClearedGetter->getCleared($addUploadId, $this, $groupId, true, "copyright", false);
132  $copyrightStmts = array_merge($copyrightStmts, $moreCopyrights['statements']);
133  $this->heartbeat(count($moreCopyrights['statements']));
134  $moreMainLicenses = $this->licenseMainGetter->getCleared($addUploadId, $this, $groupId, true, null, false);
135  $licenseStmtsMain = array_merge($licenseStmtsMain, $moreMainLicenses['statements']);
136  $this->heartbeat(count($moreMainLicenses['statements']));
137  }
138  list($licenseStmtsMain, $licenseStmts) = $this->licenseClearedGetter->updateIdentifiedGlobalLicenses($licenseStmtsMain, $licenseStmts);
139  $contents = array('licensesMain'=>$licenseStmtsMain, 'licenses'=>$licenseStmts, 'copyrights'=>$copyrightStmts, 'licenseAcknowledgements' => $licenseAcknowledgements);
140  $this->writeReport($contents, $uploadId);
141 
142  return true;
143  }
144 
150  private function writeReport($contents, $uploadId)
151  {
152  global $SysConf;
153 
154  $packageName = $this->uploadDao->getUpload($uploadId)->getFilename();
155 
156  $fileBase = $SysConf['FOSSOLOGY']['path']."/report/";
157  $fileName = $fileBase. "ReadMe_OSS_".$packageName.".txt" ;
158 
159  foreach ($this->additionalUploadIds as $addUploadId) {
160  $packageName .= ', ' . $this->uploadDao->getUpload($addUploadId)->getFilename();
161  }
162 
163  if (!is_dir($fileBase)) {
164  mkdir($fileBase, 0777, true);
165  }
166  umask(0133);
167  $message = $this->generateReport($contents, $packageName);
168 
169  file_put_contents($fileName, $message);
170 
171  $this->updateReportTable($uploadId, $this->jobId, $fileName);
172  }
173 
180  private function updateReportTable($uploadId, $jobId, $filename)
181  {
182  $this->reportutils->updateOrInsertReportgenEntry($uploadId, $jobId, $filename);
183  }
184 
193  private function createReadMeOSSFormat($addSeparator, $dataForReadME, $extract, $break)
194  {
195  $outData = "";
196  foreach ($dataForReadME as $statements) {
197  if ($extract == 'text') {
198  $outData .= $statements["content"] . $break;
199  }
200  $outData .= str_replace("\n", "\r\n", $statements[$extract]) . $break;
201  if (!empty($addSeparator)) {
202  $outData .= $addSeparator . $break;
203  }
204  }
205  return htmlspecialchars_decode($outData, ENT_DISALLOWED);
206  }
207 
215  private function generateReport($contents, $packageName)
216  {
217  $separator1 = str_repeat("=", 120);
218  $separator2 = str_repeat("-", 120);
219  $break = str_repeat("\r\n", 2);
220  $output = $separator1 . $break . $packageName . $break . $separator2 . $break;
221  if (!empty($contents['licensesMain'])) {
222  $output .= $separator1 . $break . " MAIN LICENSES " . $break . $separator2 . $break;
223  $output .= $this->createReadMeOSSFormat($separator2, $contents['licensesMain'], 'text', $break);
224  }
225  if (!empty($contents['licenses'])) {
226  $output .= $separator1 . $break . " OTHER LICENSES " . $break . $separator2 . $break;
227  $output .= $this->createReadMeOSSFormat($separator2, $contents['licenses'], 'text', $break);
228  }
229  if (!empty($contents['licenseAcknowledgements'])) {
230  $output .= $separator1 . $break . " ACKNOWLEDGEMENTS " . $break . $separator2 . $break;
231  $output .= $this->createReadMeOSSFormat($separator2, $contents['licenseAcknowledgements'], 'text', $break);
232  }
233  $copyrights = $this->createReadMeOSSFormat("", $contents['copyrights'], 'content', "\r\n");
234  if (empty($copyrights)) {
235  $output .= "<Copyright notices>";
236  $output .= $break;
237  $output .= "<notices>";
238  } else {
239  $output .= "Copyright notices";
240  $output .= $break;
241  $output .= $copyrights;
242  }
243  return $output;
244  }
245 }
246 
247 $agent = new ReadmeOssAgent();
248 $agent->scheduler_connect();
249 $agent->run_scheduler_event_loop();
250 $agent->scheduler_disconnect(0);
Structure of an Agent with all required parameters.
Definition: Agent.php:41
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
Readme_OSS agent generates list of licenses and copyrights found in an upload.
Definition: readmeoss.php:53
updateReportTable($uploadId, $jobId, $filename)
Update the report path.
Definition: readmeoss.php:180
writeReport($contents, $uploadId)
Write data to text file.
Definition: readmeoss.php:150
generateReport($contents, $packageName)
Gather all the data.
Definition: readmeoss.php:215
const UPLOAD_ADDS
The HTTP GET parameter name.
Definition: readmeoss.php:54
processUploadId($uploadId)
Given an upload ID, process the items in it.
Definition: readmeoss.php:105
createReadMeOSSFormat($addSeparator, $dataForReadME, $extract, $break)
This function lists elements of array.
Definition: readmeoss.php:193
int jobId
The id of the job.