FOSSology  4.4.0
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 
15 
17 {
24  public function createComponent(array $componentData): array
25  {
26  return $this->generateComponent($componentData);
27  }
28 
36  public function createHash($algorithm, $content): array
37  {
38  return $this->generateHash($algorithm, $content);
39  }
40 
47  public function createLicense(array $licenseData): array
48  {
49  return $this->generateLicense($licenseData);
50  }
51 
58  public function generateReport($bomdata): array
59  {
60  return [
61  'bomFormat' => 'CycloneDX',
62  '$schema' => 'http://cyclonedx.org/schema/bom-1.4.schema.json',
63  'specVersion' => '1.4',
64  'version' => 1.0,
65  'serialNumber' => 'urn:uuid:'. uuid_create(UUID_TYPE_TIME),
66  'metadata' => [
67  'timestamp' => date('c'),
68  'tools' => [
69  [
70  'vendor' => 'FOSSology',
71  'name' => 'FOSSology',
72  'version' => $bomdata['tool-version']
73  ]
74  ],
75  'component' => $bomdata['maincomponent']
76  ],
77  'components' => $bomdata['components']
78  ];
79  }
80 
87  private function generateComponent(array $componentData): array
88  {
89  $component = [
90  'type' => $componentData['type'],
91  'name' => $componentData['name']
92  ];
93 
94  if (array_key_exists('mimeType', $componentData) && !empty($componentData['mimeType'])) {
95  $component['mime-type'] = $componentData['mimeType'];
96  }
97 
98  if (array_key_exists('bomref', $componentData) && !empty($componentData['bomref'])) {
99  $component['bom-ref'] = $componentData['bomref'];
100  }
101 
106  if (array_key_exists('scope', $componentData) && !empty($componentData['scope'])) {
107  $component['scope'] = $componentData['scope'];
108  } else {
109  $component['scope'] = 'required';
110  }
111 
112  if (array_key_exists('hashes', $componentData) && !empty($componentData['hashes'])) {
113  $component['hashes'] = $componentData['hashes'];
114  }
115 
116  if (array_key_exists('licenses', $componentData) && !empty($componentData['licenses'])) {
117  $component['licenses'] = $componentData['licenses'];
118  }
119 
120  if (array_key_exists('copyright', $componentData) && !empty($componentData['copyright'])) {
121  $component['copyright'] = $componentData['copyright'];
122  }
123 
124  if (array_key_exists('description', $componentData) && !empty($componentData['description'])) {
125  $component['description'] = $componentData['description'];
126  }
127 
128  return $component;
129  }
130 
137  private function generateLicense(array $licenseData): array
138  {
139  $license = [];
140 
141  // Check license ID is a LicenseRef
142  if (array_key_exists('id', $licenseData) && !empty($licenseData['id']) &&
143  stripos($licenseData['id'], LicenseRef::SPDXREF_PREFIX) === 0) {
144  $license['expression'] = $licenseData['id'];
145  return $license;
146  }
147 
148  if (array_key_exists('id', $licenseData) && !empty($licenseData['id'])) {
149  $license['license']['id'] = $licenseData['id'];
150  } else if (array_key_exists('name', $licenseData) && !empty($licenseData['name'])) {
151  $license['license']['name'] = $licenseData['name'];
152  }
153 
154  if (array_key_exists('url', $licenseData) && !empty($licenseData['url'])) {
155  $license['license']['url'] = $licenseData['url'];
156  }
157 
158  if (array_key_exists('textContent', $licenseData) && !empty($licenseData['textContent'])) {
159  $license['license']['text'] = [
160  'content' => $licenseData['textContent'],
161  'contentType' => $licenseData['textContentType'],
162  'encoding' => 'base64'
163  ];
164  }
165 
166  return $license;
167  }
168 
176  private function generateHash(string $algorithm, string $content): array
177  {
178  return [
179  'alg' => $algorithm,
180  'content' => $content
181  ];
182  }
183 }
generateHash(string $algorithm, string $content)
Namespace used by CycloneDX agent.