FOSSology  4.4.0
Open Source License Compliance by Open Source Software
agent-nomos-once.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
11 
17 define("TITLE_AGENT_NOMOS_ONCE", _("One-Shot License Analysis"));
18 
24 {
25 
26  const FILE_INPUT = 'file_input';
27  public $HighlightInfoKeywords = array();
28  public $HighlightInfoLicenses = array();
29  function __construct()
30  {
31  $this->Name = "agent_nomos_once";
32  $this->Title = TITLE_AGENT_NOMOS_ONCE;
33  $this->Dependency = array();
34  $this->NoHTML = 0; // always print text output for now
35  /* For anyone to access, without login, use: */
36  $this->DBaccess = PLUGIN_DB_READ;
37  /* To require login access, use: */
38  // public $DBaccess = PLUGIN_DB_WRITE;
39  // public $LoginFlag = 1;
40  $this->LoginFlag = 0;
41  parent::__construct();
42  }
43 
49  function AnalyzeFile($FilePath)
50  {
51  global $SYSCONFDIR;
52 
53  exec("$SYSCONFDIR/mods-enabled/nomos/agent/nomos -S $FilePath", $out, $rtn);
54  $licensesFromAgent = explode('contains license(s)', $out[0]);
55  $licenses_and_Highlight = end($licensesFromAgent);
56  $licenses = explode('Highlighting Info at', $licenses_and_Highlight);
57 
58  preg_match_all('/Keyword at (?P<position>\d+), length (?P<length>\d+),/',
59  $licenses[1], $this->HighlightInfoKeywords);
60  preg_match_all(
61  '/License #(?P<name>[^#]*)# at (?P<position>\d+), length (?P<length>\d+),/',
62  $licenses[1], $this->HighlightInfoLicenses);
63 
64  return ($licenses[0]);
65  }
66 
67  // AnalyzeFile()
72  function Install()
73  {
74  global $PG_CONN;
75  if (empty($PG_CONN)) {
76  return (1);
77  } else {
78  return (0);
79  }
80  }
81 
89  function RegisterMenus()
90  {
91  if ($this->State != PLUGIN_STATE_READY) {
92  return 0;
93  }
94  $ShowHeader = GetParm('showheader', PARM_INTEGER);
95  if (empty($ShowHeader)) {
96  $ShowHeader = 0;
97  }
98  $ThisMod = (GetParm("mod", PARM_STRING) == $this->Name) ? 1 : 0;
99  /*
100  * This if stmt is true only for wget.
101  * For wget, populate the $_FILES array, just like the UI post would do.
102  * Sets the unlink_flag if there is a temp file.
103  */
104  if ($ThisMod && empty($_POST['showheader']) &&
105  ($_SERVER['REQUEST_METHOD'] == "POST")) {
106  $Fin = fopen("php://input", "r");
107  $Ftmp = tempnam(NULL, "fosslic-alo-");
108  $Fout = fopen($Ftmp, "w");
109  while (! feof($Fin)) {
110  $Line = fgets($Fin);
111  fwrite($Fout, $Line);
112  }
113  fclose($Fin);
114  fclose($Fout);
115 
116  /*
117  * Populate _FILES from wget so the processing logic only has to look in
118  * one
119  * place wether the data came from wget or the UI
120  */
121  if (filesize($Ftmp) > 0) {
122  $_FILES['licfile']['tmp_name'] = $Ftmp;
123  $_FILES['licfile']['size'] = filesize($Ftmp);
124  $_FILES['licfile']['unlink_flag'] = 1;
125  $this->NoHTML = 1;
126  } else {
127  unlink($Ftmp);
128  /*
129  * If there is no input data, then something is wrong.
130  * For example the php POST limit is too low and prevented
131  * the data from coming through. Or there was an apache redirect,
132  * which removes the POST data.
133  */
134  $tooltipText = _(
135  "FATAL: your file did not get passed throught. Make sure this page wasn't a result of a web server redirect, or that it didn't exceed your php POST limit.");
136  echo $tooltipText;
137  }
138  }
139 
140  /* Only register with the menu system if the user is logged in. */
141  if (! empty($_SESSION[Auth::USER_NAME])) {
142  if (array_key_exists(Auth::USER_LEVEL, $_SESSION) &&
143  $_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE) {
144  menu_insert("Main::Upload::One-Shot Nomos Analysis", $this->MenuOrder,
145  $this->Name, $this->MenuTarget);
146  }
147  }
148  }
149 
150  // RegisterMenus()
151 
156  function Output()
157  {
158  if ($this->State != PLUGIN_STATE_READY) {
159  return;
160  }
161 
162  $tmp_name = '';
163  if (array_key_exists(self::FILE_INPUT, $_FILES) &&
164  array_key_exists('tmp_name', $_FILES[self::FILE_INPUT])) {
165  $tmp_name = $_FILES[self::FILE_INPUT]['tmp_name'];
166  }
167 
168  /*
169  * For REST API:
170  * wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_nomos_once
171  */
172  if ($this->OutputType != 'HTML' && file_exists($tmp_name)) {
173  echo $this->AnalyzeFile($tmp_name) . "\n";
174  unlink($tmp_name);
175  return;
176  }
177  if (file_exists($tmp_name)) {
178  $this->vars['content'] = $this->htmlAnalyzedContent($tmp_name,
179  $_FILES[self::FILE_INPUT]['name']);
180  } else if ($this->OutputType == 'HTML') {
181  return $this->render('oneshot-upload.html.twig', $this->vars);
182  }
183  if (array_key_exists('licfile', $_FILES) &&
184  array_key_exists('unlink_flag', $_FILES['licfile'])) {
185  unlink($tmp_name);
186  }
187  unset($_FILES[self::FILE_INPUT]);
188  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
189  return $this->render($this->getTemplateName(), $this->vars);
190  }
191 
198  private function htmlAnalyzedContent($tmp_name, $filename)
199  {
200  $text = _(
201  "A one shot license analysis shows the following license(s) in file");
202  $keep = "$text <em>$filename:</em> ";
203  $keep .= "<strong>" . $this->AnalyzeFile($tmp_name) . "</strong><br>";
204  $this->vars['message'] = $keep;
205 
206  global $Plugins;
208  $view = & $Plugins[plugin_find_id("view")];
209  $ModBack = GetParm("modback", PARM_STRING);
210 
211  $highlights = array();
212 
213  for ($index = 0; $index < count($this->HighlightInfoKeywords['position']); $index ++) {
214  $position = $this->HighlightInfoKeywords['position'][$index];
215  $length = $this->HighlightInfoKeywords['length'][$index];
216 
217  $highlights[] = new Highlight($position, $position + $length,
218  Highlight::KEYWORD);
219  }
220 
221  for ($index = 0; $index < count($this->HighlightInfoLicenses['position']); $index ++) {
222  $position = $this->HighlightInfoLicenses['position'][$index];
223  $length = $this->HighlightInfoLicenses['length'][$index];
224  $name = $this->HighlightInfoLicenses['name'][$index];
225 
226  $highlights[] = new Highlight($position, $position + $length,
227  Highlight::SIGNATURE, $name);
228  }
229 
230  $inputFile = fopen($tmp_name, "r");
231  if ($inputFile) {
232  $rtn = $view->getView($inputFile, $ModBack, 0, NULL, $highlights);
233  fclose($inputFile);
234  return $rtn;
235  }
236  }
237 }
238 
239 $NewPlugin = new agent_nomos_once();
240 $NewPlugin->Install();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
Class to run one-shot nomos.
const FILE_INPUT
Resource key for input file.
Output()
Generate the text for this plugin.
$HighlightInfoLicenses
Highlight info for licenses.
$HighlightInfoKeywords
Highlight info for keywords.
Install()
This function (when defined) is only called when the plugin is first installed. It should make sure a...
__construct()
base constructor. Most plugins will just use this
AnalyzeFile($FilePath)
Analyze one uploaded file.
RegisterMenus()
Change the type of output based on user-supplied parameters.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN