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($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  $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 ($getHighlightInfo) {
113  return array($copyright_array, $highlights);
114  }
115  if ($this->NoHTML) { // For REST API:
116  return $copyright_array;
117  }
118 
119  $inputFile = fopen($tempFileName, "r");
120  if ($inputFile) {
121  $V = $view->getView($inputFile, $ModBack, 0, NULL, $highlights); // do not show Header and micro menus
122  fclose($inputFile);
123  }
124  if (!chdir($ui_dir)) {
125  return _("unable to change back to working directory $ui_dir\n");
126  }
127  /* Clean up */
128  return ($V);
129  } // AnalyzeOne()
130 
138  function RegisterMenus()
139  {
140  if ($this->State != PLUGIN_STATE_READY) {
141  return (0);
142  } // don't run
143  $Highlight = GetParm('highlight', PARM_INTEGER);
144  if (empty($Hightlight)) {
145  $Highlight = 0;
146  }
147  $ShowHeader = GetParm('showheader', PARM_INTEGER);
148  if (empty($ShowHeader)) {
149  $ShowHeader = 0;
150  }
151  if (GetParm("mod", PARM_STRING) == $this->Name) {
152  $ThisMod = 1;
153  } else {
154  $ThisMod = 0;
155  }
156  /* Check for a wget post (wget cannot post to a variable name) */
157  if ($ThisMod && empty($_POST['licfile'])) {
158  $Fin = fopen("php://input", "r");
159  $Ftmp = tempnam(NULL, "fosslic-alo-");
160  $Fout = fopen($Ftmp, "w");
161  while (!feof($Fin)) {
162  $Line = fgets($Fin);
163  fwrite($Fout, $Line);
164  }
165  fclose($Fout);
166  if (filesize($Ftmp) > 0) {
167  $_FILES['licfile']['tmp_name'] = $Ftmp;
168  $_FILES['licfile']['size'] = filesize($Ftmp);
169  $_FILES['licfile']['unlink_flag'] = 1;
170  } else {
171  unlink($Ftmp);
172  }
173  fclose($Fin);
174  }
175  if ($ThisMod && file_exists(@$_FILES['licfile']['tmp_name']) && ($Highlight != 1) && ($ShowHeader != 1)) {
176  $this->NoHTML = 1;
177  /* default header is plain text */
178  }
179  /* Only register with the menu system if the user is logged in. */
180  if (!empty($_SESSION[Auth::USER_NAME])) {
181  // Debugging changes to license analysis NOTE: this comment doesn't make sense.
182  if (array_key_exists(Auth::USER_LEVEL, $_SESSION) &&
183  $_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE) {
184  menu_insert("Main::Upload::One-Shot Copyright/ Email/ URL Analysis", $this->MenuOrder, $this->Name, $this->MenuTarget);
185  }
186  }
187  } // RegisterMenus()
188 
193  function Output()
194  {
195  if ($this->State != PLUGIN_STATE_READY) {
196  return;
197  }
198 
199  /* For REST API:
200  wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_copyright_once
201  */
202 
203  $tmp_name = '';
204  if (array_key_exists('licfile', $_FILES) && array_key_exists('tmp_name', $_FILES['licfile'])) {
205  $tmp_name = $_FILES['licfile']['tmp_name'];
206  }
207 
208  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
209  if ($this->OutputType!='HTML' && file_exists($tmp_name)) {
210  $copyright_res = $this->AnalyzeOne();
211  $cont = '';
212  foreach ($copyright_res as $copyright) {
213  $cont = "$copyright\n";
214  }
215  unlink($tmp_name);
216  return $cont;
217  }
218 
219  if ($this->OutputType=='HTML') {
220  /* If this is a POST, then process the request. */
221  if ($tmp_name) {
222  if ($_FILES['licfile']['size'] <= 1024 * 1024 * 10) {
223  $this->vars['content'] = $this->AnalyzeOne();
224  } else {
225  $this->vars['message'] = _('file is to large for one-shot copyright analyze');
226  }
227  return;
228  }
229  $this->vars['content'] = $this->htmlContent();
230  }
231  if (array_key_exists('licfile', $_FILES) && array_key_exists('unlink_flag',$_FILES['licfile'])) {
232  unlink($tmp_name);
233  }
234  // $_FILES['licfile'] = NULL;
235  }
236 
241  protected function htmlContent()
242  {
243  $V = _("This analyzer allows you to upload a single file for copyright/email/url analysis.\n");
244  $V.= "<ul>\n";
245  $V.= "<li>" . _("The analysis is done in real-time.");
246  $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");
247  $V.= "<li>" . _("Results are <b>not</b> stored. As soon as you get your results, your uploaded file is removed from the system.\n");
248  $V.= "</ul>\n";
249  /* Display the form */
250  $V.= "<form enctype='multipart/form-data' method='post'>\n";
251  $V.= _("Select the file to upload:");
252  $V.= "<br><input name='licfile' size='60' type='file' /><br />\n";
253  $V.= "<input type='hidden' name='showheader' value='1'>";
254 
255  $text = _("Upload and scan");
256  $V.= "<p><input type='submit' value='$text'>\n";
257  $V.= "</form>\n";
258  return $V;
259  }
260 }
261 
262 $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