FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
oneshot.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2010-2014 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***********************************************************/
25 define("TITLE_AGENT_COPYRIGHT_ONCE", _("One-Shot Copyright/Email/URL Analysis"));
26 
32 
33  function __construct()
34  {
35  $this->Name = "agent_copyright_once";
36  $this->Title = TITLE_AGENT_COPYRIGHT_ONCE;
37  $this->Version = "1.0";
38  //$this->Dependency = array("browse", "view");
39  $this->DBaccess = PLUGIN_DB_NONE;
40  $this->LoginFlag = 0;
41  $this->NoMenu = 0;
42  $this->NoHTML = 0;
43 
44  parent::__construct();
45 
46  global $container;
47  $this->uploadDao = $container->get('dao.upload');
48  $this->copyrightDao = $container->get('dao.copyright');
49  }
50 
55  function AnalyzeOne() {
56  global $Plugins;
57  global $SYSCONFDIR;
58  $ModBack = GetParm("modback",PARM_STRING);
59  $copyright_array = array();
60  $V = "";
61 
63  $view = & $Plugins[plugin_find_id("view") ];
64  $tempFileName = $_FILES['licfile']['tmp_name'];
65  $ui_dir = getcwd();
66  $copyright_dir = "$SYSCONFDIR/mods-enabled/copyright/agent/";
67  if(!chdir($copyright_dir)) {
68  $errmsg = _("unable to change working directory to $copyright_dir\n");
69  return $errmsg;
70  }
71  //$Sys = "./copyright -C $tempFileName -c $SYSCONFDIR";
72  $Sys = "./copyright -c $SYSCONFDIR $tempFileName";
73 
74  $inputFile = popen($Sys, "r");
75  $colors = array();
76  $colors['statement'] = 0;
77  $colors['email'] = 1;
78  $colors['url'] = 2;
79  $stuff = array();
80  $stuff['statement'] = array();
81  $stuff['email'] = array();
82  $stuff['url'] = array();
83  $realline = "";
84 
85  $highlights = array();
86 
87  $typeToHighlightTypeMap = array(
88  'statement' => Highlight::COPYRIGHT,
89  'email' => Highlight::EMAIL,
90  'url' => Highlight::URL);
91  while (!feof($inputFile)) {
92  $Line = fgets($inputFile);
93  if ($Line[0] == '/') { continue;
94  }
95  $count = strlen($Line);
96  if ($count > 0) {
98  if ((($count > 1) && ("'" != $Line[$count - 2])) || ((1 == $count) && ("'" != $Line[$count - 1])))
99  {
100  $Line = str_replace("\n", ' ', $Line); // in order to preg_match_all correctly, replace NL with white space
101  $realline .= $Line;
102  continue;
103  }
104  $realline .= $Line;
105  //print "<br>realline$realline<br>";
106  $match = array();
107  preg_match_all("/\t\[(?P<start>\d+)\:(?P<end>\d+)\:(?P<type>[A-Za-z]+)\] \'(?P<content>.+)\'/", $realline, $match);
108  //print_r($match);
109  if (!empty($match['start'])) {
110  $stuff[$match['type'][0]][] = $match['content'][0];
111  if ($this->NoHTML) { // For REST API
112  array_push($copyright_array, $match['content'][0]);
113  } else {
114  $highlights[] = new Highlight($match['start'][0], $match['end'][0], $typeToHighlightTypeMap[$match['type'][0]], -1, -1, $match['content'][0]);
115  }
116  }
117  }
118  $realline = "";
119  }
120  pclose($inputFile);
121 
122  if ($this->NoHTML) // For REST API:
123  {
124  return $copyright_array;
125  }
126 
127  $inputFile = fopen($tempFileName, "r");
128  if ($inputFile) {
129  $V = $view->getView($inputFile, $ModBack, 0, NULL, $highlights); // do not show Header and micro menus
130  fclose($inputFile);
131  }
132  if(!chdir($ui_dir)) {
133  $errmsg = _("unable to change back to working directory $ui_dir\n");
134  return $errmsg;
135  }
136  /* Clean up */
137  return ($V);
138  } // AnalyzeOne()
139 
147  function RegisterMenus() {
148  if ($this->State != PLUGIN_STATE_READY) {
149  return (0);
150  } // don't run
151  $Highlight = GetParm('highlight', PARM_INTEGER);
152  if (empty($Hightlight)) {
153  $Highlight = 0;
154  }
155  $ShowHeader = GetParm('showheader', PARM_INTEGER);
156  if (empty($ShowHeader)) {
157  $ShowHeader = 0;
158  }
159  if (GetParm("mod", PARM_STRING) == $this->Name) {
160  $ThisMod = 1;
161  }
162  else {
163  $ThisMod = 0;
164  }
165  /* Check for a wget post (wget cannot post to a variable name) */
166  if ($ThisMod && empty($_POST['licfile'])) {
167  $Fin = fopen("php://input", "r");
168  $Ftmp = tempnam(NULL, "fosslic-alo-");
169  $Fout = fopen($Ftmp, "w");
170  while (!feof($Fin)) {
171  $Line = fgets($Fin);
172  fwrite($Fout, $Line);
173  }
174  fclose($Fout);
175  if (filesize($Ftmp) > 0) {
176  $_FILES['licfile']['tmp_name'] = $Ftmp;
177  $_FILES['licfile']['size'] = filesize($Ftmp);
178  $_FILES['licfile']['unlink_flag'] = 1;
179  }
180  else {
181  unlink($Ftmp);
182  }
183  fclose($Fin);
184  }
185  if ($ThisMod && file_exists(@$_FILES['licfile']['tmp_name']) && ($Highlight != 1) && ($ShowHeader != 1)) {
186  $this->NoHTML = 1;
187  /* default header is plain text */
188  }
189  /* Only register with the menu system if the user is logged in. */
190  if (!empty($_SESSION[Auth::USER_NAME]))
191  {
192  // Debugging changes to license analysis NOTE: this comment doesn't make sense.
193  if ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE)
194  {
195  menu_insert("Main::Upload::One-Shot Copyright/Email/URL", $this->MenuOrder, $this->Name, $this->MenuTarget);
196 
197  $tooltipText = _("Copyright/Email/URL One-shot, real-time analysis");
198  $menuText = "One-Shot Copyright/Email/URL";
199  $menuPosition = 100;
200  menu_insert("View::[BREAK]", $menuPosition);
201  menu_insert("View::{$menuText}", $menuPosition + 1, $this->Name, $tooltipText);
202  menu_insert("View-Meta::[BREAK]", $menuPosition);
203  menu_insert("View-Meta::{$menuText}", $menuPosition + 1, $this->Name, $tooltipText);
204  }
205  }
206  } // RegisterMenus()
207 
212  function Output() {
213  if ($this->State != PLUGIN_STATE_READY) {
214  return;
215  }
216 
217  /* For REST API:
218  wget -qO - --post-file=myfile.c http://myserv.com/?mod=agent_copyright_once
219  */
220 
221  $tmp_name = '';
222  if (array_key_exists('licfile', $_FILES) && array_key_exists('tmp_name', $_FILES['licfile']))
223  {
224  $tmp_name = $_FILES['licfile']['tmp_name'];
225  }
226 
227  $this->vars['styles'] .= "<link rel='stylesheet' href='css/highlights.css'>\n";
228  if ($this->OutputType!='HTML' && file_exists($tmp_name))
229  {
230  $copyright_res = $this->AnalyzeOne();
231  $cont = '';
232  foreach ($copyright_res as $copyright)
233  {
234  $cont = "$copyright\n";
235  }
236  unlink($tmp_name);
237  return $cont;
238  }
239 
240  if ($this->OutputType=='HTML') {
241  /* If this is a POST, then process the request. */
242  if ($tmp_name) {
243  if ($_FILES['licfile']['size'] <= 1024 * 1024 * 10) {
244  $this->vars['content'] = $this->AnalyzeOne();
245  }
246  else {
247  $this->vars['message'] = _('file is to large for one-shot copyright analyze');
248  }
249  return;
250  }
251  $this->vars['content'] = $this->htmlContent();
252  }
253  if (array_key_exists('licfile', $_FILES) && array_key_exists('unlink_flag',$_FILES['licfile'])) {
254  unlink($tmp_name);
255  }
256  // $_FILES['licfile'] = NULL;
257  }
258 
263  protected function htmlContent()
264  {
265  $V = _("This analyzer allows you to upload a single file for copyright/email/url analysis.\n");
266  $V.= "<ul>\n";
267  $V.= "<li>" . _("The analysis is done in real-time.");
268  $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");
269  $V.= "<li>" . _("Results are <b>not</b> stored. As soon as you get your results, your uploaded file is removed from the system.\n");
270  $V.= "</ul>\n";
271  /* Display the form */
272  $V.= "<form enctype='multipart/form-data' method='post'>\n";
273  $V.= _("Select the file to upload:");
274  $V.= "<br><input name='licfile' size='60' type='file' /><br />\n";
275  $V.= "<input type='hidden' name='showheader' value='1'>";
276 
277  $text = _("Upload and scan");
278  $V.= "<p><input type='submit' value='$text'>\n";
279  $V.= "</form>\n";
280  return $V;
281  }
282 }
283 
284 $NewPlugin = new agent_copyright_once();
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_NONE
Plugin requires no DB permission.
Definition: libfossology.h:48
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
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...