Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         preferences
6
 * @author          Independend-Software-Team
7
 * @author          WebsiteBaker Project
8
 * @copyright       2004-2009, Ryan Djurovich
9
 * @copyright       2009-2011, Website Baker Org. e.V.
10
 * @link			http://www.websitebaker2.org/
11
 * @license         http://www.gnu.org/licenses/gpl.html
12
 * @platform        WebsiteBaker 2.8.x
13
 * @requirements    PHP 5.2.2 and higher
14
 * @version         $Id: save.php 1457 2011-06-25 17:18:50Z Luisehahne $
15
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/preferences/save.php $
16
 * @lastmodified    $Date: 2011-06-25 19:18:50 +0200 (Sat, 25 Jun 2011) $
17
 *
18
 */
19

    
20

    
21
// Print admin header
22
require('../../config.php');
23
require_once(WB_PATH.'/framework/class.admin.php');
24

    
25
// suppress to print the header, so no new FTAN will be set
26
$admin = new admin('Preferences','start', false);
27
// $js_back = "javascript: history.go(-1);"; // Create a javascript back link
28

    
29
function save_preferences( &$admin, &$database)
30
{
31
	global $MESSAGE;
32
	$err_msg = array();
33
	$min_pass_length = 6;
34
// first check form-tan
35
	if(!$admin->checkFTAN()){ $err_msg[] = $MESSAGE['GENERIC_SECURITY_ACCESS']; }
36
// After check print the header
37
	$admin->print_header();
38
// Get entered values and validate all
39
	// remove any dangerouse chars from display_name
40
	$display_name     = $admin->add_slashes(strip_tags(trim($admin->get_post('display_name'))));
41
	$display_name     = ( $display_name == '' ? $admin->get_display_name() : $display_name );
42
	// check that display_name is unique in whoole system (prevents from User-faking)
43
	$sql  = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'users` ';
44
	$sql .= 'WHERE `user_id` <> '.(int)$admin->get_user_id().' AND `display_name` LIKE "'.$display_name.'"';
45
	if( $database->get_one($sql) > 0 ){ $err_msg[] = $MESSAGE['USERS']['USERNAME_TAKEN']; }
46
// language must be 2 upercase letters only
47
	$language         = strtoupper($admin->get_post('language'));
48
	$language         = (preg_match('/^[A-Z]{2}$/', $language) ? $language : DEFAULT_LANGUAGE);
49
// timezone must be between -12 and +13  or -20 as system_default
50
	$timezone         = $admin->get_post('timezone');
51
	$timezone         = (is_numeric($timezone) ? $timezone : -20);
52
	$timezone         = ( ($timezone >= -12 && $timezone <= 13) ? $timezone : -20 ) * 3600;
53
// date_format must be a key from /interface/date_formats
54
	$date_format      = $admin->get_post('date_format');
55
	$date_format_key  = str_replace(' ', '|', $date_format);
56
	$user_time = true;
57
	include( ADMIN_PATH.'/interface/date_formats.php' );
58
	$date_format = (array_key_exists($date_format_key, $DATE_FORMATS) ? $date_format : 'system_default');
59
	$date_format = ($date_format == 'system_default' ? '' : $date_format);
60
	unset($DATE_FORMATS);
61
// time_format must be a key from /interface/time_formats	
62
	$time_format      = $admin->get_post('time_format');
63
	$time_format_key  = str_replace(' ', '|', $time_format);
64
	$user_time = true;
65
	include( ADMIN_PATH.'/interface/time_formats.php' );
66
	$time_format = (array_key_exists($time_format_key, $TIME_FORMATS) ? $time_format : 'system_default');
67
	$time_format = ($time_format == 'system_default' ? '' : $time_format);
68
	unset($TIME_FORMATS);
69
// email should be validatet by core
70
	$email            = ( $admin->get_post('email') == null ? '' : $admin->get_post('email') );
71
	if( !$admin->validate_email($email) )
72
	{
73
		$email = '';
74
		$err_msg[] = $MESSAGE['USERS']['INVALID_EMAIL'];
75
	}else {
76
	// check that email is unique in whoole system
77
		$email = $admin->add_slashes($email);
78
		$sql  = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'users` ';
79
		$sql .= 'WHERE `user_id` <> '.(int)$admin->get_user_id().' AND `email` LIKE "'.$email.'"';
80
		if( $database->get_one($sql) > 0 ){ $err_msg[] = $MESSAGE['USERS']['EMAIL_TAKEN']; }
81
	}
82
// receive password vars and calculate needed action
83
	$current_password = $admin->get_post('current_password');
84
	$current_password = ($current_password == null ? '' : $current_password);
85
	$new_password_1   = $admin->get_post('new_password_1');
86
	$new_password_1   = (($new_password_1 == null || $new_password_1 == '') ? '' : $new_password_1);
87
	$new_password_2   = $admin->get_post('new_password_2');
88
	$new_password_2   = (($new_password_2 == null || $new_password_2 == '') ? '' : $new_password_2);
89
	if($current_password == '')
90
	{
91
		$err_msg[] = $MESSAGE['PREFERENCES']['CURRENT_PASSWORD_INCORRECT'];
92
	}else {
93
	// if new_password is empty, still let current one
94
		if( $new_password_1 == '' )
95
		{
96
			$new_password_1 = $current_password;
97
			$new_password_2 = $current_password;
98
		}
99

    
100
	// is password lenght matching min_pass_lenght ?
101
		if( $new_password_1 != $current_password )
102
		{
103
			if( strlen($new_password_1) < $min_pass_length )
104
			{
105
				$err_msg[] = $MESSAGE['USERS']['PASSWORD_TOO_SHORT'];
106
			}
107
			$pattern = '/[^'.$admin->password_chars.']/';
108
			if( preg_match($pattern, $new_password_1) )
109
			{
110
				$err_msg[] = $MESSAGE['PREFERENCES']['INVALID_CHARS'];
111
			}
112
		}
113
	// is password lenght matching min_pass_lenght ?
114
		if( $new_password_1 != $current_password && strlen($new_password_1) < $min_pass_length )
115
		{
116
			$err_msg[] = $MESSAGE['USERS']['PASSWORD_TOO_SHORT'];
117
		}
118
	// password_1 matching password_2 ?
119
		if( $new_password_1 != $new_password_2 )
120
		{
121
			$err_msg[] = $MESSAGE['USERS']['PASSWORD_MISMATCH'];
122
		}
123
	}
124
	$current_password = md5($current_password);
125
	$new_password_1   = md5($new_password_1);
126
	$new_password_2   = md5($new_password_2);
127
// if no validation errors, try to update the database, otherwise return errormessages
128
	if(sizeof($err_msg) == 0)
129
	{
130
		$sql  = 'UPDATE `'.TABLE_PREFIX.'users` ';
131
		$sql .= 'SET `display_name` = "'.$display_name.'", ';
132
		$sql .=     '`password` = "'.$new_password_1.'", ';
133
		$sql .=     '`email` = "'.$email.'", ';
134
		$sql .=     '`language` = "'.$language.'", ';
135
		$sql .=     '`timezone` = "'.$timezone.'", ';
136
		$sql .=     '`date_format` = "'.$date_format.'", ';
137
		$sql .=     '`time_format` = "'.$time_format.'" ';
138
		$sql .= 'WHERE `user_id` = '.(int)$admin->get_user_id().' AND `password` = "'.$current_password.'"';
139
		if( $database->query($sql) )
140
		{
141
			$sql_info = mysql_info($database->db_handle);
142
			if(preg_match('/matched: *([1-9][0-9]*)/i', $sql_info) != 1)
143
			{  // if the user_id and password dosn't match
144
				$err_msg[] = $MESSAGE['PREFERENCES']['CURRENT_PASSWORD_INCORRECT'];
145
			}else {
146
				// update successfull, takeover values into the session
147
				$_SESSION['DISPLAY_NAME'] = $display_name;
148
				$_SESSION['LANGUAGE'] = $language;
149
				$_SESSION['TIMEZONE'] = $timezone;
150
				$_SESSION['EMAIL'] = $email;
151
				// Update date format
152
				if($date_format != '') {
153
					$_SESSION['DATE_FORMAT'] = $date_format;
154
					if(isset($_SESSION['USE_DEFAULT_DATE_FORMAT'])) { unset($_SESSION['USE_DEFAULT_DATE_FORMAT']); }
155
				} else {
156
					$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
157
					if(isset($_SESSION['DATE_FORMAT'])) { unset($_SESSION['DATE_FORMAT']); }
158
				}
159
				// Update time format
160
				if($time_format != '') {
161
					$_SESSION['TIME_FORMAT'] = $time_format;
162
					if(isset($_SESSION['USE_DEFAULT_TIME_FORMAT'])) { unset($_SESSION['USE_DEFAULT_TIME_FORMAT']); }
163
				} else {
164
					$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
165
					if(isset($_SESSION['TIME_FORMAT'])) { unset($_SESSION['TIME_FORMAT']); }
166
				}
167
			}
168
		}else {
169
			$err_msg[] = 'invalid database UPDATE call in '.__FILE__.'::'.__FUNCTION__.'before line '.__LINE__;
170
		}
171
	}
172
	return ( (sizeof($err_msg) > 0) ? implode('<br />', $err_msg) : '' );
173
}
174
$retval = save_preferences($admin, $database);
175
if( $retval == '')
176
{
177
	$admin->print_success($MESSAGE['PREFERENCES']['DETAILS_SAVED']);
178
	$admin->print_footer();
179
}else {
180
	$admin->print_error($retval);
181
}
182

    
183
?>
(2-2/2)