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 
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  $report = [
61  'bomFormat' => 'CycloneDX',
62  '$schema' => 'http://cyclonedx.org/schema/bom-1.7.schema.json',
63  'specVersion' => '1.7',
64  'version' => 1,
65  'serialNumber' => 'urn:uuid:'. uuid_create(UUID_TYPE_TIME),
66  'metadata' => [
67  'timestamp' => date('c'),
68  'tools' => [
69  'components' => [
70  [
71  'type' => 'application',
72  'manufacturer' => ['name' => 'FOSSology'],
73  'name' => 'FOSSology',
74  'version' => $bomdata['tool-version'],
75  'bom-ref' => 'tool-fossology'
76  ],
77  [
78  'type' => 'application',
79  'manufacturer' => ['name' => 'FOSSology'],
80  'name' => 'FOSSology Scanners',
81  'version' => $bomdata['tool-version'],
82  'bom-ref' => 'tool-fossology-scanners'
83  ]
84  ]
85  ],
86  'authors' => [
87  [
88  'name' => 'FOSSology Analyst',
89  'bom-ref' => 'person-fossology-analyst'
90  ]
91  ],
92  'component' => $bomdata['maincomponent']
93  ],
94  'components' => $bomdata['components']
95  ];
96 
97  if (isset($bomdata['citations']) && !empty($bomdata['citations'])) {
98  $report['citations'] = $bomdata['citations'];
99  }
100 
101  if (!empty($bomdata['externalReferences'])) {
102  $report['externalReferences'] = $bomdata['externalReferences'];
103  }
104 
105  return $report;
106  }
107 
114  private function generateComponent(array $componentData): array
115  {
116  $component = [
117  'type' => $componentData['type'],
118  'name' => $componentData['name']
119  ];
120 
121  if (array_key_exists('version', $componentData) && !empty($componentData['version'])) {
122  $component['version'] = $componentData['version'];
123  }
124 
125  if (array_key_exists('mimeType', $componentData) && !empty($componentData['mimeType'])) {
126  $component['mime-type'] = $componentData['mimeType'];
127  }
128 
129  if (array_key_exists('bomref', $componentData) && !empty($componentData['bomref'])) {
130  $component['bom-ref'] = $componentData['bomref'];
131  }
132 
137  if (array_key_exists('scope', $componentData) && !empty($componentData['scope'])) {
138  $component['scope'] = $componentData['scope'];
139  } else {
140  $component['scope'] = 'required';
141  }
142 
143  if (array_key_exists('hashes', $componentData) && !empty($componentData['hashes'])) {
144  $component['hashes'] = $componentData['hashes'];
145  }
146 
147  if (array_key_exists('licenses', $componentData) && !empty($componentData['licenses'])) {
148  $component['licenses'] = $componentData['licenses'];
149  }
150 
151  if (array_key_exists('copyright', $componentData) && !empty($componentData['copyright'])) {
152  $component['copyright'] = $componentData['copyright'];
153  }
154 
155  if (array_key_exists('purl', $componentData) && !empty($componentData['purl'])) {
156  $component['purl'] = $componentData['purl'];
157  }
158 
159  if (array_key_exists('description', $componentData) && !empty($componentData['description'])) {
160  $component['description'] = $componentData['description'];
161  }
162 
163  if (array_key_exists('externalReferences', $componentData) && !empty($componentData['externalReferences'])) {
164  $component['externalReferences'] = $componentData['externalReferences'];
165  }
166 
167  $properties = [];
168  if (array_key_exists('acknowledgements', $componentData) && !empty($componentData['acknowledgements'])) {
169  $properties[] = [
170  'name' => 'fossology:acknowledgement',
171  'value' => $componentData['acknowledgements']
172  ];
173  }
174  if (array_key_exists('comments', $componentData) && !empty($componentData['comments'])) {
175  $properties[] = [
176  'name' => 'fossology:comment',
177  'value' => $componentData['comments']
178  ];
179  }
180  if (!empty($properties)) {
181  $component['properties'] = $properties;
182  }
183 
184  return $component;
185  }
186 
187  private function generateLicense(array $licenseData): array
188  {
189  $license = [];
190 
191  // Check license ID is a LicenseRef
192  if (array_key_exists('id', $licenseData) && !empty($licenseData['id']) &&
193  stripos($licenseData['id'], LicenseRef::SPDXREF_PREFIX) === 0) {
194  $license['expression'] = $licenseData['id'];
195  if (array_key_exists('bom-ref', $licenseData) && !empty($licenseData['bom-ref'])) {
196  $license['bom-ref'] = $licenseData['bom-ref'];
197  }
198  if (array_key_exists('acknowledgement', $licenseData) && !empty($licenseData['acknowledgement'])) {
199  $license['acknowledgement'] = $licenseData['acknowledgement'];
200  }
201  return $license;
202  }
203 
204  if (array_key_exists('id', $licenseData) && !empty($licenseData['id'])) {
205  $license['license']['id'] = $licenseData['id'];
206  } else if (array_key_exists('name', $licenseData) && !empty($licenseData['name'])) {
207  $license['license']['name'] = $licenseData['name'];
208  }
209 
210  if (array_key_exists('bom-ref', $licenseData) && !empty($licenseData['bom-ref'])) {
211  $license['license']['bom-ref'] = $licenseData['bom-ref'];
212  }
213 
214  if (array_key_exists('acknowledgement', $licenseData) && !empty($licenseData['acknowledgement'])) {
215  $license['license']['acknowledgement'] = $licenseData['acknowledgement'];
216  }
217 
218  if (array_key_exists('url', $licenseData) && !empty($licenseData['url'])) {
219  $license['license']['url'] = $licenseData['url'];
220  }
221 
222  if (array_key_exists('textContent', $licenseData) && !empty($licenseData['textContent'])) {
223  $license['license']['text'] = [
224  'content' => $licenseData['textContent'],
225  'contentType' => $licenseData['textContentType'],
226  'encoding' => 'base64'
227  ];
228  }
229 
230  return $license;
231  }
232 
240  private function generateHash(string $algorithm, string $content): array
241  {
242  return [
243  'alg' => $algorithm,
244  'content' => $content
245  ];
246  }
247 }
generateHash(string $algorithm, string $content)
Namespace used by CycloneDX agent.