FOSSology  4.4.0
Open Source License Compliance by Open Source Software
FO_Plugin.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2017 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 
21 define("PLUGIN_STATE_FAIL", -1); // mark it as a total failure
22 define("PLUGIN_STATE_INVALID", 0);
23 define("PLUGIN_STATE_VALID", 1); // used during install
24 define("PLUGIN_STATE_READY", 2); // used during post-install
25 
30 define("PLUGIN_DB_NONE", 0);
31 define("PLUGIN_DB_READ", 1);
32 define("PLUGIN_DB_WRITE", 3); /* DB writes permitted */
33 define("PLUGIN_DB_CADMIN", 5); /* DB writes permitted, with additional clearing permissions */
34 define("PLUGIN_DB_ADMIN", 10); /* add/delete users */
35 
36 
37 
38 $NoneText = _("None");
39 $ReadText = _("Read");
40 $WriteText = _("Write");
41 $AdminText = _("Admin");
42 $cAdminText = _("Clearing Admin");
43 $GLOBALS['PERM_NAMES'] = array(Auth::PERM_NONE => $NoneText, Auth::PERM_READ => $ReadText, Auth::PERM_WRITE => $WriteText, Auth::PERM_ADMIN => $AdminText, Auth::PERM_CADMIN => $cAdminText);
44 
56 class FO_Plugin implements Plugin
57 {
62  var $State = PLUGIN_STATE_INVALID;
63 
68  var $Name = "";
69  var $Version = "1.0";
70  var $Title = ""; // used for HTML title tags and window menu bars
71 
75  var $DBaccess = PLUGIN_DB_NONE; /* what kind of access is needed? */
76  var $LoginFlag = 0; /* Must you be logged in to access this plugin? 1=yes, 0=no */
77 
81  var $NoMenu = 0; /* 1 = Don't show the HTML menu at the top of page */
82  var $NoHeader = 0; /* 1 = Don't show the HTML header at the top of page */
83 
92  var $PluginLevel = 10; /* used for sorting plugins -- higher comes first after dependencies are met */
93  var $Dependency = array();
94  var $InitOrder = 0;
95 
97  private $menu;
98 
100  protected $microMenu;
101 
103  protected $renderer;
104 
106  private $request;
107 
109  private $headers = array();
110 
111  protected $vars = array();
112 
145  var $MenuList = NULL;
146  var $MenuOrder = 0;
147  var $MenuTarget = NULL;
148 
167  function Install()
168  {
169  return 0;
170  }
171 
181  function Remove()
182  {
183  return;
184  }
185 
200  public function __construct()
201  {
202  $this->OutputType = $this->OutputType ?: "HTML";
203  $this->State = PLUGIN_STATE_VALID;
204  register_plugin($this);
205 
206  global $container;
207  $this->menu = $container->get('ui.component.menu');
208  $this->microMenu = $container->get('ui.component.micromenu');
209  $this->renderer = $container->get('twig.environment');
210  }
211 
215  function Initialize()
216  {
217  return (true);
218  } // Initialize()
219 
230  function PostInitialize()
231  {
232  if ($this->State != PLUGIN_STATE_VALID) {
233  return 0;
234  } // don't run
235 
236  if (empty($_SESSION['User']) && $this->LoginFlag) {
237  return 0;
238  }
239  // Make sure dependencies are met
240  foreach ($this->Dependency as $key => $val) {
241  $id = plugin_find_id($val);
242  if ($id < 0) {
243  $this->Destroy();
244  return (0);
245  }
246  }
247 
248  // Put your code here!
249  // If this fails, set $this->State to PLUGIN_STATE_INVALID.
250  // If it succeeds, then set $this->State to PLUGIN_STATE_READY.
251 
252  // It worked, so mark this plugin as ready.
253  $this->State = PLUGIN_STATE_READY;
254  // Add this plugin to the menu
255  if ($this->MenuList !== "") {
256  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->Name, $this->MenuTarget);
257  }
258  return ($this->State == PLUGIN_STATE_READY);
259  } // PostInitialize()
260 
270  function RegisterMenus()
271  {
272  if ($this->State != PLUGIN_STATE_READY) {
273  return (0);
274  } // don't run
275  // Add your own menu items here.
276  // E.g., menu_insert("Menu_Name::Item");
277  }
278 
285  function Destroy()
286  {
287  $this->State = PLUGIN_STATE_INVALID;
288  }
289 
295  /* Possible values: Text, HTML, XML, JSON */
296  var $OutputType = "HTML";
297  var $OutputToStdout = 0;
298 
305  function OutputOpen()
306  {
307  if ($this->State != PLUGIN_STATE_READY) {
308  return (0);
309  }
310 
311  $this->headers['Content-type'] = 'text/html';
312  $this->headers['Pragma'] = 'no-cache';
313  $this->headers['Cache-Control'] = 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0';
314  $this->headers['Expires'] = 'Expires: Thu, 19 Nov 1981 08:52:00 GMT';
315 
316  $metadata = "<meta name='description' content='The study of Open Source'>\n";
317  $metadata .= "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n";
318  $metadata .= "<meta name='viewport' content='width=device-width,initial-scale=1.0'>\n";
319 
320  $this->vars['metadata'] = $metadata;
321 
322  if (!empty($this->Title)) {
323  $this->vars['title'] = htmlentities($this->Title);
324  }
325 
326  $styles = "<link rel='stylesheet' href='css/jquery-ui.css'>\n";
327  $styles .= "<link rel='stylesheet' href='css/select2.min.css'>\n";
328  $styles .= "<link rel='stylesheet' href='css/jquery.dataTables.css'>\n";
329  $styles .= "<link rel='stylesheet' href='css/fossology.css'>\n";
330  $styles .= "<link rel='stylesheet' href='css/bootstrap-icons.css'>\n";
331  $styles .= "<link rel='stylesheet' href='css/bootstrap/bootstrap.min.css'>\n";
332  $styles .= "<link rel='icon' type='image/x-icon' href='favicon.ico'>\n";
333  $styles .= "<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>\n";
334 
335  if ($this->NoMenu == 0) {
336  $styles .= $this->menu->OutputCSS();
337  }
338  $this->vars['styles'] = $styles;
339 
340  if ($this->NoMenu == 0) {
341  $this->vars['menu'] = $this->menu->Output($this->Title);
342  }
343 
344  global $SysConf;
345  $this->vars['versionInfo'] = array(
346  'version' => $SysConf['BUILD']['VERSION'],
347  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
348  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
349  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
350  'branchName' => $SysConf['BUILD']['BRANCH']
351  );
352 
353  } // OutputOpen()
354 
361  function OutputUnSet()
362  {
363  if ($this->State != PLUGIN_STATE_READY) {
364  return 0;
365  }
366  return "";
367  }
368 
372  function getResponse()
373  {
374  ob_start();
375  $output = $this->Output();
376 
377  if ($output instanceof Response) {
378  $response = $output;
379  } else {
380  if (empty($this->vars['content']) && $output) {
381  $this->vars['content'] = $output;
382  } elseif (empty($this->vars['content'])) {
383  $this->vars['content'] = ob_get_contents();
384  }
385  $response = $this->render($this->getTemplateName());
386  }
387  ob_end_clean();
388 
389  return $response;
390  }
391 
392 
399  function Output()
400  {
401  return new Response("ERROR: Output() method of FO_Plugin not defined in class '" . get_class($this) . "'", Response::HTTP_INTERNAL_SERVER_ERROR);
402  }
403 
404  public function getTemplateName()
405  {
406  return "include/base.html.twig";
407  }
408 
414  public function renderString($templateName, $vars = null)
415  {
416  return $this->renderer->load($templateName)->render($vars ?: $this->vars);
417  }
418 
424  public function renderScripts($scripts)
425  {
426  $this->vars['scripts'] = $scripts;
427  }
428 
434  protected function render($templateName, $vars = null)
435  {
436  $content = $this->renderString($templateName, $vars);
437 
438  return new Response(
439  $content,
440  Response::HTTP_OK,
441  $this->headers
442  );
443  }
444 
448  public function getRequest()
449  {
450  if (!isset($this->request)) {
451  $this->request = Request::createFromGlobals();
452  }
453  return $this->request;
454  }
455 
456  public function execute()
457  {
458  $this->OutputOpen();
459  $response = $this->getResponse();
460  $response->prepare($this->getRequest());
461  $response->send();
462  }
463 
464  function preInstall()
465  {
466  if ($this->State == PLUGIN_STATE_VALID) {
467  $this->PostInitialize();
468  }
469  if ($this->State == PLUGIN_STATE_READY) {
470  $this->RegisterMenus();
471  }
472  }
473 
474  function postInstall()
475  {
476  $state = $this->Install();
477  if ($state != 0) {
478  throw new Exception("install of plugin " . $this->Name . " failed");
479  }
480  }
481 
482  function unInstall()
483  {
484  $this->Destroy();
485  }
486 
487  public function getName()
488  {
489  return $this->Name;
490  }
491 
492  function __toString()
493  {
494  return getStringRepresentation(get_object_vars($this), get_class($this));
495  }
496 }
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: FO_Plugin.php:399
RegisterMenus()
While menus can be added to any time at or after the PostInitialize phase, this is the standard locat...
Definition: FO_Plugin.php:270
OutputOpen()
This function is called when user output is requested. This function is responsible for assigning hea...
Definition: FO_Plugin.php:305
renderString($templateName, $vars=null)
Definition: FO_Plugin.php:414
__construct()
base constructor. Most plugins will just use this
Definition: FO_Plugin.php:200
renderScripts($scripts)
Render JavaScript in the template's footer.
Definition: FO_Plugin.php:424
Install()
This function (when defined) is only called when the plugin is first installed. It should make sure a...
Definition: FO_Plugin.php:167
Remove()
This function (when defined) is only called once, when the plugin is removed. It should uninstall and...
Definition: FO_Plugin.php:181
PostInitialize()
This function is called before the plugin is used and after all plugins have been initialized....
Definition: FO_Plugin.php:230
Initialize()
dummy stub till all references are removed.
Definition: FO_Plugin.php:215
OutputUnSet()
Similar to OutputClose, this ends the output type for this object. However, this does NOT change any ...
Definition: FO_Plugin.php:361
Destroy()
This is a destructor called after the plugin is no longer needed. It should assume that PostInitializ...
Definition: FO_Plugin.php:285
render($templateName, $vars=null)
Definition: FO_Plugin.php:434
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:20
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.
getStringRepresentation($vars, $classname)
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
#define PERM_READ
Read-only permission.
Definition: libfossology.h:32
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
Definition: libfossology.h:36
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:34
#define PERM_NONE
User has no permission (not logged in)
Definition: libfossology.h:31