Possible fix for bigint() problem, some code-cleanups
[mailer.git] / inc / modules / member / what-order.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/19/2003 *
4  * ===================                          Last change: 08/26/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-order.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Order mails here                                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Hier koennen Ihre Mitglieder Mails buchen        *
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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } elseif (!isMember()) {
44         redirectToIndexMemberOnlyModule();
45 }
46
47 // Add description as navigation point
48 addMenuDescription('member', __FILE__);
49
50 if ((!isExtensionActive('order')) && (!isAdmin())) {
51         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('order'));
52         return;
53 } // END - if
54
55 $URL = ''; $id = '0';
56 $whereStatement = " WHERE `visible`='Y'";
57
58 // Set undefined array elements
59 if (isAdmin()) $whereStatement = '';
60
61 // Count unconfirmed mails
62 $links = countSumTotalData(getMemberId(), 'user_links', 'id', 'userid', true);
63
64 // Do we have ext-holiday installed?
65 // @TODO Rewrite this to a filter
66 $extraColumn = 'userid';
67 if ((isExtensionActive('holiday')) && (getExtensionVersion('holiday') >= '0.1.3')) {
68         // Fetch also holiday activation data
69         $extraColumn = 'holiday_active';
70 } // END - if
71
72 $result_mmails = SQL_QUERY_ESC("SELECT
73         `userid`, `receive_mails`, `mail_orders`, `".$extraColumn."`
74 FROM
75         `{?_MYSQL_PREFIX?}_user_data`
76 WHERE
77         `userid`=%s AND
78         `max_mails` > 0
79 LIMIT 1",
80         array(getMemberId()), __FILE__, __LINE__);
81
82 $mmails = SQL_NUMROWS($result_mmails);
83 list($DMY, $MAXI, $ORDERS, $HOLIDAY) = SQL_FETCHROW($result_mmails);
84
85 // Free result
86 SQL_FREERESULT($result_mmails);
87
88 // Fix non-existent ext-holidy
89 if ($HOLIDAY == $DMY) $HOLIDAY = 'N';
90
91 $ALLOWED = $MAXI - $ORDERS;
92 if (getConfig('order_max_full') == 'MAX') $ALLOWED = $MAXI;
93
94 // Now check his points amount
95 $total = countSumTotalData(getMemberId(), 'user_points', 'points') - countSumTotalData(getMemberId(), 'user_data', 'used_points');;
96
97 if (($HOLIDAY == 'Y') && (getExtensionVersion('holiday') >= '0.1.3')) {
98         // Holiday is active!
99         loadTemplate('admin_settings_saved', false, getMessage('HOLIDAY_ORDER_NOT_POSSIBLE'));
100 } elseif ((isPostRequestParameterSet('frametester')) && ($ALLOWED > 0) && (postRequestParameter('receiver') > 0)) {
101         // Continue with the frametester, we first need to store the data temporary in the pool
102         //
103         // First we would like to store the data and get it's pool position back...
104         $result = SQL_QUERY_ESC("SELECT `id`, `data_type`
105 FROM
106         `{?_MYSQL_PREFIX?}_pool`
107 WHERE
108         `sender`=%s AND
109         `url`='%s' AND
110         `timestamp` > (UNIX_TIMESTAMP() - %s)
111 LIMIT 1",
112                 array(
113                         getMemberId(),
114                         postRequestParameter('url'),
115                         getConfig('url_tlock')
116                 ), __FILE__, __LINE__);
117
118         $type = 'TEMP'; $id = '0';
119         if (SQL_NUMROWS($result) == 1) {
120                 // Load id and mail type
121                 list($id, $type) = SQL_FETCHROW($result);
122         } // END - if
123
124         // Free result
125         SQL_FREERESULT($result);
126
127         if ($type == 'TEMP') {
128                 // No entry found, so we need to check out the stats table as well... :)
129                 // We have to add that suff here, now we continue WITHOUT checking and check the text and subject against some filters
130                 $URL = '';
131                 if (getConfig('allow_url_in_text') == 'Y') {
132                         // Test submitted text against some filters (length, URLs in text etc.)
133                         if ((strpos(strtolower(postRequestParameter('text')), 'https://') > -1) || (strpos(strtolower(postRequestParameter('text')), 'http://') > -1) || (strpos(strtolower(postRequestParameter('text')), "www") > -1)) {
134                                 // URL found!
135                                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('URL_FOUND');
136                         } // END - if
137
138                         // Remove new-line and carriage-return characters
139                         $TEST = str_replace("\n", '', str_replace("\r", '', postRequestParameter('text')));
140
141                         // Text length within allowed length?
142                         if (strlen($TEST) > getConfig('max_tlength')) {
143                                 // Text is too long!
144                                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('OVERLENGTH');
145                         } // END - if
146                 } // END - if
147
148                 // Shall I test the subject line against URLs?
149                 if (getConfig('allow_url_in_subject') == 'Y') {
150                         // Check the subject line for issues
151                         setPostRequestParameter('subject', str_replace("\\", '[nl]', substr(postRequestParameter('subject'), 0, 200)));
152                         if ((strpos(strtolower(postRequestParameter('subject')), 'http://') > -1) || (strpos(strtolower(postRequestParameter('subject')), "www") > -1)) {
153                                 // URL in subject found
154                                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('SUBJ_URL');
155                         } // END - if
156                 } // END - if
157
158                 // And shall I check that his URL is not in the black list?
159                 if (getConfig('url_blacklist') == 'Y') {
160                         // Ok, I do that for you know...
161                         $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`timestamp`) AS tstamp FROM `{?_MYSQL_PREFIX?}_url_blacklist` WHERE `url`='%s' LIMIT 1",
162                                 array(postRequestParameter('url')), __FILE__, __LINE__);
163
164                         if (SQL_NUMROWS($result) == 1) {
165                                 // Jupp, we got one listed
166                                 list($blist) = SQL_FETCHROW($result);
167
168                                 // Create redirect-URL
169                                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('BLIST_URL') . '&amp;blist=' . $blist;
170                         } // END - if
171
172                         // Free result
173                         SQL_FREERESULT($result);
174                 } // END - if
175
176                 // Enougth receivers entered?
177                 if ((postRequestParameter('receiver') < getConfig('order_min')) && (!isAdmin())) {
178                         // Less than allowed receivers entered!
179                         $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('MORE_RECEIVERS3');
180                 } // END - if
181
182                 // Validate URL
183                 if (!isUrlValid(postRequestParameter('url'))) {
184                         // URL is invalid!
185                         $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('INVALID_URL');
186                 } // END - if
187
188                 // Probe for HTML extension
189                 if (isExtensionActive('html_mail')) {
190                         // HTML or regular text mail?
191                         if (postRequestParameter('html') == 'Y') {
192                                 // Chek for valid HTML tags
193                                 setPostRequestParameter('text', checkHtmlTags(postRequestParameter('text')));
194
195                                 // Maybe invalid tags found?
196                                 if (!isPostRequestParameterSet('text')) $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('INVALID_TAGS')."&amp;id=".$id;
197                         } else {
198                                 // Remove any HTML code
199                                 setPostRequestParameter('text', str_replace('<', '{OPEN_HTML}', str_replace('>', '{CLOSE_HTML}', postRequestParameter('text'))));
200                         }
201                 }
202         } elseif (!isAdmin()) {
203                 // He has already sent a mail within a specific time
204                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('URL_TLOCK') . '&amp;id=' . $id;
205         }
206
207         // Still no error?
208         if (empty($URL)) {
209                 // Check if category and number of receivers is okay
210                 $add = '';
211                 if ((getConfig('order_multi_page') == 'Y') && (isPostRequestParameterSet('zip')) && (postRequestParameter('zip') != '')) {
212                         // Choose recipients by ZIP code
213                         $add = " AND d.zip LIKE '".bigintval(postRequestParameter('zip'))."{PER}'";
214                 } // END - if
215
216                 // Check for userids
217                 $result = SQL_QUERY_ESC("SELECT
218         c.userid
219 FROM
220         `{?_MYSQL_PREFIX?}_user_cats` AS c
221 LEFT JOIN
222         `{?_MYSQL_PREFIX?}_user_data` AS d
223 ON
224         c.userid=d.userid
225 WHERE
226         c.cat_id=%s AND c.userid != '%s' AND d.`status`='CONFIRMED' AND d.receive_mails > 0".$add."
227 ORDER BY
228         d.%s %s",
229                         array(
230                                 bigintval(postRequestParameter('cat')),
231                                 getMemberId(),
232                                 getConfig('order_select'),
233                                 getConfig('order_mode'),
234                         ), __FILE__, __LINE__);
235
236                 // Do we enougth receivers left?
237                 if (SQL_NUMROWS($result) >= postRequestParameter('receiver')) {
238                         // Load receivers from database
239                         $TEST = array(); $cnt = '0';
240                         while ($holidayContent = SQL_FETCHARRAY($result)) {
241                                 if (getExtensionVersion('holiday') >= '0.1.3') {
242                                         // Check for his holiday status
243                                         $result_holiday = SQL_QUERY_ESC("SELECT
244         `id`
245 FROM
246         `{?_MYSQL_PREFIX?}_user_holidays`
247 WHERE
248         `userid`=%s AND `holiday_start` < UNIX_TIMESTAMP() AND `holiday_end` > UNIX_TIMESTAMP()
249 LIMIT 1",
250                                                 array($holidayContent['userid']), __FILE__, __LINE__);
251                                         if (SQL_NUMROWS($result_holiday) == 1) $holidayContent['userid'] = '0'; // Exclude user who are in holiday
252
253                                         // Free memory
254                                         SQL_FREERESULT($result_holiday);
255                                 } // END - if
256
257                                 if ($holidayContent['userid'] > 0) {
258                                         // Add receiver
259                                         $TEST[] = $holidayContent['userid'];
260                                         $cnt++;
261                                 } // END - if
262                         } // END - while
263
264                         // Free memory
265                         SQL_FREERESULT($result);
266
267                         // Implode array into string for the sending pool
268                         $receiver = implode($TEST, ';');
269
270                         // Count array for maximum sent
271                         $content['target_send'] = count($TEST);
272
273                         // Update receiver list
274                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receive_mails`=`receive_mails`-1 WHERE `userid` IN (%s) LIMIT %s",
275                                 array(convertReceivers($receiver), $content['target_send']), __FILE__, __LINE__);
276
277                         // Is calculated max receivers larger than wanted receivers then reset it
278                         if ($content['target_send'] > postRequestParameter('receiver')) $content['target_send'] = bigintval(postRequestParameter('receiver'));
279
280                         // Calculate used points
281                         $USED = $content['target_send'] * getPaymentPoints(bigintval(postRequestParameter('type')));
282
283                         // Fix empty zip code
284                         if (!isPostRequestParameterSet('zip')) setPostRequestParameter('zip', 0);
285
286                         // Check if he has enougth points for this order and selected more than 0 receivers
287                         if (($USED > 0) && ($USED <= $total) && ($content['target_send'] > 0)) {
288                                 // Gettings points is okay, so we can add $USED later from
289                                 if (($id == '0') || ($type != 'TEMP')) {
290                                         // New order
291                                         $id = '0';
292                                         if (isExtensionActive('html_mail')) {
293                                                 // HTML extension is active
294                                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_pool` (`sender`, `subject`, `text`, `receivers`, `payment_id`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `zip`, `html_msg`)
295  VALUES ('%s','%s','%s','%s','%s','TEMP',UNIX_TIMESTAMP(),'%s','%s','%s','%s','%s')",
296                                                         array(
297                                                                 getMemberId(),
298                                                                 postRequestParameter('subject'),
299                                                                 postRequestParameter('text'),
300                                                                 $receiver,
301                                                                 bigintval(postRequestParameter('type')),
302                                                                 postRequestParameter('url'),
303                                                                 bigintval(postRequestParameter('cat')),
304                                                                 $content['target_send'],
305                                                                 bigintval(postRequestParameter('zip')),
306                                                                 postRequestParameter('html')
307                                                         ), __FILE__, __LINE__);
308                                         } else {
309                                                 // No HTML extension is active
310                                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_pool` (`sender`, `subject`, `text`, `receivers`, `payment_id`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `zip`)
311  VALUES ('%s','%s','%s','%s','%s','TEMP',UNIX_TIMESTAMP(),'%s','%s','%s','%s')",
312                                                         array(
313                                                                 getMemberId(),
314                                                                 postRequestParameter('subject'),
315                                                                 postRequestParameter('text'),
316                                                                 $receiver,
317                                                                 bigintval(postRequestParameter('type')),
318                                                                 postRequestParameter('url'),
319                                                                 bigintval(postRequestParameter('cat')),
320                                                                 $content['target_send'],
321                                                                 bigintval(postRequestParameter('zip')),
322                                                         ), __FILE__, __LINE__);
323                                         }
324                                 } else {
325                                         // Change current order
326                                         if (isExtensionActive('html_mail')) {
327                                                 // HTML extension is active
328                                                 SQL_QUERY_ESC("UPDATE
329         `{?_MYSQL_PREFIX?}_pool`
330 SET
331         `subject`='%s',
332         `text`='%s',
333         `receivers`='%s',
334         `payment_id`=%s,
335         `timestamp`=UNIX_TIMESTAMP(),
336         `url`='%s',
337         `cat_id`=%s,
338         `target_send`=%s,
339         `zip`=%s,
340         `html_msg`='%s'
341 WHERE
342         `id`=%s
343 LIMIT 1",
344                                                         array(
345                                                                 postRequestParameter('subject'),
346                                                                 postRequestParameter('text'),
347                                                                 $receiver,
348                                                                 bigintval(postRequestParameter('type')),
349                                                                 postRequestParameter('url'),
350                                                                 bigintval(postRequestParameter('cat')),
351                                                                 $content['target_send'],
352                                                                 bigintval(postRequestParameter('zip')),
353                                                                 postRequestParameter('html'),
354                                                                 bigintval($id)
355                                                         ), __FILE__, __LINE__);
356                                         } else {
357                                                 // No HTML extension is active
358                                                 SQL_QUERY_ESC("UPDATE
359         `{?_MYSQL_PREFIX?}_pool`
360 SET
361         `subject`='%s',
362         `text`='%s',
363         `receivers`='%s',
364         `payment_id`=%s,
365         `timestamp`=UNIX_TIMESTAMP(),
366         `url`='%s',
367         `cat_id`=%s,
368         `target_send`=%s,
369         `zip`=%s
370 WHERE
371         `id`=%s
372 LIMIT 1",
373                                                         array(
374                                                                 postRequestParameter('subject'),
375                                                                 postRequestParameter('text'),
376                                                                 $receiver,
377                                                                 bigintval(postRequestParameter('type')),
378                                                                 postRequestParameter('url'),
379                                                                 bigintval(postRequestParameter('cat')),
380                                                                 $content['target_send'],
381                                                                 bigintval(postRequestParameter('zip')),
382                                                                 bigintval($id)
383                                                         ), __FILE__, __LINE__);
384                                         }
385                                 }
386
387                                 // Do we need to get the id number?
388                                 if ($id == '0') {
389                                         // Order is placed as temporary. We need to get it's id for the frametester
390                                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_pool` WHERE `sender`=%s AND `subject`='%s' AND `payment_id`=%s AND `data_type`='TEMP' AND `timestamp`=UNIX_TIMESTAMP() LIMIT 1",
391                                                 array(
392                                                         getMemberId(),
393                                                         postRequestParameter('subject'),
394                                                         bigintval(postRequestParameter('type'))
395                                                 ), __FILE__, __LINE__);
396
397                                         // Get pool id
398                                         list($id) = SQL_FETCHROW($result);
399
400                                         // Free result
401                                         SQL_FREERESULT($result);
402                                 } // END - if
403
404                                 // id is received so we can redirect the user, used points will be added when he send's out the mail
405                                 $URL = 'modules.php?module=frametester&amp;order=' . $id;
406                         } elseif ($content['target_send'] == '0') {
407                                 // Not enougth receivers found which can receive mails
408                                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('MORE_RECEIVERS2');
409                         } else {
410                                 // No enougth points left!
411                                 $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('MORE_POINTS');
412                         }
413                 } else  {
414                         // Ordered more mails than he can send in this category
415                         $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('NO_RECS_LEFT');
416                 }
417         }
418 } elseif (postRequestParameter('receiver') == '0') {
419         // Not enougth receivers selected
420         $URL = 'modules.php?module=login&amp;what=order&amp;code=' . getCode('MORE_RECEIVERS1');
421 } elseif (($ALLOWED == '0') && (getConfig('order_max_full') == 'ORDER')) {
422         // No more mail orders allowed
423         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_ORDER_ALLOWED_EXHAUSTED'));
424 } elseif (($links < getConfig('unconfirmed')) && ($mmails == 1)) {
425         // Display order form
426         $result_cats = SQL_QUERY("SELECT
427         `id`, `cat`
428 FROM
429         `{?_MYSQL_PREFIX?}_cats`
430 ".$whereStatement."
431 ORDER BY
432         `sort` ASC", __FILE__, __LINE__);
433         if (SQL_NUMROWS($result_cats) > 0) {
434                 if ($total > 0) {
435                         // Initialize array...
436                         $categories = array(
437                                 'id'      => array(),
438                                 'name'    => array(),
439                                 'userids' => array()
440                         );
441
442                         // Enable HTML checking
443                         // @TODO Rewrite this to a filter
444                         $HTML = ''; $HOL_STRING = '';
445                         if ((isExtensionActive('html_mail')) && (postRequestParameter('html') == 'Y')) $HTML = " AND `html`='Y'";
446                         if (getExtensionVersion('holiday') >= '0.1.3') {
447                                 // Extension's version is fine
448                                 $HOL_STRING = " AND `holiday_active`='N'";
449                         } // END - if
450
451                         // ... and begin loading stuff
452                         while ($categoriesContent = SQL_FETCHARRAY($result_cats)) {
453                                 $categories['id'][]   = bigintval($categoriesContent['id']);
454                                 $categories['name'][] = $categoriesContent['cat'];
455
456                                 // Select users in current category
457                                 $result_userids = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s AND `userid` != '%s' ORDER BY `userid` ASC",
458                                         array(bigintval($categoriesContent['id']), getMemberId()), __FILE__, __LINE__);
459
460                                 $userid_cnt = '0';
461                                 while (list($ucat) = SQL_FETCHROW($result_userids)) {
462                                         // Check for holiday system
463                                         $HOL_ACTIVE = false;
464                                         if (getExtensionVersion('holiday') >= '0.1.3') {
465                                                 // Check user's holiday status
466                                                 $result_holiday = SQL_QUERY_ESC("SELECT
467         d.userid FROM `{?_MYSQL_PREFIX?}_user_data` AS d
468 LEFT JOIN
469         `{?_MYSQL_PREFIX?}_user_holidays` AS h
470 ON
471         d.userid=h.userid
472 WHERE
473         d.userid=%s AND
474         d.receive_mails > 0 AND
475         d.`status`='CONFIRMED' AND
476         d.`holiday_active`='Y' AND
477         h.holiday_start < UNIX_TIMESTAMP() AND
478         h.holiday_end > UNIX_TIMESTAMP()
479 LIMIT 1",
480                                                         array(bigintval($ucat)), __FILE__, __LINE__);
481
482                                                 // Is holiday is active?
483                                                 $HOL_ACTIVE = (SQL_NUMROWS($result_holiday) == 1);
484
485                                                 // Free memory
486                                                 SQL_FREERESULT($result_holiday);
487                                         } // END - if
488
489                                         if ($HOL_ACTIVE === false) {
490                                                 // Check if the user want's to receive mails?
491                                                 $result_ver = SQL_QUERY_ESC("SELECT `zip` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s".$HTML." AND `receive_mails` > 0 AND `status`='CONFIRMED' LIMIT 1",
492                                                         array(bigintval($ucat)), __FILE__, __LINE__);
493
494                                                 if ((SQL_NUMROWS($result_ver) == 1) && (isPostRequestParameterSet('zip')) && (getConfig('order_multi_page') == 'Y')) {
495                                                         // Get zip code
496                                                         list($zip) = SQL_FETCHROW($result_ver);
497                                                         if (substr($zip, 0, strlen(postRequestParameter('zip'))) == postRequestParameter('zip')) {
498                                                                 // Ok, ZIP part is found
499                                                                 $userid_cnt++;
500                                                         } // END - if
501                                                 } else {
502                                                         // Count numbers up!
503                                                         $userid_cnt += SQL_NUMROWS($result_ver);
504                                                 }
505
506                                                 // Free result
507                                                 SQL_FREERESULT($result_ver);
508                                         } // END - if
509                                 } // END - while
510
511                                 // Free memory
512                                 SQL_FREERESULT($result_userids);
513                                 $categories['userids'][] = $userid_cnt;
514                         } // END - while
515
516                         // Free memory
517                         SQL_FREERESULT($result_cats);
518
519                         // Now we need to load the mail types...
520                         $result = SQL_QUERY("SELECT `id`, `price`, `payment`, `mail_title` FROM `{?_MYSQL_PREFIX?}_payments` ORDER BY `payment` ASC", __FILE__, __LINE__);
521
522                         $types = array();
523                         if (SQL_NUMROWS($result) > 0) {
524                                 // Check for message id in URL
525                                 $message = getMessageFromErrorCode(getRequestParameter('code'));
526
527                                 if (!empty($message)) {
528                                         // We got system message so we drop it out to the user
529                                         loadTemplate('admin_settings_saved', false, $message);
530                                 } // END - if
531
532                                 // Load all email types...
533                                 while ($types[] = SQL_FETCHROW($result)) {
534                                         // Nothing to do here... ;-)
535                                 } // END - while
536
537                                 // Free memory
538                                 SQL_FREERESULT($result);
539
540                                 // Output user's points
541                                 $content['total'] = translateComma($total);
542
543                                 // Check how many mail orders he has placed today and how many he's allowed to send
544                                 switch (getConfig('order_max_full')) {
545                                         case 'MAX': // He is allowed to send as much as possible
546                                                 $content['order_max_full'] = getMessage('MEMBER_ORDER_ALLOWED_MAX');
547                                                 break;
548
549                                         case 'ORDER': // He is allowed to send as much as he setup the receiving value
550                                                 $content['order_max_full'] = sprintf(getMessage('MEMBER_ORDER_ALLOWED_RECEIVE'), $ALLOWED, $MAXI);
551                                                 break;
552
553                                         default: // Unknown/invalid
554                                                 logDebugMessage(__FILE__, __LINE__, sprintf("Unknown order_mas_full config detected.", getConfig('order_max_full')));
555                                                 $content['order_max_full'] = getMessage('MEMBER_ORDER_ALLOWED_UNKNOWN');
556                                                 break;
557                                 } // END - switch
558
559                                 // Load final template
560                                 loadTemplate('member_order_points', false, $content);
561
562                                 // Reset variables
563                                 $OLD_ORDER = false;
564                                 $subject = '';
565                                 $text = '';
566                                 $target = '';
567
568                                 // Check if we already have an order placed and make it editable
569                                 $result = SQL_QUERY_ESC("SELECT
570         `subject`, `text`, `payment_id`, `timestamp`, `url`, `target_send`, `cat_id`, `zip`
571 FROM
572         `{?_MYSQL_PREFIX?}_pool`
573 WHERE
574         `sender`=%s AND
575         `data_type`='TEMP'
576 LIMIT 1",
577                                         array(getMemberId()), __FILE__, __LINE__);
578
579                                 if (SQL_NUMROWS($result) == 1) {
580                                         // Old order found
581                                         $content = merge_array($content, SQL_FETCHARRAY($result));
582
583                                         // Fix max receivers when it is too much
584                                         if ((isset($categories['userids'][$content['cat_id']])) && ($content['target_send'] > $categories['userids'][$content['cat_id']])) {
585                                                 // Fix it
586                                                 $content['target_send'] = $categories['userids'][$content['cat_id']];
587                                         } // END - if
588
589                                         // Old order is grabbed
590                                         $OLD_ORDER = true;
591                                 } else {
592                                         // Default output for that your members don't forget it...
593                                         $content['url']         = 'http://';
594                                         $content['target_send'] = getConfig('order_min');
595                                         $content['subject']     = '{--ORDER_DEFAULT_SUBJECT--}';
596                                         $content['text']        = '{--ORDER_DEFAULT_TEXT--}';
597                                 }
598
599                                 // Free result
600                                 SQL_FREERESULT($result);
601
602                                 if ((isPostRequestParameterSet('data')) || ((getConfig('order_multi_page') != 'Y') && ((!isAdmin()) && (!isExtensionActive('html_mail'))))) {
603                                         // Pre-output categories
604                                         $content['category_selection'] = '';
605                                         foreach ($categories['id'] as $key => $value) {
606                                                 $content['category_selection'] .= "      <option value=\"".$value."\"";
607                                                 if (($OLD_ORDER) && ($content['cat_id'] == $value)) $content['category_selection'] .= ' selected="selected"';
608                                                 $content['category_selection'] .= ">".$categories['name'][$key]." (".$categories['userids'][$key]." {--USER_IN_CAT--})</option>\n";
609                                         } // END - foreach
610
611                                         // Mail type
612                                         $content['type_selection'] = '';
613                                         foreach ($types as $key => $value) {
614                                                 $P = translateComma($types[$key][1]);
615                                                 if (is_array($value)) {
616                                                         // Output option line
617                                                         $content['type_selection'] .= "      <option value=\"".$types[$key][0]."\"";
618                                                         if (($OLD_ORDER) && ($content['payment_id'] == $types[$key][0])) $content['type_selection'] .= ' selected="selected"';
619                                                         $content['type_selection'] .= ">".$P." {--PER_MAIL--} - ".$types[$key][3]." - ".round($types[$key][2])." {--PAYMENT--}</option>\n";
620                                                 } // END - if
621                                         } // END - foreach
622
623                                         // No content is default
624                                         $content['zip_content'] = '';
625
626                                         if (isPostRequestParameterSet('zip')) {
627                                                 // Output entered ZIP code
628                                                 $content['zip_content'] = loadTemplate('member_order-zip2', true, postRequestParameter('zip'));
629                                         } // END - if
630
631                                         // HTML extension
632                                         if ((isExtensionActive('html_mail')) && (postRequestParameter('html') == 'Y')) {
633                                                 // Extension is active so output valid HTML tags
634                                                 $content['html_extension'] = loadTemplate('member_order-html_ext', true, addValidHtmlTags());
635                                         } else {
636                                                 // Extension not active and/or class not uploaded
637                                                 $content['html_extension'] = '<tr><td colspan="3"><input type="hidden" name="html" value="N" /></td></tr>';
638                                         }
639
640                                         // Output form for page 2
641                                         loadTemplate('member_order_page2', false, $content);
642                                 } else {
643                                         // Remember maybe entered ZIP code in constant
644                                         if (isExtensionActive('html_mail')) {
645                                                 // Add some content when html extension is active
646                                                 $content['html_extension'] = loadTemplate('member_order-html_intro', true);
647                                         } else {
648                                                 // No HTML extension installed
649                                                 $content['html_extension'] = '<tr><td colspan="3"><input type="hidden" name="html" value="N" /></td></tr>';
650                                         }
651
652                                         // Default is no ZIP code
653                                         $content['zip_content'] = '';
654
655                                         // Do we want ZIP code or not?
656                                         if ((getConfig('order_multi_page') == 'Y') || (isAdmin())) {
657                                                 // Yes
658                                                 if (postRequestParameter('zip') > 0) {
659                                                         $data = array(
660                                                                 'zip' => bigintval(postRequestParameter('zip'))
661                                                         );
662                                                 } else {
663                                                         $data = array(
664                                                                 'zip' => ''
665                                                         );
666                                                 }
667                                                 $content['zip_content'] = loadTemplate('member_order-zip1', true, $data);
668                                         } // END - if
669
670                                         // Output form for page 1 (ZIP code or HTML)
671                                         loadTemplate('member_order_page1', false, $content);
672                                 }
673                         } else {
674                                 // No mail types defined
675                                 loadTemplate('admin_settings_saved', false, '<span class="member_failed">{--MEMBER_NO_PAYMENTS--}</span>');
676                         }
677                 } else {
678                         // No points left
679                         loadTemplate('admin_settings_saved', false, '<span class="member_failed">{--MEMBER_NO_POINTS--}</span>');
680                 }
681         } else {
682                 // No cateogries are defined yet
683                 loadTemplate('admin_settings_saved', false, '<span class="member_failed">{--MEMBER_NO_CATS--}</span>');
684         }
685 } elseif ($mmails == '0') {
686         // Please set more than 0 mails per day
687         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_HAS_ZERO_MMAILS'));
688 } else {
689         // Please confirm some mails first
690         loadTemplate('admin_settings_saved', false, getMaskedMessage('MEMBER_LINKS_LEFT'), $links);
691 }
692
693 if (!empty($URL)) {
694         // Redirect to requested URL
695         redirectToUrl($URL);
696 } // END - if
697
698 // [EOF]
699 ?>