Huge script change, see http://forum.mxchange.org/topic-458.html for details:
[mailer.git] / inc / modules / member / what-mydata.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/16/2003 *
4  * ===============                              Last change: 06/30/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-mydata.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Members can edit their profile data here         *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Mitglieder koennen hier ihre Profildaten aendern *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } elseif (!isMember()) {
43         redirectToIndexMemberOnlyModule();
44 }
45
46 // Add description as navigation point
47 addMenuDescription('member', __FILE__);
48
49 if ((!isExtensionActive('mydata')) && (!isAdmin())) {
50         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('mydata'));
51         return;
52 } // END - if
53
54 // Remember userid
55 $content['userid'] = getUserId();
56
57 // Init variable to prevent notices
58 $URL = '';
59
60 // Detect what the member wants to do
61 $mode = 'show'; // Show his data
62 if (isPostRequestElementSet('save'))   $mode = 'save';   // Save entered data
63 if (isPostRequestElementSet('edit'))   $mode = 'edit';   // Edit data
64 if (isPostRequestElementSet('notify')) $mode = 'notify'; // Switch off notification
65
66 switch ($mode) {
67         case 'show': // Show his data
68                 if (isExtensionActive('country', true)) {
69                         // New way                         0        1         2          3         4     5     6        7           8            9       10      11           12           13
70                         $result = SQL_QUERY_ESC("SELECT surname, family, street_nr, country_code, zip, city, email, birth_day, birth_month, birth_year, gender, max_mails, receive_mails, last_update FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
71                                 array(getUserId()), __FILE__, __LINE__);
72                 } else {
73                         // Old way                         0        1         2        3      4     5     6        7           8            9       10      11           12           13
74                         $result = SQL_QUERY_ESC("SELECT surname, family, street_nr, country, zip, city, email, birth_day, birth_month, birth_year, gender, max_mails, receive_mails, last_update FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
75                                 array(getUserId()), __FILE__, __LINE__);
76                 }
77                 $content = SQL_FETCHARRAY($result, 0, false);
78                 SQL_FREERESULT($result);
79
80                 // Translate some things
81                 $content['gender']      = translateGender($content['gender']);
82                 $content['last_update'] = generateDateTime($content['last_update'], '0');
83
84                 // How far is last change on his profile away from now?
85                 if ((($content['last_update'] + getConfig('profile_lock')) > time()) && (!isAdmin()) && (getConfig('profile_lock') > 0)) {
86                         // You cannot change your account
87                         $content['change'] = loadTemplate('admin_settings_saved', true, "<div class=\"member_failed\">".sprintf(getMessage('MEMBER_PROFILE_LOCKED'), generateDateTime($content['last_update'] + getConfig('profile_lock'), '0'))."</div>");
88                 } else {
89                         // He is allowed to change his profile
90                         $content['change'] = loadTemplate('member_mydata_button', true);
91                 }
92
93                 if (strlen($content['birth_day'])   == 1) $content['birth_day']   = '0' . $content['birth_day'];
94                 if (strlen($content['birth_month']) == 1) $content['birth_month'] = '0' . $content['birth_month'];
95
96                 switch (getLanguage()) {
97                         case 'de': $content['dob'] = $content['birth_day']   . '.' . $content['birth_month'] . '.' . $content['birth_year']; break;
98                         default  : $content['dob'] = $content['birth_month'] . '-' . $content['birth_day']   . '-' . $content['birth_year']; break;
99                 } // END - switch
100
101                 if (isExtensionActive('country')) {
102                         // Load country's description and code
103                         $content['country'] = generateCountryInfo($content['country_code']);
104                 } // END - if
105
106                 // Merge data in
107                 $content = merge_array($content, $content);
108
109                 // Load template
110                 loadTemplate('member_mydata_overview', false, $content);
111                 break;
112
113         case 'edit': // Edit data
114                 if (isExtensionActive('country', true)) {
115                         // New way                          0          1            2             3         4       5       6           7            8              9          10         11               12            13
116                         $result = SQL_QUERY_ESC("SELECT `surname`, `family`, `street_nr`, `country_code`, `zip`, `city`, `email`, `birth_day`, `birth_month`, `birth_year`, `gender`, `max_mails`, `receive_mails`, `last_update`
117 FROM
118         `{?_MYSQL_PREFIX?}_user_data`
119 WHERE
120         `userid`=%s
121 LIMIT 1",
122                         array(getUserId()), __FILE__, __LINE__);
123                 } else {
124                         // Old way                          0          1            2          3       4       5       6           7            8              9          10         11               12            13
125                         $result = SQL_QUERY_ESC("SELECT `surname`, `family`, `street_nr`, `country`, `zip`, `city`, `email`, `birth_day`, `birth_month`, `birth_year`, `gender`, `max_mails`, `receive_mails`, `last_update`
126 FROM
127         `{?_MYSQL_PREFIX?}_user_data`
128 WHERE
129         `userid`=%s
130 LIMIT 1",
131                                 array(getUserId()), __FILE__, __LINE__);
132                 }
133
134                 $content = SQL_FETCHARRAY($result, 0, false);
135                 SQL_FREERESULT($result);
136                 $content['update_check'] = $content['last_update'] + getConfig('profile_lock');
137
138                 // How far is last change on his profile away from now?
139                 if (($content['update_check'] > time()) && (!isAdmin()) && (getConfig('profile_lock') > 0)) {
140                         $content['update_check'] = generateDateTime($content['update_check'] + getConfig('profile_lock'), '0');
141                         // You cannot change your account
142                         loadTemplate('member_mydata_locked', false, $content);
143                 } else {
144                         // He is allowed to change his profile
145                         foreach (array('gender_m','gender_f','gender_c') as $entry) {
146                                 $content[$entry] = '';
147                         } // END - foreach
148
149                         $content['gender_' . strtolower($content['gender'])] = ' selected="selected"';
150                         $content['dob'] = '';
151
152                         switch (getLanguage()) {
153                                 case 'de': // German date format
154                                         // Day
155                                         $content['dob'] .= addSelectionBox('day', $content['birth_day']);
156
157                                         // Month
158                                         $content['dob'] .= addSelectionBox('month', $content['birth_month']);
159
160                                         // Year
161                                         $content['dob'] .= addSelectionBox('year', $content['birth_year']);
162                                         break;
163
164                                 default: // Default is the US date format... :)
165                                         // Month
166                                         $content['dob'] .= addSelectionBox('month', $content['birth_month']);
167
168                                         // Day
169                                         $content['dob'] .= addSelectionBox('day', $content['birth_day']);
170
171                                         // Year
172                                         $content['dob'] .= addSelectionBox('year', $content['birth_year']);
173                                         break;
174                         } // END - if
175
176                         $content['max_receive_list'] = addMaxReceiveList('member', $content['max_mails'], true);
177
178                         if (isExtensionActive('country')) {
179                                 // Generate selection box
180                                 $OUT  = "<select name=\"country_code\" class=\"member_select\" size=\"1\">\n";
181                                 $whereStatement = "WHERE `is_active`='Y'";
182                                 if (isAdmin()) $whereStatement = '';
183                                 $OUT .= generateOptionList('countries', 'id', 'descr', $content['country_code'], 'code', $whereStatement);
184                                 $OUT .= "</select>";
185                                 $content['country'] = $OUT;
186                         } else {
187                                 // Ouput default input box
188                                 $content['country'] = "<input type=\"text\" name=\"cntry\" class=\"member_normal\" size=\"2\" maxlength=\"3\" value=\"".$content['country']."\" />";
189                         }
190
191                         // Merge data in
192                         $content = merge_array($content, $content);
193
194                         // Load template
195                         loadTemplate('member_mydata_edit', false, $content);
196                 }
197                 break;
198
199         case 'save': // Save entered data
200                 // Load old email / password:      0          1           2
201                 $result = SQL_QUERY_ESC("SELECT `email`, `password`, `last_update` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
202                         array(getUserId()), __FILE__, __LINE__);
203                 $content = SQL_FETCHARRAY($result, 0, false);
204                 SQL_FREERESULT($result);
205                 $content['last_change'] = $content['last_update'] + getConfig('profile_lock');
206
207                 // How far is last change on his profile away from now?
208                 if (($content['last_change'] > time()) && (!isAdmin()) && (getConfig('profile_lock') > 0)) {
209                         $content['last_change'] = generateDateTime($content['last_change'] + getConfig('profile_lock'), '0');
210                         // You cannot change your account
211                         loadTemplate('member_mydata_locked');
212                 } elseif ((!isEmailValid(postRequestElement('email'))) && (!isAdmin())) {
213                         // Invalid email address!
214                         loadTemplate('admin_settings_saved', false, getMessage('INVALID_EMAIL_ADDRESS_ENTERED'));
215                 } else {
216                         // Generate hash
217                         $hash = generateHash(postRequestElement('pass1'), substr($content['password'], 0, -40));
218                         if ((($hash == $content['password']) || (postRequestElement('pass1') == postRequestElement('pass2'))) && (isPostRequestElementSet('pass1'))) {
219                                 // Only on simple changes normal mode is active = no email or password changed
220                                 $mode = 'normal'; $AND = '';
221
222                                 // Did the user changed the password?
223                                 if ($hash != $content['password']) { $AND = ", `password`='".$hash."'"; $mode = 'pass'; }
224
225                                 // Or did he changed his password?
226                                 if (postRequestElement('email') != $content['email']) {
227                                         // Jupp
228                                         if ($mode == 'normal') {
229                                                 $mode = 'email';
230                                         } else {
231                                                 $mode .= ';email';
232                                         }
233                                         setRequestPostElement('old_email', $content['email']);
234                                 } // END - if
235
236                                 // Update member's profile
237                                 if (isExtensionActive('country')) {
238                                         // New way
239                                         SQL_QUERY_ESC("UPDATE
240         `{?_MYSQL_PREFIX?}_user_data`
241 SET
242         `gender`='%s', `surname`='%s', `family`='%s',
243         `street_nr`='%s',
244         `country_code`=%s, `zip`=%s, `city`='%s',
245         `email`='%s',
246         `birth_day`=%s, `birth_month`=%s, `birth_year`=%s,
247         `max_mails`='%s',
248         `last_update`=UNIX_TIMESTAMP()".$AND.",
249         `notified`='N',
250         `last_profile_sent`=UNIX_TIMESTAMP()
251 WHERE
252         `userid`=%s
253 LIMIT 1",
254                                                 array(
255                                                         postRequestElement('gender'),
256                                                         postRequestElement('surname'),
257                                                         postRequestElement('family'),
258                                                         postRequestElement('street_nr'),
259                                                         bigintval(postRequestElement('country_code')),
260                                                         bigintval(postRequestElement('zip')),
261                                                         postRequestElement('city'),
262                                                         postRequestElement('email'),
263                                                         bigintval(postRequestElement('day')),
264                                                         bigintval(postRequestElement('month')),
265                                                         bigintval(postRequestElement('year')),
266                                                         bigintval(postRequestElement('max_mails')),
267                                                         getUserId()
268                                                 ), __FILE__, __LINE__);
269                                 } else {
270                                         // Old way
271                                         SQL_QUERY_ESC("UPDATE
272         `{?_MYSQL_PREFIX?}_user_data`
273 SET
274         `gender`='%s', `surname`='%s', `family`='%s',
275         `street_nr`='%s',
276         `country`='%s', `zip`=%s, `city`='%s',
277         `email`='%s',
278         `birth_day`=%s, `birth_month`=%s, `birth_year`=%s,
279         `max_mails`='%s',
280         `last_update`=UNIX_TIMESTAMP()".$AND.",
281         `notified`='N',
282         `last_profile_sent`=UNIX_TIMESTAMP()
283 WHERE
284         `userid`=%s
285 LIMIT 1",
286                                                 array(
287                                                         postRequestElement('gender'),
288                                                         postRequestElement('surname'),
289                                                         postRequestElement('family'),
290                                                         postRequestElement('street_nr'),
291                                                         postRequestElement('cntry'),
292                                                         bigintval(postRequestElement('zip')),
293                                                         postRequestElement('city'),
294                                                         postRequestElement('email'),
295                                                         bigintval(postRequestElement('day')),
296                                                         bigintval(postRequestElement('month')),
297                                                         bigintval(postRequestElement('year')),
298                                                         bigintval(postRequestElement('max_mails')),
299                                                         getUserId()
300                                                 ), __FILE__, __LINE__);
301                                 }
302
303                                 // Did something change?
304                                 if (SQL_AFFECTEDROWS() == 1) {
305                                         // Get all modes ...
306                                         $modes = explode(';', $mode);
307
308                                         // ... and run them through
309                                         sendModeMails ('mydata', $modes);
310                                 } else {
311                                         // Something went wrong
312                                         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_UPDATE_FAILED'));
313                                 }
314                         } else {
315                                 // Entered wrong pass for updating profile
316                                 loadTemplate('admin_settings_saved', false, getMessage('MEBER_UPDATE_PWD_WRONG'));
317                         }
318                 }
319                 break;
320
321         case 'notify': // Switch off notfication
322                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `notified`='N', `last_update`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
323                         array(getUserId()), __FILE__, __LINE__);
324                 $URL = 'modules.php?module=login&amp;what=welcome&amp;code=' . urlencode(getMessage('PROFILE_UPDATED'));
325                 break;
326 } // END - switch
327
328 if (!empty($URL)) {
329         // Load generated URL
330         redirectToUrl($URL);
331 } // END - if
332
333 // [EOF]
334 ?>