FOSSology  4.4.0
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\Logger;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 use Symfony\Component\HttpFoundation\Session\Session;
19 use Twig_Environment;
20 
21 abstract class DefaultPlugin implements Plugin
22 {
23  const PERMISSION = "permission";
24  const REQUIRES_LOGIN = "requiresLogin";
25  const ENABLE_MENU = "ENABLE_MENU";
26  const LEVEL = "level";
27  const DEPENDENCIES = "dependencies";
28  const INIT_ORDER = "initOrder";
29  const MENU_LIST = "menuList";
30  const MENU_ORDER = "menuOrder";
31  const MENU_TARGET = "menuTarget";
32  const TITLE = "title";
33 
35  protected $container;
37  protected $renderer;
39  private $session;
41  private $logger;
43  private $menu;
45  protected $microMenu;
47  private $name;
49  private $version = "1.0";
51  private $title;
53  private $permission = Auth::PERM_NONE;
55  private $requiresLogin = true;
57  private $PluginLevel = 10;
59  private $dependencies = array();
60  private $InitOrder = 0;
61 
62  private $MenuList = NULL;
63  private $MenuOrder = 0;
64  private $MenuTarget = NULL;
65 
66  public function __construct($name, $parameters = array())
67  {
68  if ($name === null || $name === "") {
69  throw new \InvalidArgumentException("plugin requires a name");
70  }
71  $this->name = $name;
72  foreach ($parameters as $key => $value) {
73  $this->setParameter($key, $value);
74  }
75 
76  global $container;
77  $this->container = $container;
78  $this->session = $this->getObject('session');
79  $this->renderer = $this->getObject('twig.environment');
80  $this->logger = $this->getObject('logger');
81  $this->menu = $this->getObject('ui.component.menu');
82  $this->microMenu = $this->getObject('ui.component.micromenu');
83  }
84 
85  private function setParameter($key, $value)
86  {
87  switch ($key) {
88  case self::TITLE:
89  $this->title = $value;
90  break;
91 
92  case self::PERMISSION:
93  $this->permission = $value;
94  break;
95 
96  case self::REQUIRES_LOGIN:
97  $this->requiresLogin = $value;
98  break;
99 
100  case self::LEVEL:
101  $this->PluginLevel = $value;
102  break;
103 
104  case self::DEPENDENCIES:
105  $this->dependencies = $value;
106  break;
107 
108  case self::INIT_ORDER:
109  $this->InitOrder = $value;
110  break;
111 
112  case self::MENU_LIST:
113  $this->MenuList = $value;
114  break;
115 
116  case self::MENU_ORDER:
117  $this->MenuOrder = $value;
118  break;
119 
120  case self::MENU_TARGET:
121  $this->MenuTarget = $value;
122  break;
123 
124  default:
125  throw new \Exception("unhandled parameter $key in module " . $this->name);
126  }
127  }
128 
132  public function getName()
133  {
134  return $this->name;
135  }
136 
140  public function getVersion()
141  {
142  return $this->version;
143  }
144 
148  public function getTitle()
149  {
150  return $this->title;
151  }
152 
156  public function isRequiresLogin()
157  {
158  return $this->requiresLogin;
159  }
160 
164  public function getDependency()
165  {
166  return $this->dependencies;
167  }
168 
172  public function getPluginLevel()
173  {
174  return $this->PluginLevel;
175  }
176 
180  public function getDBaccess()
181  {
182  return $this->permission;
183  }
184 
188  public function getState()
189  {
190  return PLUGIN_STATE_READY;
191  }
192 
196  public function getInitOrder()
197  {
198  return $this->InitOrder;
199  }
200 
201 
202  public function getNoMenu()
203  {
204  return 0;
205  }
206 
210  protected function RegisterMenus()
211  {
212  if (isset($this->MenuList) && (!$this->requiresLogin || $this->isLoggedIn())) {
213  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->name, $this->name);
214  }
215  }
216 
220  public function getResponse()
221  {
222  $request = Request::createFromGlobals();
223  $request->setSession($this->session);
224 
225  $this->checkPrerequisites();
226 
227  $startTime = microtime(true);
228  $response = $this->handle($request);
229  $response->prepare($request);
230  $this->logger->debug(sprintf("handle request in %.3fs", microtime(true) - $startTime));
231  return $response;
232  }
233 
238  public function getObject($name)
239  {
240  return $this->container->get($name);
241  }
242 
243  public function preInstall()
244  {
245  $this->RegisterMenus();
246  }
247 
248  public function postInstall()
249  {
250  }
251 
252  public function unInstall()
253  {
254  }
255 
256  public function execute()
257  {
258  $startTime = microtime(true);
259 
260  $response = $this->getResponse();
261 
262  $this->logger->debug(sprintf("prepare response in %.3fs", microtime(true) - $startTime));
263 
264  $response->send();
265  }
266 
271  protected abstract function handle(Request $request);
272 
279  protected function render($templateName, $vars = null, $headers = null)
280  {
281  if ($this->requiresLogin && !$this->isLoggedIn()) {
282  new Response("permission denied", Response::HTTP_FORBIDDEN, array("contentType" => "text/plain"));
283  }
284 
285  $startTime = microtime(true);
286 
287  $content = $this->renderer->load($templateName)
288  ->render($vars ?: $this->getDefaultVars());
289 
290  $this->logger->debug(sprintf("%s: render response in %.3fs", get_class($this), microtime(true) - $startTime));
291  return new Response(
292  $content,
293  Response::HTTP_OK,
294  $headers ?: $this->getDefaultHeaders()
295  );
296  }
297 
298  public function isLoggedIn()
299  {
300  return (!empty($_SESSION[Auth::USER_NAME]) && $_SESSION[Auth::USER_NAME] != 'Default User');
301  }
302 
303  private function checkPrerequisites()
304  {
305  if ($this->requiresLogin && !$this->isLoggedIn()) {
306  throw new \Exception("not allowed without login");
307  }
308 
309  foreach ($this->dependencies as $dependency) {
310  $id = plugin_find_id($dependency);
311  if ($id < 0) {
312  $this->unInstall();
313  throw new \Exception("unsatisfied dependency '$dependency' in module '" . $this->getName() . "'");
314  }
315  }
316  }
317 
321  protected function getDefaultHeaders()
322  {
323  return array(
324  'Content-type' => 'text/html',
325  'Pragma' => 'no-cache',
326  'Cache-Control' => 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
327  'Expires' => 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
328  }
329 
333  protected function getDefaultVars()
334  {
335  $vars = array();
336 
337  $metadata = "<meta name='description' content='The study of Open Source'>\n";
338  $metadata .= "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n";
339  $metadata .= "<meta name='viewport' content='width=device-width,initial-scale=1.0'>\n";
340 
341  $vars['metadata'] = $metadata;
342 
343  if (!empty($this->title)) {
344  $vars[self::TITLE] = htmlentities($this->title);
345  }
346 
347  $styles = "<link rel='stylesheet' href='css/jquery-ui.css'>\n";
348  $styles .= "<link rel='stylesheet' href='css/select2.min.css'>\n";
349  $styles .= "<link rel='stylesheet' href='css/jquery.dataTables.css'>\n";
350  $styles .= "<link rel='stylesheet' href='css/fossology.css'>\n";
351  $styles .= "<link rel='stylesheet' href='css/bootstrap/bootstrap.min.css'>\n";
352  $styles .= "<link rel='stylesheet' href='css/bootstrap-icons.css'>\n";
353  $styles .= "<link rel='icon' type='image/x-icon' href='favicon.ico'>\n";
354  $styles .= "<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>\n";
355 
356  $styles .= $this->menu->OutputCSS();
357 
358  $vars['styles'] = $styles;
359 
360  $vars['menu'] = $this->menu->Output($this->title);
361 
362  global $SysConf;
363  if (array_key_exists('BUILD', $SysConf)) {
364  $vars['versionInfo'] = array(
365  'version' => $SysConf['BUILD']['VERSION'],
366  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
367  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
368  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
369  'branchName' => $SysConf['BUILD']['BRANCH']
370  );
371  }
372 
373  return $vars;
374  }
375 
376  protected function mergeWithDefault($vars)
377  {
378  return array_merge($this->getDefaultVars(), $vars);
379  }
380 
381  protected function flushContent($content)
382  {
383  return $this->render("include/base.html.twig",$this->mergeWithDefault(array("content"=>$content)));
384  }
385 
391  public function __get($name)
392  {
393  if (method_exists($this, ($method = 'get' . ucwords($name)))) {
394  return $this->$method();
395  } else {
396  throw new \Exception("property '$name' not found in module " . $this->name);
397  }
398  }
399 
400  function __toString()
401  {
402  return getStringRepresentation(get_object_vars($this), get_class($this));
403  }
404 }
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)