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  public function AnalyzeFile($FilePath, $getHighlightInfo = false)
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  if ($getHighlightInfo) {
65  return array($licenses[0], $this->HighlightInfoKeywords, $this->HighlightInfoLicenses);
66  }
67  return ($licenses[0]);
68  }
69 
70  // AnalyzeFile()
75  function Install()
76  {
77  global $PG_CONN;
78  if (empty($PG_CONN)) {
79  return (1);
80  } else {
81  return (0);
82  }
83  }
84 
92  function RegisterMenus()
93  {
94  if ($this->State != PLUGIN_STATE_READY) {
95  return 0;
96  }
97  $ShowHeader = GetParm('showheader', PARM_INTEGER);
98  if (empty($ShowHeader)) {
99  $ShowHeader = 0;
100  }
101  $ThisMod = (GetParm("mod", PARM_STRING) == $this->Name) ? 1 : 0;
102  /*
103  * This if stmt is true only for wget.
104  * For wget, populate the $_FILES array, just like the UI post would do.
105  * Sets the unlink_flag if there is a temp file.
106  */
107  if ($ThisMod && empty($_POST['showheader']) &&
108  ($_SERVER['REQUEST_METHOD'] == "POST")) {
109  $Fin = fopen("php://input", "r");
110  $Ftmp = tempnam(NULL, "fosslic-alo-");
111  $Fout = fopen($Ftmp, "w");
112  while (! feof($Fin)) {
113  $Line = fgets($Fin);
114  fwrite($Fout, $Line);
115  }
116  fclose($Fin);
117  fclose($Fout);
118 
119  /*
120  * Populate _FILES from wget so the processing logic only has to look in
121  * one
122  * place wether the data came from wget or the UI
123  */
124  if (filesize($Ftmp) > 0) {
125  $_FILES['licfile']['tmp_name'] = $Ftmp;
126  $_FILES['licfile']['size'] = filesize($Ftmp);
127  $_FILES['licfile']['unlink_flag'] = 1;
128  $this->NoHTML = 1;
129  } else {
130  unlink($Ftmp);
131  /*
132  * If there is no input data, then something is wrong.
133  * For example the php POST limit is too low and prevented
134  * the data from coming through. Or there was an apache redirect,
135  * which removes the POST data.
136  */
137  $tooltipText = _(
138  "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.");
139  echo $tooltipText;
140  }
141  }
142 
143  /* Only register with the menu system if the user is logged in. */
144  if (! empty($_SESSION[Auth::USER_NAME])) {
145  if (array_key_exists(Auth::USER_LEVEL, $_SESSION) &&
146  $_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE) {
147  menu_insert("Main::Upload::One-Shot Nomos Analysis", $this->MenuOrder,
148  $this->Name, $this->MenuTarget);
149  }
150  }
151  }
152 
153  // RegisterMenus()
154 
159  function Output()
160  {
161  if ($this->State != PLUGIN_STATE_READY) {
162  return;
163  }
164 
165  $tmp_name = '';
166  if (array_key_exists(self::FILE_INPUT, $_FILES) &&
167  array_key_exists('tmp_name', $_FILES[self::FILE_INPUT])) {
168  $tmp_name = $_FILES[self::FILE_INPUT]['tmp_name'];
169  }
170 
171  /*
172  * For REST API:
173  * wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_nomos_once
174  */
175  if ($this->OutputType != 'HTML' && file_exists($tmp_name)) {
176  echo $this->AnalyzeFile($tmp_name) . "\n";
177  unlink($tmp_name);
178  return;
179  }
180  if (file_exists($tmp_name)) {
181  $this->vars['content'] = $this->htmlAnalyzedContent($tmp_name,
182  $_FILES[self::FILE_INPUT]['name']);
183  } else if ($this->OutputType == 'HTML') {
184  return $this->render('oneshot-upload.html.twig', $this->vars);
185  }
186  if (array_key_exists('licfile', $_FILES) &&
187  array_key_exists('unlink_flag', $_FILES['licfile'])) {
188  unlink($tmp_name);
189  }
190  unset($_FILES[self::FILE_INPUT]);
191  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
192  return $this->render($this->getTemplateName(), $this->vars);
193  }
194 
201  private function htmlAnalyzedContent($tmp_name, $filename)
202  {
203  $text = _(
204  "A one shot license analysis shows the following license(s) in file");
205  $keep = "$text <em>$filename:</em> ";
206  $keep .= "<strong>" . $this->AnalyzeFile($tmp_name) . "</strong><br>";
207  $this->vars['message'] = $keep;
208 
209  global $Plugins;
211  $view = & $Plugins[plugin_find_id("view")];
212  $ModBack = GetParm("modback", PARM_STRING);
213 
214  $highlights = array();
215 
216  for ($index = 0; $index < count($this->HighlightInfoKeywords['position']); $index ++) {
217  $position = $this->HighlightInfoKeywords['position'][$index];
218  $length = $this->HighlightInfoKeywords['length'][$index];
219 
220  $highlights[] = new Highlight($position, $position + $length,
221  Highlight::KEYWORD);
222  }
223 
224  for ($index = 0; $index < count($this->HighlightInfoLicenses['position']); $index ++) {
225  $position = $this->HighlightInfoLicenses['position'][$index];
226  $length = $this->HighlightInfoLicenses['length'][$index];
227  $name = $this->HighlightInfoLicenses['name'][$index];
228 
229  $highlights[] = new Highlight($position, $position + $length,
230  Highlight::SIGNATURE, $name);
231  }
232 
233  $inputFile = fopen($tmp_name, "r");
234  if ($inputFile) {
235  $rtn = $view->getView($inputFile, $ModBack, 0, NULL, $highlights);
236  fclose($inputFile);
237  return $rtn;
238  }
239  }
240 }
241 
242 $NewPlugin = new agent_nomos_once();
243 $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.
AnalyzeFile($FilePath, $getHighlightInfo=false)
Analyze one uploaded file.
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
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