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