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.4.schema.json',
63  'specVersion' => '1.4',
64  'version' => 1,
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  if (!empty($bomdata['externalReferences'])) {
81  $report['externalReferences'] = $bomdata['externalReferences'];
82  }
83 
84  return $report;
85  }
86 
93  private function generateComponent(array $componentData): array
94  {
95  $component = [
96  'type' => $componentData['type'],
97  'name' => $componentData['name']
98  ];
99 
100  if (array_key_exists('version', $componentData) && !empty($componentData['version'])) {
101  $component['version'] = $componentData['version'];
102  }
103 
104  if (array_key_exists('mimeType', $componentData) && !empty($componentData['mimeType'])) {
105  $component['mime-type'] = $componentData['mimeType'];
106  }
107 
108  if (array_key_exists('bomref', $componentData) && !empty($componentData['bomref'])) {
109  $component['bom-ref'] = $componentData['bomref'];
110  }
111 
116  if (array_key_exists('scope', $componentData) && !empty($componentData['scope'])) {
117  $component['scope'] = $componentData['scope'];
118  } else {
119  $component['scope'] = 'required';
120  }
121 
122  if (array_key_exists('hashes', $componentData) && !empty($componentData['hashes'])) {
123  $component['hashes'] = $componentData['hashes'];
124  }
125 
126  if (array_key_exists('licenses', $componentData) && !empty($componentData['licenses'])) {
127  $component['licenses'] = $componentData['licenses'];
128  }
129 
130  if (array_key_exists('copyright', $componentData) && !empty($componentData['copyright'])) {
131  $component['copyright'] = $componentData['copyright'];
132  }
133 
134  if (array_key_exists('purl', $componentData) && !empty($componentData['purl'])) {
135  $component['purl'] = $componentData['purl'];
136  }
137 
138  if (array_key_exists('description', $componentData) && !empty($componentData['description'])) {
139  $component['description'] = $componentData['description'];
140  }
141 
142  if (array_key_exists('externalReferences', $componentData) && !empty($componentData['externalReferences'])) {
143  $component['externalReferences'] = $componentData['externalReferences'];
144  }
145 
146  $properties = [];
147  if (array_key_exists('acknowledgements', $componentData) && !empty($componentData['acknowledgements'])) {
148  $properties[] = [
149  'name' => 'fossology:acknowledgement',
150  'value' => $componentData['acknowledgements']
151  ];
152  }
153  if (array_key_exists('comments', $componentData) && !empty($componentData['comments'])) {
154  $properties[] = [
155  'name' => 'fossology:comment',
156  'value' => $componentData['comments']
157  ];
158  }
159  if (!empty($properties)) {
160  $component['properties'] = $properties;
161  }
162 
163  return $component;
164  }
165 
172  private function generateLicense(array $licenseData): array
173  {
174  $license = [];
175 
176  // Check license ID is a LicenseRef
177  if (array_key_exists('id', $licenseData) && !empty($licenseData['id']) &&
178  stripos($licenseData['id'], LicenseRef::SPDXREF_PREFIX) === 0) {
179  $license['expression'] = $licenseData['id'];
180  return $license;
181  }
182 
183  if (array_key_exists('id', $licenseData) && !empty($licenseData['id'])) {
184  $license['license']['id'] = $licenseData['id'];
185  } else if (array_key_exists('name', $licenseData) && !empty($licenseData['name'])) {
186  $license['license']['name'] = $licenseData['name'];
187  }
188 
189  if (array_key_exists('url', $licenseData) && !empty($licenseData['url'])) {
190  $license['license']['url'] = $licenseData['url'];
191  }
192 
193  if (array_key_exists('textContent', $licenseData) && !empty($licenseData['textContent'])) {
194  $license['license']['text'] = [
195  'content' => $licenseData['textContent'],
196  'contentType' => $licenseData['textContentType'],
197  'encoding' => 'base64'
198  ];
199  }
200 
201  return $license;
202  }
203 
211  private function generateHash(string $algorithm, string $content): array
212  {
213  return [
214  'alg' => $algorithm,
215  'content' => $content
216  ];
217  }
218 }
generateHash(string $algorithm, string $content)
Namespace used by CycloneDX agent.