FOSSology  4.5.0-rc1
Open Source License Compliance by Open Source Software
DefaultPlugin.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2016 Siemens AG
4  Author: Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Plugin;
10 
14 use Monolog\Handler\StreamHandler;
15 use Monolog\Logger;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
19 use Symfony\Component\HttpFoundation\Session\Session;
20 use Twig_Environment;
21 
22 abstract class DefaultPlugin implements Plugin
23 {
24  const PERMISSION = "permission";
25  const REQUIRES_LOGIN = "requiresLogin";
26  const ENABLE_MENU = "ENABLE_MENU";
27  const LEVEL = "level";
28  const DEPENDENCIES = "dependencies";
29  const INIT_ORDER = "initOrder";
30  const MENU_LIST = "menuList";
31  const MENU_ORDER = "menuOrder";
32  const MENU_TARGET = "menuTarget";
33  const TITLE = "title";
34 
36  protected $container;
38  protected $renderer;
40  private $session;
42  private $logger;
44  public $fileLogger;
46  private $menu;
48  protected $microMenu;
50  private $name;
52  private $version = "1.0";
54  private $title;
56  private $permission = Auth::PERM_NONE;
58  private $requiresLogin = true;
60  private $PluginLevel = 10;
62  private $dependencies = array();
63  private $InitOrder = 0;
64 
65  private $MenuList = NULL;
66  private $MenuOrder = 0;
67  private $MenuTarget = NULL;
71  private $logdir;
72 
73  public function __construct($name, $parameters = array())
74  {
75  if ($name === null || $name === "") {
76  throw new \InvalidArgumentException("plugin requires a name");
77  }
78  $this->name = $name;
79  foreach ($parameters as $key => $value) {
80  $this->setParameter($key, $value);
81  }
82  global $SysConf;
83  if (array_key_exists('DIRECTORIES', $SysConf) && array_key_exists('LOGDIR', $SysConf['DIRECTORIES'])) {
84  $this->logdir = $SysConf['DIRECTORIES']['LOGDIR'];
85  } else {
86  $this->logdir = sys_get_temp_dir();
87  }
88  global $container;
89  $this->container = $container;
90  $this->session = $this->getObject('session');
91  $this->renderer = $this->getObject('twig.environment');
92  $this->logger = $this->getObject('logger');
93  $this->fileLogger = new Logger(get_called_class());
94  $this->fileLogger->pushHandler(new StreamHandler($this->logdir . DIRECTORY_SEPARATOR . 'plugin.log', Logger::DEBUG));
95  $this->menu = $this->getObject('ui.component.menu');
96  $this->microMenu = $this->getObject('ui.component.micromenu');
97  }
98 
99  private function setParameter($key, $value)
100  {
101  switch ($key) {
102  case self::TITLE:
103  $this->title = $value;
104  break;
105 
106  case self::PERMISSION:
107  $this->permission = $value;
108  break;
109 
110  case self::REQUIRES_LOGIN:
111  $this->requiresLogin = $value;
112  break;
113 
114  case self::LEVEL:
115  $this->PluginLevel = $value;
116  break;
117 
118  case self::DEPENDENCIES:
119  $this->dependencies = $value;
120  break;
121 
122  case self::INIT_ORDER:
123  $this->InitOrder = $value;
124  break;
125 
126  case self::MENU_LIST:
127  $this->MenuList = $value;
128  break;
129 
130  case self::MENU_ORDER:
131  $this->MenuOrder = $value;
132  break;
133 
134  case self::MENU_TARGET:
135  $this->MenuTarget = $value;
136  break;
137 
138  default:
139  throw new \Exception("unhandled parameter $key in module " . $this->name);
140  }
141  }
142 
146  public function getName()
147  {
148  return $this->name;
149  }
150 
154  public function getVersion()
155  {
156  return $this->version;
157  }
158 
162  public function getTitle()
163  {
164  return $this->title;
165  }
166 
170  public function isRequiresLogin()
171  {
172  return $this->requiresLogin;
173  }
174 
178  public function getDependency()
179  {
180  return $this->dependencies;
181  }
182 
186  public function getPluginLevel()
187  {
188  return $this->PluginLevel;
189  }
190 
194  public function getDBaccess()
195  {
196  return $this->permission;
197  }
198 
202  public function getState()
203  {
204  return PLUGIN_STATE_READY;
205  }
206 
210  public function getInitOrder()
211  {
212  return $this->InitOrder;
213  }
214 
215 
216  public function getNoMenu()
217  {
218  return 0;
219  }
220 
224  protected function RegisterMenus()
225  {
226  if (isset($this->MenuList) && (!$this->requiresLogin || $this->isLoggedIn())) {
227  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->name, $this->name);
228  }
229  }
230 
234  public function getResponse()
235  {
236  $request = Request::createFromGlobals();
237  $request->setSession($this->session);
238 
239  $this->checkPrerequisites();
240 
241  $startTime = microtime(true);
242  $response = $this->handle($request);
243  $response->prepare($request);
244  $this->logger->debug(sprintf("handle request in %.3fs", microtime(true) - $startTime));
245  return $response;
246  }
247 
252  public function getObject($name)
253  {
254  return $this->container->get($name);
255  }
256 
257  public function preInstall()
258  {
259  $this->RegisterMenus();
260  }
261 
262  public function postInstall()
263  {
264  }
265 
266  public function unInstall()
267  {
268  }
269 
270  public function execute()
271  {
272  $startTime = microtime(true);
273 
274  $response = $this->getResponse();
275 
276  $this->logger->debug(sprintf("prepare response in %.3fs", microtime(true) - $startTime));
277 
278  $response->send();
279  }
280 
285  protected abstract function handle(Request $request);
286 
293  protected function render($templateName, $vars = null, $headers = null)
294  {
295  if ($this->requiresLogin && !$this->isLoggedIn()) {
296  new Response("permission denied", Response::HTTP_FORBIDDEN, array("contentType" => "text/plain"));
297  }
298 
299  $startTime = microtime(true);
300 
301  $content = $this->renderer->load($templateName)
302  ->render($vars ?: $this->getDefaultVars());
303 
304  $this->logger->debug(sprintf("%s: render response in %.3fs", get_class($this), microtime(true) - $startTime));
305  return new Response(
306  $content,
307  Response::HTTP_OK,
308  $headers ?: $this->getDefaultHeaders()
309  );
310  }
311 
312  public function isLoggedIn()
313  {
314  return (!empty($_SESSION[Auth::USER_NAME]) && $_SESSION[Auth::USER_NAME] != 'Default User');
315  }
316 
317  private function checkPrerequisites()
318  {
319  if ($this->requiresLogin && !$this->isLoggedIn()) {
320  throw new \Exception("not allowed without login");
321  }
322 
323  foreach ($this->dependencies as $dependency) {
324  $id = plugin_find_id($dependency);
325  if ($id < 0) {
326  $this->unInstall();
327  throw new \Exception("unsatisfied dependency '$dependency' in module '" . $this->getName() . "'");
328  }
329  }
330  }
331 
335  protected function getDefaultHeaders()
336  {
337  return array(
338  'Content-type' => 'text/html',
339  'Pragma' => 'no-cache',
340  'Cache-Control' => 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
341  'Expires' => 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
342  }
343 
347  protected function getDefaultVars()
348  {
349  $vars = array();
350 
351  $metadata = "<meta name='description' content='The study of Open Source'>\n";
352  $metadata .= "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n";
353  $metadata .= "<meta name='viewport' content='width=device-width,initial-scale=1.0'>\n";
354 
355  $vars['metadata'] = $metadata;
356 
357  if (!empty($this->title)) {
358  $vars[self::TITLE] = htmlentities($this->title);
359  }
360 
361  $styles = "<link rel='stylesheet' href='css/jquery-ui.css'>\n";
362  $styles .= "<link rel='stylesheet' href='css/select2.min.css'>\n";
363  $styles .= "<link rel='stylesheet' href='css/jquery.dataTables.css'>\n";
364  $styles .= "<link rel='stylesheet' href='css/fossology.css'>\n";
365  $styles .= "<link rel='stylesheet' href='css/bootstrap/bootstrap.min.css'>\n";
366  $styles .= "<link rel='stylesheet' href='css/bootstrap-icons.css'>\n";
367  $styles .= "<link rel='icon' type='image/x-icon' href='favicon.ico'>\n";
368  $styles .= "<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>\n";
369 
370  $styles .= $this->menu->OutputCSS();
371 
372  $vars['styles'] = $styles;
373 
374  $vars['menu'] = $this->menu->Output($this->title);
375 
376  global $SysConf;
377  if (array_key_exists('BUILD', $SysConf)) {
378  $vars['versionInfo'] = array(
379  'version' => $SysConf['BUILD']['VERSION'],
380  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
381  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
382  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
383  'branchName' => $SysConf['BUILD']['BRANCH']
384  );
385  }
386 
387  return $vars;
388  }
389 
390  protected function mergeWithDefault($vars)
391  {
392  return array_merge($this->getDefaultVars(), $vars);
393  }
394 
395  protected function flushContent($content)
396  {
397  return $this->render("include/base.html.twig",$this->mergeWithDefault(array("content"=>$content)));
398  }
399 
405  public function __get($name)
406  {
407  if (method_exists($this, ($method = 'get' . ucwords($name)))) {
408  return $this->$method();
409  } else {
410  throw new \Exception("property '$name' not found in module " . $this->name);
411  }
412  }
413 
414  function __toString()
415  {
416  return getStringRepresentation(get_object_vars($this), get_class($this));
417  }
418 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
RegisterMenus()
Customize submenus.
render($templateName, $vars=null, $headers=null)
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)