Merge branch 'contrib' into 0.2.1-FINAL
[mailer.git] / inc / modules / member / what-mydata.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         exit();
41 } elseif (!isMember()) {
42         redirectToIndexMemberOnlyModule();
43 }
44
45 // Add description as navigation point
46 addYouAreHereLink('member', __FILE__);
47
48 if ((!isExtensionActive('mydata')) && (!isAdmin())) {
49         displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=mydata%}');
50         return;
51 } // END - if
52
53 // Add userid
54 $content = array();
55
56 // Init variable to prevent notices
57 $url = '';
58
59 // Detect what the member wants to do
60 $mode = 'show'; // Show his data
61 if (isFormSent('save'))   $mode = 'save';   // Save entered data
62 if (isFormSent('edit'))   $mode = 'edit';   // Edit data
63 if (isFormSent('notify')) $mode = 'notify'; // Switch off notification
64
65 switch ($mode) {
66         case 'show': // Show his data
67                 // Get line
68                 $content = merge_array($content, getUserDataArray());
69
70                 // Translate some things
71                 $content['last_update'] = generateDateTime($content['last_update'], 0);
72
73                 // How far is last change on his profile away from now?
74                 if ((($content['last_update'] + getProfileLock()) > time()) && (!isAdmin()) && (getProfileLock() > 0)) {
75                         // You cannot change your account
76                         $content['change'] = returnMessage('<div class="notice">{%message,MEMBER_PROFILE_LOCKED=' . generateDateTime($content['last_update'] + getProfileLock(), 0) . '%}</div>');
77                 } else {
78                         // He is allowed to change his profile
79                         $content['change'] = loadTemplate('member_mydata_button', TRUE);
80                 }
81
82                 // @TODO Move this to a function (for better EL code)
83                 switch (getLanguage()) {
84                         case 'de': $content['dob'] = '{%pipe,padLeftZero=' . $content['birth_day']   . '%}.{%pipe,padLeftZero=' . $content['birth_month'] . '%}.' . $content['birth_year']; break;
85                         default  : $content['dob'] = '{%pipe,padLeftZero=' . $content['birth_month'] . '%}-{%pipe,padLeftZero=' . $content['birth_day']   . '%}-' . $content['birth_year']; break;
86                 } // END - switch
87
88                 if (isExtensionActive('country')) {
89                         // Load country's description and code
90                         $content['country'] = generateCountryInfo($content['country_code']);
91                 } // END - if
92
93                 // Load template
94                 loadTemplate('member_mydata_overview', FALSE, $content);
95                 break;
96
97         case 'edit': // Edit data
98                 if (isExtensionActive('country', TRUE)) {
99                         // New way
100                         $result = sqlQueryEscaped('SELECT
101         `surname`,
102         `family`,
103         `street_nr`,
104         `country_code`,
105         `zip`,
106         `city`,
107         `email`,
108         `birth_day`,
109         `birth_month`,
110         `birth_year`,
111         `gender`,
112         `max_mails`,
113         `receive_mails`,
114         `last_update`
115 FROM
116         `{?_MYSQL_PREFIX?}_user_data`
117 WHERE
118         `userid`=%s
119 LIMIT 1',
120                         array(getMemberId()), __FILE__, __LINE__);
121                 } else {
122                         // Old way
123                         $result = sqlQueryEscaped('SELECT
124         `surname`,
125         `family`,
126         `street_nr`,
127         `country`,
128         `zip`,
129         `city`,
130         `email`,
131         `birth_day`,
132         `birth_month`,
133         `birth_year`,
134         `gender`,
135         `max_mails`,
136         `receive_mails`,
137         `last_update`
138 FROM
139         `{?_MYSQL_PREFIX?}_user_data`
140 WHERE
141         `userid`=%s
142 LIMIT 1',
143                                 array(getMemberId()), __FILE__, __LINE__);
144                 }
145
146                 // Get line
147                 $content = merge_array($content, sqlFetchArray($result));
148
149                 // Free result
150                 sqlFreeResult($result);
151
152                 $content['update_check'] = $content['last_update'] + getProfileLock();
153
154                 // How far is last change on his profile away from now?
155                 if (($content['update_check'] > time()) && (!isAdmin()) && (getProfileLock() > 0)) {
156                         // Profile is locked, so output propper message
157                         $content['update_check'] = '{%message,MEMBER_PROFILE_LOCKED=' . generateDateTime($content['update_check'] + getProfileLock(), '0') . '%}';
158
159                         // You cannot change your account
160                         displayMessage($content['update_check']);
161                 } else {
162                         // Init elements
163                         $content['dob'] = '';
164
165                         switch (getLanguage()) {
166                                 case 'de': // German date format
167                                         // Day
168                                         $content['dob'] .= addSelectionBox('da', $content['birth_day']);
169
170                                         // Month
171                                         $content['dob'] .= addSelectionBox('mo', $content['birth_month']);
172
173                                         // Year
174                                         $content['dob'] .= addSelectionBox('ye', $content['birth_year']);
175                                         break;
176
177                                 default: // Default is the US date format... :)
178                                         // Month
179                                         $content['dob'] .= addSelectionBox('mo', $content['birth_month']);
180
181                                         // Day
182                                         $content['dob'] .= addSelectionBox('da', $content['birth_day']);
183
184                                         // Year
185                                         $content['dob'] .= addSelectionBox('ye', $content['birth_year']);
186                                         break;
187                         } // END - if
188
189                         $content['max_receive_list'] = addMaxReceiveList('member', $content['max_mails']);
190
191                         if (isExtensionActive('country')) {
192                                 // Init WHERE statement
193                                 $whereStatement = " WHERE `is_active`='Y'";
194                                 if (isAdmin()) $whereStatement = '';
195
196                                 // Generate selection box
197                                 $OUT  = '<select name="country_code" class="form_select" size="1">';
198                                 $OUT .= generateOptions('countries', 'id', 'descr', $content['country_code'], 'code', $whereStatement);
199                                 $OUT .= '</select>';
200                                 $content['country'] = $OUT;
201                         } else {
202                                 // Ouput default input box
203                                 $content['country'] = '<input type="text" name="cntry" class="form_field" size="2" maxlength="3" value="' . $content['country'] . '" />';
204                         }
205
206                         // Load template
207                         loadTemplate('member_mydata_edit', FALSE, $content);
208                 }
209                 break;
210
211         case 'save': // Save entered data
212                 // Get line
213                 $content = merge_array($content, getUserDataArray());
214
215                 // Calculate time to check
216                 $content['update_check'] = $content['last_update'] + getProfileLock();
217
218                 // How far is last change on his profile away from now?
219                 if (($content['update_check'] > time()) && (!isAdmin()) && (getProfileLock() > 0)) {
220                         // You cannot change your account
221                         $content['update_check'] = generateDateTime($content['update_check'] + getProfileLock(), 0);
222
223                         displayMessage($content['update_check']);
224                 } elseif ((!isEmailValid(postRequestElement('email'))) && (!isAdmin())) {
225                         // Invalid email address!
226                         displayMessage('{--INVALID_EMAIL_ENTERED--}');
227                 } elseif ((isExtensionInstalledAndNewer('other', '0.3.0')) && (isCheckDoubleEmailEnabled()) && (!isAdmin()) && (isEmailTaken(postRequestElement('email')))) {
228                         // Email address is already registered
229                         displayMessage('{--MEMBER_EMAIL_IS_ALREADY_REGISTERED--}');
230                 } else {
231                         // Generate hash
232                         $hash = generateHash(postRequestElement('password1'), substr($content['password'], 0, -40));
233                         if ((($hash == $content['password']) || (postRequestElement('password1') == postRequestElement('password2'))) && (isPostRequestElementSet('password1'))) {
234                                 // Only on simple changes normal mode is active = no email or password changed
235                                 $mode = 'normal';
236                                 $AND = '';
237
238                                 // Did the user changed the password?
239                                 if ($hash != $content['password']) {
240                                         // Yes
241                                         $AND = ",`password`='" . $hash . "'";
242                                         $mode = 'password';
243                                 } // END - if
244
245                                 // Or did he changed his email address?
246                                 if (postRequestElement('email') != $content['email']) {
247                                         // Yes, but is it maybe blacklisted?
248                                         $filterData = array(
249                                                 'init_done'   => TRUE,
250                                                 'post_data'   => postRequestArray(),
251                                                 'blacklisted' => '',
252                                                 'message'     => '{--PRE_UPDATE_USER_DATA_FAILED--}',
253                                         );
254                                         $filterData = runFilterChain('pre_update_user_data', $filterData);
255
256                                         // Is it blacklisted?
257                                         if ($filterData['init_done'] === FALSE) {
258                                                 // Found something blacklisted
259                                                 displayMessage($filterData['message']);
260                                                 return;
261                                         } // END - if
262
263                                         if ($mode == 'normal') {
264                                                 $mode = 'email';
265                                         } else {
266                                                 $mode .= ';email';
267                                         }
268                                         setPostRequestElement('old_email', $content['email']);
269                                 } // END - if
270
271                                 // Update member's profile
272                                 if (isExtensionActive('country')) {
273                                         // New way
274                                         sqlQueryEscaped("UPDATE
275         `{?_MYSQL_PREFIX?}_user_data`
276 SET
277         `gender`='%s',
278         `surname`='%s',
279         `family`='%s',
280         `street_nr`='%s',
281         `country_code`=%s,
282         `zip`=%s,
283         `city`='%s',
284         `email`='%s',
285         `birth_day`=%s,
286         `birth_month`=%s,
287         `birth_year`=%s,
288         `max_mails`='%s',
289         `last_update`=UNIX_TIMESTAMP()".$AND.",
290         `notified`='N',
291         `last_profile_sent`=UNIX_TIMESTAMP()
292 WHERE
293         `userid`=%s
294 LIMIT 1",
295                                                 array(
296                                                         postRequestElement('gender'),
297                                                         postRequestElement('surname'),
298                                                         postRequestElement('family'),
299                                                         postRequestElement('street_nr'),
300                                                         bigintval(postRequestElement('country_code')),
301                                                         bigintval(postRequestElement('zip')),
302                                                         postRequestElement('city'),
303                                                         postRequestElement('email'),
304                                                         bigintval(postRequestElement('day')),
305                                                         bigintval(postRequestElement('month')),
306                                                         bigintval(postRequestElement('year')),
307                                                         bigintval(postRequestElement('max_mails')),
308                                                         getMemberId()
309                                                 ), __FILE__, __LINE__);
310                                 } else {
311                                         // Old way
312                                         sqlQueryEscaped("UPDATE
313         `{?_MYSQL_PREFIX?}_user_data`
314 SET
315         `gender`='%s',
316         `surname`='%s',
317         `family`='%s',
318         `street_nr`='%s',
319         `country`='%s',
320         `zip`=%s,
321         `city`='%s',
322         `email`='%s',
323         `birth_day`=%s,
324         `birth_month`=%s,
325         `birth_year`=%s,
326         `max_mails`='%s',
327         `last_update`=UNIX_TIMESTAMP()".$AND.",
328         `notified`='N',
329         `last_profile_sent`=UNIX_TIMESTAMP()
330 WHERE
331         `userid`=%s
332 LIMIT 1",
333                                                 array(
334                                                         postRequestElement('gender'),
335                                                         postRequestElement('surname'),
336                                                         postRequestElement('family'),
337                                                         postRequestElement('street_nr'),
338                                                         postRequestElement('cntry'),
339                                                         bigintval(postRequestElement('zip')),
340                                                         postRequestElement('city'),
341                                                         postRequestElement('email'),
342                                                         bigintval(postRequestElement('day')),
343                                                         bigintval(postRequestElement('month')),
344                                                         bigintval(postRequestElement('year')),
345                                                         bigintval(postRequestElement('max_mails')),
346                                                         getMemberId()
347                                                 ), __FILE__, __LINE__);
348                                 }
349
350                                 // Did something change?
351                                 if (!ifSqlHasZeroAffectedRows()) {
352                                         // Get all modes ...
353                                         $modes = explode(';', $mode);
354
355                                         // ... and run them through
356                                         sendModeMails ('mydata', $modes);
357                                 } else {
358                                         // Something went wrong
359                                         displayMessage('{--MEMBER_UPDATE_FAILED--}');
360                                 }
361                         } else {
362                                 // Entered wrong pass for updating profile
363                                 displayMessage('{--MEMBER_UPDATE_WRONG_PASSWORD--}');
364                         }
365                 }
366                 break;
367
368         case 'notify': // Switch off notfication
369                 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `notified`='N',`last_update`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
370                         array(getMemberId()), __FILE__, __LINE__);
371                 $url = 'modules.php?module=login&amp;what=mydata&amp;code=' . getCode('PROFILE_UPDATED');
372                 break;
373 } // END - switch
374
375 if (!empty($url)) {
376         // Load generated URL
377         redirectToUrl($url);
378 } // END - if
379
380 // [EOF]
381 ?>