2 /************************************************************************
3 * MXChange v0.2.1 Start: 10/08/2005 *
4 * =============== Last change: 01/01/2006 *
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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
45 // Check for version of sql_patches
46 if (GET_EXT_VERSION('sql_patches') < '0.3.6') return false;
48 // Check if there is no scrambling string
49 if (getConfig('pass_scramble') == "") {
50 // Generate 40 chars long scramble string
51 $scrambleString = genScrambleString(40);
53 // ... and store it there for future usage
54 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_config` SET pass_scramble='%s' WHERE config=0 LIMIT 1",
55 array($scrambleString), __FILE__, __LINE__);
57 // Also remember it in config
58 setConfigEntry('pass_scramble', $scrambleString);
59 unset($scrambleString);
62 // Check if there is no master salt string
63 if (getConfig('master_salt') == "") {
64 // Generate the master salt which is the first chars minus 40 chars of this random hash
65 // We do an extra scrambling here...
66 $masterSalt = scrambleString(substr(sha1(GEN_PASS(mt_rand(128, 256))), 0, -40));
68 // ... and store it there for future usage
69 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_config` SET master_salt='%s' WHERE config=0 LIMIT 1",
70 array($masterSalt), __FILE__, __LINE__);
72 // Also remember it in config
73 setConfigEntry('master_salt', $masterSalt);
77 if (getConfig('file_hash') == "") {
78 // Create filename from hashed random string
79 $fileHash = sha1(GEN_PASS(mt_rand(128, 256)));
80 $FQFN = sprintf("%sinc/.secret/.%s",
85 // Count of chars to be taken from back of the string
86 $nums = mt_rand(40, 45);
88 // Generate secret key from a randomized string
89 $secretKey = substr(sha1(GEN_PASS(mt_rand(128, 256))), -$nums);
91 // File hash was never created
92 WRITE_FILE($FQFN, $secretKey);
95 if (FILE_READABLE($FQFN)) {
96 //* DEBUG: */ unlink($FQFN);
97 //* DEBUG: */ $test = hexdec(get_session('u_hash')) / hexdec($secretKey);
98 //* DEBUG: */ $test = generateHash(str_replace('.', '', $test));
99 //* DEBUG: */ die("Secret-Key: ".$secretKey."<br />Cookie: ".get_session('u_hash')."<br />Test: ".$test);
101 // Write $fileHash to database
102 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_config` SET file_hash='%s' WHERE config=0 LIMIT 1",
103 array($fileHash), __FILE__, __LINE__);
105 // Generate FQFN for .htaccess file
106 $FQFN = sprintf("%sinc/.secret/.htaccess",
110 // Is the .htaccess file there?
111 if (!FILE_READABLE($FQFN)) {
112 // Also create .htaccess file
113 WRITE_FILE($FQFN, "Deny from all\n");
116 // Also update configuration
117 setConfigEntry('secret_key', $secretKey);
118 setConfigEntry('file_hash' , $fileHash);