467e4b4ecb735cc4cfe693174516a5cd2fb8b8bf
[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 (!defined('__SECURITY')) {
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 (EXT_VERSION_IS_OLDER("sql_patches", "0.3.6")) return false;
42
43 // Check if there is no scrambling string
44 if (empty($_CONFIG['pass_scramble'])) {
45         // Generate 40 chars long scramble string
46         $scrambleString = genScrambleString(40);
47
48         // ... and store it there for future usage
49         UPDATE_CONFIG("pass_scramble", $scrambleString);
50
51         // Also remember it in config
52         $_CONFIG['pass_scramble'] = $scrambleString;
53         unset($scrambleString);
54 }
55
56 // Check if there is no master salt string
57 if (empty($_CONFIG['master_salt'])) {
58         // Generate the master salt which is the first chars minus 40 chars of this random hash
59         // We do an extra scrambling here...
60         $masterSalt = scrambleString(substr(generateHash(GEN_PASS(rand(128, 256))), 0, -40));
61
62         // ... and store it there for future usage
63         UPDATE_CONFIG("master_salt", $masterSalt);
64
65         // Also remember it in config
66         $_CONFIG['master_salt'] = $masterSalt;
67         unset($masterSalt);
68 }
69
70 if (empty($_CONFIG['file_hash'])) {
71         // Create filename from hashed random string
72         $file_hash = generateHash(GEN_PASS(rand(128, 256)));
73         $file = sprintf("%sinc/.secret/.%s", PATH, $file_hash);
74
75         // File hash was never created
76         $fp = @fopen($file, 'w') or mxchange_die("Cannot write secret key file!");
77         if ($fp != false) {
78                 // Could write to secret file! So let's generate the secret key...
79                 // 1. Count of chars to be taken from back of the string
80                 $nums = rand(40, 45);
81                 // 2. Generate secret key from a randomized string
82                 $secretKey = substr(generateHash(GEN_PASS(rand(128, 256))), -$nums);
83                 // 3. Write the key to the file
84                 fwrite($fp, $secretKey);
85                 // 4. Close file
86                 fclose($fp);
87
88                 // Change access rights for more security
89                 @chmod($file, 0644);
90
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                 UPDATE_CONFIG("file_hash", $file_hash);
98
99                 // Also update configuration
100                 $_CONFIG['secret_key'] = $secretKey;
101                 $_CONFIG['file_hash']  = $file_hash;
102
103                 // And remove some variables
104                 unset($secretKey);
105                 unset($file_hash);
106         }
107 }
108
109 //
110 ?>