FOSSology  4.4.0
Open Source License Compliance by Open Source Software
core-init.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
10 define("TITLE_CORE_INIT", _("Initialize"));
11 
12 class core_init extends FO_Plugin
13 {
14  var $Name = "init";
15  var $Title = TITLE_CORE_INIT;
16  var $Version = "1.0";
17  var $MenuList = "Admin::Initialize";
18  var $Dependency = array("auth","refresh","menus","Default");
19  var $DBaccess = PLUGIN_DB_NONE;
20  var $LoginFlag = 0;
21  var $PluginLevel= 100; /* make this run first! */
22 
29  function PostInitialize()
30  {
31  if ($this->State != PLUGIN_STATE_VALID) {
32  return (1); // don't re-run
33  }
35  /* Enable or disable plugins based on login status */
36  global $Plugins;
37  $Filename = getcwd() . "/init.ui";
38  if (! file_exists($Filename)) {
39  $this->State = PLUGIN_STATE_INVALID;
40  return;
41  }
42  $Max = count($Plugins);
43  for ($i = 0; $i < $Max; $i ++) {
44  $P = &$Plugins[$i];
45  if ($P->State == PLUGIN_STATE_INVALID) {
46  continue;
47  }
48  /* Don't turn off plugins that are already up and running. */
49  if ($P->State == PLUGIN_STATE_READY) {
50  continue;
51  }
52  if ($P->DBaccess == PLUGIN_DB_ADMIN) {
53  continue;
54  }
55  $Key = array_search($P->Name, $this->Dependency);
56  if (($Key === FALSE) && strcmp($P->Name, $this->Name)) {
57  // print "Disable " . $P->Name . " as $Key <br>\n";
58  $P->Destroy();
59  $P->State = PLUGIN_STATE_INVALID;
60  } else {
61  // print "Keeping " . $P->Name . " as $Key <br>\n";
62  }
63  }
64  $this->State = PLUGIN_STATE_READY;
65  if (($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_ADMIN) &&
66  ($this->MenuList !== "")) {
67  menu_insert("Main::" . $this->MenuList, $this->MenuOrder, $this->Name,
68  $this->MenuTarget);
69  }
70  return($this->State == PLUGIN_STATE_READY);
71  } // PostInitialize()
72 
76  function Output()
77  {
78  if ($this->State != PLUGIN_STATE_READY) {
79  return;
80  }
81  $V="";
82  global $Plugins;
83  switch ($this->OutputType) {
84  case "XML":
85  break;
86  case "HTML":
87  /* If you are not logged in, then force a login. */
88  if (empty($_SESSION['User'])) {
89  $P = &$Plugins["auth"];
90  $P->OutputSet($this->OutputType, 0);
91  $V .= $P->Output();
92  $P->OutputUnSet();
93  } else { /* It's an init */
94  $FailFlag=0;
95  $Filename = getcwd() . "/init.ui";
96  $Schema = &$Plugins["schema"];
97  if (empty($Schema)) {
98  $V .= _("Failed to find schema plugin.\n");
99  $FailFlag = 1;
100  } else {
101  print "<pre>";
102  $FailFlag = $Schema->ApplySchema($Schema->Filename, 0, 0);
103  print "</pre>";
104  }
105  if (! $FailFlag) {
106  $V .= _(
107  "Initialization complete. Click 'Home' in the top menu to proceed.<br />");
108  if (is_writable(getcwd())) {
109  $State = unlink($Filename);
110  } else {
111  $State = 0;
112  }
113  if (! $State) {
114  $V .= "<font color='red'>";
115  $V .= _("Failed to remove $Filename\n");
116  $text = _("Remove this file to complete the initialization.\n");
117  $V .= "<br />$text";
118  $V .= "</font>\n";
119  $FailedFlag = 1;
120  }
121  } else {
122  $V .= "<font color='red'>";
123  $V .= _("Initialization complete with errors.");
124  $V .= "</font>\n";
125  }
126  }
127  break;
128  case "Text":
129  break;
130  default:
131  break;
132  }
133  if (! $this->OutputToStdout) {
134  return ($V);
135  }
136  print($V);
137  return;
138  } // Output()
139 
144  {
145  $text = _("The system requires initialization. Please login and use the Initialize option under the Admin menu.");
146  $V .= "<b>$text</b>";
147  $V .= "<P />\n";
148  /* Check for a default user */
149  global $PG_CONN;
150  $Level = PLUGIN_DB_ADMIN;
151  $sql = "SELECT * FROM users WHERE user_perm = $Level LIMIT 1;";
152  $result = pg_query($PG_CONN, $sql);
153  DBCheckResult($result, $sql, __FILE__, __LINE__);
154  $R = pg_fetch_assoc($result);
155  pg_free_result($result);
156  if (array_key_exists("user_seed", $R) && array_key_exists("user_pass", $R)) {
157  $sql = "SELECT user_name FROM users WHERE user_seed IS NULL AND user_pass IS NULL";
158  } else {
159  $sql = "SELECT user_name FROM users";
160  }
161  $result = pg_query($PG_CONN, $sql);
162  DBCheckResult($result, $sql, __FILE__, __LINE__);
163  $R = pg_fetch_assoc($result);
164  pg_free_result($result);
165  if (! empty($R['user_name'])) {
166  $V .= _(
167  "If you need an account, use '" . $R['user_name'] .
168  "' with no password.\n");
169  $V .= "<P />\n";
170  }
171  return $V;
172  }
173 }
174 
175 $NewPlugin = new core_init;
176 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
Output()
This is only called when the user logs out.
Definition: core-init.php:76
infoFirstTimeUsage()
Definition: core-init.php:143
PostInitialize()
This is where the magic for mod=init happens. This plugin only runs when the special file "....
Definition: core-init.php:29
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
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.
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
Definition: libfossology.h:36
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN