root/trunk/wifidog-auth/wifidog/include/schema_validate.php @ 553

Revision 553, 12.4 KB (checked in by benoitg, 8 years ago)

2005-04-18 Benoit Gr�goire <bock@…>

  • Hotspot and network content association, continue access control work.
  • hotspot_owner.php: Fix wrong assignement of user_id that prevented the script from working.
  • All files: Remove whitespace and carriage return after the ?> closing tags.
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3
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 as   *
7 * published by the Free Software Foundation; either version 2 of   *
8 * the License, or (at your option) any later version.              *
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*
16 * along with this program; if not, contact:                        *
17 *                                                                  *
18 * Free Software Foundation           Voice:  +1-617-542-5942       *
19 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
20 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
21 *                                                                  *
22 \********************************************************************/
23/**@file schema_validate.php
24 * Network status page
25 * @author Copyright (C) 2004 Benoit Grégoire
26 */
27error_reporting(E_ALL);
28require_once BASEPATH.'config.php';
29require_once BASEPATH.'classes/AbstractDb.php';
30require_once BASEPATH.'classes/Session.php';
31define('REQUIRED_SCHEMA_VERSION', 7);
32
33/** Check that the database schema is up to date.  If it isn't, offer to update it. */
34function validate_schema()
35{
36        global $db;
37
38        check_users_not_empty();
39        $db->ExecSqlUniqueRes("SELECT * FROM schema_info WHERE tag='schema_version'", $row, false);
40        if (empty ($row))
41        {
42                echo "<html><head><h1>"._("Unable to retrieve schema version.  The database schema is too old to be updated.")."</h1></html></head>";
43                exit ();
44        }
45        else
46                if ($row['value'] < REQUIRED_SCHEMA_VERSION)
47                {
48                        if (!empty ($_REQUEST['schema_update_confirm']) && $_REQUEST['schema_update_confirm'] == 'on')
49                        {
50                                update_schema();
51                        }
52                        else
53                        {
54                                echo "<html><head><h1>";
55                                echo _("The database schema is not up to date.  Do you want to try to update it?  This operation is irreversible.");
56                                echo "</h1><form name='login_form' method='get'>\n";
57                                echo "<input type='submit' name='submit' value='"._("Try to update database schema")."'>\n";
58                                echo _("Yes, I am sure:")."<input type='checkbox' name='schema_update_confirm'>\n";
59                                echo "</form>\n";
60                                echo "</html></head>";
61                                exit ();
62                        }
63                }
64                else
65                {
66                        //echo "<html><head><h1>The database schema is up to date.</h1></html></head>";
67                        //exit();
68                }
69}
70
71/**
72 * Auto create an administrator user with the first authenticator available
73 */
74function check_users_not_empty()
75{
76        // Extract the first account origin, assume it's the default
77        global $AUTH_SOURCE_ARRAY;
78        if (!empty ($AUTH_SOURCE_ARRAY))
79        {
80                $default_account_origin = array_values(array_keys($AUTH_SOURCE_ARRAY));
81                $default_account_origin = $default_account_origin[0];
82        }
83
84        if (!empty ($default_account_origin))
85        {
86                global $db;
87                $db->ExecSqlUniqueRes("SELECT user_id FROM users WHERE account_origin = '$default_account_origin' LIMIT 1", $row, false);
88                if ($row == null)
89                {
90                        echo "<html><head><h1>";
91                        echo _("No user matches the default account origin, a new user admin/admin will be created. Change the password as soon as possible !");
92                        echo "</html></head>";
93                        $sql = "BEGIN;";
94                        $sql .= "INSERT INTO users (user_id, username, pass, email, account_status, validation_token, account_origin) VALUES ('admin_original_user_delete_me', 'admin', 'ISMvKXpXpadDiUoOSoAfww==', 'test_user_please@delete.me', 1, 'df16cc4b1d0975e267f3425eaac31950', '$default_account_origin');";
95                        $sql .= "INSERT INTO administrators (user_id) VALUES ('admin_original_user_delete_me');";
96                        $sql .= "COMMIT;";
97                        $db->ExecSqlUpdate($sql, $row, false);
98                        exit;
99                }
100        }
101        else
102        {
103                echo "<html><head><h1>";
104                echo _("Could not get a default account origin, make sure you config.php has at least one AUTH_SOURCE_ARRAY entry.");
105                echo "</html></head>";
106                exit ();
107        }
108} /** Try to bring the database schema up to date. */
109function update_schema()
110{
111        global $db;
112        echo "<html><head><h1>\n";
113        echo _("Trying to update the database schema.");
114        echo "</h1>\n";
115        $db->ExecSqlUniqueRes("SELECT * FROM schema_info WHERE tag='schema_version'", $row, false);
116        if (empty ($row))
117        {
118                echo "<h1>"._("Unable to retrieve schema version.  The database schema is too old to be updated.")."</h1>";
119                exit ();
120        }
121        else
122        {
123                $schema_version = $row['value'];
124                $sql = '';
125                $new_schema_version = 2;
126                if ($schema_version < $new_schema_version)
127                {
128
129                        echo "<h2>Preparing SQL statements to update schema to version  $new_schema_version</h2>";
130                        $sql .= "\n\nUPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version';\n";
131                        $sql .= "ALTER TABLE users ADD COLUMN username text;\n";
132                        $sql .= "ALTER TABLE users ADD COLUMN account_origin text;\n";
133                        $db->ExecSql("SELECT user_id FROM users", $results, false);
134                        foreach ($results as $row)
135                        {
136                                $user_id = $db->EscapeString($row['user_id']);
137                                $sql .= "UPDATE users SET username='$user_id', user_id='".get_guid()."', account_origin='".LOCAL_USER_ACCOUNT_ORIGIN."' WHERE user_id='$user_id';\n";
138                        }
139                        $sql .= "CREATE UNIQUE INDEX idx_unique_username_and_account_origin ON users (username, account_origin);\n";
140                        $sql .= "CREATE UNIQUE INDEX idx_unique_email_and_account_origin ON users USING btree (email, account_origin);\n";
141                }
142
143                $new_schema_version = 3;
144                if ($schema_version < $new_schema_version)
145                {
146                        echo "<h2>Preparing SQL statements to update schema to version  $new_schema_version</h2>";
147                        $sql .= "\n\nUPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version';\n";
148                        $sql .= "DROP INDEX idx_unique_email_and_account_origin;\n";
149                        $sql .= "ALTER TABLE users DROP CONSTRAINT check_email_not_empty;\n";
150                }
151
152                $new_schema_version = 4;
153                if ($schema_version < $new_schema_version)
154                {
155                        echo "<h2>Preparing SQL statements to update schema to version  $new_schema_version</h2>";
156                        $sql .= "\n\nUPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version';\n";
157                        $sql .= "ALTER TABLE users ALTER COLUMN account_origin SET NOT NULL;\n";
158                        $sql .= "ALTER TABLE users ADD CONSTRAINT check_account_origin_not_empty CHECK (account_origin::text <> ''::text);\n";
159
160                        //We must skip all other updates because schema 5 must be manually updated:
161                        $schema_version = 1000000;
162                }
163
164                $new_schema_version = 5;
165                if ($schema_version == $new_schema_version -1)
166                {
167                        echo "<h1>Recoding database from ISO-8859-1 to UTF-8</h1>";
168                        echo "<h1>YOU MUST EXECUTE THESE COMMANDS FROM THE COMMAND_LINE:</h1>";
169                        echo "pg_dump wifidog -U wifidog > wifidog_dump.sql;<br>";
170                        echo "dropdb wifidog -U wifidog; <br>";
171                        echo "createdb --encoding=UNICODE --template=template0 -U wifidog wifidog;<br>";
172                        echo "psql wifidog -U wifidog < wifidog_dump.sql;<br>\n";
173                        echo " (Note: You may ignore the following errors:  \"ERROR:  permission denied to set session authorization\" and \"ERROR:  must be owner of schema public\")<br>";
174                        echo "psql wifidog -U wifidog -c \"UPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version'\";<br>";
175                        exit;
176                }
177                $new_schema_version = 6;
178                if ($schema_version < $new_schema_version)
179                {
180
181                        echo "<h2>Preparing SQL statements to update schema to version  $new_schema_version</h2>";
182                        $sql .= "\n\nUPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version';\n";
183                        $sql .= "CREATE TABLE locales ( \n";
184                        $sql .= "  locales_id text PRIMARY KEY \n";
185                        $sql .= "  ); \n";
186                        $sql .= "  INSERT INTO locales VALUES ('fr'); \n";
187                        $sql .= "  INSERT INTO locales VALUES ('en'); \n";
188                        $sql .= "ALTER TABLE users ADD COLUMN never_show_username bool;\n";
189                        $sql .= "ALTER TABLE users ALTER COLUMN never_show_username SET DEFAULT FALSE;\n";
190                        $sql .= "ALTER TABLE users ADD COLUMN real_name text;\n";
191                        $sql .= "ALTER TABLE users ALTER COLUMN real_name SET DEFAULT NULL;\n";
192                        $sql .= "ALTER TABLE users ADD COLUMN website text;\n";
193                        $sql .= "ALTER TABLE users ALTER COLUMN website SET DEFAULT NULL;\n";
194                        $sql .= "ALTER TABLE users ADD COLUMN prefered_locale text REFERENCES locales ON DELETE SET NULL ON UPDATE CASCADE;\n";
195
196                        $sql .= "                       
197                        CREATE TABLE content
198                        (
199                        content_id text NOT NULL PRIMARY KEY,
200                        content_type text NOT NULL  CONSTRAINT content_type_not_empty_string CHECK (content_type != ''),
201                        title text REFERENCES content ON DELETE RESTRICT ON UPDATE CASCADE,
202                        description text REFERENCES content ON DELETE RESTRICT ON UPDATE CASCADE,
203                        project_info text REFERENCES content ON DELETE RESTRICT ON UPDATE CASCADE,
204                        sponsor_info text REFERENCES content ON DELETE RESTRICT ON UPDATE CASCADE,
205                        creation_timestamp timestamp DEFAULT now()
206                        );
207
208                        CREATE TABLE content_has_owners
209                        (
210                        content_id text NOT NULL REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
211                        user_id text NOT NULL REFERENCES users ON DELETE CASCADE ON UPDATE CASCADE,
212                        is_author bool NOT NULL,
213                        owner_since timestamp DEFAULT now(),
214                        PRIMARY KEY  (content_id, user_id)
215                        );
216                       
217                        CREATE TABLE langstring_entries (
218                          langstring_entries_id text NOT NULL PRIMARY KEY,
219                          langstrings_id text REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
220                          locales_id text REFERENCES locales ON DELETE RESTRICT ON UPDATE CASCADE,
221                          value text  DEFAULT ''
222                        );
223                       
224                        CREATE TABLE content_group (
225                          content_group_id text NOT NULL PRIMARY KEY REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
226                          is_artistic_content bool NOT NULL DEFAULT FALSE,
227                          is_locative_content bool NOT NULL DEFAULT FALSE,
228                          content_selection_mode text
229                        );
230                       
231                        CREATE TABLE content_group_element (
232                          content_group_element_id text NOT NULL PRIMARY KEY REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
233                          content_group_id text NOT NULL REFERENCES content_group ON DELETE CASCADE ON UPDATE CASCADE,
234                          display_order integer DEFAULT '1',
235                          displayed_content_id text REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
236                          force_only_allowed_node bool
237                        );
238                        CREATE INDEX idx_content_group_element_content_group_id ON content_group_element (content_group_id);
239                       
240                        CREATE TABLE content_group_element_has_allowed_nodes
241                        (
242                        content_group_element_id text NOT NULL REFERENCES content_group_element ON DELETE CASCADE ON UPDATE CASCADE,
243                        node_id text NOT NULL REFERENCES nodes ON DELETE CASCADE ON UPDATE CASCADE,
244                        allowed_since timestamp DEFAULT now(),
245                        PRIMARY KEY  (content_group_element_id, node_id)
246                        );
247                       
248                        CREATE TABLE content_group_element_portal_display_log (
249                          user_id text NOT NULL REFERENCES users ON DELETE CASCADE ON UPDATE CASCADE,
250                          content_group_element_id text NOT NULL REFERENCES content_group_element ON DELETE CASCADE ON UPDATE CASCADE,
251                          display_timestamp timestamp NOT NULL DEFAULT now(),
252                          node_id text REFERENCES nodes ON DELETE CASCADE ON UPDATE CASCADE,
253                          PRIMARY KEY  (user_id,content_group_element_id, display_timestamp)
254                        );
255                       
256                        CREATE TABLE user_has_content (
257                          user_id text NOT NULL REFERENCES users ON DELETE CASCADE ON UPDATE CASCADE,
258                          content_id text NOT NULL REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
259                          subscribe_timestamp timestamp NOT NULL DEFAULT now(),
260                          PRIMARY KEY  (user_id,content_id)
261                        );
262                       
263                        CREATE TABLE node_has_content (
264                          node_id text NOT NULL REFERENCES nodes ON DELETE CASCADE ON UPDATE CASCADE,
265                          content_id text NOT NULL REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
266                          subscribe_timestamp timestamp NOT NULL DEFAULT now(),
267                          PRIMARY KEY  (node_id,content_id)
268                        );
269                       
270                        CREATE TABLE network_has_content (
271                          network_id text NOT NULL,
272                          content_id text NOT NULL REFERENCES content ON DELETE CASCADE ON UPDATE CASCADE,
273                          subscribe_timestamp timestamp NOT NULL DEFAULT now(),
274                          PRIMARY KEY  (network_id,content_id)
275                        );";
276                }
277               
278                $new_schema_version = 7;
279                if ($schema_version < $new_schema_version)
280                {
281
282                        echo "<h2>Preparing SQL statements to update schema to version  $new_schema_version</h2>";
283                        $sql .= "\n\nUPDATE schema_info SET value='$new_schema_version' WHERE tag='schema_version';\n";
284                        $sql .= "ALTER TABLE content ADD COLUMN is_persistent bool;\n";
285                        $sql .= "ALTER TABLE content ALTER COLUMN is_persistent SET DEFAULT FALSE;\n";
286                       
287                                        }
288                $db->ExecSqlUpdate("BEGIN;\n$sql\nCOMMIT;\n", true);
289                echo "</html></head>";
290                exit ();
291        }
292}
293?>
Note: See TracBrowser for help on using the browser.