FOSSology  4.7.1
Open Source License Compliance by Open Source Software
reportgenerator.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Sushant Kumar <sushantmishra02102002@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
12 namespace Fossology\CycloneDX;
13 
16 
18 {
20  private $tagNamespace = CycloneDxSettings::DEFAULT_NAMESPACE;
21 
23  private $specVersion = CycloneDxSettings::DEFAULT_VERSION;
24 
28  public function applySettings(CycloneDxSettings $settings): void
29  {
30  $this->tagNamespace = $settings->getTagNamespace();
31  $this->specVersion = $settings->getSpecVersion();
32  }
33 
34  public function getTagNamespace(): string
35  {
36  return $this->tagNamespace;
37  }
38 
39  public function getSpecVersion(): string
40  {
41  return $this->specVersion;
42  }
43 
48  private function supports(string $version): bool
49  {
50  return version_compare($this->specVersion, $version, '>=');
51  }
52 
59  public function createComponent(array $componentData): array
60  {
61  return $this->generateComponent($componentData);
62  }
63 
71  public function createHash($algorithm, $content): array
72  {
73  return $this->generateHash($algorithm, $content);
74  }
75 
82  public function createLicense(array $licenseData): array
83  {
84  return $this->generateLicense($licenseData);
85  }
86 
93  public function generateReport($bomdata): array
94  {
95  $author = ['name' => 'FOSSology Analyst'];
96  if ($this->supports('1.5')) {
97  $author['bom-ref'] = 'person-fossology-analyst';
98  }
99 
100  $report = [
101  'bomFormat' => 'CycloneDX',
102  '$schema' => 'http://cyclonedx.org/schema/bom-' . $this->specVersion . '.schema.json',
103  'specVersion' => $this->specVersion,
104  'version' => 1,
105  'serialNumber' => 'urn:uuid:'. uuid_create(UUID_TYPE_TIME),
106  'metadata' => [
107  'timestamp' => date('c'),
108  'tools' => $this->generateTools($bomdata['tool-version']),
109  'authors' => [$author],
110  'component' => $bomdata['maincomponent']
111  ],
112  'components' => $bomdata['components']
113  ];
114 
115  // citations point at bom-refs of the analyst and the scanner tool
116  if ($this->supports('1.7') && !empty($bomdata['citations'])) {
117  $report['citations'] = $bomdata['citations'];
118  }
119 
120  if (!empty($bomdata['externalReferences'])) {
121  $report['externalReferences'] = $bomdata['externalReferences'];
122  }
123 
124  return $report;
125  }
126 
131  private function generateTools($toolVersion): array
132  {
133  $tools = [
134  ['name' => 'FOSSology', 'bom-ref' => 'tool-fossology'],
135  ['name' => 'FOSSology Scanners', 'bom-ref' => 'tool-fossology-scanners']
136  ];
137  if (!$this->supports('1.5')) {
138  return array_map(function ($tool) use ($toolVersion) {
139  return ['vendor' => 'FOSSology', 'name' => $tool['name'], 'version' => $toolVersion];
140  }, $tools);
141  }
142  $components = [];
143  foreach ($tools as $tool) {
144  $component = ['type' => 'application'];
145  if ($this->supports('1.6')) {
146  $component['manufacturer'] = ['name' => 'FOSSology'];
147  }
148  $components[] = $component + [
149  'name' => $tool['name'],
150  'version' => $toolVersion,
151  'bom-ref' => $tool['bom-ref']
152  ];
153  }
154  return ['components' => $components];
155  }
156 
163  private function generateComponent(array $componentData): array
164  {
165  $component = [
166  'type' => $componentData['type'],
167  'name' => $componentData['name']
168  ];
169 
170  if (array_key_exists('version', $componentData) && !empty($componentData['version'])) {
171  $component['version'] = $componentData['version'];
172  }
173 
174  if (array_key_exists('mimeType', $componentData) && !empty($componentData['mimeType'])) {
175  $component['mime-type'] = $componentData['mimeType'];
176  }
177 
178  if (array_key_exists('bomref', $componentData) && !empty($componentData['bomref'])) {
179  $component['bom-ref'] = $componentData['bomref'];
180  }
181 
186  if (array_key_exists('scope', $componentData) && !empty($componentData['scope'])) {
187  $component['scope'] = $componentData['scope'];
188  } else {
189  $component['scope'] = 'required';
190  }
191 
192  if (array_key_exists('hashes', $componentData) && !empty($componentData['hashes'])) {
193  $component['hashes'] = $componentData['hashes'];
194  }
195 
196  if (array_key_exists('licenses', $componentData) && !empty($componentData['licenses'])) {
197  $component['licenses'] = $componentData['licenses'];
198  }
199 
200  if (array_key_exists('copyright', $componentData) && !empty($componentData['copyright'])) {
201  $component['copyright'] = $componentData['copyright'];
202  }
203 
204  if (array_key_exists('purl', $componentData) && !empty($componentData['purl'])) {
205  $component['purl'] = $componentData['purl'];
206  }
207 
208  if (array_key_exists('description', $componentData) && !empty($componentData['description'])) {
209  $component['description'] = $componentData['description'];
210  }
211 
212  if (array_key_exists('externalReferences', $componentData) && !empty($componentData['externalReferences'])) {
213  $component['externalReferences'] = $componentData['externalReferences'];
214  }
215 
216  $properties = [];
217  if (array_key_exists('acknowledgements', $componentData) && !empty($componentData['acknowledgements'])) {
218  $properties[] = [
219  'name' => $this->tagNamespace . 'acknowledgement',
220  'value' => $componentData['acknowledgements']
221  ];
222  }
223  if (array_key_exists('comments', $componentData) && !empty($componentData['comments'])) {
224  $properties[] = [
225  'name' => $this->tagNamespace . 'comment',
226  'value' => $componentData['comments']
227  ];
228  }
229  if (!empty($properties)) {
230  $component['properties'] = $properties;
231  }
232 
233  return $component;
234  }
235 
236  private function generateLicense(array $licenseData): array
237  {
238  $license = [];
239 
240  if (!$this->supports('1.5')) {
241  unset($licenseData['bom-ref']);
242  }
243  if (!$this->supports('1.6')) {
244  unset($licenseData['acknowledgement']);
245  }
246 
247  $isLicenseRef = !empty($licenseData['id']) &&
248  stripos($licenseData['id'], LicenseRef::SPDXREF_PREFIX) === 0;
249  // Before 1.7 an expression cannot share a licenses array with license
250  // objects, so a LicenseRef id is written as a license name there.
251  if ($isLicenseRef && !$this->supports('1.7')) {
252  $licenseData['name'] = $licenseData['id'];
253  unset($licenseData['id']);
254  } elseif ($isLicenseRef) {
255  $license['expression'] = $licenseData['id'];
256  if (array_key_exists('bom-ref', $licenseData) && !empty($licenseData['bom-ref'])) {
257  $license['bom-ref'] = $licenseData['bom-ref'];
258  }
259  if (array_key_exists('acknowledgement', $licenseData) && !empty($licenseData['acknowledgement'])) {
260  $license['acknowledgement'] = $licenseData['acknowledgement'];
261  }
262  return $license;
263  }
264 
265  if (array_key_exists('id', $licenseData) && !empty($licenseData['id'])) {
266  $license['license']['id'] = $licenseData['id'];
267  } else if (array_key_exists('name', $licenseData) && !empty($licenseData['name'])) {
268  $license['license']['name'] = $licenseData['name'];
269  }
270 
271  if (array_key_exists('bom-ref', $licenseData) && !empty($licenseData['bom-ref'])) {
272  $license['license']['bom-ref'] = $licenseData['bom-ref'];
273  }
274 
275  if (array_key_exists('acknowledgement', $licenseData) && !empty($licenseData['acknowledgement'])) {
276  $license['license']['acknowledgement'] = $licenseData['acknowledgement'];
277  }
278 
279  if (array_key_exists('url', $licenseData) && !empty($licenseData['url'])) {
280  $license['license']['url'] = $licenseData['url'];
281  }
282 
283  if (array_key_exists('textContent', $licenseData) && !empty($licenseData['textContent'])) {
284  $license['license']['text'] = [
285  'content' => $licenseData['textContent'],
286  'contentType' => $licenseData['textContentType'],
287  'encoding' => 'base64'
288  ];
289  }
290 
291  return $license;
292  }
293 
301  private function generateHash(string $algorithm, string $content): array
302  {
303  return [
304  'alg' => $algorithm,
305  'content' => $content
306  ];
307  }
308 }
generateHash(string $algorithm, string $content)
applySettings(CycloneDxSettings $settings)
CycloneDX export settings of a user (defaults) or of an upload.
Namespace used by CycloneDX agent.