Project

General

Profile

1 1425 Luisehahne
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         preferences
6
 * @author          WebsiteBaker Project
7 1569 darkviper
 * @copyright       2009-2012, Website Baker Org. e.V.
8 1425 Luisehahne
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id$
13
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15
 *
16
 */
17
18
function save_preferences( &$admin, &$database)
19
{
20 1815 Luisehahne
	global $MESSAGE,$TEXT;
21 1425 Luisehahne
	$err_msg = array();
22 1569 darkviper
	$iMinPassLength = 6;
23 1804 Luisehahne
	$bPassRequest = false;
24
	$bMailHasChanged = false;
25 1425 Luisehahne
// first check form-tan
26 1804 Luisehahne
	if(!$admin->checkFTAN()){
27
	   $err_msg[] = $MESSAGE['GENERIC_SECURITY_ACCESS'];
28
    } else {
29 1425 Luisehahne
// Get entered values and validate all
30
	// remove any dangerouse chars from display_name
31 1804 Luisehahne
        $display_name = $admin->add_slashes(strip_tags($admin->StripCodeFromText($admin->get_post('display_name'),true)));
32
    	$display_name = ( $display_name == '' ? $admin->get_display_name() : $display_name );
33
// check that display_name is unique in whoole system (prevents from User-faking)
34
    	$sql  = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'users` ';
35
    	$sql .= 'WHERE `user_id` <> '.(int)$admin->get_user_id().' AND `display_name` LIKE "'.$display_name.'"';
36 1815 Luisehahne
    	if( $database->get_one($sql) > 0 ){ $err_msg[] = $MESSAGE['USERS_USERNAME_TAKEN'].' ('.$TEXT['DISPLAY_NAME'].')'; }
37 1425 Luisehahne
// language must be 2 upercase letters only
38 1804 Luisehahne
    	$language         = strtoupper($admin->get_post('language'));
39
    	$language         = (preg_match('/^[A-Z]{2}$/', $language) ? $language : DEFAULT_LANGUAGE);
40 1425 Luisehahne
// timezone must be between -12 and +13  or -20 as system_default
41 1872 Luisehahne
		$timezone         = ($admin->get_post('timezone'));
42
		$timezone         = ( (is_numeric($timezone) && ($timezone!=0)) ? $timezone : -20);
43
		$timezone         = ( ($timezone >= -12 && $timezone <= 13) ? $timezone : -20 ) * 3600;
44 1425 Luisehahne
// date_format must be a key from /interface/date_formats
45 1872 Luisehahne
		$date_format      = $admin->get_post('date_format');
46
		$date_format = (($date_format==DEFAULT_DATE_FORMAT) ? '' : $date_format);
47
		$date_format_key  = str_replace(' ', '|', $date_format);
48
		$user_time = true;
49
		include( ADMIN_PATH.'/interface/date_formats.php' );
50
		$date_format = (array_key_exists($date_format_key, $DATE_FORMATS) ? $date_format : 'system_default');
51
		$date_format = ($date_format == 'system_default' ? '' : $date_format);
52
		unset($DATE_FORMATS);
53 1782 Luisehahne
// time_format must be a key from /interface/time_formats
54 1872 Luisehahne
		$time_format = $admin->get_post('time_format');
55
		$time_format = (($time_format==DEFAULT_TIME_FORMAT) ? '' : $time_format);
56
		$time_format_key  = str_replace(' ', '|', $time_format);
57
		$user_time = true;
58
		include( ADMIN_PATH.'/interface/time_formats.php' );
59
		$time_format = (array_key_exists($time_format_key, $TIME_FORMATS) ? $time_format : 'system_default');
60
		$time_format = ($time_format == 'system_default' ? '' : $time_format);
61
		unset($TIME_FORMATS);
62 1425 Luisehahne
// email should be validatet by core
63 1804 Luisehahne
64
//    	$email = trim( $admin->get_post('email') == null ? '' : $admin->get_post('email') );
65
        $email = $admin->add_slashes(strip_tags($admin->StripCodeFromText($admin->get_post('email'),true)));
66
    	if( !$admin->validate_email($email) )
67
    	{
68
    		$email = '';
69
    		$err_msg[] = $MESSAGE['USERS_INVALID_EMAIL'];
70
    	} else {
71
    		if($email != '') {
72
    		// check that email is unique in whoole system
73
    			$sql  = 'SELECT `email` FROM `'.TABLE_PREFIX.'users` ';
74
    			$sql .= 'WHERE `user_id` = '.(int)$admin->get_user_id().' AND `email` LIKE "'.$email.'"';
75
                $IsOldMail = $database->get_one($sql);
76
    		// check that email is unique in whoole system
77
    			$email = $admin->add_slashes($email);
78
    			$sql  = 'SELECT `email` FROM `'.TABLE_PREFIX.'users` ';
79
    			$sql .= 'WHERE `user_id` <> '.(int)$admin->get_user_id().' AND `email` LIKE "'.$email.'"';
80
                $checkMail = $database->get_one($sql);
81
82
    			if( $checkMail == $email ){ $err_msg[] = $MESSAGE['USERS_EMAIL_TAKEN']; }
83
                $bMailHasChanged = ($email != $IsOldMail);
84
    		}
85
    	}
86
87 1425 Luisehahne
// receive password vars and calculate needed action
88 1872 Luisehahne
	    $sCurrentPassword = $admin->add_slashes($admin->StripCodeFromText($admin->get_post('current_password'),true));
89
	    $sNewPassword = $admin->add_slashes($admin->StripCodeFromText($admin->get_post('new_password_1'),true));
90
	    $sNewPasswordRetyped = $admin->add_slashes($admin->StripCodeFromText($admin->get_post('new_password_2'),true));
91 1804 Luisehahne
92 1872 Luisehahne
	    if($bMailHasChanged == true)
93
	    {
94
	        $bPassRequest = $bMailHasChanged;
95
	    } else {
96
	        $bPassRequest = ( ( $sCurrentPassword != '') || ($sNewPassword != '') || ($sNewPasswordRetyped != '') ) ? true : false;
97
	    }
98
	    // Check existing password
99
		$sql  = 'SELECT `password` ';
100
		$sql .= 'FROM `'.TABLE_PREFIX.'users` ';
101
		$sql .= 'WHERE `user_id` = '.$admin->get_user_id();
102
		if ( $bPassRequest && md5($sCurrentPassword) != $database->get_one($sql) ) {
103
	// access denied
104
			$err_msg[] = $MESSAGE['PREFERENCES_CURRENT_PASSWORD_INCORRECT'];
105
	} else {
106
	// validate new password
107
			$sPwHashNew = false;
108
			if( ($sNewPassword != '') || ($sNewPasswordRetyped != '') ) {
109
				if(strlen($sNewPassword) < $iMinPassLength) {
110
					$err_msg[] = $MESSAGE['USERS_PASSWORD_TOO_SHORT'];
111
				} else {
112
					if($sNewPassword != $sNewPasswordRetyped) {
113
						$err_msg[] =  $MESSAGE['USERS_PASSWORD_MISMATCH'];
114
					} else {
115
						$pattern = '/[^'.$admin->password_chars.']/';
116
						if (preg_match($pattern, $sNewPassword)) {
117
							$err_msg[] = $MESSAGE['PREFERENCES_INVALID_CHARS'];
118
						} else {
119
							$sPwHashNew = md5($sNewPassword);
120
						}
121
					}
122
				}
123
			}
124 1804 Luisehahne
125 1872 Luisehahne
	// if no validation errors, try to update the database, otherwise return errormessages
126
			if(sizeof($err_msg) == 0)
127
			{
128
				$sql  = 'UPDATE `'.TABLE_PREFIX.'users` ';
129
				$sql .= 'SET `display_name`=\''.$display_name.'\', ';
130
				if($sPwHashNew) {
131
					$sql .=     '`password`=\''.$sPwHashNew.'\', ';
132
				}
133
				if($email != '') {
134
					$sql .=     '`email`=\''.$email.'\', ';
135
				}
136
				$sql .=     '`language`=\''.$language.'\', ';
137
				$sql .=     '`timezone`=\''.$timezone.'\', ';
138
				$sql .=     '`date_format`=\''.$date_format.'\', ';
139
				$sql .=     '`time_format`=\''.$time_format.'\' ';
140
				$sql .= 'WHERE `user_id`='.(int)$admin->get_user_id();
141
				if( $database->query($sql) )
142
				{
143
					// update successfull, takeover values into the session
144
					$_SESSION['DISPLAY_NAME'] = $display_name;
145
					$_SESSION['LANGUAGE'] = $language;
146
					$_SESSION['EMAIL'] = $email;
147
					$_SESSION['TIMEZONE'] = $timezone;
148
					if(isset($_SESSION['USE_DEFAULT_TIMEZONE'])) { unset($_SESSION['USE_DEFAULT_TIMEZONE']); }
149
					// Update date format
150
					if($date_format != '') {
151
						$_SESSION['DATE_FORMAT'] = $date_format;
152
						if(isset($_SESSION['USE_DEFAULT_DATE_FORMAT'])) { unset($_SESSION['USE_DEFAULT_DATE_FORMAT']); }
153
					} else {
154
						$_SESSION['USE_DEFAULT_DATE_FORMAT'] = true;
155
						if(isset($_SESSION['DATE_FORMAT'])) { unset($_SESSION['DATE_FORMAT']); }
156
					}
157
					// Update time format
158
					if($time_format != '') {
159
						$_SESSION['TIME_FORMAT'] = $time_format;
160
						if(isset($_SESSION['USE_DEFAULT_TIME_FORMAT'])) { unset($_SESSION['USE_DEFAULT_TIME_FORMAT']); }
161
					} else {
162
						$_SESSION['USE_DEFAULT_TIME_FORMAT'] = true;
163
						if(isset($_SESSION['TIME_FORMAT'])) { unset($_SESSION['TIME_FORMAT']); }
164
					}
165
				} else {
166
					$err_msg[] = 'invalid database UPDATE call in '.__FILE__.'::'.__FUNCTION__.'before line '.__LINE__;
167
				}
168
			}
169
		}
170 1804 Luisehahne
171 1872 Luisehahne
	}
172 1804 Luisehahne
173 1425 Luisehahne
	return ( (sizeof($err_msg) > 0) ? implode('<br />', $err_msg) : '' );
174
}
175 1804 Luisehahne
176
$config_file = realpath('../../config.php');
177
if(file_exists($config_file) && !defined('WB_URL'))
178
{
179
	require_once($config_file);
180
}
181
182
if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
183
184
// suppress to print the header, so no new FTAN will be set
185
$admin = new admin('Preferences','start', false);
186
187 1425 Luisehahne
$retval = save_preferences($admin, $database);
188
if( $retval == '')
189
{
190 1467 Luisehahne
	// print the header
191
	$admin->print_header();
192 1569 darkviper
	$admin->print_success($MESSAGE['PREFERENCES_DETAILS_SAVED']);
193 1425 Luisehahne
	$admin->print_footer();
194 1804 Luisehahne
} else {
195 1467 Luisehahne
	// print the header
196
	$admin->print_header();
197 1425 Luisehahne
	$admin->print_error($retval);
198
}