Project

General

Profile

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

    
19

    
20
// Print admin header
21
require('../../config.php');
22
require_once(WB_PATH.'/framework/class.admin.php');
23
$admin = new admin('Preferences');
24
$js_back = "javascript: history.go(-1);"; // Create a javascript back link
25

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

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

    
178
?>
(2-2/2)