All, except security block, include()/require() rewritten to own LOAD_INC()/LOAD_INC_...
[mailer.git] / inc / gen_sql_patches.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/08/2005 *
4  * ===============                              Last change: 01/01/2006 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : gen_sql_patches.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Patch password system after upgrading            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Patcht das Passwort-System nach DB-Update        *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Check for version of sql_patches
41 if (GET_EXT_VERSION("sql_patches") < "0.3.6") return false;
42
43 // Check if there is no scrambling string
44 if (getConfig('pass_scramble') == "") {
45         // Generate 40 chars long scramble string
46         $scrambleString = genScrambleString(40);
47
48         // ... and store it there for future usage
49         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_config` SET pass_scramble='%s' WHERE config=0 LIMIT 1",
50                 array($scrambleString), __FILE__, __LINE__);
51
52         // Also remember it in config
53         $_CONFIG['pass_scramble'] = $scrambleString;
54         unset($scrambleString);
55 } // END - if
56
57 // Check if there is no master salt string
58 if (getConfig('master_salt') == "") {
59         // Generate the master salt which is the first chars minus 40 chars of this random hash
60         // We do an extra scrambling here...
61         $masterSalt = scrambleString(substr(sha1(GEN_PASS(mt_rand(128, 256))), 0, -40));
62
63         // ... and store it there for future usage
64         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_config` SET master_salt='%s' WHERE config=0 LIMIT 1",
65                 array($masterSalt), __FILE__, __LINE__);
66
67         // Also remember it in config
68         $_CONFIG['master_salt'] = $masterSalt;
69         unset($masterSalt);
70 } // END - if
71
72 if (getConfig('file_hash') == "") {
73         // Create filename from hashed random string
74         $file_hash = sha1(GEN_PASS(mt_rand(128, 256)));
75         $file = sprintf("%sinc/.secret/.%s",
76                 constant('PATH'),
77                 $file_hash
78         );
79
80         // Count of chars to be taken from back of the string
81         $nums = mt_rand(40, 45);
82
83         // Generate secret key from a randomized string
84         $secretKey = substr(sha1(GEN_PASS(mt_rand(128, 256))), -$nums);
85
86         // File hash was never created
87         WRITE_FILE($file, $secretKey);
88
89         // Is the file there?
90         if (FILE_READABLE($file)) {
91                 //* DEBUG: */ unlink($file);
92                 //* DEBUG: */ $test = hexdec(get_session('u_hash')) / hexdec($secretKey);
93                 //* DEBUG: */ $test = generateHash(str_replace('.', "", $test));
94                 //* DEBUG: */ die("Secret-Key: ".$secretKey."<br />Cookie: ".get_session('u_hash')."<br />Test: ".$test);
95
96                 // Write $file_hash to database
97                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_config` SET file_hash='%s' WHERE config=0 LIMIT 1",
98                         array($file_hash), __FILE__, __LINE__);
99
100                 // Generate FQFN for .htaccess file
101                 $FQFN = sprintf("%sinc/.secret/.htaccess",
102                         constant('PATH')
103                 );
104
105                 // Is the .htaccess file there?
106                 if (!FILE_READABLE($FQFN)) {
107                         // Also create .htaccess file
108                         WRITE_FILE($FQFN, "Deny from all\n");
109                 } // END - if
110
111                 // Also update configuration
112                 $_CONFIG['secret_key'] = $secretKey; unset($secretKey);
113                 $_CONFIG['file_hash']  = $file_hash; unset($file_hash);
114         } // END - if
115 } // END - if
116
117 //
118 ?>