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