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