wernis extension is now alpha code (only listing in admin area is missing), naming...
[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 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 // Check for version of sql_patches
42 if (GET_EXT_VERSION("sql_patches") < "0.3.6") return false;
43
44 // Check if there is no scrambling string
45 if (empty($_CONFIG['pass_scramble']))
46 {
47         // Generate 40 chars long scramble string
48         $scrambleString = genScrambleString(40);
49
50         // ... and store it there for future usage
51         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET pass_scramble='%s' WHERE config='0' LIMIT 1",
52          array($scrambleString), __FILE__, __LINE__);
53
54         // Also remember it in config
55         $_CONFIG['pass_scramble'] = $scrambleString;
56         unset($scrambleString);
57 }
58
59 // Check if there is no master salt string
60 if (empty($_CONFIG['master_salt']))
61 {
62         // Generate the master salt which is the first chars minus 40 chars of this random hash
63         // We do an extra scrambling here...
64         $masterSalt = scrambleString(substr(generateHash(GEN_PASS(rand(128, 256))), 0, -40));
65
66         // ... and store it there for future usage
67         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET master_salt='%s' WHERE config='0' LIMIT 1",
68          array($masterSalt), __FILE__, __LINE__);
69
70         // Also remember it in config
71         $_CONFIG['master_salt'] = $masterSalt;
72         unset($masterSalt);
73 }
74
75 if (empty($_CONFIG['file_hash']))
76 {
77         // Create filename from hashed random string
78         $file_hash = generateHash(GEN_PASS(rand(128, 256)));
79         $file = PATH."inc/.secret/.".$file_hash;
80
81         // File hash was never created
82         $fp = @fopen($file, 'w') or mxchange_die("Cannot write secret key file!");
83         if ($fp != false)
84         {
85                 // Could write to secret file! So let's generate the secret key...
86                 // 1. Count of chars to be taken from back of the string
87                 $nums = rand(40, 45);
88                 // 2. Generate secret key from a randomized string
89                 $secretKey = substr(generateHash(GEN_PASS(rand(128, 256))), -$nums);
90                 // 3. Write the key to the file
91                 fwrite($fp, $secretKey);
92                 // 4. Close file
93                 fclose($fp);
94
95                 // Change access rights for more security
96                 @chmod($file, 0644);
97
98                 //* DEBUG: */ unlink($file);
99                 //* DEBUG: */ $test = hexdec($_COOKIE['u_hash']) / hexdec($secretKey);
100                 //* DEBUG: */ $test = generateHash(str_replace('.', '', $test));
101                 //* DEBUG: */ die("Secret-Key: ".$secretKey."<br>Cookie: ".$_COOKIE['u_hash']."<br>Test: ".$test);
102
103                 // Write $file_hash to database
104                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET file_hash='%s' WHERE config='0' LIMIT 1",
105                  array($file_hash), __FILE__, __LINE__);
106
107                 // Also create .htaccess file
108                 $fp = @fopen(PATH."inc/.secret/.htaccess", 'w') or mxchange_die("Cannot write to .htaccess file!");
109                 if ($fp != false)
110                 {
111                         // Add deny line to file
112                         fwrite($fp, "Deny from all");
113
114                         // Close the file
115                         fclose($fp);
116                 }
117
118                 // Also update configuration
119                 $_CONFIG['secret_key'] = $secretKey; unset($secretKey);
120                 $_CONFIG['file_hash']  = $file_hash; unset($file_hash);
121         }
122 }
123
124 //
125 ?>