FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
FO_Plugin.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2014-2017 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
26 
32 define("PLUGIN_STATE_FAIL", -1); // mark it as a total failure
33 define("PLUGIN_STATE_INVALID", 0);
34 define("PLUGIN_STATE_VALID", 1); // used during install
35 define("PLUGIN_STATE_READY", 2); // used during post-install
36 
41 define("PLUGIN_DB_NONE", 0);
42 define("PLUGIN_DB_READ", 1);
43 define("PLUGIN_DB_WRITE", 3); /* DB writes permitted */
44 define("PLUGIN_DB_CADMIN", 5); /* DB writes permitted, with additional clearing permissions */
45 define("PLUGIN_DB_ADMIN", 10); /* add/delete users */
46 
47 
48 
49 $NoneText = _("None");
50 $ReadText = _("Read");
51 $WriteText = _("Write");
52 $AdminText = _("Admin");
53 $cAdminText = _("Clearing Admin");
54 $GLOBALS['PERM_NAMES'] = array(Auth::PERM_NONE => $NoneText, Auth::PERM_READ => $ReadText, Auth::PERM_WRITE => $WriteText, Auth::PERM_ADMIN => $AdminText, Auth::PERM_CADMIN => $cAdminText);
55 
67 class FO_Plugin implements Plugin
68 {
73  var $State = PLUGIN_STATE_INVALID;
74 
79  var $Name = "";
80  var $Version = "1.0";
81  var $Title = ""; // used for HTML title tags and window menu bars
82 
86  var $DBaccess = PLUGIN_DB_NONE; /* what kind of access is needed? */
87  var $LoginFlag = 0; /* Must you be logged in to access this plugin? 1=yes, 0=no */
88 
92  var $NoMenu = 0; /* 1 = Don't show the HTML menu at the top of page */
93  var $NoHeader = 0; /* 1 = Don't show the HTML header at the top of page */
94 
103  var $PluginLevel = 10; /* used for sorting plugins -- higher comes first after dependencies are met */
104  var $Dependency = array();
105  var $InitOrder = 0;
106 
108  private $menu;
109 
111  protected $microMenu;
112 
114  protected $renderer;
115 
117  private $request;
118 
120  private $headers = array();
121 
122  protected $vars = array();
123 
156  var $MenuList = NULL;
157  var $MenuOrder = 0;
158  var $MenuTarget = NULL;
159 
178  function Install()
179  {
180  return 0;
181  }
182 
192  function Remove()
193  {
194  return;
195  }
196 
211  public function __construct()
212  {
213  $this->OutputType = $this->OutputType ?: "HTML";
214  $this->State = PLUGIN_STATE_VALID;
215  register_plugin($this);
216 
217  global $container;
218  $this->menu = $container->get('ui.component.menu');
219  $this->microMenu = $container->get('ui.component.micromenu');
220  $this->renderer = $container->get('twig.environment');
221  }
222 
226  function Initialize()
227  {
228  return (true);
229  } // Initialize()
230 
241  function PostInitialize()
242  {
243  if ($this->State != PLUGIN_STATE_VALID) {
244  return 0;
245  } // don't run
246 
247  if (empty($_SESSION['User']) && $this->LoginFlag) {
248  return 0;
249  }
250  // Make sure dependencies are met
251  foreach ($this->Dependency as $key => $val) {
252  $id = plugin_find_id($val);
253  if ($id < 0) {
254  $this->Destroy();
255  return (0);
256  }
257  }
258 
259  // Put your code here!
260  // If this fails, set $this->State to PLUGIN_STATE_INVALID.
261  // If it succeeds, then set $this->State to PLUGIN_STATE_READY.
262 
263  // It worked, so mark this plugin as ready.
264  $this->State = PLUGIN_STATE_READY;
265  // Add this plugin to the menu
266  if ($this->MenuList !== "") {
267  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->Name, $this->MenuTarget);
268  }
269  return ($this->State == PLUGIN_STATE_READY);
270  } // PostInitialize()
271 
281  function RegisterMenus()
282  {
283  if ($this->State != PLUGIN_STATE_READY) {
284  return (0);
285  } // don't run
286  // Add your own menu items here.
287  // E.g., menu_insert("Menu_Name::Item");
288  }
289 
296  function Destroy()
297  {
298  $this->State = PLUGIN_STATE_INVALID;
299  }
300 
306  /* Possible values: Text, HTML, XML, JSON */
307  var $OutputType = "HTML";
308  var $OutputToStdout = 0;
309 
316  function OutputOpen()
317  {
318  if ($this->State != PLUGIN_STATE_READY) {
319  return (0);
320  }
321 
322  $this->headers['Content-type'] = 'text/html';
323  $this->headers['Pragma'] = 'no-cache';
324  $this->headers['Cache-Control'] = 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0';
325  $this->headers['Expires'] = 'Expires: Thu, 19 Nov 1981 08:52:00 GMT';
326 
327  $metadata = "<meta name='description' content='The study of Open Source'>\n";
328  $metadata .= "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n";
329 
330  $this->vars['metadata'] = $metadata;
331 
332  if (!empty($this->Title)) {
333  $this->vars['title'] = htmlentities($this->Title);
334  }
335 
336  $styles = "<link rel='stylesheet' href='css/jquery-ui.css'>\n";
337  $styles .= "<link rel='stylesheet' href='css/select2.min.css'>\n";
338  $styles .= "<link rel='stylesheet' href='css/jquery.dataTables.css'>\n";
339  $styles .= "<link rel='stylesheet' href='css/fossology.css'>\n";
340  $styles .= "<link rel='icon' type='image/x-icon' href='favicon.ico'>\n";
341  $styles .= "<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>\n";
342 
343  if ($this->NoMenu == 0) {
344  $styles .= $this->menu->OutputCSS();
345  }
346  $this->vars['styles'] = $styles;
347 
348  if ($this->NoMenu == 0) {
349  $this->vars['menu'] = $this->menu->Output($this->Title);
350  }
351 
352  global $SysConf;
353  $this->vars['versionInfo'] = array(
354  'version' => $SysConf['BUILD']['VERSION'],
355  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
356  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
357  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
358  'branchName' => $SysConf['BUILD']['BRANCH']
359  );
360 
361  } // OutputOpen()
362 
369  function OutputUnSet()
370  {
371  if ($this->State != PLUGIN_STATE_READY) {
372  return 0;
373  }
374  return "";
375  }
376 
380  function getResponse()
381  {
382  ob_start();
383  $output = $this->Output();
384 
385  if ($output instanceof Response) {
386  $response = $output;
387  } else {
388  if (empty($this->vars['content']) && $output) {
389  $this->vars['content'] = $output;
390  } elseif (empty($this->vars['content'])) {
391  $this->vars['content'] = ob_get_contents();
392  }
393  $response = $this->render($this->getTemplateName());
394  }
395  ob_end_clean();
396 
397  return $response;
398  }
399 
400 
407  function Output()
408  {
409  return new Response("ERROR: Output() method of FO_Plugin not defined in class '" . get_class($this) . "'", Response::HTTP_INTERNAL_SERVER_ERROR);
410  }
411 
412  public function getTemplateName()
413  {
414  return "include/base.html.twig";
415  }
416 
422  public function renderString($templateName, $vars = null)
423  {
424  return $this->renderer->loadTemplate($templateName)->render($vars ?: $this->vars);
425  }
426 
432  public function renderScripts($scripts)
433  {
434  $this->vars['scripts'] = $scripts;
435  }
436 
442  protected function render($templateName, $vars = null)
443  {
444  $content = $this->renderString($templateName, $vars);
445 
446  return new Response(
447  $content,
448  Response::HTTP_OK,
449  $this->headers
450  );
451  }
452 
456  public function getRequest()
457  {
458  if (!isset($this->request)) {
459  $this->request = Request::createFromGlobals();
460  }
461  return $this->request;
462  }
463 
464  public function execute()
465  {
466  $this->OutputOpen();
467  $response = $this->getResponse();
468  $response->prepare($this->getRequest());
469  $response->send();
470  }
471 
472  function preInstall()
473  {
474  if ($this->State == PLUGIN_STATE_VALID) {
475  $this->PostInitialize();
476  }
477  if ($this->State == PLUGIN_STATE_READY) {
478  $this->RegisterMenus();
479  }
480  }
481 
482  function postInstall()
483  {
484  $state = $this->Install();
485  if ($state != 0) {
486  throw new Exception("install of plugin " . $this->Name . " failed");
487  }
488  }
489 
490  function unInstall()
491  {
492  $this->Destroy();
493  }
494 
495  public function getName()
496  {
497  return $this->Name;
498  }
499 
500  function __toString()
501  {
502  return getStringRepresentation(get_object_vars($this), get_class($this));
503  }
504 }
Remove()
This function (when defined) is only called once, when the plugin is removed. It should uninstall and...
Definition: FO_Plugin.php:192
#define PERM_NONE
User has no permission (not logged in)
Definition: libfossology.h:43
renderScripts($scripts)
Render JavaScript in the template&#39;s footer.
Definition: FO_Plugin.php:432
__construct()
base constructor. Most plugins will just use this
Definition: FO_Plugin.php:211
RegisterMenus()
While menus can be added to any time at or after the PostInitialize phase, this is the standard locat...
Definition: FO_Plugin.php:281
getStringRepresentation($vars, $classname)
OutputUnSet()
Similar to OutputClose, this ends the output type for this object. However, this does NOT change any ...
Definition: FO_Plugin.php:369
Definition: state.hpp:26
renderString($templateName, $vars=null)
Definition: FO_Plugin.php:422
Initialize()
dummy stub till all references are removed.
Definition: FO_Plugin.php:226
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
Definition: libfossology.h:48
PostInitialize()
This function is called before the plugin is used and after all plugins have been initialized...
Definition: FO_Plugin.php:241
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
#define PERM_READ
Read-only permission.
Definition: libfossology.h:44
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...
OutputOpen()
This function is called when user output is requested. This function is responsible for assigning hea...
Definition: FO_Plugin.php:316
Output()
This function is called when user output is requested. This function is responsible for content...
Definition: FO_Plugin.php:407
Install()
This function (when defined) is only called when the plugin is first installed. It should make sure a...
Definition: FO_Plugin.php:178
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:30
Destroy()
This is a destructor called after the plugin is no longer needed. It should assume that PostInitializ...
Definition: FO_Plugin.php:296
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:46
render($templateName, $vars=null)
Definition: FO_Plugin.php:442
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45