FOSSology  4.6.0
Open Source License Compliance by Open Source Software
oneshot.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
14 define("TITLE_AGENT_COPYRIGHT_ONCE", _("One-Shot Copyright/Email/URL Analysis"));
15 
21 {
22 
23  function __construct()
24  {
25  $this->Name = "agent_copyright_once";
26  $this->Title = TITLE_AGENT_COPYRIGHT_ONCE;
27  $this->Version = "1.0";
28  //$this->Dependency = array("browse", "view");
29  $this->DBaccess = PLUGIN_DB_NONE;
30  $this->LoginFlag = 0;
31  $this->NoMenu = 0;
32  $this->NoHTML = 0;
33 
34  parent::__construct();
35 
36  global $container;
37  $this->uploadDao = $container->get('dao.upload');
38  $this->copyrightDao = $container->get('dao.copyright');
39  }
40 
45  function AnalyzeOne($getHighlightInfo = false, $tempFileName = null)
46  {
47  global $Plugins;
48  global $SYSCONFDIR;
49  $ModBack = GetParm("modback",PARM_STRING);
50  $copyright_array = array();
51  $V = "";
52 
54  $view = & $Plugins[plugin_find_id("view") ];
55  $tempFileName = $getHighlightInfo ? $tempFileName : $_FILES['licfile']['tmp_name'];
56  $ui_dir = getcwd();
57  $copyright_dir = "$SYSCONFDIR/mods-enabled/copyright/agent/";
58  if (!chdir($copyright_dir)) {
59  return _("unable to change working directory to $copyright_dir\n");
60  }
61  //$Sys = "./copyright -C $tempFileName -c $SYSCONFDIR";
62  $escapedSysConfDir = escapeshellarg($SYSCONFDIR);
63  $escapedTempFile = escapeshellarg($tempFileName);
64  $Sys = "./copyright -c $escapedSysConfDir $escapedTempFile";
65 
66  $inputFile = popen($Sys, "r");
67  $colors = array();
68  $colors['statement'] = 0;
69  $colors['email'] = 1;
70  $colors['url'] = 2;
71  $stuff = array();
72  $stuff['statement'] = array();
73  $stuff['email'] = array();
74  $stuff['url'] = array();
75  $realline = "";
76 
77  $highlights = array();
78 
79  $typeToHighlightTypeMap = array(
80  'statement' => Highlight::COPYRIGHT,
81  'email' => Highlight::EMAIL,
82  'url' => Highlight::URL);
83  while (!feof($inputFile)) {
84  $Line = fgets($inputFile);
85  if ($Line[0] == '/') {
86  continue;
87  }
88  $count = strlen($Line);
89  if ($count > 0) {
91  if ((($count > 1) && ("'" != $Line[$count - 2])) || ((1 == $count) && ("'" != $Line[$count - 1]))) {
92  $Line = str_replace("\n", ' ', $Line); // in order to preg_match_all correctly, replace NL with white space
93  $realline .= $Line;
94  continue;
95  }
96  $realline .= $Line;
97  //print "<br>realline$realline<br>";
98  $match = array();
99  preg_match_all("/\t\[(?P<start>\d+)\:(?P<end>\d+)\:(?P<type>[A-Za-z]+)\] \'(?P<content>.+)\'/", $realline, $match);
100  //print_r($match);
101  if (!empty($match['start'])) {
102  $stuff[$match['type'][0]][] = $match['content'][0];
103  if ($this->NoHTML) { // For REST API
104  $copyright_array[] = $match['content'][0];
105  } else {
106  $highlights[] = new Highlight($match['start'][0], $match['end'][0], $typeToHighlightTypeMap[$match['type'][0]], -1, -1, $match['content'][0]);
107  }
108  }
109  }
110  $realline = "";
111  }
112  pclose($inputFile);
113 
114  if ($getHighlightInfo) {
115  return array($copyright_array, $highlights);
116  }
117  if ($this->NoHTML) { // For REST API:
118  return $copyright_array;
119  }
120 
121  $inputFile = fopen($tempFileName, "r");
122  if ($inputFile) {
123  $V = $view->getView($inputFile, $ModBack, 0, NULL, $highlights); // do not show Header and micro menus
124  fclose($inputFile);
125  }
126  if (!chdir($ui_dir)) {
127  return _("unable to change back to working directory $ui_dir\n");
128  }
129  /* Clean up */
130  return ($V);
131  } // AnalyzeOne()
132 
140  function RegisterMenus()
141  {
142  if ($this->State != PLUGIN_STATE_READY) {
143  return (0);
144  } // don't run
145  $Highlight = GetParm('highlight', PARM_INTEGER);
146  if (empty($Hightlight)) {
147  $Highlight = 0;
148  }
149  $ShowHeader = GetParm('showheader', PARM_INTEGER);
150  if (empty($ShowHeader)) {
151  $ShowHeader = 0;
152  }
153  if (GetParm("mod", PARM_STRING) == $this->Name) {
154  $ThisMod = 1;
155  } else {
156  $ThisMod = 0;
157  }
158  /* Check for a wget post (wget cannot post to a variable name) */
159  if ($ThisMod && empty($_POST['licfile'])) {
160  $Fin = fopen("php://input", "r");
161  $Ftmp = tempnam(NULL, "fosslic-alo-");
162  $Fout = fopen($Ftmp, "w");
163  while (!feof($Fin)) {
164  $Line = fgets($Fin);
165  fwrite($Fout, $Line);
166  }
167  fclose($Fout);
168  if (filesize($Ftmp) > 0) {
169  $_FILES['licfile']['tmp_name'] = $Ftmp;
170  $_FILES['licfile']['size'] = filesize($Ftmp);
171  $_FILES['licfile']['unlink_flag'] = 1;
172  } else {
173  unlink($Ftmp);
174  }
175  fclose($Fin);
176  }
177  if ($ThisMod && file_exists(@$_FILES['licfile']['tmp_name']) && ($Highlight != 1) && ($ShowHeader != 1)) {
178  $this->NoHTML = 1;
179  /* default header is plain text */
180  }
181  /* Only register with the menu system if the user is logged in. */
182  if (!empty($_SESSION[Auth::USER_NAME])) {
183  // Debugging changes to license analysis NOTE: this comment doesn't make sense.
184  if (array_key_exists(Auth::USER_LEVEL, $_SESSION) &&
185  $_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE) {
186  menu_insert("Main::Upload::One-Shot Copyright/ Email/ URL Analysis", $this->MenuOrder, $this->Name, $this->MenuTarget);
187  }
188  }
189  } // RegisterMenus()
190 
195  function Output()
196  {
197  if ($this->State != PLUGIN_STATE_READY) {
198  return;
199  }
200 
201  /* For REST API:
202  wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_copyright_once
203  */
204 
205  $tmp_name = '';
206  if (array_key_exists('licfile', $_FILES) && array_key_exists('tmp_name', $_FILES['licfile'])) {
207  $tmp_name = $_FILES['licfile']['tmp_name'];
208  }
209 
210  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
211  if ($this->OutputType!='HTML' && file_exists($tmp_name)) {
212  $copyright_res = $this->AnalyzeOne();
213  $cont = '';
214  foreach ($copyright_res as $copyright) {
215  $cont = "$copyright\n";
216  }
217  unlink($tmp_name);
218  return $cont;
219  }
220 
221  if ($this->OutputType=='HTML') {
222  /* If this is a POST, then process the request. */
223  if ($tmp_name) {
224  if ($_FILES['licfile']['size'] <= 1024 * 1024 * 10) {
225  $this->vars['content'] = $this->AnalyzeOne();
226  } else {
227  $this->vars['message'] = _('file is to large for one-shot copyright analyze');
228  }
229  return;
230  }
231  $this->vars['content'] = $this->htmlContent();
232  }
233  if (array_key_exists('licfile', $_FILES) && array_key_exists('unlink_flag',$_FILES['licfile'])) {
234  unlink($tmp_name);
235  }
236  // $_FILES['licfile'] = NULL;
237  }
238 
243  protected function htmlContent()
244  {
245  $V = _("This analyzer allows you to upload a single file for copyright/email/url analysis.\n");
246  $V.= "<ul>\n";
247  $V.= "<li>" . _("The analysis is done in real-time.");
248  $V.= "<li>" . _("Files that contain files are <b>not</b> unpacked. If you upload a container like a gzip file, then only that binary file will be scanned.\n");
249  $V.= "<li>" . _("Results are <b>not</b> stored. As soon as you get your results, your uploaded file is removed from the system.\n");
250  $V.= "</ul>\n";
251  /* Display the form */
252  $V.= "<form enctype='multipart/form-data' method='post'>\n";
253  $V.= _("Select the file to upload:");
254  $V.= "<br><input name='licfile' size='60' type='file' /><br />\n";
255  $V.= "<input type='hidden' name='showheader' value='1'>";
256 
257  $text = _("Upload and scan");
258  $V.= "<p><input type='submit' value='$text'>\n";
259  $V.= "</form>\n";
260  return $V;
261  }
262 }
263 
264 $NewPlugin = new agent_copyright_once();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
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_NONE
Plugin requires no DB permission.
Definition: libfossology.h:36