FOSSology  4.4.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()
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 = $_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  $Sys = "./copyright -c $SYSCONFDIR $tempFileName";
63 
64  $inputFile = popen($Sys, "r");
65  $colors = array();
66  $colors['statement'] = 0;
67  $colors['email'] = 1;
68  $colors['url'] = 2;
69  $stuff = array();
70  $stuff['statement'] = array();
71  $stuff['email'] = array();
72  $stuff['url'] = array();
73  $realline = "";
74 
75  $highlights = array();
76 
77  $typeToHighlightTypeMap = array(
78  'statement' => Highlight::COPYRIGHT,
79  'email' => Highlight::EMAIL,
80  'url' => Highlight::URL);
81  while (!feof($inputFile)) {
82  $Line = fgets($inputFile);
83  if ($Line[0] == '/') {
84  continue;
85  }
86  $count = strlen($Line);
87  if ($count > 0) {
89  if ((($count > 1) && ("'" != $Line[$count - 2])) || ((1 == $count) && ("'" != $Line[$count - 1]))) {
90  $Line = str_replace("\n", ' ', $Line); // in order to preg_match_all correctly, replace NL with white space
91  $realline .= $Line;
92  continue;
93  }
94  $realline .= $Line;
95  //print "<br>realline$realline<br>";
96  $match = array();
97  preg_match_all("/\t\[(?P<start>\d+)\:(?P<end>\d+)\:(?P<type>[A-Za-z]+)\] \'(?P<content>.+)\'/", $realline, $match);
98  //print_r($match);
99  if (!empty($match['start'])) {
100  $stuff[$match['type'][0]][] = $match['content'][0];
101  if ($this->NoHTML) { // For REST API
102  $copyright_array[] = $match['content'][0];
103  } else {
104  $highlights[] = new Highlight($match['start'][0], $match['end'][0], $typeToHighlightTypeMap[$match['type'][0]], -1, -1, $match['content'][0]);
105  }
106  }
107  }
108  $realline = "";
109  }
110  pclose($inputFile);
111 
112  if ($this->NoHTML) { // For REST API:
113  return $copyright_array;
114  }
115 
116  $inputFile = fopen($tempFileName, "r");
117  if ($inputFile) {
118  $V = $view->getView($inputFile, $ModBack, 0, NULL, $highlights); // do not show Header and micro menus
119  fclose($inputFile);
120  }
121  if (!chdir($ui_dir)) {
122  return _("unable to change back to working directory $ui_dir\n");
123  }
124  /* Clean up */
125  return ($V);
126  } // AnalyzeOne()
127 
135  function RegisterMenus()
136  {
137  if ($this->State != PLUGIN_STATE_READY) {
138  return (0);
139  } // don't run
140  $Highlight = GetParm('highlight', PARM_INTEGER);
141  if (empty($Hightlight)) {
142  $Highlight = 0;
143  }
144  $ShowHeader = GetParm('showheader', PARM_INTEGER);
145  if (empty($ShowHeader)) {
146  $ShowHeader = 0;
147  }
148  if (GetParm("mod", PARM_STRING) == $this->Name) {
149  $ThisMod = 1;
150  } else {
151  $ThisMod = 0;
152  }
153  /* Check for a wget post (wget cannot post to a variable name) */
154  if ($ThisMod && empty($_POST['licfile'])) {
155  $Fin = fopen("php://input", "r");
156  $Ftmp = tempnam(NULL, "fosslic-alo-");
157  $Fout = fopen($Ftmp, "w");
158  while (!feof($Fin)) {
159  $Line = fgets($Fin);
160  fwrite($Fout, $Line);
161  }
162  fclose($Fout);
163  if (filesize($Ftmp) > 0) {
164  $_FILES['licfile']['tmp_name'] = $Ftmp;
165  $_FILES['licfile']['size'] = filesize($Ftmp);
166  $_FILES['licfile']['unlink_flag'] = 1;
167  } else {
168  unlink($Ftmp);
169  }
170  fclose($Fin);
171  }
172  if ($ThisMod && file_exists(@$_FILES['licfile']['tmp_name']) && ($Highlight != 1) && ($ShowHeader != 1)) {
173  $this->NoHTML = 1;
174  /* default header is plain text */
175  }
176  /* Only register with the menu system if the user is logged in. */
177  if (!empty($_SESSION[Auth::USER_NAME])) {
178  // Debugging changes to license analysis NOTE: this comment doesn't make sense.
179  if (array_key_exists(Auth::USER_LEVEL, $_SESSION) &&
180  $_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE) {
181  menu_insert("Main::Upload::One-Shot Copyright/ Email/ URL Analysis", $this->MenuOrder, $this->Name, $this->MenuTarget);
182  }
183  }
184  } // RegisterMenus()
185 
190  function Output()
191  {
192  if ($this->State != PLUGIN_STATE_READY) {
193  return;
194  }
195 
196  /* For REST API:
197  wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_copyright_once
198  */
199 
200  $tmp_name = '';
201  if (array_key_exists('licfile', $_FILES) && array_key_exists('tmp_name', $_FILES['licfile'])) {
202  $tmp_name = $_FILES['licfile']['tmp_name'];
203  }
204 
205  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
206  if ($this->OutputType!='HTML' && file_exists($tmp_name)) {
207  $copyright_res = $this->AnalyzeOne();
208  $cont = '';
209  foreach ($copyright_res as $copyright) {
210  $cont = "$copyright\n";
211  }
212  unlink($tmp_name);
213  return $cont;
214  }
215 
216  if ($this->OutputType=='HTML') {
217  /* If this is a POST, then process the request. */
218  if ($tmp_name) {
219  if ($_FILES['licfile']['size'] <= 1024 * 1024 * 10) {
220  $this->vars['content'] = $this->AnalyzeOne();
221  } else {
222  $this->vars['message'] = _('file is to large for one-shot copyright analyze');
223  }
224  return;
225  }
226  $this->vars['content'] = $this->htmlContent();
227  }
228  if (array_key_exists('licfile', $_FILES) && array_key_exists('unlink_flag',$_FILES['licfile'])) {
229  unlink($tmp_name);
230  }
231  // $_FILES['licfile'] = NULL;
232  }
233 
238  protected function htmlContent()
239  {
240  $V = _("This analyzer allows you to upload a single file for copyright/email/url analysis.\n");
241  $V.= "<ul>\n";
242  $V.= "<li>" . _("The analysis is done in real-time.");
243  $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");
244  $V.= "<li>" . _("Results are <b>not</b> stored. As soon as you get your results, your uploaded file is removed from the system.\n");
245  $V.= "</ul>\n";
246  /* Display the form */
247  $V.= "<form enctype='multipart/form-data' method='post'>\n";
248  $V.= _("Select the file to upload:");
249  $V.= "<br><input name='licfile' size='60' type='file' /><br />\n";
250  $V.= "<input type='hidden' name='showheader' value='1'>";
251 
252  $text = _("Upload and scan");
253  $V.= "<p><input type='submit' value='$text'>\n";
254  $V.= "</form>\n";
255  return $V;
256  }
257 }
258 
259 $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