9 namespace Fossology\Lib\Plugin;
14 use Monolog\Handler\StreamHandler;
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;
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";
52 private $version =
"1.0";
58 private $requiresLogin =
true;
60 private $PluginLevel = 10;
62 private $dependencies = array();
63 private $InitOrder = 0;
65 private $MenuList = NULL;
66 private $MenuOrder = 0;
67 private $MenuTarget = NULL;
73 public function __construct($name, $parameters = array())
75 if ($name ===
null || $name ===
"") {
76 throw new \InvalidArgumentException(
"plugin requires a name");
79 foreach ($parameters as $key => $value) {
80 $this->setParameter($key, $value);
83 if (array_key_exists(
'DIRECTORIES', $SysConf) && array_key_exists(
'LOGDIR', $SysConf[
'DIRECTORIES'])) {
84 $this->logdir = $SysConf[
'DIRECTORIES'][
'LOGDIR'];
86 $this->logdir = sys_get_temp_dir();
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));
96 $this->microMenu = $this->
getObject(
'ui.component.micromenu');
99 private function setParameter($key, $value)
103 $this->title = $value;
106 case self::PERMISSION:
107 $this->permission = $value;
110 case self::REQUIRES_LOGIN:
111 $this->requiresLogin = $value;
115 $this->PluginLevel = $value;
118 case self::DEPENDENCIES:
119 $this->dependencies = $value;
122 case self::INIT_ORDER:
123 $this->InitOrder = $value;
126 case self::MENU_LIST:
127 $this->MenuList = $value;
130 case self::MENU_ORDER:
131 $this->MenuOrder = $value;
134 case self::MENU_TARGET:
135 $this->MenuTarget = $value;
139 throw new \Exception(
"unhandled parameter $key in module " . $this->name);
156 return $this->version;
172 return $this->requiresLogin;
180 return $this->dependencies;
188 return $this->PluginLevel;
196 return $this->permission;
204 return PLUGIN_STATE_READY;
212 return $this->InitOrder;
216 public function getNoMenu()
226 if (isset($this->MenuList) && (!$this->requiresLogin || $this->isLoggedIn())) {
227 menu_insert(
"Main::" . $this->MenuList, $this->MenuOrder, $this->name, $this->name);
236 $request = Request::createFromGlobals();
237 $request->setSession($this->session);
239 $this->checkPrerequisites();
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));
254 return $this->container->get($name);
257 public function preInstall()
262 public function postInstall()
266 public function unInstall()
270 public function execute()
272 $startTime = microtime(
true);
276 $this->logger->debug(sprintf(
"prepare response in %.3fs", microtime(
true) - $startTime));
285 protected abstract function handle(Request $request);
293 protected function render($templateName, $vars =
null, $headers =
null)
295 if ($this->requiresLogin && !$this->isLoggedIn()) {
296 new Response(
"permission denied", Response::HTTP_FORBIDDEN, array(
"contentType" =>
"text/plain"));
299 $startTime = microtime(
true);
301 $content = $this->renderer->load($templateName)
304 $this->logger->debug(sprintf(
"%s: render response in %.3fs", get_class($this), microtime(
true) - $startTime));
312 public function isLoggedIn()
317 private function checkPrerequisites()
319 if ($this->requiresLogin && !$this->isLoggedIn()) {
320 throw new \Exception(
"not allowed without login");
323 foreach ($this->dependencies as $dependency) {
324 $id = plugin_find_id($dependency);
327 throw new \Exception(
"unsatisfied dependency '$dependency' in module '" . $this->
getName() .
"'");
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');
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";
355 $vars[
'metadata'] = $metadata;
357 if (!empty($this->title)) {
358 $vars[self::TITLE] = htmlentities($this->title);
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";
370 $styles .= $this->
menu->OutputCSS();
372 $vars[
'styles'] = $styles;
374 $vars[
'menu'] = $this->
menu->Output($this->title);
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']
390 protected function mergeWithDefault($vars)
395 protected function flushContent($content)
397 return $this->
render(
"include/base.html.twig",$this->mergeWithDefault(array(
"content"=>$content)));
407 if (method_exists($this, ($method =
'get' . ucwords($name)))) {
408 return $this->$method();
410 throw new \Exception(
"property '$name' not found in module " . $this->name);
414 function __toString()
Contains the constants and helpers for authentication of user.
RegisterMenus()
Customize submenus.
render($templateName, $vars=null, $headers=null)
getStringRepresentation($vars, $classname)