FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
DefaultPlugin.php
1 <?php
2 /*
3 Copyright (C) 2014-2016, Siemens AG
4 Author: Andreas Würl
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 
20 namespace Fossology\Lib\Plugin;
21 
25 use Monolog\Logger;
31 
32 abstract class DefaultPlugin implements Plugin
33 {
34  const PERMISSION = "permission";
35  const REQUIRES_LOGIN = "requiresLogin";
36  const ENABLE_MENU = "ENABLE_MENU";
37  const LEVEL = "level";
38  const DEPENDENCIES = "dependencies";
39  const INIT_ORDER = "initOrder";
40  const MENU_LIST = "menuList";
41  const MENU_ORDER = "menuOrder";
42  const MENU_TARGET = "menuTarget";
43  const TITLE = "title";
44 
46  protected $container;
48  protected $renderer;
50  private $session;
52  private $logger;
54  private $menu;
56  protected $microMenu;
58  private $name;
60  private $version = "1.0";
62  private $title;
64  private $permission = Auth::PERM_NONE;
66  private $requiresLogin = true;
68  private $PluginLevel = 10;
70  private $dependencies = array();
71  private $InitOrder = 0;
72 
73  private $MenuList = NULL;
74  private $MenuOrder = 0;
75  private $MenuTarget = NULL;
76 
77  public function __construct($name, $parameters = array())
78  {
79  if ($name === null || $name === "") {
80  throw new \InvalidArgumentException("plugin requires a name");
81  }
82  $this->name = $name;
83  foreach ($parameters as $key => $value) {
84  $this->setParameter($key, $value);
85  }
86 
87  global $container;
88  $this->container = $container;
89  $this->session = $this->getObject('session');
90  $this->renderer = $this->getObject('twig.environment');
91  $this->logger = $this->getObject('logger');
92  $this->menu = $this->getObject('ui.component.menu');
93  $this->microMenu = $this->getObject('ui.component.micromenu');
94  }
95 
96  private function setParameter($key, $value)
97  {
98  switch ($key) {
99  case self::TITLE:
100  $this->title = $value;
101  break;
102 
103  case self::PERMISSION:
104  $this->permission = $value;
105  break;
106 
107  case self::REQUIRES_LOGIN:
108  $this->requiresLogin = $value;
109  break;
110 
111  case self::LEVEL:
112  $this->PluginLevel = $value;
113  break;
114 
115  case self::DEPENDENCIES:
116  $this->dependencies = $value;
117  break;
118 
119  case self::INIT_ORDER:
120  $this->InitOrder = $value;
121  break;
122 
123  case self::MENU_LIST:
124  $this->MenuList = $value;
125  break;
126 
127  case self::MENU_ORDER:
128  $this->MenuOrder = $value;
129  break;
130 
131  case self::MENU_TARGET:
132  $this->MenuTarget = $value;
133  break;
134 
135  default:
136  throw new \Exception("unhandled parameter $key in module " . $this->name);
137  }
138  }
139 
143  public function getName()
144  {
145  return $this->name;
146  }
147 
151  public function getVersion()
152  {
153  return $this->version;
154  }
155 
159  public function getTitle()
160  {
161  return $this->title;
162  }
163 
167  public function isRequiresLogin()
168  {
169  return $this->requiresLogin;
170  }
171 
175  public function getDependency()
176  {
177  return $this->dependencies;
178  }
179 
183  public function getPluginLevel()
184  {
185  return $this->PluginLevel;
186  }
187 
191  public function getDBaccess()
192  {
193  return $this->permission;
194  }
195 
199  public function getState()
200  {
201  return PLUGIN_STATE_READY;
202  }
203 
207  public function getInitOrder()
208  {
209  return $this->InitOrder;
210  }
211 
212 
213  public function getNoMenu()
214  {
215  return 0;
216  }
217 
221  protected function RegisterMenus()
222  {
223  if (isset($this->MenuList) && (!$this->requiresLogin || $this->isLoggedIn())) {
224  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->name, $this->name);
225  }
226  }
227 
231  public function getResponse()
232  {
233  $request = Request::createFromGlobals();
234  $request->setSession($this->session);
235 
236  $this->checkPrerequisites();
237 
238  $startTime = microtime(true);
239  $response = $this->handle($request);
240  $response->prepare($request);
241  $this->logger->debug(sprintf("handle request in %.3fs", microtime(true) - $startTime));
242  return $response;
243  }
244 
249  public function getObject($name)
250  {
251  return $this->container->get($name);
252  }
253 
254  public function preInstall()
255  {
256  $this->RegisterMenus();
257  }
258 
259  public function postInstall()
260  {
261  }
262 
263  public function unInstall()
264  {
265  }
266 
267  public function execute()
268  {
269  $startTime = microtime(true);
270 
271  $response = $this->getResponse();
272 
273  $this->logger->debug(sprintf("prepare response in %.3fs", microtime(true) - $startTime));
274 
275  $response->send();
276  }
277 
282  protected abstract function handle(Request $request);
283 
290  protected function render($templateName, $vars = null, $headers = null)
291  {
292  if ($this->requiresLogin && !$this->isLoggedIn()) {
293  new Response("permission denied", Response::HTTP_FORBIDDEN, array("contentType" => "text/plain"));
294  }
295 
296  $startTime = microtime(true);
297 
298  $content = $this->renderer->loadTemplate($templateName)
299  ->render($vars ?: $this->getDefaultVars());
300 
301  $this->logger->debug(sprintf("%s: render response in %.3fs", get_class($this), microtime(true) - $startTime));
302  return new Response(
303  $content,
304  Response::HTTP_OK,
305  $headers ?: $this->getDefaultHeaders()
306  );
307  }
308 
309  public function isLoggedIn()
310  {
311  return (!empty($_SESSION[Auth::USER_NAME]) && $_SESSION[Auth::USER_NAME] != 'Default User');
312  }
313 
314  private function checkPrerequisites()
315  {
316  if ($this->requiresLogin && !$this->isLoggedIn()) {
317  throw new \Exception("not allowed without login");
318  }
319 
320  foreach ($this->dependencies as $dependency) {
321  $id = plugin_find_id($dependency);
322  if ($id < 0) {
323  $this->unInstall();
324  throw new \Exception("unsatisfied dependency '$dependency' in module '" . $this->getName() . "'");
325  }
326  }
327  }
328 
332  protected function getDefaultHeaders()
333  {
334  return array(
335  'Content-type' => 'text/html',
336  'Pragma' => 'no-cache',
337  'Cache-Control' => 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0',
338  'Expires' => 'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
339  }
340 
344  protected function getDefaultVars()
345  {
346  $vars = array();
347 
348  $metadata = "<meta name='description' content='The study of Open Source'>\n";
349  $metadata .= "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n";
350 
351  $vars['metadata'] = $metadata;
352 
353  if (!empty($this->title)) {
354  $vars[self::TITLE] = htmlentities($this->title);
355  }
356 
357  $styles = "<link rel='stylesheet' href='css/jquery-ui.css'>\n";
358  $styles .= "<link rel='stylesheet' href='css/select2.min.css'>\n";
359  $styles .= "<link rel='stylesheet' href='css/jquery.dataTables.css'>\n";
360  $styles .= "<link rel='stylesheet' href='css/fossology.css'>\n";
361  $styles .= "<link rel='icon' type='image/x-icon' href='favicon.ico'>\n";
362  $styles .= "<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>\n";
363 
364  $styles .= $this->menu->OutputCSS();
365 
366  $vars['styles'] = $styles;
367 
368  $vars['menu'] = $this->menu->Output($this->title);
369 
370  global $SysConf;
371  if (array_key_exists('BUILD', $SysConf)) {
372  $vars['versionInfo'] = array(
373  'version' => $SysConf['BUILD']['VERSION'],
374  'buildDate' => $SysConf['BUILD']['BUILD_DATE'],
375  'commitHash' => $SysConf['BUILD']['COMMIT_HASH'],
376  'commitDate' => $SysConf['BUILD']['COMMIT_DATE'],
377  'branchName' => $SysConf['BUILD']['BRANCH']
378  );
379  }
380 
381  return $vars;
382  }
383 
384  protected function mergeWithDefault($vars)
385  {
386  return array_merge($this->getDefaultVars(), $vars);
387  }
388 
389  protected function flushContent($content)
390  {
391  return $this->render("include/base.html.twig",$this->mergeWithDefault(array("content"=>$content)));
392  }
393 
399  public function __get($name)
400  {
401  if (method_exists($this, ($method = 'get' . ucwords($name)))) {
402  return $this->$method();
403  } else {
404  throw new \Exception("property '$name' not found in module " . $this->name);
405  }
406  }
407 
408  function __toString()
409  {
410  return getStringRepresentation(get_object_vars($this), get_class($this));
411  }
412 }
render($templateName, $vars=null, $headers=null)
RegisterMenus()
Customize submenus.
getStringRepresentation($vars, $classname)
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...
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:30