FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
agent-nomos-once.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2015 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 ***********************************************************/
21 
27 define("TITLE_AGENT_NOMOS_ONCE", _("One-Shot License Analysis"));
28 
34 {
35 
36  const FILE_INPUT = 'file_input';
37  public $HighlightInfoKeywords = array();
38  public $HighlightInfoLicenses = array();
39  function __construct()
40  {
41  $this->Name = "agent_nomos_once";
42  $this->Title = TITLE_AGENT_NOMOS_ONCE;
43  $this->Dependency = array();
44  $this->NoHTML = 0; // always print text output for now
45  /* For anyone to access, without login, use: */
46  $this->DBaccess = PLUGIN_DB_READ;
47  /* To require login access, use: */
48  // public $DBaccess = PLUGIN_DB_WRITE;
49  // public $LoginFlag = 1;
50  $this->LoginFlag = 0;
51  parent::__construct();
52  }
53 
59  function AnalyzeFile($FilePath)
60  {
61  global $SYSCONFDIR;
62 
63  exec("$SYSCONFDIR/mods-enabled/nomos/agent/nomos -S $FilePath", $out, $rtn);
64  $licensesFromAgent = explode('contains license(s)', $out[0]);
65  $licenses_and_Highlight = end($licensesFromAgent);
66  $licenses = explode('Highlighting Info at', $licenses_and_Highlight);
67 
68  preg_match_all('/Keyword at (?P<position>\d+), length (?P<length>\d+),/',
69  $licenses[1], $this->HighlightInfoKeywords);
70  preg_match_all(
71  '/License #(?P<name>[^#]*)# at (?P<position>\d+), length (?P<length>\d+),/',
72  $licenses[1], $this->HighlightInfoLicenses);
73 
74  return ($licenses[0]);
75  }
76 
77  // AnalyzeFile()
82  function Install()
83  {
84  global $PG_CONN;
85  if (empty($PG_CONN)) {
86  return (1);
87  } else {
88  return (0);
89  }
90  }
91 
99  function RegisterMenus()
100  {
101  if ($this->State != PLUGIN_STATE_READY) {
102  return 0;
103  }
104  $ShowHeader = GetParm('showheader', PARM_INTEGER);
105  if (empty($ShowHeader)) {
106  $ShowHeader = 0;
107  }
108  $ThisMod = (GetParm("mod", PARM_STRING) == $this->Name) ? 1 : 0;
109  /*
110  * This if stmt is true only for wget.
111  * For wget, populate the $_FILES array, just like the UI post would do.
112  * Sets the unlink_flag if there is a temp file.
113  */
114  if ($ThisMod && empty($_POST['showheader']) &&
115  ($_SERVER['REQUEST_METHOD'] == "POST")) {
116  $Fin = fopen("php://input", "r");
117  $Ftmp = tempnam(NULL, "fosslic-alo-");
118  $Fout = fopen($Ftmp, "w");
119  while (! feof($Fin)) {
120  $Line = fgets($Fin);
121  fwrite($Fout, $Line);
122  }
123  fclose($Fin);
124  fclose($Fout);
125 
126  /*
127  * Populate _FILES from wget so the processing logic only has to look in
128  * one
129  * place wether the data came from wget or the UI
130  */
131  if (filesize($Ftmp) > 0) {
132  $_FILES['licfile']['tmp_name'] = $Ftmp;
133  $_FILES['licfile']['size'] = filesize($Ftmp);
134  $_FILES['licfile']['unlink_flag'] = 1;
135  $this->NoHTML = 1;
136  } else {
137  unlink($Ftmp);
138  /*
139  * If there is no input data, then something is wrong.
140  * For example the php POST limit is too low and prevented
141  * the data from coming through. Or there was an apache redirect,
142  * which removes the POST data.
143  */
144  $tooltipText = _(
145  "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.");
146  echo $tooltipText;
147  }
148  }
149 
150  /* Only register with the menu system if the user is logged in. */
151  if (! empty($_SESSION['User'])) {
152  if ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE) {
153  menu_insert("Main::Upload::One-Shot Analysis", $this->MenuOrder,
154  $this->Name, $this->MenuTarget);
155  }
156  // Debugging changes to license analysis
157  if ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_ADMIN) {
158  $URI = $this->Name . Traceback_parm_keep(array(
159  "format",
160  "item"
161  ));
162  $menuText = "One-Shot License";
163  $menuPosition = 100;
164  $tooltipText = _("One-shot License, real-time license analysis");
165  menu_insert("View::[BREAK]", $menuPosition);
166  menu_insert("View::{$menuText}", $menuPosition + 1, $URI, $tooltipText);
167  menu_insert("View-Meta::[BREAK]", $menuPosition);
168  menu_insert("View-Meta::{$menuText}", $menuPosition + 1, $URI,
169  $tooltipText);
170  }
171  }
172  }
173 
174  // RegisterMenus()
175 
180  function Output()
181  {
182  if ($this->State != PLUGIN_STATE_READY) {
183  return;
184  }
185 
186  $tmp_name = '';
187  if (array_key_exists(self::FILE_INPUT, $_FILES) &&
188  array_key_exists('tmp_name', $_FILES[self::FILE_INPUT])) {
189  $tmp_name = $_FILES[self::FILE_INPUT]['tmp_name'];
190  }
191 
192  /*
193  * For REST API:
194  * wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_nomos_once
195  */
196  if ($this->OutputType != 'HTML' && file_exists($tmp_name)) {
197  echo $this->AnalyzeFile($tmp_name) . "\n";
198  unlink($tmp_name);
199  return;
200  }
201  if (file_exists($tmp_name)) {
202  $this->vars['content'] = $this->htmlAnalyzedContent($tmp_name,
203  $_FILES[self::FILE_INPUT]['name']);
204  } else if ($this->OutputType == 'HTML') {
205  return $this->render('oneshot-upload.html.twig', $this->vars);
206  }
207  if (array_key_exists('licfile', $_FILES) &&
208  array_key_exists('unlink_flag', $_FILES['licfile'])) {
209  unlink($tmp_name);
210  }
211  unset($_FILES[self::FILE_INPUT]);
212  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
213  return $this->render($this->getTemplateName(), $this->vars);
214  }
215 
222  private function htmlAnalyzedContent($tmp_name, $filename)
223  {
224  $text = _(
225  "A one shot license analysis shows the following license(s) in file");
226  $keep = "$text <em>$filename:</em> ";
227  $keep .= "<strong>" . $this->AnalyzeFile($tmp_name) . "</strong><br>";
228  $this->vars['message'] = $keep;
229 
230  global $Plugins;
232  $view = & $Plugins[plugin_find_id("view")];
233  $ModBack = GetParm("modback", PARM_STRING);
234 
235  $highlights = array();
236 
237  for ($index = 0; $index < count($this->HighlightInfoKeywords['position']); $index ++) {
238  $position = $this->HighlightInfoKeywords['position'][$index];
239  $length = $this->HighlightInfoKeywords['length'][$index];
240 
241  $highlights[] = new Highlight($position, $position + $length,
242  Highlight::KEYWORD);
243  }
244 
245  for ($index = 0; $index < count($this->HighlightInfoLicenses['position']); $index ++) {
246  $position = $this->HighlightInfoLicenses['position'][$index];
247  $length = $this->HighlightInfoLicenses['length'][$index];
248  $name = $this->HighlightInfoLicenses['name'][$index];
249 
250  $highlights[] = new Highlight($position, $position + $length,
251  Highlight::SIGNATURE, $name);
252  }
253 
254  $inputFile = fopen($tmp_name, "r");
255  if ($inputFile) {
256  $rtn = $view->getView($inputFile, $ModBack, 0, NULL, $highlights);
257  fclose($inputFile);
258  return $rtn;
259  }
260  }
261 }
262 
263 $NewPlugin = new agent_nomos_once();
264 $NewPlugin->Install();
$HighlightInfoLicenses
Highlight info for licenses.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
Output()
Generate the text for this plugin.
AnalyzeFile($FilePath)
Analyze one uploaded file.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
const FILE_INPUT
Resource key for input file.
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
RegisterMenus()
Change the type of output based on user-supplied parameters.
const PARM_INTEGER
Definition: common-parm.php:25
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
$HighlightInfoKeywords
Highlight info for keywords.
Class to run one-shot nomos.
Install()
This function (when defined) is only called when the plugin is first installed. It should make sure a...
render($templateName, $vars=null)
Definition: FO_Plugin.php:442